Commits

John Mulligan committed 8f477e7

commander: test main dispatch and errors

  • Participants
  • Parent commits 06bae9d

Comments (0)

Files changed (2)

File test/test_cli_commands.py

 
 import unittest
+import StringIO
+import sys
 
 from vanity import commander
 
     """
     return 1
 
+
 def cmd2(**opts):
     return 2
 
+
 def cmd3(**opts):
     return 3
 
         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):

File vanity/commander.py

 
 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"""
     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'