-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flag_set_test.go
1157 lines (1141 loc) · 37.6 KB
/
flag_set_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 command
import (
"bytes"
stdcmp "cmp"
"reflect"
"testing"
. "github.com/arikkfir/justest"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
func TestNewFlagSet(t *testing.T) {
t.Parallel()
type testCase struct {
config any
expectedError string
expectedFlags func(tc *testCase) []*flagDef
expectedPositionalsTargets func(tc *testCase) []*[]string
}
testCases := map[string]testCase{
"nil config": {},
"config wih no flags": {config: &struct{}{}},
"config with ignored flags": {config: &struct{ MyField string }{MyField: "abc"}},
"config with a single flag": {
config: &struct {
MyField string `name:"my-field" env:"MY_FIELD" value-name:"VVV" desc:"desc" required:"true" inherited:"true" args:"false"`
}{MyField: "abc"},
expectedFlags: func(tc *testCase) []*flagDef {
return []*flagDef{
{
flagInfo: flagInfo{
Name: "my-field",
EnvVarName: ptrOf("MY_FIELD"),
HasValue: true,
ValueName: ptrOf("VVV"),
Description: ptrOf("desc"),
Required: ptrOf(true),
DefaultValue: "abc",
},
Inherited: true,
Targets: []reflect.Value{reflect.ValueOf(tc.config).Elem().FieldByName("MyField")},
},
}
},
},
"non struct pointer config is ignored": {
config: struct {
MyField string `name:"my-field" env:"MY_FIELD" value-name:"VVV" desc:"desc" required:"true" inherited:"true" args:"false"`
}{MyField: "abc"},
},
"config with multiple flags": {
config: &struct {
MyField1 string `name:"my-field1" env:"MY_FIELD1" value-name:"V1" desc:"desc1" required:"true" inherited:"true" args:"false"`
MyField2 string `name:"my-field2" env:"MY_FIELD2" value-name:"V2" desc:"desc2" required:"false" inherited:"false" args:"false"`
}{MyField1: "abc1", MyField2: "abc2"},
expectedFlags: func(tc *testCase) []*flagDef {
return []*flagDef{
{
flagInfo: flagInfo{
Name: "my-field1",
EnvVarName: ptrOf("MY_FIELD1"),
HasValue: true,
ValueName: ptrOf("V1"),
Description: ptrOf("desc1"),
Required: ptrOf(true),
DefaultValue: "abc1",
},
Inherited: true,
Targets: []reflect.Value{reflect.ValueOf(tc.config).Elem().FieldByName("MyField1")},
},
{
flagInfo: flagInfo{
Name: "my-field2",
EnvVarName: ptrOf("MY_FIELD2"),
HasValue: true,
ValueName: ptrOf("V2"),
Description: ptrOf("desc2"),
Required: ptrOf(false),
DefaultValue: "abc2",
},
Targets: []reflect.Value{reflect.ValueOf(tc.config).Elem().FieldByName("MyField2")},
},
}
},
},
"bad 'flag' tag": {
config: &struct {
MyField string `flag:"bad-value"`
}{},
expectedError: `^invalid field 'struct \{ MyField string "flag:\\"bad-value\\"" \}.MyField': invalid tag 'flag=bad-value': invalid syntax$`,
},
"field with 'flag=false' tag is ignored": {
config: &struct {
MyField string `flag:"false"`
}{},
},
"field with just 'flag=true' tag is picked up": {
config: &struct {
MyField string `flag:"true"`
}{},
expectedFlags: func(tc *testCase) []*flagDef {
return []*flagDef{
{
flagInfo: flagInfo{
Name: "my-field",
HasValue: true,
},
Targets: []reflect.Value{reflect.ValueOf(tc.config).Elem().FieldByName("MyField")},
},
}
},
},
"field with empty 'name' tag is rejected": {
config: &struct {
MyField string `name:""`
}{},
expectedError: `^invalid field 'struct \{ MyField string "name:\\"\\"" \}.MyField': invalid tag 'name=': must not be empty$`,
},
"value of 'name' tag is used": {
config: &struct {
MyField string `name:"a"`
}{},
expectedFlags: func(tc *testCase) []*flagDef {
return []*flagDef{
{
flagInfo: flagInfo{Name: "a", HasValue: true},
Targets: []reflect.Value{reflect.ValueOf(tc.config).Elem().FieldByName("MyField")},
},
}
},
},
"field with empty 'env' tag is rejected": {
config: &struct {
MyField string `env:""`
}{},
expectedError: `^invalid field 'struct \{ MyField string "env:\\"\\"" \}.MyField': invalid tag 'env=': must not be empty$`,
},
"value of 'env' tag is used and uppercased": {
config: &struct {
MyField string `env:"a"`
}{},
expectedFlags: func(tc *testCase) []*flagDef {
return []*flagDef{
{
flagInfo: flagInfo{Name: "my-field", EnvVarName: ptrOf("A"), HasValue: true},
Targets: []reflect.Value{reflect.ValueOf(tc.config).Elem().FieldByName("MyField")},
},
}
},
},
"field with empty 'value-name' tag is rejected": {
config: &struct {
MyField string `value-name:""`
}{},
expectedError: `^invalid field 'struct \{ MyField string "value-name:\\"\\"" \}.MyField': invalid tag 'value-name=': must not be empty$`,
},
"value of 'value-name' tag is used": {
config: &struct {
MyField string `value-name:"V"`
}{},
expectedFlags: func(tc *testCase) []*flagDef {
return []*flagDef{
{
flagInfo: flagInfo{Name: "my-field", HasValue: true, ValueName: ptrOf("V")},
Targets: []reflect.Value{reflect.ValueOf(tc.config).Elem().FieldByName("MyField")},
},
}
},
},
"field with empty 'description' tag is allowed": {
config: &struct {
MyField string `desc:""`
}{},
expectedFlags: func(tc *testCase) []*flagDef {
return []*flagDef{
{
flagInfo: flagInfo{Name: "my-field", HasValue: true, Description: ptrOf("")},
Targets: []reflect.Value{reflect.ValueOf(tc.config).Elem().FieldByName("MyField")},
},
}
},
},
"value of 'description' tag is used": {
config: &struct {
MyField string `desc:"Some Description"`
}{},
expectedFlags: func(tc *testCase) []*flagDef {
return []*flagDef{
{
flagInfo: flagInfo{Name: "my-field", HasValue: true, Description: ptrOf("Some Description")},
Targets: []reflect.Value{reflect.ValueOf(tc.config).Elem().FieldByName("MyField")},
},
}
},
},
"bad 'required' tag": {
config: &struct {
MyField string `required:"bad-value"`
}{},
expectedError: `^invalid field 'struct \{ MyField string "required:\\"bad-value\\"" \}.MyField': invalid tag 'required=bad-value': invalid syntax$`,
},
"field with 'required=false' tag is not required": {
config: &struct {
MyField string `required:"false"`
}{},
expectedFlags: func(tc *testCase) []*flagDef {
return []*flagDef{
{
flagInfo: flagInfo{Name: "my-field", HasValue: true, Required: ptrOf(false)},
Targets: []reflect.Value{reflect.ValueOf(tc.config).Elem().FieldByName("MyField")},
},
}
},
},
"field with 'required=true' tag is required": {
config: &struct {
MyField string `required:"true"`
}{},
expectedFlags: func(tc *testCase) []*flagDef {
return []*flagDef{
{
flagInfo: flagInfo{Name: "my-field", HasValue: true, Required: ptrOf(true)},
Targets: []reflect.Value{reflect.ValueOf(tc.config).Elem().FieldByName("MyField")},
},
}
},
},
"bad 'inherited' tag": {
config: &struct {
MyField string `inherited:"bad-value"`
}{},
expectedError: `^invalid field 'struct \{ MyField string "inherited:\\"bad-value\\"" \}.MyField': invalid tag 'inherited=bad-value': invalid syntax$`,
},
"field with 'inherited=false' tag is not inherited": {
config: &struct {
MyField string `inherited:"false"`
}{},
expectedFlags: func(tc *testCase) []*flagDef {
return []*flagDef{
{
flagInfo: flagInfo{Name: "my-field", HasValue: true},
Inherited: false,
Targets: []reflect.Value{reflect.ValueOf(tc.config).Elem().FieldByName("MyField")},
},
}
},
},
"field with 'inherited=true' tag is inherited": {
config: &struct {
MyField string `inherited:"true"`
}{},
expectedFlags: func(tc *testCase) []*flagDef {
return []*flagDef{
{
flagInfo: flagInfo{Name: "my-field", HasValue: true},
Inherited: true,
Targets: []reflect.Value{reflect.ValueOf(tc.config).Elem().FieldByName("MyField")},
},
}
},
},
"bad 'args' tag": {
config: &struct {
MyField string `args:"bad-value"`
}{},
expectedError: `^invalid field 'struct \{ MyField string "args:\\"bad-value\\"" \}.MyField': invalid tag 'args=bad-value': invalid syntax$`,
},
"field with 'args=false' tag is not marked as args": {
config: &struct {
MyField string `name:"f" args:"false"`
}{},
expectedFlags: func(tc *testCase) []*flagDef {
return []*flagDef{
{
flagInfo: flagInfo{Name: "f", HasValue: true},
Targets: []reflect.Value{reflect.ValueOf(tc.config).Elem().FieldByName("MyField")},
},
}
},
},
"field with 'args=true' tag is marked as args": {
config: &struct {
MyField []string `args:"true"`
}{},
expectedPositionalsTargets: func(tc *testCase) []*[]string {
typedVal := reflect.ValueOf(tc.config).Elem().FieldByName("MyField").Interface().([]string)
return []*[]string{&typedVal}
},
},
"field with 'name' and 'args' tags is rejected": {
config: &struct {
MyField []string `name:"f" args:"true"`
}{},
expectedError: `^invalid field 'struct \{ MyField \[\]string "name:\\"f\\" args:\\"true\\"" \}.MyField': invalid tag 'args=true': cannot be a flag as well$`,
},
"field with 'env' and 'args' tags is rejected": {
config: &struct {
MyField []string `env:"f" args:"true"`
}{},
expectedError: `^invalid field 'struct \{ MyField \[\]string "env:\\"f\\" args:\\"true\\"" \}.MyField': invalid tag 'args=true': cannot be a flag as well$`,
},
"field with 'value-name' and 'args' tags is rejected": {
config: &struct {
MyField []string `value-name:"f" args:"true"`
}{},
expectedError: `^invalid field 'struct \{ MyField \[\]string "value-name:\\"f\\" args:\\"true\\"" \}.MyField': invalid tag 'args=true': cannot be a flag as well$`,
},
"field with 'desc' and 'args' tags is rejected": {
config: &struct {
MyField []string `desc:"f" args:"true"`
}{},
expectedError: `^invalid field 'struct \{ MyField \[\]string "desc:\\"f\\" args:\\"true\\"" \}.MyField': invalid tag 'args=true': cannot be a flag as well$`,
},
"field with 'required' and 'args' tags is rejected": {
config: &struct {
MyField []string `required:"true" args:"true"`
}{},
expectedError: `^invalid field 'struct \{ MyField \[\]string "required:\\"true\\" args:\\"true\\"" \}.MyField': invalid tag 'args=true': cannot be a flag as well$`,
},
"field with 'inherited' and 'args' tags is rejected": {
config: &struct {
MyField []string `inherited:"true" args:"true"`
}{},
expectedError: `^invalid field 'struct \{ MyField \[\]string "inherited:\\"true\\" args:\\"true\\"" \}.MyField': invalid tag 'args=true': cannot be a flag as well$`,
},
"field with 'args' of incorrect type is rejected": {
config: &struct {
MyField int `args:"true"`
}{},
expectedError: `^invalid field 'struct \{ MyField int "args:\\"true\\"" \}.MyField': invalid tag 'args=true': must be typed as \[\]string$`,
},
"struct field cannot use 'args' tag": {
config: &struct {
MyField struct{} `args:"true"`
}{},
expectedError: `^invalid field 'struct \{ MyField struct \{\} "args:\\"true\\"" \}.MyField': invalid tag 'args=true': cannot be used on struct fields$`,
},
"flag name is inferred from field name": {
config: &struct {
MyField int `flag:"true"`
}{},
expectedFlags: func(tc *testCase) []*flagDef {
return []*flagDef{
{
flagInfo: flagInfo{Name: "my-field", HasValue: true, DefaultValue: "0"},
Targets: []reflect.Value{reflect.ValueOf(tc.config).Elem().FieldByName("MyField")},
},
}
},
},
"tag 'value-name' is not allowed for bool fields": {
config: &struct {
MyField bool `value-name:"VAL"`
}{},
expectedError: `^invalid field 'struct \{ MyField bool "value-name:\\"VAL\\"" \}.MyField': invalid tag 'value-name=VAL': not supported for bool fields$`,
},
"nested config": {
config: &struct {
OuterField1 string `name:"outer-field1" env:"OUTER_FIELD1" value-name:"outer-V1" desc:"outer-desc1" required:"true" inherited:"true"`
OuterField2 string `name:"outer-field2" env:"OUTER_FIELD2" value-name:"outer-V2" desc:"outer-desc2" required:"false" inherited:"false"`
OuterArgs []string `args:"true"`
MyStruct struct {
InnerField1 string `name:"inner-field1" env:"INNER_FIELD1" value-name:"inner-V1" desc:"inner-desc1" required:"true" inherited:"true"`
InnerField2 string `name:"inner-field2" env:"INNER_FIELD2" value-name:"inner-V2" desc:"inner-desc2" required:"false" inherited:"false"`
InnerArgs []string `args:"true"`
}
}{
OuterField1: "out1",
OuterField2: "out2",
MyStruct: struct {
InnerField1 string `name:"inner-field1" env:"INNER_FIELD1" value-name:"inner-V1" desc:"inner-desc1" required:"true" inherited:"true"`
InnerField2 string `name:"inner-field2" env:"INNER_FIELD2" value-name:"inner-V2" desc:"inner-desc2" required:"false" inherited:"false"`
InnerArgs []string `args:"true"`
}{
InnerField1: "inner1",
InnerField2: "inner2",
},
},
expectedFlags: func(tc *testCase) []*flagDef {
return []*flagDef{
{
flagInfo: flagInfo{
Name: "outer-field1",
EnvVarName: ptrOf("OUTER_FIELD1"),
HasValue: true,
ValueName: ptrOf("outer-V1"),
Description: ptrOf("outer-desc1"),
Required: ptrOf(true),
DefaultValue: "out1",
},
Inherited: true,
Targets: []reflect.Value{reflect.ValueOf(tc.config).Elem().FieldByName("OuterField1")},
},
{
flagInfo: flagInfo{
Name: "outer-field2",
EnvVarName: ptrOf("OUTER_FIELD2"),
HasValue: true,
ValueName: ptrOf("outer-V2"),
Description: ptrOf("outer-desc2"),
Required: ptrOf(false),
DefaultValue: "out2",
},
Inherited: false,
Targets: []reflect.Value{reflect.ValueOf(tc.config).Elem().FieldByName("OuterField2")},
},
{
flagInfo: flagInfo{
Name: "inner-field1",
EnvVarName: ptrOf("INNER_FIELD1"),
HasValue: true,
ValueName: ptrOf("inner-V1"),
Description: ptrOf("inner-desc1"),
Required: ptrOf(true),
DefaultValue: "inner1",
},
Inherited: true,
Targets: []reflect.Value{reflect.ValueOf(tc.config).Elem().FieldByName("MyStruct").FieldByName("InnerField1")},
},
{
flagInfo: flagInfo{
Name: "inner-field2",
EnvVarName: ptrOf("INNER_FIELD2"),
HasValue: true,
ValueName: ptrOf("inner-V2"),
Description: ptrOf("inner-desc2"),
Required: ptrOf(false),
DefaultValue: "inner2",
},
Targets: []reflect.Value{reflect.ValueOf(tc.config).Elem().FieldByName("MyStruct").FieldByName("InnerField2")},
},
}
},
expectedPositionalsTargets: func(tc *testCase) []*[]string {
valueOfOuterArgs := reflect.ValueOf(tc.config).Elem().FieldByName("OuterArgs").Interface().([]string)
valueOfInnerArgs := reflect.ValueOf(tc.config).Elem().FieldByName("MyStruct").FieldByName("InnerArgs").Interface().([]string)
return []*[]string{&valueOfOuterArgs, &valueOfInnerArgs}
},
},
"redeclared field cannot change environment variable": {
config: &struct {
MyField1 string `name:"my-field1" env:"MY_FIELD1"`
MyField2 string `name:"my-field1" env:"MY_FIELD2"`
}{},
expectedError: `^invalid field 'struct \{ MyField1 string "name:\\"my-field1\\" env:\\"MY_FIELD1\\""; MyField2 string "name:\\"my-field1\\" env:\\"MY_FIELD2\\"" }.MyField2': invalid tag 'env=MY_FIELD2': cannot redefine environment variable name$`,
},
"redeclared field can set environment variable": {
config: &struct {
MyField1 string `name:"my-field"`
MyField2 string `name:"my-field" env:"MF"`
}{},
expectedFlags: func(tc *testCase) []*flagDef {
return []*flagDef{
{
flagInfo: flagInfo{
Name: "my-field",
EnvVarName: ptrOf("MF"),
HasValue: true,
},
Targets: []reflect.Value{
reflect.ValueOf(tc.config).Elem().FieldByName("MyField1"),
reflect.ValueOf(tc.config).Elem().FieldByName("MyField2"),
},
},
}
},
},
"redeclared field cannot change has-value": {
config: &struct {
MyField1 string `name:"my-field1"`
MyField2 bool `name:"my-field1"`
}{},
expectedError: `^invalid field 'struct \{ MyField1 string "name:\\"my-field1\\""; MyField2 bool "name:\\"my-field1\\"" }.MyField2': incompatible field types detected \(is one a bool and another isn't\?\)$`,
},
"redeclared field cannot change value-name": {
config: &struct {
MyField1 string `name:"my-field1" value-name:"V1"`
MyField2 string `name:"my-field1" value-name:"V2"`
}{},
expectedError: `^invalid field 'struct \{ MyField1 string "name:\\"my-field1\\" value-name:\\"V1\\""; MyField2 string "name:\\"my-field1\\" value-name:\\"V2\\"" }.MyField2': invalid tag 'value-name=V2': cannot redefine value name$`,
},
"redeclared field can set value-name": {
config: &struct {
MyField1 string `name:"my-field"`
MyField2 string `name:"my-field" value-name:"VVV"`
}{},
expectedFlags: func(tc *testCase) []*flagDef {
return []*flagDef{
{
flagInfo: flagInfo{
Name: "my-field",
HasValue: true,
ValueName: ptrOf("VVV"),
},
Targets: []reflect.Value{
reflect.ValueOf(tc.config).Elem().FieldByName("MyField1"),
reflect.ValueOf(tc.config).Elem().FieldByName("MyField2"),
},
},
}
},
},
"redeclared field cannot change description": {
config: &struct {
MyField1 string `name:"my-field1" desc:"V1"`
MyField2 string `name:"my-field1" desc:"V2"`
}{},
expectedError: `^invalid field 'struct \{ MyField1 string "name:\\"my-field1\\" desc:\\"V1\\""; MyField2 string "name:\\"my-field1\\" desc:\\"V2\\"" }.MyField2': invalid tag 'desc=V2': cannot redefine description$`,
},
"redeclared field can set description": {
config: &struct {
MyField1 string `name:"my-field"`
MyField2 string `name:"my-field" desc:"DESC"`
}{},
expectedFlags: func(tc *testCase) []*flagDef {
return []*flagDef{
{
flagInfo: flagInfo{
Name: "my-field",
HasValue: true,
Description: ptrOf("DESC"),
},
Targets: []reflect.Value{
reflect.ValueOf(tc.config).Elem().FieldByName("MyField1"),
reflect.ValueOf(tc.config).Elem().FieldByName("MyField2"),
},
},
}
},
},
"redeclared field cannot change required status": {
config: &struct {
MyField1 string `name:"my-field1" required:"true"`
MyField2 string `name:"my-field1" required:"false"`
}{},
expectedError: `^invalid field 'struct \{ MyField1 string "name:\\"my-field1\\" required:\\"true\\""; MyField2 string "name:\\"my-field1\\" required:\\"false\\"" }.MyField2': invalid tag 'required=false': cannot redefine required status$`,
},
"redeclared field can set required status": {
config: &struct {
MyField1 string `name:"my-field"`
MyField2 string `name:"my-field" required:"true"`
}{},
expectedFlags: func(tc *testCase) []*flagDef {
return []*flagDef{
{
flagInfo: flagInfo{
Name: "my-field",
HasValue: true,
Required: ptrOf(true),
},
Targets: []reflect.Value{
reflect.ValueOf(tc.config).Elem().FieldByName("MyField1"),
reflect.ValueOf(tc.config).Elem().FieldByName("MyField2"),
},
},
}
},
},
"redeclared field cannot change default value": {
config: &struct {
MyField1 string `name:"my-field1"`
MyField2 string `name:"my-field1"`
}{
MyField1: "v1",
MyField2: "v2",
},
expectedError: `^invalid field 'struct \{ MyField1 string "name:\\"my-field1\\""; MyField2 string "name:\\"my-field1\\"" }.MyField2': incompatible default values detected: 'v1' vs 'v2'$`,
},
"redeclared field cannot change inherited status": {
config: &struct {
MyField1 string `name:"my-field1" inherited:"true"`
MyField2 string `name:"my-field1" inherited:"false"`
}{},
expectedError: `^invalid field 'struct \{ MyField1 string "name:\\"my-field1\\" inherited:\\"true\\""; MyField2 string "name:\\"my-field1\\" inherited:\\"false\\"" }.MyField2': incompatible inherited status detected: 'true' vs 'false'$`,
},
}
for name, tc := range testCases {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
valueOfConfig := reflect.ValueOf(tc.config)
if tc.expectedError != "" {
With(t).Verify(newFlagSet(nil, valueOfConfig)).Will(Fail(tc.expectedError)).OrFail()
} else {
fs, err := newFlagSet(nil, valueOfConfig)
With(t).Verify(err).Will(BeNil()).OrFail()
if tc.expectedFlags != nil {
expectedFlags := tc.expectedFlags(&tc)
With(t).
Verify(fs.flags).
Will(EqualTo(
expectedFlags,
cmp.AllowUnexported(flagDef{}),
cmpopts.SortSlices(func(a *flagDef, b *flagDef) bool { return stdcmp.Less(a.Name, b.Name) }),
)).
OrFail()
} else {
With(t).Verify(fs.flags).Will(BeNil()).OrFail()
}
if tc.expectedPositionalsTargets != nil {
With(t).Verify(fs.positionalsTargets).Will(EqualTo(tc.expectedPositionalsTargets(&tc))).OrFail()
} else {
With(t).Verify(fs.positionalsTargets).Will(BeNil()).OrFail()
}
}
})
}
}
func TestFlagSetWithArrays(t *testing.T) {
t.Parallel()
config := &struct {
MyArray []string `flag:"true" `
}{MyArray: []string{"v1", "v2"}}
valueOfConfig := reflect.ValueOf(config)
fs, err := newFlagSet(nil, valueOfConfig)
With(t).Verify(err).Will(BeNil()).OrFail()
if len(fs.flags) != 1 {
t.Fatalf("Expected 1 flag, got %d", len(fs.flags))
}
f := fs.flags[0]
With(t).Verify(f.Name).Will(EqualTo("my-array")).OrFail()
With(t).Verify(f.EnvVarName).Will(BeNil()).OrFail()
With(t).Verify(f.HasValue).Will(EqualTo(true)).OrFail()
With(t).Verify(f.ValueName).Will(BeNil()).OrFail()
With(t).Verify(f.Description).Will(BeNil()).OrFail()
With(t).Verify(f.Required).Will(BeNil()).OrFail()
With(t).Verify(f.DefaultValue).Will(EqualTo("v1,v2")).OrFail()
}
func TestFlagSetGetMergedFlagDefs(t *testing.T) {
t.Parallel()
type testCase struct {
parentConfig any
config any
expectedError string
expectedFlags func(tc *testCase) []*mergedFlagDef
}
testCases := map[string]testCase{
"no parent": {
config: &struct {
F string `name:"my-field" env:"MY_FIELD" desc:"desc" inherited:"true"`
S struct {
F string `name:"my-field" value-name:"VVV" required:"true" inherited:"true"`
}
}{
F: "abc",
S: struct {
F string `name:"my-field" value-name:"VVV" required:"true" inherited:"true"`
}{F: "abc"},
},
expectedFlags: func(tc *testCase) []*mergedFlagDef {
return []*mergedFlagDef{
{
flagInfo: flagInfo{
Name: "my-field",
EnvVarName: ptrOf("MY_FIELD"),
HasValue: true,
ValueName: ptrOf("VVV"),
Description: ptrOf("desc"),
Required: ptrOf(true),
DefaultValue: "abc",
},
flagDefs: []*flagDef{
{
flagInfo: flagInfo{
Name: "my-field",
EnvVarName: ptrOf("MY_FIELD"),
HasValue: true,
ValueName: ptrOf("VVV"),
Required: ptrOf(true),
Description: ptrOf("desc"),
DefaultValue: "abc",
},
Inherited: true,
Targets: []reflect.Value{
reflect.ValueOf(tc.config).Elem().FieldByName("F"),
reflect.ValueOf(tc.config).Elem().FieldByName("S").FieldByName("F"),
},
},
},
},
}
},
},
"flags merged across parents": {
parentConfig: &struct {
F1 string `name:"my-field1" env:"MF1" value-name:"VVV" inherited:"true"`
}{F1: "v1"},
config: &struct {
F1 string `name:"my-field1"`
F11 string `name:"my-field1" desc:"desc1"`
F2 string `name:"my-field2" env:"MF2" desc:"desc2"`
}{
F1: "v1",
F11: "v1",
F2: "v2",
},
expectedFlags: func(tc *testCase) []*mergedFlagDef {
return []*mergedFlagDef{
{
flagInfo: flagInfo{
Name: "my-field1",
EnvVarName: ptrOf("MF1"),
HasValue: true,
ValueName: ptrOf("VVV"),
Description: ptrOf("desc1"),
Required: ptrOf(false),
DefaultValue: "v1",
},
flagDefs: []*flagDef{
{
flagInfo: flagInfo{
Name: "my-field1",
HasValue: true,
Description: ptrOf("desc1"),
DefaultValue: "v1",
},
Inherited: false,
Targets: []reflect.Value{
reflect.ValueOf(tc.config).Elem().FieldByName("F1"),
reflect.ValueOf(tc.config).Elem().FieldByName("F11"),
},
},
{
flagInfo: flagInfo{
Name: "my-field1",
EnvVarName: ptrOf("MF1"),
HasValue: true,
ValueName: ptrOf("VVV"),
DefaultValue: "v1",
},
Inherited: true,
Targets: []reflect.Value{reflect.ValueOf(tc.parentConfig).Elem().FieldByName("F1")},
},
},
},
{
flagInfo: flagInfo{
Name: "my-field2",
EnvVarName: ptrOf("MF2"),
HasValue: true,
ValueName: ptrOf("VALUE"),
Description: ptrOf("desc2"),
Required: ptrOf(false),
DefaultValue: "v2",
},
flagDefs: []*flagDef{
{
flagInfo: flagInfo{
Name: "my-field2",
EnvVarName: ptrOf("MF2"),
HasValue: true,
Description: ptrOf("desc2"),
DefaultValue: "v2",
},
Targets: []reflect.Value{reflect.ValueOf(tc.config).Elem().FieldByName("F2")},
},
},
},
}
},
},
}
for name, tc := range testCases {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
var parent *flagSet
if tc.parentConfig != nil {
valueOfParentConfig := reflect.ValueOf(tc.parentConfig)
fs, err := newFlagSet(nil, valueOfParentConfig)
With(t).Verify(err).Will(BeNil()).OrFail()
parent = fs
}
valueOfConfig := reflect.ValueOf(tc.config)
if tc.expectedError != "" {
With(t).Verify(newFlagSet(parent, valueOfConfig)).Will(Fail(tc.expectedError)).OrFail()
} else {
fs, err := newFlagSet(parent, valueOfConfig)
With(t).Verify(err).Will(BeNil()).OrFail()
if tc.expectedFlags != nil {
mergedFlagDefs, err := fs.getMergedFlagDefs()
With(t).Verify(err).Will(BeNil()).OrFail()
With(t).
Verify(mergedFlagDefs).
Will(EqualTo(tc.expectedFlags(&tc), cmp.AllowUnexported(flagDef{}, mergedFlagDef{}))).OrFail()
} else {
With(t).Verify(fs.flags).Will(BeNil()).OrFail()
}
}
})
}
}
func TestFlagSetUsagePrinting(t *testing.T) {
t.Parallel()
type testCase struct {
parentConfig any
config any
width int
expectedSingleLineUsage string
expectedMultiLineUsage string
}
testCases := map[string]testCase{
"no parent, single flag for multiple fields in nested structure": {
config: &struct {
F string `name:"my-field" env:"MY_FIELD" desc:"desc" inherited:"true"`
S struct {
F string `name:"my-field" value-name:"VVV" required:"true" inherited:"true"`
}
}{
F: "abc",
S: struct {
F string `name:"my-field" value-name:"VVV" required:"true" inherited:"true"`
}{F: "abc"},
},
expectedSingleLineUsage: `--my-field=VVV`,
expectedMultiLineUsage: `
--my-field=VVV desc (default value: abc, environment variable:
MY_FIELD)
`,
},
"flags merged across parents": {
parentConfig: &struct {
F1 string `name:"my-field1" env:"MF1" value-name:"VVV" inherited:"true"`
}{F1: "v1"},
config: &struct {
F1 string `name:"my-field1" required:"true"`
F11 string `name:"my-field1" desc:"desc1"`
F2 bool `name:"my-field2" env:"MF2" desc:"desc2"`
}{
F1: "v1",
F11: "v1",
},
expectedSingleLineUsage: `--my-field1=VVV [--my-field2]`,
expectedMultiLineUsage: `
--my-field1=VVV desc1 (default value: v1, environment variable:
MF1)
[--my-field2] desc2 (default value: false, environment
variable: MF2)
`,
},
"positionals without flags": {
config: &struct {
Args []string `args:"true"`
}{},
expectedSingleLineUsage: `[ARGS...]`,
expectedMultiLineUsage: `
`,
},
"flags and positionals": {
config: &struct {
F1 string `name:"my-field1" value-name:"FF"`
F2 bool `name:"my-field2" env:"MF2" desc:"desc2"`
Args []string `args:"true"`
}{
F1: "v1",
},
expectedSingleLineUsage: `[--my-field1=FF] [--my-field2] [ARGS...]`,
expectedMultiLineUsage: `
[--my-field1=FF] default value: v1, environment variable: MY_FIELD1
[--my-field2] desc2 (default value: false, environment
variable: MF2)
`,
},
}
for name, tc := range testCases {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
var parent *flagSet
if tc.parentConfig != nil {
valueOfParentConfig := reflect.ValueOf(tc.parentConfig)
fs, err := newFlagSet(nil, valueOfParentConfig)
With(t).Verify(err).Will(BeNil()).OrFail()
parent = fs
}
valueOfConfig := reflect.ValueOf(tc.config)
fs, err := newFlagSet(parent, valueOfConfig)
With(t).Verify(err).Will(BeNil()).OrFail()
width := tc.width
if width == 0 {
width = 70
}
singleLine := &bytes.Buffer{}
With(t).Verify(fs.printFlagsSingleLine(singleLine)).Will(Succeed()).OrFail()
With(t).Verify(singleLine.String()).Will(EqualTo(tc.expectedSingleLineUsage)).OrFail()
multiLine, err := NewWrappingWriter(width)
With(t).Verify(err).Will(BeNil()).OrFail()
With(t).Verify(fs.printFlagsMultiLine(multiLine, "")).Will(Succeed()).OrFail()
With(t).Verify(multiLine.String()).Will(EqualTo(tc.expectedMultiLineUsage[1:])).OrFail()
})
}
}
func TestFlagSetApply(t *testing.T) {
t.Parallel()
type testCase struct {
parentConfig any
config any
envVars map[string]string
args []string
expectedParentConfig any
expectedConfig any
expectedError string
}
testCases := map[string]testCase{
"all types are supported from CLI": {
config: &struct {
String string `flag:"true"`
Int int `flag:"true"`
Float32 float32 `flag:"true"`
Float64 float64 `flag:"true"`
Bool bool `flag:"true"`
StringArray []string `flag:"true"`
IntArray []int `flag:"true"`
Float32Array []float32 `flag:"true"`
Float64Array []float64 `flag:"true"`
}{},
args: []string{
"--string", "s1",
"--int", "9",
"--float32", "1.2",
"--float64", "123.456",
"--bool",
"--string-array", `sa1,"s with space",sa3,,,"`,
"--int-array", `1,2,3,5,8`,
"--float32array", `1.2,3.4,5.6`,
"--float64array", `11.22,33.44,55.66`,
},
expectedConfig: &struct {
String string `flag:"true"`
Int int `flag:"true"`
Float32 float32 `flag:"true"`
Float64 float64 `flag:"true"`
Bool bool `flag:"true"`
StringArray []string `flag:"true"`
IntArray []int `flag:"true"`
Float32Array []float32 `flag:"true"`
Float64Array []float64 `flag:"true"`
}{
String: "s1",
Int: 9,
Float32: 1.2,
Float64: 123.456,
Bool: true,
StringArray: []string{"sa1", "s with space", "sa3", "", "", ""},
IntArray: []int{1, 2, 3, 5, 8},
Float32Array: []float32{1.2, 3.4, 5.6},
Float64Array: []float64{11.22, 33.44, 55.66},
},
},
"all types are supported from ENV": {
config: &struct {
String string `flag:"true"`
Int int `flag:"true"`
Float32 float32 `flag:"true"`
Float64 float64 `flag:"true"`
Bool bool `flag:"true"`
StringArray []string `flag:"true"`
IntArray []int `flag:"true"`
Float32Array []float32 `flag:"true"`
Float64Array []float64 `flag:"true"`
HTTPAllowedOrigins []string `flag:"true"`
}{},
envVars: map[string]string{
"STRING": "s1",
"INT": "9",
"FLOAT32": "1.2",
"FLOAT64": "123.456",
"BOOL": "true",
"STRING_ARRAY": `sa1,"s with space",sa3,,,`,
"INT_ARRAY": "1,2,3,5,8",
"FLOAT32ARRAY": "1.2,3.4,5.6",
"FLOAT64ARRAY": "11.22,33.44,55.66",
"HTTP_ALLOWED_ORIGINS": "a,b,c",
},
expectedConfig: &struct {
String string `flag:"true"`
Int int `flag:"true"`
Float32 float32 `flag:"true"`
Float64 float64 `flag:"true"`
Bool bool `flag:"true"`
StringArray []string `flag:"true"`
IntArray []int `flag:"true"`
Float32Array []float32 `flag:"true"`
Float64Array []float64 `flag:"true"`
HTTPAllowedOrigins []string `flag:"true"`
}{
String: "s1",