[Python-checkins] distutils2: Factor out repeated code used to call pysetup from tests.

eric.araujo python-checkins at python.org
Mon May 21 21:56:02 CEST 2012


http://hg.python.org/distutils2/rev/1c09e0c94dd3
changeset:   1342:1c09e0c94dd3
user:        Éric Araujo <merwok at netwok.org>
date:        Sun May 20 00:45:41 2012 -0400
summary:
  Factor out repeated code used to call pysetup from tests.

The whole assert_python_* business was unfriendly for readers, now the
code needed to have the equivalent of pysetup run from tests is moved to
helper methods and the tests can have much clearer code.

Also remove Python 2.4 compatibility (using -c instead of -m).

files:
  distutils2/tests/test_run.py |  42 +++++++++--------------
  1 files changed, 17 insertions(+), 25 deletions(-)


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
@@ -62,28 +62,32 @@
 
     def get_pythonpath(self):
         pythonpath = os.environ.get('PYTHONPATH')
-        d2parent = os.path.dirname(os.path.dirname(__file__))
+        d2parent = os.path.dirname(os.path.dirname(__file__))  # XXX buggy
         if pythonpath is not None:
             pythonpath = os.pathsep.join((pythonpath, d2parent))
         else:
             pythonpath = d2parent
         return pythonpath
 
+    def call_pysetup(self, *args):
+        _, out, err = assert_python_ok('-m', 'distutils2.run', *args,
+                                       PYTHONPATH=self.get_pythonpath())
+        return out, err
+
+    def call_pysetup_fail(self, *args):
+        _, out, err = assert_python_failure('-m', 'distutils2.run', *args,
+                                            PYTHONPATH=self.get_pythonpath())
+        return out, err
+
     def test_show_help(self):
         # smoke test, just makes sure some help is displayed
-        status, out, err = assert_python_ok(
-            '-c', 'from distutils2.run import main; main()', '--help',
-            PYTHONPATH=self.get_pythonpath())
-        self.assertEqual(status, 0)
+        out, err = self.call_pysetup('--help')
         self.assertGreater(out, '')
         self.assertEqual(err, '')
 
     def test_list_commands(self):
-        status, out, err = assert_python_ok(
-            '-c', 'from distutils2.run import main; main()', 'run',
-            '--list-commands', PYTHONPATH=self.get_pythonpath())
+        out, err = self.call_pysetup('run', '--list-commands')
         # check that something is displayed
-        self.assertEqual(status, 0)
         self.assertGreater(out, '')
         self.assertEqual(err, '')
 
@@ -97,10 +101,7 @@
     # 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())
-        self.assertEqual(status, 1)
+        out, err = self.call_pysetup_fail('run', 'build', '--unknown')
         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
@@ -109,28 +110,19 @@
                          '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)
+        out, err = self.call_pysetup_fail('run', 'com#mand')
         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())
-        self.assertEqual(status, 1)
+        out, err = self.call_pysetup_fail('run', 'invalid_command')
         self.assertGreater(out, 1)
         self.assertEqual(err.splitlines()[-1],
                          "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())
-        self.assertEqual(status, 1)
+        out, err = self.call_pysetup_fail('invalid_action')
         self.assertGreater(out, 1)
         self.assertEqual(err.splitlines()[-1],
                          "error: action 'invalid_action' not recognized")

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


More information about the Python-checkins mailing list