-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvcfg_test.go
619 lines (577 loc) · 13.4 KB
/
envcfg_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
package envcfg_test
import (
"errors"
"os"
"reflect"
"regexp"
"testing"
"github.com/sethpollack/envcfg"
errs "github.com/sethpollack/envcfg/errors"
"github.com/sethpollack/envcfg/sources/osenv"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestParse(t *testing.T) {
tempFile, err := os.CreateTemp("", "env.txt")
if err != nil {
t.Fatal(err)
}
_, err = tempFile.WriteString("${OTHER_VAR}")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tempFile.Name())
tempDotEnvFile, err := os.CreateTemp("", ".env")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tempDotEnvFile.Name())
_, err = tempDotEnvFile.WriteString("FIELD=value")
if err != nil {
t.Fatal(err)
}
tt := map[string]struct {
env map[string]string
cfg any
expected any
options []envcfg.Option
expectedErr error
skipErrIs bool
skip bool
skipReason string
}{
"WithTagName": {
env: map[string]string{"CUSTOM": "value"},
options: []envcfg.Option{envcfg.WithTagName("foo")},
expected: struct {
Field string `foo:"CUSTOM"`
}{
Field: "value",
},
},
"WithDelimiterTag": {
env: map[string]string{"FIELD": "key|value"},
options: []envcfg.Option{envcfg.WithDelimiterTag("custom_delim")},
expected: struct {
Field []string `custom_delim:"|"`
}{
Field: []string{"key", "value"},
},
},
"WithDelimiter": {
env: map[string]string{"FIELD": "key|value"},
options: []envcfg.Option{envcfg.WithDelimiter("|")},
expected: struct {
Field []string
}{
Field: []string{"key", "value"},
},
},
"WithSeparatorTag": {
env: map[string]string{"FIELD": "key1|value1,key2|value2"},
options: []envcfg.Option{envcfg.WithSeparatorTag("custom_sep")},
expected: struct {
Field map[string]string `custom_sep:"|"`
}{
Field: map[string]string{"key1": "value1", "key2": "value2"},
},
},
"WithSeparator": {
env: map[string]string{"FIELD": "key|value"},
options: []envcfg.Option{envcfg.WithSeparator("|")},
expected: struct {
Field map[string]string
}{
Field: map[string]string{"key": "value"},
},
},
"WithDecodeUnsetTag": {
options: []envcfg.Option{envcfg.WithDecodeUnsetTag("custom_decodeunset")},
expected: struct {
Field unset `custom_decodeunset:"true"`
}{
Field: unset{
Value: "Hello World!",
},
},
},
"WithDecodeUnset": {
options: []envcfg.Option{envcfg.WithDecodeUnset()},
expected: struct {
Field unset
}{
Field: unset{
Value: "Hello World!",
},
},
},
"WithInitTag": {
options: []envcfg.Option{envcfg.WithInitTag("custom_init")},
expected: struct {
Field *string `custom_init:"always"`
}{
Field: ptr(""),
},
},
"WithInitAny": {
options: []envcfg.Option{envcfg.WithInitAny()},
expected: struct {
Field *string `default:"value"`
}{
Field: ptr("value"),
},
},
"WithInitNever": {
env: map[string]string{"FIELD": "value"},
options: []envcfg.Option{envcfg.WithInitNever()},
expected: struct {
Field *string `default:"value"`
}{
Field: nil,
},
},
"WithInitAlways": {
options: []envcfg.Option{envcfg.WithInitAlways()},
expected: struct {
Field *string
}{
Field: ptr(""),
},
},
"WithDefaultTag": {
options: []envcfg.Option{envcfg.WithDefaultTag("custom_default")},
expected: struct {
Field string `custom_default:"value"`
}{
Field: "value",
},
},
"WithExpandTag": {
env: map[string]string{"FIELD": "${FOO}", "FOO": "value"},
options: []envcfg.Option{envcfg.WithExpandTag("custom_expand")},
expected: struct {
Field string `custom_expand:"true"`
}{
Field: "value",
},
},
"WithFileTag": {
env: map[string]string{"FIELD": tempFile.Name()},
options: []envcfg.Option{envcfg.WithFileTag("custom_file")},
expected: struct {
Field string `custom_file:"true"`
}{
Field: "${OTHER_VAR}",
},
},
"WithNotEmptyTag": {
env: map[string]string{"FIELD": ""},
options: []envcfg.Option{envcfg.WithNotEmptyTag("custom_notempty")},
expected: struct {
Field string `custom_notempty:"true"`
}{},
expectedErr: errs.ErrNotEmpty,
},
"WithNotEmpty": {
env: map[string]string{"FIELD": ""},
options: []envcfg.Option{envcfg.WithNotEmpty()},
expected: struct {
Field string
}{},
expectedErr: errs.ErrNotEmpty,
},
"WithExpand": {
env: map[string]string{"FIELD": "${FOO}", "FOO": "value"},
options: []envcfg.Option{envcfg.WithExpand()},
expected: struct {
Field string
}{
Field: "value",
},
},
"WithRequiredTag": {
options: []envcfg.Option{envcfg.WithRequiredTag("custom_required")},
expected: struct {
Field string `custom_required:"true"`
}{},
expectedErr: errs.ErrRequired,
},
"WithRequired": {
options: []envcfg.Option{envcfg.WithRequired()},
expected: struct {
Field string
}{},
expectedErr: errs.ErrRequired,
},
"WithDisableFallback": {
env: map[string]string{"FIELD": "value"},
options: []envcfg.Option{envcfg.WithDisableFallback()},
expected: struct {
Field string
}{},
},
"WithDecoder": {
env: map[string]string{"FIELD": "hello"},
options: []envcfg.Option{envcfg.WithDecoder((*customIface)(nil), func(v any, value string) error {
return v.(*custom).CustomDecode(value)
})},
expected: struct {
Field custom
}{
Field: custom{field: "hello world!"},
},
},
"WithTypeParser": {
env: map[string]string{"FIELD": "value"},
options: []envcfg.Option{envcfg.WithTypeParser(reflect.TypeOf((*Inter)(nil)).Elem(), func(value string) (any, error) {
return &Impl{Field: value}, nil
})},
expected: struct {
Field Inter
}{
Field: &Impl{Field: "value"},
},
},
"WithTypeParsers": {
env: map[string]string{"FIELD": "value"},
options: []envcfg.Option{envcfg.WithTypeParsers(map[reflect.Type]func(value string) (any, error){
reflect.TypeOf((*Inter)(nil)).Elem(): func(value string) (any, error) {
return &Impl{Field: value}, nil
},
})},
expected: struct {
Field Inter
}{
Field: &Impl{Field: "value"},
},
},
"WithKindParser": {
env: map[string]string{"FIELD": "hello"},
options: []envcfg.Option{envcfg.WithKindParser(reflect.String, func(value string) (any, error) {
return value + " world", nil
})},
expected: struct {
Field string
}{
Field: "hello world",
},
},
"WithKindParsers": {
env: map[string]string{"FIELD": "hello"},
options: []envcfg.Option{envcfg.WithKindParsers(map[reflect.Kind]func(value string) (any, error){
reflect.String: func(value string) (any, error) {
return value + " world", nil
},
})},
expected: struct {
Field string
}{
Field: "hello world",
},
},
"WithLoader": {
options: []envcfg.Option{envcfg.WithLoader(
envcfg.WithMapEnvSource(map[string]string{"FIELD": "value"}),
)},
expected: struct {
Field string
}{
Field: "value",
},
},
"WithLoader Error": {
options: []envcfg.Option{envcfg.WithLoader(
envcfg.WithSource(&customSource{}),
)},
expected: struct {
Field string
}{},
expectedErr: errs.ErrLoadEnv,
},
"WithSources": {
env: map[string]string{"FIELD": "value"},
options: []envcfg.Option{envcfg.WithLoader(
envcfg.WithSources(osenv.New()),
)},
expected: struct {
Field string
}{
Field: "value",
},
},
"WithFilter": {
env: map[string]string{"FIELD": "value", "OTHER": "value"},
options: []envcfg.Option{envcfg.WithLoader(
envcfg.WithFilter(func(key string) bool {
return key == "FIELD"
}),
)},
expected: struct {
Field string
Other string
}{
Field: "value",
Other: "",
},
},
"WithTransform": {
env: map[string]string{"FIELD": "value"},
options: []envcfg.Option{envcfg.WithLoader(
envcfg.WithTransform(func(key string) string {
return "TRANSFORMED_" + key
}),
)},
expected: struct {
TransformedField string
}{
TransformedField: "value",
},
},
"WithPrefix": {
env: map[string]string{"PREFIXED_FIELD": "value", "OTHER": "value"},
options: []envcfg.Option{envcfg.WithLoader(
envcfg.WithPrefix("PREFIXED_"),
)},
expected: struct {
Field string
Other string
}{
Field: "value",
Other: "",
},
},
"WithSuffix": {
env: map[string]string{"FIELD_SUFFIX": "value", "OTHER": "value"},
options: []envcfg.Option{envcfg.WithLoader(
envcfg.WithSuffix("_SUFFIX"),
)},
expected: struct {
Field string
Other string
}{
Field: "value",
Other: "",
},
},
"WithHasPrefix": {
env: map[string]string{"PREFIXED_FIELD": "value", "OTHER": "value"},
options: []envcfg.Option{envcfg.WithLoader(
envcfg.WithHasPrefix("PREFIXED_"),
)},
expected: struct {
PrefixedField string
Other string
}{
PrefixedField: "value",
Other: "",
},
},
"WithHasSuffix": {
env: map[string]string{"FIELD_SUFFIX": "value", "OTHER": "value"},
options: []envcfg.Option{envcfg.WithLoader(
envcfg.WithHasSuffix("_SUFFIX"),
)},
expected: struct {
FieldSuffix string
Other string
}{
FieldSuffix: "value",
Other: "",
},
},
"WithHasMatch": {
env: map[string]string{"MATCHED_FIELD": "value", "OTHER": "value"},
options: []envcfg.Option{envcfg.WithLoader(
envcfg.WithHasMatch(regexp.MustCompile("MATCHED_")),
)},
expected: struct {
MatchedField string
Other string
}{
MatchedField: "value",
Other: "",
},
},
"WithKeys": {
env: map[string]string{"KEY1": "key1", "KEY2": "key2", "KEY3": "key3", "KEY4": "key4"},
options: []envcfg.Option{envcfg.WithLoader(
envcfg.WithKeys("KEY1", "KEY2"),
)},
expected: struct {
Key1 string
Key2 string
Key3 string
Key4 string
}{
Key1: "key1",
Key2: "key2",
Key3: "",
Key4: "",
},
},
"WithTrimPrefix": {
env: map[string]string{"PREFIXED_FIELD": "hello", "OTHER": "123"},
options: []envcfg.Option{envcfg.WithLoader(
envcfg.WithTrimPrefix("PREFIXED_"),
)},
expected: struct {
Field string
Other string
}{
Field: "hello",
Other: "123",
},
},
"WithTrimSuffix": {
env: map[string]string{"FIELD_SUFFIX": "hello", "OTHER": "123"},
options: []envcfg.Option{envcfg.WithLoader(
envcfg.WithTrimSuffix("_SUFFIX"),
)},
expected: struct {
Field string
Other string
}{
Field: "hello",
Other: "123",
},
},
"WithMapEnvSource": {
options: []envcfg.Option{envcfg.WithLoader(
envcfg.WithMapEnvSource(map[string]string{"FIELD": "value"}),
)},
expected: struct {
Field string
}{
Field: "value",
},
},
"WithOSEnvSource": {
env: map[string]string{"FIELD": "value"},
options: []envcfg.Option{envcfg.WithLoader(
envcfg.WithOSEnvSource(),
)},
expected: struct {
Field string
}{
Field: "value",
},
},
"WithDotEnvSource": {
options: []envcfg.Option{envcfg.WithLoader(
envcfg.WithDotEnvSource(tempDotEnvFile.Name()),
)},
expected: struct {
Field string
}{
Field: "value",
},
},
}
for name, tc := range tt {
t.Run(name, func(t *testing.T) {
if tc.skip {
t.Skip(tc.skipReason)
}
for k, v := range tc.env {
t.Setenv(k, v)
}
cfg := tc.cfg
if cfg == nil {
cfg = reflect.New(reflect.TypeOf(tc.expected)).Interface()
}
err := envcfg.Parse(cfg, tc.options...)
if tc.expectedErr != nil {
require.Error(t, err)
if !tc.skipErrIs {
assert.ErrorIs(t, err, tc.expectedErr)
}
} else {
require.NoError(t, err)
actual := reflect.ValueOf(cfg).Elem().Interface()
assert.Equal(t, tc.expected, actual)
}
})
}
}
func TestParseAs(t *testing.T) {
type Config struct {
Field string `required:"true"`
}
t.Run("success", func(t *testing.T) {
t.Setenv("FIELD", "value")
cfg, err := envcfg.ParseAs[Config]()
require.NoError(t, err)
assert.Equal(t, Config{Field: "value"}, cfg)
})
t.Run("error", func(t *testing.T) {
cfg, err := envcfg.ParseAs[Config]()
assert.ErrorIs(t, err, errs.ErrRequired)
assert.Equal(t, Config{}, cfg)
})
}
func TestMustParse(t *testing.T) {
type Config struct {
Field string `required:"true"`
}
t.Run("success", func(t *testing.T) {
t.Setenv("FIELD", "value")
cfg := Config{}
assert.NotPanics(t, func() {
envcfg.MustParse(&cfg)
})
assert.Equal(t, Config{Field: "value"}, cfg)
})
t.Run("error", func(t *testing.T) {
cfg := Config{}
assert.Panics(t, func() {
envcfg.MustParse(&cfg)
})
})
}
func TestMustParseAs(t *testing.T) {
type Config struct {
Field string `required:"true"`
}
t.Run("success", func(t *testing.T) {
t.Setenv("FIELD", "value")
var cfg Config
assert.NotPanics(t, func() {
cfg = envcfg.MustParseAs[Config]()
})
assert.Equal(t, Config{Field: "value"}, cfg)
})
t.Run("error", func(t *testing.T) {
assert.Panics(t, func() {
envcfg.MustParseAs[Config]()
})
})
}
func ptr[T any](v T) *T {
return &v
}
type unset struct {
Value string
}
func (u *unset) UnmarshalText(text []byte) error {
u.Value = "Hello World!"
return nil
}
type customIface interface {
CustomDecode(value string) error
}
type custom struct {
field string
}
func (c *custom) CustomDecode(value string) error {
c.field = value + " world!"
return nil
}
type Inter interface{}
type Impl struct {
Field string
}
type customSource struct {
}
func (c *customSource) Load() (map[string]string, error) {
return nil, errors.New("source error")
}