~juju-qa/ubuntu/yakkety/juju/2.0-rc3-again

« back to all changes in this revision

Viewing changes to src/launchpad.net/juju-core/cmd/supercommand_test.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-04-24 22:34:47 UTC
  • Revision ID: package-import@ubuntu.com-20130424223447-f0qdji7ubnyo0s71
Tags: upstream-1.10.0.1
ImportĀ upstreamĀ versionĀ 1.10.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package cmd_test
 
2
 
 
3
import (
 
4
        . "launchpad.net/gocheck"
 
5
        "launchpad.net/juju-core/cmd"
 
6
        "launchpad.net/juju-core/log"
 
7
        "launchpad.net/juju-core/testing"
 
8
)
 
9
 
 
10
func initDefenestrate(args []string) (*cmd.SuperCommand, *TestCommand, error) {
 
11
        jc := cmd.NewSuperCommand(cmd.SuperCommandParams{Name: "jujutest"})
 
12
        tc := &TestCommand{Name: "defenestrate"}
 
13
        jc.Register(tc)
 
14
        return jc, tc, testing.InitCommand(jc, args)
 
15
}
 
16
 
 
17
type SuperCommandSuite struct{}
 
18
 
 
19
var _ = Suite(&SuperCommandSuite{})
 
20
 
 
21
const helpText = "\n    help\\s+- show help on a command or other topic"
 
22
const helpCommandsText = "commands:" + helpText
 
23
 
 
24
func (s *SuperCommandSuite) TestDispatch(c *C) {
 
25
        jc := cmd.NewSuperCommand(cmd.SuperCommandParams{Name: "jujutest"})
 
26
        info := jc.Info()
 
27
        c.Assert(info.Name, Equals, "jujutest")
 
28
        c.Assert(info.Args, Equals, "<command> ...")
 
29
        c.Assert(info.Doc, Matches, helpCommandsText)
 
30
 
 
31
        jc, _, err := initDefenestrate([]string{"discombobulate"})
 
32
        c.Assert(err, ErrorMatches, "unrecognized command: jujutest discombobulate")
 
33
        info = jc.Info()
 
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)
 
37
 
 
38
        jc, tc, err := initDefenestrate([]string{"defenestrate"})
 
39
        c.Assert(err, IsNil)
 
40
        c.Assert(tc.Option, Equals, "")
 
41
        info = jc.Info()
 
42
        c.Assert(info.Name, Equals, "jujutest defenestrate")
 
43
        c.Assert(info.Args, Equals, "<something>")
 
44
        c.Assert(info.Doc, Equals, "defenestrate-doc")
 
45
 
 
46
        _, tc, err = initDefenestrate([]string{"defenestrate", "--option", "firmly"})
 
47
        c.Assert(err, IsNil)
 
48
        c.Assert(tc.Option, Equals, "firmly")
 
49
 
 
50
        _, tc, err = initDefenestrate([]string{"defenestrate", "gibberish"})
 
51
        c.Assert(err, ErrorMatches, `unrecognized args: \["gibberish"\]`)
 
52
}
 
53
 
 
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")
 
60
}
 
61
 
 
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"}})
 
65
 
 
66
        info := jc.Info()
 
67
        c.Assert(info.Doc, Equals, `commands:
 
68
    flap - alias for flip
 
69
    flip - flip the juju
 
70
    flop - alias for flip
 
71
    help - show help on a command or other topic`)
 
72
}
 
73
 
 
74
var commandsDoc = `commands:
 
75
    flapbabble - flapbabble the juju
 
76
    flip       - flip the juju`
 
77
 
 
78
func (s *SuperCommandSuite) TestInfo(c *C) {
 
79
        jc := cmd.NewSuperCommand(cmd.SuperCommandParams{
 
80
                Name:    "jujutest",
 
81
                Purpose: "to be purposeful",
 
82
                Doc:     "doc\nblah\ndoc",
 
83
        })
 
84
        info := jc.Info()
 
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)
 
90
 
 
91
        jc.Register(&TestCommand{Name: "flip"})
 
92
        jc.Register(&TestCommand{Name: "flapbabble"})
 
93
        info = jc.Info()
 
94
        c.Assert(info.Doc, Matches, jc.Doc+"\n\n"+commandsDoc+helpText)
 
95
 
 
96
        jc.Doc = ""
 
97
        info = jc.Info()
 
98
        c.Assert(info.Doc, Matches, commandsDoc+helpText)
 
99
}
 
100
 
 
101
func (s *SuperCommandSuite) TestLogging(c *C) {
 
102
        target, debug := log.Target(), log.Debug
 
103
        defer func() {
 
104
                log.SetTarget(target)
 
105
                log.Debug = debug
 
106
        }()
 
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!
 
113
error: BAM!
 
114
`)
 
115
}