-
Notifications
You must be signed in to change notification settings - Fork 16
/
commando_test.go
375 lines (313 loc) · 10.5 KB
/
commando_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
package commando
import (
"fmt"
"os"
"os/exec"
"strings"
"testing"
)
/*----------------*/
// executable name should not be empty
func TestEmptyExecutableName(t *testing.T) {
// command
cmd := exec.Command("go", "run", "tests/empty-exec-name.go")
// get output
if output, err := cmd.Output(); err != nil {
fmt.Println("Error:", err)
} else {
if !strings.Contains(fmt.Sprintf("%s", output), "Error: executable name must be a non-empty string.") {
t.Fail()
}
}
}
// a sub-command must have an action function
func TestMissingActionFunction(t *testing.T) {
// command
cmdRoot := exec.Command("go", "run", "tests/missing-action-function.go")
// get output
if output, err := cmdRoot.Output(); err != nil {
fmt.Println("Error:", err)
} else {
if fmt.Sprintf("%s", output) != "" {
t.Fail()
}
}
/*---------------*/
// command
cmdCreate := exec.Command("go", "run", "tests/missing-action-function.go", "create")
// get output
if output, err := cmdCreate.Output(); err != nil {
fmt.Println("Error:", err)
} else {
if !strings.Contains(fmt.Sprintf("%s", output), "Error: action function for the create command is not registered.") {
t.Fail()
}
}
}
// default value of a flag must match the data type
func TestInvalidDefaultValue(t *testing.T) {
// command
cmdCreate := exec.Command("go", "run", "tests/invalid-default-value.go", "create")
// get output
if output, err := cmdCreate.Output(); err != nil {
fmt.Println("Error:", err)
} else {
if !strings.Contains(fmt.Sprintf("%s", output), "Error: value of the --dir flag must be a string or nil.") {
t.Fail()
}
}
}
/*----------------*/
// unknown command show display an error
func TestUnknownCommand(t *testing.T) {
// command
cmdPrint := exec.Command("go", "run", "tests/valid-registry.go", "print")
cmdPrint.Env = append(os.Environ(), "NO_ROOT=TRUE")
// get output
if output, err := cmdPrint.Output(); err != nil {
fmt.Println("Error:", err)
} else {
if !strings.Contains(fmt.Sprintf("%s", output), "Error: print is not a valid command.") {
t.Fail()
}
}
}
// unsupported flag must display an error
func TestUnsupportedFlag(t *testing.T) {
unsupportedFlags := []string{"---version", "-version"}
for _, flag := range unsupportedFlags {
// command
cmdRoot := exec.Command("go", "run", "tests/valid-registry.go", flag)
// get output
if output, err := cmdRoot.Output(); err != nil {
fmt.Println("Error:", err)
} else {
if !strings.Contains(fmt.Sprintf("%s", output), fmt.Sprintf("Error: %s is not a supported flag.", flag)) {
t.Fail()
}
}
}
}
// missing argument value of a required argument must display an error
func TestMissingArgument(t *testing.T) {
// command
cmdRoot := exec.Command("go", "run", "tests/valid-registry.go")
// get output
if output, err := cmdRoot.Output(); err != nil {
fmt.Println("Error:", err)
} else {
if !strings.Contains(fmt.Sprintf("%s", output), "Error: value of the category argument can not be empty.") {
t.Fail()
}
}
/*----------------*/
// command
cmdCreate := exec.Command("go", "run", "tests/valid-registry.go", "create")
// get output
if output, err := cmdCreate.Output(); err != nil {
fmt.Println("Error:", err)
} else {
if !strings.Contains(fmt.Sprintf("%s", output), "Error: value of the name argument can not be empty.") {
t.Fail()
}
}
}
// missing flag value of a required flag must display an error
func TestMissingFlag(t *testing.T) {
// command
cmdCreate := exec.Command("go", "run", "tests/valid-registry.go", "create", "my-service")
// get output
if output, err := cmdCreate.Output(); err != nil {
fmt.Println("Error:", err)
} else {
if !strings.Contains(fmt.Sprintf("%s", output), "Error: value of the --dir flag can not be empty.") {
t.Fail()
}
}
}
// wrong value of a flag must display an error
func TestInvalidFlagValue(t *testing.T) {
// command
cmdCreate := exec.Command("go", "run", "tests/valid-registry.go", "create", "my-service", "-d", "./services/my-service", "--timeout", "10sec")
// get output
if output, err := cmdCreate.Output(); err != nil {
fmt.Println("Error:", err)
} else {
if !strings.Contains(fmt.Sprintf("%s", output), "Error: value of the --timeout flag must be an integer.") {
t.Fail()
}
}
}
// test if all values of a root-command are valid
func TestValidRootCommand(t *testing.T) {
// command
cmdCreate := exec.Command("go", "run", "tests/valid-registry.go", "service")
// get output
if output, err := cmdCreate.Output(); err != nil {
fmt.Println("Error:", err)
} else {
values := []string{
"arg -> category: service(string)",
"flag -> verbose: false(bool)",
"flag -> version: false(bool)",
"flag -> help: false(bool)",
}
for _, value := range values {
if !strings.Contains(fmt.Sprintf("%s", output), value) {
t.Fail()
}
}
}
}
// test if default value of an argument is correct
func TestDefaultArgValue(t *testing.T) {
// command
cmdCreate := exec.Command("go", "run", "tests/valid-registry.go", "create", "my-service", "-t", "service", "--dir=./service/my-service", "--timeout", "10", "-v")
// get output
if output, err := cmdCreate.Output(); err != nil {
fmt.Println("Error:", err)
} else {
if !strings.Contains(fmt.Sprintf("%s", output), "arg -> version: 1.0.0(string)") {
t.Fail()
}
}
}
// test if all values of a sub-command are valid
func TestValidSubCommand(t *testing.T) {
// command
cmdCreate := exec.Command("go", "run", "tests/valid-registry.go", "create", "my-service", "1.0.0", "file1.txt", "file2.txt", "-t", "service", "--dir=./service/my-service", "--timeout", "10", "-v", "--no-clean")
// get output
if output, err := cmdCreate.Output(); err != nil {
fmt.Println("Error:", err)
} else {
values := []string{
"arg -> version: 1.0.0(string)",
"arg -> name: my-service(string)",
"arg -> files: file1.txt,file2.txt(string)",
"flag -> dir: ./service/my-service(string)",
"flag -> type: service(string)",
"flag -> timeout: 10(int)",
"flag -> verbose: true(bool)",
"flag -> help: false(bool)",
"flag -> clean: false(bool)",
}
for _, value := range values {
if !strings.Contains(fmt.Sprintf("%s", output), value) {
t.Fail()
}
}
}
}
// test if version is displayed properly
func TestValidVersion(t *testing.T) {
versionTriggers := []string{"-v", "--version", "version"}
for _, versionTrigger := range versionTriggers {
// command
cmdCreate := exec.Command("go", "run", "tests/valid-registry.go", versionTrigger)
// get output
if output, err := cmdCreate.Output(); err != nil {
fmt.Println("Error:", err)
} else {
if !strings.Contains(fmt.Sprintf("%s", output), "Version: v1.0.0") {
t.Fail()
}
}
}
}
// test if usage of the root-command is displayed properly
func TestValidRootCommandUsage(t *testing.T) {
helpTriggers := []string{"-h", "--help", "help"}
for _, helpTrigger := range helpTriggers {
// command
cmdCreate := exec.Command("go", "run", "tests/valid-registry.go", helpTrigger)
// get output
if output, err := cmdCreate.Output(); err != nil {
fmt.Println("Error:", err)
} else {
values := []string{
"Reactor is a command-line tool to generate React projects.",
"It helps you create components, write test cases, start a development server and much more.",
"Usage:",
"reactor <category> {flags}",
"reactor <command> {flags}",
"Commands: ",
"build creates build artifacts",
"create creates a component",
"help displays usage information",
"serve starts a development server",
"version displays version number",
"Arguments: ",
"category category of the information to look for",
"Flags: ",
"-h, --help displays usage information of the application or a command (default: false)",
"-V, --verbose display log information (default: false)",
"-v, --version displays version number (default: false)",
}
for _, value := range values {
if !strings.Contains(fmt.Sprintf("%s", output), value) {
t.Fail()
}
}
}
}
}
// test if usage of the sub-command is displayed properly
func TestValidSubCommandUsage(t *testing.T) {
helpTriggers := []string{"-h", "--help"}
for _, helpTrigger := range helpTriggers {
// command
cmdCreate := exec.Command("go", "run", "tests/valid-registry.go", "create", helpTrigger)
// get output
if output, err := cmdCreate.Output(); err != nil {
fmt.Println("Error:", err)
} else {
values := []string{
"This command creates a component of a given type and outputs component files in the project directory.",
"Usage:",
"reactor <name> [version] [files] {flags}",
"Arguments: ",
"name name of the component to create",
"version version of the component (default: 1.0.0)",
"files files to remove once component is created {variadic}",
"Flags: ",
"-d, --dir output directory for the component files",
"-h, --help displays usage information of the application or a command",
"--timeout operation timeout in seconds (default: 60)",
"-t, --type type of the component to create (default: simple_type)",
"-v, --verbose display logs while creating the component files (default: false)",
"--no-clean avoid cleanup of the component directory (default: false)",
}
for _, value := range values {
if !strings.Contains(fmt.Sprintf("%s", output), value) {
t.Fail()
}
}
}
}
}
// test if version and help events are working properly
func TestEvents(t *testing.T) {
// test `version` event
cmdRootVersion := exec.Command("go", "run", "tests/valid-registry.go", "--version")
cmdRootVersion.Env = append(os.Environ(), "LISTEN_EVENTS=TRUE")
// get output
if output, err := cmdRootVersion.Output(); err != nil {
fmt.Println("Error:", err)
} else {
if !strings.Contains(fmt.Sprintf("%s", output), fmt.Sprintf("event-name: %s", EventVersion)) {
t.Fail()
}
}
/*--------------*/
// test `help` event
cmdRootHelp := exec.Command("go", "run", "tests/valid-registry.go", "--help")
cmdRootHelp.Env = append(os.Environ(), "LISTEN_EVENTS=TRUE")
// get output
if output, err := cmdRootHelp.Output(); err != nil {
fmt.Println("Error:", err)
} else {
if !strings.Contains(fmt.Sprintf("%s", output), fmt.Sprintf("event-name: %s", EventHelp)) {
t.Fail()
}
}
}