@@ -15,7 +15,6 @@ func TestGetEnvFloat(t *testing.T) {
15
15
tests := []struct {
16
16
name string
17
17
key string
18
- value string
19
18
defaultVal float64
20
19
expected float64
21
20
setup func ()
@@ -24,7 +23,6 @@ func TestGetEnvFloat(t *testing.T) {
24
23
{
25
24
name : "env variable exists and is valid" ,
26
25
key : "TEST_FLOAT" ,
27
- value : "123.456" ,
28
26
defaultVal : 0.0 ,
29
27
expected : 123.456 ,
30
28
setup : func () {
@@ -37,7 +35,6 @@ func TestGetEnvFloat(t *testing.T) {
37
35
{
38
36
name : "env variable exists but is invalid" ,
39
37
key : "TEST_FLOAT" ,
40
- value : "invalid" ,
41
38
defaultVal : 99.9 ,
42
39
expected : 99.9 ,
43
40
setup : func () {
@@ -76,7 +73,6 @@ func TestGetEnvDuration(t *testing.T) {
76
73
tests := []struct {
77
74
name string
78
75
key string
79
- value string
80
76
defaultVal time.Duration
81
77
expected time.Duration
82
78
setup func ()
@@ -85,7 +81,6 @@ func TestGetEnvDuration(t *testing.T) {
85
81
{
86
82
name : "env variable exists and is valid" ,
87
83
key : "TEST_DURATION" ,
88
- value : "1h30m" ,
89
84
defaultVal : 0 ,
90
85
expected : 1 * time .Hour + 30 * time .Minute ,
91
86
setup : func () {
@@ -98,7 +93,6 @@ func TestGetEnvDuration(t *testing.T) {
98
93
{
99
94
name : "env variable exists but is invalid" ,
100
95
key : "TEST_DURATION" ,
101
- value : "invalid-duration" ,
102
96
defaultVal : 5 * time .Minute ,
103
97
expected : 5 * time .Minute ,
104
98
setup : func () {
@@ -119,7 +113,6 @@ func TestGetEnvDuration(t *testing.T) {
119
113
{
120
114
name : "env variable is empty string" ,
121
115
key : "TEST_DURATION_EMPTY" ,
122
- value : "" ,
123
116
defaultVal : 1 * time .Millisecond ,
124
117
expected : 1 * time .Millisecond ,
125
118
setup : func () {
@@ -150,7 +143,6 @@ func TestGetEnvInt(t *testing.T) {
150
143
tests := []struct {
151
144
name string
152
145
key string
153
- value string
154
146
defaultVal int
155
147
expected int
156
148
setup func ()
@@ -159,7 +151,6 @@ func TestGetEnvInt(t *testing.T) {
159
151
{
160
152
name : "env variable exists and is valid" ,
161
153
key : "TEST_INT" ,
162
- value : "123" ,
163
154
defaultVal : 0 ,
164
155
expected : 123 ,
165
156
setup : func () {
@@ -172,7 +163,6 @@ func TestGetEnvInt(t *testing.T) {
172
163
{
173
164
name : "env variable exists but is invalid" ,
174
165
key : "TEST_INT" ,
175
- value : "invalid" ,
176
166
defaultVal : 99 ,
177
167
expected : 99 ,
178
168
setup : func () {
@@ -193,7 +183,6 @@ func TestGetEnvInt(t *testing.T) {
193
183
{
194
184
name : "env variable is empty string" ,
195
185
key : "TEST_INT_EMPTY" ,
196
- value : "" ,
197
186
defaultVal : 77 ,
198
187
expected : 77 ,
199
188
setup : func () {
@@ -218,13 +207,82 @@ func TestGetEnvInt(t *testing.T) {
218
207
}
219
208
}
220
209
210
+ func TestGetEnvBool (t * testing.T ) {
211
+ logger := testr .New (t )
212
+
213
+ tests := []struct {
214
+ name string
215
+ key string
216
+ defaultVal bool
217
+ expected bool
218
+ setup func ()
219
+ teardown func ()
220
+ }{
221
+ {
222
+ name : "env variable exists and is valid" ,
223
+ key : "TEST_BOOL" ,
224
+ defaultVal : false ,
225
+ expected : true ,
226
+ setup : func () {
227
+ os .Setenv ("TEST_BOOL" , "true" )
228
+ },
229
+ teardown : func () {
230
+ os .Unsetenv ("TEST_BOOL" )
231
+ },
232
+ },
233
+ {
234
+ name : "env variable exists but is invalid" ,
235
+ key : "TEST_BOOL" ,
236
+ defaultVal : false ,
237
+ expected : false ,
238
+ setup : func () {
239
+ os .Setenv ("TEST_BOOL" , "invalid" )
240
+ },
241
+ teardown : func () {
242
+ os .Unsetenv ("TEST_BOOL" )
243
+ },
244
+ },
245
+ {
246
+ name : "env variable does not exist" ,
247
+ key : "TEST_BOOL_MISSING" ,
248
+ defaultVal : false ,
249
+ expected : false ,
250
+ setup : func () {},
251
+ teardown : func () {},
252
+ },
253
+ {
254
+ name : "env variable is empty string" ,
255
+ key : "TEST_BOOL_EMPTY" ,
256
+ defaultVal : false ,
257
+ expected : false ,
258
+ setup : func () {
259
+ os .Setenv ("TEST_BOOL_EMPTY" , "" )
260
+ },
261
+ teardown : func () {
262
+ os .Unsetenv ("TEST_BOOL_EMPTY" )
263
+ },
264
+ },
265
+ }
266
+
267
+ for _ , tc := range tests {
268
+ t .Run (tc .name , func (t * testing.T ) {
269
+ tc .setup ()
270
+ defer tc .teardown ()
271
+
272
+ result := GetEnvBool (tc .key , tc .defaultVal , logger .V (logutil .VERBOSE ))
273
+ if result != tc .expected {
274
+ t .Errorf ("GetEnvBool(%s, %v) = %v, expected %v" , tc .key , tc .defaultVal , result , tc .expected )
275
+ }
276
+ })
277
+ }
278
+ }
279
+
221
280
func TestGetEnvString (t * testing.T ) {
222
281
logger := testr .New (t )
223
282
224
283
tests := []struct {
225
284
name string
226
285
key string
227
- value string
228
286
defaultVal string
229
287
expected string
230
288
setup func ()
@@ -233,7 +291,6 @@ func TestGetEnvString(t *testing.T) {
233
291
{
234
292
name : "env variable exists and is valid" ,
235
293
key : "TEST_STR" ,
236
- value : "123" ,
237
294
defaultVal : "default" ,
238
295
expected : "123" ,
239
296
setup : func () {
@@ -254,7 +311,6 @@ func TestGetEnvString(t *testing.T) {
254
311
{
255
312
name : "env variable is empty string" ,
256
313
key : "TEST_STR_EMPTY" ,
257
- value : "" ,
258
314
defaultVal : "default" ,
259
315
expected : "" ,
260
316
setup : func () {
0 commit comments