forked from alecthomas/kingpin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_test.go
374 lines (322 loc) · 11 KB
/
cmd_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
package kingpin
import (
"sort"
"strings"
"github.com/stretchr/testify/assert"
"testing"
)
func parseAndExecute(app *Application, context *ParseContext) (string, error) {
if err := parse(context, app); err != nil {
return "", err
}
selected, err := app.setValues(context)
if err != nil {
return "", err
}
return app.execute(context, selected)
}
func complete(t *testing.T, app *Application, args ...string) []string {
context, err := app.ParseContext(args)
assert.NoError(t, err)
if err != nil {
return nil
}
completions := app.completionOptions(context)
sort.Strings(completions)
return completions
}
func TestNestedCommands(t *testing.T) {
app := New("app", "")
sub1 := app.Command("sub1", "")
sub1.Flag("sub1", "")
subsub1 := sub1.Command("sub1sub1", "")
subsub1.Command("sub1sub1end", "")
sub2 := app.Command("sub2", "")
sub2.Flag("sub2", "")
sub2.Command("sub2sub1", "")
context := tokenize([]string{"sub1", "sub1sub1", "sub1sub1end"}, false)
selected, err := parseAndExecute(app, context)
assert.NoError(t, err)
assert.True(t, context.EOL())
assert.Equal(t, "sub1 sub1sub1 sub1sub1end", selected)
}
func TestNestedCommandsWithArgs(t *testing.T) {
app := New("app", "")
cmd := app.Command("a", "").Command("b", "")
a := cmd.Arg("a", "").String()
b := cmd.Arg("b", "").String()
context := tokenize([]string{"a", "b", "c", "d"}, false)
selected, err := parseAndExecute(app, context)
assert.NoError(t, err)
assert.True(t, context.EOL())
assert.Equal(t, "a b", selected)
assert.Equal(t, "c", *a)
assert.Equal(t, "d", *b)
}
func TestNestedCommandsWithFlags(t *testing.T) {
app := New("app", "")
cmd := app.Command("a", "").Command("b", "")
a := cmd.Flag("aaa", "").Short('a').String()
b := cmd.Flag("bbb", "").Short('b').String()
err := app.init()
assert.NoError(t, err)
context := tokenize(strings.Split("a b --aaa x -b x", " "), false)
selected, err := parseAndExecute(app, context)
assert.NoError(t, err)
assert.True(t, context.EOL())
assert.Equal(t, "a b", selected)
assert.Equal(t, "x", *a)
assert.Equal(t, "x", *b)
}
func TestNestedCommandWithMergedFlags(t *testing.T) {
app := New("app", "")
cmd0 := app.Command("a", "")
cmd0f0 := cmd0.Flag("aflag", "").Bool()
// cmd1 := app.Command("b", "")
// cmd1f0 := cmd0.Flag("bflag", "").Bool()
cmd00 := cmd0.Command("aa", "")
cmd00f0 := cmd00.Flag("aaflag", "").Bool()
err := app.init()
assert.NoError(t, err)
context := tokenize(strings.Split("a aa --aflag --aaflag", " "), false)
selected, err := parseAndExecute(app, context)
assert.NoError(t, err)
assert.True(t, *cmd0f0)
assert.True(t, *cmd00f0)
assert.Equal(t, "a aa", selected)
}
func TestNestedCommandWithDuplicateFlagErrors(t *testing.T) {
app := New("app", "")
app.Flag("test", "").Bool()
app.Command("cmd0", "").Flag("test", "").Bool()
err := app.init()
assert.Error(t, err)
}
func TestNestedCommandWithArgAndMergedFlags(t *testing.T) {
app := New("app", "")
cmd0 := app.Command("a", "")
cmd0f0 := cmd0.Flag("aflag", "").Bool()
// cmd1 := app.Command("b", "")
// cmd1f0 := cmd0.Flag("bflag", "").Bool()
cmd00 := cmd0.Command("aa", "")
cmd00a0 := cmd00.Arg("arg", "").String()
cmd00f0 := cmd00.Flag("aaflag", "").Bool()
err := app.init()
assert.NoError(t, err)
context := tokenize(strings.Split("a aa hello --aflag --aaflag", " "), false)
selected, err := parseAndExecute(app, context)
assert.NoError(t, err)
assert.True(t, *cmd0f0)
assert.True(t, *cmd00f0)
assert.Equal(t, "a aa", selected)
assert.Equal(t, "hello", *cmd00a0)
}
func TestDefaultSubcommandEOL(t *testing.T) {
app := newTestApp()
c0 := app.Command("c0", "").Default()
c0.Command("c01", "").Default()
c0.Command("c02", "")
cmd, err := app.Parse([]string{"c0"})
assert.NoError(t, err)
assert.Equal(t, "c0 c01", cmd)
}
func TestDefaultSubcommandWithArg(t *testing.T) {
app := newTestApp()
c0 := app.Command("c0", "").Default()
c01 := c0.Command("c01", "").Default()
c012 := c01.Command("c012", "").Default()
a0 := c012.Arg("a0", "").String()
c0.Command("c02", "")
cmd, err := app.Parse([]string{"c0", "hello"})
assert.NoError(t, err)
assert.Equal(t, "c0 c01 c012", cmd)
assert.Equal(t, "hello", *a0)
}
func TestDefaultSubcommandWithFlags(t *testing.T) {
app := newTestApp()
c0 := app.Command("c0", "").Default()
_ = c0.Flag("f0", "").Int()
c0c1 := c0.Command("c1", "").Default()
c0c1f1 := c0c1.Flag("f1", "").Int()
selected, err := app.Parse([]string{"--f1=2"})
assert.NoError(t, err)
assert.Equal(t, "c0 c1", selected)
assert.Equal(t, 2, *c0c1f1)
_, err = app.Parse([]string{"--f2"})
assert.Error(t, err)
}
func TestMultipleDefaultCommands(t *testing.T) {
app := newTestApp()
app.Command("c0", "").Default()
app.Command("c1", "").Default()
_, err := app.Parse([]string{})
assert.Error(t, err)
}
func TestAliasedCommand(t *testing.T) {
app := newTestApp()
app.Command("one", "").Alias("two")
selected, _ := app.Parse([]string{"one"})
assert.Equal(t, "one", selected)
selected, _ = app.Parse([]string{"two"})
assert.Equal(t, "one", selected)
// 2 due to "help" and "one"
assert.Equal(t, 2, len(app.Model().FlattenedCommands()))
}
func TestDuplicateAlias(t *testing.T) {
app := newTestApp()
app.Command("one", "")
app.Command("two", "").Alias("one")
_, err := app.Parse([]string{"one"})
assert.Error(t, err)
}
func TestFlagCompletion(t *testing.T) {
app := newTestApp()
app.Command("one", "")
two := app.Command("two", "")
two.Flag("flag-1", "")
two.Flag("flag-2", "").HintOptions("opt1", "opt2", "opt3")
two.Flag("flag-3", "")
cases := []struct {
target cmdMixin
flagName string
flagValue string
expectedFlagMatch bool
expectedOptionMatch bool
expectedFlags []string
}{
{
// Test top level flags
target: app.cmdMixin,
flagName: "",
flagValue: "",
expectedFlagMatch: false,
expectedOptionMatch: false,
expectedFlags: []string{"--help"},
},
{
// Test no flag passed
target: two.cmdMixin,
flagName: "",
flagValue: "",
expectedFlagMatch: false,
expectedOptionMatch: false,
expectedFlags: []string{"--flag-1", "--flag-2", "--flag-3"},
},
{
// Test an incomplete flag. Should still give all options as if the flag wasn't given at all.
target: two.cmdMixin,
flagName: "flag-",
flagValue: "",
expectedFlagMatch: false,
expectedOptionMatch: false,
expectedFlags: []string{"--flag-1", "--flag-2", "--flag-3"},
},
{
// Test with a complete flag. Should show available choices for the flag
// This flag has no options. No options should be produced.
// Should also report an option was matched
target: two.cmdMixin,
flagName: "flag-1",
flagValue: "",
expectedFlagMatch: true,
expectedOptionMatch: true,
expectedFlags: []string(nil),
},
{
// Test with a complete flag. Should show available choices for the flag
target: two.cmdMixin,
flagName: "flag-2",
flagValue: "",
expectedFlagMatch: true,
expectedOptionMatch: false,
expectedFlags: []string{"opt1", "opt2", "opt3"},
},
{
// Test with a complete flag and complete option for that flag.
target: two.cmdMixin,
flagName: "flag-2",
flagValue: "opt1",
expectedFlagMatch: true,
expectedOptionMatch: true,
expectedFlags: []string{"opt1", "opt2", "opt3"},
},
}
for i, c := range cases {
choices, flagMatch, optionMatch := c.target.FlagCompletion(c.flagName, c.flagValue)
assert.Equal(t, c.expectedFlags, choices, "Test case %d: expectedFlags != actual flags", i+1)
assert.Equal(t, c.expectedFlagMatch, flagMatch, "Test case %d: expectedFlagMatch != flagMatch", i+1)
assert.Equal(t, c.expectedOptionMatch, optionMatch, "Test case %d: expectedOptionMatch != optionMatch", i+1)
}
}
func TestCmdCompletion(t *testing.T) {
app := newTestApp()
app.Command("one", "")
two := app.Command("two", "")
two.Command("sub1", "")
two.Command("sub2", "")
assert.Equal(t, []string{"help", "one", "two"}, complete(t, app))
assert.Equal(t, []string{"sub1", "sub2"}, complete(t, app, "two"))
}
func TestHiddenCmdCompletion(t *testing.T) {
app := newTestApp()
// top level visible & hidden cmds, with no sub-cmds
app.Command("visible1", "")
app.Command("hidden1", "").Hidden()
// visible cmd with visible & hidden sub-cmds
visible2 := app.Command("visible2", "")
visible2.Command("visible2-visible", "")
visible2.Command("visible2-hidden", "").Hidden()
// hidden cmd with visible & hidden sub-cmds
hidden2 := app.Command("hidden2", "").Hidden()
hidden2.Command("hidden2-visible", "")
hidden2.Command("hidden2-hidden", "").Hidden()
// Only top level visible cmds should show
assert.Equal(t, []string{"help", "visible1", "visible2"}, complete(t, app))
// Only visible sub-cmds should show
assert.Equal(t, []string{"visible2-visible"}, complete(t, app, "visible2"))
// Hidden commands should still complete visible sub-cmds
assert.Equal(t, []string{"hidden2-visible"}, complete(t, app, "hidden2"))
}
func TestDefaultCmdCompletion(t *testing.T) {
app := newTestApp()
cmd1 := app.Command("cmd1", "")
cmd1Sub1 := cmd1.Command("cmd1-sub1", "")
cmd1Sub1.Arg("cmd1-sub1-arg1", "").HintOptions("cmd1-arg1").String()
cmd2 := app.Command("cmd2", "").Default()
cmd2.Command("cmd2-sub1", "")
cmd2Sub2 := cmd2.Command("cmd2-sub2", "").Default()
cmd2Sub2Sub1 := cmd2Sub2.Command("cmd2-sub2-sub1", "").Default()
cmd2Sub2Sub1.Arg("cmd2-sub2-sub1-arg1", "").HintOptions("cmd2-sub2-sub1-arg1").String()
cmd2Sub2Sub1.Arg("cmd2-sub2-sub1-arg2", "").HintOptions("cmd2-sub2-sub1-arg2").String()
// Without args, should get:
// - root cmds (including implicit "help")
// - thread of default cmds
// - first arg hints for the final default cmd
assert.Equal(t, []string{"cmd1", "cmd2", "cmd2-sub1", "cmd2-sub2", "cmd2-sub2-sub1", "cmd2-sub2-sub1-arg1", "help"}, complete(t, app))
// With a non-default cmd already listed, should get:
// - sub cmds of that arg
assert.Equal(t, []string{"cmd1-sub1"}, complete(t, app, "cmd1"))
// With an explicit default cmd listed, should get:
// - default child-cmds
// - first arg hints for the final default cmd
assert.Equal(t, []string{"cmd2-sub1", "cmd2-sub2", "cmd2-sub2-sub1", "cmd2-sub2-sub1-arg1"}, complete(t, app, "cmd2"))
// Args should be completed when all preceding cmds are explicit, and when
// any of them are implicit (not listed). Check this by trying all possible
// combinations of choosing/excluding the three levels of cmds. This tests
// root-level default, middle default, and end default.
for i := 0; i < 8; i++ {
var cmdline []string
if i&1 != 0 {
cmdline = append(cmdline, "cmd2")
}
if i&2 != 0 {
cmdline = append(cmdline, "cmd2-sub2")
}
if i&4 != 0 {
cmdline = append(cmdline, "cmd2-sub2-sub1")
}
assert.Contains(t, complete(t, app, cmdline...), "cmd2-sub2-sub1-arg1", "with cmdline: %v", cmdline)
}
// With both args of a default sub cmd, should get no completions
assert.Empty(t, complete(t, app, "arg1", "arg2"))
}