[Python-checkins] distutils2: Harmonize error messages and add one missing test for #13339

eric.araujo python-checkins at python.org
Wed May 16 07:07:27 CEST 2012


http://hg.python.org/distutils2/rev/91ac9c36f09e
changeset:   1336:91ac9c36f09e
user:        Éric Araujo <merwok at netwok.org>
date:        Wed May 16 00:51:01 2012 -0400
summary:
  Harmonize error messages and add one missing test for #13339

files:
  distutils2/run.py            |  13 +++-----
  distutils2/tests/test_run.py |  33 +++++++++++++++--------
  2 files changed, 26 insertions(+), 20 deletions(-)


diff --git a/distutils2/run.py b/distutils2/run.py
--- a/distutils2/run.py
+++ b/distutils2/run.py
@@ -387,6 +387,7 @@
         self.parser.set_negative_aliases(negative_opt)
         # FIXME this parses everything, including command options (e.g. "run
         # build -i" errors with "option -i not recognized")
+        # FIXME unknown options are not detected
         args = self.parser.getopt(args=args, object=self)
 
         # if first arg is "run", we have some commands
@@ -397,9 +398,8 @@
 
         allowed = [action[0] for action in actions] + [None]
         if self.action not in allowed:
-            msg = 'Unrecognized action %s' % self.action
             self.show_help()
-            self.exit_with_error_msg(msg)
+            sys.exit('error: action %r not recognized' % self.action)
 
         self._set_logger()
         self.args = args
@@ -436,7 +436,7 @@
         # Pull the current command from the head of the command line
         command = args[0]
         if not command_re.match(command):
-            raise SystemExit("invalid command name %r" % (command,))
+            sys.exit('error: invalid command name %r' % command)
         self.commands.append(command)
 
         # Dig up the command class that implements this command, so we
@@ -446,7 +446,7 @@
             cmd_class = get_command_class(command)
         except PackagingModuleError, msg:
             self.show_help()
-            self.exit_with_error_msg(msg)
+            sys.exit('error: command %r not recognized' % command)
 
         # XXX We want to push this in distutils2.command
         #
@@ -491,7 +491,7 @@
             args, opts = parser.getopt(args[1:])
         except PackagingArgError, msg:
             self.show_help()
-            self.exit_with_error_msg(msg)
+            sys.exit('error: %s' % msg)
 
         if hasattr(opts, 'help') and opts.help:
             self._show_command_help(cmd_class)
@@ -536,9 +536,6 @@
     def show_help(self):
         self._show_help(self.parser)
 
-    def exit_with_error_msg(self, msg):
-        sys.exit('error: ' + msg.__str__())
-
     def print_usage(self, parser):
         parser.set_option_table(global_options)
 
diff --git a/distutils2/tests/test_run.py b/distutils2/tests/test_run.py
--- a/distutils2/tests/test_run.py
+++ b/distutils2/tests/test_run.py
@@ -94,37 +94,46 @@
         self.assertTrue(build_position, out)
         self.assertLess(check_position, build_position, out)
 
-    def test_unknown_run_option(self):
+    # TODO test that custom commands don't break --list-commands
+
+    def test_unknown_command_option(self):
         status, out, err = assert_python_failure(
             '-c', 'from distutils2.run import main; main()', 'run', 'build',
-            '--unknown', PYTHONPATH=self.get_pythonpath()
-        )
+            '--unknown', PYTHONPATH=self.get_pythonpath())
         self.assertEqual(status, 1)
         self.assertGreater(out, '')
+        # sadly this message comes straight from the getopt module and can't be
+        # modified to use repr instead of str for the unknown option; to be
+        # changed when the command line parsers are replaced by something clean
         self.assertEqual(err.splitlines()[-1],
-                        'error: option --unknown not recognized')
+                         'error: option --unknown not recognized')
+
+    def test_invalid_command(self):
+        status, out, err = assert_python_failure(
+            '-c', 'from distutils2.run import main; main()', 'run',
+            'com#mand', PYTHONPATH=self.get_pythonpath())
+        self.assertEqual(status, 1)
+        self.assertGreater(out, 1)
+        self.assertEqual(err.splitlines()[-1],
+                         "error: invalid command name 'com#mand'")
 
     def test_unknown_command(self):
         status, out, err = assert_python_failure(
             '-c', 'from distutils2.run import main; main()', 'run',
-            'invalid_command', PYTHONPATH=self.get_pythonpath()
-        )
+            'invalid_command', PYTHONPATH=self.get_pythonpath())
         self.assertEqual(status, 1)
         self.assertGreater(out, 1)
         self.assertEqual(err.splitlines()[-1],
-            'error: Invalid command invalid_command')
+                         "error: command 'invalid_command' not recognized")
 
     def test_unknown_action(self):
         status, out, err = assert_python_failure(
             '-c', 'from distutils2.run import main; main()', 'invalid_action',
-            PYTHONPATH=self.get_pythonpath()
-        )
+            PYTHONPATH=self.get_pythonpath())
         self.assertEqual(status, 1)
         self.assertGreater(out, 1)
         self.assertEqual(err.splitlines()[-1],
-            'error: Unrecognized action invalid_action')
-
-        # TODO test that custom commands don't break --list-commands
+                         "error: action 'invalid_action' not recognized")
 
 
 def test_suite():

-- 
Repository URL: http://hg.python.org/distutils2


More information about the Python-checkins mailing list