~bzr-pqm/bzr/bzr.dev

5557.1.7 by John Arbash Meinel
Merge in the bzr.dev 5582
1
# Copyright (C) 2005-2011 Canonical Ltd
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
2
#       Author: Robert Collins <robert.collins@canonical.com>
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
17
#
18
19
import sys
20
import logging
21
import unittest
5340.15.1 by John Arbash Meinel
supersede exc-info branch
22
import weakref
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
23
5436.2.1 by Andrew Bennetts
Add bzrlib.pyutils, which has get_named_object, a wrapper around __import__.
24
from bzrlib import pyutils
25
1739.1.8 by Robert Collins
Review feedback.
26
# Mark this python module as being part of the implementation
27
# of unittest: this gives us better tracebacks where the last
28
# shown frame is the test code, not our assertXYZ.
29
__unittest = 1
30
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
31
32
class LogCollector(logging.Handler):
5574.4.2 by Vincent Ladeuil
Fix vertical spaces and lines too long.
33
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
34
    def __init__(self):
35
        logging.Handler.__init__(self)
36
        self.records=[]
5574.4.2 by Vincent Ladeuil
Fix vertical spaces and lines too long.
37
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
38
    def emit(self, record):
39
        self.records.append(record.getMessage())
40
41
42
def makeCollectingLogger():
43
    """I make a logger instance that collects its logs for programmatic analysis
44
    -> (logger, collector)"""
45
    logger=logging.Logger("collector")
46
    handler=LogCollector()
47
    handler.setFormatter(logging.Formatter("%(levelname)s: %(message)s"))
48
    logger.addHandler(handler)
49
    return logger, handler
50
51
52
def visitTests(suite, visitor):
53
    """A foreign method for visiting the tests in a test suite."""
54
    for test in suite._tests:
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
55
        #Abusing types to avoid monkey patching unittest.TestCase.
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
56
        # Maybe that would be better?
57
        try:
58
            test.visit(visitor)
59
        except AttributeError:
60
            if isinstance(test, unittest.TestCase):
61
                visitor.visitCase(test)
62
            elif isinstance(test, unittest.TestSuite):
63
                visitor.visitSuite(test)
64
                visitTests(test, visitor)
65
            else:
5574.4.2 by Vincent Ladeuil
Fix vertical spaces and lines too long.
66
                print "unvisitable non-unittest.TestCase element %r (%r)" % (
67
                    test, test.__class__)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
68
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
69
5340.16.15 by Martin
Use pseudo testcase to always fail as suggested by lifeless in review rather than hacking the subunit stream
70
class FailedCollectionCase(unittest.TestCase):
71
    """Pseudo-test to run and report failure if given case was uncollected"""
72
73
    def __init__(self, case):
74
        super(FailedCollectionCase, self).__init__("fail_uncollected")
75
        # GZ 2011-09-16: Maybe catch errors from id() method as cases may be
76
        #                in a bit of a funny state by now.
77
        self._problem_case_id = case.id()
78
79
    def id(self):
80
        if self._problem_case_id[-1:] == ")":
81
            return self._problem_case_id[:-1] + ",uncollected)"
82
        return self._problem_case_id + "(uncollected)"
83
84
    def fail_uncollected(self):
85
        self.fail("Uncollected test case: " + self._problem_case_id)
86
87
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
88
class TestSuite(unittest.TestSuite):
89
    """I am an extended TestSuite with a visitor interface.
90
    This is primarily to allow filtering of tests - and suites or
91
    more in the future. An iterator of just tests wouldn't scale..."""
92
93
    def visit(self, visitor):
94
        """visit the composite. Visiting is depth-first.
95
        current callbacks are visitSuite and visitCase."""
96
        visitor.visitSuite(self)
97
        visitTests(self, visitor)
98
4794.1.5 by Robert Collins
Free tests after executing them as an alternative way to clean up memory usage.
99
    def run(self, result):
100
        """Run the tests in the suite, discarding references after running."""
101
        tests = list(self)
102
        tests.reverse()
103
        self._tests = []
5340.15.1 by John Arbash Meinel
supersede exc-info branch
104
        stored_count = 0
5340.16.9 by Martin
Tweaks and comments
105
        count_stored_tests = getattr(result, "_count_stored_tests", int)
5340.15.1 by John Arbash Meinel
supersede exc-info branch
106
        from bzrlib.tests import selftest_debug_flags
5340.16.3 by Martin
Use 'uncollected_cases' as the flag name which seems a bit clearer
107
        notify = "uncollected_cases" in selftest_debug_flags
4794.1.5 by Robert Collins
Free tests after executing them as an alternative way to clean up memory usage.
108
        while tests:
109
            if result.shouldStop:
110
                self._tests = reversed(tests)
111
                break
5340.15.1 by John Arbash Meinel
supersede exc-info branch
112
            case = _run_and_collect_case(tests.pop(), result)()
5340.16.9 by Martin
Tweaks and comments
113
            new_stored_count = count_stored_tests()
5340.15.1 by John Arbash Meinel
supersede exc-info branch
114
            if case is not None and isinstance(case, unittest.TestCase):
115
                if stored_count == new_stored_count and notify:
116
                    # Testcase didn't fail, but somehow is still alive
5340.16.15 by Martin
Use pseudo testcase to always fail as suggested by lifeless in review rather than hacking the subunit stream
117
                    FailedCollectionCase(case).run(result)
5340.16.18 by Martin Packman
After adding a pseudo-failure update the stored test count again
118
                    # Adding a new failure so need to reupdate the count
119
                    new_stored_count = count_stored_tests()
5340.16.16 by Martin
Stop making zombie testcases for now as it's an extra risk
120
                # GZ 2011-09-16: Previously zombied the case at this point by
121
                #                clearing the dict as fallback, skip for now.
5340.15.1 by John Arbash Meinel
supersede exc-info branch
122
            stored_count = new_stored_count
4794.1.5 by Robert Collins
Free tests after executing them as an alternative way to clean up memory usage.
123
        return result
124
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
125
5340.15.1 by John Arbash Meinel
supersede exc-info branch
126
def _run_and_collect_case(case, res):
127
    """Run test case against result and use weakref to drop the refcount"""
128
    case.run(res)
129
    return weakref.ref(case)
130
131
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
132
class TestLoader(unittest.TestLoader):
2921.6.13 by Robert Collins
* Modules can now customise their tests by defining a ``load_tests``
133
    """Custom TestLoader to extend the stock python one."""
134
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
135
    suiteClass = TestSuite
3146.7.2 by Vincent Ladeuil
Review feedback. Fix variable names and scope.
136
    # Memoize test names by test class dict
137
    test_func_names = {}
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
138
1707.2.2 by Robert Collins
Start on bench_add, an add benchtest.
139
    def loadTestsFromModuleNames(self, names):
140
        """use a custom means to load tests from modules.
141
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
142
        There is an undesirable glitch in the python TestLoader where a
143
        import error is ignore. We think this can be solved by ensuring the
1707.2.2 by Robert Collins
Start on bench_add, an add benchtest.
144
        requested name is resolvable, if its not raising the original error.
145
        """
146
        result = self.suiteClass()
147
        for name in names:
3302.7.3 by Vincent Ladeuil
Prepare TestLoader for specialization.
148
            result.addTests(self.loadTestsFromModuleName(name))
149
        return result
150
151
    def loadTestsFromModuleName(self, name):
152
        result = self.suiteClass()
5436.2.1 by Andrew Bennetts
Add bzrlib.pyutils, which has get_named_object, a wrapper around __import__.
153
        module = pyutils.get_named_object(name)
3302.7.3 by Vincent Ladeuil
Prepare TestLoader for specialization.
154
155
        result.addTests(self.loadTestsFromModule(module))
1707.2.2 by Robert Collins
Start on bench_add, an add benchtest.
156
        return result
157
2921.6.13 by Robert Collins
* Modules can now customise their tests by defining a ``load_tests``
158
    def loadTestsFromModule(self, module):
159
        """Load tests from a module object.
160
161
        This extension of the python test loader looks for an attribute
162
        load_tests in the module object, and if not found falls back to the
163
        regular python loadTestsFromModule.
164
165
        If a load_tests attribute is found, it is called and the result is
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
166
        returned.
2921.6.13 by Robert Collins
* Modules can now customise their tests by defining a ``load_tests``
167
168
        load_tests should be defined like so:
169
        >>> def load_tests(standard_tests, module, loader):
170
        >>>    pass
171
172
        standard_tests is the tests found by the stock TestLoader in the
173
        module, module and loader are the module and loader instances.
174
175
        For instance, to run every test twice, you might do:
176
        >>> def load_tests(standard_tests, module, loader):
177
        >>>     result = loader.suiteClass()
178
        >>>     for test in iter_suite_tests(standard_tests):
179
        >>>         result.addTests([test, test])
180
        >>>     return result
181
        """
5340.6.3 by Martin
Less hacky spelling for avoiding unittest native load_tests, as per review by jam
182
        if sys.version_info < (2, 7):
183
            basic_tests = super(TestLoader, self).loadTestsFromModule(module)
184
        else:
185
            # GZ 2010-07-19: Python 2.7 unittest also uses load_tests but with
186
            #                a different and incompatible signature
187
            basic_tests = super(TestLoader, self).loadTestsFromModule(module,
188
                use_load_tests=False)
2921.6.13 by Robert Collins
* Modules can now customise their tests by defining a ``load_tests``
189
        load_tests = getattr(module, "load_tests", None)
190
        if load_tests is not None:
191
            return load_tests(basic_tests, module, self)
192
        else:
193
            return basic_tests
194
3146.7.2 by Vincent Ladeuil
Review feedback. Fix variable names and scope.
195
    def getTestCaseNames(self, test_case_class):
196
        test_fn_names = self.test_func_names.get(test_case_class, None)
197
        if test_fn_names is not None:
3302.7.8 by Vincent Ladeuil
Fix typos.
198
            # We already know them
3146.7.2 by Vincent Ladeuil
Review feedback. Fix variable names and scope.
199
            return test_fn_names
3146.7.1 by Vincent Ladeuil
Reduce selftest overhead to establish test names by memoization.
200
3146.7.2 by Vincent Ladeuil
Review feedback. Fix variable names and scope.
201
        test_fn_names = unittest.TestLoader.getTestCaseNames(self,
202
                                                             test_case_class)
203
        self.test_func_names[test_case_class] = test_fn_names
204
        return test_fn_names
1707.2.2 by Robert Collins
Start on bench_add, an add benchtest.
205
3302.8.2 by Vincent Ladeuil
New test loader reducing modules imports and tests loaded.
206
207
class FilteredByModuleTestLoader(TestLoader):
208
    """A test loader that import only the needed modules."""
209
210
    def __init__(self, needs_module):
211
        """Constructor.
212
213
        :param needs_module: a callable taking a module name as a
214
            parameter returing True if the module should be loaded.
215
        """
216
        TestLoader.__init__(self)
217
        self.needs_module = needs_module
218
219
    def loadTestsFromModuleName(self, name):
220
        if self.needs_module(name):
221
            return TestLoader.loadTestsFromModuleName(self, name)
222
        else:
223
            return self.suiteClass()
224
225
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
226
class TestVisitor(object):
227
    """A visitor for Tests"""
5574.4.2 by Vincent Ladeuil
Fix vertical spaces and lines too long.
228
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
229
    def visitSuite(self, aTestSuite):
230
        pass
5574.4.2 by Vincent Ladeuil
Fix vertical spaces and lines too long.
231
1185.1.29 by Robert Collins
merge merge tweaks from aaron, which includes latest .dev
232
    def visitCase(self, aTestCase):
233
        pass