forked from ukautz/clif
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli_test.go
479 lines (439 loc) · 13.7 KB
/
cli_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
package clif
import (
"bytes"
"fmt"
. "github.com/smartystreets/goconvey/convey"
"io"
"reflect"
"strings"
"testing"
)
type testCliAlias interface {
Hello() int
}
type testCliInject struct {
Foo int
}
func (this *testCliInject) Hello() int {
return this.Foo
}
func TestCliCall(t *testing.T) {
Convey("Call cli command", t, func() {
val := []string{}
cmd := NewCommand("foo", "Do foo", func() {
val = append(val, "called")
})
cli := New("cli", "0.1.0", "The cli")
vals, err := cli.Call(cmd)
So(err, ShouldBeNil)
So(len(vals), ShouldEqual, 0)
So(strings.Join(val, " ** "), ShouldEqual, "called")
Convey("With return values", func() {
val = []string{}
cmd.Call = reflect.ValueOf(func() (int, string, error) {
val = append(val, "called")
return 10, "foo", nil
})
vals, err = cli.Call(cmd)
So(len(vals), ShouldEqual, 2)
So(err, ShouldBeNil)
So(strings.Join(val, " ** "), ShouldEqual, "called")
So(vals[0].Interface(), ShouldResemble, 10)
So(vals[1].Interface(), ShouldResemble, "foo")
Convey("With error and values", func() {
val = []string{}
cmd.Call = reflect.ValueOf(func() (int, string, error) {
val = append(val, "called")
return 10, "foo", fmt.Errorf("The error")
})
vals, err = cli.Call(cmd)
So(len(vals), ShouldEqual, 2)
So(err, ShouldResemble, NewCallError(fmt.Errorf("The error")))
So(strings.Join(val, " ** "), ShouldEqual, "called")
So(vals[0].Interface(), ShouldResemble, 10)
So(vals[1].Interface(), ShouldResemble, "foo")
})
})
Convey("With pre call on command", func() {
val = []string{}
cmd.SetPreCall(func(c *Command) {
val = append(val, fmt.Sprintf("pre (%s)", c.Name))
})
_, err = cli.Call(cmd)
So(err, ShouldBeNil)
So(strings.Join(val, " ** "), ShouldEqual, "pre (foo) ** called")
Convey("With values, which are not delegated", func() {
val = []string{}
cmd.SetPreCall(func(c *Command) (int, string) {
val = append(val, fmt.Sprintf("pre (%s)", c.Name))
return 10, "foo"
})
vals, err := cli.Call(cmd)
So(err, ShouldBeNil)
So(len(vals), ShouldEqual, 0)
So(strings.Join(val, " ** "), ShouldEqual, "pre (foo) ** called")
})
Convey("With error, which is delegated", func() {
val = []string{}
cmd.SetPreCall(func(c *Command) error {
val = append(val, fmt.Sprintf("pre (%s)", c.Name))
return fmt.Errorf("Pre Error")
})
_, err = cli.Call(cmd)
So(err, ShouldResemble, NewCallError(fmt.Errorf("Pre Error")))
So(strings.Join(val, " ** "), ShouldEqual, "pre (foo)")
Convey("With error and values, which are not delegated", func() {
val = []string{}
cmd.SetPreCall(func(c *Command) (int, string, error) {
val = append(val, fmt.Sprintf("pre (%s)", c.Name))
return 10, "foo", fmt.Errorf("Pre Error")
})
vals, err := cli.Call(cmd)
So(err, ShouldResemble, NewCallError(fmt.Errorf("Pre Error")))
So(len(vals), ShouldEqual, 0)
So(strings.Join(val, " ** "), ShouldEqual, "pre (foo)")
})
})
Convey("With post call", func() {
val = []string{}
cmd.SetPostCall(func(c *Command) {
val = append(val, fmt.Sprintf("post (%s)", c.Name))
})
_, err = cli.Call(cmd)
So(err, ShouldBeNil)
So(strings.Join(val, " ** "), ShouldEqual, "pre (foo) ** called ** post (foo)")
})
})
Convey("With post call", func() {
val = []string{}
cmd.SetPostCall(func(c *Command) {
val = append(val, fmt.Sprintf("post (%s)", c.Name))
})
_, err = cli.Call(cmd)
So(err, ShouldBeNil)
So(strings.Join(val, " ** "), ShouldEqual, "called ** post (foo)")
Convey("With values, which are not delegated", func() {
val = []string{}
cmd.SetPostCall(func(c *Command) (int, string) {
val = append(val, fmt.Sprintf("post (%s)", c.Name))
return 10, "foo"
})
vals, err := cli.Call(cmd)
So(err, ShouldBeNil)
So(len(vals), ShouldEqual, 0)
So(strings.Join(val, " ** "), ShouldEqual, "called ** post (foo)")
})
Convey("With error, which is delegated", func() {
val = []string{}
cmd.SetPostCall(func(c *Command) error {
val = append(val, fmt.Sprintf("post (%s)", c.Name))
return fmt.Errorf("Post Error")
})
_, err = cli.Call(cmd)
So(err, ShouldResemble, NewCallError(fmt.Errorf("Post Error")))
So(strings.Join(val, " ** "), ShouldEqual, "called ** post (foo)")
Convey("With error and values, which are not delegated", func() {
val = []string{}
cmd.SetPostCall(func(c *Command) (int, string, error) {
val = append(val, fmt.Sprintf("post (%s)", c.Name))
return 10, "foo", fmt.Errorf("Post Error")
})
vals, err := cli.Call(cmd)
So(err, ShouldResemble, NewCallError(fmt.Errorf("Post Error")))
So(strings.Join(val, " ** "), ShouldEqual, "called ** post (foo)")
So(len(vals), ShouldEqual, 0)
})
})
})
})
}
func TestCliRun(t *testing.T) {
Convey("Run cli command", t, func() {
called := 0
var handledErr error
Die = func(msg string, args ...interface{}) {
panic(fmt.Sprintf(msg, args...))
}
Exit = func(s int) {
panic(fmt.Sprintf("Exit %d", s))
}
namedActual := make(map[string]interface{})
c := New("foo", "1.0.0", "").
New("bar", "", func(c *Cli, o *Command) error {
called = 1
return nil
}).
New("zoing", "", func(x *testCliInject) error {
called = x.Foo
return nil
}).
New("zoing2", "", func(x testCliAlias) error {
called = x.Hello()
return nil
}).
New("oops", "", func(x io.Writer) error {
panic("Should never be called")
return nil
}).
New("errme", "", func() error {
return fmt.Errorf("I error!")
}).
New("named", "", func(named NamedParameters) {
namedActual = map[string]interface{}(named)
}).
New("named2", "", func(x testCliAlias, named NamedParameters, y *testCliInject) {
namedActual = map[string]interface{}(named)
}).
Register(&testCliInject{
Foo: 100,
}).
RegisterAs("clif.testCliAlias", &testCliInject{
Foo: 200,
})
cmdInvalid := NewCommand("bla", "Dont use me", func() {})
argInvalid := NewArgument("something", "..", "", false, false)
argInvalid.SetParse(func(name, value string) (string, error) {
return "", fmt.Errorf("Never works!")
})
cmdInvalid.AddArgument(argInvalid)
c.Add(cmdInvalid)
Convey("Run existing method", func() {
c.RunWith([]string{"bar"})
So(handledErr, ShouldBeNil)
So(called, ShouldEqual, 1)
})
Convey("Run existing method with injection", func() {
c.RunWith([]string{"zoing"})
So(handledErr, ShouldBeNil)
So(called, ShouldEqual, 100)
})
Convey("Run existing method with interface injection", func() {
c.RunWith([]string{"zoing2"})
So(handledErr, ShouldBeNil)
So(called, ShouldEqual, 200)
})
Convey("Run existing method with named parameters", func() {
c.RegisterNamed("foo", "bar")
c.RegisterNamed("baz", 213)
c.RunWith([]string{"named"})
So(namedActual, ShouldResemble, map[string]interface{}{"foo": "bar", "baz": 213})
})
Convey("Run existing method with named parameters on arbitrary position", func() {
c.RegisterNamed("foo", "bar")
c.RegisterNamed("baz", 213)
c.RunWith([]string{"named2"})
So(namedActual, ShouldResemble, map[string]interface{}{"foo": "bar", "baz": 213})
})
Convey("Run not existing method", func() {
So(func() {
c.RunWith([]string{"baz"})
}, ShouldPanicWith, "Command \"baz\" unknown")
})
Convey("Run without args describes and exits", func() {
buf := bytes.NewBuffer(nil)
out := NewOutput(buf, NewDefaultFormatter(map[string]string{}))
c.SetOutput(out)
c.RunWith([]string{})
So(buf.String(), ShouldEqual, DescribeCli(c))
})
Convey("Run method with not registered arg fails", func() {
So(func() {
c.RunWith([]string{"oops"})
}, ShouldPanicWith, `Callback parameter of type io.Writer for command "oops" was not found in registry`)
})
Convey("Run method with invalid arg fails", func() {
So(func() {
buf := bytes.NewBuffer(nil)
out := NewOutput(buf, NewDefaultFormatter(map[string]string{}))
c.SetOutput(out)
c.RunWith([]string{"bla", "bla"})
}, ShouldPanicWith, "Parse error: Parameter \"something\" invalid: Never works!")
})
Convey("Run method with resulting error returns it", func() {
So(func() {
c.RunWith([]string{"errme"})
}, ShouldPanicWith, "Failure in execution: I error!")
})
Convey("Run with cli-wide pre call", func() {
name := "NOT"
c.SetPreCall(func(c *Command) error {
name = c.Name
return nil
})
c.RunWith([]string{"bar"})
So(name, ShouldEqual, "bar")
Convey("Run with error in cli-wide pre call", func() {
c.SetPreCall(func(c *Command) error {
return fmt.Errorf("Abort "+ c.Name)
})
So(func() {
c.RunWith([]string{"bar"})
}, ShouldPanicWith, `Abort bar`)
})
})
})
}
func TestCliConstruction(t *testing.T) {
Convey("Create new Cli with commands", t, func() {
app := New("My App", "1.0.0", "Testing app")
cb := func() {}
Convey("Two default commands exist", func() {
So(len(app.Commands), ShouldEqual, 2)
Convey("One is \"help\"", func() {
_, ok := app.Commands["help"]
So(ok, ShouldBeTrue)
Convey("Other is \"list\"", func() {
_, ok := app.Commands["list"]
So(ok, ShouldBeTrue)
})
})
})
Convey("Command constructur adds new command", func() {
app.New("foo", "For fooing", cb)
So(len(app.Commands), ShouldEqual, 3)
So(app.Commands["foo"], ShouldNotBeNil)
})
Convey("Adding can be used variadic", func() {
app.New("foo", "For fooing", cb)
cmds := []*Command{
NewCommand("foo", "For fooing", cb),
NewCommand("bar", "For baring", cb),
}
app.Add(cmds...)
So(len(app.Commands), ShouldEqual, 4)
So(app.Commands["foo"], ShouldNotBeNil)
So(app.Commands["bar"], ShouldNotBeNil)
})
})
}
func TestCliDefaultCommand(t *testing.T) {
Convey("Change default command of cli", t, func() {
x := 0
app := New("My App", "1.0.0", "Testing app").
SetDefaultCommand("other").
New("other", "Something else", func() { x += 1 })
So(app.DefaultCommand, ShouldEqual, "other")
Convey("Calling default command", func() {
app.RunWith(nil)
So(x, ShouldEqual, 1)
})
})
}
func TestCliDefaultOptions(t *testing.T) {
Convey("Adding default options to cli", t, func() {
app := New("My App", "1.0.0", "Testing app")
So(len(app.DefaultOptions), ShouldEqual, 0)
Convey("Using default option creator adds option", func() {
app.NewDefaultOption("foo", "f", "fooing", "", false, false)
So(len(app.DefaultOptions), ShouldEqual, 1)
})
Convey("Adding default option .. adds them", func() {
app.AddDefaultOptions(
NewOption("foo", "f", "fooing", "", false, false),
NewOption("bar", "b", "baring", "", false, false),
)
So(len(app.DefaultOptions), ShouldEqual, 2)
})
Convey("Cli default options are not added to command on command create", func() {
app.NewDefaultOption("foo", "f", "fooing", "", false, false)
cmd := NewCommand("bla", "bla", func() {})
app.Add(cmd)
So(len(cmd.Options), ShouldEqual, len(DefaultOptions))
Convey("Default options are added in run", func() {
app.RunWith([]string{"bla"})
So(len(cmd.Options), ShouldEqual, len(DefaultOptions)+1)
})
})
})
}
func TestCliHeralds(t *testing.T) {
Convey("Command heralds are add late, in run", t, func() {
app := New("My App", "1.0.0", "Testing app")
So(len(app.Commands), ShouldEqual, 2)
So(len(app.Heralds), ShouldEqual, 0)
Convey("Heralding command does not add it to list", func() {
x := 0
app.Herald(func(c *Cli) *Command {
return NewCommand("foo", "fooing", func() { x = 2 })
})
So(len(app.Commands), ShouldEqual, 2)
So(len(app.Heralds), ShouldEqual, 1)
Convey("Running adds heralded commands", func() {
app.RunWith([]string{"foo"})
So(x, ShouldEqual, 2)
So(len(app.Commands), ShouldEqual, 3)
So(len(app.Heralds), ShouldEqual, 0)
})
})
})
}
func TestCliNamedRegistryParameter(t *testing.T) {
Convey("Setting and accessing named parameter in registry", t, func() {
app := New("My App", "1.0.0", "Testing app")
app.RegisterNamed("foo", "foo")
app.RegisterNamed("bar", 123)
obj := &testCliInject{}
app.RegisterNamed("baz", obj)
Convey("Accessing named parameters", func() {
So(app.Named("foo"), ShouldEqual, "foo")
So(app.Named("bar"), ShouldEqual, 123)
So(app.Named("baz"), ShouldEqual, obj)
Convey("Accessing not existing named paraemter", func() {
So(app.Named("zoing"), ShouldBeNil)
})
})
})
}
var testCliSeparateArgs = []struct {
args []string
expectName string
expectArgs []string
}{
{
args: []string{"foo", "--bar", "baz"},
expectName: "foo",
expectArgs: []string{"--bar", "baz"},
},
{
args: []string{"foo", "-bar", "baz"},
expectName: "foo",
expectArgs: []string{"-bar", "baz"},
},
{
args: []string{"foo", "baz", "--bar"},
expectName: "foo",
expectArgs: []string{"baz", "--bar"},
},
{
args: []string{"--bar", "foo", "baz"},
expectName: "foo",
expectArgs: []string{"--bar", "baz"},
},
{
args: []string{"--bar=boing", "foo", "baz"},
expectName: "foo",
expectArgs: []string{"--bar=boing", "baz"},
},
{
args: []string{"-bar=boing", "foo", "baz"},
expectName: "foo",
expectArgs: []string{"-bar=boing", "baz"},
},
{
args: []string{"--bar", "boing", "foo", "baz"},
expectName: "boing",
expectArgs: []string{"--bar", "foo", "baz"},
},
}
func TestCliSeparateArgs(t *testing.T) {
Convey("Separate command line args", t, func() {
app := New("My App", "1.0.0", "Testing app")
for i, test := range testCliSeparateArgs {
Convey(fmt.Sprintf("%d) From %v", i, test.args), func() {
cname, cargs := app.SeparateArgs(test.args)
So(cname, ShouldEqual, test.expectName)
So(cargs, ShouldResemble, test.expectArgs)
})
}
})
}