-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_test.go
303 lines (253 loc) · 8.58 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
// Copyright (c) Christoph Berger. All rights reserved.
// Use of this source code is governed by the BSD (3-Clause)
// License that can be found in the LICENSE.txt file.
//
// This source code may import third-party source code whose
// licenses are provided in the respective license files.
package start
import (
"fmt"
"os"
"testing"
. "github.com/smartystreets/goconvey/convey"
flag "github.com/spf13/pflag"
)
func TestAdd(t *testing.T) {
Convey("When adding a command to the command map, then...", t, func() {
Commands.Add(&Command{
Name: "cmd1",
})
Convey("the command map should contain it", func() {
So(Commands["cmd1"], ShouldNotBeNil)
So(Commands["cmd1"].Name, ShouldEqual, "cmd1")
})
Convey("adding a command with the same name should fail", func() {
err := Commands.Add(&Command{
Name: "cmd1",
})
So(err, ShouldNotBeNil)
})
Convey("adding a subcommand to the command should work", func() {
err := Commands.Add(&Command{
Parent: "cmd1",
Name: "subcmd1",
})
So(err, ShouldBeNil)
So(len(Commands["cmd1"].children), ShouldEqual, 1)
So(Commands["cmd1"].children["subcmd1"], ShouldNotBeNil)
So(Commands["cmd1"].children["subcmd1"].Name, ShouldEqual, "subcmd1")
Convey("adding the same subcommand twice should fail", func() {
err := Commands.Add(&Command{
Parent: "cmd1",
Name: "subcmd1",
})
So(err, ShouldNotBeNil)
})
})
Reset(func() {
Commands = make(CommandMap)
})
})
}
func TestCommands(t *testing.T) {
var yes bool
var size int
var global string
// ContinueOnError is required when running goconvey as server; otherwise, unrecognized
// flags that are passed to the test executable will cause an error:
// "unknown shorthand flag: 't' in -test.v=true"
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
// To suppress warnings resulting from standard flags -test and -json,
// read -t and -j into dummy flags.
// The flags used for actual testing must not use -t or -j shorthands.
var testflag string
var jsonflag string
flag.StringVarP(&testflag, "t", "t", "t", "t")
flag.StringVarP(&jsonflag, "j", "j", "j", "j")
flag.BoolVarP(&yes, "yes", "y", false, "A boolean flag")
flag.IntVarP(&size, "size", "s", 23, "An int flag")
flag.StringVarP(&global, "global", "g", "global flag", "A global string flag")
Commands = make(CommandMap) // Ensure the Commands map starts empty for the test
os.Args = []string{os.Args[0]}
Convey("Ensure that the test flags exist", t, func() {
if err := Parse(); err != nil {
fmt.Println(err)
}
So(flag.Lookup("yes").Name, ShouldEqual, "yes")
So(flag.Lookup("size").Name, ShouldEqual, "size")
})
Convey("When setting up some commands, then...", t, func() {
SetDescription("This is the test application for the start package.")
Add(&Command{
Name: "test",
Short: "A test command",
Long: "Command test helps testing the start package. It accepts all flags.",
Cmd: func(cmd *Command) error {
fmt.Println("This is the test command.")
return nil
},
})
Add(&Command{
Name: "flags",
Flags: []string{"yes", "size"},
Short: "A test command with flags",
Long: "Command flags helps testing flags.",
Cmd: func(cmd *Command) error {
fmt.Println("This is the testflags command.")
fmt.Printf("--yes is %v", yes)
fmt.Printf("--size is %v", size)
return nil
},
})
Add(&Command{
Name: "do",
Short: "A command with subcommands",
Long: `Command do helps testing subcommands.
Usage:
do something
do nothing`,
})
Add(&Command{
Parent: "do",
Name: "something",
Short: "A subcommand that does something.",
Long: "do something does something",
Cmd: func(cmd *Command) error {
fmt.Println("This is the do something command.")
return nil
},
})
Add(&Command{
Parent: "do",
Name: "nothing",
Short: "A subcommand that does nothing.",
Long: "do nothing does nothing",
Cmd: func(cmd *Command) error {
fmt.Println("This is the do nothing command.")
return nil
},
})
if err := Parse(); err != nil {
fmt.Println(err)
}
Convey("readCommand should identify all of them correctly", func() {
cmd, err := readCommand([]string{"test", "arg1", "arg2"})
So(cmd, ShouldNotBeNil)
So(cmd.Name, ShouldEqual, "test")
So(cmd.Args, ShouldResemble, []string{"arg1", "arg2"})
So(err, ShouldBeNil)
cmd, err = readCommand([]string{"do", "something", "arg1"})
So(cmd, ShouldNotBeNil)
So(cmd.Name, ShouldEqual, "something")
So(cmd.Args, ShouldResemble, []string{"arg1"})
So(err, ShouldBeNil)
cmd, err = readCommand([]string{"do", "nothing"})
So(cmd, ShouldNotBeNil)
So(cmd.Name, ShouldEqual, "nothing")
So(cmd.Args, ShouldResemble, []string{})
So(err, ShouldBeNil)
})
Convey("readCommand should return the Usage command if no valid command was passed in", func() {
cmd, err := readCommand([]string{"invalid", "arg1"})
So(cmd, ShouldNotBeNil)
// Currently not possible: Test if cmd.Cmd returns the Usage command.
So(err, ShouldBeNil)
})
Convey("Usage() should print the usage", func() {
Usage(nil)
Usage(Commands["test"])
Usage(Commands["flags"])
Usage(Commands["do"])
Usage(Commands["do"].children["something"])
Usage(Commands["do"].children["nothing"])
})
Reset(func() {
Commands = make(CommandMap)
})
})
}
func TestCheckFlags(t *testing.T) {
var first int
var second int
var third int
var global int
var rejectedFlags map[string]bool
// ContinueOnError is required when running goconvey as server; otherwise, unrecognized
// flags that are passed to the test executable will cause an error:
// "unknown shorthand flag: 't' in -test.v=true"
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
// To suppress warnings resulting from standard flags -test and -json,
// read -t and -j into dummy flags.
// The flags used for actual testing must not use -t or -j shorthands.
var testflag string
var jsonflag string
flag.StringVarP(&testflag, "t", "t", "t", "t")
flag.StringVarP(&jsonflag, "j", "j", "j", "j")
flag.IntVarP(&first, "first", "f", 1, "The first flag")
flag.IntVarP(&second, "second", "s", 2, "The second flag")
flag.IntVarP(&third, "third", "h", 3, "The third flag")
flag.IntVarP(&global, "global", "g", 4, "The global flag")
os.Args = []string{os.Args[0], "--first=10", "--second=20", "--third=30", "--global=40", "anargument", "anotherarg"}
Commands = make(CommandMap) // clear the commands map for this test
Add(&Command{
Name: "cmd12",
Flags: []string{"first", "second"},
})
Add(&Command{
Name: "cmd23",
Flags: []string{"second", "third"},
})
Add(&Command{
Name: "cmd123",
Flags: []string{"first", "second", "third"},
})
if err := Parse(); err != nil {
fmt.Println(err)
}
Convey("A command should accept its own flags and all global flags", t, func() {
rejectedFlags = checkFlags(Commands["cmd123"])
So(len(rejectedFlags), ShouldEqual, 0)
})
Convey("A command should reject the flags that belong to the other command only", t, func() {
rejectedFlags = checkFlags(Commands["cmd23"])
So(len(rejectedFlags), ShouldEqual, 1)
So(rejectedFlags["first"], ShouldEqual, true)
rejectedFlags = checkFlags(Commands["cmd12"])
So(len(rejectedFlags), ShouldEqual, 1)
So(rejectedFlags["third"], ShouldEqual, true)
})
}
func TestExternal(t *testing.T) {
var yes bool
// ContinueOnError is required when running goconvey as server; otherwise, unrecognized
// flags that are passed to the test executable will cause an error:
// "unknown shorthand flag: 't' in -test.v=true"
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
// To suppress warnings resulting from standard flags -test and -json,
// read -t and -j into dummy flags.
// The flags used for actual testing must not use -t or -j shorthands.
var testflag, jsonflag string
flag.StringVarP(&testflag, "t", "t", "t", "t")
flag.StringVarP(&jsonflag, "j", "j", "j", "j")
flag.BoolVarP(&yes, "yes", "y", false, "A boolean flag")
Commands = make(CommandMap) // Ensure the Commands map starts empty for the test
os.Args = []string{os.Args[0], "-y"}
cmd := &Command{
Name: "external",
Flags: []string{"yes"},
Short: "An external subcommand",
Long: "Command external calls the cmd '<appname>-external'.",
Cmd: External(),
Path: "examples/test/start-external",
}
Add(cmd)
Convey("Ensure that the test flag exists", t, func() {
if err := Parse(); err != nil {
fmt.Println(err)
}
So(flag.Lookup("yes").Name, ShouldEqual, "yes")
})
Convey("Ensure that the external command is called successfully", t, func() {
So(cmd.Cmd(cmd), ShouldBeNil)
})
}