Left: | ||
Right: |
OLD | NEW |
---|---|
1 // Copyright 2012, 2013 Canonical Ltd. | 1 // Copyright 2012, 2013 Canonical Ltd. |
2 // Licensed under the AGPLv3, see LICENCE file for details. | 2 // Licensed under the AGPLv3, see LICENCE file for details. |
3 | 3 |
4 package cmd_test | 4 package cmd_test |
5 | 5 |
6 import ( | 6 import ( |
7 "bytes" | 7 "bytes" |
8 "fmt" | 8 "fmt" |
9 "strings" | 9 "strings" |
10 | 10 |
11 "launchpad.net/gnuflag" | 11 "launchpad.net/gnuflag" |
12 gc "launchpad.net/gocheck" | 12 gc "launchpad.net/gocheck" |
13 | 13 |
14 "launchpad.net/juju-core/cmd" | 14 "launchpad.net/juju-core/cmd" |
15 "launchpad.net/juju-core/testing" | 15 "launchpad.net/juju-core/testing" |
16 "launchpad.net/juju-core/testing/testbase" | 16 "launchpad.net/juju-core/testing/testbase" |
17 "launchpad.net/juju-core/version" | |
17 ) | 18 ) |
18 | 19 |
19 func initDefenestrate(args []string) (*cmd.SuperCommand, *TestCommand, error) { | 20 func initDefenestrate(args []string) (*cmd.SuperCommand, *TestCommand, error) { |
20 jc := cmd.NewSuperCommand(cmd.SuperCommandParams{Name: "jujutest"}) | 21 jc := cmd.NewSuperCommand(cmd.SuperCommandParams{Name: "jujutest"}) |
21 tc := &TestCommand{Name: "defenestrate"} | 22 tc := &TestCommand{Name: "defenestrate"} |
22 jc.Register(tc) | 23 jc.Register(tc) |
23 return jc, tc, testing.InitCommand(jc, args) | 24 return jc, tc, testing.InitCommand(jc, args) |
24 } | 25 } |
25 | 26 |
26 type SuperCommandSuite struct { | 27 type SuperCommandSuite struct { |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
79 jc.Register(&TestCommand{Name: "flip", Aliases: []string{"flap", "flop"} }) | 80 jc.Register(&TestCommand{Name: "flip", Aliases: []string{"flap", "flop"} }) |
80 | 81 |
81 info := jc.Info() | 82 info := jc.Info() |
82 c.Assert(info.Doc, gc.Equals, `commands: | 83 c.Assert(info.Doc, gc.Equals, `commands: |
83 flap - alias for flip | 84 flap - alias for flip |
84 flip - flip the juju | 85 flip - flip the juju |
85 flop - alias for flip | 86 flop - alias for flip |
86 help - show help on a command or other topic`) | 87 help - show help on a command or other topic`) |
87 } | 88 } |
88 | 89 |
89 var commandsDoc = `commands: | 90 func (s *SuperCommandSuite) TestInfo(c *gc.C) { |
91 » commandsDoc := `commands: | |
90 flapbabble - flapbabble the juju | 92 flapbabble - flapbabble the juju |
91 flip - flip the juju` | 93 flip - flip the juju` |
92 | 94 |
93 func (s *SuperCommandSuite) TestInfo(c *gc.C) { | |
94 jc := cmd.NewSuperCommand(cmd.SuperCommandParams{ | 95 jc := cmd.NewSuperCommand(cmd.SuperCommandParams{ |
95 Name: "jujutest", | 96 Name: "jujutest", |
96 Purpose: "to be purposeful", | 97 Purpose: "to be purposeful", |
97 Doc: "doc\nblah\ndoc", | 98 Doc: "doc\nblah\ndoc", |
98 }) | 99 }) |
99 info := jc.Info() | 100 info := jc.Info() |
100 c.Assert(info.Name, gc.Equals, "jujutest") | 101 c.Assert(info.Name, gc.Equals, "jujutest") |
101 c.Assert(info.Purpose, gc.Equals, "to be purposeful") | 102 c.Assert(info.Purpose, gc.Equals, "to be purposeful") |
102 // info doc starts with the jc.Doc and ends with the help command | 103 // info doc starts with the jc.Doc and ends with the help command |
103 c.Assert(info.Doc, gc.Matches, jc.Doc+"(.|\n)*") | 104 c.Assert(info.Doc, gc.Matches, jc.Doc+"(.|\n)*") |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
166 // and there should be no output. The --version flag on the 'test' | 167 // and there should be no output. The --version flag on the 'test' |
167 // subcommand has a different type to the "juju --version" flag. | 168 // subcommand has a different type to the "juju --version" flag. |
168 code = cmd.Main(jc, ctx, []string{"test", "--version=abc.123"}) | 169 code = cmd.Main(jc, ctx, []string{"test", "--version=abc.123"}) |
169 c.Check(code, gc.Equals, 0) | 170 c.Check(code, gc.Equals, 0) |
170 c.Assert(stderr.String(), gc.Equals, "") | 171 c.Assert(stderr.String(), gc.Equals, "") |
171 c.Assert(stdout.String(), gc.Equals, "") | 172 c.Assert(stdout.String(), gc.Equals, "") |
172 c.Assert(testVersionFlagCommand.version, gc.Equals, "abc.123") | 173 c.Assert(testVersionFlagCommand.version, gc.Equals, "abc.123") |
173 } | 174 } |
174 | 175 |
175 func (s *SuperCommandSuite) TestLogging(c *gc.C) { | 176 func (s *SuperCommandSuite) TestLogging(c *gc.C) { |
176 » jc := cmd.NewSuperCommand(cmd.SuperCommandParams{Name: "jujutest", Log: &cmd.Log{}}) | 177 » saveVersion := version.Current |
177 » jc.Register(&TestCommand{Name: "blah"}) | 178 » saveCompiler := version.Compiler |
178 » ctx := testing.Context(c) | 179 » defer func() { |
179 » code := cmd.Main(jc, ctx, []string{"blah", "--option", "error", "--debug "}) | 180 » » version.Current = saveVersion |
180 » c.Assert(code, gc.Equals, 1) | 181 » » version.Compiler = saveCompiler |
181 » c.Assert(bufferString(ctx.Stderr), gc.Matches, `^.* running juju-.* | 182 » }() |
183 » version.Current = version.Binary{ | |
184 » » Number: version.Number{1, 2, 3, 4}, | |
185 » » Series: "plan9", | |
186 » » Arch: "mips", | |
187 » } | |
188 » version.Compiler = "llgo" | |
fwereade
2014/05/21 07:42:25
Search for PatchValue, you might find it helpful.
cmars
2014/05/21 19:16:31
Done.
| |
189 | |
190 » loggingTests := []struct { | |
191 » » usagePrefix, name string | |
192 » » pattern string | |
193 » }{ | |
194 » » {"juju", "juju", `^.* running juju \[1.2.3.4-plan9-mips llgo\] | |
182 .* ERROR .* BAM! | 195 .* ERROR .* BAM! |
183 `) | 196 `}, |
197 » » {"something", "else", `^.* running something else \[1.2.3.4-plan 9-mips llgo\] | |
198 .* ERROR .* BAM! | |
199 `}, | |
200 » » {"", "juju", `^.* running juju \[1.2.3.4-plan9-mips llgo\] | |
201 .* ERROR .* BAM! | |
202 `}, | |
203 » » {"", "myapp", `^.* running myapp \[1.2.3.4-plan9-mips llgo\] | |
204 .* ERROR .* BAM! | |
205 `}, | |
206 » » {"same", "same", `^.* running same \[1.2.3.4-plan9-mips llgo\] | |
207 .* ERROR .* BAM! | |
208 `}, | |
209 » } | |
210 | |
211 » for _, test := range loggingTests { | |
212 » » jc := cmd.NewSuperCommand(cmd.SuperCommandParams{ | |
213 » » » UsagePrefix: test.usagePrefix, | |
214 » » » Name: test.name, | |
215 » » » Log: &cmd.Log{}, | |
216 » » }) | |
217 » » jc.Register(&TestCommand{Name: "blah"}) | |
218 » » ctx := testing.Context(c) | |
219 » » code := cmd.Main(jc, ctx, []string{"blah", "--option", "error", "--debug"}) | |
220 » » c.Assert(code, gc.Equals, 1) | |
221 » » c.Assert(bufferString(ctx.Stderr), gc.Matches, test.pattern) | |
222 » } | |
184 } | 223 } |
185 | 224 |
186 func (s *SuperCommandSuite) TestDescription(c *gc.C) { | 225 func (s *SuperCommandSuite) TestDescription(c *gc.C) { |
187 jc := cmd.NewSuperCommand(cmd.SuperCommandParams{Name: "jujutest", Purpo se: "blow up the death star"}) | 226 jc := cmd.NewSuperCommand(cmd.SuperCommandParams{Name: "jujutest", Purpo se: "blow up the death star"}) |
188 jc.Register(&TestCommand{Name: "blah"}) | 227 jc.Register(&TestCommand{Name: "blah"}) |
189 ctx := testing.Context(c) | 228 ctx := testing.Context(c) |
190 code := cmd.Main(jc, ctx, []string{"blah", "--description"}) | 229 code := cmd.Main(jc, ctx, []string{"blah", "--description"}) |
191 c.Assert(code, gc.Equals, 0) | 230 c.Assert(code, gc.Equals, 0) |
192 c.Assert(bufferString(ctx.Stdout), gc.Equals, "blow up the death star\n" ) | 231 c.Assert(bufferString(ctx.Stdout), gc.Equals, "blow up the death star\n" ) |
193 } | 232 } |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
280 jc.Register(sub) | 319 jc.Register(sub) |
281 for _, name := range []string{"jubar", "jubaz", "jubing"} { | 320 for _, name := range []string{"jubar", "jubaz", "jubing"} { |
282 c.Logf("testing command name %q", name) | 321 c.Logf("testing command name %q", name) |
283 ctx := testing.Context(c) | 322 ctx := testing.Context(c) |
284 code := cmd.Main(jc, ctx, []string{name, "--help"}) | 323 code := cmd.Main(jc, ctx, []string{name, "--help"}) |
285 c.Assert(code, gc.Equals, 0) | 324 c.Assert(code, gc.Equals, 0) |
286 stripped := strings.Replace(bufferString(ctx.Stdout), "\n", "", -1) | 325 stripped := strings.Replace(bufferString(ctx.Stdout), "\n", "", -1) |
287 c.Assert(stripped, gc.Matches, ".*usage: juju jujutest jubar.*al iases: jubaz, jubing") | 326 c.Assert(stripped, gc.Matches, ".*usage: juju jujutest jubar.*al iases: jubaz, jubing") |
288 } | 327 } |
289 } | 328 } |
OLD | NEW |