diff options
| author | ?ric Araujo <merwok@netwok.org> | 2011-09-18 23:56:21 +0200 |
|---|---|---|
| committer | ?ric Araujo <merwok@netwok.org> | 2011-09-18 23:56:21 +0200 |
| commit | 44a140ea34bc9a523dec50da1b25227d84a10517 (patch) | |
| tree | a16c0df89b7eed741ca1cfbc5e8a6b4fe11b0681 /distutils2/tests | |
| parent | 124b695da330e94f78e18663217df65aad2ad08e (diff) | |
| download | disutils2-44a140ea34bc9a523dec50da1b25227d84a10517.tar.gz | |
Remove some duplication in tests subpackage.
This commit adds some missing names found by pyflakes/pylint; the test
suite passed before, but we might as well have correct code. Some other
functions have been removed; running the test suite through
d2.tests.__main__ now requires Python 2.7.
There?s still quite a bit of unnecessary duplication between __init__
and support, but I won?t fix that now.
Diffstat (limited to 'distutils2/tests')
| -rw-r--r-- | distutils2/tests/__init__.py | 1 | ||||
| -rw-r--r-- | distutils2/tests/__main__.py | 3 | ||||
| -rw-r--r-- | distutils2/tests/support.py | 147 |
3 files changed, 31 insertions, 120 deletions
diff --git a/distutils2/tests/__init__.py b/distutils2/tests/__init__.py index 1ca1e74..cc8927f 100644 --- a/distutils2/tests/__init__.py +++ b/distutils2/tests/__init__.py @@ -22,6 +22,7 @@ from StringIO import StringIO here = os.path.dirname(__file__) or os.curdir verbose = 1 + def test_suite(): suite = unittest.TestSuite() for fn in os.listdir(here): diff --git a/distutils2/tests/__main__.py b/distutils2/tests/__main__.py index f9c3c67..e238ed6 100644 --- a/distutils2/tests/__main__.py +++ b/distutils2/tests/__main__.py @@ -4,8 +4,9 @@ import os import sys +from test.test_support import reap_children, reap_threads, run_unittest + from distutils2.tests import unittest -from distutils2.tests.support import reap_children, reap_threads, run_unittest @reap_threads diff --git a/distutils2/tests/support.py b/distutils2/tests/support.py index df04b67..ef4eb27 100644 --- a/distutils2/tests/support.py +++ b/distutils2/tests/support.py @@ -33,7 +33,9 @@ Existing tests should also be used as examples. """ import os +import re import sys +import errno import codecs import shutil import logging @@ -42,11 +44,6 @@ import subprocess import weakref import tempfile try: - import _thread, threading -except ImportError: - _thread = None - threading = None -try: import zlib except ImportError: zlib = None @@ -54,9 +51,17 @@ except ImportError: from distutils2.dist import Distribution from distutils2.tests import unittest -__all__ = ['LoggingCatcher', 'TempdirManager', 'EnvironRestorer', - 'DummyCommand', 'unittest', 'create_distribution', - 'skip_unless_symlink', 'requires_zlib'] +# define __all__ to make pydoc more useful +__all__ = [ + # TestCase mixins + 'LoggingCatcher', 'TempdirManager', 'EnvironRestorer', + # mocks + 'DummyCommand', 'TestDistribution', + # misc. functions and decorators + 'fake_dec', 'create_distribution', + # imported from this module for backport purposes + 'unittest', 'requires_zlib', 'skip_unless_symlink', +] logger = logging.getLogger('distutils2') @@ -297,7 +302,9 @@ try: from test.test_support import skip_unless_symlink except ImportError: skip_unless_symlink = unittest.skip( - 'requires test.support.skip_unless_symlink') + 'requires test.test_support.skip_unless_symlink') + +requires_zlib = unittest.skipUnless(zlib, 'requires zlib') def unlink(filename): @@ -308,112 +315,17 @@ def unlink(filename): if error.errno not in (errno.ENOENT, errno.ENOTDIR): raise -def _filter_suite(suite, pred): - """Recursively filter test cases in a suite based on a predicate.""" - newtests = [] - for test in suite._tests: - if isinstance(test, unittest.TestSuite): - _filter_suite(test, pred) - newtests.append(test) - else: - if pred(test): - newtests.append(test) - suite._tests = newtests - -class Error(Exception): - """Base class for regression test exceptions.""" - -class TestFailed(Error): - """Test failed.""" - - -verbose = True -failfast = False - -def _run_suite(suite): - """Run tests from a unittest.TestSuite-derived class.""" - if verbose: - runner = unittest.TextTestRunner(sys.stdout, verbosity=2, - failfast=failfast) - else: - runner = BasicTestRunner() - - result = runner.run(suite) - if not result.wasSuccessful(): - if len(result.errors) == 1 and not result.failures: - err = result.errors[0][1] - elif len(result.failures) == 1 and not result.errors: - err = result.failures[0][1] - else: - err = "multiple errors occurred" - if not verbose: err += "; run in verbose mode for details" - raise TestFailed(err) - -match_tests = None - -def run_unittest(*classes): - """Run tests from unittest.TestCase-derived classes.""" - valid_types = (unittest.TestSuite, unittest.TestCase) - suite = unittest.TestSuite() - for cls in classes: - if isinstance(cls, basestring): - if cls in sys.modules: - suite.addTest(unittest.findTestCases(sys.modules[cls])) - else: - raise ValueError("str arguments must be keys in sys.modules") - elif isinstance(cls, valid_types): - suite.addTest(cls) - else: - suite.addTest(unittest.makeSuite(cls)) - def case_pred(test): - if match_tests is None: - return True - for name in test.id().split("."): - if fnmatch.fnmatchcase(name, match_tests): - return True - return False - _filter_suite(suite, case_pred) - _run_suite(suite) - -def reap_threads(func): - """Use this function when threads are being used. This will - ensure that the threads are cleaned up even when the test fails. - If threading is unavailable this function does nothing. - """ - if not _thread: - return func +def strip_python_stderr(stderr): + """Strip the stderr of a Python process from potential debug output + emitted by the interpreter. - @wraps(func) - def decorator(*args): - key = threading_setup() - try: - return func(*args) - finally: - threading_cleanup(*key) - return decorator - -def reap_children(): - """Use this function at the end of test_main() whenever sub-processes - are started. This will help ensure that no extra children (zombies) - stick around to hog resources and create problems when looking - for refleaks. + This will typically be run on the result of the communicate() method + of a subprocess.Popen object. """ + stderr = re.sub(r"\[\d+ refs\]\r?\n?$", "", stderr).strip() + return stderr - # Reap all our dead child processes so we don't leave zombies around. - # These hog resources and might be causing some of the buildbots to die. - if hasattr(os, 'waitpid'): - any_process = -1 - while True: - try: - # This will raise an exception on Windows. That's ok. - pid, status = os.waitpid(any_process, os.WNOHANG) - if pid == 0: - break - except: - break - -requires_zlib = unittest.skipUnless(zlib, 'requires zlib') # Executing the interpreter in a subprocess def _assert_python(expected_success, *args, **env_vars): @@ -435,28 +347,25 @@ def _assert_python(expected_success, *args, **env_vars): p.stdout.close() p.stderr.close() rc = p.returncode - err = strip_python_stderr(err) + err = strip_python_stderr(err) if (rc and expected_success) or (not rc and not expected_success): raise AssertionError( "Process return code is %d, " "stderr follows:\n%s" % (rc, err.decode('ascii', 'ignore'))) return rc, out, err + def assert_python_ok(*args, **env_vars): """ Assert that running the interpreter with `args` and optional environment - variables `env_vars` is ok and return a (return code, stdout, stderr) tuple. + variables `env_vars` is ok and return a (return code, stdout, stderr) + tuple. """ return _assert_python(True, *args, **env_vars) + def unload(name): try: del sys.modules[name] except KeyError: pass - -try: - from test.test_support import skip_unless_symlink -except ImportError: - skip_unless_symlink = unittest.skip( - 'requires test.test_support.skip_unless_symlink') |
