summaryrefslogtreecommitdiff
path: root/runtests.py
blob: fa032abc807fd1db6191ea70b5175143e46f6a43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env python
"""Test runner for distutils2.

The tests for distutils2 are defined in the distutils2.tests package.
They can also be executed with the unittest2 runner or nose.
"""

import os
import sys
from os.path import dirname, islink, realpath, join, abspath
from optparse import OptionParser
from distutils2.tests import unittest


# unittest machinery copied from stdlib's test.regrtest and test.support

class TestFailed(Exception):
    """Test failed."""


class BasicTestRunner(object):
    def run(self, test):
        result = unittest.TestResult()
        test(result)
        return result


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

    # 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


def _run_suite(suite, verbose=True):
    """Run tests from a unittest.TestSuite-derived class."""
    if verbose:
        runner = unittest.TextTestRunner(sys.stdout, verbosity=2)
    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 = "errors occurred; run in verbose mode for details"
        raise TestFailed(err)


def run_unittest(classes, verbose=True):
    """Run tests from unittest.TestCase-derived classes.

    Originally extracted from stdlib test.test_support and modified to
    support unittest2.
    """
    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))
    _run_suite(suite, verbose)


def run_tests(verbose):
    # do NOT import those at the top level, coverage will be inaccurate if
    # distutils2 modules are imported before coverage magic is started
    from distutils2.tests import test_suite
    from distutils2._backport.tests import test_suite as btest_suite
    try:
        try:
            run_unittest([test_suite(), btest_suite()], verbose=verbose)
            return 0
        except TestFailed:
            return 1
    finally:
        reap_children()


# coverage-related code

COVERAGE_FILE = join(dirname(abspath(__file__)), '.coverage')


def get_coverage():
    """ Return a usable coverage object. """
    # deferred import because coverage is optional
    import coverage
    cov = getattr(coverage, "the_coverage", None)
    if not cov:
        cov = coverage.coverage(COVERAGE_FILE)
    return cov


def ignore_prefixes(module):
    """ Return a list of prefixes to ignore in the coverage report if
    we want to completely skip `module`.
    """
    # A function like that is needed because some operating systems like Debian
    # and derivatives use symlinks directory in order to save disk space
    dirnames = [dirname(module.__file__)]

    pymod = module.__file__.rstrip('co')
    if islink(pymod):
        dirnames.append(dirname(realpath(pymod)))
    return dirnames


def coverage_report(opts):
    from distutils2.tests.support import unittest
    cov = get_coverage()
    if hasattr(cov, "load"):
        # running coverage 3.x
        cov.load()
        # morfs means modules or files
        morfs = None
    else:
        # running coverage 2.x
        cov.cache = COVERAGE_FILE
        cov.restore()
        morfs = [m for m in cov.cexecuted if "distutils2" in m]

    prefixes = ["runtests", "distutils2/tests", "distutils2/_backport"]
    prefixes += ignore_prefixes(unittest)

    try:
        import docutils
        prefixes += ignore_prefixes(docutils)
    except ImportError:
        # that module is completely optional
        pass

    try:
        import roman
        prefixes += ignore_prefixes(roman)
    except ImportError:
        # that module is also completely optional
        pass

    try:
        cov.report(morfs,
                   omit_prefixes=prefixes,
                   show_missing=opts.show_missing)
    except TypeError:
        # Coverage 3.4 turned `omit_prefixes` into a list of globbing patterns
        cov.report(morfs,
                   omit=[p + "*" for p in prefixes],
                   show_missing=opts.show_missing)


# command-line parsing

def parse_opts():
    parser = OptionParser(usage="%prog [OPTIONS]",
                          description="run the distutils2 unittests")

    parser.add_option("-q", "--quiet", help="do not print verbose messages",
                      action="store_true", default=False)
    parser.add_option("-c", "--coverage", action="store_true", default=False,
                      help="produce a coverage report at the end of the run")
    parser.add_option("-r", "--report", action="store_true", default=False,
                      help="produce a coverage report from the last test run")
    parser.add_option("-m", "--show-missing", action="store_true",
                      default=False,
                      help=("Show line numbers of statements in each module "
                            "that weren't executed."))

    opts, args = parser.parse_args()
    return opts, args


def test_main():
    opts, args = parse_opts()
    # FIXME when we run with --quiet, we still want to see errors and failures
    verbose = not opts.quiet
    ret = 0

    if opts.coverage:
        cov = get_coverage()
        cov.erase()
        cov.start()
    if not opts.report:
        ret = run_tests(verbose)
    if opts.coverage:
        cov.stop()
        cov.save()

    if opts.report or opts.coverage:
        coverage_report(opts)

    return ret


if __name__ == "__main__":
    if sys.version_info[:2] < (2, 5):
        try:
            from distutils2._backport import hashlib
        except ImportError:
            import subprocess
            subprocess.call([sys.executable, 'setup.py', 'build_ext'])
    sys.exit(test_main())