-
Notifications
You must be signed in to change notification settings - Fork 50
/
conf_test.go
1157 lines (1044 loc) · 28.2 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
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package conf_test
import (
"errors"
"fmt"
"os"
"strings"
"testing"
"time"
"github.com/ardanlabs/conf/v3"
"github.com/ardanlabs/conf/v3/yaml"
"github.com/google/go-cmp/cmp"
)
const (
success = "\u2713"
failed = "\u2717"
)
// =============================================================================
// CustomValue provides support for testing a custom value.
type CustomValue struct {
something string
}
// Set implements the Setter interface
func (c *CustomValue) Set(data string) error {
*c = CustomValue{something: fmt.Sprintf("@%s@", data)}
return nil
}
// String implements the Stringer interface
func (c CustomValue) String() string {
return c.something
}
// Equal implements the Equal "interface" for go-cmp
func (c CustomValue) Equal(o CustomValue) bool {
return c.something == o.something
}
// =============================================================================
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"`
Duration time.Duration `conf:"default:1s,flag:e-dur,short:d"`
}
type config struct {
AnInt int `conf:"default:9"`
AString string `conf:"default:B,short:s"`
Bool bool
Skip string `conf:"-"`
IP ip
DebugHost string `conf:"default:http://user:[email protected]:4000,mask"`
Password string `conf:"default:password,mask"`
Custom CustomValue `conf:"default:hello"`
Embed
}
// =============================================================================
func TestRequired(t *testing.T) {
t.Logf("\tTest: %d\tWhen required values are missing.", 1)
{
f := func(t *testing.T) {
os.Args = []string{"conf.test"}
var cfg struct {
TestInt int `conf:"required, default:1"`
TestString string
TestBool bool
}
_, err := conf.Parse("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 required env integer is zero.", 1)
{
f := func(t *testing.T) {
os.Args = []string{"conf.test"}
os.Setenv("TEST_TEST_INT", "0")
var cfg struct {
TestInt int `conf:"required"`
TestString string
TestBool bool
}
_, err := conf.Parse("TEST", &cfg)
if err != nil {
t.Fatalf("\t%s\tShould have parsed the required zero env integer : %s", failed, err)
}
t.Logf("\t%s\tShould have parsed the required zero env integer.", success)
}
t.Run("required-env-integer-zero", f)
}
t.Logf("\tTest: %d\tWhen required env string is empty.", 1)
{
f := func(t *testing.T) {
os.Args = []string{"conf.test"}
os.Setenv("TEST_TEST_STRING", "")
var cfg struct {
TestInt int
TestString string `conf:"required"`
TestBool bool
}
_, err := conf.Parse("TEST", &cfg)
if err != nil {
t.Fatalf("\t%s\tShould have parsed the required empty env string : %s", failed, err)
}
t.Logf("\t%s\tShould have parsed the required empty env string.", success)
}
t.Run("required-env-string-empty", f)
}
t.Logf("\tTest: %d\tWhen required env boolean is false.", 1)
{
f := func(t *testing.T) {
os.Args = []string{"conf.test"}
os.Setenv("TEST_TEST_BOOL", "false")
var cfg struct {
TestInt int
TestString string
TestBool bool `conf:"required"`
}
_, err := conf.Parse("TEST", &cfg)
if err != nil {
t.Fatalf("\t%s\tShould have parsed the required false env boolean : %s", failed, err)
}
t.Logf("\t%s\tShould have parsed the required false env boolean.", success)
}
t.Run("required-env-boolean-false", f)
}
t.Logf("\tTest: %d\tWhen struct has no fields.", 2)
{
f := func(t *testing.T) {
os.Args = []string{"conf.test"}
var cfg struct {
testInt int `conf:"required, default:1"`
testString string
testBool bool
}
_, err := conf.Parse("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) {
os.Args = []string{"conf.test", "--test-int", "1"}
var cfg struct {
TestInt int `conf:"required, default:1"`
TestString string
TestBool bool
}
_, err := conf.Parse("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) {
os.Args = []string{"conf.test"}
os.Setenv("TEST_TEST_INT", "1")
var cfg struct {
TestInt int `conf:"required, default:1"`
TestString string
TestBool bool
}
_, err := conf.Parse("TEST", &cfg)
if err != nil {
t.Fatalf("\t%s\tShould have parsed the required field on Env : %s", failed, err)
}
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"}}, "http://user:[email protected]:4000", "password", CustomValue{something: "@hello@"}, 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_DEBUG_HOST": "http://bill:[email protected]:4000", "TEST_PASSWORD": "gopher", "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"}}, "http://bill:[email protected]:4000", "gopher", CustomValue{something: "@hello@"}, Embed{"andy", time.Minute}},
},
{
"flag",
nil,
[]string{"conf.test", "--an-int", "1", "-s", "s", "--bool", "--skip", "skip", "--ip-name", "local", "--debug-host", "http://bill:[email protected]:4000", "--password", "gopher", "--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"}}, "http://bill:[email protected]:4000", "gopher", CustomValue{something: "@hello@"}, Embed{"andy", time.Minute}},
},
{
"multi",
map[string]string{"TEST_A_STRING": "s", "TEST_BOOL": "TRUE", "TEST_IP_NAME_VAR": "local", "TEST_DEBUG_HOST": "http://bill:[email protected]:4000", "TEST_PASSWORD": "gopher", "TEST_NAME": "andy", "TEST_DURATION": "1m"},
[]string{"conf.test", "--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"}}, "http://bill:[email protected]:4000", "gopher", CustomValue{something: "@hello@"}, 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) {
os.Args = tt.args
var cfg config
if _, err := conf.Parse("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\tShould have properly initialized struct value\n%s", failed, diff)
}
t.Logf("\t%s\tShould have properly initialized struct value.", success)
}
t.Run(tt.name, f)
}
}
}
}
func TestParseEmptyNamespace(t *testing.T) {
tests := []struct {
name string
envs map[string]string
args []string
want config
}{
{
"env",
map[string]string{"AN_INT": "1", "A_STRING": "s", "BOOL": "TRUE", "SKIP": "SKIP", "IP_NAME_VAR": "local", "DEBUG_HOST": "http://bill:[email protected]:4000", "PASSWORD": "gopher", "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"}}, "http://bill:[email protected]:4000", "gopher", CustomValue{something: "@hello@"}, Embed{"andy", time.Minute}},
},
}
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) {
os.Args = tt.args
var cfg config
if _, err := conf.Parse("", &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\tShould have properly initialized struct value\n%s", failed, 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
}
want := configArgs{
Port: 9000,
Args: conf.Args{"migrate", "seed"},
}
os.Args = []string{"conf.test", "--port", "9000", "migrate", "seed"}
var cfg configArgs
if _, err := conf.Parse("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) {
os.Args = []string{"conf.test"}
var cfg struct {
TestInt int
TestString string
TestBool bool
}
_, err := conf.Parse("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) {
os.Args = []string{"conf.test"}
var cfg []string
_, err := conf.Parse("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) {
os.Args = []string{"conf.test"}
var cfg struct {
TestInt int `conf:"default:"`
TestString string
TestBool bool
}
_, err := conf.Parse("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) {
os.Args = []string{"conf.test"}
var cfg struct {
TestInt int `conf:"short:ab"`
TestString string
TestBool bool
}
_, err := conf.Parse("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)
}
}
}
var withNamespace = `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/$TEST_IP_IP <string> (default: 127.0.0.0)
--ip-endpoints/$TEST_IP_ENDPOINTS <string>,[string...] (default: 127.0.0.1:200;127.0.0.1:829)
--debug-host/$TEST_DEBUG_HOST <string> (default: http://xxxxxx:[email protected]:4000)
--password/$TEST_PASSWORD <string> (default: xxxxxx)
--custom/$TEST_CUSTOM <value> (default: hello)
--name/$TEST_NAME <string> (default: bill)
--e-dur/-d/$TEST_DURATION <duration> (default: 1s)
--help/-h
display this help message`
var emptyNamespace = `Usage: conf.test [options] [arguments]
OPTIONS
--an-int/$AN_INT <int> (default: 9)
--a-string/-s/$A_STRING <string> (default: B)
--bool/$BOOL <bool>
--ip-name/$IP_NAME_VAR <string> (default: localhost)
--ip-ip/$IP_IP <string> (default: 127.0.0.0)
--ip-endpoints/$IP_ENDPOINTS <string>,[string...] (default: 127.0.0.1:200;127.0.0.1:829)
--debug-host/$DEBUG_HOST <string> (default: http://xxxxxx:[email protected]:4000)
--password/$PASSWORD <string> (default: xxxxxx)
--custom/$CUSTOM <value> (default: hello)
--name/$NAME <string> (default: bill)
--e-dur/-d/$DURATION <duration> (default: 1s)
--help/-h
display this help message`
var withNamespaceOptions = `Usage: conf.test [options] [arguments]
OPTIONS
--port/$TEST_PORT <int>
--help/-h
display this help message`
var emptyNamespaceOptions = `Usage: conf.test [options] [arguments]
OPTIONS
--port/$PORT <int>
--help/-h
display this help message`
func TestUsage(t *testing.T) {
tests := []struct {
name string
namespace string
envs map[string]string
want string
options string
}{
{
name: "with-namespace",
namespace: "TEST",
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"},
want: withNamespace,
options: withNamespaceOptions,
},
{
name: "empty-namespace",
namespace: "",
envs: map[string]string{"AN_INT": "1", "A_STRING": "s", "BOOL": "TRUE", "SKIP": "SKIP", "IP_NAME_VAR": "local", "NAME": "andy", "DURATION": "1m"},
want: emptyNamespace,
options: emptyNamespaceOptions,
},
}
t.Log("Given the need validate usage output.")
{
for testID, tt := range tests {
f := func(t *testing.T) {
t.Logf("\tTest: %d\tWhen testing %s", testID, tt.name)
{
os.Clearenv()
for k, v := range tt.envs {
os.Setenv(k, v)
}
os.Args = []string{"conf.test"}
var cfg config
if _, err := conf.Parse(tt.namespace, &cfg); err != nil {
fmt.Print(err)
return
}
got, err := conf.UsageInfo(tt.namespace, &cfg)
if err != nil {
fmt.Print(err)
return
}
got = strings.TrimRight(got, " \n")
t.Log(got)
gotS := strings.Split(got, "\n")
wantS := strings.Split(tt.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.UsageInfo(tt.namespace, &cfg)
if err != nil {
fmt.Print(err)
return
}
got = strings.TrimRight(got, " \n")
gotS := strings.Split(got, "\n")
wantS := strings.Split(tt.options, "\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.Run(tt.name, f)
}
}
}
func ExampleString() {
tt := struct {
envs map[string]string
}{
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)
}
os.Args = []string{"conf.test"}
var cfg config
if _, err := conf.Parse("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]
// --debug-host=http://xxxxxx:[email protected]:4000
// --password=xxxxxx
// --custom=@hello@
// --name=andy
// --e-dur/-d=1m0s
}
type ConfExplicit struct {
Version conf.Version
Address string
}
type ConfImplicit struct {
conf.Version
Address string
}
func TestVersionExplicit(t *testing.T) {
tests := []struct {
name string
config ConfExplicit
args []string
want string
wantErr bool
}{
{
name: "version",
args: []string{"--version"},
config: ConfExplicit{
Version: conf.Version{
Build: "v1.0.0",
},
},
wantErr: false,
want: "Version: v1.0.0",
},
{
name: "vershort",
args: []string{"conf.test", "-v"},
config: ConfExplicit{
Version: conf.Version{
Build: "v1.0.0",
},
},
wantErr: false,
want: "Version: v1.0.0",
},
{
name: "verdes",
args: []string{"conf.test", "-version"},
config: ConfExplicit{
Version: conf.Version{
Build: "v1.0.0",
Desc: "Service Description",
},
},
wantErr: false,
want: "Version: v1.0.0\nService Description",
},
{
name: "verdesshort",
args: []string{"conf.test", "-v"},
config: ConfExplicit{
Version: conf.Version{
Build: "v1.0.0",
Desc: "Service Description",
},
},
wantErr: false,
want: "Version: v1.0.0\nService Description",
},
{
name: "desshort",
args: []string{"conf.test", "-v"},
config: ConfExplicit{
Version: conf.Version{
Desc: "Service Description",
},
},
wantErr: false,
want: "Service Description",
},
{
name: "none",
args: []string{"conf.test", "-v"},
config: ConfExplicit{},
want: "",
wantErr: false,
},
}
t.Log("Given the need validate version output.")
{
for i, tt := range tests {
t.Logf("\tTest: %d\tWhen using an explicit struct.", i)
{
f := func(t *testing.T) {
os.Args = tt.args
if help, err := conf.Parse("APP", &tt.config); err != nil {
if err == conf.ErrHelpWanted {
if diff := cmp.Diff(tt.want, help); 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.Run(tt.name, f)
}
}
}
}
func TestVersionImplicit(t *testing.T) {
tests := []struct {
name string
config ConfImplicit
args []string
want string
wantErr bool
}{
{
name: "only version",
args: []string{"conf.test", "--version"},
config: ConfImplicit{
Version: conf.Version{
Build: "v1.0.0",
},
},
wantErr: false,
want: "Version: v1.0.0",
},
{
name: "only version shortcut",
args: []string{"conf.test", "-v"},
config: ConfImplicit{
Version: conf.Version{
Build: "v1.0.0",
},
},
wantErr: false,
want: "Version: v1.0.0",
},
{
name: "version and description",
args: []string{"conf.test", "-version"},
config: ConfImplicit{
Version: conf.Version{
Build: "v1.0.0",
Desc: "Service Description",
},
},
wantErr: false,
want: "Version: v1.0.0\nService Description",
},
{
name: "version and description shortcut",
args: []string{"conf.test", "-v"},
config: ConfImplicit{
Version: conf.Version{
Build: "v1.0.0",
Desc: "Service Description",
},
},
wantErr: false,
want: "Version: v1.0.0\nService Description",
},
{
name: "only description shortcut",
args: []string{"conf.test", "-v"},
config: ConfImplicit{
Version: conf.Version{
Desc: "Service Description",
},
},
wantErr: false,
want: "Service Description",
},
{
name: "no version",
args: []string{"conf.test", "-v"},
config: ConfImplicit{},
want: "",
wantErr: false,
},
}
t.Log("Given the need validate version output.")
{
for i, tt := range tests {
t.Logf("\tTest: %d\tWhen using an emplicit struct with.", i)
{
f := func(t *testing.T) {
os.Args = tt.args
if help, err := conf.Parse("APP", &tt.config); err != nil {
if err == conf.ErrHelpWanted {
if diff := cmp.Diff(tt.want, help); 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.Run(tt.name, f)
}
}
}
}
func TestParseBoolFlag(t *testing.T) {
type Inner struct {
Bool2 bool
}
type config struct {
Bool bool `conf:"short:b"`
Args conf.Args
}
type config2 struct {
Bool bool `conf:"short:b"`
Args conf.Args
Inner Inner
}
type config3 struct {
Bool bool `conf:"short:b"`
Args conf.Args
Inner
}
type args struct {
prefix string
cfg interface{}
parsers []conf.Parsers
}
tests := []struct {
name string
osags []string
args args
wantErr bool
expect interface{}
}{
{
name: "long w/o equals",
osags: []string{"cmd", "--bool", "extra"},
args: args{
cfg: &config{},
},
wantErr: false,
expect: &config{
Bool: true,
Args: conf.Args{"extra"},
},
},
{
name: "short w/o equals",
osags: []string{"cmd", "-b", "extra"},
args: args{
cfg: &config{},
},
wantErr: false,
expect: &config{
Bool: true,
Args: conf.Args{"extra"},
},
},
{
name: "long w/equals true",
osags: []string{"cmd", "--bool=true", "extra"},
args: args{
cfg: &config{},
},
wantErr: false,
expect: &config{
Bool: true,
Args: conf.Args{"extra"},
},
},
{
name: "short w/equals true",
osags: []string{"cmd", "-b=true", "extra"},
args: args{
cfg: &config{},
},
wantErr: false,
expect: &config{
Bool: true,
Args: conf.Args{"extra"},
},
},
{
name: "long w/equals false",
osags: []string{"cmd", "--bool=false", "extra"},
args: args{
cfg: &config{},
},
wantErr: false,
expect: &config{
Bool: false,
Args: conf.Args{"extra"},
},
},
{
name: "short w/equals false",
osags: []string{"cmd", "-b=false", "extra"},
args: args{
cfg: &config{},
},
wantErr: false,
expect: &config{
Bool: false,
Args: conf.Args{"extra"},
},
},
{
name: "just long flag",
osags: []string{"cmd", "--bool"},
args: args{
cfg: &config{},
},
wantErr: false,
expect: &config{
Bool: true,
Args: conf.Args{},
},
},
{
name: "just short flag",
osags: []string{"cmd", "-b"},
args: args{
cfg: &config{},
},
wantErr: false,
expect: &config{
Bool: true,
Args: conf.Args{},
},
},
{
name: "just extra",
osags: []string{"cmd", "extra"},
args: args{
cfg: &config{},
},
wantErr: false,
expect: &config{
Bool: false,
Args: conf.Args{"extra"},
},
},
{
name: "inner",
osags: []string{"cmd", "extra"},
args: args{
cfg: &config2{},
},
wantErr: false,
expect: &config2{
Bool: false,
Args: conf.Args{"extra"},
},
},
{
name: "embedded",
osags: []string{"cmd", "extra"},
args: args{
cfg: &config3{},
},
wantErr: false,
expect: &config3{
Bool: false,
Args: conf.Args{"extra"},
},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
tmpArgs := os.Args
t.Cleanup(func() {
os.Args = tmpArgs
})
os.Args = tt.osags
prefix := tt.args.prefix
if prefix == "" {
prefix = "CONF_TEST_PARSE_BOOL_FLAG"
}
_, err := conf.Parse(prefix, tt.args.cfg, tt.args.parsers...)
if (err != nil) != tt.wantErr {
t.Fatalf("parse bool flag with args: error = %v, wantErr %v", err, tt.wantErr)
}
if diff := cmp.Diff(tt.expect, tt.args.cfg); diff != "" {
t.Errorf("parse bool flag with args: cfg mismatch (-expect +got):\n%s", diff)
}
})
}
}
// =============================================================================
type internal struct {
RenamedC int `yaml:"c"`