forked from juju/cmd
/
supercommand_test.go
346 lines (299 loc) · 11.1 KB
/
supercommand_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
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package cmd_test
import (
"bytes"
"fmt"
"strings"
gitjujutesting "github.com/juju/testing"
"launchpad.net/gnuflag"
gc "launchpad.net/gocheck"
"github.com/juju/cmd"
"github.com/juju/cmd/cmdtesting"
)
func initDefenestrate(args []string) (*cmd.SuperCommand, *TestCommand, error) {
jc := cmd.NewSuperCommand(cmd.SuperCommandParams{Name: "jujutest"})
tc := &TestCommand{Name: "defenestrate"}
jc.Register(tc)
return jc, tc, cmdtesting.InitCommand(jc, args)
}
type SuperCommandSuite struct {
gitjujutesting.IsolationSuite
}
var _ = gc.Suite(&SuperCommandSuite{})
const helpText = "\n help\\s+- show help on a command or other topic"
const helpCommandsText = "commands:" + helpText
func (s *SuperCommandSuite) TestDispatch(c *gc.C) {
jc := cmd.NewSuperCommand(cmd.SuperCommandParams{Name: "jujutest"})
info := jc.Info()
c.Assert(info.Name, gc.Equals, "jujutest")
c.Assert(info.Args, gc.Equals, "<command> ...")
c.Assert(info.Doc, gc.Matches, helpCommandsText)
jc, _, err := initDefenestrate([]string{"discombobulate"})
c.Assert(err, gc.ErrorMatches, "unrecognized command: jujutest discombobulate")
info = jc.Info()
c.Assert(info.Name, gc.Equals, "jujutest")
c.Assert(info.Args, gc.Equals, "<command> ...")
c.Assert(info.Doc, gc.Matches, "commands:\n defenestrate - defenestrate the juju"+helpText)
jc, tc, err := initDefenestrate([]string{"defenestrate"})
c.Assert(err, gc.IsNil)
c.Assert(tc.Option, gc.Equals, "")
info = jc.Info()
c.Assert(info.Name, gc.Equals, "jujutest defenestrate")
c.Assert(info.Args, gc.Equals, "<something>")
c.Assert(info.Doc, gc.Equals, "defenestrate-doc")
_, tc, err = initDefenestrate([]string{"defenestrate", "--option", "firmly"})
c.Assert(err, gc.IsNil)
c.Assert(tc.Option, gc.Equals, "firmly")
_, tc, err = initDefenestrate([]string{"defenestrate", "gibberish"})
c.Assert(err, gc.ErrorMatches, `unrecognized args: \["gibberish"\]`)
// --description must be used on it's own.
_, _, err = initDefenestrate([]string{"--description", "defenestrate"})
c.Assert(err, gc.ErrorMatches, `unrecognized args: \["defenestrate"\]`)
}
func (s *SuperCommandSuite) TestRegister(c *gc.C) {
jc := cmd.NewSuperCommand(cmd.SuperCommandParams{Name: "jujutest"})
jc.Register(&TestCommand{Name: "flip"})
jc.Register(&TestCommand{Name: "flap"})
badCall := func() { jc.Register(&TestCommand{Name: "flap"}) }
c.Assert(badCall, gc.PanicMatches, "command already registered: flap")
}
func (s *SuperCommandSuite) TestRegisterAlias(c *gc.C) {
jc := cmd.NewSuperCommand(cmd.SuperCommandParams{Name: "jujutest"})
jc.Register(&TestCommand{Name: "flip", Aliases: []string{"flap", "flop"}})
info := jc.Info()
c.Assert(info.Doc, gc.Equals, `commands:
flap - alias for flip
flip - flip the juju
flop - alias for flip
help - show help on a command or other topic`)
}
func (s *SuperCommandSuite) TestInfo(c *gc.C) {
commandsDoc := `commands:
flapbabble - flapbabble the juju
flip - flip the juju`
jc := cmd.NewSuperCommand(cmd.SuperCommandParams{
Name: "jujutest",
Purpose: "to be purposeful",
Doc: "doc\nblah\ndoc",
})
info := jc.Info()
c.Assert(info.Name, gc.Equals, "jujutest")
c.Assert(info.Purpose, gc.Equals, "to be purposeful")
// info doc starts with the jc.Doc and ends with the help command
c.Assert(info.Doc, gc.Matches, jc.Doc+"(.|\n)*")
c.Assert(info.Doc, gc.Matches, "(.|\n)*"+helpCommandsText)
jc.Register(&TestCommand{Name: "flip"})
jc.Register(&TestCommand{Name: "flapbabble"})
info = jc.Info()
c.Assert(info.Doc, gc.Matches, jc.Doc+"\n\n"+commandsDoc+helpText)
jc.Doc = ""
info = jc.Info()
c.Assert(info.Doc, gc.Matches, commandsDoc+helpText)
}
type testVersionFlagCommand struct {
cmd.CommandBase
version string
}
func (c *testVersionFlagCommand) Info() *cmd.Info {
return &cmd.Info{Name: "test"}
}
func (c *testVersionFlagCommand) SetFlags(f *gnuflag.FlagSet) {
f.StringVar(&c.version, "version", "", "")
}
func (c *testVersionFlagCommand) Run(_ *cmd.Context) error {
return nil
}
func (s *SuperCommandSuite) TestVersionFlag(c *gc.C) {
jc := cmd.NewSuperCommand(cmd.SuperCommandParams{
Name: "jujutest",
Purpose: "to be purposeful",
Doc: "doc\nblah\ndoc",
Version: "111.222.333",
})
testVersionFlagCommand := &testVersionFlagCommand{}
jc.Register(testVersionFlagCommand)
var stdout, stderr bytes.Buffer
ctx := &cmd.Context{
Stdout: &stdout,
Stderr: &stderr,
}
// baseline: juju version
code := cmd.Main(jc, ctx, []string{"version"})
c.Check(code, gc.Equals, 0)
baselineStderr := stderr.String()
baselineStdout := stdout.String()
stderr.Reset()
stdout.Reset()
// juju --version output should match that of juju version.
code = cmd.Main(jc, ctx, []string{"--version"})
c.Check(code, gc.Equals, 0)
c.Assert(stderr.String(), gc.Equals, baselineStderr)
c.Assert(stdout.String(), gc.Equals, baselineStdout)
stderr.Reset()
stdout.Reset()
// juju test --version should update testVersionFlagCommand.version,
// and there should be no output. The --version flag on the 'test'
// subcommand has a different type to the "juju --version" flag.
code = cmd.Main(jc, ctx, []string{"test", "--version=abc.123"})
c.Check(code, gc.Equals, 0)
c.Assert(stderr.String(), gc.Equals, "")
c.Assert(stdout.String(), gc.Equals, "")
c.Assert(testVersionFlagCommand.version, gc.Equals, "abc.123")
}
func (s *SuperCommandSuite) TestVersionNotProvided(c *gc.C) {
jc := cmd.NewSuperCommand(cmd.SuperCommandParams{
Name: "jujutest",
Purpose: "to be purposeful",
Doc: "doc\nblah\ndoc",
})
var stdout, stderr bytes.Buffer
ctx := &cmd.Context{
Stdout: &stdout,
Stderr: &stderr,
}
// juju version
baselineCode := cmd.Main(jc, ctx, []string{"version"})
c.Check(baselineCode, gc.Not(gc.Equals), 0)
c.Assert(stderr.String(), gc.Equals, "error: unrecognized command: jujutest version\n")
stderr.Reset()
stdout.Reset()
// juju --version
code := cmd.Main(jc, ctx, []string{"--version"})
c.Check(code, gc.Equals, baselineCode)
c.Assert(stderr.String(), gc.Equals, "error: flag provided but not defined: --version\n")
}
func (s *SuperCommandSuite) TestLogging(c *gc.C) {
sc := cmd.NewSuperCommand(cmd.SuperCommandParams{
UsagePrefix: "juju",
Name: "command",
Log: &cmd.Log{},
})
sc.Register(&TestCommand{Name: "blah"})
ctx := cmdtesting.Context(c)
code := cmd.Main(sc, ctx, []string{"blah", "--option", "error", "--debug"})
c.Assert(code, gc.Equals, 1)
c.Assert(bufferString(ctx.Stderr), gc.Matches, `^.* ERROR .* BAM!\n`)
}
func (s *SuperCommandSuite) TestNotifyRun(c *gc.C) {
notifyTests := []struct {
usagePrefix string
name string
expectName string
}{
{"juju", "juju", "juju"},
{"something", "else", "something else"},
{"", "juju", "juju"},
{"", "myapp", "myapp"},
}
for i, test := range notifyTests {
c.Logf("test %d. %q %q", i, test.usagePrefix, test.name)
notifyName := ""
sc := cmd.NewSuperCommand(cmd.SuperCommandParams{
UsagePrefix: test.usagePrefix,
Name: test.name,
NotifyRun: func(name string) {
notifyName = name
},
})
sc.Register(&TestCommand{Name: "blah"})
ctx := cmdtesting.Context(c)
code := cmd.Main(sc, ctx, []string{"blah", "--option", "error"})
c.Assert(bufferString(ctx.Stderr), gc.Matches, "")
c.Assert(code, gc.Equals, 1)
c.Assert(notifyName, gc.Equals, test.expectName)
}
}
func (s *SuperCommandSuite) TestDescription(c *gc.C) {
jc := cmd.NewSuperCommand(cmd.SuperCommandParams{Name: "jujutest", Purpose: "blow up the death star"})
jc.Register(&TestCommand{Name: "blah"})
ctx := cmdtesting.Context(c)
code := cmd.Main(jc, ctx, []string{"blah", "--description"})
c.Assert(code, gc.Equals, 0)
c.Assert(bufferString(ctx.Stdout), gc.Equals, "blow up the death star\n")
}
func (s *SuperCommandSuite) TestHelp(c *gc.C) {
jc := cmd.NewSuperCommand(cmd.SuperCommandParams{Name: "jujutest"})
jc.Register(&TestCommand{Name: "blah"})
ctx := cmdtesting.Context(c)
code := cmd.Main(jc, ctx, []string{"blah", "--help"})
c.Assert(code, gc.Equals, 0)
stripped := strings.Replace(bufferString(ctx.Stdout), "\n", "", -1)
c.Assert(stripped, gc.Matches, ".*usage: jujutest blah.*blah-doc.*")
}
func (s *SuperCommandSuite) TestHelpWithPrefix(c *gc.C) {
jc := cmd.NewSuperCommand(cmd.SuperCommandParams{Name: "jujutest", UsagePrefix: "juju"})
jc.Register(&TestCommand{Name: "blah"})
ctx := cmdtesting.Context(c)
code := cmd.Main(jc, ctx, []string{"blah", "--help"})
c.Assert(code, gc.Equals, 0)
stripped := strings.Replace(bufferString(ctx.Stdout), "\n", "", -1)
c.Assert(stripped, gc.Matches, ".*usage: juju jujutest blah.*blah-doc.*")
}
func NewSuperWithCallback(callback func(*cmd.Context, string, []string) error) cmd.Command {
return cmd.NewSuperCommand(cmd.SuperCommandParams{
Name: "jujutest",
Log: &cmd.Log{},
MissingCallback: callback,
})
}
func (s *SuperCommandSuite) TestMissingCallback(c *gc.C) {
var calledName string
var calledArgs []string
callback := func(ctx *cmd.Context, subcommand string, args []string) error {
calledName = subcommand
calledArgs = args
return nil
}
code := cmd.Main(
NewSuperWithCallback(callback),
cmdtesting.Context(c),
[]string{"foo", "bar", "baz", "--debug"})
c.Assert(code, gc.Equals, 0)
c.Assert(calledName, gc.Equals, "foo")
c.Assert(calledArgs, gc.DeepEquals, []string{"bar", "baz", "--debug"})
}
func (s *SuperCommandSuite) TestMissingCallbackErrors(c *gc.C) {
callback := func(ctx *cmd.Context, subcommand string, args []string) error {
return fmt.Errorf("command not found %q", subcommand)
}
ctx := cmdtesting.Context(c)
code := cmd.Main(NewSuperWithCallback(callback), ctx, []string{"foo"})
c.Assert(code, gc.Equals, 1)
c.Assert(cmdtesting.Stdout(ctx), gc.Equals, "")
c.Assert(cmdtesting.Stderr(ctx), gc.Equals, "ERROR command not found \"foo\"\n")
}
func (s *SuperCommandSuite) TestMissingCallbackContextWiredIn(c *gc.C) {
callback := func(ctx *cmd.Context, subcommand string, args []string) error {
fmt.Fprintf(ctx.Stdout, "this is std out")
fmt.Fprintf(ctx.Stderr, "this is std err")
return nil
}
ctx := cmdtesting.Context(c)
code := cmd.Main(NewSuperWithCallback(callback), ctx, []string{"foo", "bar", "baz", "--debug"})
c.Assert(code, gc.Equals, 0)
c.Assert(cmdtesting.Stdout(ctx), gc.Equals, "this is std out")
c.Assert(cmdtesting.Stderr(ctx), gc.Equals, "this is std err")
}
func (s *SuperCommandSuite) TestSupercommandAliases(c *gc.C) {
jc := cmd.NewSuperCommand(cmd.SuperCommandParams{
Name: "jujutest",
UsagePrefix: "juju",
})
sub := cmd.NewSuperCommand(cmd.SuperCommandParams{
Name: "jubar",
UsagePrefix: "juju jujutest",
Aliases: []string{"jubaz", "jubing"},
})
info := sub.Info()
c.Check(info.Aliases, gc.DeepEquals, []string{"jubaz", "jubing"})
jc.Register(sub)
for _, name := range []string{"jubar", "jubaz", "jubing"} {
c.Logf("testing command name %q", name)
ctx := cmdtesting.Context(c)
code := cmd.Main(jc, ctx, []string{name, "--help"})
c.Assert(code, gc.Equals, 0)
stripped := strings.Replace(bufferString(ctx.Stdout), "\n", "", -1)
c.Assert(stripped, gc.Matches, ".*usage: juju jujutest jubar.*aliases: jubaz, jubing")
}
}