-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathenv_test.go
695 lines (567 loc) · 19.3 KB
/
env_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
package env
import (
"errors"
"fmt"
"math"
"os"
"strings"
"testing"
"time"
)
func TestEnvBool(t *testing.T) {
os.Setenv("PROP", "true")
config := struct {
Prop bool `env:"PROP"`
}{}
ErrorNil(t, Set(&config))
Equals(t, true, config.Prop)
}
func TestEnvIntegers(t *testing.T) {
os.Setenv("PROP", "123")
config := struct {
PropInt int `env:"PROP"`
PropInt8 int8 `env:"PROP"`
PropInt16 int16 `env:"PROP"`
PropInt32 int32 `env:"PROP"`
PropInt64 int64 `env:"PROP"`
}{}
ErrorNil(t, Set(&config))
Equals(t, int(123), config.PropInt)
Equals(t, int8(123), config.PropInt8)
Equals(t, int16(123), config.PropInt16)
Equals(t, int32(123), config.PropInt32)
Equals(t, int64(123), config.PropInt64)
}
func TestIntegerRanges(t *testing.T) {
testCases := []struct {
Prop8 int8
Prop16 int16
Prop32 int32
Prop64 int64
}{
{Prop8: math.MinInt8, Prop16: math.MinInt16, Prop32: math.MinInt32, Prop64: math.MinInt64},
{Prop8: math.MaxInt8, Prop16: math.MaxInt16, Prop32: math.MaxInt32, Prop64: math.MaxInt64},
}
for _, testCase := range testCases {
t.Run(fmt.Sprintf("%v", testCase), func(t *testing.T) {
os.Setenv("PROP8", fmt.Sprintf("%d", testCase.Prop8))
os.Setenv("PROP16", fmt.Sprintf("%d", testCase.Prop16))
os.Setenv("PROP32", fmt.Sprintf("%d", testCase.Prop32))
os.Setenv("PROP64", fmt.Sprintf("%d", testCase.Prop64))
config := struct {
Prop8 int8 `env:"PROP8"`
Prop16 int16 `env:"PROP16"`
Prop32 int32 `env:"PROP32"`
Prop64 int64 `env:"PROP64"`
}{}
ErrorNil(t, Set(&config))
Equals(t, testCase.Prop8, config.Prop8)
Equals(t, testCase.Prop16, config.Prop16)
Equals(t, testCase.Prop32, config.Prop32)
Equals(t, testCase.Prop64, config.Prop64)
})
}
}
func TestUnsignedIntegerRanges(t *testing.T) {
testCases := []struct {
Prop8 uint8
Prop16 uint16
Prop32 uint32
Prop64 uint64
}{
{Prop8: 0, Prop16: 0, Prop32: 0, Prop64: 0},
{Prop8: math.MaxUint8, Prop16: math.MaxUint16, Prop32: math.MaxUint32, Prop64: math.MaxUint64},
}
for _, testCase := range testCases {
t.Run(fmt.Sprintf("%v", testCase), func(t *testing.T) {
os.Setenv("PROP8", fmt.Sprintf("%d", testCase.Prop8))
os.Setenv("PROP16", fmt.Sprintf("%d", testCase.Prop16))
os.Setenv("PROP32", fmt.Sprintf("%d", testCase.Prop32))
os.Setenv("PROP64", fmt.Sprintf("%d", testCase.Prop64))
config := struct {
Prop8 uint8 `env:"PROP8"`
Prop16 uint16 `env:"PROP16"`
Prop32 uint32 `env:"PROP32"`
Prop64 uint64 `env:"PROP64"`
}{}
ErrorNil(t, Set(&config))
Equals(t, testCase.Prop8, config.Prop8)
Equals(t, testCase.Prop16, config.Prop16)
Equals(t, testCase.Prop32, config.Prop32)
Equals(t, testCase.Prop64, config.Prop64)
})
}
}
func TestUnsignedFloatRanges(t *testing.T) {
testCases := []struct {
Prop32 float32
Prop64 float64
}{
{Prop32: math.SmallestNonzeroFloat32, Prop64: math.SmallestNonzeroFloat64},
{Prop32: math.MaxFloat32, Prop64: math.MaxFloat64},
}
for _, testCase := range testCases {
t.Run(fmt.Sprintf("%v", testCase), func(t *testing.T) {
os.Setenv("PROP32", fmt.Sprintf("%g", testCase.Prop32))
os.Setenv("PROP64", fmt.Sprintf("%g", testCase.Prop64))
config := struct {
Prop32 float32 `env:"PROP32"`
Prop64 float64 `env:"PROP64"`
}{}
ErrorNil(t, Set(&config))
Equals(t, testCase.Prop32, config.Prop32)
Equals(t, testCase.Prop64, config.Prop64)
})
}
}
func TestEnvUnsignedIntegers(t *testing.T) {
os.Setenv("PROP", "123")
config := struct {
PropUint uint `env:"PROP"`
PropUint8 uint8 `env:"PROP"`
PropUint16 uint16 `env:"PROP"`
PropUint32 uint32 `env:"PROP"`
PropUint64 uint64 `env:"PROP"`
}{}
ErrorNil(t, Set(&config))
Equals(t, uint(123), config.PropUint)
Equals(t, uint8(123), config.PropUint8)
Equals(t, uint16(123), config.PropUint16)
Equals(t, uint32(123), config.PropUint32)
Equals(t, uint64(123), config.PropUint64)
}
func TestEnvFloats(t *testing.T) {
os.Setenv("PROP", "1.23")
config := struct {
PropFloat32 float32 `env:"PROP"`
PropFloat64 float64 `env:"PROP"`
}{}
ErrorNil(t, Set(&config))
Equals(t, float32(1.23), config.PropFloat32)
Equals(t, float64(1.23), config.PropFloat64)
}
func TestEnvString(t *testing.T) {
os.Setenv("PROP", "}D-Z2P£T!E*#zE=.gc@")
config := struct {
Prop string `env:"PROP"`
}{}
ErrorNil(t, Set(&config))
Equals(t, "}D-Z2P£T!E*#zE=.gc@", config.Prop)
}
func TestEnvDuration(t *testing.T) {
os.Setenv("PROP", "1m30s")
config := struct {
Prop time.Duration `env:"PROP"`
}{}
ErrorNil(t, Set(&config))
Equals(t, "1m30s", config.Prop.String())
}
func TestEnvUnsupportedType(t *testing.T) {
os.Setenv("PROP", "1")
config := struct {
Prop chan int `env:"PROP"`
}{}
err := Set(&config)
ErrorNotNil(t, err)
Equals(t, `error setting "Prop": chan is not supported`, err.Error())
}
func TestByteSlice(t *testing.T) {
os.Setenv("PROP", "hello")
config := struct {
Prop []byte `env:"PROP"`
}{}
err := Set(&config)
ErrorNil(t, err)
Equals(t, "hello", string(config.Prop))
}
func TestEnvBoolSlice(t *testing.T) {
os.Setenv("PROPS", "true, false, true")
config := struct {
Items []bool `env:"PROPS"`
}{}
ErrorNil(t, Set(&config))
Equals(t, []bool{true, false, true}, config.Items)
}
func TestEnvStringSlice(t *testing.T) {
os.Setenv("PROPS", "a, b, c")
config := struct {
Items []string `env:"PROPS"`
}{}
ErrorNil(t, Set(&config))
Equals(t, []string{"a", "b", "c"}, config.Items)
}
func TestEnvEmptyStringSlice(t *testing.T) {
os.Setenv("PROPS", "")
config := struct {
Items []string `env:"PROPS"`
}{}
ErrorNil(t, Set(&config))
Equals(t, 0, len(config.Items))
}
func TestEnvIntegerSlices(t *testing.T) {
os.Setenv("PROP", "1, 2, 3")
config := struct {
PropInt []int `env:"PROP"`
PropInt8 []int8 `env:"PROP"`
PropInt16 []int16 `env:"PROP"`
PropInt32 []int32 `env:"PROP"`
PropInt64 []int64 `env:"PROP"`
}{}
ErrorNil(t, Set(&config))
Equals(t, []int{1, 2, 3}, config.PropInt)
Equals(t, []int8{1, 2, 3}, config.PropInt8)
Equals(t, []int16{1, 2, 3}, config.PropInt16)
Equals(t, []int32{1, 2, 3}, config.PropInt32)
Equals(t, []int64{1, 2, 3}, config.PropInt64)
}
func TestEnvUnsignedIntegerSlices(t *testing.T) {
os.Setenv("PROP", "1, 2, 3")
config := struct {
PropUint []uint `env:"PROP"`
PropUint16 []uint16 `env:"PROP"`
PropUint32 []uint32 `env:"PROP"`
PropUint64 []uint64 `env:"PROP"`
}{}
ErrorNil(t, Set(&config))
Equals(t, []uint{1, 2, 3}, config.PropUint)
Equals(t, []uint16{1, 2, 3}, config.PropUint16)
Equals(t, []uint32{1, 2, 3}, config.PropUint32)
Equals(t, []uint64{1, 2, 3}, config.PropUint64)
}
func TestEnvFloatSlices(t *testing.T) {
os.Setenv("PROP", "1.23, 2.34, 3.45")
config := struct {
PropFloat32 []float32 `env:"PROP"`
PropFloat64 []float64 `env:"PROP"`
}{}
ErrorNil(t, Set(&config))
Equals(t, []float32{1.23, 2.34, 3.45}, config.PropFloat32)
Equals(t, []float64{1.23, 2.34, 3.45}, config.PropFloat64)
}
func TestEnvDurationSlice(t *testing.T) {
os.Setenv("PROP", "1s, 2s, 4s")
config := struct {
Prop []time.Duration `env:"PROP"`
}{}
ErrorNil(t, Set(&config))
Equals(t, []time.Duration{time.Second, time.Second * 2, time.Second * 4}, config.Prop)
}
func TestEnvUnsupportedBoolSlice(t *testing.T) {
os.Setenv("PROPS", "true, false, true")
config := struct {
Items []chan int `env:"PROPS"`
}{}
err := Set(&config)
ErrorNotNil(t, err)
Assert(t, "[]chan int is not supported" == err.Error())
}
func TestEnvSetUnexportedProperty(t *testing.T) {
os.Setenv("PROP", "hello")
config := struct {
prop string `env:"PROP"`
}{}
err := Set(&config)
ErrorNotNil(t, err)
Assert(t, strings.Contains(err.Error(), "field 'prop' cannot be set"))
}
func TestEnvUnsupportedPropertyWithoutTag(t *testing.T) {
os.Setenv("PROP", "hello")
config := struct {
Prop string `env:"PROP"`
Prop2 chan int
}{}
ErrorNil(t, Set(&config))
Equals(t, "hello", config.Prop)
}
func TestInvalidValueForRequiredTag(t *testing.T) {
os.Unsetenv("PROP")
config := struct {
Prop int `env:"PROP" required:"invalid"`
}{}
err := Set(&config)
ErrorNotNil(t, err)
Equals(t, `invalid required tag "invalid": strconv.ParseBool: parsing "invalid": invalid syntax`, err.Error())
}
func TestEnvNoEnvTag(t *testing.T) {
config := struct {
Prop string
}{}
ErrorNil(t, Set(&config))
}
func TestEnvRequiredWhenProvided(t *testing.T) {
os.Setenv("PROP", "hello")
config := struct {
Prop string `env:"PROP" required:"true"`
}{}
ErrorNil(t, Set(&config))
Equals(t, "hello", config.Prop)
}
func TestEnvRequiredWhenMissing(t *testing.T) {
config := struct {
Prop string `env:"MISSING_PROP" required:"true"`
}{}
err := Set(&config)
ErrorNotNil(t, err)
}
func TestEnvWithDefaultWhenProvided(t *testing.T) {
os.Setenv("PROP", "goodbye")
config := struct {
Prop string `env:"PROP" default:"hello"`
}{}
ErrorNil(t, Set(&config))
Equals(t, "goodbye", config.Prop)
}
func TestEnvWithDefaultWhenMissing(t *testing.T) {
unsetEnvironment()
config := struct {
BoolProp bool `env:"MISSING" default:"true"`
BoolsProp []bool `env:"MISSING" default:"true,false,true"`
StringProp string `env:"MISSING" default:"abc"`
StringsProp []string `env:"MISSING" default:"a,b,c"`
BytesProp []byte `env:"MISSING" default:"abc"`
IntProp int `env:"MISSING" default:"1"`
IntsProp []int `env:"MISSING" default:"1,2,3"`
Int8Prop int8 `env:"MISSING" default:"1"`
Int8sProp []int8 `env:"MISSING" default:"1,2,3"`
Int16Prop int16 `env:"MISSING" default:"1"`
Int16sProp []int16 `env:"MISSING" default:"1,2,3"`
Int32Prop int32 `env:"MISSING" default:"1"`
Int32sProp []int32 `env:"MISSING" default:"1,2,3"`
Int64Prop int64 `env:"MISSING" default:"1"`
Int64sProp []int64 `env:"MISSING" default:"1,2,3"`
UIntProp uint `env:"MISSING" default:"1"`
UIntsProp []uint `env:"MISSING" default:"1,2,3"`
UInt16Prop uint16 `env:"MISSING" default:"1"`
UInt16sProp []uint16 `env:"MISSING" default:"1,2,3"`
UInt32Prop uint32 `env:"MISSING" default:"1"`
UInt32sProp []uint32 `env:"MISSING" default:"1,2,3"`
UInt64Prop uint64 `env:"MISSING" default:"1"`
UInt64sProp []uint64 `env:"MISSING" default:"1,2,3"`
Float32Prop float32 `env:"MISSING" default:"1.1"`
Float32sProp []float32 `env:"MISSING" default:"1.1,2.2,3.3"`
Float64Prop float64 `env:"MISSING" default:"1.1"`
Float64sProp []float64 `env:"MISSING" default:"1.1,2.2,3.3"`
DurationProp time.Duration `env:"MISSING" default:"1h2m3s"`
DurationsProp []time.Duration `env:"MISING" default:"1h,2m,3s"`
}{}
ErrorNil(t, Set(&config))
Equals(t, true, config.BoolProp)
Equals(t, []bool{true, false, true}, config.BoolsProp)
Equals(t, "abc", config.StringProp)
Equals(t, []string{"a", "b", "c"}, config.StringsProp)
Equals(t, []byte("abc"), config.BytesProp)
Equals(t, 1, config.IntProp)
Equals(t, []int{1, 2, 3}, config.IntsProp)
Equals(t, int8(1), config.Int8Prop)
Equals(t, []int8{1, 2, 3}, config.Int8sProp)
Equals(t, int16(1), config.Int16Prop)
Equals(t, []int16{1, 2, 3}, config.Int16sProp)
Equals(t, int32(1), config.Int32Prop)
Equals(t, []int32{1, 2, 3}, config.Int32sProp)
Equals(t, int64(1), config.Int64Prop)
Equals(t, []int64{1, 2, 3}, config.Int64sProp)
Equals(t, 1, config.IntProp)
Equals(t, []uint{1, 2, 3}, config.UIntsProp)
Equals(t, uint16(1), config.UInt16Prop)
Equals(t, []uint16{1, 2, 3}, config.UInt16sProp)
Equals(t, uint32(1), config.UInt32Prop)
Equals(t, []uint32{1, 2, 3}, config.UInt32sProp)
Equals(t, uint64(1), config.UInt64Prop)
Equals(t, []uint64{1, 2, 3}, config.UInt64sProp)
Equals(t, float32(1.1), config.Float32Prop)
Equals(t, []float32{1.1, 2.2, 3.3}, config.Float32sProp)
Equals(t, float64(1.1), config.Float64Prop)
Equals(t, []float64{1.1, 2.2, 3.3}, config.Float64sProp)
Equals(t, time.Hour+time.Minute*2+time.Second*3, config.DurationProp)
Equals(t, []time.Duration{time.Hour, time.Minute * 2, time.Second * 3}, config.DurationsProp)
}
func TestEnvRequiredWithDefaultWhenMissing(t *testing.T) {
unsetEnvironment()
config := struct {
BoolProp bool `env:"MISSING" required:"true" default:"true"`
BoolsProp []bool `env:"MISSING" required:"true" default:"true,false,true"`
StringProp string `env:"MISSING" required:"true" default:"a"`
StringsProp []string `env:"MISSING" required:"true" default:"a,b,c"`
BytesProp []byte `env:"MISSING" required:"true" default:"a"`
IntProp int `env:"MISSING" required:"true" default:"1"`
IntsProp []int `env:"MISSING" required:"true" default:"1,2,3"`
Int8Prop int8 `env:"MISSING" required:"true" default:"1"`
Int8sProp []int8 `env:"MISSING" required:"true" default:"1,2,3"`
Int16Prop int16 `env:"MISSING" required:"true" default:"1"`
Int16sProp []int16 `env:"MISSING" required:"true" default:"1,2,3"`
Int32Prop int32 `env:"MISSING" required:"true" default:"1"`
Int32sProp []int32 `env:"MISSING" required:"true" default:"1,2,3"`
Int64Prop int64 `env:"MISSING" required:"true" default:"1"`
Int64sProp []int64 `env:"MISSING" required:"true" default:"1,2,3"`
UIntProp uint `env:"MISSING" required:"true" default:"1"`
UIntsProp []uint `env:"MISSING" required:"true" default:"1,2,3"`
UInt16Prop uint16 `env:"MISSING" required:"true" default:"1"`
UInt16sProp []uint16 `env:"MISSING" required:"true" default:"1,2,3"`
UInt32Prop uint32 `env:"MISSING" required:"true" default:"1"`
UInt32sProp []uint32 `env:"MISSING" required:"true" default:"1,2,3"`
UInt64Prop uint64 `env:"MISSING" required:"true" default:"1"`
UInt64sProp []uint64 `env:"MISSING" required:"true" default:"1,2,3"`
Float32Prop float32 `env:"MISSING" required:"true" default:"1.1"`
Float32sProp []float32 `env:"MISSING" required:"true" default:"1.1,2.2,3.3"`
Float64Prop float64 `env:"MISSING" required:"true" default:"1.1"`
Float64sProp []float64 `env:"MISSING" required:"true" default:"1.1,2.2,3.3"`
DurationProp time.Duration `env:"MISSING" required:"true" default:"1h2m3s"`
DurationsProp []time.Duration `env:"MISSING" required:"true" default:"1h,2m,3s"`
}{}
ErrorNil(t, Set(&config))
Equals(t, true, config.BoolProp)
Equals(t, []bool{true, false, true}, config.BoolsProp)
Equals(t, "a", config.StringProp)
Equals(t, []string{"a", "b", "c"}, config.StringsProp)
Equals(t, []byte("a"), config.BytesProp)
Equals(t, 1, config.IntProp)
Equals(t, []int{1, 2, 3}, config.IntsProp)
Equals(t, int8(1), config.Int8Prop)
Equals(t, []int8{1, 2, 3}, config.Int8sProp)
Equals(t, int16(1), config.Int16Prop)
Equals(t, []int16{1, 2, 3}, config.Int16sProp)
Equals(t, int32(1), config.Int32Prop)
Equals(t, []int32{1, 2, 3}, config.Int32sProp)
Equals(t, int64(1), config.Int64Prop)
Equals(t, []int64{1, 2, 3}, config.Int64sProp)
Equals(t, 1, config.IntProp)
Equals(t, []uint{1, 2, 3}, config.UIntsProp)
Equals(t, uint16(1), config.UInt16Prop)
Equals(t, []uint16{1, 2, 3}, config.UInt16sProp)
Equals(t, uint32(1), config.UInt32Prop)
Equals(t, []uint32{1, 2, 3}, config.UInt32sProp)
Equals(t, uint64(1), config.UInt64Prop)
Equals(t, []uint64{1, 2, 3}, config.UInt64sProp)
Equals(t, float32(1.1), config.Float32Prop)
Equals(t, []float32{1.1, 2.2, 3.3}, config.Float32sProp)
Equals(t, float64(1.1), config.Float64Prop)
Equals(t, []float64{1.1, 2.2, 3.3}, config.Float64sProp)
Equals(t, time.Hour+time.Minute*2+time.Second*3, config.DurationProp)
Equals(t, []time.Duration{time.Hour, time.Minute * 2, time.Second * 3}, config.DurationsProp)
}
func TestEnvCustomDelimiter(t *testing.T) {
os.Setenv("PROP", "a b c")
config := struct {
Prop []string `env:"PROP" delimiter:" "`
}{}
ErrorNil(t, Set(&config))
Equals(t, []string{"a", "b", "c"}, config.Prop)
}
func TestEnvNotRequiredImplicitWhenMissing(t *testing.T) {
os.Unsetenv("PROP")
config := struct {
Prop string `env:"PROP"`
}{}
err := Set(&config)
ErrorNil(t, err)
}
func TestEnvNotRequiredExplicitWhenMissing(t *testing.T) {
os.Unsetenv("PROP")
config := struct {
Prop string `env:"PROP" required:"false"`
}{}
err := Set(&config)
ErrorNil(t, err)
}
func TestInvalidConfigurationForBoolType(t *testing.T) {
os.Setenv("PROP", "hello")
config := struct {
Prop bool `env:"PROP"`
}{}
err := Set(&config)
ErrorNotNil(t, err)
Equals(t, `error setting "Prop": strconv.ParseBool: parsing "hello": invalid syntax`, err.Error())
}
func TestInvalidConfigurationForIntType(t *testing.T) {
os.Setenv("PROP", "hello")
config := struct {
Prop int `env:"PROP"`
}{}
err := Set(&config)
ErrorNotNil(t, err)
Equals(t, `error setting "Prop": strconv.ParseInt: parsing "hello": invalid syntax`, err.Error())
}
func TestInvalidConfigurationForUintType(t *testing.T) {
os.Setenv("PROP", "hello")
config := struct {
Prop uint `env:"PROP"`
}{}
err := Set(&config)
ErrorNotNil(t, err)
Equals(t, `error setting "Prop": strconv.ParseUint: parsing "hello": invalid syntax`, err.Error())
}
func TestInvalidConfigurationForFloatType(t *testing.T) {
os.Setenv("PROP", "hello")
config := struct {
Prop float32 `env:"PROP"`
}{}
err := Set(&config)
ErrorNotNil(t, err)
Equals(t, `error setting "Prop": strconv.ParseFloat: parsing "hello": invalid syntax`, err.Error())
}
func TestInvalidConfigurationForDuration(t *testing.T) {
os.Setenv("PROP", "1hh")
config := struct {
Prop time.Duration `env:"PROP"`
}{}
err := Set(&config)
ErrorNotNil(t, err)
Equals(t, `error setting "Prop": time: unknown unit "hh" in duration "1hh"`, err.Error())
}
func TestEnvNonPointer(t *testing.T) {
config := struct {
Prop float32 `env:"PROP"`
}{}
err := Set(config)
ErrorNotNil(t, err)
Equals(t, err.Error(), "struct is not a pointer")
}
func TestEnvCustomTypeAliasedPrimitiveWithoutSetter(t *testing.T) {
os.Setenv("PROP", "1234")
config := struct {
Prop myInt `env:"PROP"`
}{}
ErrorNil(t, Set(&config))
Equals(t, myInt(1234), config.Prop)
}
type myInt int16
func TestEnvCustomTypeStruct(t *testing.T) {
os.Setenv("PROP", "3h2m1s")
config := struct {
Timeout *configDuration `env:"PROP"`
}{}
ErrorNil(t, Set(&config))
Equals(t, time.Hour*3+time.Minute*2+time.Second*1, config.Timeout.Duration)
}
func TestEnvCustomTypeStructWithError(t *testing.T) {
os.Setenv("PROP", "3h2m1s")
config := struct {
Timeout *configDurationError `env:"PROP"`
}{}
err := Set(&config)
ErrorNotNil(t, err)
Assert(t, strings.HasPrefix(err.Error(), "error in custom setter"))
Assert(t, strings.Contains(err.Error(), errConfigDurationError.Error()))
}
func TestEnvPrefixed(t *testing.T) {
os.Setenv("PROP_PROP", "hello")
config := struct {
Prop string `env:"PROP"`
}{}
ErrorNil(t, SetPrefix(&config, "PROP_"))
Equals(t, "hello", config.Prop)
}
type configDuration struct {
Duration time.Duration
}
func (d *configDuration) Set(config string) (err error) {
d.Duration, err = time.ParseDuration(config)
return
}
type configDurationError struct {
Duration time.Duration
}
var errConfigDurationError = errors.New("example error from custom Set code")
func (d *configDurationError) Set(config string) (err error) {
return errConfigDurationError
}