forked from Showmax/env
-
Notifications
You must be signed in to change notification settings - Fork 1
/
env_test.go
676 lines (595 loc) · 14.5 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
package env
import (
"encoding/json"
"fmt"
"math"
"net/url"
"os"
"reflect"
"regexp"
"strconv"
"strings"
"testing"
tt "text/template"
"time"
)
type Foo struct {
Foo string `env:"FOO"`
}
type Bar struct {
Bar string `env:"BAR"`
}
type config struct {
Foo
Bar `env:"BAR_"`
Bool *bool `env:"BOOL"`
Duration time.Duration `env:"DURATION"`
Inner Foo `env:"INNER_"`
Int int `env:"INT"`
IntSlice *[]int `env:"INT_SLICE"`
String string `env:"STRING"`
StringSlice []string `env:"STRING_SLICE"`
URLValue url.URL `env:"URL_VALUE"`
URLPtr *url.URL `env:"URL_PTR"`
Regexp regexp.Regexp `env:"REGEXP"`
Template tt.Template `env:"TEMPLATE"`
badConfig int //nolint:structcheck,unused
}
type badConfig struct {
//nolint:structcheck,unused
foo Foo `env:"FOO"` // trying to load badConfig field!
}
const examplePrefix = "PREFIX_"
type environment map[string]string
func (e environment) dup() environment {
f := make(environment, len(e))
for k, v := range e {
f[k] = v
}
return f
}
//nolint:gochecknoglobals
var (
goodEnv = environment{
"BAR_BAR": "BAR_BAR",
"FOO": "FOO",
"BOOL": "true",
"DURATION": "10ms",
"INNER_FOO": "INNER_FOO",
"INT": "1",
"INT_SLICE": `1,2,"3"`,
"STRING": "STRING",
"STRING_SLICE": `"comma separated",values`,
"URL_VALUE": "https://example.org",
"URL_PTR": "https://example.org",
"REGEXP": "^[a-c]bbf*d+c*$",
"TEMPLATE": "{{23 -}} < {{- 45}}",
}
invalidVars = environment{
"BOOL": "flase",
"DURATION": "10d",
"INT": "2.2",
"INT_SLICE": "1,2,2.5,3",
"STRING_SLICE": `"missing","one","quote`,
}
trueVar = true
goodConfig = config{
Foo: Foo{"FOO"},
Bar: Bar{"BAR_BAR"},
Bool: &trueVar,
Duration: 10 * time.Millisecond,
Inner: Foo{"INNER_FOO"},
Int: 1,
IntSlice: &[]int{1, 2, 3},
String: "STRING",
StringSlice: []string{"comma separated", "values"},
URLValue: url.URL{Scheme: "https", Host: "example.org"},
URLPtr: &url.URL{Scheme: "https", Host: "example.org"},
Regexp: *regexp.MustCompile("^[a-c]bbf*d+c*$"),
Template: func() tt.Template {
t, err := tt.New("from_env").Parse("{{23 -}} < {{- 45}}")
if err != nil {
panic(err.Error())
}
return *t
}(),
}
)
func setenv(env environment) {
os.Clearenv()
for k, v := range env {
if err := os.Setenv("PREFIX_"+k, v); err != nil {
panic("bug: os.Setenv: " + err.Error())
}
}
}
// TestLoadOK tests that a correct environment (containing all the variables
// with valid values for their respective types) doesn't fail and produces
// a config which contains the values from the environment.
func TestLoadOK(t *testing.T) {
var cfg config
setenv(goodEnv)
err := Load(&cfg, examplePrefix)
noError(t, err, "loading correct env failed")
wantEqual(t, goodConfig, cfg)
}
// TestLoadMissingVar will try to remove each variable from goodEnv one by one.
// We expect to get an error.
func TestLoadMissingVar(t *testing.T) {
var cfg config
for k := range goodEnv {
oneMissing := goodEnv.dup()
delete(oneMissing, k)
setenv(oneMissing)
err := Load(&cfg, examplePrefix)
wantError(t, err, "loading env with %q missing should fail", k)
}
}
// TestLoadInvalidVar sets each variable to an invalid value in turn and tests
// that the resulting environment is reported as invalid. We don't test all the
// combinations of bad inputs and input types since we're using library parsing
// routines anyway. Therefore, this test only checks that the errors from those
// parsing routines are handled correctly for each type and one of its possible
// invalid inputs.
func TestLoadInvalidVar(t *testing.T) {
var cfg config
for k, v := range invalidVars {
oneInvalid := goodEnv.dup()
oneInvalid[k] = v
setenv(oneInvalid)
err := Load(&cfg, examplePrefix)
wantError(t, err, "loading env with invalid %q should fail", k)
}
}
func TestSlice(t *testing.T) {
samples := map[string][]string{
`"x"`: {"x"},
``: {},
`a`: {`a`},
`a,`: {`a`},
`a,b`: {`a`, `b`},
`a, b`: {`a`, `b`},
`a,b, "c,d"`: {`a`, `b`, `c,d`},
`\"x\"`: {`"x"`},
`"\"x\""`: {`"x"`},
`\\\"\,x`: {`\",x`},
`"",`: {``},
`\"`: {`"`},
"x\n\ty": {"x\n\ty"},
` " x " `: {` x `},
`" fo\\\"o ",bar`: {` fo\"o `, `bar`},
`""","`: {`,`},
`a\,b,c`: {"a,b", "c"},
`abc,def,"gh,i", jkl ," mno pqr ",s"t uv", " wxy","", ` +
`,\"あ"鋸",a\,bc,"fo\"o"\,o,"aa"`: {
`abc`,
`def`,
`gh,i`,
`jkl`,
` mno pqr `,
`st uv`,
` wxy`,
``,
``,
`"あ鋸`,
`a,bc`,
`fo"o,o`,
`aa`,
},
`\?`: {`?`},
`"foo\\"`: {`foo\`},
`foo \\\\ bar`: {`foo \\ bar`},
` "" ,`: {``},
` " " ,`: {` `},
}
type cfg struct {
Slice []string `env:"SLICE"`
}
var c cfg
for s, refOut := range samples {
os.Setenv("SLICE", s)
err := Load(&c, "")
noError(t, err)
wantEqual(t, refOut, c.Slice)
}
}
func TestSliceBad(t *testing.T) {
samples := []string{
`,`,
`"`,
`"x""`,
`\`,
`"x,`,
}
type cfg struct {
Slice []string `env:"SLICE"`
}
var c cfg
for _, s := range samples {
os.Setenv("SLICE", s)
err := Load(&c, "")
wantError(t, err)
}
}
// TestLoadUnexported tries to load good environment into a structure with an
// badConfig field. That should fail, but it should not panic.
func TestLoadUnexported(t *testing.T) {
var cfg badConfig
setenv(goodEnv)
err := Load(&cfg, examplePrefix)
wantError(t, err, "loading badConfig struct should fail")
}
func ExampleLoad() {
// These variables will come from the environment.
os.Setenv("EXAMPLE_FOO", "42")
os.Setenv("EXAMPLE_BAR", "orange")
os.Setenv("EXAMPLE_FOOBAR", "foobar")
type config struct {
Foo int `env:"FOO"`
Bar string `env:"BAR"`
Foobar string // No env tag, won't be loaded.
}
var c config
if err := Load(&c, "EXAMPLE_"); err != nil {
panic(err)
}
fmt.Println(c.Foo, c.Bar, c.Foobar)
// Output: 42 orange
}
func ExampleLoad_missing() {
type config struct {
Foo string `env:"FOO"`
}
var c config
// There's nothing like "optional variables" or "defaults". Defaults
// in configuration are evil. This will blow up.
os.Clearenv()
fmt.Println(Load(&c, ""))
// Output: env: cannot load environment config: "FOO": variable missing
}
func ExampleLoad_nesting() {
// These variables will come from the environment.
os.Setenv("EXAMPLE_ADDR", "localhost:1234")
os.Setenv("EXAMPLE_DB_USER", "joe")
os.Setenv("EXAMPLE_DB_PASS", "joetherollingstone")
type dbConfig struct {
User string `env:"USER"`
Pass string `env:"PASS"`
}
type config struct {
DB dbConfig `env:"DB_"` // Note the _.
Addr string `env:"ADDR"`
}
var c config
if err := Load(&c, "EXAMPLE_"); err != nil {
panic(err)
}
fmt.Println(c.Addr, c.DB.User, c.DB.Pass)
// Output: localhost:1234 joe joetherollingstone
}
func ExampleLoad_shared() {
// These variables will come from the environment.
os.Setenv("EXAMPLE_LOG_LEVEL", "debug")
os.Setenv("EXAMPLE_FOO", "foo")
type SharedConfig struct {
LogLevel string `env:"LOG_LEVEL"`
}
type config struct {
// Anonymous nested structures are visited. This way it's easy
// to share some configuration options in all services.
SharedConfig
Foo string `env:"FOO"`
}
var c config
if err := Load(&c, "EXAMPLE_"); err != nil {
panic(err)
}
fmt.Println(c.LogLevel, c.Foo)
// Output: debug foo
}
func TestMapStrings(t *testing.T) {
samples := []map[string]string{
{
"a": "A",
"b": "B",
"c": "some string",
},
{
"a b c": "A B C",
"a:b:c": "A/B\\C",
"a,b,c": "A=B=C",
"a\"b": "A\"B",
},
{
"": "empty key",
"\n": "newline\nin\nname",
"a\rb": "variable_hidden\rab=ab",
"\\": "slash_var",
},
}
type cfg struct {
Map map[string]string `env:"MAP_"`
}
for _, ref := range samples {
for k, v := range ref {
os.Setenv("MAP_"+k, v)
}
var c cfg
err := Load(&c, "")
noError(t, err)
wantEqual(t, ref, c.Map)
for k := range ref {
os.Unsetenv("MAP_" + k)
}
}
}
func TestMapIntKeys(t *testing.T) {
samples := []map[int]string{
{
1: "one",
2: "two",
-5: "minus five",
0: "zero",
},
}
type cfg struct {
Map map[int]string `env:"MAP_"`
}
for _, ref := range samples {
for k, v := range ref {
os.Setenv("MAP_"+strconv.Itoa(k), v)
}
var c cfg
err := Load(&c, "")
noError(t, err)
wantEqual(t, ref, c.Map)
for k := range ref {
os.Unsetenv("MAP_" + strconv.Itoa(k))
}
}
}
func TestMapFloatKeys(t *testing.T) {
samples := []map[float64]string{
{
1.0: "one",
.25: "decimal",
0: "zero",
},
{
math.Pow(0.1, 20): "Exp form",
math.Inf(1): "+inf",
math.Inf(-1): "-inf",
},
}
type cfg struct {
Map map[float64]string `env:"MAP_"`
}
for _, ref := range samples {
for k, v := range ref {
os.Setenv("MAP_"+fmt.Sprintf("%g", k), v)
}
var c cfg
err := Load(&c, "")
noError(t, err)
wantEqual(t, ref, c.Map)
for k := range ref {
os.Unsetenv("MAP_" + fmt.Sprintf("%g", k))
}
}
}
func TestMapArrVals(t *testing.T) {
samples := []map[string][]string{
{
"a": {"A", "B", "C"},
"b": {},
"c": {"=bc", "=ef"},
},
{
"a": {"A,B", "C"},
"": {"", "", ""},
"c": {"a\nb\rc", "a\n\n\na", ",,,\n,,,"},
},
}
type cfg struct {
Map map[string][]string `env:"MAP_"`
}
for _, ref := range samples {
for k, vals := range ref {
ev := make([]string, 0)
// Escape array values - commas
for _, v := range vals {
v = strings.ReplaceAll(v, ",", "\\,")
if len(v) == 0 {
v = "\"" + v + "\""
}
ev = append(ev, v)
}
os.Setenv("MAP_"+k, strings.Join(ev, ","))
}
var c cfg
err := Load(&c, "")
noError(t, err)
wantEqual(t, ref, c.Map)
for k := range ref {
os.Unsetenv("MAP_" + k)
}
}
}
func TestMapPtrs(t *testing.T) {
x := "x"
y := "y"
X := "X"
Z := "Z"
empty := ""
samples := []map[*string]*string{
{
&x: &X,
&y: &y,
&empty: &Z,
},
}
type cfg struct {
Map map[*string]*string `env:"MAP_"`
}
// *string aren't comparable, just drop them
deptr := func(m map[*string]*string) map[string]*string {
ret := make(map[string]*string, len(m))
for k, v := range m {
ret[*k] = v
}
return ret
}
for _, ref := range samples {
for k, v := range ref {
os.Setenv("MAP_"+*k, *v)
}
var c cfg
err := Load(&c, "")
noError(t, err)
dRef := deptr(ref)
dMap := deptr(c.Map)
wantEqual(t, dRef, dMap)
for k := range ref {
os.Unsetenv("MAP_" + *k)
}
}
}
func TestMapDurations(t *testing.T) {
samples := []map[time.Duration]time.Duration{
{
1 * time.Second: 1 * time.Minute,
0 * time.Microsecond: 2 * time.Minute,
-1 * time.Hour: 1 * time.Nanosecond,
-2 * time.Millisecond: 2 * time.Minute,
},
}
type cfg struct {
Map *map[time.Duration]time.Duration `env:"MAP_"`
}
for _, ref := range samples {
for k, v := range ref {
os.Setenv("MAP_"+k.String(), v.String())
}
var c cfg
err := Load(&c, "")
noError(t, err)
wantEqual(t, ref, *c.Map)
for k := range ref {
os.Unsetenv("MAP_" + k.String())
}
}
}
type customMap map[string]string
func (m *customMap) UnmarshalText(text []byte) error {
return json.Unmarshal(text, (*map[string]string)(m))
}
func TestMapWithCustomUnmarshaler(t *testing.T) {
type cfg struct {
Map customMap `env:"MAP"`
}
os.Setenv("MAP", `{"key": "value"}`)
defer os.Unsetenv("MAP")
var c cfg
err := Load(&c, "")
noError(t, err)
wantEqual(t, customMap{"key": "value"}, c.Map)
}
func TestFileMode(t *testing.T) {
samples := map[string]string{
"0644": "-rw-r--r--",
"0777": "-rwxrwxrwx",
}
type cfg struct {
Mode os.FileMode `env:"FILE_MODE"`
}
for k, v := range samples {
os.Setenv("FILE_MODE", k)
var c cfg
err := Load(&c, "")
noError(t, err)
wantEqual(t, v, c.Mode.String())
os.Unsetenv("FILE_MODE")
}
}
func TestDefaultValue(t *testing.T) {
type Foo struct {
Foo string `env:"FOO" default:"FOO"`
}
type Bar struct {
Bar string `env:"BAR" default:"BAR_BAR"`
}
type config struct {
Foo
Bar `env:"BAR_"`
Bool *bool `env:"BOOL" default:"true"`
Duration time.Duration `env:"DURATION" default:"10ms"`
Int int `env:"INT" default:"1"`
IntSlice *[]int `env:"INT_SLICE" default:"1,2,\"3\""`
String string `env:"STRING" default:"STRING"`
StringSlice []string `env:"STRING_SLICE" default:"\"comma separated\",values"`
URLValue url.URL `env:"URL_VALUE" default:"https://example.org"`
URLPtr *url.URL `env:"URL_PTR" default:"https://example.org"`
Regexp regexp.Regexp `env:"REGEXP" default:"^[a-c]bbf*d+c*$"`
Template tt.Template `env:"TEMPLATE" default:"{{23 -}} < {{- 45}}"`
badConfig int //nolint:structcheck,unused
}
var cfg config
err := Load(&cfg, "")
noError(t, err)
goodConfig := config{
Foo: Foo{"FOO"},
Bar: Bar{"BAR_BAR"},
Bool: &trueVar,
Duration: 10 * time.Millisecond,
Int: 1,
IntSlice: &[]int{1, 2, 3},
String: "STRING",
StringSlice: []string{"comma separated", "values"},
URLValue: url.URL{Scheme: "https", Host: "example.org"},
URLPtr: &url.URL{Scheme: "https", Host: "example.org"},
Regexp: *regexp.MustCompile("^[a-c]bbf*d+c*$"),
Template: func() tt.Template {
return *tt.Must(tt.New("from_env").
Parse("{{23 -}} < {{- 45}}"))
}(),
}
wantEqual(t, goodConfig, cfg)
}
func noError(t *testing.T, err error, msgAndArgs ...any) {
t.Helper()
if err != nil {
t.Fatalf("unexpected error: %v\n%s", err,
""+messageFromMsgAndArgs(msgAndArgs...),
)
}
}
func wantError(t *testing.T, err error, msgAndArgs ...any) {
t.Helper()
if err == nil {
t.Fatalf("expected error, got nil\n%s",
""+messageFromMsgAndArgs(msgAndArgs...),
)
}
}
func wantEqual[T any](t *testing.T, expected, actual T) {
t.Helper()
if !reflect.DeepEqual(expected, actual) {
t.Fatalf("expected %v, got %v", expected, actual)
}
}
func messageFromMsgAndArgs(msgAndArgs ...interface{}) string {
if len(msgAndArgs) == 0 || msgAndArgs == nil {
return ""
}
if len(msgAndArgs) == 1 {
msg := msgAndArgs[0]
if msgAsStr, ok := msg.(string); ok {
return msgAsStr
}
return fmt.Sprintf("%+v", msg)
}
if len(msgAndArgs) > 1 {
return fmt.Sprintf(msgAndArgs[0].(string), msgAndArgs[1:]...)
}
return ""
}