LEFT | RIGHT |
(no file at all) | |
1 package cmd_test | 1 package cmd_test |
2 | 2 |
3 import ( | 3 import ( |
4 "fmt" | 4 "fmt" |
5 "launchpad.net/gnuflag" | 5 "launchpad.net/gnuflag" |
6 . "launchpad.net/gocheck" | 6 . "launchpad.net/gocheck" |
7 cmd "launchpad.net/juju/go/cmd" | 7 cmd "launchpad.net/juju/go/cmd" |
8 "launchpad.net/juju/go/log" | 8 "launchpad.net/juju/go/log" |
9 "os" | 9 "os" |
10 "path/filepath" | 10 "path/filepath" |
11 "testing" | 11 "testing" |
12 ) | 12 ) |
13 | 13 |
14 func Test(t *testing.T) { TestingT(t) } | 14 func Test(t *testing.T) { TestingT(t) } |
15 | 15 |
16 type TestCommand struct { | 16 type TestCommand struct { |
17 Name string | 17 Name string |
18 Value string | 18 Value string |
19 } | 19 } |
20 | 20 |
21 func (c *TestCommand) Info() *cmd.Info { | 21 func (c *TestCommand) Info() *cmd.Info { |
22 return &cmd.Info{ | 22 return &cmd.Info{ |
23 c.Name, "[options]", | 23 c.Name, "[options]", |
24 fmt.Sprintf("%s the juju", c.Name), | 24 fmt.Sprintf("%s the juju", c.Name), |
25 "blah doc", | 25 "blah doc", |
26 true, | |
27 } | 26 } |
28 } | 27 } |
29 | 28 |
30 func (c *TestCommand) InitFlagSet(f *gnuflag.FlagSet) { | 29 func (c *TestCommand) Init(f *gnuflag.FlagSet, args []string) error { |
31 f.StringVar(&c.Value, "value", "", "doc") | 30 f.StringVar(&c.Value, "value", "", "doc") |
| 31 if err := f.Parse(true, args); err != nil { |
| 32 return err |
| 33 } |
| 34 return cmd.CheckEmpty(f.Args()) |
32 } | 35 } |
33 | 36 |
34 func (c *TestCommand) ParsePositional(args []string) error { | 37 func (c *TestCommand) Run(ctx *cmd.Context) error { |
35 » if len(args) != 0 { | |
36 » » return fmt.Errorf("BADARGS: %s", args) | |
37 » } | |
38 » return nil | |
39 } | |
40 | |
41 func (c *TestCommand) Run() error { | |
42 return fmt.Errorf("BORKEN: value is %s.", c.Value) | 38 return fmt.Errorf("BORKEN: value is %s.", c.Value) |
43 } | 39 } |
44 | 40 |
45 func parseEmpty(args []string) (*cmd.SuperCommand, error) { | 41 func initCmd(c cmd.Command, args []string) error { |
46 » jc := cmd.NewSuperCommand("jujutest", "") | 42 » return c.Init(gnuflag.NewFlagSet("", gnuflag.ContinueOnError), args) |
47 » err := cmd.Parse(jc, args) | |
48 » return jc, err | |
49 } | 43 } |
50 | 44 |
51 func parseDefenestrate(args []string) (*cmd.SuperCommand, *TestCommand, error) { | 45 func initEmpty(args []string) (*cmd.SuperCommand, error) { |
52 » jc := cmd.NewSuperCommand("jujutest", "") | 46 » jc := cmd.NewSuperCommand("jujutest", "", "") |
| 47 » return jc, initCmd(jc, args) |
| 48 } |
| 49 |
| 50 func initDefenestrate(args []string) (*cmd.SuperCommand, *TestCommand, error) { |
| 51 » jc := cmd.NewSuperCommand("jujutest", "", "") |
53 tc := &TestCommand{Name: "defenestrate"} | 52 tc := &TestCommand{Name: "defenestrate"} |
54 jc.Register(tc) | 53 jc.Register(tc) |
55 » err := cmd.Parse(jc, args) | 54 » return jc, tc, initCmd(jc, args) |
56 » return jc, tc, err | |
57 } | 55 } |
58 | 56 |
59 type CommandSuite struct{} | 57 type CommandSuite struct{} |
60 | 58 |
61 var _ = Suite(&CommandSuite{}) | 59 var _ = Suite(&CommandSuite{}) |
62 | 60 |
63 func (s *CommandSuite) TestSubcommandDispatch(c *C) { | 61 func (s *CommandSuite) TestSubcommandDispatch(c *C) { |
64 » jc, err := parseEmpty([]string{}) | 62 » jc, err := initEmpty([]string{}) |
65 c.Assert(err, ErrorMatches, `no command specified`) | 63 c.Assert(err, ErrorMatches, `no command specified`) |
66 c.Assert(jc.Info().Usage(), Equals, "jujutest <command> [options] ...") | 64 c.Assert(jc.Info().Usage(), Equals, "jujutest <command> [options] ...") |
67 | 65 |
68 » _, _, err = parseDefenestrate([]string{"discombobulate"}) | 66 » _, _, err = initDefenestrate([]string{"discombobulate"}) |
69 » c.Assert(err, ErrorMatches, "unrecognised command: discombobulate") | 67 » c.Assert(err, ErrorMatches, "unrecognised command: jujutest discombobula
te") |
70 | 68 |
71 » jc, tc, err := parseDefenestrate([]string{"defenestrate"}) | 69 » jc, tc, err := initDefenestrate([]string{"defenestrate"}) |
72 c.Assert(err, IsNil) | 70 c.Assert(err, IsNil) |
73 c.Assert(tc.Value, Equals, "") | 71 c.Assert(tc.Value, Equals, "") |
74 c.Assert(jc.Info().Usage(), Equals, "jujutest defenestrate [options]") | 72 c.Assert(jc.Info().Usage(), Equals, "jujutest defenestrate [options]") |
75 | 73 |
76 » _, tc, err = parseDefenestrate([]string{"defenestrate", "--value", "firm
ly"}) | 74 » _, tc, err = initDefenestrate([]string{"defenestrate", "--value", "firml
y"}) |
77 c.Assert(err, IsNil) | 75 c.Assert(err, IsNil) |
78 c.Assert(tc.Value, Equals, "firmly") | 76 c.Assert(tc.Value, Equals, "firmly") |
79 | 77 |
80 » _, tc, err = parseDefenestrate([]string{"defenestrate", "gibberish"}) | 78 » _, tc, err = initDefenestrate([]string{"defenestrate", "gibberish"}) |
81 » c.Assert(err, ErrorMatches, `BADARGS: \[gibberish\]`) | 79 » c.Assert(err, ErrorMatches, `unrecognised args: \[gibberish\]`) |
82 } | 80 } |
83 | 81 |
84 func (s *CommandSuite) TestRegister(c *C) { | 82 func (s *CommandSuite) TestRegister(c *C) { |
85 » jc := cmd.NewSuperCommand("jujutest", "") | 83 » jc := cmd.NewSuperCommand("jujutest", "", "") |
86 jc.Register(&TestCommand{Name: "flip"}) | 84 jc.Register(&TestCommand{Name: "flip"}) |
87 jc.Register(&TestCommand{Name: "flap"}) | 85 jc.Register(&TestCommand{Name: "flap"}) |
88 | 86 |
89 badCall := func() { jc.Register(&TestCommand{Name: "flap"}) } | 87 badCall := func() { jc.Register(&TestCommand{Name: "flap"}) } |
90 c.Assert(badCall, PanicMatches, "command already registered: flap") | 88 c.Assert(badCall, PanicMatches, "command already registered: flap") |
91 | 89 |
92 cmds := jc.DescribeCommands() | 90 cmds := jc.DescribeCommands() |
93 c.Assert(cmds, Equals, "commands:\n flap flap the juju\n f
lip flip the juju\n") | 91 c.Assert(cmds, Equals, "commands:\n flap flap the juju\n f
lip flip the juju\n") |
94 } | 92 } |
95 | 93 |
96 func (s *CommandSuite) TestDebug(c *C) { | 94 func (s *CommandSuite) TestDebug(c *C) { |
97 » jc, err := parseEmpty([]string{}) | 95 » jc, err := initEmpty([]string{}) |
98 c.Assert(err, ErrorMatches, "no command specified") | 96 c.Assert(err, ErrorMatches, "no command specified") |
99 c.Assert(jc.Debug, Equals, false) | 97 c.Assert(jc.Debug, Equals, false) |
100 | 98 |
101 » jc, _, err = parseDefenestrate([]string{"defenestrate"}) | 99 » jc, _, err = initDefenestrate([]string{"defenestrate"}) |
102 c.Assert(err, IsNil) | 100 c.Assert(err, IsNil) |
103 c.Assert(jc.Debug, Equals, false) | 101 c.Assert(jc.Debug, Equals, false) |
104 | 102 |
105 » jc, err = parseEmpty([]string{"--debug"}) | 103 » jc, err = initEmpty([]string{"--debug"}) |
106 c.Assert(err, ErrorMatches, "no command specified") | 104 c.Assert(err, ErrorMatches, "no command specified") |
107 c.Assert(jc.Debug, Equals, true) | 105 c.Assert(jc.Debug, Equals, true) |
108 | 106 |
109 » jc, _, err = parseDefenestrate([]string{"-d", "defenestrate"}) | 107 » jc, _, err = initDefenestrate([]string{"--debug", "defenestrate"}) |
110 c.Assert(err, IsNil) | 108 c.Assert(err, IsNil) |
111 c.Assert(jc.Debug, Equals, true) | 109 c.Assert(jc.Debug, Equals, true) |
112 } | 110 } |
113 | 111 |
114 func (s *CommandSuite) TestVerbose(c *C) { | 112 func (s *CommandSuite) TestVerbose(c *C) { |
115 » jc, err := parseEmpty([]string{}) | 113 » jc, err := initEmpty([]string{}) |
116 c.Assert(err, ErrorMatches, "no command specified") | 114 c.Assert(err, ErrorMatches, "no command specified") |
117 c.Assert(jc.Verbose, Equals, false) | 115 c.Assert(jc.Verbose, Equals, false) |
118 | 116 |
119 » jc, _, err = parseDefenestrate([]string{"defenestrate"}) | 117 » jc, _, err = initDefenestrate([]string{"defenestrate"}) |
120 c.Assert(err, IsNil) | 118 c.Assert(err, IsNil) |
121 c.Assert(jc.Verbose, Equals, false) | 119 c.Assert(jc.Verbose, Equals, false) |
122 | 120 |
123 » jc, err = parseEmpty([]string{"--verbose"}) | 121 » jc, err = initEmpty([]string{"--verbose"}) |
124 c.Assert(err, ErrorMatches, "no command specified") | 122 c.Assert(err, ErrorMatches, "no command specified") |
125 c.Assert(jc.Verbose, Equals, true) | 123 c.Assert(jc.Verbose, Equals, true) |
126 | 124 |
127 » jc, _, err = parseDefenestrate([]string{"-v", "defenestrate"}) | 125 » jc, _, err = initDefenestrate([]string{"-v", "defenestrate"}) |
128 c.Assert(err, IsNil) | 126 c.Assert(err, IsNil) |
129 c.Assert(jc.Verbose, Equals, true) | 127 c.Assert(jc.Verbose, Equals, true) |
130 } | 128 } |
131 | 129 |
132 func (s *CommandSuite) TestLogFile(c *C) { | 130 func (s *CommandSuite) TestLogFile(c *C) { |
133 » jc, err := parseEmpty([]string{}) | 131 » jc, err := initEmpty([]string{}) |
134 c.Assert(err, ErrorMatches, "no command specified") | 132 c.Assert(err, ErrorMatches, "no command specified") |
135 c.Assert(jc.LogFile, Equals, "") | 133 c.Assert(jc.LogFile, Equals, "") |
136 | 134 |
137 » jc, _, err = parseDefenestrate([]string{"defenestrate"}) | 135 » jc, _, err = initDefenestrate([]string{"defenestrate"}) |
138 c.Assert(err, IsNil) | 136 c.Assert(err, IsNil) |
139 c.Assert(jc.LogFile, Equals, "") | 137 c.Assert(jc.LogFile, Equals, "") |
140 | 138 |
141 » jc, err = parseEmpty([]string{"--log-file", "foo"}) | 139 » jc, err = initEmpty([]string{"--log-file", "foo"}) |
142 c.Assert(err, ErrorMatches, "no command specified") | 140 c.Assert(err, ErrorMatches, "no command specified") |
143 c.Assert(jc.LogFile, Equals, "foo") | 141 c.Assert(jc.LogFile, Equals, "foo") |
144 | 142 |
145 » jc, _, err = parseDefenestrate([]string{"--log-file", "bar", "defenestra
te"}) | 143 » jc, _, err = initDefenestrate([]string{"--log-file", "bar", "defenestrat
e"}) |
146 c.Assert(err, IsNil) | 144 c.Assert(err, IsNil) |
147 c.Assert(jc.LogFile, Equals, "bar") | 145 c.Assert(jc.LogFile, Equals, "bar") |
148 } | 146 } |
149 | 147 |
150 func saveLog() func() { | 148 func saveLog() func() { |
151 target, debug := log.Target, log.Debug | 149 target, debug := log.Target, log.Debug |
152 log.Target, log.Debug = nil, false | |
153 return func() { | 150 return func() { |
154 log.Target, log.Debug = target, debug | 151 log.Target, log.Debug = target, debug |
155 } | 152 } |
156 } | 153 } |
157 | 154 |
158 func checkRun(c *C, args []string, debug bool, target Checker, logfile string) { | 155 func checkRun(c *C, args []string, debug bool, target Checker, logfile string) { |
159 defer saveLog()() | 156 defer saveLog()() |
160 args = append([]string{"defenestrate", "--value", "cheese"}, args...) | 157 args = append([]string{"defenestrate", "--value", "cheese"}, args...) |
161 » jc, _, err := parseDefenestrate(args) | 158 » jc, _, err := initDefenestrate(args) |
162 c.Assert(err, IsNil) | 159 c.Assert(err, IsNil) |
163 | 160 |
164 » err = jc.Run() | 161 » err = jc.Run(cmd.DefaultContext()) |
165 c.Assert(err, ErrorMatches, "BORKEN: value is cheese.") | 162 c.Assert(err, ErrorMatches, "BORKEN: value is cheese.") |
166 | 163 |
167 c.Assert(log.Debug, Equals, debug) | 164 c.Assert(log.Debug, Equals, debug) |
168 c.Assert(log.Target, target) | 165 c.Assert(log.Target, target) |
169 if logfile != "" { | 166 if logfile != "" { |
170 _, err := os.Stat(logfile) | 167 _, err := os.Stat(logfile) |
171 c.Assert(err, IsNil) | 168 c.Assert(err, IsNil) |
172 } | 169 } |
173 } | 170 } |
174 | 171 |
175 func (s *CommandSuite) TestRun(c *C) { | 172 func (s *CommandSuite) TestRun(c *C) { |
176 checkRun(c, []string{}, false, IsNil, "") | 173 checkRun(c, []string{}, false, IsNil, "") |
177 checkRun(c, []string{"--debug"}, true, NotNil, "") | 174 checkRun(c, []string{"--debug"}, true, NotNil, "") |
178 checkRun(c, []string{"--verbose"}, false, NotNil, "") | 175 checkRun(c, []string{"--verbose"}, false, NotNil, "") |
179 checkRun(c, []string{"--verbose", "--debug"}, true, NotNil, "") | 176 checkRun(c, []string{"--verbose", "--debug"}, true, NotNil, "") |
180 | 177 |
181 tmp := c.MkDir() | 178 tmp := c.MkDir() |
182 path := filepath.Join(tmp, "log-1") | 179 path := filepath.Join(tmp, "log-1") |
183 checkRun(c, []string{"--log-file", path}, false, NotNil, path) | 180 checkRun(c, []string{"--log-file", path}, false, NotNil, path) |
184 | 181 |
185 path = filepath.Join(tmp, "log-2") | 182 path = filepath.Join(tmp, "log-2") |
186 checkRun(c, []string{"--log-file", path, "--debug"}, true, NotNil, path) | 183 checkRun(c, []string{"--log-file", path, "--debug"}, true, NotNil, path) |
187 | 184 |
188 path = filepath.Join(tmp, "log-3") | 185 path = filepath.Join(tmp, "log-3") |
189 checkRun(c, []string{"--log-file", path, "--verbose"}, false, NotNil, pa
th) | 186 checkRun(c, []string{"--log-file", path, "--verbose"}, false, NotNil, pa
th) |
190 | 187 |
191 path = filepath.Join(tmp, "log-4") | 188 path = filepath.Join(tmp, "log-4") |
192 checkRun(c, []string{"--log-file", path, "--verbose", "--debug"}, true,
NotNil, path) | 189 checkRun(c, []string{"--log-file", path, "--verbose", "--debug"}, true,
NotNil, path) |
193 } | 190 } |
LEFT | RIGHT |