-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
clino_test.go
480 lines (454 loc) · 13.4 KB
/
clino_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
480
package clino
import (
"bytes"
"context"
"errors"
"flag"
"io/ioutil"
"os/exec"
"reflect"
"testing"
)
var update = flag.Bool("update", false, "update golden files")
func TestProgramNoArguments(t *testing.T) {
var buf bytes.Buffer
sc := &simpleCommand{}
p := Program{
Root: sc,
Output: &buf,
}
if err := p.Run(context.Background()); err != nil {
t.Errorf("wanted error to be nil, got %v instead", err)
}
if buf.Len() != 0 {
t.Errorf("got unexpected output, should be empty: %v", buf.String())
}
if !sc.ran {
t.Error("run function wasn't called")
}
if len(sc.args) != 0 {
t.Errorf("expected no arguments to be passed, got %v instead", sc.args)
}
}
func TestProgramNoArgumentsDoubleHyphenMinus(t *testing.T) {
var buf bytes.Buffer
sc := &simpleCommand{}
p := Program{
Root: sc,
Output: &buf,
}
// -- should be ignored and not passed up to the command.
if err := p.Run(context.Background(), "--"); err != nil {
t.Errorf("wanted error to be nil, got %v instead", err)
}
if buf.Len() != 0 {
t.Errorf("got unexpected output, should be empty: %v", buf.String())
}
if !sc.ran {
t.Error("run function wasn't called")
}
if len(sc.args) != 0 {
t.Errorf("expected no arguments to be passed, got %v instead", sc.args)
}
}
func TestProgramArguments(t *testing.T) {
var buf bytes.Buffer
sc := &simpleCommand{}
p := Program{
Root: sc,
Output: &buf,
}
// passing "dangerous" arguments (like, unparsed flags after "--" - which should be not parsed by the flag package)
if err := p.Run(context.Background(), "abc", "def", "123", "--", "help", "xyz", "-name", "Gopher", "-h"); err != nil {
t.Errorf("wanted error to be nil, got %v instead", err)
}
if buf.Len() != 0 {
t.Errorf("got unexpected output, should be empty: %v", buf.String())
}
if !sc.ran {
t.Error("run function wasn't called")
}
wantArgs := []string{"abc", "def", "123", "--", "help", "xyz", "-name", "Gopher", "-h"}
if n := len(wantArgs); len(sc.args) != n {
t.Errorf("expected %d arguments to be passed, got %v instead", n, sc.args)
}
if !reflect.DeepEqual(wantArgs, sc.args) {
t.Errorf("expected arguments %v, got %v instead", wantArgs, sc.args)
}
}
func TestProgramGopher(t *testing.T) {
var buf bytes.Buffer
sc := &simpleCommand{}
p := Program{
Root: sc,
Output: &buf,
}
if err := p.Run(context.Background(), "-name", "Gopher"); err != nil {
t.Errorf("wanted error to be nil, got %v instead", err)
}
if buf.Len() != 0 {
t.Errorf("got unexpected output, should be empty: %v", buf.String())
}
if !sc.ran {
t.Error("run function wasn't called")
}
if n := 0; len(sc.args) != n {
t.Errorf("expected %d arguments to be passed, got %v instead", n, sc.args)
}
if want := "Gopher"; sc.name != want {
t.Errorf("expected name flag to be %q, got %v instead", want, sc.name)
}
}
func TestProgramHelp(t *testing.T) {
testCases := []struct {
desc string
program Program
args []string
err string
golden string // be careful: golden files are reused below.
}{
{
desc: "simple program called with -h",
program: Program{Root: &simpleCommand{}},
args: []string{"-h"},
golden: "testdata/simple_help.golden",
},
{
desc: "simple program called with -h and global flags",
program: Program{
Root: &simpleCommand{},
GlobalFlags: func(flags *flag.FlagSet) {
var globalflag string
flags.StringVar(&globalflag, "globalflag", "none", "global flag")
},
},
args: []string{"-h"},
golden: "testdata/simple_help_global_flags.golden",
},
{
desc: "simple program with persistent flags called with -h and persistent flags",
program: Program{
Root: &persistentFlagsCommand{},
},
args: []string{"-h"},
golden: "testdata/simple_help_persistent_flags.golden",
},
{
desc: "simple program called with -help",
program: Program{Root: &simpleCommand{}},
args: []string{"-help"},
golden: "testdata/simple_help.golden",
},
{
desc: "simple program called with --help",
program: Program{Root: &simpleCommand{}},
args: []string{"--help"},
golden: "testdata/simple_help.golden",
},
{
desc: "simple program called with help",
program: Program{Root: &simpleCommand{}},
args: []string{"help"},
golden: "testdata/simple_help.golden",
},
{
desc: "simple program called with undefined flag",
program: Program{Root: &simpleCommand{}},
args: []string{"-undefined"},
err: "flag provided but not defined: -undefined",
},
{
desc: "multiple commands program",
program: Program{Root: &rootCommand{}},
golden: "testdata/root_help.golden",
},
{
desc: "multiple commands program called with help",
program: Program{Root: &rootCommand{}},
args: []string{"help"},
golden: "testdata/root_help.golden",
},
{
desc: "multiple commands program called with unimplemented command",
program: Program{Root: &rootCommand{}},
args: []string{"unimplemented"},
err: "command or topic 'unimplemented' is missing implementation",
},
{
desc: "multiple commands program called with help -h",
program: Program{Root: &rootCommand{}},
args: []string{"help", "-h"},
golden: "testdata/root_help.golden",
},
{
desc: "multiple commands program called with help -h command-not-found",
program: Program{Root: &rootCommand{}},
args: []string{"help", "command-not-found"},
err: "unknown command: 'app command-not-found'",
golden: "testdata/root_help.golden",
},
{
desc: "not-runnable command called with help",
program: Program{Root: &rootCommand{}},
args: []string{"help", "not-runnable"},
golden: "testdata/root_not_runnable_help.golden",
},
{
desc: "not-runnable command called with -h",
program: Program{Root: &rootCommand{}},
args: []string{"not-runnable", "-h"},
golden: "testdata/root_not_runnable_help.golden",
},
{
desc: "--help not-runnable command called",
program: Program{Root: &rootCommand{}},
args: []string{"help", "not-runnable"},
golden: "testdata/root_not_runnable_help.golden",
},
{
desc: "not-runnable command called with -help",
program: Program{Root: &rootCommand{}},
args: []string{"not-runnable", "-help"},
golden: "testdata/root_not_runnable_help.golden",
},
{
desc: "not-runnable command called with --help",
program: Program{Root: &rootCommand{}},
args: []string{"not-runnable", "--help"},
golden: "testdata/root_not_runnable_help.golden",
},
{
desc: "command with flags",
program: Program{Root: &rootCommandWithFlags{}},
golden: "testdata/commands_with_flags.golden",
args: []string{"-h"},
},
{
desc: "inner command with children -h",
program: Program{Root: &rootCommandWithFlags{}},
golden: "testdata/inner_commands_with_children.golden",
args: []string{"help", "inner"},
},
{
desc: "-help inner command with children",
program: Program{Root: &rootCommandWithFlags{}},
golden: "testdata/inner_commands_with_children.golden",
args: []string{"inner", "-help"},
},
{
desc: "inner command with children -help",
program: Program{Root: &rootCommandWithFlags{}},
golden: "testdata/inner_commands_with_children.golden",
args: []string{"inner", "-help"},
},
{
desc: "inner command with children -help and global flags",
program: Program{
Root: &rootCommandWithFlags{},
GlobalFlags: func(flags *flag.FlagSet) {
var globalflag string
flags.StringVar(&globalflag, "globalflag", "none", "global flag")
},
},
golden: "testdata/inner_commands_with_children_global_flags.golden",
args: []string{"inner", "-help"},
},
{
desc: "inner command with children -help and persistent flags",
program: Program{
Root: &rootCommandWithFlagsAndPersistentFlags{},
},
golden: "testdata/inner_commands_with_children_persistent_flags.golden",
args: []string{"inner", "-help"},
},
{
desc: "not runnable inner subcommand",
program: Program{Root: &rootCommandWithFlags{}},
golden: "testdata/inner_not_runnable_command.golden",
args: []string{"inner", "not-runnable"},
},
{
desc: "inner simple subcommand",
program: Program{Root: &rootCommandWithFlags{}},
err: "flag provided but not defined: -undefined",
args: []string{"inner", "simple", "-undefined"},
},
{
desc: "ignored command not found",
program: Program{Root: &rootCommandWithFlags{}},
args: []string{"help", "ignored-notfound"},
golden: "testdata/ignored_notfound_command.golden",
// 'unknown command' error message is not printed because the rootCommandWithFlags command is runnable,
// therefore this can be interpreted as its arguments.
},
{
desc: "command not found",
program: Program{Root: &rootCommand{}},
args: []string{"notfound", "x", "-v"},
err: "unknown command: 'app notfound'",
golden: "testdata/help_notfound_command.golden",
},
{
desc: "another command not found",
program: Program{Root: &anotherCommand{}},
args: []string{"not-runnable", "notfound", "x", "-v"},
err: "unknown command: 'app not-runnable notfound'",
golden: "testdata/help_notfound_another_command.golden",
},
{
desc: "help command not found",
program: Program{Root: &rootCommandWithFlags{}},
args: []string{"help", "inner", "notfound", "x", "-v"},
err: "unknown command: 'cmd inner notfound'",
golden: "testdata/inner_notfound_command.golden",
},
{
desc: "help command not found with global flags",
program: Program{
Root: &rootCommandWithFlags{},
GlobalFlags: func(flags *flag.FlagSet) {
var globalflag string
flags.StringVar(&globalflag, "globalflag", "none", "global flag")
},
},
args: []string{"help", "inner", "notfound", "x", "-v"},
err: "unknown command: 'cmd inner notfound'",
golden: "testdata/inner_notfound_command_global_flags.golden",
},
{
desc: "inner command not found -help",
program: Program{Root: &rootCommandWithFlags{}},
args: []string{"inner", "notfound", "-help", "x", "-v"},
err: "unknown command: 'cmd inner notfound'",
golden: "testdata/inner_notfound_command.golden",
},
{
desc: "inner command not found -help with global flags",
program: Program{
Root: &rootCommandWithFlags{},
GlobalFlags: func(flags *flag.FlagSet) {
var globalflag string
flags.StringVar(&globalflag, "globalflag", "none", "global flag")
},
},
args: []string{"inner", "notfound", "-help", "x", "-v"},
err: "unknown command: 'cmd inner notfound'",
golden: "testdata/inner_notfound_command_global_flags.golden",
},
{
desc: "inner command not found -help with global flags",
program: Program{
Root: &rootCommandWithFlagsAndPersistentFlags{},
},
args: []string{"inner", "notfound", "-help", "x", "-v"},
err: "unknown command: 'cmd inner notfound'",
golden: "testdata/inner_notfound_command_persistent_flags.golden",
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
var buf bytes.Buffer
tc.program.Output = &buf
err := tc.program.Run(context.Background(), tc.args...)
if (err == nil && tc.err != "") || (err != nil && err.Error() != tc.err) {
t.Errorf("wanted error to be %v, got %v instead", tc.err, err)
}
got := buf.String()
if tc.golden == "" {
if buf.Len() == 0 {
return
}
t.Errorf("got output %v\n, but found no golden file", got)
}
if *update {
if err = ioutil.WriteFile(tc.golden, buf.Bytes(), 0666); err != nil {
t.Fatal(err)
}
}
bs, err := ioutil.ReadFile(tc.golden)
if err != nil {
t.Fatalf("opening %s: %v", tc.golden, err)
}
if got != string(bs) {
t.Errorf("got output %v\n, wanted %v", got, string(bs))
}
})
}
}
func TestRunRootCommandNotImplemented(t *testing.T) {
want := "root command not implemented"
defer func() {
if r := recover(); r.(string) != want {
t.Errorf("expected panic message not found, got %v instead", r)
}
}()
var p Program
t.Fatal(p.Run(context.Background()))
}
func TestRunCommandImplementedMultipleTimes(t *testing.T) {
want := "command implemented multiple times: 'bad simple'"
defer func() {
if r := recover(); r.(string) != want {
t.Errorf("expected panic message not found, got %v instead", r)
}
}()
p := Program{
Root: &badRootCommand{},
}
t.Fatal(p.Run(context.Background()))
}
func TestExitCode(t *testing.T) {
testCases := []struct {
desc string
in error
want int
}{
{
desc: "no error",
in: nil,
want: 0,
},
{
desc: "exit error 2",
in: ExitError{
Code: 2,
Err: errors.New("cannot find system"),
},
want: 2,
},
{
desc: "exit default error code",
in: errors.New("cannot find error code"),
want: 1,
},
{
desc: "copy regular program error code",
// explanation: by default, Go binary exits with error code = 2.
in: func() error {
cmd := exec.Command("go")
return cmd.Run()
}(),
want: 2,
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
if got := ExitCode(tc.in); got != tc.want {
t.Errorf("wanted ExitCode(%v) = %v, got %v instead", tc.in, tc.want, got)
}
})
}
}
func TestExitError(t *testing.T) {
err := errors.New("this is the original error")
ee := ExitError{
Code: 3,
Err: err,
}
if ee.Unwrap() != err {
t.Errorf("expected unwrapped error to be %v", err)
}
if ee.Error() != err.Error() {
t.Error("expected wrapped error to print the same error message")
}
}