-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsubcmd_test.go
307 lines (249 loc) · 7.09 KB
/
subcmd_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
package subcmd
import (
"bytes"
"errors"
"flag"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
type testCommand struct {
*Cmd
rootFlag *bool
sub1 *Cmd
sub1Flag *string
sub1Args *[]string
sub11 *Cmd
sub11Flag *string
sub2 *Cmd
sub2Args ArgsStr
out bytes.Buffer
}
const longText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
func testRoot() *testCommand {
var root testCommand
root.Cmd = Root(
OptName("cmd"),
OptErrorHandling(flag.ContinueOnError),
OptOutput(&root.out),
OptSynopsis("cmd synopsis"),
OptDetails("testing command line example"))
root.rootFlag = root.Bool("flag", false, "example of bool flag")
root.sub1 = root.SubCommand("sub1", "a sub command with flags and sub commands", OptDetails(longText))
root.sub1Flag = root.sub1.String("flag", "", "example of string flag")
root.sub1Args = root.sub1.Args("", "")
root.sub11 = root.sub1.SubCommand("sub1", "sub command of sub command")
root.sub11Flag = root.sub11.String("flag", "", "example of string flag")
root.sub2 = root.SubCommand("sub2", "a sub command without flags and sub commands")
root.sub2Args = make(ArgsStr, 0, 1)
root.sub2.ArgsVar(&root.sub2Args, "[arg]", "arg is a single argument")
return &root
}
func TestSubCmd(t *testing.T) {
t.Parallel()
tests := []struct {
args []string
wantErr bool
sub1Parsed bool
sub11Parsed bool
sub2Parsed bool
rootFlag bool
sub1Flag string
sub1Args []string
sub11Flag string
sub2Args []string
}{
{
args: []string{"cmd"},
},
{
args: []string{"cmd", "-flag"},
rootFlag: true,
},
{
args: []string{"cmd", "-flag", "sub1", "-flag", "value"},
rootFlag: true,
sub1Parsed: true,
sub1Flag: "value",
},
{
args: []string{"cmd", "sub1", "-flag", "value"},
sub1Parsed: true,
sub1Flag: "value",
},
{
args: []string{"cmd", "sub1", "sub1", "-flag", "value"},
sub1Parsed: true,
sub11Parsed: true,
sub11Flag: "value",
},
{
args: []string{"cmd", "-no-such-flag"},
wantErr: true,
},
{
args: []string{"cmd", "-rootflag", "-no-such-flag"},
wantErr: true,
},
{
args: []string{"cmd", "sub1", "-no-such-flag"},
wantErr: true,
},
{
args: []string{"cmd", "arg1"},
wantErr: true,
},
{
args: []string{"cmd", "sub1", "arg1"},
sub1Parsed: true,
sub1Args: []string{"arg1"},
},
{
args: []string{"cmd", "sub1", "arg1", "arg2"},
sub1Parsed: true,
sub1Args: []string{"arg1", "arg2"},
},
{
args: []string{"cmd", "sub1", "-flag", "value", "arg1"},
sub1Parsed: true,
sub1Flag: "value",
sub1Args: []string{"arg1"},
},
{
args: []string{"cmd", "sub2", "arg1"},
sub2Parsed: true,
},
{
args: []string{"cmd", "sub2", "arg1", "arg2"},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(strings.Join(tt.args, " "), func(t *testing.T) {
root := testRoot()
err := root.Parse(tt.args)
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.sub1Parsed, root.sub1.Parsed())
assert.Equal(t, tt.sub11Parsed, root.sub11.Parsed())
assert.Equal(t, tt.sub2Parsed, root.sub2.Parsed())
assert.Equal(t, tt.rootFlag, *root.rootFlag)
assert.Equal(t, tt.sub1Flag, *root.sub1Flag)
assert.Equal(t, tt.sub11Flag, *root.sub11Flag)
}
})
}
}
func TestHelp(t *testing.T) {
t.Parallel()
tests := []struct {
args []string
want string
}{
{
args: []string{"cmd", "-h"},
want: `Usage: cmd [flags]
cmd synopsis
testing command line example
Subcommands:
sub1 a sub command with flags and sub commands
sub2 a sub command without flags and sub commands
Flags:
-flag
example of bool flag
`,
},
{
args: []string{"cmd", "sub1", "-h"},
want: `Usage: cmd sub1 [flags] [args...]
a sub command with flags and sub commands
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore
eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt
in culpa qui officia deserunt mollit anim id est laborum.
Subcommands:
sub1 sub command of sub command
Flags:
-flag string
example of string flag
`,
},
{
args: []string{"cmd", "sub2", "-h"},
want: `Usage: cmd sub2 [arg]
a sub command without flags and sub commands
Positional arguments:
arg is a single argument
`,
},
{
args: []string{"cmd", "sub1", "sub1", "-h"},
want: `Usage: cmd sub1 sub1 [flags]
sub command of sub command
Flags:
-flag string
example of string flag
`,
},
}
for _, tt := range tests {
t.Run(strings.Join(tt.args, " "), func(t *testing.T) {
root := testRoot()
err := root.Parse(tt.args)
assert.True(t, errors.As(err, &flag.ErrHelp))
assert.Equal(t, tt.want, root.out.String())
})
}
}
func TestCmd_failures(t *testing.T) {
t.Parallel()
t.Run("command can't have two sub commands with the same name", func(t *testing.T) {
cmd := Root()
cmd.SubCommand("sub", "synopsis")
assert.Panics(t, func() { cmd.SubCommand("sub", "synopsis") })
})
t.Run("parse must get at least one argument", func(t *testing.T) {
cmd := Root()
assert.Panics(t, func() { cmd.Parse(nil) })
})
t.Run("both command and sub command have positional arguments should panic", func(t *testing.T) {
cmd := Root()
cmd.Args("", "")
subcmd := cmd.SubCommand("sub", "synopsis")
subcmd.Args("", "")
assert.Panics(t, func() { cmd.ParseArgs() })
})
t.Run("both command and sub sub command have positional arguments should panic", func(t *testing.T) {
cmd := Root()
cmd.Args("", "")
sub := cmd.SubCommand("sub", "synopsis")
subsub := sub.SubCommand("sub", "synopsis")
subsub.Args("", "")
assert.Panics(t, func() { cmd.ParseArgs() })
})
t.Run("both sub command and sub sub command have positional arguments should panic", func(t *testing.T) {
cmd := Root()
sub := cmd.SubCommand("sub", "synopsis")
sub.Args("", "")
subsub := sub.SubCommand("sub", "synopsis")
subsub.Args("", "")
assert.Panics(t, func() { cmd.ParseArgs() })
})
t.Run("two different sub command may have positional arguments", func(t *testing.T) {
cmd := Root()
sub1 := cmd.SubCommand("sub1", "synopsis")
sub1.Args("", "")
sub2 := cmd.SubCommand("sub2", "synopsis")
sub2.Args("", "")
assert.NotPanics(t, func() { cmd.Parse([]string{"cmd"}) })
})
t.Run("calling positional more than once is not allowed", func(t *testing.T) {
cmd := Root()
cmd.Args("", "")
assert.Panics(t, func() { cmd.Args("", "") })
})
}