forked from ukautz/clif
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli_test.go
239 lines (217 loc) · 6.64 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
package clif
import (
"bytes"
"fmt"
. "github.com/smartystreets/goconvey/convey"
"io"
"testing"
)
type testCliAlias interface {
Hello() int
}
type testCliInject struct {
Foo int
}
func (this *testCliInject) Hello() int {
return this.Foo
}
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() {
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!")
})
})
}
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)
})
})
})
}