-
Notifications
You must be signed in to change notification settings - Fork 44
/
cli_test.go
729 lines (682 loc) · 17.2 KB
/
cli_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
package cli
import (
"bytes"
"encoding/json"
"fmt"
"math"
"os"
"reflect"
"testing"
"github.com/labstack/gommon/color"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var donothing = func(*Context) error { return nil }
func TestTree(t *testing.T) {
cmd := &Command{Name: "cmd", Fn: donothing}
subcmd := &Command{Name: "subcmd", Fn: donothing}
tree := Tree(cmd, Tree(subcmd))
assert.Equal(t, tree.command.Name, "cmd")
require.Len(t, tree.forest, 1)
assert.Equal(t, tree.forest[0].command.Name, "subcmd")
assert.Len(t, tree.forest[0].forest, 0)
}
func TestRoot(t *testing.T) {
donothing := func(*Context) error { return nil }
root := Root(&Command{Name: "cmd", Fn: donothing},
Tree(&Command{Name: "subcmd", Fn: donothing}),
)
assert.Equal(t, root.Name, "cmd")
require.Len(t, root.children, 1)
assert.Equal(t, root.children[0].Name, "subcmd")
}
type argT struct {
Short bool `cli:"s" usage:"short flag"`
Short2 bool `cli:"2" usage:"another short flag"`
ShortAndLong string `cli:"S,long" usage:"short and long flags"`
ShortsAndLong int `cli:"x,y,abcd,omitof" usage:"many short and long flags"`
Long uint `cli:"long-flag" usage:"long flag"`
Required int8 `cli:"*required" usage:"required flag, note the *"`
Default uint8 `cli:"dft,default" usage:"default value" dft:"102"`
UnName uint16 `usage:"unname field"`
Int8 int8 `cli:"i8" usage:"type int8"`
Uint8 uint8 `cli:"u8" usage:"type uint8"`
Int16 int16 `cli:"i16" usage:"type int16"`
Uint16 uint16 `cli:"u16" usage:"type uint16"`
Int32 int32 `cli:"i32" usage:"type int32"`
Uint32 uint32 `cli:"u32" usage:"type uint32"`
Int64 int64 `cli:"i64" usage:"type int64"`
Uint64 uint64 `cli:"u64" usage:"type uint64"`
Float32 float32 `cli:"f32" usage:"type float32"`
Float64 float64 `cli:"f64" usage:"type float64"`
}
func toStr(i interface{}) string {
return fmt.Sprintf("%v", i)
}
func TestParse(t *testing.T) {
clr := color.Color{}
for i, tab := range []struct {
args []string
want argT
isErr bool
freedomArgs []string
}{
//Case: ENV
{
args: []string{"--required=0"},
want: argT{Default: 102},
},
//Case: missing required
{
args: []string{},
isErr: true,
},
//Case: undefined flag
{
args: []string{"--required=0", "-Q"},
isErr: true,
},
//Case: undefined flag
{
args: []string{"--required=0", "--KdjiiejdfwkHJH"},
isErr: true,
},
//Case: short flag group
{
args: []string{"--required=0", "-s2"},
want: argT{Default: 102, Short: true, Short2: true},
},
//Case: check default
{
args: []string{"--required=0"},
want: argT{Default: 102},
},
//Case: modify default
{
args: []string{"--required=0", "--dft", "55"},
want: argT{Default: 55},
},
//Case: modify default
{
args: []string{"--required=0", "--default", "55"},
want: argT{Default: 55},
},
//Case: UnName
{
args: []string{"--required=0", "--UnName", "64"},
want: argT{Default: 102, UnName: 64},
},
//Case: not a bool
{
args: []string{"--required=0", "-s=not-a-bool"},
isErr: true,
},
//Case: "" -> bool
{
args: []string{"--required=0", "-s"},
want: argT{Default: 102, Short: true},
},
//Case: "true" -> bool
{
args: []string{"--required=0", "-s=true"},
want: argT{Default: 102, Short: true},
},
//Case: non-zero integer -> bool
{
args: []string{"--required=0", "-s=1"},
want: argT{Default: 102, Short: true},
},
//Case: zero -> bool
{
args: []string{"--required=0", "-s=0"},
want: argT{Default: 102},
},
//Case: no -> bool
{
args: []string{"--required=0", "-s=no"},
want: argT{Default: 102},
},
//Case: not -> bool
{
args: []string{"--required=0", "-s=not"},
want: argT{Default: 102},
},
//Case: none -> bool
{
args: []string{"--required=0", "-s=none"},
want: argT{Default: 102},
},
//Case: false -> bool
{
args: []string{"--required=0", "-s=false"},
want: argT{Default: 102},
},
//Case: int64
{
args: []string{"--required=0", "--i64", toStr(12)},
want: argT{Default: 102, Int64: 12},
},
//Case: int64 overflow
{
args: []string{"--required=0", "--i64", toStr(uint64(math.MaxUint64))},
isErr: true,
},
//Case: uint64
{
args: []string{"--required=0", "--u64", toStr(12)},
want: argT{Default: 102, Uint64: 12},
},
//Case: max uint64
{
args: []string{"--required=0", "--u64", toStr(uint64(math.MaxUint64))},
want: argT{Default: 102, Uint64: uint64(math.MaxUint64)},
},
//Case: negative -> uint64
{
args: []string{"--required=0", "--u64", "-1"},
isErr: true,
},
//Case: int32
{
args: []string{"--required=0", "--i32", toStr(12)},
want: argT{Default: 102, Int32: 12},
},
//Case: int32 overflow
{
args: []string{"--required=0", "--i32", toStr(uint32(math.MaxUint32))},
isErr: true,
},
//Case: uint32
{
args: []string{"--required=0", "--u32", toStr(12)},
want: argT{Default: 102, Uint32: 12},
},
//Case: max uint32
{
args: []string{"--required=0", "--u32", toStr(uint32(math.MaxUint32))},
want: argT{Default: 102, Uint32: uint32(math.MaxUint32)},
},
//Case: negative -> uint32
{
args: []string{"--required=0", "--u32", "-1"},
isErr: true,
},
//Case: int16
{
args: []string{"--required=0", "--i16", toStr(12)},
want: argT{Default: 102, Int16: 12},
},
//Case: int16 overflow
{
args: []string{"--required=0", "--i16", toStr(uint16(math.MaxUint16))},
isErr: true,
},
//Case: uint16
{
args: []string{"--required=0", "--u16", toStr(12)},
want: argT{Default: 102, Uint16: 12},
},
//Case: max uint16
{
args: []string{"--required=0", "--u16", toStr(uint16(math.MaxUint16))},
want: argT{Default: 102, Uint16: uint16(math.MaxUint16)},
},
//Case: negative -> uint16
{
args: []string{"--required=0", "--u16", "-1"},
isErr: true,
},
//Case: int8
{
args: []string{"--required=0", "--i8", toStr(12)},
want: argT{Default: 102, Int8: 12},
},
//Case: int8 overflow
{
args: []string{"--required=0", "--i8", toStr(uint8(math.MaxUint8))},
isErr: true,
},
//Case: uint8
{
args: []string{"--required=0", "--u8", toStr(12)},
want: argT{Default: 102, Uint8: 12},
},
//Case: max uint8
{
args: []string{"--required=0", "--u8", toStr(uint8(math.MaxUint8))},
want: argT{Default: 102, Uint8: uint8(math.MaxUint8)},
},
//Case: negative -> uint8
{
args: []string{"--required=0", "--u8", "-1"},
isErr: true,
},
//Case: many invalid value
{
args: []string{"--required=0", "--u8", "-1", "--u8", "256"},
isErr: true,
},
//Case: too many value
{
args: []string{"--required=0=1"},
isErr: true,
},
//Case: float32
{
args: []string{"--required=0", "--f32", "12.34"},
want: argT{Default: 102, Float32: 12.34},
},
//Case: not a float32
{
args: []string{"--required=0", "--f32", "not-a-float32"},
isErr: true,
},
//Case: float32 overflow
{
args: []string{"--required=0", "--f32", "123456789123456789123456789123456789123456789"},
isErr: true,
},
//Case: float32 overflow
{
args: []string{"--required=0", "--f32", "-123456789123456789123456789123456789123456789"},
isErr: true,
},
//Case: float64
{
args: []string{"--required=0", "--f64=-1234.5678"},
want: argT{Default: 102, Float64: -1234.5678},
},
//Case: not a float64
{
args: []string{"--required=0", "--f64=not-a-float64"},
isErr: true,
},
//Case: fold flag not boolean
{
args: []string{"--required=0", "-2y"},
isErr: true,
},
//Case: test -F<value>
{
args: []string{"--required=0", "-Sshort-and-long"},
want: argT{Default: 102, ShortAndLong: "short-and-long"},
},
//Case: test `--`
{
args: []string{"--required=0", "--", "-Sshort-and-long"},
want: argT{Default: 102, ShortAndLong: ""},
freedomArgs: []string{"-Sshort-and-long"},
},
//Case: test flags and args
{
args: []string{"--required=0", "-Sshort-and-long", "abc"},
want: argT{Default: 102, ShortAndLong: "short-and-long"},
freedomArgs: []string{"abc"},
},
{
args: []string{"--required=0", "-Sshort-and-long", "abc", "-s", "xyz"},
want: argT{Default: 102, Short: true, ShortAndLong: "short-and-long"},
freedomArgs: []string{"abc", "xyz"},
},
} {
if tab.args == nil {
tab.args = []string{}
}
v := new(argT)
flagSet := parseArgv(tab.args, v, clr)
if tab.isErr {
if flagSet.err == nil {
t.Errorf("[%2d] want error, got nil", i)
}
continue
}
if flagSet.err != nil {
t.Errorf("[%2d] parseArgv error: %v", i, flagSet.err)
continue
}
if !reflect.DeepEqual(*v, tab.want) {
t.Errorf("[%2d] want %v, got %v", i, tab.want, *v)
}
if !stringsEqual(tab.freedomArgs, flagSet.args) {
t.Errorf("[%2d] want %v, got %v", i, tab.freedomArgs, flagSet.args)
}
}
//Case parse non-pointer object
if flagSet := parseArgv([]string{}, argT{}, clr); flagSet.err != errNotAPointer {
t.Errorf("want %v, got %v", errNotAPointer, flagSet.err)
}
if usage([]interface{}{argT{}}, clr, NormalStyle) != "" {
t.Errorf("want usage empty, but not")
}
//Case parse pointer, but not indirect a struct
tmp := 0
ptrInt := &tmp
if flagSet := parseArgv([]string{}, ptrInt, clr); flagSet.err != errNotAPointerToStruct {
t.Errorf("want %v, got %v", errNotAPointerToStruct, flagSet.err)
}
if usage([]interface{}{ptrInt}, clr, NormalStyle) != "" {
t.Errorf("want usage empty, but not")
}
//Case repeat tag
type tmpT struct {
A bool `cli:"a"`
B bool `cli:"a"`
}
if flagSet := parseArgv([]string{}, new(tmpT), clr); flagSet.err == nil {
t.Errorf("want error, got nil")
}
if usage([]interface{}{new(tmpT)}, clr, NormalStyle) != "" {
t.Errorf("want usage empty, but not")
}
type envT struct {
DefaultEnv string `cli:"default-env" usage:"default value" dft:"$ENV_CLI_TEST"`
}
envV := new(envT)
if flagSet := parseArgv([]string{}, envV, clr); flagSet.err != nil {
t.Errorf(flagSet.err.Error())
} else {
if want := os.Getenv("ENV_CLI_TEST"); want != envV.DefaultEnv {
t.Errorf("ENV_CLI_TEST want `%s`, got `%s`", want, envV.DefaultEnv)
}
}
}
func TestUsage(t *testing.T) {
clr := color.Color{}
clr.Disable()
got := usage([]interface{}{new(argT)}, clr, NormalStyle)
want := fmt.Sprintf(
` -s short flag
-2 another short flag
-S, --long short and long flags
-x, -y, --abcd, --omitof many short and long flags
--long-flag long flag
--required *required flag, note the *
--dft, --default[=102] default value
--UnName unname field
--i8 type int8
--u8 type uint8
--i16 type int16
--u16 type uint16
--i32 type int32
--u32 type uint32
--i64 type int64
--u64 type uint64
--f32 type float32
--f64 type float64
`)
assert.Equal(t, got, want)
got = usage([]interface{}{new(argT)}, clr, ManualStyle)
want = ` -s
short flag
-2
another short flag
-S, --long
short and long flags
-x, -y, --abcd, --omitof
many short and long flags
--long-flag
long flag
--required
*required flag, note the *
--dft, --default[=102]
default value
--UnName
unname field
--i8
type int8
--u8
type uint8
--i16
type int16
--u16
type uint16
--i32
type int32
--u32
type uint32
--i64
type int64
--u64
type uint64
--f32
type float32
--f64
type float64
`
assert.Equal(t, got, want)
}
func TestStructField(t *testing.T) {
type BaseT struct {
Help bool `cli:"!h,help" usage:"display help"`
}
type subT struct {
BaseT
Version string `cli:"v" usage:"display version" dft:"v0.0.1"`
}
args := []string{
"-h",
"-v=v1.1.1",
}
argv := new(subT)
clr := color.Color{}
flagSet := parseArgv(args, argv, clr)
if flagSet.err != nil {
t.Errorf("parseArgv error: %v", flagSet.err)
} else {
if argv.BaseT.Help != true {
t.Errorf("Help want true, but got false")
}
if argv.Version != "v1.1.1" {
t.Errorf("Version want v1.1.1, but got %s", argv.Version)
}
}
}
func stringsEqual(ss1, ss2 []string) bool {
if len(ss1) != len(ss2) {
return false
}
for i := 0; i < len(ss1); i++ {
if ss1[i] != ss2[i] {
return false
}
}
return true
}
func intsEqual(ss1, ss2 []uint32) bool {
if ss1 == nil && ss2 == nil {
return true
}
if len(ss1) != len(ss2) {
return false
}
for i := 0; i < len(ss1); i++ {
if ss1[i] != ss2[i] {
return false
}
}
return true
}
func mapEqual(m1, m2 map[uint32]string) bool {
if m1 == nil && m2 == nil {
return true
}
for k, v := range m1 {
if v2, ok := m2[k]; !ok {
return false
} else if v2 != v {
return false
}
}
for k, v := range m2 {
if v1, ok := m1[k]; !ok {
return false
} else if v1 != v {
return false
}
}
return true
}
func TestSliceAndMap(t *testing.T) {
clr := color.Color{}
type T struct {
Slice []uint32 `cli:"s,slice"`
Map map[uint32]string `cli:"m,map"`
}
for _, tab := range []struct {
args []string
want T
isErr bool
}{
{
args: []string{"--slice=12", "--slice", "23"},
want: T{Slice: []uint32{12, 23}},
},
{
args: []string{"-s12", "-s23"},
want: T{Slice: []uint32{12, 23}},
},
{
args: []string{"-s=12", "-s=23"},
want: T{Slice: []uint32{12, 23}},
},
{
args: []string{"-s", "12", "-s", "23"},
want: T{Slice: []uint32{12, 23}},
},
{
args: []string{"-s", "12", "-s", "not-a-number"},
isErr: true,
},
{
args: []string{"-m2=s", "-m3=y"},
want: T{Map: map[uint32]string{
2: "s",
3: "y",
}},
},
{
args: []string{"-m2=s", "-m", "3=y"},
want: T{Map: map[uint32]string{
2: "s",
3: "y",
}},
},
{
args: []string{"-m"},
isErr: true,
},
{
args: []string{"-ms=2", "-my=3"},
isErr: true,
},
} {
v := new(T)
flagSet := parseArgv(tab.args, v, clr)
if tab.isErr {
if flagSet.err == nil {
t.Errorf("want error, but not. tab=%v", tab)
}
continue
}
if flagSet.err != nil {
t.Errorf("TestSliceAndMap: error: %v", flagSet.err)
continue
}
if !intsEqual(tab.want.Slice, v.Slice) {
t.Errorf("want %v, got %v", tab.want.Slice, v.Slice)
}
if !mapEqual(tab.want.Map, v.Map) {
t.Errorf("want %v, got %v", tab.want.Map, v.Map)
}
}
}
func TestErrorSliceType(t *testing.T) {
type A struct {
Value string
}
type T struct {
Slice []A `cli:"s"`
}
clr := color.Color{}
v := new(T)
flagSet := parseArgv([]string{"-s4"}, v, clr)
if flagSet.err == nil {
t.Errorf("want error, but not")
}
}
func TestErrorMapType(t *testing.T) {
type A struct {
Value string
}
type T struct {
Map map[string]A `cli:"m"`
}
clr := color.Color{}
v := new(T)
flagSet := parseArgv([]string{"-mkey=val"}, v, clr)
if flagSet.err == nil {
t.Errorf("want error, but not")
}
}
func TestIsSet(t *testing.T) {
type argT struct {
A int `cli:"a,aa" dft:"1"`
B int `cli:"b"`
}
for i, tt := range []struct {
args []string
isSetA bool
isSetB bool
}{
{[]string{"app", "-a=1", "-b=1"}, true, true},
{[]string{"app", "--aa=1", "-b=1"}, true, true},
{[]string{"app", "-a=1"}, true, false},
{[]string{"app", "-b=1"}, false, true},
{[]string{"app"}, false, false},
} {
RunWithArgs(new(argT), tt.args, func(ctx *Context) error {
assert.Equal(t, ctx.IsSet("-a"), tt.isSetA, "case %d", i)
assert.Equal(t, ctx.IsSet("-a", "--aa"), tt.isSetA, "case %d", i)
assert.Equal(t, ctx.IsSet("-b"), tt.isSetB, "case %d", i)
return nil
})
}
}
func TestHelpCommand(t *testing.T) {
w := bytes.NewBufferString("")
root := &Command{Name: "root"}
help := HelpCommand("help command")
root.Register(help)
assert.Nil(t, root.RunWith([]string{"help"}, w, nil))
assert.Equal(t, w.String(), "Commands:\n\n help help command\n")
assert.Error(t, root.RunWith([]string{"help", "not-found"}, nil, nil))
}
func TestError(t *testing.T) {
assert.Equal(t, ExitError.Error(), "exit")
assert.Equal(t, throwCommandNotFound("cmd").Error(), "command cmd not found")
assert.Equal(t, throwMethodNotAllowed("POST").Error(), "method POST not allowed")
assert.Equal(t, throwRouterRepeat("R").Error(), "router R repeat")
clr := color.Color{}
clr.Disable()
assert.Equal(t, wrapErr(throwCommandNotFound("cmd"), "_end", clr).Error(), `ERR! command cmd not found_end`)
assert.Equal(t, argvError{isEmpty: true}.Error(), "argv list is empty")
assert.Equal(t, argvError{isOutOfRange: true}.Error(), "argv list out of range")
assert.Equal(t, argvError{ith: 1, msg: "ERROR MSG"}.Error(), "1th argv: ERROR MSG")
}
type customT struct {
K1 string
K2 int
}
func (t *customT) Decode(s string) error {
return json.Unmarshal([]byte(s), t)
}
func TestDecoder(t *testing.T) {
type argT struct {
D customT `cli:"d"`
}
v := new(argT)
clr := color.Color{}
flagSet := parseArgv([]string{`-d`, `{"k1": "string", "k2": 2}`}, v, clr)
assert.Nil(t, flagSet.err)
assert.Equal(t, v.D, customT{K1: "string", K2: 2})
}