-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstev_test.go
711 lines (656 loc) · 15.1 KB
/
stev_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
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
package stev_test
import (
"errors"
"os"
"strings"
"testing"
"time"
"github.com/rez-go/stev"
)
func TestNonPtr(t *testing.T) {
os.Clearenv()
var val int
err := stev.LoadEnv("", val)
if err == nil {
t.Errorf("Expected error")
}
}
func TestEmpty(t *testing.T) {
os.Clearenv()
cfg := struct{}{}
err := stev.LoadEnv("", &cfg)
if err != nil {
t.Errorf("Expected nil, got %#v", err)
}
}
func TestNilTarget(t *testing.T) {
os.Clearenv()
var cfg *struct{}
err := stev.LoadEnv("", &cfg)
if err != nil {
t.Errorf("Expected nil, got %#v", err)
}
}
func TestUnexportedField(t *testing.T) {
os.Clearenv()
cfg := struct {
myField string `env:"MY_FIELD"`
}{}
err := stev.LoadEnv("", &cfg)
if err != nil {
t.Errorf("Expected nil, got %#v", err)
}
}
func TestString(t *testing.T) {
os.Clearenv()
cfg := struct {
Name string `env:"NAME"`
}{}
err := stev.LoadEnv("", &cfg)
if err != nil {
t.Errorf("Expected nil, got %#v", err)
}
if cfg.Name != "" {
t.Errorf(`Expected "" got %q`, cfg.Name)
}
}
func TestFieldNames(t *testing.T) {
os.Clearenv()
cfg := struct {
Name string
REST string
APIVersion string
ServerURL string
ModuleName string
MinAPIVersion string
Area51 string
IPV4Address string
}{}
os.Setenv("NAME", "Name")
os.Setenv("REST", "REST")
os.Setenv("API_VERSION", "APIVersion")
os.Setenv("SERVER_URL", "ServerURL")
os.Setenv("MODULE_NAME", "ModuleName")
os.Setenv("MIN_API_VERSION", "MinAPIVersion")
os.Setenv("AREA_51", "Area51")
os.Setenv("IPV4_ADDRESS", "IPV4Address")
err := stev.LoadEnv("", &cfg)
if err != nil {
t.Errorf("Expected nil, got %#v", err)
}
if cfg.Name != "Name" {
t.Errorf(`Unexpected value`)
}
if cfg.REST != "REST" {
t.Errorf(`Unexpected value`)
}
if cfg.APIVersion != "APIVersion" {
t.Errorf(`Unexpected value`)
}
if cfg.ServerURL != "ServerURL" {
t.Errorf(`Unexpected value`)
}
if cfg.ModuleName != "ModuleName" {
t.Errorf(`Assertion failed`)
}
if cfg.MinAPIVersion != "MinAPIVersion" {
t.Errorf(`Assertion failed`)
}
if cfg.Area51 != "Area51" {
t.Errorf(`Assertion failed`)
}
if cfg.IPV4Address != "IPV4Address" {
t.Errorf(`Assertion failed`)
}
}
func TestStringWithValue(t *testing.T) {
os.Clearenv()
cfg := struct {
Name string `env:"NAME"`
}{}
os.Setenv("NAME", "Go")
err := stev.LoadEnv("", &cfg)
if err != nil {
t.Errorf("Expected nil, got %#v", err)
}
if cfg.Name != "Go" {
t.Errorf(`Expected "Go" got %q`, cfg.Name)
}
}
func TestStringWithPrefix(t *testing.T) {
os.Clearenv()
cfg := struct {
Name string `env:"NAME"`
}{}
os.Setenv("NAME", "Go")
os.Setenv("PFX_NAME", "Prefixed Go")
err := stev.LoadEnv("PFX_", &cfg)
if err != nil {
t.Errorf("Expected nil, got %#v", err)
}
if cfg.Name != "Prefixed Go" {
t.Errorf(`Expected "Prefixed Go" got %q`, cfg.Name)
}
}
func TestStringRequiredNoValue(t *testing.T) {
os.Clearenv()
cfg := struct {
Name string `env:"NAME,required"`
}{}
err := stev.LoadEnv("", &cfg)
if err == nil {
t.Errorf(`Expected error`)
}
}
func TestStringRequiredWithValue(t *testing.T) {
os.Clearenv()
cfg := struct {
Name string `env:"NAME,required"`
}{}
os.Setenv("NAME", "Go")
err := stev.LoadEnv("", &cfg)
if err != nil {
t.Errorf("Expected nil, got %#v", err)
}
if cfg.Name != "Go" {
t.Errorf(`Expected "Go" got %q`, cfg.Name)
}
}
type NameOnly struct {
Name string `env:"NAME"`
}
func TestStringWithValueNilTarget(t *testing.T) {
os.Clearenv()
var cfg *NameOnly
os.Setenv("NAME", "Go")
err := stev.LoadEnv("", cfg)
if err == nil {
t.Errorf("Expected error")
}
}
func TestNameOnlyWithDefaultTargetPointer(t *testing.T) {
os.Clearenv()
cfg := &NameOnly{Name: "Not GO"}
err := stev.LoadEnv("", &cfg)
if err != nil {
t.Errorf("Expected nil, got %#v", err)
}
if cfg.Name != "Not GO" {
t.Errorf(`Expected "Not GO" got %q`, cfg.Name)
}
}
func TestNameOnlyWithDefaultTargetPointerValidValue(t *testing.T) {
os.Clearenv()
cfg := &NameOnly{Name: "Not GO"}
os.Setenv("NAME", "Go")
err := stev.LoadEnv("", &cfg)
if err != nil {
t.Errorf("Expected nil, got %#v", err)
}
if cfg.Name != "Go" {
t.Errorf(`Expected "Go" got %q`, cfg.Name)
}
}
func TestNameOnlyWithDoubleNilTargetValid(t *testing.T) {
os.Clearenv()
var cfg *NameOnly
os.Setenv("NAME", "Go")
err := stev.LoadEnv("", &cfg)
if err != nil {
t.Errorf("Expected nil, got %#v", err)
}
if cfg.Name != "Go" {
t.Errorf(`Expected "Go" got %q`, cfg.Name)
}
}
type RequiredName struct {
Name string `env:",required"`
}
func TestRequiredTargetPointerNil(t *testing.T) {
os.Clearenv()
var cfg *RequiredName
err := stev.LoadEnv("", &cfg)
if err != nil {
t.Errorf("Expected nil, got %#v", err)
}
if cfg != nil {
t.Errorf(`Expected nil, got %#v`, cfg)
}
}
func TestRequiredTargetPointerNonNil(t *testing.T) {
os.Clearenv()
cfg := &RequiredName{}
err := stev.LoadEnv("", &cfg)
if err == nil {
t.Errorf("Expected error")
}
}
func TestStringDefault(t *testing.T) {
os.Clearenv()
cfg := struct {
Name string `env:"NAME"`
}{Name: "Not GO"}
err := stev.LoadEnv("", &cfg)
if err != nil {
t.Errorf("Expected nil, got %#v", err)
}
if cfg.Name != "Not GO" {
t.Errorf(`Expected "Not GO" got %q`, cfg.Name)
}
}
func TestStringUntaggedWithValue(t *testing.T) {
os.Clearenv()
cfg := struct {
Name string
}{}
os.Setenv("NAME", "Go")
err := stev.LoadEnv("", &cfg)
if err != nil {
t.Errorf("Expected nil, got %#v", err)
}
if cfg.Name != "Go" {
t.Errorf(`Expected "Go" got %q`, cfg.Name)
}
}
func TestStringWithValueIgnore(t *testing.T) {
os.Clearenv()
cfg := struct {
Name string `env:"-"`
}{}
os.Setenv("NAME", "Go")
err := stev.LoadEnv("", &cfg)
if err != nil {
t.Errorf("Expected nil, got %#v", err)
}
if cfg.Name != "" {
t.Errorf(`Expected "" got %q`, cfg.Name)
}
}
func TestStringAnonymous(t *testing.T) {
os.Clearenv()
cfg := struct {
Name string `env:",squash"`
}{}
err := stev.LoadEnv("", &cfg)
if err == nil {
t.Errorf("Expected error")
}
}
func TestStringUntagged(t *testing.T) {
os.Clearenv()
cfg := struct {
Name string
}{}
err := stev.LoadEnv("", &cfg)
if err != nil {
t.Errorf("Expected nil, got %#v", err)
}
if cfg.Name != "" {
t.Errorf(`Expected "" got %q`, cfg.Name)
}
}
func TestBoolUntaggedNoValue(t *testing.T) {
os.Clearenv()
cfg := struct {
Enabled bool
}{}
err := stev.LoadEnv("", &cfg)
if err != nil {
t.Errorf("Expected nil, got %#v", err)
}
if cfg.Enabled {
t.Errorf(`Unexpected value`)
}
}
func TestBoolUntaggedImplicitValue(t *testing.T) {
os.Clearenv()
cfg := struct {
Enabled bool
}{}
os.Setenv("ENABLED", "")
err := stev.LoadEnv("", &cfg)
if err != nil {
t.Errorf("Expected nil, got %#v", err)
}
if !cfg.Enabled {
t.Errorf(`Unexpected value`)
}
}
func TestBoolUntaggedTrue(t *testing.T) {
os.Clearenv()
cfg := struct {
Enabled bool
}{}
os.Setenv("ENABLED", "true")
err := stev.LoadEnv("", &cfg)
if err != nil {
t.Errorf("Expected nil, got %#v", err)
}
if !cfg.Enabled {
t.Errorf(`Unexpected value`)
}
}
func TestBoolUntaggedFalse(t *testing.T) {
os.Clearenv()
cfg := struct {
Enabled bool
}{}
os.Setenv("ENABLED", "false")
err := stev.LoadEnv("", &cfg)
if err != nil {
t.Errorf("Expected nil, got %#v", err)
}
if cfg.Enabled {
t.Errorf(`Unexpected value`)
}
}
type InnerStruct struct {
Color string
Size int64
Strength uint32
AspectRatio float32
}
type OuterStruct struct {
Name string
NamePtr *string
NamePtr2 *string
NamePtr3 *string
Inner InnerStruct
}
type OuterStructEmbeddedInner struct {
Name string
InnerStruct
}
type OuterStructForcedAnonInner struct {
Name string
Inner InnerStruct `env:",squash"`
}
type InnerPrefix struct {
Color string
Size int64 `env:"!ABSOLUTE_SIZE"`
}
type OuterStructNoPrefixInner struct {
Name string
Description string `env:"!ABSOLUTE_DESC"`
WithPrefix InnerPrefix `env:"WITH"`
WithoutPrefix InnerPrefix `env:"!WITHOUT"`
WithPtr *InnerPrefix `env:"PTR"`
//TODO: test squash, anon and ptr
}
func TestPrefixes(t *testing.T) {
os.Clearenv()
os.Setenv("NAME", "Go (no prefix)")
os.Setenv("PFX_NAME", "Go")
os.Setenv("ABSOLUTE_DESC", "Description")
os.Setenv("PFX_ABSOLUTE_DESC", "Description (prefixed)")
os.Setenv("COLOR", "BLACK")
os.Setenv("PFX_COLOR", "WHITE")
os.Setenv("PFX_WITH_COLOR", "RED")
os.Setenv("WITHOUT_COLOR", "BLUE")
os.Setenv("PFX_WITHOUT_COLOR", "GREEN")
os.Setenv("ABSOLUTE_SIZE", "9001")
os.Setenv("PFX_WITH_ABSOLUTE_SIZE", "9002")
os.Setenv("PFX_PTR_COLOR", "ORANGE")
cfg := OuterStructNoPrefixInner{}
err := stev.LoadEnv("PFX_", &cfg)
if err != nil {
t.Errorf("Expected nil, got %#v", err)
}
assertStrEq(t, cfg.Name, "Go")
assertStrEq(t, cfg.Description, "Description")
assertStrEq(t, cfg.WithPrefix.Color, "RED")
assertInt64Eq(t, cfg.WithPrefix.Size, 9001)
assertStrEq(t, cfg.WithoutPrefix.Color, "BLUE")
assertInt64Eq(t, cfg.WithoutPrefix.Size, 9001)
assertStrEq(t, cfg.WithPtr.Color, "ORANGE")
assertInt64Eq(t, cfg.WithPtr.Size, 9001)
}
func TestStructUntagged(t *testing.T) {
os.Clearenv()
cfg := OuterStruct{}
err := stev.LoadEnv("", &cfg)
if err != nil {
t.Errorf("Expected nil, got %#v", err)
}
if cfg.Name != "" {
t.Errorf(`Unexpected value`)
}
if cfg.Inner.Color != "" {
t.Errorf(`Unexpected value`)
}
}
func TestStructUntaggedValidValues(t *testing.T) {
os.Clearenv()
ptr2 := "Default String 2"
ptr3 := "Default String 3"
cfg := OuterStruct{
NamePtr2: &ptr2,
NamePtr3: &ptr3,
}
os.Setenv("NAME", "Go")
os.Setenv("NAME_PTR", "Pointer to String: The Second Link")
os.Setenv("NAME_PTR_3", "Overriden String")
os.Setenv("INNER_COLOR", "RED")
os.Setenv("INNER_SIZE", "10")
os.Setenv("INNER_STRENGTH", "9001")
err := stev.LoadEnv("", &cfg)
if err != nil {
t.Errorf("Expected nil, got %#v", err)
}
if cfg.Name != "Go" {
t.Errorf(`Unexpected value`)
}
if *cfg.NamePtr != "Pointer to String: The Second Link" {
t.Errorf(`Assertion failed`)
}
if *cfg.NamePtr2 != "Default String 2" {
t.Errorf(`Assertion failed`)
}
if *cfg.NamePtr3 != "Overriden String" {
t.Errorf(`Assertion failed`)
}
if cfg.Inner.Color != "RED" {
t.Errorf(`Unexpected value`)
}
if cfg.Inner.Size != 10 {
t.Errorf(`Assertion failed`)
}
if cfg.Inner.Strength != 9001 {
t.Errorf(`Assertion failed`)
}
}
func TestEmbeddedStructUntaggedValidValues(t *testing.T) {
os.Clearenv()
cfg := OuterStructEmbeddedInner{}
os.Setenv("NAME", "Go")
os.Setenv("COLOR", "RED")
os.Setenv("ASPECT_RATIO", "1.3333")
err := stev.LoadEnv("", &cfg)
if err != nil {
t.Errorf("Expected nil, got %#v", err)
}
if cfg.Name != "Go" {
t.Errorf(`Unexpected value`)
}
if cfg.Color != "RED" {
t.Errorf(`Unexpected value`)
}
if cfg.AspectRatio != 1.3333 {
t.Errorf(`Assertion failed`)
}
}
func TestStructForcedAnonInnerUntaggedValidValues(t *testing.T) {
os.Clearenv()
cfg := OuterStructForcedAnonInner{}
os.Setenv("NAME", "Go")
os.Setenv("COLOR", "RED")
err := stev.LoadEnv("", &cfg)
if err != nil {
t.Errorf("Expected nil, got %#v", err)
}
if cfg.Name != "Go" {
t.Errorf(`Unexpected value`)
}
if cfg.Inner.Color != "RED" {
t.Errorf(`Unexpected value`)
}
}
type ModularConfig struct {
Modules map[string]interface{} `env:"MOD,map"`
}
type ModularSquashedConfig struct {
Modules map[string]interface{} `env:",map,squash"`
}
type AuthModuleConfig struct {
ClientID string `env:",required"`
}
type FilesModuleConfig struct {
Directory string `env:",required"`
}
func TestModularEmpty(t *testing.T) {
os.Clearenv()
cfg := ModularConfig{}
err := stev.LoadEnv("", &cfg)
if err != nil {
t.Errorf("Expected nil, got %#v", err)
}
}
func TestModular(t *testing.T) {
os.Clearenv()
cfg := ModularSquashedConfig{
Modules: map[string]interface{}{
"auth": &AuthModuleConfig{},
"files": &FilesModuleConfig{},
},
}
os.Setenv("MOD_AUTH_CLIENT_ID", "home")
os.Setenv("MOD_FILES_DIRECTORY", "/home")
os.Setenv("AUTH_CLIENT_ID", "root")
os.Setenv("FILES_DIRECTORY", "/root")
err := stev.LoadEnv("", &cfg)
if err != nil {
unwrappedErr := errors.Unwrap(err)
t.Errorf("Expected nil, got %#v (%#v)", err, unwrappedErr)
}
authCfg := cfg.Modules["auth"].(*AuthModuleConfig)
if authCfg.ClientID != "root" {
t.Errorf(`Unexpected value`)
}
filesCfg := cfg.Modules["files"].(*FilesModuleConfig)
if filesCfg.Directory != "/root" {
t.Errorf(`Unexpected value`)
}
}
func TestModularSquash(t *testing.T) {
os.Clearenv()
cfg := ModularConfig{
Modules: map[string]interface{}{
"auth": &AuthModuleConfig{},
"files": &FilesModuleConfig{},
},
}
os.Setenv("MOD_AUTH_CLIENT_ID", "root")
os.Setenv("MOD_FILES_DIRECTORY", "/root")
err := stev.LoadEnv("", &cfg)
if err != nil {
unwrappedErr := errors.Unwrap(err)
t.Errorf("Expected nil, got %#v (%#v)", err, unwrappedErr)
}
authCfg := cfg.Modules["auth"].(*AuthModuleConfig)
if authCfg.ClientID != "root" {
t.Errorf(`Unexpected value`)
}
filesCfg := cfg.Modules["files"].(*FilesModuleConfig)
if filesCfg.Directory != "/root" {
t.Errorf(`Unexpected value`)
}
}
func TestDurationUntaggedNoValue(t *testing.T) {
os.Clearenv()
cfg := struct {
Whatever time.Duration
}{}
err := stev.LoadEnv("", &cfg)
if err != nil {
t.Errorf("Expected nil, got %#v", err)
}
if cfg.Whatever != 0 {
t.Errorf(`Expected 0 got %v`, cfg.Whatever)
}
}
func TestDurationUntaggedWithValue(t *testing.T) {
os.Clearenv()
cfg := struct {
Delay time.Duration
}{}
os.Setenv("DELAY", "60s")
err := stev.LoadEnv("", &cfg)
if err != nil {
t.Errorf("Expected nil, got %#v", err)
}
if cfg.Delay != 60*time.Second {
t.Errorf(`Expected 60s got %v`, cfg.Delay)
}
}
func TestDurationPtrUntaggedWithValue(t *testing.T) {
os.Clearenv()
cfg := struct {
Delay *time.Duration
}{}
os.Setenv("DELAY", "60s")
err := stev.LoadEnv("", &cfg)
if err != nil {
t.Errorf("Expected nil, got %#v", err)
}
if *cfg.Delay != 60*time.Second {
t.Errorf(`Expected 60s got %v`, cfg.Delay)
}
}
type RequirementTest struct {
OptionalMixed *MixedRequirements
RequiredOptional *AllOptional `env:",required"`
}
type AllOptional struct {
Name string
Color string
}
type MixedRequirements struct {
Name string `env:",required"`
Color string
}
func TestReqNothingSet(t *testing.T) {
os.Clearenv()
cfg := RequirementTest{}
err := stev.LoadEnv("", &cfg)
if err == nil {
t.Errorf("Expected error")
}
if !strings.HasSuffix(err.Error(), "(field RequiredOptional key REQUIRED_OPTIONAL_*)") {
unwrappedErr := errors.Unwrap(err)
t.Errorf("Unexpected error: %#v (%#v)", err, unwrappedErr)
}
}
func TestReqMixedOptionalSet(t *testing.T) {
os.Clearenv()
cfg := RequirementTest{}
os.Setenv("OPTIONAL_MIXED_COLOR", "RED")
os.Setenv("REQUIRED_OPTIONAL_NAME", "Optional")
err := stev.LoadEnv("", &cfg)
if err == nil {
t.Errorf("Expected error")
}
if !strings.HasSuffix(err.Error(), "fields are required [{Name OPTIONAL_MIXED_NAME}]") {
unwrappedErr := errors.Unwrap(err)
t.Errorf("Unexpected error: %#v (%#v)", err, unwrappedErr)
}
}
func assertStrEq(t *testing.T, have, wanted string) {
if wanted != have {
t.Errorf("Assertion failed:\n\twanted: %v\n\thave: %v", wanted, have)
}
}
func assertInt64Eq(t *testing.T, have, wanted int64) {
if wanted != have {
t.Errorf("Assertion failed:\n\twanted: %v\n\thave: %v", wanted, have)
}
}