forked from ipfs/go-ipfs-cmds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_test.go
868 lines (808 loc) · 29 KB
/
parse_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
package cli
import (
"context"
"fmt"
"io"
"net/url"
"os"
"path/filepath"
"strings"
"testing"
"github.com/ipfs/boxo/files"
cmds "github.com/ipfs/go-ipfs-cmds"
)
type kvs map[string]interface{}
type words []string
func sameWords(a words, b words) bool {
if len(a) != len(b) {
return false
}
for i, w := range a {
if w != b[i] {
return false
}
}
return true
}
func sameKVs(a kvs, b kvs) bool {
if len(a) != len(b) {
return false
}
for k, v := range a {
if ks, ok := v.([]string); ok {
bks, _ := b[k].([]string)
for i := 0; i < len(ks); i++ {
if ks[i] != bks[i] {
return false
}
}
} else if v != b[k] {
return false
}
}
return true
}
func TestSameWords(t *testing.T) {
a := []string{"v1", "v2"}
b := []string{"v1", "v2", "v3"}
c := []string{"v2", "v3"}
d := []string{"v2"}
e := []string{"v2", "v3"}
f := []string{"v2", "v1"}
test := func(a words, b words, v bool) {
if sameWords(a, b) != v {
t.Errorf("sameWords('%v', '%v') != %v", a, b, v)
}
}
test(a, b, false)
test(a, a, true)
test(a, c, false)
test(b, c, false)
test(c, d, false)
test(c, e, true)
test(b, e, false)
test(a, b, false)
test(a, f, false)
test(e, f, false)
test(f, f, true)
}
func testOptionHelper(t *testing.T, cmd *cmds.Command, args string, expectedOpts kvs, expectedWords words, expectErr bool) {
req := &cmds.Request{}
err := parse(req, strings.Split(args, " "), cmd)
if err == nil {
err = req.FillDefaults()
}
if expectErr {
if err == nil {
t.Errorf("Command line '%v' parsing should have failed", args)
}
} else if err != nil {
t.Errorf("Command line '%v' failed to parse: %v", args, err)
} else if !sameWords(req.Arguments, expectedWords) || !sameKVs(kvs(req.Options), expectedOpts) {
t.Errorf("Command line '%v':\n parsed as %v %v\n instead of %v %v",
args, req.Options, req.Arguments, expectedOpts, expectedWords)
}
}
func TestOptionParsing(t *testing.T) {
cmd := &cmds.Command{
Options: []cmds.Option{
cmds.StringOption("string", "s", "a string"),
cmds.StringOption("flag", "alias", "multiple long"),
cmds.BoolOption("bool", "b", "a bool"),
cmds.StringsOption("strings", "r", "strings array"),
cmds.DelimitedStringsOption(",", "delimstrings", "d", "comma delimited string array"),
},
Subcommands: map[string]*cmds.Command{
"test": {},
"defaults": {
Options: []cmds.Option{
cmds.StringOption("opt", "o", "an option").WithDefault("def"),
},
},
},
}
testFail := func(args string) {
testOptionHelper(t, cmd, args, kvs{}, words{}, true)
}
test := func(args string, expectedOpts kvs, expectedWords words) {
testOptionHelper(t, cmd, args, expectedOpts, expectedWords, false)
}
test("test -", kvs{}, words{"-"})
testFail("-b -b")
test("test beep boop", kvs{}, words{"beep", "boop"})
testFail("-s")
test("-s foo", kvs{"string": "foo"}, words{})
test("-sfoo", kvs{"string": "foo"}, words{})
test("-s=foo", kvs{"string": "foo"}, words{})
test("-b", kvs{"bool": true}, words{})
test("-bs foo", kvs{"bool": true, "string": "foo"}, words{})
test("-sb", kvs{"string": "b"}, words{})
test("-b test foo", kvs{"bool": true}, words{"foo"})
test("--bool test foo", kvs{"bool": true}, words{"foo"})
testFail("--bool=foo")
testFail("--string")
test("--string foo", kvs{"string": "foo"}, words{})
test("--string=foo", kvs{"string": "foo"}, words{})
test("-- -b", kvs{}, words{"-b"})
test("test foo -b", kvs{"bool": true}, words{"foo"})
test("-b=false", kvs{"bool": false}, words{})
test("-b=true", kvs{"bool": true}, words{})
test("-b=false test foo", kvs{"bool": false}, words{"foo"})
test("-b=true test foo", kvs{"bool": true}, words{"foo"})
test("--bool=true test foo", kvs{"bool": true}, words{"foo"})
test("--bool=false test foo", kvs{"bool": false}, words{"foo"})
test("-b test true", kvs{"bool": true}, words{"true"})
test("-b test false", kvs{"bool": true}, words{"false"})
test("-b=FaLsE test foo", kvs{"bool": false}, words{"foo"})
test("-b=TrUe test foo", kvs{"bool": true}, words{"foo"})
test("-b test true", kvs{"bool": true}, words{"true"})
test("-b test false", kvs{"bool": true}, words{"false"})
test("-b --string foo test bar", kvs{"bool": true, "string": "foo"}, words{"bar"})
test("-b=false --string bar", kvs{"bool": false, "string": "bar"}, words{})
test("--strings a --strings b", kvs{"strings": []string{"a", "b"}}, words{})
test("--delimstrings a,b", kvs{"delimstrings": []string{"a", "b"}}, words{})
test("--delimstrings=a,b", kvs{"delimstrings": []string{"a", "b"}}, words{})
test("-d a,b", kvs{"delimstrings": []string{"a", "b"}}, words{})
test("-d=a,b", kvs{"delimstrings": []string{"a", "b"}}, words{})
test("-d=a,b -d c --delimstrings d", kvs{"delimstrings": []string{"a", "b", "c", "d"}}, words{})
testFail("foo test")
test("defaults", kvs{"opt": "def"}, words{})
test("defaults -o foo", kvs{"opt": "foo"}, words{})
test("--flag=foo", kvs{"flag": "foo"}, words{})
test("--alias=foo", kvs{"flag": "foo"}, words{})
testFail("--flag=bar --alias=foo")
testFail("--alias=bar --flag=foo")
testFail("--bad-flag")
testFail("--bad-flag=")
testFail("--bad-flag=xyz")
testFail("-z")
testFail("-zz--- --")
}
func TestDefaultOptionParsing(t *testing.T) {
testPanic := func(f func()) {
fnFinished := false
defer func() {
if r := recover(); fnFinished == true {
panic(r)
}
}()
f()
fnFinished = true
t.Error("expected panic")
}
testPanic(func() { cmds.StringOption("string", "s", "a string").WithDefault(0) })
testPanic(func() { cmds.StringOption("string", "s", "a string").WithDefault(false) })
testPanic(func() { cmds.StringOption("string", "s", "a string").WithDefault(nil) })
testPanic(func() { cmds.StringOption("string", "s", "a string").WithDefault([]string{"foo"}) })
testPanic(func() { cmds.StringsOption("strings", "a", "a string array").WithDefault(0) })
testPanic(func() { cmds.StringsOption("strings", "a", "a string array").WithDefault(false) })
testPanic(func() { cmds.StringsOption("strings", "a", "a string array").WithDefault(nil) })
testPanic(func() { cmds.StringsOption("strings", "a", "a string array").WithDefault("foo") })
testPanic(func() { cmds.StringsOption("strings", "a", "a string array").WithDefault([]bool{false}) })
testPanic(func() { cmds.DelimitedStringsOption(",", "dstrings", "d", "delimited string array").WithDefault(0) })
testPanic(func() { cmds.DelimitedStringsOption(",", "dstrs", "d", "delimited string array").WithDefault(false) })
testPanic(func() { cmds.DelimitedStringsOption(",", "dstrings", "d", "delimited string array").WithDefault(nil) })
testPanic(func() { cmds.DelimitedStringsOption(",", "dstrs", "d", "delimited string array").WithDefault("foo") })
testPanic(func() { cmds.DelimitedStringsOption(",", "dstrs", "d", "delimited string array").WithDefault([]int{0}) })
testPanic(func() { cmds.BoolOption("bool", "b", "a bool").WithDefault(0) })
testPanic(func() { cmds.BoolOption("bool", "b", "a bool").WithDefault(1) })
testPanic(func() { cmds.BoolOption("bool", "b", "a bool").WithDefault(nil) })
testPanic(func() { cmds.BoolOption("bool", "b", "a bool").WithDefault([]bool{false}) })
testPanic(func() { cmds.BoolOption("bool", "b", "a bool").WithDefault([]string{"foo"}) })
testPanic(func() { cmds.UintOption("uint", "u", "a uint").WithDefault(int(0)) })
testPanic(func() { cmds.UintOption("uint", "u", "a uint").WithDefault(int32(0)) })
testPanic(func() { cmds.UintOption("uint", "u", "a uint").WithDefault(int64(0)) })
testPanic(func() { cmds.UintOption("uint", "u", "a uint").WithDefault(uint64(0)) })
testPanic(func() { cmds.UintOption("uint", "u", "a uint").WithDefault(uint32(0)) })
testPanic(func() { cmds.UintOption("uint", "u", "a uint").WithDefault(float32(0)) })
testPanic(func() { cmds.UintOption("uint", "u", "a uint").WithDefault(float64(0)) })
testPanic(func() { cmds.UintOption("uint", "u", "a uint").WithDefault(nil) })
testPanic(func() { cmds.UintOption("uint", "u", "a uint").WithDefault([]uint{0}) })
testPanic(func() { cmds.UintOption("uint", "u", "a uint").WithDefault([]string{"foo"}) })
testPanic(func() { cmds.Uint64Option("uint64", "v", "a uint64").WithDefault(int(0)) })
testPanic(func() { cmds.Uint64Option("uint64", "v", "a uint64").WithDefault(int32(0)) })
testPanic(func() { cmds.Uint64Option("uint64", "v", "a uint64").WithDefault(int64(0)) })
testPanic(func() { cmds.Uint64Option("uint64", "v", "a uint64").WithDefault(uint(0)) })
testPanic(func() { cmds.Uint64Option("uint64", "v", "a uint64").WithDefault(uint32(0)) })
testPanic(func() { cmds.Uint64Option("uint64", "v", "a uint64").WithDefault(float32(0)) })
testPanic(func() { cmds.Uint64Option("uint64", "v", "a uint64").WithDefault(float64(0)) })
testPanic(func() { cmds.Uint64Option("uint64", "v", "a uint64").WithDefault(nil) })
testPanic(func() { cmds.Uint64Option("uint64", "v", "a uint64").WithDefault([]uint64{0}) })
testPanic(func() { cmds.Uint64Option("uint64", "v", "a uint64").WithDefault([]string{"foo"}) })
testPanic(func() { cmds.IntOption("int", "i", "an int").WithDefault(int32(0)) })
testPanic(func() { cmds.IntOption("int", "i", "an int").WithDefault(int64(0)) })
testPanic(func() { cmds.IntOption("int", "i", "an int").WithDefault(uint(0)) })
testPanic(func() { cmds.IntOption("int", "i", "an int").WithDefault(uint32(0)) })
testPanic(func() { cmds.IntOption("int", "i", "an int").WithDefault(uint64(0)) })
testPanic(func() { cmds.IntOption("int", "i", "an int").WithDefault(float32(0)) })
testPanic(func() { cmds.IntOption("int", "i", "an int").WithDefault(float64(0)) })
testPanic(func() { cmds.IntOption("int", "i", "an int").WithDefault(nil) })
testPanic(func() { cmds.IntOption("int", "i", "an int").WithDefault([]int{0}) })
testPanic(func() { cmds.IntOption("int", "i", "an int").WithDefault([]string{"foo"}) })
testPanic(func() { cmds.Int64Option("int64", "j", "an int64").WithDefault(int(0)) })
testPanic(func() { cmds.Int64Option("int64", "j", "an int64").WithDefault(int32(0)) })
testPanic(func() { cmds.Int64Option("int64", "j", "an int64").WithDefault(uint(0)) })
testPanic(func() { cmds.Int64Option("int64", "j", "an int64").WithDefault(uint32(0)) })
testPanic(func() { cmds.Int64Option("int64", "j", "an int64").WithDefault(uint64(0)) })
testPanic(func() { cmds.Int64Option("int64", "j", "an int64").WithDefault(float32(0)) })
testPanic(func() { cmds.Int64Option("int64", "j", "an int64").WithDefault(float64(0)) })
testPanic(func() { cmds.Int64Option("int64", "j", "an int64").WithDefault(nil) })
testPanic(func() { cmds.Int64Option("int64", "j", "an int64").WithDefault([]int64{0}) })
testPanic(func() { cmds.Int64Option("int64", "j", "an int64").WithDefault([]string{"foo"}) })
testPanic(func() { cmds.FloatOption("float", "f", "a float64").WithDefault(int(0)) })
testPanic(func() { cmds.FloatOption("float", "f", "a float64").WithDefault(int32(0)) })
testPanic(func() { cmds.FloatOption("float", "f", "a float64").WithDefault(int64(0)) })
testPanic(func() { cmds.FloatOption("float", "f", "a float64").WithDefault(uint(0)) })
testPanic(func() { cmds.FloatOption("float", "f", "a float64").WithDefault(uint32(0)) })
testPanic(func() { cmds.FloatOption("float", "f", "a float64").WithDefault(uint64(0)) })
testPanic(func() { cmds.FloatOption("float", "f", "a float64").WithDefault(float32(0)) })
testPanic(func() { cmds.FloatOption("float", "f", "a float64").WithDefault(nil) })
testPanic(func() { cmds.FloatOption("float", "f", "a float64").WithDefault([]int{0}) })
testPanic(func() { cmds.FloatOption("float", "f", "a float64").WithDefault([]string{"foo"}) })
cmd := &cmds.Command{
Subcommands: map[string]*cmds.Command{
"defaults": {
Options: []cmds.Option{
cmds.StringOption("string", "s", "a string").WithDefault("foo"),
cmds.StringsOption("strings1", "a", "a string array").WithDefault([]string{"foo"}),
cmds.StringsOption("strings2", "b", "a string array").WithDefault([]string{"foo", "bar"}),
cmds.DelimitedStringsOption(",", "dstrings1", "c", "a delimited string array").WithDefault([]string{"foo"}),
cmds.DelimitedStringsOption(",", "dstrings2", "d", "a delimited string array").WithDefault([]string{"foo", "bar"}),
cmds.BoolOption("boolT", "t", "a bool").WithDefault(true),
cmds.BoolOption("boolF", "a bool").WithDefault(false),
cmds.UintOption("uint", "u", "a uint").WithDefault(uint(1)),
cmds.Uint64Option("uint64", "v", "a uint64").WithDefault(uint64(1)),
cmds.IntOption("int", "i", "an int").WithDefault(int(1)),
cmds.Int64Option("int64", "j", "an int64").WithDefault(int64(1)),
cmds.FloatOption("float", "f", "a float64").WithDefault(float64(1)),
},
},
},
}
test := func(args string, expectedOpts kvs, expectedWords words) {
testOptionHelper(t, cmd, args, expectedOpts, expectedWords, false)
}
test("defaults", kvs{
"string": "foo",
"strings1": []string{"foo"},
"strings2": []string{"foo", "bar"},
"dstrings1": []string{"foo"},
"dstrings2": []string{"foo", "bar"},
"boolT": true,
"boolF": false,
"uint": uint(1),
"uint64": uint64(1),
"int": int(1),
"int64": int64(1),
"float": float64(1),
}, words{})
test("defaults --string baz --strings1=baz -b baz -b=foo -c=foo -d=foo,baz,bing -d=zip,zap -d=zorp -t=false --boolF -u=0 -v=10 -i=-5 -j=10 -f=-3.14", kvs{
"string": "baz",
"strings1": []string{"baz"},
"strings2": []string{"baz", "foo"},
"dstrings1": []string{"foo"},
"dstrings2": []string{"foo", "baz", "bing", "zip", "zap", "zorp"},
"boolT": false,
"boolF": true,
"uint": uint(0),
"uint64": uint64(10),
"int": int(-5),
"int64": int64(10),
"float": float64(-3.14),
}, words{})
}
func TestArgumentParsing(t *testing.T) {
rootCmd := &cmds.Command{
Subcommands: map[string]*cmds.Command{
"noarg": {},
"onearg": {
Arguments: []cmds.Argument{
cmds.StringArg("a", true, false, "some arg"),
},
},
"twoargs": {
Arguments: []cmds.Argument{
cmds.StringArg("a", true, false, "some arg"),
cmds.StringArg("b", true, false, "another arg"),
},
},
"variadic": {
Arguments: []cmds.Argument{
cmds.StringArg("a", true, true, "some arg"),
},
},
"optional": {
Arguments: []cmds.Argument{
cmds.StringArg("b", false, true, "another arg"),
},
},
"optionalsecond": {
Arguments: []cmds.Argument{
cmds.StringArg("a", true, false, "some arg"),
cmds.StringArg("b", false, false, "another arg"),
},
},
"reversedoptional": {
Arguments: []cmds.Argument{
cmds.StringArg("a", false, false, "some arg"),
cmds.StringArg("b", true, false, "another arg"),
},
},
},
}
test := func(cmd words, f *os.File, res words) {
if f != nil {
if _, err := f.Seek(0, io.SeekStart); err != nil {
t.Fatal(err)
}
}
ctx := context.Background()
req, err := Parse(ctx, cmd, f, rootCmd)
if err != nil {
t.Errorf("Command '%v' should have passed parsing: %v", cmd, err)
}
if !sameWords(req.Arguments, res) {
t.Errorf("Arguments parsed from '%v' are '%v' instead of '%v'", cmd, req.Arguments, res)
}
}
testFail := func(cmd words, fi *os.File, msg string) {
_, err := Parse(context.Background(), cmd, nil, rootCmd)
if err == nil {
t.Errorf("Should have failed: %v", msg)
}
}
test([]string{"noarg"}, nil, []string{})
testFail([]string{"noarg", "value!"}, nil, "provided an arg, but command didn't define any")
test([]string{"onearg", "value!"}, nil, []string{"value!"})
testFail([]string{"onearg"}, nil, "didn't provide any args, arg is required")
test([]string{"twoargs", "value1", "value2"}, nil, []string{"value1", "value2"})
testFail([]string{"twoargs", "value!"}, nil, "only provided 1 arg, needs 2")
testFail([]string{"twoargs"}, nil, "didn't provide any args, 2 required")
test([]string{"variadic", "value!"}, nil, []string{"value!"})
test([]string{"variadic", "value1", "value2", "value3"}, nil, []string{"value1", "value2", "value3"})
testFail([]string{"variadic"}, nil, "didn't provide any args, 1 required")
test([]string{"optional", "value!"}, nil, []string{"value!"})
test([]string{"optional"}, nil, []string{})
test([]string{"optional", "value1", "value2"}, nil, []string{"value1", "value2"})
test([]string{"optionalsecond", "value!"}, nil, []string{"value!"})
test([]string{"optionalsecond", "value1", "value2"}, nil, []string{"value1", "value2"})
testFail([]string{"optionalsecond"}, nil, "didn't provide any args, 1 required")
testFail([]string{"optionalsecond", "value1", "value2", "value3"}, nil, "provided too many args, takes 2 maximum")
test([]string{"reversedoptional", "value1", "value2"}, nil, []string{"value1", "value2"})
test([]string{"reversedoptional", "value!"}, nil, []string{"value!"})
testFail([]string{"reversedoptional"}, nil, "didn't provide any args, 1 required")
testFail([]string{"reversedoptional", "value1", "value2", "value3"}, nil, "provided too many args, only takes 1")
}
func errEq(err1, err2 error) bool {
if err1 == nil && err2 == nil {
return true
}
if err1 == nil || err2 == nil {
return false
}
return err1.Error() == err2.Error()
}
func TestBodyArgs(t *testing.T) {
rootCmd := &cmds.Command{
Subcommands: map[string]*cmds.Command{
"noarg": {},
"stdinenabled": {
Arguments: []cmds.Argument{
cmds.StringArg("a", true, true, "some arg").EnableStdin(),
},
},
"stdinenabled2args": {
Arguments: []cmds.Argument{
cmds.StringArg("a", true, false, "some arg"),
cmds.StringArg("b", true, true, "another arg").EnableStdin(),
},
},
"stdinenablednotvariadic": {
Arguments: []cmds.Argument{
cmds.StringArg("a", true, false, "some arg").EnableStdin(),
},
},
"stdinenablednotvariadic2args": {
Arguments: []cmds.Argument{
cmds.StringArg("a", true, false, "some arg"),
cmds.StringArg("b", true, false, "another arg").EnableStdin(),
},
},
"optionalsecond": {
Arguments: []cmds.Argument{
cmds.StringArg("a", true, false, "some arg"),
cmds.StringArg("b", false, false, "another arg"),
},
},
"optionalstdin": {
Arguments: []cmds.Argument{
cmds.StringArg("a", true, false, "some arg"),
cmds.StringArg("b", false, false, "another arg").EnableStdin(),
},
},
"optionalvariadicstdin": {
Arguments: []cmds.Argument{
cmds.StringArg("a", true, false, "some arg"),
cmds.StringArg("b", false, true, "another arg").EnableStdin(),
},
},
},
}
// Use a temp file to simulate stdin
fileToSimulateStdin := func(t *testing.T, content string) *os.File {
fstdin, err := os.CreateTemp("", "")
if err != nil {
t.Fatal(err)
}
defer os.Remove(fstdin.Name())
if _, err := io.WriteString(fstdin, content); err != nil {
t.Fatal(err)
}
return fstdin
}
fstdin1 := fileToSimulateStdin(t, "stdin1")
fstdin12 := fileToSimulateStdin(t, "stdin1\nstdin2")
fstdin123 := fileToSimulateStdin(t, "stdin1\nstdin2\nstdin3")
var tcs = []struct {
cmd words
f *os.File
posArgs, varArgs words
parseErr error
bodyArgs bool
}{
{
cmd: words{"stdinenabled", "value1", "value2"}, f: nil,
posArgs: words{"value1", "value2"}, varArgs: nil,
parseErr: nil, bodyArgs: false,
},
{
cmd: words{"stdinenabled"}, f: fstdin1,
posArgs: words{"stdin1"}, varArgs: words{},
parseErr: nil, bodyArgs: true,
},
{
cmd: words{"stdinenabled", "value1"}, f: fstdin1,
posArgs: words{"value1"}, varArgs: words{},
parseErr: nil, bodyArgs: false,
},
{
cmd: words{"stdinenabled", "value1", "value2"}, f: fstdin1,
posArgs: words{"value1", "value2"}, varArgs: words{},
parseErr: nil, bodyArgs: false,
},
{
cmd: words{"stdinenabled"}, f: fstdin12,
posArgs: words{"stdin1"}, varArgs: words{"stdin2"},
parseErr: nil, bodyArgs: true,
},
{
cmd: words{"stdinenabled"}, f: fstdin123,
posArgs: words{"stdin1"}, varArgs: words{"stdin2", "stdin3"},
parseErr: nil, bodyArgs: true,
},
{
cmd: words{"stdinenabled2args", "value1", "value2"}, f: nil,
posArgs: words{"value1", "value2"}, varArgs: words{},
parseErr: nil, bodyArgs: false,
},
{
cmd: words{"stdinenabled2args", "value1"}, f: fstdin1,
posArgs: words{"value1", "stdin1"}, varArgs: words{},
parseErr: nil, bodyArgs: true,
},
{
cmd: words{"stdinenabled2args", "value1", "value2"}, f: fstdin1,
posArgs: words{"value1", "value2"}, varArgs: words{},
parseErr: nil, bodyArgs: false,
},
{
cmd: words{"stdinenabled2args", "value1", "value2", "value3"}, f: fstdin1,
posArgs: words{"value1", "value2", "value3"}, varArgs: words{},
parseErr: nil, bodyArgs: false,
},
{
cmd: words{"stdinenabled2args", "value1"}, f: fstdin12,
posArgs: words{"value1", "stdin1"}, varArgs: words{"stdin2"},
parseErr: nil, bodyArgs: true,
},
{
cmd: words{"stdinenablednotvariadic", "value1"}, f: nil,
posArgs: words{"value1"}, varArgs: words{},
parseErr: nil, bodyArgs: false,
},
{
cmd: words{"stdinenablednotvariadic"}, f: fstdin1,
posArgs: words{"stdin1"}, varArgs: words{},
parseErr: nil, bodyArgs: true,
},
{
cmd: words{"stdinenablednotvariadic", "value1"}, f: fstdin1,
posArgs: words{"value1"}, varArgs: words{"value1"},
parseErr: nil, bodyArgs: false,
},
{
cmd: words{"stdinenablednotvariadic2args", "value1", "value2"}, f: nil,
posArgs: words{"value1", "value2"}, varArgs: words{},
parseErr: nil, bodyArgs: false,
},
{
cmd: words{"stdinenablednotvariadic2args", "value1"}, f: fstdin1,
posArgs: words{"value1", "stdin1"}, varArgs: words{},
parseErr: nil, bodyArgs: true,
},
{
cmd: words{"stdinenablednotvariadic2args", "value1", "value2"}, f: fstdin1,
posArgs: words{"value1", "value2"}, varArgs: words{},
parseErr: nil, bodyArgs: false,
},
{
cmd: words{"stdinenablednotvariadic2args"}, f: fstdin1,
posArgs: words{}, varArgs: words{},
parseErr: fmt.Errorf(`argument %q is required`, "a"), bodyArgs: true,
},
{
cmd: words{"stdinenablednotvariadic2args", "value1"}, f: nil,
posArgs: words{"value1"}, varArgs: words{},
parseErr: fmt.Errorf(`argument %q is required`, "b"), bodyArgs: true,
},
{
cmd: words{"noarg"}, f: fstdin1,
posArgs: words{}, varArgs: words{},
parseErr: nil, bodyArgs: false,
},
{
cmd: words{"optionalsecond", "value1", "value2"}, f: fstdin1,
posArgs: words{"value1", "value2"}, varArgs: words{},
parseErr: nil, bodyArgs: false,
},
{
cmd: words{"optionalstdin", "value1"}, f: fstdin1,
posArgs: words{"value1"}, varArgs: words{"stdin1"},
parseErr: nil, bodyArgs: true,
},
{
cmd: words{"optionalstdin", "value1"}, f: nil,
posArgs: words{"value1"}, varArgs: words{},
parseErr: nil, bodyArgs: false,
},
{
cmd: words{"optionalstdin"}, f: fstdin1,
posArgs: words{"value1"}, varArgs: words{},
parseErr: fmt.Errorf(`argument %q is required`, "a"), bodyArgs: false,
},
{
cmd: words{"optionalvariadicstdin", "value1"}, f: nil,
posArgs: words{"value1"}, varArgs: words{},
parseErr: nil, bodyArgs: false,
},
{
cmd: words{"optionalvariadicstdin", "value1"}, f: fstdin1,
posArgs: words{"value1"}, varArgs: words{"stdin1"},
parseErr: nil, bodyArgs: true,
},
{
cmd: words{"optionalvariadicstdin", "value1"}, f: fstdin12,
posArgs: words{"value1"}, varArgs: words{"stdin1", "stdin2"},
parseErr: nil, bodyArgs: true,
},
}
for _, tc := range tcs {
if tc.f != nil {
if _, err := tc.f.Seek(0, io.SeekStart); err != nil {
t.Fatal(err)
}
}
req, err := Parse(context.Background(), tc.cmd, tc.f, rootCmd)
if err == nil {
err = req.Command.CheckArguments(req)
}
if !errEq(err, tc.parseErr) {
t.Fatalf("parsing request for cmd %q: expected error %q, got %q", tc.cmd, tc.parseErr, err)
}
if err != nil {
continue
}
if !sameWords(req.Arguments, tc.posArgs) {
t.Errorf("Arguments parsed from %v are %v instead of %v", tc.cmd, req.Arguments, tc.posArgs)
}
s := req.BodyArgs()
if !tc.bodyArgs {
if s != nil {
t.Fatalf("expected no BodyArgs for cmd %q", tc.cmd)
}
continue
}
if s == nil {
t.Fatalf("expected BodyArgs for cmd %q", tc.cmd)
}
var bodyArgs words
for s.Scan() {
bodyArgs = append(bodyArgs, s.Argument())
}
if err := s.Err(); err != nil {
t.Fatal(err)
}
if !sameWords(bodyArgs, tc.varArgs) {
t.Errorf("BodyArgs parsed from %v are %v instead of %v", tc.cmd, bodyArgs, tc.varArgs)
}
}
}
func Test_isURL(t *testing.T) {
for _, u := range []string{
"http://www.example.com",
"https://www.example.com",
} {
if isURL(u) == nil {
t.Errorf("expected url: %s", u)
}
}
for _, u := range []string{
"adir/afile",
"http:/ /afile",
"http:/a/file",
} {
if isURL(u) != nil {
t.Errorf("expected non-url: %s", u)
}
}
}
func Test_urlBase(t *testing.T) {
for _, test := range []struct{ url, base string }{
{"http://host", "host"},
{"http://host/test", "test"},
{"http://host/test?param=val", "test"},
{"http://host/test?param=val¶m2=val", "test"},
} {
u, err := url.Parse(test.url)
if err != nil {
t.Errorf("failed to parse %q: %v", test.url, err)
continue
}
if got := urlBase(u); got != test.base {
t.Errorf("expected %q but got %q", test.base, got)
}
}
}
func TestFileArgs(t *testing.T) {
rootCmd := &cmds.Command{
Subcommands: map[string]*cmds.Command{
"fileOp": {
Arguments: []cmds.Argument{
cmds.FileArg("path", true, true, "The path to the file to be operated upon.").EnableRecursive().EnableStdin(),
},
Options: []cmds.Option{
cmds.OptionRecursivePath, // a builtin option that allows recursive paths (-r, --recursive)
cmds.OptionHidden,
cmds.OptionIgnoreRules,
cmds.OptionIgnore,
},
},
},
}
mkTempFile := func(t *testing.T, dir, pattern, content string) *os.File {
pat := "test_tmpFile_"
if pattern != "" {
pat = pattern
}
tmpFile, err := os.CreateTemp(dir, pat)
if err != nil {
t.Fatal(err)
}
if _, err := io.WriteString(tmpFile, content); err != nil {
t.Fatal(err)
}
return tmpFile
}
tmpDir1, err := os.MkdirTemp("", "parsetest_fileargs_tmpdir_")
if err != nil {
t.Fatal(err)
}
tmpDir2, err := os.MkdirTemp("", "parsetest_utildir_")
if err != nil {
t.Fatal(err)
}
tmpFile1 := mkTempFile(t, "", "", "test1")
tmpFile2 := mkTempFile(t, tmpDir1, "", "toBeIgnored")
tmpFile3 := mkTempFile(t, tmpDir1, "", "test3")
ignoreFile := mkTempFile(t, tmpDir2, "", filepath.Base(tmpFile2.Name()))
tmpHiddenFile := mkTempFile(t, tmpDir1, ".test_hidden_file_*", "test")
defer func() {
for _, f := range []string{
tmpDir1,
tmpFile1.Name(),
tmpFile2.Name(),
tmpHiddenFile.Name(),
tmpFile3.Name(),
ignoreFile.Name(),
tmpDir2,
} {
os.Remove(f)
}
}()
var testCases = []struct {
cmd words
f *os.File
args words
parseErr error
}{
{
cmd: words{"fileOp"},
args: nil,
parseErr: fmt.Errorf("argument %q is required", "path"),
},
{
cmd: words{"fileOp", "--ignore", filepath.Base(tmpFile2.Name()), tmpDir1, tmpFile1.Name()}, f: nil,
args: words{tmpDir1, tmpFile1.Name(), tmpFile3.Name()},
parseErr: fmt.Errorf(notRecursiveFmtStr, tmpDir1, "r"),
},
{
cmd: words{"fileOp", tmpFile1.Name(), "--ignore", filepath.Base(tmpFile2.Name()), "--ignore"}, f: nil,
args: words{tmpDir1, tmpFile1.Name(), tmpFile3.Name()},
parseErr: fmt.Errorf("missing argument for option %q", "ignore"),
},
{
cmd: words{"fileOp", "-r", "--ignore", filepath.Base(tmpFile2.Name()), tmpDir1, tmpFile1.Name()}, f: nil,
args: words{tmpDir1, tmpFile1.Name(), tmpFile3.Name()},
parseErr: nil,
},
{
cmd: words{"fileOp", "--hidden", "-r", "--ignore", filepath.Base(tmpFile2.Name()), tmpDir1, tmpFile1.Name()}, f: nil,
args: words{tmpDir1, tmpFile1.Name(), tmpFile3.Name(), tmpHiddenFile.Name()},
parseErr: nil,
},
{
cmd: words{"fileOp", "-r", "--ignore", filepath.Base(tmpFile2.Name()), tmpDir1, tmpFile1.Name(), "--ignore", "anotherRule"}, f: nil,
args: words{tmpDir1, tmpFile1.Name(), tmpFile3.Name()},
parseErr: nil,
},
{
cmd: words{"fileOp", "-r", "--ignore-rules-path", ignoreFile.Name(), tmpDir1, tmpFile1.Name()}, f: nil,
args: words{tmpDir1, tmpFile1.Name(), tmpFile3.Name()},
parseErr: nil,
},
}
for _, tc := range testCases {
req, err := Parse(context.Background(), tc.cmd, tc.f, rootCmd)
if err == nil {
err = req.Command.CheckArguments(req)
}
if !errEq(err, tc.parseErr) {
t.Fatalf("parsing request for cmd %q: expected error %q, got %q", tc.cmd, tc.parseErr, err)
}
if err != nil {
continue
}
if len(tc.args) == 0 {
continue
}
expectedFileMap := make(map[string]bool)
for _, arg := range tc.args {
expectedFileMap[filepath.Base(arg)] = false
}
it := req.Files.Entries()
for it.Next() {
name := it.Name()
if _, ok := expectedFileMap[name]; ok {
expectedFileMap[name] = true
} else {
t.Errorf("found unexpected file %q in request %v", name, req)
}
file := it.Node()
files.Walk(file, func(fpath string, nd files.Node) error {
if fpath != "" {
if _, ok := expectedFileMap[fpath]; ok {
expectedFileMap[fpath] = true
} else {
t.Errorf("found unexpected file %q in request file arguments", fpath)
}
}
return nil
})
}
for p, found := range expectedFileMap {
if !found {
t.Errorf("failed to find expected path %q in req %v", p, req)
}
}
}
}