-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsubsubcommand_options_test.go
515 lines (410 loc) · 16.1 KB
/
subsubcommand_options_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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
// Copyright (c) 2011, SoundCloud Ltd., Daniel Bornkessel
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Source code and contact info at http://github.com/kesselborn/go-getopt
package getopt
import (
"os"
"strings"
"testing"
)
func testingSubSubDefinitions() (ssco SubSubCommandOptions) {
ssco = SubSubCommandOptions{
Options{
"global description",
Definitions{
{"config|c", "config file", IsConfigFile | ExampleIsDefault, "/etc/visor.conf"},
{"server|s", "doozer server", Optional, ""},
{"scope", "scope", IsSubCommand, ""},
},
},
Scopes{
"app": {
Options{
"app description",
Definitions{
{"foo|f", "a param", Optional, ""},
{"command", "command to execute", IsSubCommand, ""},
},
},
SubCommands{
"getenv": {
"app getenv description",
Definitions{
{"key", "environment variable's name", IsArg | Required, ""},
},
},
"setenv": {
"app setenv description",
Definitions{
{"persist|p", "persist this", Optional | Flag | NoLongOpt, ""},
{"alias|a", "alias name", Optional, ""},
{"name", "app name", IsArg | Required, ""},
{"key", "env key", IsArg | Required, ""},
{"value", "env value", IsArg | Optional, ""},
},
},
},
},
"revision": {
Options{
"app revision description",
Definitions{
{"rev|r", "revision", IsArg | Required, ""},
{"command", "command to execute", IsSubCommand, ""},
},
},
SubCommands{
"list": {
"list revisions",
Definitions{
{"all|a", "long list output", Flag, ""},
},
},
},
},
},
}
return
}
func TestExampleIsDefaultError(t *testing.T) {
ssco := SubSubCommandOptions{
Options{
"global description",
Definitions{
{"server|s", "doozer server", Required | ExampleIsDefault, ""},
{"scope", "scope", IsSubCommand, ""},
},
},
Scopes{
"app": {
Options{
"app description",
Definitions{
{"foo|f", "a param", Optional, ""},
{"command", "command to execute", IsSubCommand, ""},
},
},
SubCommands{
"getenv": {
"app getenv description",
Definitions{
{"key", "environment variable's name", IsArg | Required, ""},
},
},
},
},
},
}
os.Args = []string{"prog", "app", "getenv", "foo"}
_, _, _, _, _, err := ssco.ParseCommandLine()
if err != nil {
t.Errorf("got error where no error was: %#v", err)
}
os.Args = []string{"prog", "nonexistantscope"}
_, _, _, _, _, err = ssco.ParseCommandLine()
if err.ErrorCode != UnknownScope {
t.Errorf("wrong scope was detected incorrectly, got error: %d: %s", err.ErrorCode, err.Error())
}
}
func TestSubSubCommandOptionsConverter(t *testing.T) {
ssco := testingSubSubDefinitions()
expectedAppSubOptions := SubCommandOptions{
Options{
"app description",
Definitions{
{"config|c", "config file", IsConfigFile | ExampleIsDefault, "/etc/visor.conf"},
{"server|s", "doozer server", Optional, ""},
{"scope", "scope", IsSubCommand, ""},
{"foo|f", "a param", Optional, ""},
{"command", "command to execute", IsSubCommand, ""},
},
},
SubCommands{
"getenv": {
"app getenv description",
Definitions{
{"key", "environment variable's name", IsArg | Required, ""},
},
},
"setenv": {
"app setenv description",
Definitions{
{"persist|p", "persist this", Optional | Flag | NoLongOpt, ""},
{"alias|a", "alias name", Optional, ""},
{"name", "app name", IsArg | Required, ""},
{"key", "env key", IsArg | Required, ""},
{"value", "env value", IsArg | Optional, ""},
},
},
},
}
expectedRevisionOptions := SubCommandOptions{
Options{
"app revision description",
Definitions{
{"config|c", "config file", IsConfigFile | ExampleIsDefault, "/etc/visor.conf"},
{"server|s", "doozer server", Optional, ""},
{"scope", "scope", IsSubCommand, ""},
{"rev|r", "revision", IsArg | Required, ""},
{"command", "command to execute", IsSubCommand, ""},
},
},
SubCommands{
"list": {
"list revisions",
Definitions{
{"all|a", "long list output", Flag, ""},
},
},
},
}
if _, err := ssco.flattenToSubCommandOptions("app"); err != nil {
t.Errorf("conversion SuSubCommandOptions -> SubCommandOptions failed (app); \nGot the following error: %s", err.Error())
}
if sco, _ := ssco.flattenToSubCommandOptions("app"); equalSubCommandOptions(sco, expectedAppSubOptions) == false {
t.Errorf("conversion SubSubCommandOptions -> SubCommandOptions failed (app); \nGot\n\t#%#v#\nExpected:\n\t#%#v#\n", sco, expectedAppSubOptions)
}
if _, err := ssco.flattenToSubCommandOptions("revision"); err != nil {
t.Errorf("conversion SuSubCommandOptions -> SubCommandOptions failed (revision); \nGot the following error: %s", err.Error())
}
if sco, _ := ssco.flattenToSubCommandOptions("revision"); equalSubCommandOptions(sco, expectedRevisionOptions) == false {
t.Errorf("conversion SubSubCommandOptions -> SubCommandOptions failed (revision); \nGot\n\t#%#v#\nExpected:\n\t#%#v#\n", sco, expectedRevisionOptions)
}
if _, err := ssco.flattenToSubCommandOptions("nonexistantsubcommand"); err.ErrorCode != UnknownSubCommand {
t.Errorf("non existant sub command didn't throw error")
}
expectedAppGetEnvOptions := Options{
"app getenv description",
Definitions{
{"config|c", "config file", IsConfigFile | ExampleIsDefault, "/etc/visor.conf"},
{"server|s", "doozer server", Optional, ""},
{"scope", "scope", IsSubCommand, ""},
{"foo|f", "a param", Optional, ""},
{"command", "command to execute", IsSubCommand, ""},
{"key", "environment variable's name", IsArg | Required, ""},
},
}
if _, err := ssco.flattenToOptions("app", "getenv"); err != nil {
t.Errorf("conversion SubSubCommandOptions -> Options failed (app/getenv); \nGot the following error: %s", err.Error())
}
if options, _ := ssco.flattenToOptions("app", "getenv"); equalOptions(options, expectedAppGetEnvOptions) == false {
t.Errorf("conversion SubCommandOptions -> Options failed (app/getenv); \nGot\n\t#%#v#\nExpected:\n\t#%#v#\n", options, expectedAppGetEnvOptions)
}
}
func TestSubSubCommandScopeFinder(t *testing.T) {
ssco := testingSubSubDefinitions()
os.Args = []string{"prog", "app"}
if command, _ := ssco.findScope(); command != "app" {
t.Errorf("did not correctly find subcommand app")
}
os.Args = []string{"prog", "-s", "10.20.30.40", "app", "getenv", "key"}
if command, _ := ssco.findScope(); command != "app" {
t.Errorf("did not correctly find subcommand app (w/ other options)")
}
os.Args = []string{"prog"}
if _, err := ssco.findScope(); err == nil || err.ErrorCode != NoScope {
t.Errorf("did not throw error on unknown subcommand")
}
}
func TestSubSubCommandSubCommand(t *testing.T) {
ssco := testingSubSubDefinitions()
os.Args = []string{"prog", "app", "getenv"}
if scope, command, _ := ssco.findScopeAndSubCommand(); scope != "app" || command != "getenv" {
t.Errorf("did not correctly find subcommand app / getenv (got: " + scope + " / " + command + ")")
}
os.Args = []string{"prog", "-s", "10.20.30.40", "app", "-ffoo", "getenv", "key"}
if _, _, err := ssco.findScopeAndSubCommand(); err != nil {
t.Errorf("did not correctly find subcommand app / getenv; Error message: " + err.Error())
}
if scope, command, _ := ssco.findScopeAndSubCommand(); scope != "app" || command != "getenv" {
t.Errorf("did not correctly find subcommand app / getenv (got: " + scope + " / " + command + ")")
}
os.Args = []string{"prog"}
if _, _, err := ssco.findScopeAndSubCommand(); err == nil || err.ErrorCode != NoScope {
t.Errorf("did not throw error on missing scope")
}
os.Args = []string{"prog", "app"}
if _, _, err := ssco.findScopeAndSubCommand(); err == nil || err.ErrorCode != NoSubCommand {
t.Errorf("did not throw error on missing subcommand")
}
os.Args = []string{"prog", "unknownscope"}
if _, _, err := ssco.findScopeAndSubCommand(); err == nil || err.ErrorCode != UnknownScope {
t.Errorf("did not throw error on unknown scope")
}
os.Args = []string{"prog", "app", "unknowncommand"}
if _, _, err := ssco.findScopeAndSubCommand(); err == nil || err.ErrorCode != UnknownSubCommand {
t.Errorf("did not throw error on unknown subcommand")
}
}
func TestSubSubCommandOptionsParser(t *testing.T) {
ssco := testingSubSubDefinitions()
os.Args = []string{"prog", "-sfoo.com", "app", "-ffoo", "setenv", "-p", "-aFOO", "name", "val", "--", "pass", "through"}
scope, command, options, arguments, passThrough, err := ssco.ParseCommandLine()
if err != nil {
t.Errorf("Got an unexpected error while parsing SubSubCommandOptions: " + err.Error())
}
if scope != "app" {
t.Errorf("SubSubCommandOptions parsing: failed to correctly parse scope: Expected: app, Got: " + scope)
}
if command != "setenv" {
t.Errorf("SubSubCommandOptions parsing: failed to correctly parse command: Expected: setenv, Got: " + scope)
}
if options["server"].String != "foo.com" {
t.Errorf("SubSubCommandOptions parsing: failed to correctly parse option: Expected: foo.com, Got: " + options["server"].String)
}
if options["alias"].String != "FOO" {
t.Errorf("SubSubCommandOptions parsing: failed to correctly parse option: Expected: FOO, Got: " + options["foo"].String)
}
if arguments[0] != "name" {
t.Errorf("SubSubCommandOptions parsing: failed to correctly parse arg1: Expected: name, Got: " + arguments[0])
}
if arguments[1] != "val" {
t.Errorf("SubSubCommandOptions parsing: failed to correctly parse arg2: Expected: val, Got: " + arguments[1])
}
if passThrough[0] != "pass" {
t.Errorf("SubSubCommandOptions parsing: failed to correctly parse pass through[0]: Expected: pass, Got: " + passThrough[0])
}
if passThrough[1] != "through" {
t.Errorf("SubSubCommandOptions parsing: failed to correctly parse pass through[0]: Expected: through, Got: " + passThrough[0])
}
}
func TestErrorMessages(t *testing.T) {
ssco := testingSubSubDefinitions()
os.Args = []string{"prog"}
_, _, _, _, _, err := ssco.ParseCommandLine()
if err == nil {
t.Errorf("NoScope error not triggered on missing")
}
if err.ErrorCode != NoScope {
t.Errorf("NoScope error not triggered on missing")
}
os.Args = []string{"prog", "app"}
_, _, _, _, _, err = ssco.ParseCommandLine()
if err == nil {
t.Errorf("NoCommand error not triggered on missing")
}
if err.ErrorCode != NoSubCommand {
t.Errorf("NoCommand error not triggered on missing")
}
os.Args = []string{"prog", "--help"}
_, _, parsedOptions, _, _, _ := ssco.ParseCommandLine()
if helpOption, present := parsedOptions["help"]; !present || helpOption.String != "help" {
t.Errorf("Wants help for global command set help option")
}
os.Args = []string{"prog", "app", "-h"}
_, _, parsedOptions, _, _, _ = ssco.ParseCommandLine()
if helpOption, present := parsedOptions["help"]; !present || helpOption.String != "usage" {
t.Errorf("Wants usage for global command did set usage option correctly")
}
os.Args = []string{"prog", "app", "--help"}
_, _, parsedOptions, _, _, _ = ssco.ParseCommandLine()
if helpOption, present := parsedOptions["help"]; !present || helpOption.String != "help" {
t.Errorf("Wants help for global command did not set help option correctly")
}
}
func TestWantsHelpAndUsage(t *testing.T) {
ssco := testingSubSubDefinitions()
os.Args = []string{"prog", "-h"}
_, _, parsedOptions, _, _, _ := ssco.ParseCommandLine()
if helpOption, present := parsedOptions["help"]; !present || helpOption.String != "usage" {
t.Errorf("Wants usage for global command did not set usage option correctly")
}
os.Args = []string{"prog", "--help"}
_, _, parsedOptions, _, _, _ = ssco.ParseCommandLine()
if helpOption, present := parsedOptions["help"]; !present || helpOption.String != "help" {
t.Errorf("Wants help for global command did not help option correctly")
}
os.Args = []string{"prog", "app", "-h"}
_, _, parsedOptions, _, _, _ = ssco.ParseCommandLine()
if helpOption, present := parsedOptions["help"]; !present || helpOption.String != "usage" {
t.Errorf("Wants usage for global command did not set usage option correclty")
}
os.Args = []string{"prog", "app", "--help"}
_, _, parsedOptions, _, _, _ = ssco.ParseCommandLine()
if helpOption, present := parsedOptions["help"]; !present || helpOption.String != "help" {
t.Errorf("Wants help for global command did not set help option correctly")
}
}
func TestErrorMessageForMissingArgsInSsco(t *testing.T) {
ssco := testingSubSubDefinitions()
os.Args = []string{"prog", "-sfoo.com", "app", "-ffoo", "setenv"}
_, _, _, _, _, err := ssco.ParseCommandLine()
if err == nil {
t.Errorf("missing arg did not raise error")
}
if expected := "Missing required argument <name>"; err.Error() != expected {
t.Errorf("Error handling for missing arguments is messed up:\n\tGot : " + err.Error() + "\n\tExpected: " + expected)
}
}
func TestSubSubCommandHelpForGlobalCommand(t *testing.T) {
ssco := testingSubSubDefinitions()
os.Args = []string{"prog"}
expectedUsage := `Usage: prog [-c <config>] [-s <server>] <scope>
`
expectedHelp := expectedUsage + `global description
Options:
-c, --config=<config> config file (default: /etc/visor.conf)
-s, --server=<server> doozer server
-h, --help usage (-h) / detailed help text (--help)
Available scopes:
app app description
revision app revision description
`
if got := ssco.Usage(); got != expectedUsage {
t.Errorf("Usage output not as expected:\ngot: |" + strings.Replace(got, " ", "_", -1) + "|\nexpected: |" + strings.Replace(expectedUsage, " ", "_", -1) + "|\n")
}
if got := ssco.Help(); got != expectedHelp {
t.Errorf("Usage output not as expected:\ngot: |" + strings.Replace(got, " ", "_", -1) + "|\nexpected: |" + strings.Replace(expectedHelp, " ", "_", -1) + "|\n")
}
}
func TestSubSubCommandHelpForSubCommand(t *testing.T) {
ssco := testingSubSubDefinitions()
os.Args = []string{"prog", "app"}
expectedUsage := `Usage: prog app [-f <foo>] <command>
`
expectedHelp := expectedUsage + `app description
Options:
-f, --foo=<foo> a param
-h, --help usage (-h) / detailed help text (--help)
Available commands:
getenv app getenv description
setenv app setenv description
`
if got := ssco.Help(); got != expectedHelp {
t.Errorf("Usage output not as expected:\ngot: |" + strings.Replace(got, " ", "_", -1) + "|\nexpected: |" + strings.Replace(expectedHelp, " ", "_", -1) + "|\n")
}
if got := ssco.Usage(); got != expectedUsage {
t.Errorf("Usage output not as expected:\ngot: |" + strings.Replace(got, " ", "_", -1) + "|\nexpected: |" + strings.Replace(expectedHelp, " ", "_", -1) + "|\n")
}
os.Args = []string{"prog", "app", "--help"}
if got := ssco.Help(); got != expectedHelp {
t.Errorf("Usage output not as expected:\ngot: |" + strings.Replace(got, " ", "_", -1) + "|\nexpected: |" + strings.Replace(expectedHelp, " ", "_", -1) + "|\n")
}
os.Args = []string{"prog", "app", "-h"}
if got := ssco.Usage(); got != expectedUsage {
t.Errorf("Usage output not as expected:\ngot: |" + strings.Replace(got, " ", "_", -1) + "|\nexpected: |" + strings.Replace(expectedHelp, " ", "_", -1) + "|\n")
}
}
func TestSubSubCommandHelpForSubSubCommand(t *testing.T) {
ssco := testingSubSubDefinitions()
os.Args = []string{"prog", "app", "setenv"}
expectedUsage := `Usage: prog app setenv [-p] [-a <alias>] <name> <key> [<value>]
`
expectedHelp := expectedUsage + `app setenv description
Options:
-p persist this
-a, --alias=<alias> alias name
-h, --help usage (-h) / detailed help text (--help)
Arguments:
<name> app name
<key> env key
<value> env value
`
if got := ssco.Help(); got != expectedHelp {
t.Errorf("Usage output not as expected:\ngot: |" + strings.Replace(got, " ", "_", -1) + "|\nexpected: |" + strings.Replace(expectedHelp, " ", "_", -1) + "|\n")
}
if got := ssco.Usage(); got != expectedUsage {
t.Errorf("Usage output not as expected:\ngot: |" + strings.Replace(got, " ", "_", -1) + "|\nexpected: |" + strings.Replace(expectedHelp, " ", "_", -1) + "|\n")
}
}