Merge lp:~mwhudson/lava-test/help-command-subcommand into lp:lava-test/0.0
- help-command-subcommand
- Merge into trunk
Proposed by
Michael Hudson-Doyle
Status: | Merged | ||||||||
---|---|---|---|---|---|---|---|---|---|
Merged at revision: | 46 | ||||||||
Proposed branch: | lp:~mwhudson/lava-test/help-command-subcommand | ||||||||
Merge into: | lp:lava-test/0.0 | ||||||||
Diff against target: |
887 lines (+333/-249) 10 files modified
abrek/builtins.py (+17/-9) abrek/command.py (+55/-22) abrek/dashboard.py (+82/-84) abrek/main.py (+1/-8) abrek/results.py (+81/-84) tests/test_abrekcmd.py (+45/-20) tests/test_builtins.py (+28/-0) tests/test_dashboard.py (+8/-8) tests/test_main.py (+4/-2) tests/test_results.py (+12/-12) |
||||||||
To merge this branch: | bzr merge lp:~mwhudson/lava-test/help-command-subcommand | ||||||||
Related bugs: |
|
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Paul Larson (community) | Approve | ||
Review via email: mp+38176@code.launchpad.net |
Commit message
Description of the change
Hi Paul,
I wanted to fix the bug I reported about 'abrek help dashboard put' not being useful and ended up rewriting how subcommands work to a moderate extent :-) Hope that's OK. The changes are I hope reasonably clear and I think I added tests for everything.
Cheers,
mwh
To post a comment you must log in.
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'abrek/builtins.py' |
2 | --- abrek/builtins.py 2010-10-01 14:45:24 +0000 |
3 | +++ abrek/builtins.py 2010-10-12 02:45:55 +0000 |
4 | @@ -13,7 +13,6 @@ |
5 | # You should have received a copy of the GNU General Public License |
6 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
7 | |
8 | -import json |
9 | import os |
10 | import sys |
11 | from optparse import make_option |
12 | @@ -38,21 +37,30 @@ |
13 | If the command name is ommited, calling the help command will return a |
14 | list of valid commands. |
15 | """ |
16 | - arglist = ['command'] |
17 | + arglist = ['command', 'subcommand'] |
18 | def run(self): |
19 | - if len(self.args) != 1: |
20 | + if len(self.args) < 1: |
21 | print "Available commands:" |
22 | for cmd in abrek.command.get_all_cmds(): |
23 | print " %s" % cmd |
24 | |
25 | print "To access extended help on a command use 'abrek help " \ |
26 | "[command]'" |
27 | - else: |
28 | - cmd = abrek.command.get_command(self.args[0]) |
29 | - if cmd: |
30 | - print cmd.help() |
31 | - else: |
32 | - print "No command found for '%s'" % self.args[0] |
33 | + return |
34 | + command_name = self.args.pop(0) |
35 | + cmd = abrek.command.get_command(command_name) |
36 | + if not cmd: |
37 | + print "No command found for '%s'" % command_name |
38 | + return |
39 | + while self.args: |
40 | + subcommand_name = self.args.pop(0) |
41 | + cmd = cmd.get_subcommand(subcommand_name) |
42 | + if not cmd: |
43 | + print "No sub-command of '%s' found for '%s'" % ( |
44 | + command_name, subcommand_name) |
45 | + return |
46 | + command_name += ' ' + subcommand_name |
47 | + print cmd.help() |
48 | |
49 | |
50 | class cmd_install(abrek.command.AbrekCmd): |
51 | |
52 | === modified file 'abrek/command.py' |
53 | --- abrek/command.py 2010-09-03 14:49:59 +0000 |
54 | +++ abrek/command.py 2010-10-12 02:45:55 +0000 |
55 | @@ -53,27 +53,25 @@ |
56 | """ |
57 | options = [] |
58 | arglist = [] |
59 | - subcmds = {} |
60 | |
61 | - def __init__(self): |
62 | + def __init__(self, name_prefix=''): |
63 | + self._name_prefix = name_prefix |
64 | self.parser = _AbrekOptionParser(usage=self._usage(), |
65 | epilog=self._desc()) |
66 | for opt in self.options: |
67 | self.parser.add_option(opt) |
68 | |
69 | def main(self, argv): |
70 | - if len(argv) and argv[0] in self.subcmds.keys(): |
71 | - return self.subcmds[argv[0]].main(argv[1:]) |
72 | - else: |
73 | - (self.opts, self.args) = self.parser.parse_args(argv) |
74 | - return self.run() |
75 | + (self.opts, self.args) = self.parser.parse_args(argv) |
76 | + return self.run() |
77 | |
78 | def name(self): |
79 | - return _convert_command_name(self.__class__.__name__) |
80 | + return self._name_prefix + _convert_command_name(self.__class__.__name__) |
81 | |
82 | def run(self): |
83 | raise NotImplementedError("%s: command defined but not implemented!" % |
84 | self.name()) |
85 | + |
86 | def _usage(self): |
87 | usagestr = "Usage: abrek %s" % self.name() |
88 | for arg in self.arglist: |
89 | @@ -81,7 +79,6 @@ |
90 | usagestr += " %s" % arg[1:].upper() |
91 | else: |
92 | usagestr += " [%s]" % arg.upper() |
93 | - usagestr += self._list_subcmds() |
94 | return usagestr |
95 | |
96 | def _desc(self): |
97 | @@ -93,29 +90,67 @@ |
98 | description += docstr + "\n" |
99 | return description |
100 | |
101 | - def _list_subcmds(self): |
102 | - str = "" |
103 | - if self.subcmds: |
104 | - str = "\n\nSub-Commands:" |
105 | - for cmd in self.subcmds.keys(): |
106 | - str += "\n " + cmd |
107 | - return str |
108 | - |
109 | def help(self): |
110 | #For some reason, format_help includes an extra \n |
111 | return self.parser.format_help()[:-1] |
112 | |
113 | + def get_subcommand(self, name): |
114 | + return None |
115 | + |
116 | + |
117 | +class AbrekCmdWithSubcommands(AbrekCmd): |
118 | + |
119 | + arglist = ['subcommand'] |
120 | + |
121 | + def main(self, argv): |
122 | + if not argv: |
123 | + print "Missing sub-command." + self._list_subcmds() |
124 | + else: |
125 | + subcmd = self.get_subcommand(argv[0]) |
126 | + if subcmd is None: |
127 | + # This line might print the help and raise SystemExit if |
128 | + # --help is passed or if an invalid option was passed. |
129 | + opts, args = self.parser.parse_args(argv) |
130 | + # If it didn't, complain. |
131 | + print "'%s' not found as a sub-command of '%s'" % ( |
132 | + args[0], self.name()) + self._list_subcmds() |
133 | + else: |
134 | + return subcmd.main(argv[1:]) |
135 | + |
136 | + def get_subcommand(self, name): |
137 | + subcmd_cls = getattr(self, 'cmd_' + name.replace('_', '-'), None) |
138 | + if subcmd_cls is None: |
139 | + return None |
140 | + return subcmd_cls(self.name() + ' ') |
141 | + |
142 | + def _usage(self): |
143 | + usagestr = AbrekCmd._usage(self) |
144 | + usagestr += self._list_subcmds() |
145 | + return usagestr |
146 | + |
147 | + def _list_subcmds(self): |
148 | + subcmds = [] |
149 | + for attrname in self.__class__.__dict__.keys(): |
150 | + if attrname.startswith('cmd_'): |
151 | + subcmds.append(_convert_command_name(attrname)) |
152 | + if not subcmds: |
153 | + return '' |
154 | + return "\n\nAvailable sub-commands:\n " + "\n ".join(subcmds) |
155 | + |
156 | + |
157 | def _convert_command_name(cmd): |
158 | return cmd[4:].replace('_','-') |
159 | |
160 | + |
161 | def _find_commands(module): |
162 | cmds = {} |
163 | for name, func in module.__dict__.iteritems(): |
164 | if name.startswith("cmd_"): |
165 | real_name = _convert_command_name(name) |
166 | - cmds[real_name] = func |
167 | + cmds[real_name] = func() |
168 | return cmds |
169 | |
170 | + |
171 | def get_all_cmds(): |
172 | from abrek import builtins, dashboard, results |
173 | cmds = _find_commands(builtins) |
174 | @@ -123,9 +158,7 @@ |
175 | cmds.update(_find_commands(results)) |
176 | return cmds |
177 | |
178 | + |
179 | def get_command(cmd_name): |
180 | cmds = get_all_cmds() |
181 | - try: |
182 | - return cmds[cmd_name]() |
183 | - except KeyError: |
184 | - return None |
185 | + return cmds.get(cmd_name) |
186 | |
187 | === modified file 'abrek/dashboard.py' |
188 | --- abrek/dashboard.py 2010-10-07 19:34:16 +0000 |
189 | +++ abrek/dashboard.py 2010-10-12 02:45:55 +0000 |
190 | @@ -24,7 +24,7 @@ |
191 | from getpass import getpass |
192 | from optparse import make_option |
193 | |
194 | -from abrek.command import AbrekCmd |
195 | +from abrek.command import AbrekCmd, AbrekCmdWithSubcommands |
196 | from abrek.config import get_config |
197 | from abrek.testdef import testloader |
198 | |
199 | @@ -92,82 +92,87 @@ |
200 | self.dashboardconf.write(fd) |
201 | |
202 | |
203 | -class subcmd_dashboard_setup(AbrekCmd): |
204 | - """ |
205 | - Configure information needed to push results to the dashboard |
206 | - """ |
207 | - options = [make_option("-u", "--user", dest="user"), |
208 | - make_option("-p", "--password", dest="password")] |
209 | - arglist = ["*server"] |
210 | - |
211 | - def run(self): |
212 | - if len(self.args) != 1: |
213 | - print "You must specify a server" |
214 | - sys.exit(1) |
215 | - config = DashboardConfig() |
216 | - if self.opts.user: |
217 | - user = self.opts.user |
218 | - else: |
219 | - user = raw_input("Username: ") |
220 | - if self.opts.password: |
221 | - password = self.opts.password |
222 | - else: |
223 | - password = getpass() |
224 | - config.host = self.args[0] |
225 | - config.user = user |
226 | - config.password = password |
227 | - config.write() |
228 | - |
229 | - |
230 | -class subcmd_dashboard_put(AbrekCmd): |
231 | - """ |
232 | - Push the results from a test to the server |
233 | - The stream name must include slashes (e.g. /anonymous/foo/) |
234 | - """ |
235 | - arglist = ["*stream", "*result"] |
236 | - |
237 | - def run(self): |
238 | - if len(self.args) != 2: |
239 | - print "You must specify a stream and a result" |
240 | - sys.exit(1) |
241 | - stream_name = self.args[0] |
242 | - result_name = self.args[1] |
243 | - bundle = generate_bundle(result_name) |
244 | - db_config = DashboardConfig() |
245 | - hosturl = urllib.basejoin(db_config.host, "xml-rpc/") |
246 | - try: |
247 | - server = xmlrpclib.Server(hosturl) |
248 | - except IOError: |
249 | - print "Error connecting to server, please run 'abrek " \ |
250 | - "dashboard setup [host]'" |
251 | - sys.exit(1) |
252 | - try: |
253 | - result = server.put(json.dumps(bundle, indent=2), result_name, |
254 | - stream_name) |
255 | - print "Bundle successfully uploaded to id: %s" % result |
256 | - except xmlrpclib.Fault as strerror: |
257 | - print "Error uploading bundle: %s" % strerror.faultString |
258 | - sys.exit(1) |
259 | - except socket.error as strerror: |
260 | - print "Unable to connect to host: %s" % strerror |
261 | - sys.exit(1) |
262 | - except xmlrpclib.ProtocolError as strerror: |
263 | - print "Connection error: %s" % strerror |
264 | - sys.exit(1) |
265 | - |
266 | - |
267 | -class subcmd_dashboard_bundle(AbrekCmd): |
268 | - """ |
269 | - Print JSON output that can be imported into the dashboard |
270 | - """ |
271 | - arglist = ["*result"] |
272 | - |
273 | - def run(self): |
274 | - if len(self.args) != 1: |
275 | - print "You must specify a result" |
276 | - sys.exit(1) |
277 | - bundle = generate_bundle(self.args[0]) |
278 | - print json.dumps(bundle, indent=2) |
279 | +class cmd_dashboard(AbrekCmdWithSubcommands): |
280 | + """ |
281 | + Connect to the Launch-control dashboard |
282 | + """ |
283 | + |
284 | + class cmd_setup(AbrekCmd): |
285 | + """ |
286 | + Configure information needed to push results to the dashboard |
287 | + """ |
288 | + options = [make_option("-u", "--user", dest="user"), |
289 | + make_option("-p", "--password", dest="password")] |
290 | + arglist = ["*server"] |
291 | + |
292 | + def run(self): |
293 | + if len(self.args) != 1: |
294 | + print "You must specify a server" |
295 | + sys.exit(1) |
296 | + config = DashboardConfig() |
297 | + if self.opts.user: |
298 | + user = self.opts.user |
299 | + else: |
300 | + user = raw_input("Username: ") |
301 | + if self.opts.password: |
302 | + password = self.opts.password |
303 | + else: |
304 | + password = getpass() |
305 | + config.host = self.args[0] |
306 | + config.user = user |
307 | + config.password = password |
308 | + config.write() |
309 | + |
310 | + |
311 | + class cmd_put(AbrekCmd): |
312 | + """ |
313 | + Push the results from a test to the server |
314 | + The stream name must include slashes (e.g. /anonymous/foo/) |
315 | + """ |
316 | + arglist = ["*stream", "*result"] |
317 | + |
318 | + def run(self): |
319 | + if len(self.args) != 2: |
320 | + print "You must specify a stream and a result" |
321 | + sys.exit(1) |
322 | + stream_name = self.args[0] |
323 | + result_name = self.args[1] |
324 | + bundle = generate_bundle(result_name) |
325 | + db_config = DashboardConfig() |
326 | + hosturl = urllib.basejoin(db_config.host, "xml-rpc/") |
327 | + try: |
328 | + server = xmlrpclib.Server(hosturl) |
329 | + except IOError: |
330 | + print "Error connecting to server, please run 'abrek " \ |
331 | + "dashboard setup [host]'" |
332 | + sys.exit(1) |
333 | + try: |
334 | + result = server.put(json.dumps(bundle, indent=2), result_name, |
335 | + stream_name) |
336 | + print "Bundle successfully uploaded to id: %s" % result |
337 | + except xmlrpclib.Fault as strerror: |
338 | + print "Error uploading bundle: %s" % strerror.faultString |
339 | + sys.exit(1) |
340 | + except socket.error as strerror: |
341 | + print "Unable to connect to host: %s" % strerror |
342 | + sys.exit(1) |
343 | + except xmlrpclib.ProtocolError as strerror: |
344 | + print "Connection error: %s" % strerror |
345 | + sys.exit(1) |
346 | + |
347 | + |
348 | + class cmd_bundle(AbrekCmd): |
349 | + """ |
350 | + Print JSON output that can be imported into the dashboard |
351 | + """ |
352 | + arglist = ["*result"] |
353 | + |
354 | + def run(self): |
355 | + if len(self.args) != 1: |
356 | + print "You must specify a result" |
357 | + sys.exit(1) |
358 | + bundle = generate_bundle(self.args[0]) |
359 | + print json.dumps(bundle, indent=2) |
360 | |
361 | |
362 | def generate_bundle(result): |
363 | @@ -188,10 +193,3 @@ |
364 | return testdata |
365 | |
366 | |
367 | -class cmd_dashboard(AbrekCmd): |
368 | - """ |
369 | - Connect to the Launch-control dashboard |
370 | - """ |
371 | - subcmds = {'bundle':subcmd_dashboard_bundle(), |
372 | - 'put':subcmd_dashboard_put(), |
373 | - 'setup':subcmd_dashboard_setup()} |
374 | |
375 | === modified file 'abrek/main.py' |
376 | --- abrek/main.py 2010-09-15 20:32:29 +0000 |
377 | +++ abrek/main.py 2010-10-12 02:45:55 +0000 |
378 | @@ -13,12 +13,10 @@ |
379 | # You should have received a copy of the GNU General Public License |
380 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
381 | |
382 | -import sys |
383 | import abrek.command |
384 | |
385 | |
386 | def main(argv): |
387 | - origargv = argv |
388 | argv = argv[1:] |
389 | if not argv: |
390 | argv = ['help'] |
391 | @@ -27,9 +25,4 @@ |
392 | if not cmd_func: |
393 | print "command '%s' not found" % cmd |
394 | return 1 |
395 | - main = cmd_func.main |
396 | - try: |
397 | - main(argv) |
398 | - except NotImplementedError: |
399 | - print >>sys.stderr, "Unknown usage '%s'" % " ".join(origargv) |
400 | - print >>sys.stderr, "Use 'abrek help [cmd]' for help" |
401 | + return cmd_func.main(argv) |
402 | |
403 | === modified file 'abrek/results.py' |
404 | --- abrek/results.py 2010-09-28 18:53:14 +0000 |
405 | +++ abrek/results.py 2010-10-12 02:45:55 +0000 |
406 | @@ -18,94 +18,91 @@ |
407 | import sys |
408 | from optparse import make_option |
409 | |
410 | -from abrek.command import AbrekCmd |
411 | +from abrek.command import AbrekCmd, AbrekCmdWithSubcommands |
412 | from abrek.config import get_config |
413 | from abrek.utils import read_file |
414 | |
415 | |
416 | -class subcmd_results_list(AbrekCmd): |
417 | - """ |
418 | - List results of previous runs |
419 | - """ |
420 | - def run(self): |
421 | - config = get_config() |
422 | - print "Saved results:" |
423 | - try: |
424 | - for dir in os.listdir(config.resultsdir): |
425 | - print dir |
426 | - except OSError: |
427 | - print "No results found" |
428 | - |
429 | - |
430 | -class subcmd_results_show(AbrekCmd): |
431 | - """ |
432 | - Display the output from a previous test run |
433 | - """ |
434 | - arglist = ['*result'] |
435 | - def run(self): |
436 | - if len(self.args) != 1: |
437 | - print "please specify the name of the result dir" |
438 | - sys.exit(1) |
439 | - config = get_config() |
440 | - resultsdir = os.path.join(config.resultsdir, self.args[0]) |
441 | - testoutput = os.path.join(resultsdir, "testoutput.log") |
442 | - if not os.path.exists(testoutput): |
443 | - print "No result found for '%s'" % self.args[0] |
444 | - sys.exit(1) |
445 | - print(read_file(testoutput)) |
446 | - |
447 | - |
448 | -class subcmd_results_remove(AbrekCmd): |
449 | - """ |
450 | - Remove the results of a previous test run |
451 | - """ |
452 | - arglist = ['*result'] |
453 | - options = [make_option('-f', '--force', action='store_true', |
454 | - dest='force')] |
455 | - def run(self): |
456 | - if len(self.args) != 1: |
457 | - print "please specify the name of the result dir" |
458 | - sys.exit(1) |
459 | - config = get_config() |
460 | - resultsdir = os.path.join(config.resultsdir, self.args[0]) |
461 | - if not os.path.exists(resultsdir): |
462 | - print "No result found for '%s'" % self.args[0] |
463 | - sys.exit(1) |
464 | - if not self.opts.force: |
465 | - print "Delete result '%s' for good? [Y/N]" % self.args[0], |
466 | - response = raw_input() |
467 | - if response[0].upper() != 'Y': |
468 | - sys.exit(0) |
469 | - shutil.rmtree(resultsdir) |
470 | - |
471 | - |
472 | -class subcmd_results_rename(AbrekCmd): |
473 | - """ |
474 | - Rename the results from a previous test run |
475 | - """ |
476 | - arglist = ['*source', '*destination'] |
477 | - |
478 | - def run(self): |
479 | - if len(self.args) != 2: |
480 | - print "please specify the name of the result, and the new name" |
481 | - sys.exit(1) |
482 | - config = get_config() |
483 | - srcdir = os.path.join(config.resultsdir, self.args[0]) |
484 | - destdir = os.path.join(config.resultsdir, self.args[1]) |
485 | - if not os.path.exists(srcdir): |
486 | - print "Result directory not found" |
487 | - sys.exit(1) |
488 | - if os.path.exists(destdir): |
489 | - print "Destination result name already exists" |
490 | - sys.exit(1) |
491 | - shutil.move(srcdir, destdir) |
492 | - |
493 | - |
494 | -class cmd_results(AbrekCmd): |
495 | +class cmd_results(AbrekCmdWithSubcommands): |
496 | """ |
497 | Operate on results of previous test runs stored locally |
498 | """ |
499 | - subcmds = {'list':subcmd_results_list(), |
500 | - 'rm':subcmd_results_remove(), |
501 | - 'rename':subcmd_results_rename(), |
502 | - 'show':subcmd_results_show()} |
503 | + |
504 | + class cmd_list(AbrekCmd): |
505 | + """ |
506 | + List results of previous runs |
507 | + """ |
508 | + def run(self): |
509 | + config = get_config() |
510 | + print "Saved results:" |
511 | + try: |
512 | + for dir in os.listdir(config.resultsdir): |
513 | + print dir |
514 | + except OSError: |
515 | + print "No results found" |
516 | + |
517 | + |
518 | + class cmd_show(AbrekCmd): |
519 | + """ |
520 | + Display the output from a previous test run |
521 | + """ |
522 | + arglist = ['*result'] |
523 | + def run(self): |
524 | + if len(self.args) != 1: |
525 | + print "please specify the name of the result dir" |
526 | + sys.exit(1) |
527 | + config = get_config() |
528 | + resultsdir = os.path.join(config.resultsdir, self.args[0]) |
529 | + testoutput = os.path.join(resultsdir, "testoutput.log") |
530 | + if not os.path.exists(testoutput): |
531 | + print "No result found for '%s'" % self.args[0] |
532 | + sys.exit(1) |
533 | + print(read_file(testoutput)) |
534 | + |
535 | + |
536 | + class cmd_remove(AbrekCmd): |
537 | + """ |
538 | + Remove the results of a previous test run |
539 | + """ |
540 | + arglist = ['*result'] |
541 | + options = [make_option('-f', '--force', action='store_true', |
542 | + dest='force')] |
543 | + def run(self): |
544 | + if len(self.args) != 1: |
545 | + print "please specify the name of the result dir" |
546 | + sys.exit(1) |
547 | + config = get_config() |
548 | + resultsdir = os.path.join(config.resultsdir, self.args[0]) |
549 | + if not os.path.exists(resultsdir): |
550 | + print "No result found for '%s'" % self.args[0] |
551 | + sys.exit(1) |
552 | + if not self.opts.force: |
553 | + print "Delete result '%s' for good? [Y/N]" % self.args[0], |
554 | + response = raw_input() |
555 | + if response[0].upper() != 'Y': |
556 | + sys.exit(0) |
557 | + shutil.rmtree(resultsdir) |
558 | + |
559 | + |
560 | + class cmd_rename(AbrekCmd): |
561 | + """ |
562 | + Rename the results from a previous test run |
563 | + """ |
564 | + arglist = ['*source', '*destination'] |
565 | + |
566 | + def run(self): |
567 | + if len(self.args) != 2: |
568 | + print "please specify the name of the result, and the new name" |
569 | + sys.exit(1) |
570 | + config = get_config() |
571 | + srcdir = os.path.join(config.resultsdir, self.args[0]) |
572 | + destdir = os.path.join(config.resultsdir, self.args[1]) |
573 | + if not os.path.exists(srcdir): |
574 | + print "Result directory not found" |
575 | + sys.exit(1) |
576 | + if os.path.exists(destdir): |
577 | + print "Destination result name already exists" |
578 | + sys.exit(1) |
579 | + shutil.move(srcdir, destdir) |
580 | + |
581 | + |
582 | |
583 | === modified file 'tests/test_abrekcmd.py' |
584 | --- tests/test_abrekcmd.py 2010-09-10 17:09:14 +0000 |
585 | +++ tests/test_abrekcmd.py 2010-10-12 02:45:55 +0000 |
586 | @@ -15,7 +15,12 @@ |
587 | |
588 | import unittest |
589 | from optparse import make_option |
590 | -from abrek.command import AbrekCmd, get_command, get_all_cmds |
591 | +from abrek.command import ( |
592 | + AbrekCmd, |
593 | + AbrekCmdWithSubcommands, |
594 | + get_command, |
595 | + get_all_cmds, |
596 | + ) |
597 | |
598 | |
599 | class testAbrekCmd(unittest.TestCase): |
600 | @@ -72,40 +77,60 @@ |
601 | self.assertTrue(expected_str in cmd.help()) |
602 | |
603 | def test_subcmds(self): |
604 | - expected_str = 'Sub-Commands:\n foo' |
605 | - class subcmd_test(AbrekCmd): |
606 | - pass |
607 | + expected_str = 'Available sub-commands:\n foo' |
608 | |
609 | - class cmd_test_subcmds(AbrekCmd): |
610 | - subcmds = {'foo':subcmd_test()} |
611 | - pass |
612 | + class cmd_test_subcmds(AbrekCmdWithSubcommands): |
613 | + """Help for test-subcmds.""" |
614 | + class cmd_foo(AbrekCmd): |
615 | + pass |
616 | cmd = cmd_test_subcmds() |
617 | - self.assertTrue(expected_str in cmd.help()) |
618 | + self.assertTrue( |
619 | + expected_str in cmd.help() |
620 | + and 'Help for test-subcmds.' in cmd.help()) |
621 | |
622 | def test_subcmds_run(self): |
623 | expected_str = "subcmd test str" |
624 | - class subcmd_test(AbrekCmd): |
625 | - def run(self): |
626 | - return expected_str |
627 | |
628 | - class cmd_test_subcmds(AbrekCmd): |
629 | - subcmds = {'foo':subcmd_test()} |
630 | - pass |
631 | + class cmd_test_subcmds(AbrekCmdWithSubcommands): |
632 | + class cmd_foo(AbrekCmd): |
633 | + def run(self): |
634 | + return expected_str |
635 | cmd = cmd_test_subcmds() |
636 | argv = ['foo'] |
637 | self.assertEqual(expected_str, cmd.main(argv)) |
638 | |
639 | + def test_subcmds_name(self): |
640 | + expected_str = "subcmd test str" |
641 | + |
642 | + class cmd_test_subcmds(AbrekCmdWithSubcommands): |
643 | + class cmd_foo(AbrekCmd): |
644 | + def run(self): |
645 | + return expected_str |
646 | + cmd = cmd_test_subcmds().get_subcommand('foo') |
647 | + self.assertEqual('test-subcmds foo', cmd.name()) |
648 | + |
649 | + def test_subcmds_help(self): |
650 | + expected_str = "subcmd test str" |
651 | + |
652 | + class cmd_test_subcmds(AbrekCmdWithSubcommands): |
653 | + class cmd_foo(AbrekCmd): |
654 | + """Help for foo.""" |
655 | + def run(self): |
656 | + return expected_str |
657 | + cmd = cmd_test_subcmds().get_subcommand('foo') |
658 | + self.assertTrue( |
659 | + 'test-subcmds foo' in cmd.help() |
660 | + and 'Help for foo.' in cmd.help()) |
661 | + |
662 | def test_subcmd_strip_argv(self): |
663 | """ |
664 | Make sure that the argv list is stripped after calling the subcmd |
665 | """ |
666 | - class subcmd_test(AbrekCmd): |
667 | - def main(self, argv): |
668 | - return len(argv) |
669 | |
670 | - class cmd_test_subcmds(AbrekCmd): |
671 | - subcmds = {'foo':subcmd_test()} |
672 | - pass |
673 | + class cmd_test_subcmds(AbrekCmdWithSubcommands): |
674 | + class cmd_foo(AbrekCmd): |
675 | + def main(self, argv): |
676 | + return len(argv) |
677 | cmd = cmd_test_subcmds() |
678 | argv = ['foo'] |
679 | self.assertEqual(0, cmd.main(argv)) |
680 | |
681 | === modified file 'tests/test_builtins.py' |
682 | --- tests/test_builtins.py 2010-09-10 17:09:14 +0000 |
683 | +++ tests/test_builtins.py 2010-10-12 02:45:55 +0000 |
684 | @@ -36,3 +36,31 @@ |
685 | cmd = abrek.builtins.cmd_list_installed() |
686 | cmd.run() |
687 | self.assertTrue(test_name in out.getvalue()) |
688 | + |
689 | +class TestHelp(TestCaseWithFixtures): |
690 | + |
691 | + def test_command_help(self): |
692 | + out = self.add_fixture(OutputImposter()) |
693 | + abrek.builtins.cmd_help().main(['results']) |
694 | + self.assertEqual( |
695 | + abrek.results.cmd_results().help() + '\n', out.getvalue()) |
696 | + |
697 | + def test_subcommand_help(self): |
698 | + out = self.add_fixture(OutputImposter()) |
699 | + abrek.builtins.cmd_help().main(['results', 'list']) |
700 | + self.assertEqual( |
701 | + abrek.results.cmd_results().get_subcommand('list').help() + '\n', |
702 | + out.getvalue()) |
703 | + |
704 | + def test_bad_command(self): |
705 | + out = self.add_fixture(OutputImposter()) |
706 | + abrek.builtins.cmd_help().main(['foo']) |
707 | + self.assertEqual( |
708 | + "No command found for 'foo'\n", out.getvalue()) |
709 | + |
710 | + def test_bad_subcommand(self): |
711 | + out = self.add_fixture(OutputImposter()) |
712 | + abrek.builtins.cmd_help().main(['results', 'foo']) |
713 | + self.assertEqual( |
714 | + "No sub-command of 'results' found for 'foo'\n", |
715 | + out.getvalue()) |
716 | |
717 | === modified file 'tests/test_dashboard.py' |
718 | --- tests/test_dashboard.py 2010-10-07 17:11:55 +0000 |
719 | +++ tests/test_dashboard.py 2010-10-12 02:45:55 +0000 |
720 | @@ -16,10 +16,10 @@ |
721 | import json |
722 | import os |
723 | from uuid import uuid1 |
724 | -from abrek.dashboard import (DashboardConfig, |
725 | - cmd_dashboard, |
726 | - subcmd_dashboard_bundle, |
727 | - subcmd_dashboard_setup) |
728 | +from abrek.dashboard import ( |
729 | + DashboardConfig, |
730 | + cmd_dashboard, |
731 | + ) |
732 | from imposters import ConfigImposter, OutputImposter |
733 | from fixtures import TestCaseWithFixtures |
734 | |
735 | @@ -44,7 +44,7 @@ |
736 | |
737 | def test_dashboard_setup_noserver(self): |
738 | errmsg = "You must specify a server" |
739 | - cmd = subcmd_dashboard_setup() |
740 | + cmd = cmd_dashboard.cmd_setup() |
741 | self.assertRaises(SystemExit, cmd.main, argv=[]) |
742 | self.assertEqual(errmsg, self.out.getvalue().strip()) |
743 | |
744 | @@ -56,13 +56,13 @@ |
745 | |
746 | def test_dashboard_bundle_badresult(self): |
747 | errmsg = "Result directory not found" |
748 | - cmd = subcmd_dashboard_bundle() |
749 | + cmd = cmd_dashboard.cmd_bundle() |
750 | self.assertRaises(SystemExit, cmd.main, argv=['badresult']) |
751 | self.assertEqual(errmsg, self.out.getvalue().strip()) |
752 | |
753 | def test_dashboard_bundle_noresult(self): |
754 | errmsg = "You must specify a result" |
755 | - cmd = subcmd_dashboard_bundle() |
756 | + cmd = cmd_dashboard.cmd_bundle() |
757 | self.assertRaises(SystemExit, cmd.main, argv=[]) |
758 | self.assertEqual(errmsg, self.out.getvalue().strip()) |
759 | |
760 | @@ -73,7 +73,7 @@ |
761 | self.out = self.add_fixture(OutputImposter()) |
762 | |
763 | def test_dashboard_bundle_good(self): |
764 | - cmd = subcmd_dashboard_bundle() |
765 | + cmd = cmd_dashboard.cmd_bundle() |
766 | (testname, testuuid) = make_stream_result(self.config) |
767 | expected_dict = { |
768 | "test_runs": [{ |
769 | |
770 | === modified file 'tests/test_main.py' |
771 | --- tests/test_main.py 2010-09-18 04:07:37 +0000 |
772 | +++ tests/test_main.py 2010-10-12 02:45:55 +0000 |
773 | @@ -24,7 +24,9 @@ |
774 | self.out = self.add_fixture(OutputImposter()) |
775 | |
776 | def test_bad_subcmd(self): |
777 | - errmsg = "Unknown usage './abrek results foo'\nUse 'abrek help [cmd]' for help\n" |
778 | + # Running a subcommand that does not exist of a command that does |
779 | + # gives a nice error message. |
780 | + errmsg = "'foo' not found as a sub-command of 'results'" |
781 | main(['./abrek', 'results', 'foo']) |
782 | - self.assertEqual(errmsg, self.out.getvalue()) |
783 | + self.assertEqual(errmsg, self.out.getvalue().splitlines()[0]) |
784 | |
785 | |
786 | === modified file 'tests/test_results.py' |
787 | --- tests/test_results.py 2010-09-10 17:09:14 +0000 |
788 | +++ tests/test_results.py 2010-10-12 02:45:55 +0000 |
789 | @@ -15,7 +15,7 @@ |
790 | |
791 | import os |
792 | |
793 | -import abrek.results |
794 | +from abrek.results import cmd_results |
795 | from abrek.utils import write_file |
796 | from imposters import ConfigImposter, OutputImposter |
797 | from fixtures import TestCaseWithFixtures |
798 | @@ -30,13 +30,13 @@ |
799 | def test_results_list(self): |
800 | result_name = "test_results_list000" |
801 | os.makedirs(os.path.join(self.config.resultsdir, result_name)) |
802 | - cmd = abrek.results.subcmd_results_list() |
803 | + cmd = cmd_results.cmd_list() |
804 | cmd.run() |
805 | self.assertTrue(result_name in self.out.getvalue()) |
806 | |
807 | def test_results_list_nodir(self): |
808 | errmsg = "No results found" |
809 | - cmd = abrek.results.subcmd_results_list() |
810 | + cmd = cmd_results.cmd_list() |
811 | cmd.run() |
812 | self.assertTrue(errmsg in self.out.getvalue()) |
813 | |
814 | @@ -47,20 +47,20 @@ |
815 | os.makedirs(result_dir) |
816 | outputfile = os.path.join(result_dir, 'testoutput.log') |
817 | write_file(result_output, outputfile) |
818 | - cmd = abrek.results.subcmd_results_show() |
819 | + cmd = cmd_results.cmd_show() |
820 | cmd.main(argv=[result_name]) |
821 | self.assertEqual(result_output, self.out.getvalue().strip()) |
822 | |
823 | def test_results_show_noarg(self): |
824 | errmsg = "please specify the name of the result dir" |
825 | - cmd = abrek.results.subcmd_results_show() |
826 | + cmd = cmd_results.cmd_show() |
827 | self.assertRaises(SystemExit, cmd.main, argv=[]) |
828 | self.assertEqual(errmsg, self.out.getvalue().strip()) |
829 | |
830 | def test_results_show_nodir(self): |
831 | testname = "foo" |
832 | errmsg = "No result found for '%s'" % testname |
833 | - cmd = abrek.results.subcmd_results_show() |
834 | + cmd = cmd_results.cmd_show() |
835 | self.assertRaises(SystemExit, cmd.main, argv=[testname]) |
836 | self.assertEqual(errmsg, self.out.getvalue().strip()) |
837 | |
838 | @@ -68,20 +68,20 @@ |
839 | result_name = "test_results_remove000" |
840 | result_dir = os.path.join(self.config.resultsdir, result_name) |
841 | os.makedirs(result_dir) |
842 | - cmd = abrek.results.subcmd_results_remove() |
843 | + cmd = cmd_results.cmd_remove() |
844 | cmd.main(argv=[result_name, '-f']) |
845 | self.assertFalse(os.path.exists(result_dir)) |
846 | |
847 | def test_results_remove_noarg(self): |
848 | errmsg = "please specify the name of the result dir" |
849 | - cmd = abrek.results.subcmd_results_remove() |
850 | + cmd = cmd_results.cmd_remove() |
851 | self.assertRaises(SystemExit, cmd.main, argv=[]) |
852 | self.assertEqual(errmsg, self.out.getvalue().strip()) |
853 | |
854 | def test_results_remove_nodir(self): |
855 | testname = "foo" |
856 | errmsg = "No result found for '%s'" % testname |
857 | - cmd = abrek.results.subcmd_results_remove() |
858 | + cmd = cmd_results.cmd_remove() |
859 | self.assertRaises(SystemExit, cmd.main, argv=[testname]) |
860 | self.assertEqual(errmsg, self.out.getvalue().strip()) |
861 | |
862 | @@ -91,7 +91,7 @@ |
863 | result_srcdir = os.path.join(self.config.resultsdir, result_src) |
864 | result_destdir = os.path.join(self.config.resultsdir, result_dest) |
865 | os.makedirs(result_srcdir) |
866 | - cmd = abrek.results.subcmd_results_rename() |
867 | + cmd = cmd_results.cmd_rename() |
868 | cmd.main(argv=[result_src, result_dest]) |
869 | self.assertFalse(os.path.exists(result_srcdir)) |
870 | self.assertTrue(os.path.exists(result_destdir)) |
871 | @@ -100,7 +100,7 @@ |
872 | errmsg = "Result directory not found" |
873 | result_src = "test_results_old" |
874 | result_dest = "test_results_new" |
875 | - cmd = abrek.results.subcmd_results_rename() |
876 | + cmd = cmd_results.cmd_rename() |
877 | self.assertRaises(SystemExit, cmd.main, argv=[result_src, result_dest]) |
878 | self.assertEqual(errmsg, self.out.getvalue().strip()) |
879 | |
880 | @@ -112,6 +112,6 @@ |
881 | result_destdir = os.path.join(self.config.resultsdir, result_dest) |
882 | os.makedirs(result_srcdir) |
883 | os.makedirs(result_destdir) |
884 | - cmd = abrek.results.subcmd_results_rename() |
885 | + cmd = cmd_results.cmd_rename() |
886 | self.assertRaises(SystemExit, cmd.main, argv=[result_src, result_dest]) |
887 | self.assertEqual(errmsg, self.out.getvalue().strip()) |
I like it, much nicer. Thanks a lot Michael!