-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconf_test.go
766 lines (605 loc) · 19.9 KB
/
conf_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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
package conf_test
import (
"os"
"testing"
"github.com/rsb/conf"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type SomeFeatureConfig struct {
DB
LambdaHandler
FeatureField string `conf:"env:SOME_FEATURE_FIELD,default:foo"`
IgnoreField int `conf:"-"`
}
type DB struct {
CLIHost string `conf:"env:CURRICULUM_DB_CLI_HOST,required"`
Host string `conf:"env:CURRICULUM_DB_HOST,required,cli:db-host,cli-s:h,cli-u:database host"`
Name string `conf:"env:CURRICULUM_DB_NAME,required"`
User string `conf:"env:CURRICULUM_DB_USER,required"`
Port int `conf:"env:CURRICULUM_DB_PORT,default:5432"`
Pass string `conf:"env:CURRICULUM_DB_PASS,required"`
IsDebug bool `conf:"env:CURRICULUM_DB_DEBUG,default:true"`
Logfile string `conf:"env:CURRICULUM_DB_LOGFILE,default:/curriculum.log"`
}
type LambdaHandler struct {
AppName string `conf:"env:APP_NAME,required"`
}
type InvalidConfigTagParse struct {
Value string `conf:"env:,default:,"`
}
type InvalidConfigEmptyEnv struct {
Value string `conf:"default:foo"`
}
func TestProcessEnv_FieldsFailure(t *testing.T) {
var config InvalidConfigTagParse
err := conf.ProcessEnv(&config)
require.Error(t, err, "conf.ProcessEnv is expected to fail")
assert.Contains(t, err.Error(), "Fields failed: parseTag failed (Value)")
}
func TestProcessEnv_EmptyEnv(t *testing.T) {
var config InvalidConfigEmptyEnv
err := conf.ProcessEnv(&config)
require.Error(t, err, "conf.ProcessEnv is expected to fail")
assert.Contains(t, err.Error(), "env: is required but empty for (Value)")
}
func TestProcessEnv_NoDefaultRequired(t *testing.T) {
type MyConfig struct {
Foo string `conf:"env:MY_FOO, required"`
}
var config MyConfig
err := conf.ProcessEnv(&config)
require.Error(t, err, "conf.ProcessEnv is expected to fail")
assert.Contains(t, err.Error(), "required key (Foo,MY_FOO) missing value")
}
func TestProcessEnv_ProcessFieldFailure(t *testing.T) {
type MyConfig struct {
Foo string `conf:"env:MY_FOO"`
Nbr int `conf:"env:MY_NBR"`
}
setenv(t, "MY_NBR", "abc")
var config MyConfig
err := conf.ProcessEnv(&config)
require.Error(t, err, "conf.ProcessEnv is expected to fail")
assert.Contains(t, err.Error(), "ProcessField failed (Nbr)")
}
func TestProcessEnvNoPrefix_Success(t *testing.T) {
os.Clearenv()
var config SomeFeatureConfig
setenv(t, "CURRICULUM_DB_CLI_HOST", "localhost")
setenv(t, "CURRICULUM_DB_HOST", "some-host")
setenv(t, "CURRICULUM_DB_NAME", "some-name")
setenv(t, "CURRICULUM_DB_USER", "some-user")
setenv(t, "CURRICULUM_DB_PASS", "some-pass")
setenv(t, "APP_NAME", "some-app-name")
err := conf.ProcessEnv(&config)
require.NoError(t, err, "conf.ProcessEnv is not expected to fail")
assert.Equal(t, "some-app-name", config.AppName)
assert.Equal(t, "foo", config.FeatureField)
assert.Equal(t, 0, config.IgnoreField)
assert.Equal(t, "localhost", config.CLIHost)
assert.Equal(t, "some-host", config.Host)
assert.Equal(t, "some-name", config.DB.Name)
assert.Equal(t, "some-user", config.DB.User)
assert.Equal(t, "some-pass", config.DB.Pass)
assert.Equal(t, 5432, config.DB.Port)
assert.True(t, config.DB.IsDebug)
assert.Equal(t, "/curriculum.log", config.DB.Logfile)
}
func TestEnvVar_Success(t *testing.T) {
os.Clearenv()
setenv(t, "FOO", "Bar")
value, err := conf.EnvVar("FOO")
require.NoError(t, err)
assert.Equal(t, "Bar", value)
os.Clearenv()
}
func TestEnvVar_FailureNotSet(t *testing.T) {
os.Clearenv()
_, err := conf.EnvVar("FOO")
require.Error(t, err)
assert.Contains(t, err.Error(), "env var (FOO) is not set")
os.Clearenv()
}
func TestEnvVarStrict_Success(t *testing.T) {
os.Clearenv()
setenv(t, "FOO", "Bar")
value, err := conf.EnvVarStrict("FOO")
require.NoError(t, err)
assert.Equal(t, "Bar", value)
os.Clearenv()
}
func TestEnvVarStrict_FailureNotSet(t *testing.T) {
os.Clearenv()
_, err := conf.EnvVarStrict("FOO")
require.Error(t, err)
assert.Contains(t, err.Error(), "env var (FOO) is not set")
os.Clearenv()
}
func TestEnvVarStrict_FailureEmpty(t *testing.T) {
os.Clearenv()
setenv(t, "FOO", "")
_, err := conf.EnvVarStrict("FOO")
require.Error(t, err)
assert.Contains(t, err.Error(), "env var (FOO) is empty")
os.Clearenv()
}
func TestEnvVarOptional(t *testing.T) {
os.Clearenv()
setenv(t, "FOO", "Bar")
value := conf.EnvVarOptional("FOO")
assert.Equal(t, "Bar", value)
os.Clearenv()
value = conf.EnvVarOptional("FOO")
assert.Equal(t, "", value)
setenv(t, "FOO", "")
value = conf.EnvVarOptional("FOO")
assert.Equal(t, "", value)
}
func setenv(t *testing.T, key, value string) {
require.NoError(t, os.Setenv(key, value))
}
func TestBindCLI_FieldsFailure(t *testing.T) {
var config InvalidConfigTagParse
var cmd = &cobra.Command{
Use: "my-cmd",
}
v := viper.GetViper()
err := conf.BindCLI(cmd, v, &config)
require.Error(t, err, "conf.ProcessEnv is expected to fail")
assert.Contains(t, err.Error(), "Fields failed: parseTag failed (Value)")
}
func TestBindCLI_ParseBoolFailure(t *testing.T) {
type MyConfig struct {
ValueB bool `conf:"cli:value-b,cli-u:other usage,default:xya"`
}
var config MyConfig
var cmd = &cobra.Command{
Use: "my-cmd",
}
v := viper.GetViper()
err := conf.BindCLI(cmd, v, &config)
require.Error(t, err, "conf.ProcessEnv is expected to fail")
assert.Contains(t, err.Error(), "strconv.ParseBool failed")
}
func TestBindCLI_Success(t *testing.T) {
var cmd = &cobra.Command{
Use: "my-cmd",
}
type MyConfig struct {
ValueA string `conf:"cli:value-a,cli-s:i,cli-u:my usage text,default:abc"`
ValueB string `conf:"cli:value-b,cli-u:other usage"`
}
var config MyConfig
v := viper.GetViper()
err := conf.BindCLI(cmd, v, &config)
require.NoError(t, err, "conf.BindCLI is not expected to failed")
result := cmd.Flags().Lookup("value-a")
require.NotNil(t, result, "expecting value-a to be found")
assert.Equal(t, "value-a", result.Name)
assert.Equal(t, "i", result.Shorthand)
assert.Equal(t, "my usage text", result.Usage)
assert.Equal(t, "abc", result.DefValue)
result = cmd.Flags().Lookup("value-b")
require.NotNil(t, result, "expecting value-a to be found")
assert.Equal(t, "value-b", result.Name)
assert.Equal(t, "", result.Shorthand)
assert.Equal(t, "other usage", result.Usage)
assert.Equal(t, "", result.DefValue)
}
func TestBindCLI_PersistentFlagsSuccess(t *testing.T) {
var cmd = &cobra.Command{
Use: "my-cmd",
}
type MyConfig struct {
ValueA string `conf:"cli:value-a,cli-s:i,cli-u:my usage text,default:abc,global-flag"`
ValueB bool `conf:"cli:value-b,cli-s:b,cli-u:other usage,default:true,global-flag"`
}
var config MyConfig
v := viper.GetViper()
err := conf.BindCLI(cmd, v, &config)
require.NoError(t, err, "conf.BindCLI is not expected to failed")
result := cmd.PersistentFlags().Lookup("value-a")
require.NotNil(t, result, "expecting value-a to be found")
assert.Equal(t, "value-a", result.Name)
assert.Equal(t, "i", result.Shorthand)
assert.Equal(t, "my usage text", result.Usage)
assert.Equal(t, "abc", result.DefValue)
result = cmd.PersistentFlags().Lookup("value-b")
require.NotNil(t, result, "expecting value-a to be found")
assert.Equal(t, "value-b", result.Name)
assert.Equal(t, "b", result.Shorthand)
assert.Equal(t, "other usage", result.Usage)
assert.Equal(t, "true", result.DefValue)
}
func TestBindCLI_WithBoolFieldNoShort_Success(t *testing.T) {
var cmd = &cobra.Command{
Use: "my-cmd",
}
type MyConfig struct {
ValueA string `conf:"cli:value-a,cli-s:i,cli-u:my usage text,default:abc"`
ValueB bool `conf:"cli:value-b,cli-u:other usage"`
}
var config MyConfig
v := viper.GetViper()
err := conf.BindCLI(cmd, v, &config)
require.NoError(t, err, "conf.BindCLI is not expected to failed")
result := cmd.Flags().Lookup("value-a")
require.NotNil(t, result, "expecting value-a to be found")
assert.Equal(t, "value-a", result.Name)
assert.Equal(t, "i", result.Shorthand)
assert.Equal(t, "my usage text", result.Usage)
assert.Equal(t, "abc", result.DefValue)
result = cmd.Flags().Lookup("value-b")
require.NotNil(t, result, "expecting value-b to be found")
assert.Equal(t, "value-b", result.Name)
assert.Equal(t, "", result.Shorthand)
assert.Equal(t, "other usage", result.Usage)
assert.Equal(t, "false", result.DefValue)
}
func TestBindCLI_WithBoolFieldShort_Success(t *testing.T) {
var cmd = &cobra.Command{
Use: "my-cmd",
}
type MyConfig struct {
ValueB bool `conf:"cli:value-b,cli-s:b,cli-u:other usage,default:true"`
}
var config MyConfig
v := viper.GetViper()
err := conf.BindCLI(cmd, v, &config)
require.NoError(t, err, "conf.BindCLI is not expected to failed")
result := cmd.Flags().Lookup("value-b")
require.NotNil(t, result, "expecting value-b to be found")
assert.Equal(t, "value-b", result.Name)
assert.Equal(t, "b", result.Shorthand)
assert.Equal(t, "other usage", result.Usage)
assert.Equal(t, "true", result.DefValue)
}
func TestBindCLI_SkipCliSuccess(t *testing.T) {
var cmd = &cobra.Command{
Use: "my-cmd",
}
type MyConfig struct {
ValueA string `conf:"env:value-a,default:abc"`
}
var config MyConfig
v := viper.GetViper()
err := conf.BindCLI(cmd, v, &config)
require.NoError(t, err, "conf.BindCLI is not expected to failed")
result := cmd.Flags().Lookup("value-a")
require.Nil(t, result, "expecting value-a to be missing")
}
func TestProcessCLI_SimpleFieldSuccess(t *testing.T) {
type MyConfig struct {
Field string `conf:"env:MY_FIELD,default:abc,cmds:my-field,cmds-s:f,cmds-u:some field usage"`
}
expectedValue := "foobar"
cmd := &cobra.Command{
Use: "my-cmd",
}
runFn := func(_ *cobra.Command, args []string) error {
v := viper.GetViper()
var config MyConfig
err := conf.ProcessCLI(cmd, v, &config)
require.NoError(t, err, "conf.ProcessCLI is not expected to fail")
assert.Equal(t, expectedValue, config.Field)
return nil
}
cmd.RunE = runFn
v := viper.GetViper()
var config MyConfig
err := conf.BindCLI(cmd, v, &config)
require.NoError(t, err, "conf.ProcessCLI is not expected to fail")
cmd.SetArgs([]string{"--my-field", expectedValue})
err = cmd.Execute()
}
func TestProcessCLI_SimpleFieldDefaultValue(t *testing.T) {
type MyConfig struct {
Field string `conf:"env:MY_FIELD,default:abc,cli:my-field,cli-s:f,cli-u:some field usage"`
}
expectedValue := "abc"
cmd := &cobra.Command{
Use: "my-cmd",
}
runFn := func(_ *cobra.Command, args []string) error {
v := viper.GetViper()
var config MyConfig
err := conf.ProcessCLI(cmd, v, &config)
require.NoError(t, err, "conf.ProcessCLI is not expected to fail")
assert.Equal(t, expectedValue, config.Field)
return nil
}
cmd.RunE = runFn
v := viper.GetViper()
var config MyConfig
err := conf.BindCLI(cmd, v, &config)
require.NoError(t, err, "conf.ProcessCLI is not expected to fail")
err = cmd.Execute()
}
func TestProcessCLI_SimpleFieldENVDefaultValue(t *testing.T) {
type MyConfig struct {
Field string `conf:"env:MY_FIELD,default:abc"`
}
expectedValue := "abc"
cmd := &cobra.Command{
Use: "my-cmd",
}
runFn := func(_ *cobra.Command, args []string) error {
v := viper.GetViper()
var config MyConfig
err := conf.ProcessCLI(cmd, v, &config)
require.NoError(t, err, "conf.ProcessCLI is not expected to fail")
assert.Equal(t, expectedValue, config.Field)
return nil
}
cmd.RunE = runFn
v := viper.GetViper()
var config MyConfig
err := conf.BindCLI(cmd, v, &config)
require.NoError(t, err, "conf.ProcessCLI is not expected to fail")
err = cmd.Execute()
}
func TestProcessCLI_SimpleFieldsSuccess(t *testing.T) {
type MyConfig struct {
Field int `conf:"env:MY_FIELD,cli:my-field,cli-s:y,cli-u:some usage"`
}
expectedValue := 999
cmd := &cobra.Command{
Use: "my-cmd",
}
runFn := func(_ *cobra.Command, args []string) error {
v := viper.GetViper()
var config MyConfig
err := conf.ProcessCLI(cmd, v, &config)
require.NoError(t, err, "conf.ProcessCLI is not expected to fail")
assert.Equal(t, expectedValue, config.Field)
return nil
}
cmd.RunE = runFn
var config MyConfig
cmd.SetArgs([]string{"--my-field", "999"})
v := viper.GetViper()
err := conf.BindCLI(cmd, v, &config)
require.NoError(t, err, "conf.ProcessCLI is not expected to fail")
err = cmd.Execute()
require.NoError(t, err, "cmd.Execute is not expected to fail")
}
func TestProcessCLI_FieldsFailure(t *testing.T) {
var config InvalidConfigTagParse
v := viper.GetViper()
cmd := &cobra.Command{}
err := conf.ProcessCLI(cmd, v, &config)
require.Error(t, err, "conf.ProcessCLI is expected to fail")
assert.Contains(t, err.Error(), "Fields failed: parseTag failed (Value)")
}
func TestProcessCLI_RequiredFieldFailure(t *testing.T) {
type MyConfig struct {
Field int `conf:"env:MY_FIELD,cli:my-field,cli-s:v,cli-u:some usage,required"`
}
cmd := &cobra.Command{
Use: "my-cmd",
}
runFn := func(_ *cobra.Command, args []string) error {
v := viper.GetViper()
var config MyConfig
err := conf.ProcessCLI(cmd, v, &config)
require.Error(t, err, "conf.ProcessCLI is expected to fail")
assert.Contains(t, err.Error(), "required key (field:Field,env:MY_FIELD,cli:my-field) missing value")
return nil
}
cmd.RunE = runFn
v := viper.GetViper()
var config MyConfig
err := conf.BindCLI(cmd, v, &config)
require.NoError(t, err, "conf.ProcessCLI is not expected to fail")
err = cmd.Execute()
require.NoError(t, err, "cmd.Execute is not expected to fail")
}
func TestProcessCLI_NoValueNoDefaultNotRequired(t *testing.T) {
type MyConfig struct {
Field int `conf:"env:MY_FIELD,cli:my-field,cli-s:v,cli-u:some usage"`
}
cmd := &cobra.Command{
Use: "my-cmd",
}
runE := func(_ *cobra.Command, args []string) error {
v := viper.GetViper()
var config MyConfig
err := conf.ProcessCLI(cmd, v, &config)
require.NoError(t, err, "conf.ProcessCLI is not expected to fail")
assert.Equal(t, 0, config.Field)
return nil
}
cmd.RunE = runE
v := viper.GetViper()
var config MyConfig
err := conf.BindCLI(cmd, v, &config)
require.NoError(t, err, "conf.BindCLI is not expected to fail")
err = cmd.Execute()
require.NoError(t, err, "cmd.Execute is not expected to fail")
}
func TestProcessCLI_NoValueNoDefaultNotRequiredUint(t *testing.T) {
type MyConfig struct {
Field uint `conf:"env:MY_FIELD,cli:my-field,cli-s:v,cli-u:some usage"`
}
cmd := &cobra.Command{
Use: "my-cmd",
}
runE := func(_ *cobra.Command, args []string) error {
v := viper.GetViper()
var config MyConfig
err := conf.ProcessCLI(cmd, v, &config)
require.NoError(t, err, "conf.ProcessCLI is not expected to fail")
assert.Equal(t, uint(0), config.Field)
return nil
}
cmd.RunE = runE
v := viper.GetViper()
var config MyConfig
err := conf.BindCLI(cmd, v, &config)
require.NoError(t, err, "conf.BindCLI is not expected to fail")
err = cmd.Execute()
require.NoError(t, err, "cmd.Execute is not expected to fail")
}
func TestProcessCLI_NoValueNoDefaultNotRequiredFloat(t *testing.T) {
type MyConfig struct {
Field float64 `conf:"env:MY_FIELD,cli:my-field,cli-s:v,cli-u:some usage"`
}
cmd := &cobra.Command{
Use: "my-cmd",
}
runE := func(_ *cobra.Command, args []string) error {
v := viper.GetViper()
var config MyConfig
err := conf.ProcessCLI(cmd, v, &config)
require.NoError(t, err, "conf.ProcessCLI is not expected to fail")
assert.Equal(t, float64(0), config.Field)
return nil
}
cmd.RunE = runE
v := viper.GetViper()
var config MyConfig
err := conf.BindCLI(cmd, v, &config)
require.NoError(t, err, "conf.BindCLI is not expected to fail")
err = cmd.Execute()
require.NoError(t, err, "cmd.Execute is not expected to fail")
}
func TestProcessCLI_NoValueNoDefaultNotRequiredBool(t *testing.T) {
type MyConfig struct {
Field bool `conf:"env:MY_FIELD,cli:my-field,cli-s:v,cli-u:some usage"`
}
cmd := &cobra.Command{
Use: "my-cmd",
}
runE := func(_ *cobra.Command, args []string) error {
v := viper.GetViper()
var config MyConfig
err := conf.ProcessCLI(cmd, v, &config)
require.NoError(t, err, "conf.ProcessCLI is not expected to fail")
assert.Equal(t, false, config.Field)
return nil
}
cmd.RunE = runE
v := viper.GetViper()
var config MyConfig
err := conf.BindCLI(cmd, v, &config)
require.NoError(t, err, "conf.BindCLI is not expected to fail")
err = cmd.Execute()
require.NoError(t, err, "cmd.Execute is not expected to fail")
}
func TestProcessCLI_ProcessFieldFailure(t *testing.T) {
type MyConfig struct {
Field int `conf:"env:MY_FIELD,cli:my-field,cli-s:v,cmds-u:some usage"`
}
cmd := &cobra.Command{
Use: "my-cmd",
}
runFn := func(_ *cobra.Command, args []string) error {
v := viper.GetViper()
var config MyConfig
err := conf.ProcessCLI(cmd, v, &config)
require.Error(t, err, "conf.ProcessCLI is expected to fail")
assert.Contains(t, err.Error(), "ProcessField failed (Field)")
return nil
}
cmd.RunE = runFn
setenv(t, "MY_FIELD", "abc")
v := viper.GetViper()
var config MyConfig
err := conf.BindCLI(cmd, v, &config)
require.NoError(t, err, "conf.BindCLI is not expected to fail")
err = cmd.Execute()
require.NoError(t, err, "cmd.Execute is not expected to fail")
}
func TestEnvNames_FieldsFailure(t *testing.T) {
var config InvalidConfigTagParse
_, err := conf.EnvNames(&config)
require.Error(t, err, "conf.EnvNames is expected to fail")
assert.Contains(t, err.Error(), "Fields failed: parseTag failed (Value)")
}
func TestEnvNames_SuccessWithPrefix(t *testing.T) {
type MyConfig struct {
FieldA string `conf:"env:FIELD_A"`
FieldB string `conf:"env:FIELD_B"`
FieldC string `conf:"-"`
FieldD int `conf:"env:FIELD_D"`
}
var config MyConfig
names, err := conf.EnvNames(&config, "FOO")
require.NoError(t, err, "conf.EnvNames is not expected to fail")
expected := []string{"FOO_FIELD_A", "FOO_FIELD_B", "FOO_FIELD_D"}
assert.Equal(t, expected, names)
}
func TestEnvNames_SuccessWithNoPrefix(t *testing.T) {
type MyConfig struct {
FieldA string `conf:"env:FIELD_A"`
FieldB string `conf:"env:FIELD_B"`
FieldC string `conf:"-"`
FieldD int `conf:"env:FIELD_D"`
}
var config MyConfig
names, err := conf.EnvNames(&config)
require.NoError(t, err, "conf.EnvNames is not expected to fail")
expected := []string{"FIELD_A", "FIELD_B", "FIELD_D"}
assert.Equal(t, expected, names)
}
func TestEnvToMap_FieldsFailure(t *testing.T) {
var config InvalidConfigTagParse
_, err := conf.EnvToMap(&config)
require.Error(t, err, "conf.EnvToMap is expected to fail")
assert.Contains(t, err.Error(), "Fields failed: parseTag failed (Value)")
}
func TestEnvToMap_Success(t *testing.T) {
type MyConfig struct {
FieldA string `conf:"env:FIELD_A,default:abc"`
FieldB string `conf:"env:FIELD_B,required"`
FieldC string `conf:"-"`
FieldD int `conf:"env:FIELD_D,default:888"`
FieldE int `conf:"env:FIELD_E"`
FieldX string `conf:"env:-,cmds:abc"`
}
setenv(t, "FIELD_B", "xyz")
setenv(t, "FIELD_D", "777")
var config MyConfig
result, err := conf.EnvToMap(&config)
require.NoError(t, err, "conf.EnvNames is not expected to fail")
expected := map[string]string{
"FIELD_A": "abc",
"FIELD_B": "xyz",
"FIELD_D": "777",
"FIELD_E": "",
}
assert.Equal(t, expected, result)
}
func TestEnvToMap_EnvMissingFailure(t *testing.T) {
type MyConfig struct {
FieldA string `conf:"cmds:abc, default:abc"`
FieldB string `conf:"env:FIELD_B,required"`
FieldC string `conf:"-"`
FieldD int `conf:"env:FIELD_D,default:888"`
FieldX string `conf:"env:-,cmds:abc"`
}
setenv(t, "FIELD_B", "xyz")
setenv(t, "FIELD_D", "777")
var config MyConfig
_, err := conf.EnvToMap(&config)
require.Error(t, err, "conf.EnvToMap is expected to fail")
assert.Contains(t, err.Error(), "env: is required but empty for (FieldA)")
}
func TestEnvToMap_RequiredFailure(t *testing.T) {
type MyConfig struct {
FieldA string `conf:"env:FIELD_A,default:abc"`
FieldB string `conf:"env:FIELD_B,required"`
FieldC string `conf:"-"`
FieldD int `conf:"env:FIELD_D,default:888"`
FieldX string `conf:"env:-,cmds:abc"`
}
os.Clearenv()
setenv(t, "FIELD_D", "777")
var config MyConfig
_, err := conf.EnvToMap(&config)
require.Error(t, err, "conf.EnvToMap is expected to fail")
assert.Contains(t, err.Error(), "required key (FieldB,FIELD_B) missing value")
}