-
Notifications
You must be signed in to change notification settings - Fork 15
/
command_test.go
425 lines (406 loc) · 11.6 KB
/
command_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
package clif
import (
"fmt"
. "github.com/smartystreets/goconvey/convey"
"regexp"
"strings"
"testing"
"time"
"os"
)
func _testInitCommand() *Command {
return &Command{
Arguments: []*Argument{
{
parameter: parameter{
Name: "foo",
Required: true,
Regex: regexp.MustCompile(`^a`),
},
},
{
parameter: parameter{
Name: "bar",
Multiple: true,
Parse: func(name, value string) (string, error) {
if strings.Index(value, "B") == -1 {
return "", fmt.Errorf("Missing B")
} else {
return value, nil
}
},
},
},
},
Options: []*Option{
{
parameter: parameter{
Name: "baz",
Required: true,
},
},
{
parameter: parameter{
Name: "bang",
Default: "the default",
Multiple: true,
},
},
{
parameter: parameter{
Name: "zoing",
Multiple: true,
},
Flag: true,
Alias: "z",
},
},
}
}
func TestCommandCreation(t *testing.T) {
Convey("Creating command with constructor", t, func() {
Convey("Adding command with empty signature and empty return", func() {
c := NewCommand("name", "Usage", func() {
//
})
So(c, ShouldNotBeNil)
})
Convey("Adding command with non-empty signature and empty return", func() {
c := NewCommand("name", "Usage", func(foo string, bar int, baz time.Time) {
//
})
So(c, ShouldNotBeNil)
})
Convey("Adding command with empty signature and non-empty return", func() {
c := NewCommand("name", "Usage", func() (string, int, time.Time, error) {
return "", 0, time.Now(), nil
})
So(c, ShouldNotBeNil)
})
Convey("Adding command with non-empty signature and non-empty return", func() {
c := NewCommand("name", "Usage", func(foo string, bar int, baz time.Time) (string, int, time.Time, error) {
return "", 0, time.Now(), nil
})
So(c, ShouldNotBeNil)
})
Convey("Adding command which is not a func", func() {
So(func() {
NewCommand("name", "Usage", "foo")
}, ShouldPanicWith, "Call must be method, but is string")
})
Convey("Adding command which is also not a func", func() {
So(func() {
NewCommand("name", "Usage", time.Now())
}, ShouldPanicWith, "Call must be method, but is struct")
})
})
}
var testsCommandParse = []struct {
in []string
vals map[string][]string
err error
}{
{
in: []string{},
err: fmt.Errorf("Argument \"foo\" is required but missing"),
},
{
in: []string{"first"},
err: fmt.Errorf("Parameter \"foo\" invalid: Does not match criteria"),
},
{
in: []string{""},
err: fmt.Errorf("Parameter \"foo\" invalid: Does not match criteria"),
},
{
in: []string{"afirst"},
err: fmt.Errorf("Option \"baz\" is required but missing"),
},
{
in: []string{"afirst", "second"},
err: fmt.Errorf("Parameter \"bar\" invalid: Missing B"),
},
{
in: []string{"afirst", "second with B"},
err: fmt.Errorf("Option \"baz\" is required but missing"),
},
{
in: []string{"afirst", "--baz=x"},
vals: map[string][]string{
"foo": []string{"afirst"},
"baz": []string{"x"},
"bang": []string{"the default"},
},
},
{
in: []string{"afirst", "--baz", "x"},
vals: map[string][]string{
"foo": []string{"afirst"},
"baz": []string{"x"},
"bang": []string{"the default"},
},
},
{
in: []string{"afirst", "--baz=x", "--baz=y"},
err: fmt.Errorf("Parameter \"baz\" does not support multiple values"),
},
{
in: []string{"afirst", "--baz", "--baz", "x"},
err: fmt.Errorf("Missing value for option \"--baz\""),
},
{
in: []string{"afirst", "second with B", "--baz=x"},
vals: map[string][]string{
"foo": []string{"afirst"},
"bar": []string{"second with B"},
"baz": []string{"x"},
"bang": []string{"the default"},
},
},
{
in: []string{"afirst", "second with B", "another bar param", "--baz=x"},
err: fmt.Errorf("Parameter \"bar\" invalid: Missing B"),
},
{
in: []string{"afirst", "second with B", "another Bar param", "--baz=x"},
vals: map[string][]string{
"foo": []string{"afirst"},
"bar": []string{"second with B", "another Bar param"},
"baz": []string{"x"},
"bang": []string{"the default"},
},
},
{
in: []string{"afirst", "--baz=x", "second with B", "another Bar param"},
vals: map[string][]string{
"foo": []string{"afirst"},
"bar": []string{"second with B", "another Bar param"},
"baz": []string{"x"},
"bang": []string{"the default"},
},
},
{
in: []string{"afirst", "--baz=x", "second with B", "another Bar param", "--bang", "Bang!"},
vals: map[string][]string{
"foo": []string{"afirst"},
"bar": []string{"second with B", "another Bar param"},
"baz": []string{"x"},
"bang": []string{"Bang!"},
},
},
{
in: []string{"afirst", "--baz=x", "second with B", "another Bar param", "--bang", "Bang!", "--bang=Bang!!"},
vals: map[string][]string{
"foo": []string{"afirst"},
"bar": []string{"second with B", "another Bar param"},
"baz": []string{"x"},
"bang": []string{"Bang!", "Bang!!"},
},
},
{
in: []string{"afirst", "--baz=x", "second with B", "another Bar param", "--bang", "Bang!", "--zoing=z"},
err: fmt.Errorf("Flag \"--zoing=z\" cannot have value"),
},
{
in: []string{"afirst", "--baz=x", "second with B", "another Bar param", "--bang", "Bang!", "--zoing"},
vals: map[string][]string{
"foo": []string{"afirst"},
"bar": []string{"second with B", "another Bar param"},
"baz": []string{"x"},
"bang": []string{"Bang!"},
"zoing": []string{"true"},
},
},
{
in: []string{"afirst", "--baz=x", "second with B", "another Bar param", "--bang", "Bang!", "--zoing", "-z", "-z"},
vals: map[string][]string{
"foo": []string{"afirst"},
"bar": []string{"second with B", "another Bar param"},
"baz": []string{"x"},
"bang": []string{"Bang!"},
"zoing": []string{"true", "true", "true"},
},
},
{
in: []string{"-="},
err: fmt.Errorf("Malformed option \"-=\""),
},
{
in: []string{"--not-there"},
err: fmt.Errorf("Unrecognized option \"--not-there\""),
},
}
func TestCommandParse(t *testing.T) {
Convey("Parse commands", t, func() {
for i, test := range testsCommandParse {
Convey(fmt.Sprintf("%2d) \"%s\"", i, strings.Join(test.in, "\", \"")), func() {
c := _testInitCommand()
err := c.Parse(test.in)
So(err, ShouldResemble, test.err)
if err == nil {
So(c.Input(), ShouldResemble, test.vals)
}
})
}
})
}
func TestCommandParseFallsBackToDefaults(t *testing.T) {
Convey("Parse commands with default values for options and/or arguments", t, func() {
c := NewCommand("command", "Usage", func() {}).
NewArgument("foo", "For fooing", "FOO", true, false).
NewOption("bar", "b", "For baring", "BAR", true, false)
Convey("Missing required argument defaults", func() {
err := c.Parse([]string{})
So(err, ShouldBeNil)
So(c.Argument("foo").String(), ShouldEqual, "FOO")
})
Convey("Missing required option defaults", func() {
err := c.Parse([]string{})
So(err, ShouldBeNil)
So(c.Option("bar").String(), ShouldEqual, "BAR")
})
})
}
func TestCommandParseFallsBackToEnvBeforeDefault(t *testing.T) {
Convey("Parse commands with default values for options and/or arguments", t, func() {
os.Setenv("the_foo", "FOO_ENV")
os.Setenv("the_bar", "BAR_ENV")
c := NewCommand("command", "Usage", func() {}).
AddArgument(NewArgument("foo", "For fooing", "FOO", true, false).SetEnv("the_foo")).
AddOption(NewOption("bar", "b", "For baring", "BAR", true, false).SetEnv("the_bar"))
Convey("Missing required argument defaults", func() {
err := c.Parse([]string{})
So(err, ShouldBeNil)
So(c.Argument("foo").String(), ShouldEqual, "FOO_ENV")
})
Convey("Missing required option defaults", func() {
err := c.Parse([]string{})
So(err, ShouldBeNil)
So(c.Option("bar").String(), ShouldEqual, "BAR_ENV")
})
})
}
func TestCommandAccess(t *testing.T) {
Convey("Comand argument & option access", t, func() {
c := _testInitCommand()
Convey("Existing argument accessible", func() {
So(c.Argument("foo"), ShouldNotBeNil)
})
Convey("Not existing argument not accessible", func() {
So(c.Argument("fooz"), ShouldBeNil)
})
Convey("Existing option accessible", func() {
So(c.Option("baz"), ShouldNotBeNil)
})
Convey("Not existing option not accessible", func() {
So(c.Option("bazz"), ShouldBeNil)
})
})
}
func TestCommandAddingArgument(t *testing.T) {
Convey("Add arguments to command", t, func() {
c := NewCommand("foo", "Doing foo", func(c *Command) error {
return nil
})
Convey("Adding single argument", func() {
c.NewArgument("bar", "A bar", "123", true, false)
So(len(c.Arguments), ShouldEqual, 1)
So(c.Arguments[0], ShouldResemble, &Argument{
parameter: parameter{
Name: "bar",
Usage: "A bar",
Default: "123",
Required: true,
},
})
Convey("Adding additional argument with different name", func() {
c.NewArgument("baz", "A baz", "", false, false)
So(len(c.Arguments), ShouldEqual, 2)
Convey("Adding argument of same name does not fly", func() {
So(func() {
c.NewArgument("baz", "A baz", "", false, false)
}, ShouldPanicWith, `Argument with name "baz" already existing`)
})
})
Convey("Adding multiple style argument", func() {
c.NewArgument("baz", "A baz", "", false, true)
So(len(c.Arguments), ShouldEqual, 2)
Convey("Cannot add another argument after that", func() {
So(func() {
c.NewArgument("other", "A baz", "", false, false)
}, ShouldPanicWith, `Cannot add argument after multiple style argument`)
})
})
Convey("Adding argument with existing option does not fly", func() {
c.Options = []*Option{
{
parameter: parameter{
Name: "aaa",
},
},
{
parameter: parameter{
Name: "bbb",
},
Alias: "b",
},
}
So(func() {
c.NewArgument("aaa", "A baz", "", false, false)
}, ShouldPanicWith, `Option with name or alias "aaa" already existing`)
So(func() {
c.NewArgument("b", "A baz", "", false, false)
}, ShouldPanicWith, `Option with name or alias "b" already existing`)
})
})
})
}
func TestCommandAddingOptions(t *testing.T) {
Convey("Add options to command", t, func() {
c := NewCommand("foo", "Doing foo", func(c *Command) error {
return nil
})
Convey("Adding single option", func() {
c.NewOption("bar", "b", "A bar", "123", true, false)
So(len(c.Options), ShouldEqual, 2)
So(c.Options, ShouldResemble, []*Option{DefaultHelpOption, {
parameter: parameter{
Name: "bar",
Usage: "A bar",
Default: "123",
Required: true,
},
Alias: "b",
}})
Convey("Adding additional option with different name", func() {
c.NewOption("baz", "", "A baz", "", false, false)
So(len(c.Options), ShouldEqual, 3)
Convey("Adding option with existing name does not fly", func() {
So(func() {
c.NewOption("baz", "", "A baz", "", false, false)
}, ShouldPanicWith, `Option with name or alias "baz" already existing`)
})
Convey("Adding option with existing alias does not fly", func() {
So(func() {
c.NewOption("bazz", "b", "A baz", "", false, false)
}, ShouldPanicWith, `Option with name or alias "b" already existing`)
})
})
Convey("Adding option with existing argument does not fly", func() {
c.Arguments = []*Argument{
{
parameter: parameter{
Name: "a",
},
},
}
So(func() {
c.NewOption("a", "", "A baz", "", false, false)
}, ShouldPanicWith, `Argument with name "a" already existing`)
So(func() {
c.NewOption("aaa", "a", "A baz", "", false, false)
}, ShouldPanicWith, `Cannot use alias: Argument with name "a" already existing`)
})
})
})
}