~sidnei/juju-core/lxc-clone-with-overlayfs

« back to all changes in this revision

Viewing changes to cmd/supercommand_test.go

  • Committer: Sidnei da Silva
  • Date: 2013-10-21 18:33:07 UTC
  • mfrom: (1667.1.329 juju-core)
  • Revision ID: sidnei.da.silva@canonical.com-20131021183307-fylyr68s4gtbxr08
- Merge from trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
        "strings"
10
10
 
11
11
        "launchpad.net/gnuflag"
12
 
        . "launchpad.net/gocheck"
 
12
        gc "launchpad.net/gocheck"
13
13
 
14
14
        "launchpad.net/juju-core/cmd"
15
15
        "launchpad.net/juju-core/testing"
 
16
        "launchpad.net/juju-core/testing/testbase"
16
17
)
17
18
 
18
19
func initDefenestrate(args []string) (*cmd.SuperCommand, *TestCommand, error) {
22
23
        return jc, tc, testing.InitCommand(jc, args)
23
24
}
24
25
 
25
 
type SuperCommandSuite struct{}
 
26
type SuperCommandSuite struct {
 
27
        testbase.LoggingSuite
 
28
}
26
29
 
27
 
var _ = Suite(&SuperCommandSuite{})
 
30
var _ = gc.Suite(&SuperCommandSuite{})
28
31
 
29
32
const helpText = "\n    help\\s+- show help on a command or other topic"
30
33
const helpCommandsText = "commands:" + helpText
31
34
 
32
 
func (s *SuperCommandSuite) TestDispatch(c *C) {
 
35
func (s *SuperCommandSuite) TestDispatch(c *gc.C) {
33
36
        jc := cmd.NewSuperCommand(cmd.SuperCommandParams{Name: "jujutest"})
34
37
        info := jc.Info()
35
 
        c.Assert(info.Name, Equals, "jujutest")
36
 
        c.Assert(info.Args, Equals, "<command> ...")
37
 
        c.Assert(info.Doc, Matches, helpCommandsText)
 
38
        c.Assert(info.Name, gc.Equals, "jujutest")
 
39
        c.Assert(info.Args, gc.Equals, "<command> ...")
 
40
        c.Assert(info.Doc, gc.Matches, helpCommandsText)
38
41
 
39
42
        jc, _, err := initDefenestrate([]string{"discombobulate"})
40
 
        c.Assert(err, ErrorMatches, "unrecognized command: jujutest discombobulate")
 
43
        c.Assert(err, gc.ErrorMatches, "unrecognized command: jujutest discombobulate")
41
44
        info = jc.Info()
42
 
        c.Assert(info.Name, Equals, "jujutest")
43
 
        c.Assert(info.Args, Equals, "<command> ...")
44
 
        c.Assert(info.Doc, Matches, "commands:\n    defenestrate - defenestrate the juju"+helpText)
 
45
        c.Assert(info.Name, gc.Equals, "jujutest")
 
46
        c.Assert(info.Args, gc.Equals, "<command> ...")
 
47
        c.Assert(info.Doc, gc.Matches, "commands:\n    defenestrate - defenestrate the juju"+helpText)
45
48
 
46
49
        jc, tc, err := initDefenestrate([]string{"defenestrate"})
47
 
        c.Assert(err, IsNil)
48
 
        c.Assert(tc.Option, Equals, "")
 
50
        c.Assert(err, gc.IsNil)
 
51
        c.Assert(tc.Option, gc.Equals, "")
49
52
        info = jc.Info()
50
 
        c.Assert(info.Name, Equals, "jujutest defenestrate")
51
 
        c.Assert(info.Args, Equals, "<something>")
52
 
        c.Assert(info.Doc, Equals, "defenestrate-doc")
 
53
        c.Assert(info.Name, gc.Equals, "jujutest defenestrate")
 
54
        c.Assert(info.Args, gc.Equals, "<something>")
 
55
        c.Assert(info.Doc, gc.Equals, "defenestrate-doc")
53
56
 
54
57
        _, tc, err = initDefenestrate([]string{"defenestrate", "--option", "firmly"})
55
 
        c.Assert(err, IsNil)
56
 
        c.Assert(tc.Option, Equals, "firmly")
 
58
        c.Assert(err, gc.IsNil)
 
59
        c.Assert(tc.Option, gc.Equals, "firmly")
57
60
 
58
61
        _, tc, err = initDefenestrate([]string{"defenestrate", "gibberish"})
59
 
        c.Assert(err, ErrorMatches, `unrecognized args: \["gibberish"\]`)
 
62
        c.Assert(err, gc.ErrorMatches, `unrecognized args: \["gibberish"\]`)
60
63
 
61
64
        // --description must be used on it's own.
62
65
        _, _, err = initDefenestrate([]string{"--description", "defenestrate"})
63
 
        c.Assert(err, ErrorMatches, `unrecognized args: \["defenestrate"\]`)
 
66
        c.Assert(err, gc.ErrorMatches, `unrecognized args: \["defenestrate"\]`)
64
67
}
65
68
 
66
 
func (s *SuperCommandSuite) TestRegister(c *C) {
 
69
func (s *SuperCommandSuite) TestRegister(c *gc.C) {
67
70
        jc := cmd.NewSuperCommand(cmd.SuperCommandParams{Name: "jujutest"})
68
71
        jc.Register(&TestCommand{Name: "flip"})
69
72
        jc.Register(&TestCommand{Name: "flap"})
70
73
        badCall := func() { jc.Register(&TestCommand{Name: "flap"}) }
71
 
        c.Assert(badCall, PanicMatches, "command already registered: flap")
 
74
        c.Assert(badCall, gc.PanicMatches, "command already registered: flap")
72
75
}
73
76
 
74
 
func (s *SuperCommandSuite) TestRegisterAlias(c *C) {
 
77
func (s *SuperCommandSuite) TestRegisterAlias(c *gc.C) {
75
78
        jc := cmd.NewSuperCommand(cmd.SuperCommandParams{Name: "jujutest"})
76
79
        jc.Register(&TestCommand{Name: "flip", Aliases: []string{"flap", "flop"}})
77
80
 
78
81
        info := jc.Info()
79
 
        c.Assert(info.Doc, Equals, `commands:
 
82
        c.Assert(info.Doc, gc.Equals, `commands:
80
83
    flap - alias for flip
81
84
    flip - flip the juju
82
85
    flop - alias for flip
87
90
    flapbabble - flapbabble the juju
88
91
    flip       - flip the juju`
89
92
 
90
 
func (s *SuperCommandSuite) TestInfo(c *C) {
 
93
func (s *SuperCommandSuite) TestInfo(c *gc.C) {
91
94
        jc := cmd.NewSuperCommand(cmd.SuperCommandParams{
92
95
                Name:    "jujutest",
93
96
                Purpose: "to be purposeful",
94
97
                Doc:     "doc\nblah\ndoc",
95
98
        })
96
99
        info := jc.Info()
97
 
        c.Assert(info.Name, Equals, "jujutest")
98
 
        c.Assert(info.Purpose, Equals, "to be purposeful")
 
100
        c.Assert(info.Name, gc.Equals, "jujutest")
 
101
        c.Assert(info.Purpose, gc.Equals, "to be purposeful")
99
102
        // info doc starts with the jc.Doc and ends with the help command
100
 
        c.Assert(info.Doc, Matches, jc.Doc+"(.|\n)*")
101
 
        c.Assert(info.Doc, Matches, "(.|\n)*"+helpCommandsText)
 
103
        c.Assert(info.Doc, gc.Matches, jc.Doc+"(.|\n)*")
 
104
        c.Assert(info.Doc, gc.Matches, "(.|\n)*"+helpCommandsText)
102
105
 
103
106
        jc.Register(&TestCommand{Name: "flip"})
104
107
        jc.Register(&TestCommand{Name: "flapbabble"})
105
108
        info = jc.Info()
106
 
        c.Assert(info.Doc, Matches, jc.Doc+"\n\n"+commandsDoc+helpText)
 
109
        c.Assert(info.Doc, gc.Matches, jc.Doc+"\n\n"+commandsDoc+helpText)
107
110
 
108
111
        jc.Doc = ""
109
112
        info = jc.Info()
110
 
        c.Assert(info.Doc, Matches, commandsDoc+helpText)
 
113
        c.Assert(info.Doc, gc.Matches, commandsDoc+helpText)
111
114
}
112
115
 
113
116
type testVersionFlagCommand struct {
127
130
        return nil
128
131
}
129
132
 
130
 
func (s *SuperCommandSuite) TestVersionFlag(c *C) {
 
133
func (s *SuperCommandSuite) TestVersionFlag(c *gc.C) {
131
134
        jc := cmd.NewSuperCommand(cmd.SuperCommandParams{
132
135
                Name:    "jujutest",
133
136
                Purpose: "to be purposeful",
145
148
 
146
149
        // baseline: juju version
147
150
        code := cmd.Main(jc, ctx, []string{"version"})
148
 
        c.Check(code, Equals, 0)
 
151
        c.Check(code, gc.Equals, 0)
149
152
        baselineStderr := stderr.String()
150
153
        baselineStdout := stdout.String()
151
154
        stderr.Reset()
153
156
 
154
157
        // juju --version output should match that of juju version.
155
158
        code = cmd.Main(jc, ctx, []string{"--version"})
156
 
        c.Check(code, Equals, 0)
157
 
        c.Assert(stderr.String(), Equals, baselineStderr)
158
 
        c.Assert(stdout.String(), Equals, baselineStdout)
 
159
        c.Check(code, gc.Equals, 0)
 
160
        c.Assert(stderr.String(), gc.Equals, baselineStderr)
 
161
        c.Assert(stdout.String(), gc.Equals, baselineStdout)
159
162
        stderr.Reset()
160
163
        stdout.Reset()
161
164
 
163
166
        // and there should be no output. The --version flag on the 'test'
164
167
        // subcommand has a different type to the "juju --version" flag.
165
168
        code = cmd.Main(jc, ctx, []string{"test", "--version=abc.123"})
166
 
        c.Check(code, Equals, 0)
167
 
        c.Assert(stderr.String(), Equals, "")
168
 
        c.Assert(stdout.String(), Equals, "")
169
 
        c.Assert(testVersionFlagCommand.version, Equals, "abc.123")
 
169
        c.Check(code, gc.Equals, 0)
 
170
        c.Assert(stderr.String(), gc.Equals, "")
 
171
        c.Assert(stdout.String(), gc.Equals, "")
 
172
        c.Assert(testVersionFlagCommand.version, gc.Equals, "abc.123")
170
173
}
171
174
 
172
 
func (s *SuperCommandSuite) TestLogging(c *C) {
 
175
func (s *SuperCommandSuite) TestLogging(c *gc.C) {
173
176
        jc := cmd.NewSuperCommand(cmd.SuperCommandParams{Name: "jujutest", Log: &cmd.Log{}})
174
177
        jc.Register(&TestCommand{Name: "blah"})
175
178
        ctx := testing.Context(c)
176
179
        code := cmd.Main(jc, ctx, []string{"blah", "--option", "error", "--debug"})
177
 
        c.Assert(code, Equals, 1)
178
 
        c.Assert(bufferString(ctx.Stderr), Matches, `^.* ERROR .* command failed: BAM!
179
 
error: BAM!
 
180
        c.Assert(code, gc.Equals, 1)
 
181
        c.Assert(bufferString(ctx.Stderr), gc.Matches, `^.* ERROR .* BAM!
180
182
`)
181
183
}
182
184
 
183
 
func (s *SuperCommandSuite) TestDescription(c *C) {
 
185
func (s *SuperCommandSuite) TestDescription(c *gc.C) {
184
186
        jc := cmd.NewSuperCommand(cmd.SuperCommandParams{Name: "jujutest", Purpose: "blow up the death star"})
185
187
        jc.Register(&TestCommand{Name: "blah"})
186
188
        ctx := testing.Context(c)
187
189
        code := cmd.Main(jc, ctx, []string{"blah", "--description"})
188
 
        c.Assert(code, Equals, 0)
189
 
        c.Assert(bufferString(ctx.Stdout), Equals, "blow up the death star\n")
 
190
        c.Assert(code, gc.Equals, 0)
 
191
        c.Assert(bufferString(ctx.Stdout), gc.Equals, "blow up the death star\n")
190
192
}
191
193
 
192
 
func (s *SuperCommandSuite) TestHelp(c *C) {
 
194
func (s *SuperCommandSuite) TestHelp(c *gc.C) {
193
195
        jc := cmd.NewSuperCommand(cmd.SuperCommandParams{Name: "jujutest"})
194
196
        jc.Register(&TestCommand{Name: "blah"})
195
197
        ctx := testing.Context(c)
196
198
        code := cmd.Main(jc, ctx, []string{"blah", "--help"})
197
 
        c.Assert(code, Equals, 0)
 
199
        c.Assert(code, gc.Equals, 0)
198
200
        stripped := strings.Replace(bufferString(ctx.Stdout), "\n", "", -1)
199
 
        c.Assert(stripped, Matches, ".*usage: jujutest blah.*blah-doc.*")
 
201
        c.Assert(stripped, gc.Matches, ".*usage: jujutest blah.*blah-doc.*")
200
202
}
201
203
 
202
 
func (s *SuperCommandSuite) TestHelpWithPrefix(c *C) {
 
204
func (s *SuperCommandSuite) TestHelpWithPrefix(c *gc.C) {
203
205
        jc := cmd.NewSuperCommand(cmd.SuperCommandParams{Name: "jujutest", UsagePrefix: "juju"})
204
206
        jc.Register(&TestCommand{Name: "blah"})
205
207
        ctx := testing.Context(c)
206
208
        code := cmd.Main(jc, ctx, []string{"blah", "--help"})
207
 
        c.Assert(code, Equals, 0)
 
209
        c.Assert(code, gc.Equals, 0)
208
210
        stripped := strings.Replace(bufferString(ctx.Stdout), "\n", "", -1)
209
 
        c.Assert(stripped, Matches, ".*usage: juju jujutest blah.*blah-doc.*")
 
211
        c.Assert(stripped, gc.Matches, ".*usage: juju jujutest blah.*blah-doc.*")
210
212
}
211
213
 
212
214
func NewSuperWithCallback(callback func(*cmd.Context, string, []string) error) cmd.Command {
217
219
        })
218
220
}
219
221
 
220
 
func (s *SuperCommandSuite) TestMissingCallback(c *C) {
 
222
func (s *SuperCommandSuite) TestMissingCallback(c *gc.C) {
221
223
        var calledName string
222
224
        var calledArgs []string
223
225
 
231
233
                NewSuperWithCallback(callback),
232
234
                testing.Context(c),
233
235
                []string{"foo", "bar", "baz", "--debug"})
234
 
        c.Assert(code, Equals, 0)
235
 
        c.Assert(calledName, Equals, "foo")
236
 
        c.Assert(calledArgs, DeepEquals, []string{"bar", "baz", "--debug"})
 
236
        c.Assert(code, gc.Equals, 0)
 
237
        c.Assert(calledName, gc.Equals, "foo")
 
238
        c.Assert(calledArgs, gc.DeepEquals, []string{"bar", "baz", "--debug"})
237
239
}
238
240
 
239
 
func (s *SuperCommandSuite) TestMissingCallbackErrors(c *C) {
 
241
func (s *SuperCommandSuite) TestMissingCallbackErrors(c *gc.C) {
240
242
        callback := func(ctx *cmd.Context, subcommand string, args []string) error {
241
243
                return fmt.Errorf("command not found %q", subcommand)
242
244
        }
243
245
 
244
246
        ctx := testing.Context(c)
245
247
        code := cmd.Main(NewSuperWithCallback(callback), ctx, []string{"foo"})
246
 
        c.Assert(code, Equals, 1)
247
 
        c.Assert(testing.Stdout(ctx), Equals, "")
248
 
        c.Assert(testing.Stderr(ctx), Equals, "error: command not found \"foo\"\n")
 
248
        c.Assert(code, gc.Equals, 1)
 
249
        c.Assert(testing.Stdout(ctx), gc.Equals, "")
 
250
        c.Assert(testing.Stderr(ctx), gc.Equals, "ERROR command not found \"foo\"\n")
249
251
}
250
252
 
251
 
func (s *SuperCommandSuite) TestMissingCallbackContextWiredIn(c *C) {
 
253
func (s *SuperCommandSuite) TestMissingCallbackContextWiredIn(c *gc.C) {
252
254
        callback := func(ctx *cmd.Context, subcommand string, args []string) error {
253
255
                fmt.Fprintf(ctx.Stdout, "this is std out")
254
256
                fmt.Fprintf(ctx.Stderr, "this is std err")
257
259
 
258
260
        ctx := testing.Context(c)
259
261
        code := cmd.Main(NewSuperWithCallback(callback), ctx, []string{"foo", "bar", "baz", "--debug"})
260
 
        c.Assert(code, Equals, 0)
261
 
        c.Assert(testing.Stdout(ctx), Equals, "this is std out")
262
 
        c.Assert(testing.Stderr(ctx), Equals, "this is std err")
 
262
        c.Assert(code, gc.Equals, 0)
 
263
        c.Assert(testing.Stdout(ctx), gc.Equals, "this is std out")
 
264
        c.Assert(testing.Stderr(ctx), gc.Equals, "this is std err")
263
265
}