-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsubcommand_options_test.go
331 lines (274 loc) · 9.78 KB
/
subcommand_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
// 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 equalSubCommandOptions(sco1 SubCommandOptions, sco2 SubCommandOptions) (equal bool) {
if equalOptions(sco1.Global, sco2.Global) && len(sco1.SubCommands) == len(sco2.SubCommands) {
for key, options := range sco1.SubCommands {
if !equalOptions(options, sco2.SubCommands[key]) {
goto loopend
}
}
equal = true
}
loopend:
return
}
func equalOptions(options1 Options, options2 Options) (equal bool) {
if options1.Description == options2.Description && len(options1.Definitions) == len(options2.Definitions) {
for i := 0; i < len(options1.Definitions); i++ {
if options1.Definitions[i] != options2.Definitions[i] {
goto loopend
}
}
equal = true
}
loopend:
return
}
func TestSubCommandOptionsConverter(t *testing.T) {
sco := SubCommandOptions{
Options{
"global description",
Definitions{
{"command", "command to execute", IsSubCommand, ""},
},
},
SubCommands{
"getenv": {
"getenv description",
Definitions{
{"name", "app's name", IsArg | Required, ""},
{"key", "environment variable's name", IsArg | Required, ""},
},
},
"register": {
"register description",
Definitions{
{"name|n", "app's name", IsArg | Required, ""},
{"deploytype|t", "deploy type (one of mount, bazapta, lxc)", Optional | ExampleIsDefault, "lxc"},
},
},
},
}
expectedGetenvOptions := Options{
"getenv description",
Definitions{
{"command", "command to execute", IsSubCommand, ""},
{"name", "app's name", IsArg | Required, ""},
{"key", "environment variable's name", IsArg | Required, ""},
},
}
expectedRegisterOptions := Options{
"register description",
Definitions{
{"command", "command to execute", IsSubCommand, ""},
{"name|n", "app's name", IsArg | Required, ""},
{"deploytype|t", "deploy type (one of mount, bazapta, lxc)", Optional | ExampleIsDefault, "lxc"},
},
}
if _, err := sco.flattenToOptions("getenv"); err != nil {
t.Errorf("conversion SubCommandOptions -> Options failed (getenv); \nGot the following error: %s", err.Error())
}
if options, _ := sco.flattenToOptions("getenv"); equalOptions(options, expectedGetenvOptions) == false {
t.Errorf("conversion SubCommandOptions -> Options failed (getenv); \nGot\n\t#%#v#\nExpected:\n\t#%#v#\n", options, expectedGetenvOptions)
}
if _, err := sco.flattenToOptions("register"); err != nil {
t.Errorf("conversion SubCommandOptions -> Options failed (register); \nGot the following error: %s", err.Error())
}
if options, _ := sco.flattenToOptions("register"); equalOptions(options, expectedRegisterOptions) == false {
t.Errorf("conversion SubCommandOptions -> Options failed (register); \nGot\n\t#%#v#\nExpected:\n\t#%#v#\n", options, expectedGetenvOptions)
}
if _, err := sco.flattenToOptions("nonexistantsubcommand"); err.ErrorCode != UnknownSubCommand {
t.Errorf("non existant sub command didn't throw error")
}
}
func TestSubCommandOptionsSubCommandFinder(t *testing.T) {
sco := SubCommandOptions{
Options{
"global description",
Definitions{
{"command", "command to execute", IsSubCommand, ""},
{"foo|f", "some arg", Optional, ""},
},
},
SubCommands{
"getenv": {
"getenv description",
Definitions{
{"name", "app's name", IsArg | Required, ""},
{"key", "environment variable's name", IsArg | Required, ""},
},
},
},
}
os.Args = []string{"prog", "getenv"}
if command, _ := sco.findSubCommand(); command != "getenv" {
t.Errorf("did not correctly find subcommand getenv")
}
os.Args = []string{"prog", "-f", "bar", "getenv", "name", "key"}
if command, _ := sco.findSubCommand(); command != "getenv" {
t.Errorf("did not correctly find subcommand getenv")
}
os.Args = []string{"prog"}
if _, err := sco.findSubCommand(); err == nil || err.ErrorCode != NoSubCommand {
t.Errorf("did not throw error on unknown subcommand")
}
}
func TestSubCommandOptionsParser(t *testing.T) {
sco := SubCommandOptions{
Options{
"global description",
Definitions{
{"command", "command to execute", IsSubCommand, ""},
{"foo|f", "some arg", Optional, ""},
},
},
SubCommands{
"getenv": {
"getenv description",
Definitions{
{"bar|b", "some arg", Optional, ""},
{"name", "app's name", IsArg | Required, ""},
{"key", "environment variable's name", IsArg | Required, ""},
},
},
},
}
os.Args = []string{"prog", "-fbar", "getenv", "--bar=foo", "foo", "bar"}
scope, options, arguments, _, _ := sco.ParseCommandLine()
if scope != "getenv" {
t.Errorf("SubCommandOptions parsing: failed to correctly parse scope: Expected: getenv, Got: " + scope)
}
if options["foo"].String != "bar" {
t.Errorf("SubCommandOptions parsing: failed to correctly parse option: Expected: bar, Got: " + options["foo"].String)
}
if options["bar"].String != "foo" {
t.Errorf("SubCommandOptions parsing: failed to correctly parse option: Expected: foo, Got: " + options["foo"].String)
}
if scope != "getenv" {
t.Errorf("SubCommandOptions parsing: failed to correctly parse sub command: Expected: getenv, Got: " + scope)
}
if arguments[0] != "foo" {
t.Errorf("SubCommandOptions parsing: failed to correctly parse arg1: Expected: foo, Got: " + arguments[0])
}
if arguments[1] != "bar" {
t.Errorf("SubCommandOptions parsing: failed to correctly parse arg2: Expected: bar, Got: " + arguments[1])
}
os.Args = []string{"prog", "-h"}
_, parsedOptions, _, _, _ := sco.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, _, _, _ = sco.ParseCommandLine()
if helpOption, present := parsedOptions["help"]; !present || helpOption.String != "help" {
t.Errorf("Wants help for global command did not set help option correctly")
}
}
func TestErrorMessageForMissingArgs(t *testing.T) {
sco := SubCommandOptions{
Options{
"global description",
Definitions{
{"foo|f", "some arg", Optional, ""},
{"command", "command to execute", IsSubCommand, ""},
},
},
SubCommands{
"getenv": {
"getenv description",
Definitions{
{"bar|b", "some arg", Optional, ""},
{"name", "app's name", IsArg | Required, ""},
{"key", "environment variable's name", IsArg | Required, ""},
},
},
},
}
os.Args = []string{"prog", "getenv"}
_, _, _, _, err := sco.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 TestSubCommandHelp(t *testing.T) {
sco := SubCommandOptions{
Options{
"global description",
Definitions{
{"foo|f", "some arg", Optional, ""},
{"command", "command to execute", IsSubCommand, ""},
},
},
SubCommands{
"getenv": {
"getenv description",
Definitions{
{"bar|b", "some arg", Optional, ""},
{"name", "app's name", IsArg | Required, ""},
{"key", "environment variable's name", IsArg | Required, ""},
},
},
"register": {
"register description",
Definitions{
{"deploytype|t", "deploy type (one of mount, bazapta, lxc)", NoLongOpt | Optional | ExampleIsDefault, "lxc"},
{"name|n", "app's name", IsArg | Required, ""},
},
},
},
}
os.Args = []string{"prog"}
expectedHelp := `Usage: prog [-f <foo>] <command>
global description
Options:
-f, --foo=<foo> some arg
-h, --help usage (-h) / detailed help text (--help)
Available commands:
getenv getenv description
register register description
`
expectedUsage := `Usage: prog [-f <foo>] <command>
`
if got := sco.Help(); got != expectedHelp {
t.Errorf("Usage output not as expected:\ngot: |" + strings.Replace(got, " ", "_", -1) + "|\nexpected: |" + strings.Replace(expectedHelp, " ", "_", -1) + "|\n")
}
if got := sco.Usage(); got != expectedUsage {
t.Errorf("Usage output not as expected:\ngot: |" + strings.Replace(got, " ", "_", -1) + "|\nexpected: |" + strings.Replace(expectedUsage, " ", "_", -1) + "|\n")
}
os.Args = []string{"prog", "register"}
expectedHelp = `Usage: prog register [-t <deploytype>] <name>
register description
Options:
-t <deploytype> deploy type (one of mount, bazapta, lxc) (default: lxc)
-h, --help usage (-h) / detailed help text (--help)
Arguments:
<name> app's name
`
expectedUsage = `Usage: prog register [-t <deploytype>] <name>
`
if got := sco.Help(); got != expectedHelp {
t.Errorf("Usage output not as expected:\ngot: |" + strings.Replace(got, " ", "_", -1) + "|\nexpected: |" + strings.Replace(expectedHelp, " ", "_", -1) + "|\n")
}
if got := sco.Usage(); got != expectedUsage {
t.Errorf("Usage output not as expected:\ngot: |" + strings.Replace(got, " ", "_", -1) + "|\nexpected: |" + strings.Replace(expectedUsage, " ", "_", -1) + "|\n")
}
os.Args = []string{"prog", "register", "--help"}
if got := sco.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", "register", "-h"}
if got := sco.Usage(); got != expectedUsage {
t.Errorf("Usage output not as expected:\ngot: |" + strings.Replace(got, " ", "_", -1) + "|\nexpected: |" + strings.Replace(expectedUsage, " ", "_", -1) + "|\n")
}
}