-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_test.go
396 lines (357 loc) · 11.1 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
package cli
import (
"bytes"
"context"
"errors"
"flag"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// testState is a helper struct to hold the commands for testing
//
// root --verbose --version
// ├── add --dry-run
// └── nested --force
// └── sub --echo
// └── hello --mandatory-flag=false --another-mandatory-flag some-value
type testState struct {
add *Command
nested, sub, hello *Command
root *Command
}
func newTestState() testState {
exec := func(ctx context.Context, s *State) error { return errors.New("not implemented") }
add := &Command{
Name: "add",
Flags: FlagsFunc(func(fset *flag.FlagSet) {
fset.Bool("dry-run", false, "enable dry-run mode")
}),
Exec: exec,
}
sub := &Command{
Name: "sub",
Flags: FlagsFunc(func(fset *flag.FlagSet) {
fset.String("echo", "", "echo the message")
}),
FlagsMetadata: []FlagMetadata{
{Name: "echo", Required: false}, // not required
},
Exec: exec,
}
hello := &Command{
Name: "hello",
Flags: FlagsFunc(func(fset *flag.FlagSet) {
fset.Bool("mandatory-flag", false, "mandatory flag")
fset.String("another-mandatory-flag", "", "another mandatory flag")
}),
FlagsMetadata: []FlagMetadata{
{Name: "mandatory-flag", Required: true},
{Name: "another-mandatory-flag", Required: true},
},
Exec: exec,
}
nested := &Command{
Name: "nested",
Flags: FlagsFunc(func(fset *flag.FlagSet) {
fset.Bool("force", false, "force the operation")
}),
SubCommands: []*Command{sub, hello},
Exec: exec,
}
root := &Command{
Name: "todo",
Flags: FlagsFunc(func(fset *flag.FlagSet) {
fset.Bool("verbose", false, "enable verbose mode")
fset.Bool("version", false, "show version")
}),
SubCommands: []*Command{add, nested},
Exec: exec,
}
return testState{
add: add,
nested: nested,
sub: sub,
root: root,
hello: hello,
}
}
func TestParse(t *testing.T) {
t.Parallel()
t.Run("error on parse with no exec", func(t *testing.T) {
t.Parallel()
cmd := &Command{
Name: "foo",
Exec: func(ctx context.Context, s *State) error { return nil },
SubCommands: []*Command{
{
Name: "bar",
Exec: func(ctx context.Context, s *State) error { return nil },
SubCommands: []*Command{
{
Name: "baz",
},
},
},
},
}
err := Parse(cmd, []string{"bar", "baz"})
require.Error(t, err)
assert.ErrorContains(t, err, `command "foo bar baz": no exec function defined`)
})
t.Run("parsing errors", func(t *testing.T) {
t.Parallel()
err := Parse(nil, nil)
require.Error(t, err)
require.Contains(t, err.Error(), "command is nil")
err = Parse(&Command{}, nil)
require.Error(t, err)
require.Contains(t, err.Error(), "root command has no name")
})
t.Run("subcommand nil flags", func(t *testing.T) {
t.Parallel()
err := Parse(&Command{
Name: "root",
SubCommands: []*Command{{
Name: "sub",
Exec: func(ctx context.Context, s *State) error { return nil },
}},
Exec: func(ctx context.Context, s *State) error { return nil },
}, []string{"sub"})
require.NoError(t, err)
})
t.Run("default flag usage", func(t *testing.T) {
t.Parallel()
by := bytes.NewBuffer(nil)
root := &Command{
Name: "root",
Usage: "root [flags]",
Flags: FlagsFunc(func(fset *flag.FlagSet) {
fset.SetOutput(by)
}),
}
err := Parse(root, []string{"--help"})
require.Error(t, err)
require.ErrorIs(t, err, flag.ErrHelp)
require.Empty(t, by.String())
})
t.Run("no flags", func(t *testing.T) {
t.Parallel()
s := newTestState()
err := Parse(s.root, []string{"add", "item1"})
require.NoError(t, err)
cmd := getCommand(t, s.root)
require.Equal(t, s.add, cmd)
require.False(t, GetFlag[bool](s.root.state, "dry-run"))
})
t.Run("unknown flag", func(t *testing.T) {
t.Parallel()
s := newTestState()
err := Parse(s.root, []string{"add", "--unknown", "item1"})
require.Error(t, err)
require.Contains(t, err.Error(), `command "todo add": flag provided but not defined: -unknown`)
})
t.Run("with subcommand flags", func(t *testing.T) {
t.Parallel()
s := newTestState()
err := Parse(s.root, []string{"add", "--dry-run", "item1"})
require.NoError(t, err)
cmd := getCommand(t, s.root)
assert.Equal(t, s.add, cmd)
assert.True(t, GetFlag[bool](s.root.state, "dry-run"))
})
t.Run("help flag", func(t *testing.T) {
t.Parallel()
s := newTestState()
err := Parse(s.root, []string{"--help"})
require.Error(t, err)
require.ErrorIs(t, err, flag.ErrHelp)
})
t.Run("help flag with subcommand", func(t *testing.T) {
t.Parallel()
s := newTestState()
err := Parse(s.root, []string{"add", "--help"})
require.Error(t, err)
require.ErrorIs(t, err, flag.ErrHelp)
})
t.Run("help flag with subcommand at s.root", func(t *testing.T) {
t.Parallel()
s := newTestState()
err := Parse(s.root, []string{"--help", "add"})
require.Error(t, err)
require.ErrorIs(t, err, flag.ErrHelp)
})
t.Run("help flag with subcommand and other flags", func(t *testing.T) {
t.Parallel()
s := newTestState()
err := Parse(s.root, []string{"add", "--help", "--dry-run"})
require.Error(t, err)
require.ErrorIs(t, err, flag.ErrHelp)
})
t.Run("unknown subcommand", func(t *testing.T) {
t.Parallel()
s := newTestState()
err := Parse(s.root, []string{"unknown"})
require.Error(t, err)
require.Contains(t, err.Error(), "unknown command")
})
t.Run("flags at multiple levels", func(t *testing.T) {
t.Parallel()
s := newTestState()
err := Parse(s.root, []string{"add", "--dry-run", "item1", "--verbose"})
require.NoError(t, err)
cmd := getCommand(t, s.root)
assert.Equal(t, s.add, cmd)
assert.True(t, GetFlag[bool](s.root.state, "dry-run"))
assert.True(t, GetFlag[bool](s.root.state, "verbose"))
})
t.Run("nested subcommand and root flag", func(t *testing.T) {
t.Parallel()
s := newTestState()
err := Parse(s.root, []string{"--verbose", "nested", "sub", "--echo", "hello"})
require.NoError(t, err)
cmd := getCommand(t, s.root)
assert.Equal(t, s.sub, cmd)
assert.Equal(t, "hello", GetFlag[string](s.root.state, "echo"))
assert.True(t, GetFlag[bool](s.root.state, "verbose"))
})
t.Run("nested subcommand with mixed flags", func(t *testing.T) {
t.Parallel()
s := newTestState()
err := Parse(s.root, []string{"nested", "sub", "--echo", "hello", "--verbose"})
require.NoError(t, err)
cmd := getCommand(t, s.root)
assert.Equal(t, s.sub, cmd)
assert.Equal(t, "hello", GetFlag[string](s.root.state, "echo"))
assert.True(t, GetFlag[bool](s.root.state, "verbose"))
})
t.Run("end of options delimiter", func(t *testing.T) {
t.Parallel()
s := newTestState()
err := Parse(s.root, []string{"--verbose", "--", "nested", "sub", "--echo", "hello"})
require.NoError(t, err)
cmd := getCommand(t, s.root)
assert.Equal(t, s.root, cmd)
assert.Equal(t, []string{"nested", "sub", "--echo", "hello"}, s.root.state.Args)
assert.True(t, GetFlag[bool](s.root.state, "verbose"))
})
t.Run("flags and args", func(t *testing.T) {
t.Parallel()
s := newTestState()
err := Parse(s.root, []string{"add", "item1", "--dry-run", "item2"})
require.NoError(t, err)
cmd := getCommand(t, s.root)
assert.Equal(t, s.add, cmd)
assert.True(t, GetFlag[bool](s.root.state, "dry-run"))
assert.Equal(t, []string{"item1", "item2"}, s.root.state.Args)
})
t.Run("nested subcommand with flags and args", func(t *testing.T) {
t.Parallel()
s := newTestState()
err := Parse(s.root, []string{"nested", "sub", "--echo", "hello", "world"})
require.NoError(t, err)
cmd := getCommand(t, s.root)
assert.Equal(t, s.sub, cmd)
assert.Equal(t, "hello", GetFlag[string](s.root.state, "echo"))
assert.Equal(t, []string{"world"}, s.root.state.Args)
})
t.Run("subcommand flags not available in parent", func(t *testing.T) {
t.Parallel()
s := newTestState()
err := Parse(s.root, []string{"--dry-run"})
require.Error(t, err)
require.ErrorContains(t, err, "flag provided but not defined")
})
t.Run("parent flags inherited in subcommand", func(t *testing.T) {
t.Parallel()
s := newTestState()
err := Parse(s.root, []string{"nested", "sub", "--force"})
require.NoError(t, err)
cmd := getCommand(t, s.root)
assert.Equal(t, s.sub, cmd)
assert.True(t, GetFlag[bool](s.root.state, "force"))
})
t.Run("unrelated subcommand flags not inherited in other subcommands", func(t *testing.T) {
t.Parallel()
s := newTestState()
err := Parse(s.root, []string{"nested", "sub", "--dry-run"})
require.Error(t, err)
require.ErrorContains(t, err, "flag provided but not defined")
})
t.Run("empty name in subcommand", func(t *testing.T) {
t.Parallel()
s := newTestState()
s.sub.Name = ""
err := Parse(s.root, nil)
require.Error(t, err)
require.ErrorContains(t, err, `subcommand in path [todo, nested] has no name`)
})
t.Run("required flag", func(t *testing.T) {
t.Parallel()
{
s := newTestState()
err := Parse(s.root, []string{"nested", "hello"})
require.Error(t, err)
require.ErrorContains(t, err, `command "todo nested hello": required flags "-mandatory-flag, -another-mandatory-flag" not set`)
}
{
// Correct type - true
s := newTestState()
err := Parse(s.root, []string{"nested", "hello", "--mandatory-flag=true", "--another-mandatory-flag", "some-value"})
require.NoError(t, err)
cmd := getCommand(t, s.root)
assert.Equal(t, s.hello, cmd)
require.True(t, GetFlag[bool](s.root.state, "mandatory-flag"))
}
{
// Correct type - false
s := newTestState()
err := Parse(s.root, []string{"nested", "hello", "--mandatory-flag=false", "--another-mandatory-flag=some-value"})
require.NoError(t, err)
cmd := s.root.terminal()
assert.Equal(t, s.hello, cmd)
require.False(t, GetFlag[bool](s.root.state, "mandatory-flag"))
}
{
// Incorrect type
s := newTestState()
err := Parse(s.root, []string{"nested", "hello", "--mandatory-flag=not-a-bool"})
require.Error(t, err)
require.ErrorContains(t, err, `command "todo nested hello": invalid boolean value "not-a-bool" for -mandatory-flag: parse error`)
}
})
t.Run("unknown required flag set by cli author", func(t *testing.T) {
t.Parallel()
cmd := &Command{
Name: "root",
FlagsMetadata: []FlagMetadata{
{Name: "some-other-flag", Required: true},
},
}
err := Parse(cmd, nil)
require.Error(t, err)
// TODO(mf): consider improving this error message so it's obvious that a "required" flag
// was set by the cli author but not registered in the flag set
require.ErrorContains(t, err, `command "root": internal error: required flag -some-other-flag not found in flag set`)
})
t.Run("space in command name", func(t *testing.T) {
t.Parallel()
cmd := &Command{
Name: "root",
SubCommands: []*Command{
{Name: "sub command"},
},
}
err := Parse(cmd, nil)
require.Error(t, err)
require.ErrorContains(t, err, `failed to parse: command ["root", "sub command"]: must contain only letters, no spaces or special characters`)
})
}
func getCommand(t *testing.T, c *Command) *Command {
require.NotNil(t, c)
require.NotNil(t, c.state)
require.NotEmpty(t, c.state.path)
terminal := c.terminal()
require.NotNil(t, terminal)
return terminal
}