bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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 logging |
|
20 |
import unittest |
|
5340.15.1
by John Arbash Meinel
supersede exc-info branch |
21 |
import weakref |
1185.1.29
by Robert Collins
merge merge tweaks from aaron, which includes latest .dev |
22 |
|
6624
by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes') |
23 |
from .. import pyutils |
5436.2.1
by Andrew Bennetts
Add bzrlib.pyutils, which has get_named_object, a wrapper around __import__. |
24 |
|
1739.1.8
by Robert Collins
Review feedback. |
25 |
# Mark this python module as being part of the implementation
|
26 |
# of unittest: this gives us better tracebacks where the last
|
|
27 |
# shown frame is the test code, not our assertXYZ.
|
|
28 |
__unittest = 1 |
|
29 |
||
1185.1.29
by Robert Collins
merge merge tweaks from aaron, which includes latest .dev |
30 |
|
31 |
class LogCollector(logging.Handler): |
|
5574.4.2
by Vincent Ladeuil
Fix vertical spaces and lines too long. |
32 |
|
1185.1.29
by Robert Collins
merge merge tweaks from aaron, which includes latest .dev |
33 |
def __init__(self): |
34 |
logging.Handler.__init__(self) |
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
35 |
self.records = [] |
5574.4.2
by Vincent Ladeuil
Fix vertical spaces and lines too long. |
36 |
|
1185.1.29
by Robert Collins
merge merge tweaks from aaron, which includes latest .dev |
37 |
def emit(self, record): |
38 |
self.records.append(record.getMessage()) |
|
39 |
||
40 |
||
41 |
def makeCollectingLogger(): |
|
42 |
"""I make a logger instance that collects its logs for programmatic analysis |
|
43 |
-> (logger, collector)"""
|
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
44 |
logger = logging.Logger("collector") |
45 |
handler = LogCollector() |
|
1185.1.29
by Robert Collins
merge merge tweaks from aaron, which includes latest .dev |
46 |
handler.setFormatter(logging.Formatter("%(levelname)s: %(message)s")) |
47 |
logger.addHandler(handler) |
|
48 |
return logger, handler |
|
49 |
||
50 |
||
51 |
def visitTests(suite, visitor): |
|
52 |
"""A foreign method for visiting the tests in a test suite.""" |
|
53 |
for test in suite._tests: |
|
7143.15.2
by Jelmer Vernooij
Run autopep8. |
54 |
# Abusing types to avoid monkey patching unittest.TestCase.
|
1185.1.29
by Robert Collins
merge merge tweaks from aaron, which includes latest .dev |
55 |
# Maybe that would be better?
|
56 |
try: |
|
57 |
test.visit(visitor) |
|
58 |
except AttributeError: |
|
59 |
if isinstance(test, unittest.TestCase): |
|
60 |
visitor.visitCase(test) |
|
61 |
elif isinstance(test, unittest.TestSuite): |
|
62 |
visitor.visitSuite(test) |
|
63 |
visitTests(test, visitor) |
|
64 |
else: |
|
6619.3.3
by Jelmer Vernooij
Apply 2to3 print fix. |
65 |
print("unvisitable non-unittest.TestCase element %r (%r)" % ( |
66 |
test, test.__class__)) |
|
3943.8.1
by Marius Kruger
remove all trailing whitespace from bzr source |
67 |
|
1185.1.29
by Robert Collins
merge merge tweaks from aaron, which includes latest .dev |
68 |
|
5340.16.15
by Martin
Use pseudo testcase to always fail as suggested by lifeless in review rather than hacking the subunit stream |
69 |
class FailedCollectionCase(unittest.TestCase): |
70 |
"""Pseudo-test to run and report failure if given case was uncollected""" |
|
71 |
||
72 |
def __init__(self, case): |
|
73 |
super(FailedCollectionCase, self).__init__("fail_uncollected") |
|
74 |
# GZ 2011-09-16: Maybe catch errors from id() method as cases may be
|
|
75 |
# in a bit of a funny state by now.
|
|
76 |
self._problem_case_id = case.id() |
|
77 |
||
78 |
def id(self): |
|
79 |
if self._problem_case_id[-1:] == ")": |
|
80 |
return self._problem_case_id[:-1] + ",uncollected)" |
|
81 |
return self._problem_case_id + "(uncollected)" |
|
82 |
||
83 |
def fail_uncollected(self): |
|
84 |
self.fail("Uncollected test case: " + self._problem_case_id) |
|
85 |
||
86 |
||
1185.1.29
by Robert Collins
merge merge tweaks from aaron, which includes latest .dev |
87 |
class TestSuite(unittest.TestSuite): |
88 |
"""I am an extended TestSuite with a visitor interface. |
|
89 |
This is primarily to allow filtering of tests - and suites or
|
|
90 |
more in the future. An iterator of just tests wouldn't scale..."""
|
|
91 |
||
92 |
def visit(self, visitor): |
|
93 |
"""visit the composite. Visiting is depth-first. |
|
94 |
current callbacks are visitSuite and visitCase."""
|
|
95 |
visitor.visitSuite(self) |
|
96 |
visitTests(self, visitor) |
|
97 |
||
4794.1.5
by Robert Collins
Free tests after executing them as an alternative way to clean up memory usage. |
98 |
def run(self, result): |
99 |
"""Run the tests in the suite, discarding references after running.""" |
|
100 |
tests = list(self) |
|
101 |
tests.reverse() |
|
102 |
self._tests = [] |
|
5340.15.1
by John Arbash Meinel
supersede exc-info branch |
103 |
stored_count = 0 |
5340.16.9
by Martin
Tweaks and comments |
104 |
count_stored_tests = getattr(result, "_count_stored_tests", int) |
6622.1.34
by Jelmer Vernooij
Rename brzlib => breezy. |
105 |
from breezy.tests import selftest_debug_flags |
5340.16.3
by Martin
Use 'uncollected_cases' as the flag name which seems a bit clearer |
106 |
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. |
107 |
while tests: |
108 |
if result.shouldStop: |
|
109 |
self._tests = reversed(tests) |
|
110 |
break
|
|
5340.15.1
by John Arbash Meinel
supersede exc-info branch |
111 |
case = _run_and_collect_case(tests.pop(), result)() |
5340.16.9
by Martin
Tweaks and comments |
112 |
new_stored_count = count_stored_tests() |
5340.15.1
by John Arbash Meinel
supersede exc-info branch |
113 |
if case is not None and isinstance(case, unittest.TestCase): |
114 |
if stored_count == new_stored_count and notify: |
|
115 |
# 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 |
116 |
FailedCollectionCase(case).run(result) |
5340.16.18
by Martin Packman
After adding a pseudo-failure update the stored test count again |
117 |
# Adding a new failure so need to reupdate the count
|
118 |
new_stored_count = count_stored_tests() |
|
5340.16.16
by Martin
Stop making zombie testcases for now as it's an extra risk |
119 |
# GZ 2011-09-16: Previously zombied the case at this point by
|
120 |
# clearing the dict as fallback, skip for now.
|
|
5340.15.1
by John Arbash Meinel
supersede exc-info branch |
121 |
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. |
122 |
return result |
123 |
||
1185.1.29
by Robert Collins
merge merge tweaks from aaron, which includes latest .dev |
124 |
|
5340.15.1
by John Arbash Meinel
supersede exc-info branch |
125 |
def _run_and_collect_case(case, res): |
126 |
"""Run test case against result and use weakref to drop the refcount""" |
|
127 |
case.run(res) |
|
128 |
return weakref.ref(case) |
|
129 |
||
130 |
||
1185.1.29
by Robert Collins
merge merge tweaks from aaron, which includes latest .dev |
131 |
class TestLoader(unittest.TestLoader): |
2921.6.13
by Robert Collins
* Modules can now customise their tests by defining a ``load_tests`` |
132 |
"""Custom TestLoader to extend the stock python one.""" |
133 |
||
1185.1.29
by Robert Collins
merge merge tweaks from aaron, which includes latest .dev |
134 |
suiteClass = TestSuite |
3146.7.2
by Vincent Ladeuil
Review feedback. Fix variable names and scope. |
135 |
# Memoize test names by test class dict
|
136 |
test_func_names = {} |
|
1185.1.29
by Robert Collins
merge merge tweaks from aaron, which includes latest .dev |
137 |
|
1707.2.2
by Robert Collins
Start on bench_add, an add benchtest. |
138 |
def loadTestsFromModuleNames(self, names): |
139 |
"""use a custom means to load tests from modules. |
|
140 |
||
3943.8.1
by Marius Kruger
remove all trailing whitespace from bzr source |
141 |
There is an undesirable glitch in the python TestLoader where a
|
142 |
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. |
143 |
requested name is resolvable, if its not raising the original error.
|
144 |
"""
|
|
145 |
result = self.suiteClass() |
|
146 |
for name in names: |
|
3302.7.3
by Vincent Ladeuil
Prepare TestLoader for specialization. |
147 |
result.addTests(self.loadTestsFromModuleName(name)) |
148 |
return result |
|
149 |
||
150 |
def loadTestsFromModuleName(self, name): |
|
151 |
result = self.suiteClass() |
|
5436.2.1
by Andrew Bennetts
Add bzrlib.pyutils, which has get_named_object, a wrapper around __import__. |
152 |
module = pyutils.get_named_object(name) |
3302.7.3
by Vincent Ladeuil
Prepare TestLoader for specialization. |
153 |
|
154 |
result.addTests(self.loadTestsFromModule(module)) |
|
1707.2.2
by Robert Collins
Start on bench_add, an add benchtest. |
155 |
return result |
156 |
||
3146.7.2
by Vincent Ladeuil
Review feedback. Fix variable names and scope. |
157 |
def getTestCaseNames(self, test_case_class): |
158 |
test_fn_names = self.test_func_names.get(test_case_class, None) |
|
159 |
if test_fn_names is not None: |
|
3302.7.8
by Vincent Ladeuil
Fix typos. |
160 |
# We already know them
|
3146.7.2
by Vincent Ladeuil
Review feedback. Fix variable names and scope. |
161 |
return test_fn_names |
3146.7.1
by Vincent Ladeuil
Reduce selftest overhead to establish test names by memoization. |
162 |
|
3146.7.2
by Vincent Ladeuil
Review feedback. Fix variable names and scope. |
163 |
test_fn_names = unittest.TestLoader.getTestCaseNames(self, |
164 |
test_case_class) |
|
165 |
self.test_func_names[test_case_class] = test_fn_names |
|
166 |
return test_fn_names |
|
1707.2.2
by Robert Collins
Start on bench_add, an add benchtest. |
167 |
|
3302.8.2
by Vincent Ladeuil
New test loader reducing modules imports and tests loaded. |
168 |
|
169 |
class FilteredByModuleTestLoader(TestLoader): |
|
170 |
"""A test loader that import only the needed modules.""" |
|
171 |
||
172 |
def __init__(self, needs_module): |
|
173 |
"""Constructor. |
|
174 |
||
175 |
:param needs_module: a callable taking a module name as a
|
|
176 |
parameter returing True if the module should be loaded.
|
|
177 |
"""
|
|
178 |
TestLoader.__init__(self) |
|
179 |
self.needs_module = needs_module |
|
180 |
||
181 |
def loadTestsFromModuleName(self, name): |
|
182 |
if self.needs_module(name): |
|
183 |
return TestLoader.loadTestsFromModuleName(self, name) |
|
184 |
else: |
|
185 |
return self.suiteClass() |
|
186 |
||
187 |
||
1185.1.29
by Robert Collins
merge merge tweaks from aaron, which includes latest .dev |
188 |
class TestVisitor(object): |
189 |
"""A visitor for Tests""" |
|
5574.4.2
by Vincent Ladeuil
Fix vertical spaces and lines too long. |
190 |
|
1185.1.29
by Robert Collins
merge merge tweaks from aaron, which includes latest .dev |
191 |
def visitSuite(self, aTestSuite): |
192 |
pass
|
|
5574.4.2
by Vincent Ladeuil
Fix vertical spaces and lines too long. |
193 |
|
1185.1.29
by Robert Collins
merge merge tweaks from aaron, which includes latest .dev |
194 |
def visitCase(self, aTestCase): |
195 |
pass
|