forked from spf13/cobra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
args_test.go
515 lines (450 loc) · 15.2 KB
/
args_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
package cobra
import (
"bytes"
"strings"
"testing"
)
func executeUsage(c *Command) (string, error) {
buf := new(bytes.Buffer)
c.SetOutput(buf)
err := c.Usage()
return buf.String(), err
}
func checkOutput(o string, t *testing.T, i string) {
str := map[rune]string{
'u': "Usage:",
'h': "Run 'c --help' for usage",
'c': "c [command]",
'v': "Valid Args:",
'a': "c [flags] [args]",
'f': "c [flags]",
}
for _, x := range "uhcva" {
b := strings.Contains(i, string(x))
if s := str[x]; b != strings.Contains(o, s) {
m := "Did not expect"
if b {
m = "Expected"
}
t.Errorf("%s to find '%s' in the output", m, s)
continue
}
if (x == 'a') && b {
return
}
}
}
func expectErrorAndCheckOutput(t *testing.T, err error, errK, o, i string) {
// expectError(err, t, errK)
// checkOutput(o, t, i)
}
type argsTestcase struct {
exerr string // Expected error key (see map[string][string])
args PositionalArgs // Args validator
wValid, wRun bool // Define `ValidArgs` in the command
rargs []string // Runtime args
}
var errStrings = map[string]string{
"run": `command "c" is not runnable`,
"runsub": `command "c" is not runnable; please provide a subcmd`,
"no": `"one" rejected; "c" does not accept args`,
"invalid": `invalid argument "a" for "c"`,
"unknown": `unknown command "one" for "c"`,
"less": "requires at least 2 arg(s), only received 1",
"more": "accepts at most 2 arg(s), received 3",
"notexact": "accepts 2 arg(s), received 3",
"notinrange": "accepts between 2 and 4 arg(s), received 1",
}
func newCmd(args PositionalArgs, wValid, wRun bool) *Command {
c := &Command{
Use: "c",
Short: "A generator",
Long: `Cobra is a CLI ...`,
//Run: emptyRun,
}
if args != nil {
c.Args = args
}
if wValid {
c.ValidArgs = []string{"one", "two", "three"}
}
if wRun {
c.Run = func(cmd *Command, args []string) {
//fmt.Println("RUN", args)
}
}
return c
}
func (tc *argsTestcase) test(t *testing.T) {
o, e := executeCommand(newCmd(tc.args, tc.wValid, tc.wRun), tc.rargs...)
if len(tc.exerr) > 0 {
// Expect error
if e == nil {
t.Fatal("Expected an error")
}
expected, ok := errStrings[tc.exerr]
if !ok {
t.Errorf(`key "%s" is not found in map "errStrings"`, tc.exerr)
return
}
if got := e.Error(); got != expected {
t.Errorf("Expected: %q, got: %q", expected, got)
}
} else {
// Expect success
if o != "" {
t.Errorf("Unexpected output: %v", o)
}
if e != nil {
t.Fatalf("Unexpected error: %v", e)
}
}
}
func testArgs(t *testing.T, tests map[string]argsTestcase) {
for name, tc := range tests {
t.Run(name, tc.test)
}
}
func TestArgs_No(t *testing.T) {
testArgs(t, map[string]argsTestcase{
" | ": {"", NoArgs, false, true, []string{}},
" | Arb": {"no", NoArgs, false, true, []string{"one"}},
"Valid | Valid": {"no", NoArgs, true, true, []string{"one"}},
})
}
func TestArgs_Arbitrary(t *testing.T) {
testArgs(t, map[string]argsTestcase{
" | Arb": {"", ArbitraryArgs, false, true, []string{"a", "b"}},
"Valid | Valid": {"", ArbitraryArgs, true, true, []string{"one", "two"}},
"Valid | Invalid": {"invalid", ArbitraryArgs, true, true, []string{"a"}},
})
}
func TestArgs_MinimumN(t *testing.T) {
testArgs(t, map[string]argsTestcase{
" | Arb": {"", MinimumNArgs(2), false, true, []string{"a", "b", "c"}},
"Valid | Valid": {"", MinimumNArgs(2), true, true, []string{"one", "three"}},
"Valid | Invalid": {"invalid", MinimumNArgs(2), true, true, []string{"a", "b"}},
" | Less": {"less", MinimumNArgs(2), false, true, []string{"a"}},
"Valid | Less": {"less", MinimumNArgs(2), true, true, []string{"one"}},
"Valid | LessInvalid": {"invalid", MinimumNArgs(2), true, true, []string{"a"}},
})
}
func TestArgs_MaximumN(t *testing.T) {
testArgs(t, map[string]argsTestcase{
" | Arb": {"", MaximumNArgs(3), false, true, []string{"a", "b"}},
"Valid | Valid": {"", MaximumNArgs(2), true, true, []string{"one", "three"}},
"Valid | Invalid": {"invalid", MaximumNArgs(2), true, true, []string{"a", "b"}},
" | More": {"more", MaximumNArgs(2), false, true, []string{"a", "b", "c"}},
"Valid | More": {"more", MaximumNArgs(2), true, true, []string{"one", "three", "two"}},
"Valid | MoreInvalid": {"invalid", MaximumNArgs(2), true, true, []string{"a", "b", "c"}},
})
}
func TestArgs_Exact(t *testing.T) {
testArgs(t, map[string]argsTestcase{
" | Arb": {"", ExactArgs(3), false, true, []string{"a", "b", "c"}},
"Valid | Valid": {"", ExactArgs(3), true, true, []string{"three", "one", "two"}},
"Valid | Invalid": {"invalid", ExactArgs(3), true, true, []string{"three", "a", "two"}},
" | InvalidCount": {"notexact", ExactArgs(2), false, true, []string{"a", "b", "c"}},
"Valid | InvalidCount": {"notexact", ExactArgs(2), true, true, []string{"three", "one", "two"}},
"Valid | InvalidCountInvalid": {"invalid", ExactArgs(2), true, true, []string{"three", "a", "two"}},
})
}
func TestArgs_Range(t *testing.T) {
testArgs(t, map[string]argsTestcase{
" | Arb": {"", RangeArgs(2, 4), false, true, []string{"a", "b", "c"}},
"Valid | Valid": {"", RangeArgs(2, 4), true, true, []string{"three", "one", "two"}},
"Valid | Invalid": {"invalid", RangeArgs(2, 4), true, true, []string{"three", "a", "two"}},
" | InvalidCount": {"notinrange", RangeArgs(2, 4), false, true, []string{"a"}},
"Valid | InvalidCount": {"notinrange", RangeArgs(2, 4), true, true, []string{"two"}},
"Valid | InvalidCountInvalid": {"invalid", RangeArgs(2, 4), true, true, []string{"a"}},
})
}
func TestArgs_DEPRECATED(t *testing.T) {
testArgs(t, map[string]argsTestcase{
"OnlyValid | Valid | Valid": {"", OnlyValidArgs, true, true, []string{"one", "two"}},
"OnlyValid | Valid | Invalid": {"invalid", OnlyValidArgs, true, true, []string{"a"}},
"ExactValid | Valid | Valid": {"", ExactValidArgs(3), true, true, []string{"two", "three", "one"}},
"ExactValid | Valid | InvalidCount": {"notexact", ExactValidArgs(2), true, true, []string{"two", "three", "one"}},
"ExactValid | Valid | Invalid": {"invalid", ExactValidArgs(2), true, true, []string{"two", "a"}},
})
}
// Takes(No)Args
func TestRootTakesNoArgs(t *testing.T) {
rootCmd := &Command{Use: "root", Run: emptyRun}
childCmd := &Command{Use: "child", Run: emptyRun}
rootCmd.AddCommand(childCmd)
_, err := executeCommand(rootCmd, "illegal", "args")
if err == nil {
t.Fatal("Expected an error")
}
got := err.Error()
expected := `unknown command "illegal" for "root"`
if !strings.Contains(got, expected) {
t.Errorf("expected %q, got %q", expected, got)
}
}
func TestRootTakesArgs(t *testing.T) {
rootCmd := &Command{Use: "root", Args: ArbitraryArgs, Run: emptyRun}
childCmd := &Command{Use: "child", Run: emptyRun}
rootCmd.AddCommand(childCmd)
_, err := executeCommand(rootCmd, "legal", "args")
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
}
func TestChildTakesNoArgs(t *testing.T) {
rootCmd := &Command{Use: "root", Run: emptyRun}
childCmd := &Command{Use: "child", Args: NoArgs, Run: emptyRun}
rootCmd.AddCommand(childCmd)
_, err := executeCommand(rootCmd, "child", "illegal", "args")
if err == nil {
t.Fatal("Expected an error")
}
got := err.Error()
expected := `"illegal" rejected; "root child" does not accept args`
if !strings.Contains(got, expected) {
t.Errorf("expected %q, got %q", expected, got)
}
}
func TestChildTakesArgs(t *testing.T) {
rootCmd := &Command{Use: "root", Run: emptyRun}
childCmd := &Command{Use: "child", Args: ArbitraryArgs, Run: emptyRun}
rootCmd.AddCommand(childCmd)
_, err := executeCommand(rootCmd, "child", "legal", "args")
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
}
/*
// NOTE 'c [command]' is not shown because this command does not have any subcommand
// NOTE 'Valid Args:' is not shown because this command is not runnable
// NOTE 'c [flags]' is not shown because this command is not runnable
func noRunChecks(t *testing.T, err error, errK, o string) {
expectErrorAndCheckOutput(t, err, errK, o, "u")
}
*/
// NoRun (no children)
func TestArgs_NoRun(t *testing.T) {
tc := argsTestcase{"run", nil, false, false, []string{}}
t.Run("|", tc.test)
//noRunChecks(t, e, "run", o)
}
func TestArgs_NoRun_ArbValid(t *testing.T) {
tc := argsTestcase{"run", nil, false, false, []string{"one", "three"}}
t.Run("|", tc.test)
// noRunChecks(t, e, "run", o)
}
func TestArgs_NoRun_Invalid(t *testing.T) {
tc := argsTestcase{"run", nil, false, false, []string{"two", "a"}}
t.Run("|", tc.test)
//noRunChecks(t, e, "run", o)
}
// NoRun (with children)
// NOTE 'Valid Args:' is not shown because this command is not runnable
// NOTE 'c [flags]' is not shown because this command is not runnable
func TestArgs_NoRun_wChild(t *testing.T) {
c := newCmd(nil, false, false)
d := newCmd(nil, false, true)
c.AddCommand(d)
o, e := executeCommand(c)
expectErrorAndCheckOutput(t, e, "runsub", o, "uc")
}
func TestArgs_NoRun_wChild_ArbValid(t *testing.T) {
c := newCmd(nil, false, false)
d := newCmd(nil, false, true)
c.AddCommand(d)
o, e := executeCommand(c, "one", "three")
expectErrorAndCheckOutput(t, e, "runsub", o, "h")
}
func TestArgs_NoRun_wChild_Invalid(t *testing.T) {
c := newCmd(nil, false, false)
d := newCmd(nil, false, true)
c.AddCommand(d)
o, e := executeCommand(c, "one", "a")
expectErrorAndCheckOutput(t, e, "runsub", o, "h")
}
// NoRun Args
func TestArgs_NoRun_wArgs(t *testing.T) {
testArgs(t, map[string]argsTestcase{
"|": {"run", ArbitraryArgs, false, false, []string{}},
})
//noRunChecks(t, e, "run", o)
}
func TestArgs_NoRun_wArgs_ArbValid(t *testing.T) {
testArgs(t, map[string]argsTestcase{
"|": {"run", ArbitraryArgs, false, false, []string{"one", "three"}},
})
//noRunChecks(t, e, "run", o)
}
func TestArgs_NoRun_wArgs_Invalid(t *testing.T) {
testArgs(t, map[string]argsTestcase{
"|": {"run", ArbitraryArgs, false, false, []string{"two", "a"}},
})
//noRunChecks(t, e, "run", o)
}
// NoRun ValidArgs
func TestArgs_NoRun_wValid(t *testing.T) {
testArgs(t, map[string]argsTestcase{
"|": {"run", nil, true, false, []string{}},
})
//noRunChecks(t, e, "run", o)
}
func TestArgs_NoRun_wValid_ArbValid(t *testing.T) {
testArgs(t, map[string]argsTestcase{
"|": {"run", nil, true, false, []string{"one", "three"}},
})
//noRunChecks(t, e, "run", o)
}
func TestArgs_NoRun_wValid_Invalid(t *testing.T) {
testArgs(t, map[string]argsTestcase{
"|": {"run", nil, true, false, []string{"two", "a"}},
})
//noRunChecks(t, e, "run", o)
}
// NoRun Args ValidArgs
func TestArgs_NoRun_wArgswValid(t *testing.T) {
testArgs(t, map[string]argsTestcase{
"|": {"run", ArbitraryArgs, true, false, []string{}},
})
// noRunChecks(t, e, "run", o)
}
func TestArgs_NoRun_wArgswValid_ArbValid(t *testing.T) {
testArgs(t, map[string]argsTestcase{
"|": {"run", ArbitraryArgs, true, false, []string{"one", "three"}},
})
// noRunChecks(t, e, "run", o)
}
func TestArgs_NoRun_wArgswValid_Invalid(t *testing.T) {
testArgs(t, map[string]argsTestcase{
"|": {"run", ArbitraryArgs, true, false, []string{"two", "a"}},
})
// noRunChecks(t, e, "run", o)
}
// Run (no children)
// NOTE 'c [command]' is not shown because this command does not have any subcommand
// NOTE 'Valid Args:' is not shown because ValidArgs is not defined
func TestArgs_Run(t *testing.T) {
testArgs(t, map[string]argsTestcase{
"|": {"", nil, false, true, []string{}},
})
//o, e = executeUsage(c)
//checkOutput(o, t, "ua")
}
func TestArgs_Run_ArbValid(t *testing.T) {
testArgs(t, map[string]argsTestcase{
"|": {"", nil, false, true, []string{"one", "three"}},
})
// o, e = executeUsage(c)
// checkOutput(o, t, "ua")
}
func TestArgs_Run_Invalid(t *testing.T) {
testArgs(t, map[string]argsTestcase{
"|": {"", nil, false, true, []string{"two", "a"}},
})
//o, e = executeUsage(c)
//checkOutput(o, t, "ua")
}
// Run (with children)
// NOTE 'Valid Args:' is not shown because ValidArgs is not defined
func TestArgs_Run_wChild(t *testing.T) {
c := newCmd(nil, false, true)
d := newCmd(nil, false, true)
c.AddCommand(d)
// o, e := executeCommand(c)
// expectSuccess(o, e, t)
o, _ := executeUsage(c)
checkOutput(o, t, "ucf")
}
func TestArgs_Run_wChild_ArbValid(t *testing.T) {
c := newCmd(nil, false, true)
d := newCmd(nil, false, false)
c.AddCommand(d)
o, _ := executeCommand(c, "one", "three")
// expectError(e, t, "no")
// NOTE 'c [command]' is not shown because this command does not have any available subcommand
checkOutput(o, t, "uf")
}
func TestArgs_Run_wChild_Invalid(t *testing.T) {
c := newCmd(nil, false, true)
d := newCmd(nil, false, false)
c.AddCommand(d)
o, _ := executeCommand(c, "one", "a")
// expectError(e, t, "no")
// NOTE 'c [command]' is not shown because this command does not have any available subcommand
checkOutput(o, t, "uf")
}
// Run Args
// NOTE 'c [command]' is not shown because this command does not have any subcommand
func TestArgs_Run_wArgs(t *testing.T) {
testArgs(t, map[string]argsTestcase{
"|": {"", ArbitraryArgs, false, true, []string{}},
})
// o, e = executeUsage(c)
// checkOutput(o, t, "ua")
}
func TestArgs_Run_wArgs_ArbValid(t *testing.T) {
testArgs(t, map[string]argsTestcase{
"|": {"", ArbitraryArgs, false, true, []string{"one", "three"}},
})
// o, e = executeUsage(c)
// checkOutput(o, t, "ua")
}
func TestArgs_Run_wArgs_Invalid(t *testing.T) {
testArgs(t, map[string]argsTestcase{
"|": {"", ArbitraryArgs, false, true, []string{"two", "a"}},
})
// o, e = executeUsage(c)
// checkOutput(o, t, "ua")
}
// Run ValidArgs
// NOTE 'c [command]' is not shown because this command does not have any subcommand
func TestArgs_Run_wValid(t *testing.T) {
testArgs(t, map[string]argsTestcase{
"|": {"", nil, true, true, []string{}},
})
// o, e = executeUsage(c)
// checkOutput(o, t, "uva")
}
func TestArgs_Run_wValid_ArbValid(t *testing.T) {
testArgs(t, map[string]argsTestcase{
"|": {"", nil, true, true, []string{"one", "three"}},
})
// o, e = executeUsage(c)
// checkOutput(o, t, "uva")
}
func TestArgs_Run_wValid_Invalid(t *testing.T) {
testArgs(t, map[string]argsTestcase{
"|": {"invalid", nil, true, true, []string{"two", "a"}},
})
// checkOutput(o, t, "uva")
}
// Run Args ValidArgs
// NOTE 'c [command]' is not shown because this command does not have any subcommand
func TestArgs_Run_wArgswValid(t *testing.T) {
testArgs(t, map[string]argsTestcase{
"|": {"", ArbitraryArgs, true, true, []string{}},
})
//o, e = executeUsage(c)
//checkOutput(o, t, "uva")
}
func TestArgs_Run_wArgswValid_ArbValid(t *testing.T) {
testArgs(t, map[string]argsTestcase{
"|": {"", ArbitraryArgs, true, true, []string{"one", "three"}},
})
//o, e = executeUsage(c)
//checkOutput(o, t, "uva")
}
func TestArgs_Run_wArgswValid_Invalid(t *testing.T) {
testArgs(t, map[string]argsTestcase{
"|": {"invalid", ArbitraryArgs, true, true, []string{"two", "a"}},
})
//checkOutput(o, t, "uva")
}
//
func TestArgs_Run_wMinimumNArgs_ArbValid(t *testing.T) {
testArgs(t, map[string]argsTestcase{
"|": {"", MinimumNArgs(2), false, true, []string{"one", "three"}},
})
//o, e = executeUsage(c)
//checkOutput(o, t, "ua")
}