...

Source file src/sigs.k8s.io/release-utils/env/env_test.go

Documentation: sigs.k8s.io/release-utils/env

     1  /*
     2  Copyright 2019 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package env
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/require"
    23  
    24  	"sigs.k8s.io/release-utils/env/internal"
    25  	"sigs.k8s.io/release-utils/env/internal/internalfakes"
    26  )
    27  
    28  func TestDefault(t *testing.T) {
    29  	for _, tc := range []struct {
    30  		prepare      func(*internalfakes.FakeImpl)
    31  		defaultValue string
    32  		expected     string
    33  	}{
    34  		{ // default LookupEnvReturns empty string and false
    35  			prepare: func(mock *internalfakes.FakeImpl) {
    36  				mock.LookupEnvReturns("", false)
    37  			},
    38  			defaultValue: "default",
    39  			expected:     "default",
    40  		},
    41  		{ // default LookupEnvReturns empty string and true
    42  			prepare: func(mock *internalfakes.FakeImpl) {
    43  				mock.LookupEnvReturns("", true)
    44  			},
    45  			defaultValue: "default",
    46  			expected:     "default",
    47  		},
    48  		{ // default LookupEnvReturns string and false
    49  			prepare: func(mock *internalfakes.FakeImpl) {
    50  				mock.LookupEnvReturns("value", false)
    51  			},
    52  			defaultValue: "default",
    53  			expected:     "default",
    54  		},
    55  		{ // value is set
    56  			prepare: func(mock *internalfakes.FakeImpl) {
    57  				mock.LookupEnvReturns("value", true)
    58  			},
    59  			defaultValue: "default",
    60  			expected:     "value",
    61  		},
    62  	} {
    63  		mock := &internalfakes.FakeImpl{}
    64  		tc.prepare(mock)
    65  		internal.Impl = mock
    66  
    67  		res := Default("key", tc.defaultValue)
    68  		require.Equal(t, tc.expected, res)
    69  	}
    70  }
    71  
    72  func TestIsSet(t *testing.T) {
    73  	for _, tc := range []struct {
    74  		prepare  func(*internalfakes.FakeImpl)
    75  		expected bool
    76  	}{
    77  		{ // LookupEnvReturns false
    78  			prepare: func(mock *internalfakes.FakeImpl) {
    79  				mock.LookupEnvReturns("", false)
    80  			},
    81  			expected: false,
    82  		},
    83  		{ // LookupEnvReturns true
    84  			prepare: func(mock *internalfakes.FakeImpl) {
    85  				mock.LookupEnvReturns("", true)
    86  			},
    87  			expected: true,
    88  		},
    89  	} {
    90  		mock := &internalfakes.FakeImpl{}
    91  		tc.prepare(mock)
    92  		internal.Impl = mock
    93  
    94  		res := IsSet("key")
    95  		require.Equal(t, tc.expected, res)
    96  	}
    97  }
    98  

View as plain text