# HG changeset patch # User John Mulligan # Date 1257117968 18000 # Sun Nov 01 18:26:08 2009 -0500 # Node ID 8f477e723d7ca5b73948efd20c3e8dfb04b5eb4f # Parent 06bae9da75a5797b6d325c72a2a2d3a7fee69e8f commander: test main dispatch and errors diff --git a/test/test_cli_commands.py b/test/test_cli_commands.py --- a/test/test_cli_commands.py +++ b/test/test_cli_commands.py @@ -1,5 +1,7 @@ import unittest +import StringIO +import sys from vanity import commander @@ -16,9 +18,11 @@ """ return 1 + def cmd2(**opts): return 2 + def cmd3(**opts): return 3 @@ -206,6 +210,32 @@ tst('help zork'.split()) self.assertRaises(commander.InvalidCommand, tst, 'zork -h'.split()) + def test_main_dispatch(self): + arguments = 'foo check.txt'.split() + self.assertEqual(1, + commander.main(GOPT_A, CMDTABLE_A, arguments)) + + def test_main_dispatch_special(self): + sys.stderr = StringIO.StringIO() + try: + commander.main(GOPT_A, CMDTABLE_A, 'foo -h'.split()) + caught = False + except: + caught = True + self.assertTrue(caught) + try: + commander.main(GOPT_A, CMDTABLE_A, 'fizz'.split()) + caught = False + except: + caught = True + self.assertTrue(caught) + try: + commander.main(GOPT_A, CMDTABLE_A, 'b'.split()) + caught = False + except: + caught = True + self.assertTrue(caught) + class Capture(list): diff --git a/vanity/commander.py b/vanity/commander.py --- a/vanity/commander.py +++ b/vanity/commander.py @@ -23,11 +23,15 @@ class InvalidCommand(cli.CliError): """The CLI was given an invalid command""" - pass + def __init__(self, value): + cli.CliError.__init__(self, 'invalid command: %s' % value) + class MissingCommand(cli.CliError): """The user failed to specify a command""" - pass + def __init__(self): + cli.CliError.__init__(self, 'missing required command name') + class AmbiguousCommand(cli.CliError): """The given command was ambigouous""" @@ -216,8 +220,9 @@ def docstring(self): """Return a documentation string for the command. """ - if self.target and hasattr(self.target, '__doc__'): - return self.target.__doc__ or 'No usage available' + doc = getattr(self.target, '__doc__', None) + if doc: + return doc else: return 'No usage available'