forked from ardanlabs/conf
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconf_test.go
462 lines (415 loc) · 12.7 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
package conf_test
import (
"fmt"
"os"
"strings"
"testing"
"time"
"github.com/ardanlabs/conf"
"github.com/google/go-cmp/cmp"
)
const (
success = "\u2713"
failed = "\u2717"
)
type ip struct {
Name string `conf:"default:localhost,env:IP_NAME_VAR"`
IP string `conf:"default:127.0.0.0"`
Endpoints []string `conf:"default:127.0.0.1:200;127.0.0.1:829"`
}
type Embed struct {
Name string `conf:"default:bill,env:NAME"`
Duration time.Duration `conf:"default:1s,flag:e-dur,short:d,env:DURATION"`
}
type config struct {
AnInt int `conf:"default:9,env:AN_INT"`
AString string `conf:"default:B,short:s,env:A_STRING"`
Bool bool `conf:"env:BOOL"`
Skip string `conf:"-"`
IP ip
Embed
}
type configNoEnvNamespace struct {
AnInt int `conf:"default:9,env:AN_INT"`
AString string `conf:"default:B,short:s,env:A_STRING"`
Bool bool `conf:"env:BOOL"`
Skip string `conf:"-"`
IP ip
Embed
}
// =============================================================================
func TestRequired(t *testing.T) {
t.Logf("\tTest: %d\tWhen required values are missing.", 1)
{
f := func(t *testing.T) {
var cfg struct {
TestInt int `conf:"required"`
TestString string
TestBool bool
}
err := conf.Parse(nil, "TEST", &cfg)
if err == nil {
t.Fatalf("\t%s\tShould fail for missing required value.", failed)
}
t.Logf("\t%s\tShould fail for missing required value : %s", success, err)
}
t.Run("required-missing-value", f)
}
t.Logf("\tTest: %d\tWhen struct has no fields.", 2)
{
f := func(t *testing.T) {
var cfg struct {
testInt int `conf:"required, default:1"`
testString string
testBool bool
}
err := conf.Parse(nil, "TEST", &cfg)
if err == nil {
t.Fatalf("\t%s\tShould fail for struct with no exported fields.", failed)
}
t.Logf("\t%s\tShould fail for struct with no exported fields : %s", success, err)
}
t.Run("struct-missing-fields", f)
}
t.Logf("\tTest: %d\tWhen required values exist and are passed on args.", 3)
{
f := func(t *testing.T) {
var cfg struct {
TestInt int `conf:"default:1"`
TestString string
TestBool bool
}
err := conf.Parse([]string{"--test-int", "1"}, "TEST", &cfg)
if err != nil {
t.Fatalf("\t%s\tShould have parsed the required field on args : %s", failed, err)
}
t.Logf("\t%s\tShould have parsed the required field on args.", success)
}
t.Run("required-existing-fields-args", f)
}
t.Logf("\tTest: %d\tWhen required values exist and are passed on env.", 4)
{
f := func(t *testing.T) {
var cfg struct {
TestInt int `conf:"default:1, env:TEST_INT"`
TestString string
TestBool bool
}
os.Setenv("TEST_TEST_INT", "2")
err := conf.Parse(nil, "TEST", &cfg)
if err != nil {
t.Fatalf("\t%s\tShould have parsed the required field on Env : %s", failed, err)
}
expected := 2
if cfg.TestInt != expected {
t.Fatalf("\t%s\tExpected %d, got %d", failed, expected, cfg.TestInt)
}
t.Logf("\t%s\tShould have parsed the required field on Env.", success)
}
t.Run("required-existing-fields-args", f)
}
}
func TestParse(t *testing.T) {
tests := []struct {
name string
envs map[string]string
args []string
want config
}{
{
"default",
nil,
nil,
config{9, "B", false, "", ip{"localhost", "127.0.0.0", []string{"127.0.0.1:200", "127.0.0.1:829"}}, Embed{"bill", time.Second}},
},
{
"env",
map[string]string{"TEST_AN_INT": "1", "TEST_A_STRING": "s", "TEST_BOOL": "TRUE", "TEST_SKIP": "SKIP", "TEST_IP_NAME_VAR": "local", "TEST_NAME": "andy", "TEST_DURATION": "1m"},
nil,
config{1, "s", true, "", ip{"local", "127.0.0.0", []string{"127.0.0.1:200", "127.0.0.1:829"}}, Embed{"andy", time.Minute}},
},
{
"flag",
nil,
[]string{"--an-int", "1", "-s", "s", "--bool", "--skip", "skip", "--ip-name", "local", "--name", "andy", "--e-dur", "1m"},
config{1, "s", true, "", ip{"local", "127.0.0.0", []string{"127.0.0.1:200", "127.0.0.1:829"}}, Embed{"andy", time.Minute}},
},
{
"multi",
map[string]string{"TEST_A_STRING": "s", "TEST_BOOL": "TRUE", "TEST_IP_NAME_VAR": "local", "TEST_NAME": "andy", "TEST_DURATION": "1m"},
[]string{"--an-int", "2", "--bool", "--skip", "skip", "--name", "jack", "-d", "1ms"},
config{2, "s", true, "", ip{"local", "127.0.0.0", []string{"127.0.0.1:200", "127.0.0.1:829"}}, Embed{"jack", time.Millisecond}},
},
}
t.Log("Given the need to parse basic configuration.")
{
for i, tt := range tests {
t.Logf("\tTest: %d\tWhen checking with arguments %v", i, tt.args)
{
os.Clearenv()
for k, v := range tt.envs {
os.Setenv(k, v)
}
f := func(t *testing.T) {
var cfg config
if err := conf.Parse(tt.args, "TEST", &cfg); err != nil {
t.Fatalf("\t%s\tShould be able to Parse arguments : %s.", failed, err)
}
t.Logf("\t%s\tShould be able to Parse arguments.", success)
if diff := cmp.Diff(tt.want, cfg); diff != "" {
t.Fatalf("\t%s\t%s Should have properly initialized struct value\n%s", failed, tt.name, diff)
}
t.Logf("\t%s\tShould have properly initialized struct value.", success)
}
t.Run(tt.name, f)
}
}
}
}
func TestParseWithoutNamespace(t *testing.T) {
tests := []struct {
name string
envs map[string]string
args []string
want config
}{
{
"default",
nil,
nil,
config{9, "B", false, "", ip{"localhost", "127.0.0.0", []string{"127.0.0.1:200", "127.0.0.1:829"}}, Embed{"bill", time.Second}},
},
{
"env",
map[string]string{"AN_INT": "1", "A_STRING": "s", "BOOL": "TRUE", "SKIP": "SKIP", "IP_NAME_VAR": "local", "NAME": "andy", "DURATION": "1m"},
nil,
config{1, "s", true, "", ip{"local", "127.0.0.0", []string{"127.0.0.1:200", "127.0.0.1:829"}}, Embed{"andy", time.Minute}},
},
{
"flag",
nil,
[]string{"--an-int", "1", "-s", "s", "--bool", "--skip", "skip", "--ip-name", "local", "--name", "andy", "--e-dur", "1m"},
config{1, "s", true, "", ip{"local", "127.0.0.0", []string{"127.0.0.1:200", "127.0.0.1:829"}}, Embed{"andy", time.Minute}},
},
{
"multi",
map[string]string{"A_STRING": "s", "BOOL": "TRUE", "IP_NAME_VAR": "local", "NAME": "andy", "DURATION": "1m"},
[]string{"--an-int", "2", "--bool", "--skip", "skip", "--name", "jack", "-d", "1ms"},
config{2, "s", true, "", ip{"local", "127.0.0.0", []string{"127.0.0.1:200", "127.0.0.1:829"}}, Embed{"jack", time.Millisecond}},
},
}
t.Log("Given the need to parse basic configuration.")
{
for i, tt := range tests {
t.Logf("\tTest: %d\tWhen checking with arguments %v", i, tt.args)
{
os.Clearenv()
for k, v := range tt.envs {
os.Setenv(k, v)
}
f := func(t *testing.T) {
var cfg config
if err := conf.Parse(tt.args, "", &cfg); err != nil {
t.Fatalf("\t%s\tShould be able to Parse arguments : %s.", failed, err)
}
t.Logf("\t%s\tShould be able to Parse arguments.", success)
if diff := cmp.Diff(tt.want, cfg); diff != "" {
t.Fatalf("\t%s\t%s Should have properly initialized struct value\n%s", failed, tt.name, diff)
}
t.Logf("\t%s\tShould have properly initialized struct value.", success)
}
t.Run(tt.name, f)
}
}
}
}
func TestParse_Args(t *testing.T) {
t.Log("Given the need to capture remaining command line arguments after flags.")
{
type configArgs struct {
Port int
Args conf.Args
}
args := []string{"--port", "9000", "migrate", "seed"}
want := configArgs{
Port: 9000,
Args: conf.Args{"migrate", "seed"},
}
var cfg configArgs
if err := conf.Parse(args, "TEST", &cfg); err != nil {
t.Fatalf("\t%s\tShould be able to Parse arguments : %s.", failed, err)
}
t.Logf("\t%s\tShould be able to Parse arguments.", success)
if diff := cmp.Diff(want, cfg); diff != "" {
t.Fatalf("\t%s\tShould have properly initialized struct value\n%s", failed, diff)
}
t.Logf("\t%s\tShould have properly initialized struct value.", success)
}
}
func TestErrors(t *testing.T) {
t.Log("Given the need to validate errors that can occur with Parse.")
{
t.Logf("\tTest: %d\tWhen passing bad values to Parse.", 0)
{
f := func(t *testing.T) {
var cfg struct {
TestInt int
TestString string
TestBool bool
}
err := conf.Parse(nil, "TEST", cfg)
if err == nil {
t.Fatalf("\t%s\tShould NOT be able to accept a value by value.", failed)
}
t.Logf("\t%s\tShould NOT be able to accept a value by value : %s", success, err)
}
t.Run("not-by-ref", f)
f = func(t *testing.T) {
var cfg []string
err := conf.Parse(nil, "TEST", &cfg)
if err == nil {
t.Fatalf("\t%s\tShould NOT be able to pass anything but a struct value.", failed)
}
t.Logf("\t%s\tShould NOT be able to pass anything but a struct value : %s", success, err)
}
t.Run("not-struct-value", f)
}
t.Logf("\tTest: %d\tWhen bad tags to Parse.", 1)
{
f := func(t *testing.T) {
var cfg struct {
TestInt int `conf:"default:"`
TestString string
TestBool bool
}
err := conf.Parse(nil, "TEST", &cfg)
if err == nil {
t.Fatalf("\t%s\tShould NOT be able to accept tag missing value.", failed)
}
t.Logf("\t%s\tShould NOT be able to accept tag missing value : %s", success, err)
}
t.Run("tag-missing-value", f)
f = func(t *testing.T) {
var cfg struct {
TestInt int `conf:"short:ab"`
TestString string
TestBool bool
}
err := conf.Parse(nil, "TEST", &cfg)
if err == nil {
t.Fatalf("\t%s\tShould NOT be able to accept invalid short tag.", failed)
}
t.Logf("\t%s\tShould NOT be able to accept invalid short tag : %s", success, err)
}
t.Run("tag-bad-short", f)
}
}
}
func TestUsage(t *testing.T) {
tt := struct {
name string
envs map[string]string
}{
name: "one-example",
envs: map[string]string{"TEST_AN_INT": "1", "TEST_A_STRING": "s", "TEST_BOOL": "TRUE", "TEST_SKIP": "SKIP", "TEST_IP_NAME_VAR": "local", "TEST_NAME": "andy", "TEST_DURATION": "1m"},
}
t.Log("Given the need validate usage output.")
{
t.Logf("\tTest: %d\tWhen using a basic struct.", 0)
{
os.Clearenv()
for k, v := range tt.envs {
os.Setenv(k, v)
}
var cfg config
if err := conf.Parse(nil, "TEST", &cfg); err != nil {
fmt.Print(err)
return
}
got, err := conf.Usage("TEST", &cfg)
if err != nil {
fmt.Print(err)
return
}
got = strings.TrimRight(got, " \n")
want := `Usage: conf.test [options] [arguments]
OPTIONS
--an-int/$TEST_AN_INT <int> (default: 9)
--a-string/-s/$TEST_A_STRING <string> (default: B)
--bool/$TEST_BOOL <bool>
--ip-name/$TEST_IP_NAME_VAR <string> (default: localhost)
--ip-ip <string> (default: 127.0.0.0)
--ip-endpoints <string>,[string...] (default: 127.0.0.1:200;127.0.0.1:829)
--name/$TEST_NAME <string> (default: bill)
--e-dur/-d/$TEST_DURATION <duration> (default: 1s)
--help/-h
display this help message`
t.Log(got)
gotS := strings.Split(got, "\n")
wantS := strings.Split(want, "\n")
if diff := cmp.Diff(gotS, wantS); diff != "" {
t.Errorf("\t%s\tShould match the output byte for byte. See diff:", failed)
t.Log(diff)
}
t.Logf("\t%s\tShould match byte for byte the output.", success)
}
t.Logf("\tTest: %d\tWhen using a struct with arguments.", 1)
{
var cfg struct {
Port int
Args conf.Args
}
got, err := conf.Usage("TEST", &cfg)
if err != nil {
fmt.Print(err)
return
}
got = strings.TrimRight(got, " \n")
want := `Usage: conf.test [options] [arguments]
OPTIONS
--port/$TEST_PORT <int>
--help/-h
display this help message`
gotS := strings.Split(got, "\n")
wantS := strings.Split(want, "\n")
if diff := cmp.Diff(gotS, wantS); diff != "" {
t.Errorf("\t%s\tShould match the output byte for byte. See diff:", failed)
t.Log(diff)
}
t.Logf("\t%s\tShould match byte for byte the output.", success)
}
}
}
func ExampleString() {
tt := struct {
name string
envs map[string]string
}{
name: "one-example",
envs: map[string]string{"TEST_AN_INT": "1", "TEST_S": "s", "TEST_BOOL": "TRUE", "TEST_SKIP": "SKIP", "TEST_IP_NAME": "local", "TEST_NAME": "andy", "TEST_DURATION": "1m"},
}
os.Clearenv()
for k, v := range tt.envs {
os.Setenv(k, v)
}
var cfg config
if err := conf.Parse(nil, "TEST", &cfg); err != nil {
fmt.Print(err)
return
}
out, err := conf.String(&cfg)
if err != nil {
fmt.Print(err)
return
}
fmt.Print(out)
// Output:
// --an-int=1
// --a-string/-s=B
// --bool=true
// --ip-name=localhost
// --ip-ip=127.0.0.0
// --ip-endpoints=[127.0.0.1:200 127.0.0.1:829]
// --name=andy
// --e-dur/-d=1m0s
}