4
. "launchpad.net/gocheck"
5
"launchpad.net/juju-core/cmd"
6
"launchpad.net/juju-core/log"
7
"launchpad.net/juju-core/testing"
10
func initDefenestrate(args []string) (*cmd.SuperCommand, *TestCommand, error) {
11
jc := cmd.NewSuperCommand(cmd.SuperCommandParams{Name: "jujutest"})
12
tc := &TestCommand{Name: "defenestrate"}
14
return jc, tc, testing.InitCommand(jc, args)
17
type SuperCommandSuite struct{}
19
var _ = Suite(&SuperCommandSuite{})
21
const helpText = "\n help\\s+- show help on a command or other topic"
22
const helpCommandsText = "commands:" + helpText
24
func (s *SuperCommandSuite) TestDispatch(c *C) {
25
jc := cmd.NewSuperCommand(cmd.SuperCommandParams{Name: "jujutest"})
27
c.Assert(info.Name, Equals, "jujutest")
28
c.Assert(info.Args, Equals, "<command> ...")
29
c.Assert(info.Doc, Matches, helpCommandsText)
31
jc, _, err := initDefenestrate([]string{"discombobulate"})
32
c.Assert(err, ErrorMatches, "unrecognized command: jujutest discombobulate")
34
c.Assert(info.Name, Equals, "jujutest")
35
c.Assert(info.Args, Equals, "<command> ...")
36
c.Assert(info.Doc, Matches, "commands:\n defenestrate - defenestrate the juju"+helpText)
38
jc, tc, err := initDefenestrate([]string{"defenestrate"})
40
c.Assert(tc.Option, Equals, "")
42
c.Assert(info.Name, Equals, "jujutest defenestrate")
43
c.Assert(info.Args, Equals, "<something>")
44
c.Assert(info.Doc, Equals, "defenestrate-doc")
46
_, tc, err = initDefenestrate([]string{"defenestrate", "--option", "firmly"})
48
c.Assert(tc.Option, Equals, "firmly")
50
_, tc, err = initDefenestrate([]string{"defenestrate", "gibberish"})
51
c.Assert(err, ErrorMatches, `unrecognized args: \["gibberish"\]`)
54
func (s *SuperCommandSuite) TestRegister(c *C) {
55
jc := cmd.NewSuperCommand(cmd.SuperCommandParams{Name: "jujutest"})
56
jc.Register(&TestCommand{Name: "flip"})
57
jc.Register(&TestCommand{Name: "flap"})
58
badCall := func() { jc.Register(&TestCommand{Name: "flap"}) }
59
c.Assert(badCall, PanicMatches, "command already registered: flap")
62
func (s *SuperCommandSuite) TestRegisterAlias(c *C) {
63
jc := cmd.NewSuperCommand(cmd.SuperCommandParams{Name: "jujutest"})
64
jc.Register(&TestCommand{Name: "flip", Aliases: []string{"flap", "flop"}})
67
c.Assert(info.Doc, Equals, `commands:
71
help - show help on a command or other topic`)
74
var commandsDoc = `commands:
75
flapbabble - flapbabble the juju
78
func (s *SuperCommandSuite) TestInfo(c *C) {
79
jc := cmd.NewSuperCommand(cmd.SuperCommandParams{
81
Purpose: "to be purposeful",
82
Doc: "doc\nblah\ndoc",
85
c.Assert(info.Name, Equals, "jujutest")
86
c.Assert(info.Purpose, Equals, "to be purposeful")
87
// info doc starts with the jc.Doc and ends with the help command
88
c.Assert(info.Doc, Matches, jc.Doc+"(.|\n)*")
89
c.Assert(info.Doc, Matches, "(.|\n)*"+helpCommandsText)
91
jc.Register(&TestCommand{Name: "flip"})
92
jc.Register(&TestCommand{Name: "flapbabble"})
94
c.Assert(info.Doc, Matches, jc.Doc+"\n\n"+commandsDoc+helpText)
98
c.Assert(info.Doc, Matches, commandsDoc+helpText)
101
func (s *SuperCommandSuite) TestLogging(c *C) {
102
target, debug := log.Target(), log.Debug
104
log.SetTarget(target)
107
jc := cmd.NewSuperCommand(cmd.SuperCommandParams{Name: "jujutest", Log: &cmd.Log{}})
108
jc.Register(&TestCommand{Name: "blah"})
109
ctx := testing.Context(c)
110
code := cmd.Main(jc, ctx, []string{"blah", "--option", "error", "--debug"})
111
c.Assert(code, Equals, 1)
112
c.Assert(bufferString(ctx.Stderr), Matches, `^.* ERROR command failed: BAM!