-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp_test.go
647 lines (566 loc) · 17.6 KB
/
app_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
package fisk
import (
"bytes"
"embed"
"errors"
"fmt"
"io"
"os"
"sort"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func newTestApp() *Application {
return New("test", "").Terminate(nil).UsageTemplate(KingpinDefaultUsageTemplate)
}
func TestCommander(t *testing.T) {
c := newTestApp()
ping := c.Command("ping", "Ping an IP address.")
pingTTL := ping.Flag("ttl", "TTL for ICMP packets").Short('t').Default("5s").Duration()
selected, err := c.Parse([]string{"ping"})
assert.NoError(t, err)
assert.Equal(t, "ping", selected)
assert.Equal(t, 5*time.Second, *pingTTL)
selected, err = c.Parse([]string{"ping", "--ttl=10s"})
assert.NoError(t, err)
assert.Equal(t, "ping", selected)
assert.Equal(t, 10*time.Second, *pingTTL)
}
func TestRequiredFlags(t *testing.T) {
c := newTestApp()
c.Flag("a", "a").String()
c.Flag("b", "b").Required().String()
_, err := c.Parse([]string{"--a=foo"})
assert.Error(t, err)
_, err = c.Parse([]string{"--b=foo"})
assert.NoError(t, err)
}
func TestRepeatableFlags(t *testing.T) {
c := newTestApp()
c.Flag("a", "a").String()
c.Flag("b", "b").Strings()
_, err := c.Parse([]string{"--a=foo", "--a=bar"})
assert.Error(t, err)
_, err = c.Parse([]string{"--b=foo", "--b=bar"})
assert.NoError(t, err)
}
func TestInvalidDefaultFlagValueErrors(t *testing.T) {
c := newTestApp()
c.Flag("foo", "foo").Default("a").Int()
_, err := c.Parse([]string{})
assert.Error(t, err)
}
func TestInvalidDefaultArgValueErrors(t *testing.T) {
c := newTestApp()
cmd := c.Command("cmd", "cmd")
cmd.Arg("arg", "arg").Default("one").Int()
_, err := c.Parse([]string{"cmd"})
assert.Error(t, err)
}
func TestArgsRequiredAfterNonRequiredErrors(t *testing.T) {
c := newTestApp()
cmd := c.Command("cmd", "")
cmd.Arg("a", "a").String()
cmd.Arg("b", "b").Required().String()
_, err := c.Parse([]string{"cmd"})
assert.Error(t, err)
}
func TestArgsMultipleRequiredThenNonRequired(t *testing.T) {
c := newTestApp().Writer(io.Discard)
cmd := c.Command("cmd", "")
cmd.Arg("a", "a").Required().String()
cmd.Arg("b", "b").Required().String()
cmd.Arg("c", "c").String()
cmd.Arg("d", "d").String()
_, err := c.Parse([]string{"cmd", "a", "b"})
assert.NoError(t, err)
_, err = c.Parse([]string{})
assert.Error(t, err)
}
func TestDispatchCallbackIsCalled(t *testing.T) {
dispatched := false
c := newTestApp()
c.Command("cmd", "").Action(func(*ParseContext) error {
dispatched = true
return nil
})
_, err := c.Parse([]string{"cmd"})
assert.NoError(t, err)
assert.True(t, dispatched)
}
func TestTopLevelArgWorks(t *testing.T) {
c := newTestApp()
s := c.Arg("arg", "help").String()
_, err := c.Parse([]string{"foo"})
assert.NoError(t, err)
assert.Equal(t, "foo", *s)
}
func TestTopLevelArgCantBeUsedWithCommands(t *testing.T) {
c := newTestApp()
c.Arg("arg", "help").String()
c.Command("cmd", "help")
_, err := c.Parse([]string{})
assert.Error(t, err)
}
func TestTooManyArgs(t *testing.T) {
a := newTestApp()
a.Arg("a", "").String()
_, err := a.Parse([]string{"a", "b"})
assert.Error(t, err)
}
func TestTooManyArgsAfterCommand(t *testing.T) {
a := newTestApp()
a.Command("a", "")
assert.NoError(t, a.init())
_, err := a.Parse([]string{"a", "b"})
assert.Error(t, err)
}
func TestArgsLooksLikeFlagsWithConsumeRemainder(t *testing.T) {
a := newTestApp()
a.Arg("opts", "").Required().Strings()
_, err := a.Parse([]string{"hello", "-world"})
assert.Error(t, err)
}
func TestCommandParseDoesNotResetFlagsToDefault(t *testing.T) {
app := newTestApp()
flag := app.Flag("flag", "").Default("default").String()
app.Command("cmd", "")
_, err := app.Parse([]string{"--flag=123", "cmd"})
assert.NoError(t, err)
assert.Equal(t, "123", *flag)
}
func TestCommandParseDoesNotFailRequired(t *testing.T) {
app := newTestApp()
flag := app.Flag("flag", "").Required().String()
app.Command("cmd", "")
_, err := app.Parse([]string{"cmd", "--flag=123"})
assert.NoError(t, err)
assert.Equal(t, "123", *flag)
}
func TestSelectedCommand(t *testing.T) {
app := newTestApp()
c0 := app.Command("c0", "")
c0.Command("c1", "")
s, err := app.Parse([]string{"c0", "c1"})
assert.NoError(t, err)
assert.Equal(t, "c0 c1", s)
}
func TestSubCommandRequired(t *testing.T) {
app := newTestApp()
c0 := app.Command("c0", "")
c0.Command("c1", "")
_, err := app.Parse([]string{"c0"})
assert.Error(t, err)
}
func TestInterspersedFalse(t *testing.T) {
app := newTestApp().Interspersed(false)
a1 := app.Arg("a1", "").String()
a2 := app.Arg("a2", "").String()
f1 := app.Flag("flag", "").String()
_, err := app.Parse([]string{"a1", "--flag=flag"})
assert.NoError(t, err)
assert.Equal(t, "a1", *a1)
assert.Equal(t, "--flag=flag", *a2)
assert.Equal(t, "", *f1)
}
func TestInterspersedTrue(t *testing.T) {
// test once with the default value and once with explicit true
for i := 0; i < 2; i++ {
app := newTestApp()
if i != 0 {
t.Log("Setting explicit")
app.Interspersed(true)
} else {
t.Log("Using default")
}
a1 := app.Arg("a1", "").String()
a2 := app.Arg("a2", "").String()
f1 := app.Flag("flag", "").String()
_, err := app.Parse([]string{"a1", "--flag=flag"})
assert.NoError(t, err)
assert.Equal(t, "a1", *a1)
assert.Equal(t, "", *a2)
assert.Equal(t, "flag", *f1)
}
}
func TestDefaultEnvars(t *testing.T) {
a := New("some-app", "").Terminate(nil).DefaultEnvars()
f0 := a.Flag("some-flag", "")
f0.Bool()
f1 := a.Flag("some-other-flag", "").NoEnvar()
f1.Bool()
f2 := a.Flag("a-1-flag", "")
f2.Bool()
_, err := a.Parse([]string{})
assert.NoError(t, err)
assert.Equal(t, "SOME_APP_SOME_FLAG", f0.envar)
assert.Equal(t, "", f1.envar)
assert.Equal(t, "SOME_APP_A_1_FLAG", f2.envar)
}
func TestBashCompletionOptionsWithEmptyApp(t *testing.T) {
a := newTestApp()
context, err := a.ParseContext([]string{"--completion-bash"})
if err != nil {
t.Errorf("Unexpected error whilst parsing context: [%v]", err)
}
args := a.completionOptions(context)
assert.Equal(t, []string(nil), args)
}
func TestBashCompletionOptions(t *testing.T) {
a := newTestApp()
a.Command("one", "")
a.Flag("flag-0", "").String()
a.Flag("flag-1", "").HintOptions("opt1", "opt2", "opt3").String()
two := a.Command("two", "")
two.Flag("flag-2", "").String()
two.Flag("flag-3", "").HintOptions("opt4", "opt5", "opt6").String()
three := a.Command("three", "")
three.Flag("flag-4", "").String()
three.Arg("arg-1", "").String()
three.Arg("arg-2", "").HintOptions("arg-2-opt-1", "arg-2-opt-2").String()
three.Arg("arg-3", "").String()
three.Arg("arg-4", "").HintAction(func() []string {
return []string{"arg-4-opt-1", "arg-4-opt-2"}
}).String()
cases := []struct {
Args string
ExpectedOptions []string
}{
{
Args: "--completion-bash",
ExpectedOptions: []string{"help", "one", "three", "two"},
},
{
Args: "--completion-bash --",
ExpectedOptions: []string{"--flag-0", "--flag-1", "--help"},
},
{
Args: "--completion-bash --fla",
ExpectedOptions: []string{"--flag-0", "--flag-1", "--help"},
},
{
// No options available for flag-0, return to cmd completion
Args: "--completion-bash --flag-0",
ExpectedOptions: []string{"help", "one", "three", "two"},
},
{
Args: "--completion-bash --flag-0 --",
ExpectedOptions: []string{"--flag-0", "--flag-1", "--help"},
},
{
Args: "--completion-bash --flag-1",
ExpectedOptions: []string{"opt1", "opt2", "opt3"},
},
{
Args: "--completion-bash --flag-1 opt",
ExpectedOptions: []string{"opt1", "opt2", "opt3"},
},
{
Args: "--completion-bash --flag-1 opt1",
ExpectedOptions: []string{"help", "one", "three", "two"},
},
{
Args: "--completion-bash --flag-1 opt1 --",
ExpectedOptions: []string{"--flag-0", "--flag-1", "--help"},
},
// Try Subcommand
{
Args: "--completion-bash two",
ExpectedOptions: []string(nil),
},
{
Args: "--completion-bash two --",
ExpectedOptions: []string{"--help", "--flag-2", "--flag-3", "--flag-0", "--flag-1"},
},
{
Args: "--completion-bash two --flag",
ExpectedOptions: []string{"--help", "--flag-2", "--flag-3", "--flag-0", "--flag-1"},
},
{
Args: "--completion-bash two --flag-2",
ExpectedOptions: []string(nil),
},
{
// Top level flags carry downwards
Args: "--completion-bash two --flag-1",
ExpectedOptions: []string{"opt1", "opt2", "opt3"},
},
{
// Top level flags carry downwards
Args: "--completion-bash two --flag-1 opt",
ExpectedOptions: []string{"opt1", "opt2", "opt3"},
},
{
// Top level flags carry downwards
Args: "--completion-bash two --flag-1 opt1",
ExpectedOptions: []string(nil),
},
{
Args: "--completion-bash two --flag-3",
ExpectedOptions: []string{"opt4", "opt5", "opt6"},
},
{
Args: "--completion-bash two --flag-3 opt",
ExpectedOptions: []string{"opt4", "opt5", "opt6"},
},
{
Args: "--completion-bash two --flag-3 opt4",
ExpectedOptions: []string(nil),
},
{
Args: "--completion-bash two --flag-3 opt4 --",
ExpectedOptions: []string{"--help", "--flag-2", "--flag-3", "--flag-0", "--flag-1"},
},
// Args complete
{
// After a command with an arg with no options, nothing should be
// shown
Args: "--completion-bash three ",
ExpectedOptions: []string(nil),
},
{
// After a command with an arg, explicitly starting a flag should
// complete flags
Args: "--completion-bash three --",
ExpectedOptions: []string{"--flag-0", "--flag-1", "--flag-4", "--help"},
},
{
// After a command with an arg that does have completions, they
// should be shown
Args: "--completion-bash three arg1 ",
ExpectedOptions: []string{"arg-2-opt-1", "arg-2-opt-2"},
},
{
// After a command with an arg that does have completions, but a
// flag is started, flag options should be completed
Args: "--completion-bash three arg1 --",
ExpectedOptions: []string{"--flag-0", "--flag-1", "--flag-4", "--help"},
},
{
// After a command with an arg that has no completions, and isn't first,
// nothing should be shown
Args: "--completion-bash three arg1 arg2 ",
ExpectedOptions: []string(nil),
},
{
// After a command with a different arg that also has completions,
// those different options should be shown
Args: "--completion-bash three arg1 arg2 arg3 ",
ExpectedOptions: []string{"arg-4-opt-1", "arg-4-opt-2"},
},
{
// After a command with all args listed, nothing should complete
Args: "--completion-bash three arg1 arg2 arg3 arg4",
ExpectedOptions: []string(nil),
},
{
// After a -- argument, no more flags should be suggested
Args: "--completion-bash three --flag-0 -- --",
ExpectedOptions: []string(nil),
},
{
// After a -- argument, argument options should still be suggested
Args: "--completion-bash three -- arg1 ",
ExpectedOptions: []string{"arg-2-opt-1", "arg-2-opt-2"},
},
}
for _, c := range cases {
context, _ := a.ParseContext(strings.Split(c.Args, " "))
args := a.completionOptions(context)
sort.Strings(args)
sort.Strings(c.ExpectedOptions)
assert.Equal(t, c.ExpectedOptions, args, "Expected != Actual: [%v] != [%v]. \nInput was: [%v]", c.ExpectedOptions, args, c.Args)
}
}
func TestCmdValidation(t *testing.T) {
c := newTestApp()
cmd := c.Command("cmd", "")
var a, b string
cmd.Flag("a", "a").StringVar(&a)
cmd.Flag("b", "b").StringVar(&b)
cmd.Validate(func(*CmdClause) error {
if a == "" && b == "" {
return errors.New("must specify either a or b")
}
return nil
})
_, err := c.Parse([]string{"cmd"})
assert.Error(t, err)
_, err = c.Parse([]string{"cmd", "--a", "A"})
assert.NoError(t, err)
}
func TestCheatTopLevel(t *testing.T) {
var buf bytes.Buffer
c := newTestApp()
c.Cheat("", `# top cheat`)
c.Command("sub", "Sub commands").Cheat("sub", "# sub cheat")
c.Command("without", "Sub without cheat")
c.UsageWriter(&buf)
_, err := c.Parse([]string{"cheat", "test"})
assert.NoError(t, err)
expected := "# top cheat\n"
assert.Equal(t, expected, buf.String())
}
func TestCheatTopLevelWithout(t *testing.T) {
var buf bytes.Buffer
c := newTestApp()
c.WithCheats()
c.Command("sub", "Sub commands")
c.Command("without", "Sub without cheat")
c.UsageWriter(&buf)
_, err := c.Parse([]string{"cheat"})
assert.NoError(t, err)
expected := "No cheats defined\n"
assert.Equal(t, expected, buf.String())
}
func TestCheatSubLevel(t *testing.T) {
var buf bytes.Buffer
c := newTestApp()
c.Cheat("", `# top cheat`)
c.Command("sub", "Sub commands").Cheat("sub", "# sub cheat")
c.Command("without", "Sub without cheat")
c.UsageWriter(&buf)
_, err := c.Parse([]string{"cheat", "sub"})
assert.NoError(t, err)
expected := "# sub cheat\n"
assert.Equal(t, expected, buf.String())
}
func TestCheatSubWithout(t *testing.T) {
var buf bytes.Buffer
c := newTestApp().WithCheats()
s := c.Command("sub", "Sub commands").Cheat("sub", "# sub cheat")
s.Command("subsbub", "Subsub command")
w := c.Command("without", "Sub without cheat")
w.Command("with", "sub with").Cheat("with", "without -> with")
w.Command("also_with", "sub with").Cheat("also", "without -> also_with")
c.UsageWriter(&buf)
_, err := c.Parse([]string{"cheat", "without"})
assert.NoError(t, err)
expected := `Available Cheats:
also
sub
with
`
assert.Equal(t, expected, buf.String())
}
func TestCheatList(t *testing.T) {
var buf bytes.Buffer
c := newTestApp()
c.Cheat("", `# top cheat`)
s := c.Command("sub", "Sub commands").Cheat("sub", "# sub cheat")
s.Command("subsbub", "Subsub command")
w := c.Command("without", "Sub without cheat")
w.Command("with", "sub with").Cheat("with", "without -> with")
w.Command("also_with", "sub with").Cheat("also", "without -> also_with")
c.UsageWriter(&buf)
_, err := c.Parse([]string{"cheat", "--list"})
assert.NoError(t, err)
expected := `Available Cheats:
test
also
sub
with
`
assert.Equal(t, expected, buf.String())
}
func TestCheatShowDefaultNotList(t *testing.T) {
var buf bytes.Buffer
c := newTestApp().Cheat("", `# top cheat`)
s := c.Command("sub", "Sub commands")
s.Command("subsbub", "Subsub command")
c.Command("without", "Sub without cheat")
c.UsageWriter(&buf)
_, err := c.Parse([]string{"cheat"})
assert.NoError(t, err)
expected := `# top cheat
`
assert.Equal(t, expected, buf.String())
}
func TestCheatSave(t *testing.T) {
var buf bytes.Buffer
c := newTestApp()
c.Cheat("", `# top cheat`)
s := c.Command("sub", "Sub commands").Cheat("sub", "# sub cheat")
s.Command("subsbub", "Subsub command")
w := c.Command("without", "Sub without cheat")
w.Command("with", "sub with").Cheat("with", "without -> with")
w.Command("also_with", "sub with").Cheat("also", "without -> also_with")
td, err := os.MkdirTemp("", "")
assert.NoError(t, err)
defer os.RemoveAll(td)
c.UsageWriter(&buf)
_, err = c.Parse([]string{"cheat", "--save", td})
assert.NoError(t, err)
expected := `Saved cheat to {{dir}}/also
Saved cheat to {{dir}}/sub
Saved cheat to {{dir}}/test
Saved cheat to {{dir}}/with
`
assert.Equal(t, strings.ReplaceAll(expected, "{{dir}}", td), buf.String())
}
func TestNewF(t *testing.T) {
app := Newf("test", "foo %s", "bar")
assert.Equal(t, app.Help, "foo bar")
}
func TestCommandf(t *testing.T) {
c := newTestApp()
x := c.Commandf("x", "foo %s", "bar")
y := x.Commandf("y", "foo bar %s", "baz")
assert.Equal(t, x.help, "foo bar")
assert.Equal(t, y.help, "foo bar baz")
}
//go:embed doc.go
var docFS embed.FS
func TestCheatFile(t *testing.T) {
c := newTestApp().CheatFile(docFS, "", "doc.go")
c.Command("x", "x").CheatFile(docFS, "y", "doc.go")
assert.Contains(t, c.cheats["test"], "Package fisk provides")
assert.Contains(t, c.cheats["y"], "Package fisk provides")
}
func TestFiskIntrospect(t *testing.T) {
var buf bytes.Buffer
c := newTestApp()
c.usageWriter = &buf
c.errorWriter = &buf
c.Flag("required", "required").Required().String()
c.MustParseWithUsage([]string{"--fisk-introspect"})
assert.NotContains(t, buf.String(), "required flag --required not provided")
}
func TestParseWithUsage(t *testing.T) {
var buf bytes.Buffer
c := newTestApp()
c.usageWriter = &buf
c.errorWriter = &buf
p := c.Command("parent", "")
ch := p.Command("child", "").Action(func(_ *ParseContext) error { return fmt.Errorf("not impl") })
ch.Flag("thing", "thing").Required().String()
c.MustParseWithUsage([]string{"parent"})
assert.Contains(t, buf.String(), " a subcommand from the list below is required")
assert.NotContains(t, buf.String(), "Flags")
buf.Reset()
c.MustParseWithUsage([]string{"parent", "child"})
assert.Contains(t, buf.String(), "required flag --thing not provided")
assert.Contains(t, buf.String(), "Flags")
buf.Reset()
c.MustParseWithUsage([]string{"parent", "foo"})
assert.Contains(t, buf.String(), "expected command but got")
assert.NotContains(t, buf.String(), "Flags")
buf.Reset()
c.MustParseWithUsage([]string{"parent", "child", "--thing"})
assert.Contains(t, buf.String(), "expected argument for flag")
assert.Contains(t, buf.String(), "Flags")
buf.Reset()
c.MustParseWithUsage([]string{"parent", "child", "--unknown"})
assert.Contains(t, buf.String(), "unknown long flag")
assert.Contains(t, buf.String(), "Flags")
buf.Reset()
c.MustParseWithUsage([]string{"parent", "child", "-u"})
assert.Contains(t, buf.String(), "unknown short flag")
assert.Contains(t, buf.String(), "Flags")
buf.Reset()
c.MustParseWithUsage([]string{"parent", "child", "--thing", "x", "--thing", "x"})
assert.Contains(t, buf.String(), "flag 'thing' cannot be repeated")
assert.Contains(t, buf.String(), "Flags")
}