-
Notifications
You must be signed in to change notification settings - Fork 30
/
envconfig_test.go
630 lines (505 loc) · 14.5 KB
/
envconfig_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
package envconfig_test
import (
"fmt"
"os"
"strconv"
"strings"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/vrischmann/envconfig"
)
func TestParseSimpleConfig(t *testing.T) {
var conf struct {
Name string
Log struct {
Path string
}
}
err := envconfig.Init(&conf)
require.Equal(t, "envconfig: keys NAME, name not found", err.Error())
os.Setenv("NAME", "foobar")
err = envconfig.Init(&conf)
require.Equal(t, "envconfig: keys LOG_PATH, log_path not found", err.Error())
os.Setenv("LOG_PATH", "/var/log/foobar")
err = envconfig.Init(&conf)
require.Nil(t, err)
require.Equal(t, "foobar", conf.Name)
require.Equal(t, "/var/log/foobar", conf.Log.Path)
// Clean up at the end of the test - some tests share the same key and we don't values to be seen by those tests
os.Setenv("NAME", "")
os.Setenv("LOG_PATH", "")
}
func TestParseIntegerConfig(t *testing.T) {
var conf struct {
Port int
Long uint64
Version uint8
}
timestamp := time.Now().UnixNano()
os.Setenv("PORT", "80")
os.Setenv("LONG", fmt.Sprintf("%d", timestamp))
os.Setenv("VERSION", "2")
err := envconfig.Init(&conf)
require.Nil(t, err)
require.Equal(t, 80, conf.Port)
require.Equal(t, uint64(timestamp), conf.Long)
require.Equal(t, uint8(2), conf.Version)
}
func TestParseBoolConfig(t *testing.T) {
var conf struct {
DoIt bool
}
os.Setenv("DOIT", "true")
err := envconfig.Init(&conf)
require.Nil(t, err)
require.Equal(t, true, conf.DoIt)
}
func TestParseBytesConfig(t *testing.T) {
var conf struct {
Data []byte
}
os.Setenv("DATA", "Rk9PQkFS")
err := envconfig.Init(&conf)
require.Nil(t, err)
require.Equal(t, []byte("FOOBAR"), conf.Data)
}
func TestParseFloatConfig(t *testing.T) {
var conf struct {
Delta float32
DeltaV float64
}
os.Setenv("DELTA", "0.02")
os.Setenv("DELTAV", "400.20000000001")
err := envconfig.Init(&conf)
require.Nil(t, err)
require.Equal(t, float32(0.02), conf.Delta)
require.Equal(t, float64(400.20000000001), conf.DeltaV)
}
func TestParseSliceConfig(t *testing.T) {
var conf struct {
Names []string
Ports []int
Shards []struct {
Name string
Addr string
}
}
os.Setenv("NAMES", "foobar,barbaz")
os.Setenv("PORTS", "900,100")
os.Setenv("SHARDS", "{foobar,localhost:2929},{barbaz,localhost:2828}")
err := envconfig.Init(&conf)
require.Nil(t, err)
require.Equal(t, 2, len(conf.Names))
require.Equal(t, "foobar", conf.Names[0])
require.Equal(t, "barbaz", conf.Names[1])
require.Equal(t, 2, len(conf.Ports))
require.Equal(t, 900, conf.Ports[0])
require.Equal(t, 100, conf.Ports[1])
require.Equal(t, 2, len(conf.Shards))
require.Equal(t, "foobar", conf.Shards[0].Name)
require.Equal(t, "localhost:2929", conf.Shards[0].Addr)
require.Equal(t, "barbaz", conf.Shards[1].Name)
require.Equal(t, "localhost:2828", conf.Shards[1].Addr)
}
func TestParseStructSliceWrongData(t *testing.T) {
var conf struct {
Shards []struct {
Name string
Addr string
}
}
os.Setenv("SHARDS", "foobar")
err := envconfig.Init(&conf)
require.Equal(t, `envconfig: unable to parse value "foobar" for possible keys [SHARDS shards]. err=struct token has 1 fields but struct has 2`, err.Error())
}
func TestParseStructSliceWrongValue(t *testing.T) {
var conf struct {
Shards []struct {
Name string
Port int32
}
}
os.Setenv("SHARDS", "{foobar,barbaz}")
err := envconfig.Init(&conf)
require.Equal(t, `envconfig: unable to parse value "{foobar,barbaz}" for possible keys [SHARDS shards]. err=envconfig: unable to parse value "barbaz" for possible keys [SHARDS shards]. err=strconv.ParseInt: parsing "barbaz": invalid syntax`, err.Error())
}
func TestParseWrongValues(t *testing.T) {
var conf struct{ OK bool }
os.Setenv("OK", "foobar")
err := envconfig.Init(&conf)
require.Equal(t, `envconfig: unable to parse value "foobar" for possible keys [OK ok]. err=strconv.ParseBool: parsing "foobar": invalid syntax`, err.Error())
var conf2 struct{ Port int }
os.Setenv("PORT", "foobar")
err = envconfig.Init(&conf2)
require.Equal(t, `envconfig: unable to parse value "foobar" for possible keys [PORT port]. err=strconv.ParseInt: parsing "foobar": invalid syntax`, err.Error())
var conf3 struct{ Port uint }
os.Setenv("PORT", "foobar")
err = envconfig.Init(&conf3)
require.Equal(t, `envconfig: unable to parse value "foobar" for possible keys [PORT port]. err=strconv.ParseUint: parsing "foobar": invalid syntax`, err.Error())
var conf4 struct{ Port float32 }
os.Setenv("PORT", "foobar")
err = envconfig.Init(&conf4)
require.Equal(t, `envconfig: unable to parse value "foobar" for possible keys [PORT port]. err=strconv.ParseFloat: parsing "foobar": invalid syntax`, err.Error())
var conf5 struct{ Data []byte }
os.Setenv("DATA", "foobar")
err = envconfig.Init(&conf5)
require.Equal(t, `envconfig: unable to parse value "foobar" as bytes for possible keys [DATA data]. err=illegal base64 data at input byte 4`, err.Error())
}
func TestDurationConfig(t *testing.T) {
var conf struct {
Timeout time.Duration
}
os.Setenv("TIMEOUT", "1m")
err := envconfig.Init(&conf)
require.Nil(t, err)
require.Equal(t, time.Minute*1, conf.Timeout)
}
func TestInvalidDurationConfig(t *testing.T) {
var conf struct {
Timeout time.Duration
}
os.Setenv("TIMEOUT", "foo")
err := envconfig.Init(&conf)
require.NotNil(t, err)
}
func TestAllPointerConfig(t *testing.T) {
var conf struct {
Name *string
Port *int
Delta *float32
DeltaV *float64
Hosts *[]string
Shards *[]*struct {
Name *string
Addr *string
}
Master *struct {
Name *string
Addr *string
}
Timeout *time.Duration
}
os.Setenv("NAME", "foobar")
os.Setenv("PORT", "9000")
os.Setenv("DELTA", "40.01")
os.Setenv("DELTAV", "200.00001")
os.Setenv("HOSTS", "localhost,free.fr")
os.Setenv("SHARDS", "{foobar,localhost:2828},{barbaz,localhost:2929}")
os.Setenv("MASTER_NAME", "master")
os.Setenv("MASTER_ADDR", "localhost:2727")
os.Setenv("TIMEOUT", "1m")
err := envconfig.Init(&conf)
require.Nil(t, err)
require.Equal(t, "foobar", *conf.Name)
require.Equal(t, 9000, *conf.Port)
require.Equal(t, float32(40.01), *conf.Delta)
require.Equal(t, 200.00001, *conf.DeltaV)
require.Equal(t, 2, len(*conf.Hosts))
require.Equal(t, "localhost", (*conf.Hosts)[0])
require.Equal(t, "free.fr", (*conf.Hosts)[1])
require.Equal(t, 2, len(*conf.Shards))
require.Equal(t, "foobar", *(*conf.Shards)[0].Name)
require.Equal(t, "localhost:2828", *(*conf.Shards)[0].Addr)
require.Equal(t, "barbaz", *(*conf.Shards)[1].Name)
require.Equal(t, "localhost:2929", *(*conf.Shards)[1].Addr)
require.Equal(t, "master", *conf.Master.Name)
require.Equal(t, "localhost:2727", *conf.Master.Addr)
require.Equal(t, time.Minute*1, *conf.Timeout)
}
type logMode uint
const (
logFile logMode = iota + 1
logStdout
)
func (m *logMode) Unmarshal(s string) error {
switch strings.ToLower(s) {
case "file":
*m = logFile
case "stdout":
*m = logStdout
default:
return fmt.Errorf("unable to unmarshal %s", s)
}
return nil
}
func TestUnmarshaler(t *testing.T) {
var conf struct {
LogMode logMode
}
os.Setenv("LOGMODE", "file")
err := envconfig.Init(&conf)
require.Nil(t, err)
require.Equal(t, logFile, conf.LogMode)
var conf2 struct {
LogMode *logMode
}
err = envconfig.Init(&conf2)
require.Nil(t, err)
require.Equal(t, logFile, *conf2.LogMode)
}
func TestParseOptionalConfig(t *testing.T) {
var conf struct {
Name string `envconfig:"optional"`
Flag bool `envconfig:"optional"`
Timeout time.Duration `envconfig:"optional"`
Port int `envconfig:"optional"`
Port2 uint `envconfig:"optional"`
Delta float32 `envconfig:"optional"`
DeltaV float64 `envconfig:"optional"`
Slice []string `envconfig:"optional"`
Struct struct {
A string
B int
} `envconfig:"optional"`
}
os.Setenv("NAME", "")
os.Setenv("FLAG", "")
os.Setenv("TIMEOUT", "")
os.Setenv("PORT", "")
os.Setenv("PORT2", "")
os.Setenv("DELTA", "")
os.Setenv("DELTAV", "")
os.Setenv("SLICE", "")
os.Setenv("STRUCT_A", "")
err := envconfig.Init(&conf)
require.Nil(t, err)
require.Equal(t, "", conf.Name)
os.Setenv("NAME", "foobar")
os.Setenv("SLICE", "a,b")
os.Setenv("STRUCT_A", "foobar")
os.Setenv("STRUCT_B", "1")
err = envconfig.Init(&conf)
require.Nil(t, err)
require.Equal(t, "foobar", conf.Name)
require.Equal(t, []string{"a", "b"}, conf.Slice)
require.Equal(t, "foobar", conf.Struct.A)
require.Equal(t, 1, conf.Struct.B)
}
func TestParseSkippableConfig(t *testing.T) {
var conf struct {
Flag bool `envconfig:"-"`
}
os.Setenv("FLAG", "true")
err := envconfig.Init(&conf)
require.Nil(t, err)
require.Equal(t, false, conf.Flag)
}
func TestParseCustomNameConfig(t *testing.T) {
var conf struct {
Name string `envconfig:"customName"`
}
os.Setenv("customName", "foobar")
err := envconfig.Init(&conf)
require.Nil(t, err)
require.Equal(t, "foobar", conf.Name)
}
func TestParseOptionalStruct(t *testing.T) {
var conf struct {
Master struct {
Name string
} `envconfig:"optional"`
}
os.Setenv("MASTER_NAME", "")
err := envconfig.Init(&conf)
require.Nil(t, err)
require.Equal(t, "", conf.Master.Name)
}
func TestParsePrefixedStruct(t *testing.T) {
var conf struct {
Name string
}
os.Setenv("NAME", "")
os.Setenv("FOO_NAME", "")
os.Setenv("NAME", "bad")
err := envconfig.InitWithPrefix(&conf, "FOO")
require.NotNil(t, err)
os.Setenv("FOO_NAME", "good")
err = envconfig.InitWithPrefix(&conf, "FOO")
require.Nil(t, err)
require.Equal(t, "good", conf.Name)
}
func TestUnexportedField(t *testing.T) {
var conf struct {
name string
}
os.Setenv("NAME", "foobar")
err := envconfig.Init(&conf)
require.Equal(t, envconfig.ErrUnexportedField, err)
err = envconfig.InitWithOptions(&conf, envconfig.Options{AllowUnexported: true})
require.Equal(t, nil, err)
}
func TestNestedUnexportedField(t *testing.T) {
var conf struct {
Foo struct {
Bar struct {
baz string
}
}
}
os.Setenv("FOO_BAR_BAZ", "foobar")
err := envconfig.Init(&conf)
require.Equal(t, envconfig.ErrUnexportedField, err)
err = envconfig.InitWithOptions(&conf, envconfig.Options{AllowUnexported: true})
require.Equal(t, nil, err)
}
type sliceWithUnmarshaler []int
func (sl *sliceWithUnmarshaler) Unmarshal(s string) error {
tokens := strings.Split(s, ".")
for _, tok := range tokens {
tmp, err := strconv.Atoi(tok)
if err != nil {
return err
}
*sl = append(*sl, tmp)
}
return nil
}
func TestSliceTypeWithUnmarshaler(t *testing.T) {
var conf struct {
Data sliceWithUnmarshaler
}
os.Setenv("DATA", "1.2.3")
err := envconfig.Init(&conf)
require.Nil(t, err)
require.Equal(t, 3, len(conf.Data))
require.Equal(t, 1, conf.Data[0])
require.Equal(t, 2, conf.Data[1])
require.Equal(t, 3, conf.Data[2])
}
func TestParseDefaultVal(t *testing.T) {
var conf struct {
MySQL struct {
Master struct {
Address string `envconfig:"default=localhost"`
Port int `envconfig:"default=3306"`
}
Timeout time.Duration `envconfig:"default=1m,myTimeout"`
LocalTimeout time.Duration `envconfig:"myTimeout2,default=1m"`
}
}
err := envconfig.Init(&conf)
require.Nil(t, err)
require.Equal(t, "localhost", conf.MySQL.Master.Address)
require.Equal(t, 3306, conf.MySQL.Master.Port)
require.Equal(t, time.Minute*1, conf.MySQL.Timeout)
os.Setenv("myTimeout", "2m")
os.Setenv("myTimeout2", "20m")
err = envconfig.Init(&conf)
require.Nil(t, err)
require.Equal(t, "localhost", conf.MySQL.Master.Address)
require.Equal(t, 3306, conf.MySQL.Master.Port)
require.Equal(t, time.Minute*2, conf.MySQL.Timeout)
require.Equal(t, time.Minute*20, conf.MySQL.LocalTimeout)
}
func TestDefaultSlice(t *testing.T) {
var conf struct {
Names []string `envconfig:"default=foobar;barbaz"`
Ports []int `envconfig:"default=900;100"`
Shards []struct {
Name string
Addr string
} `envconfig:"default={foobar;localhost:2929};{barbaz;localhost:2828}"`
}
os.Setenv("NAMES", "")
os.Setenv("PORTS", "")
os.Setenv("SHARDS", "")
err := envconfig.Init(&conf)
require.Nil(t, err)
require.Equal(t, 2, len(conf.Names))
require.Equal(t, "foobar", conf.Names[0])
require.Equal(t, "barbaz", conf.Names[1])
require.Equal(t, 2, len(conf.Ports))
require.Equal(t, 900, conf.Ports[0])
require.Equal(t, 100, conf.Ports[1])
require.Equal(t, 2, len(conf.Shards))
require.Equal(t, "foobar", conf.Shards[0].Name)
require.Equal(t, "localhost:2929", conf.Shards[0].Addr)
require.Equal(t, "barbaz", conf.Shards[1].Name)
require.Equal(t, "localhost:2828", conf.Shards[1].Addr)
}
func TestInitNotAPointer(t *testing.T) {
err := envconfig.Init("foobar")
require.Equal(t, envconfig.ErrNotAPointer, err)
}
func TestInitPointerToAPointer(t *testing.T) {
type Conf struct {
Name string
}
var tmp *Conf
os.Setenv("NAME", "foobar")
err := envconfig.Init(&tmp)
require.Nil(t, err)
require.Equal(t, "foobar", tmp.Name)
}
func TestInitInvalidValueKind(t *testing.T) {
sl := []string{"foo", "bar"}
err := envconfig.Init(&sl)
require.Equal(t, envconfig.ErrInvalidValueKind, err)
}
func TestInvalidFieldValueKind(t *testing.T) {
var conf struct {
Foo interface{}
}
os.Setenv("FOO", "lalala")
err := envconfig.Init(&conf)
require.Equal(t, "envconfig: kind interface not supported", err.Error())
}
func TestInvalidSliceElementValueKind(t *testing.T) {
var conf struct {
Foo []interface{}
}
os.Setenv("FOO", "lalala")
err := envconfig.Init(&conf)
require.Equal(t, "envconfig: kind interface not supported", err.Error())
}
func TestParseEmptyTag(t *testing.T) {
var conf struct {
Name string `envconfig:""`
}
os.Setenv("NAME", "foobar")
err := envconfig.Init(&conf)
require.Nil(t, err)
require.Equal(t, "foobar", conf.Name)
}
func TestLeaveNil(t *testing.T) {
var conf struct {
MySQL *struct {
Name string
}
}
err := envconfig.InitWithOptions(&conf, envconfig.Options{
AllOptional: true,
LeaveNil: true,
})
require.Nil(t, err)
require.Nil(t, conf.MySQL)
}
type myMapType map[string]int
func (t *myMapType) Unmarshal(s string) error {
(*t)[s] = 1
return nil
}
func TestParseMapType(t *testing.T) {
var conf struct {
Map myMapType
}
os.Setenv("MAP", "a")
err := envconfig.Init(&conf)
require.Nil(t, err)
require.Equal(t, 1, conf.Map["a"])
}
func TestSliceOverwrite(t *testing.T) {
var conf struct {
Single []string
More []int
}
conf.Single = append(conf.Single, "foo", "bar")
conf.More = append(conf.More, 1, 2)
os.Setenv("SINGLE", "baz")
os.Setenv("MORE", "3,4")
err := envconfig.Init(&conf)
require.Nil(t, err)
require.Equal(t, []string{"baz"}, conf.Single)
require.Equal(t, []int{3, 4}, conf.More)
}