bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
| 
2052.3.1
by John Arbash Meinel
 Add tests to cleanup the copyright of all source files  | 
1  | 
# Copyright (C) 2004, 2005, 2006 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
 | 
|
16  | 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 | 
|
17  | 
#
 | 
|
18  | 
||
19  | 
import sys  | 
|
20  | 
import logging  | 
|
21  | 
import unittest  | 
|
22  | 
||
| 
1739.1.8
by Robert Collins
 Review feedback.  | 
23  | 
# Mark this python module as being part of the implementation
 | 
24  | 
# of unittest: this gives us better tracebacks where the last
 | 
|
25  | 
# shown frame is the test code, not our assertXYZ.
 | 
|
26  | 
__unittest = 1  | 
|
27  | 
||
| 
1185.1.29
by Robert Collins
 merge merge tweaks from aaron, which includes latest .dev  | 
28  | 
|
29  | 
class LogCollector(logging.Handler):  | 
|
30  | 
def __init__(self):  | 
|
31  | 
logging.Handler.__init__(self)  | 
|
32  | 
self.records=[]  | 
|
33  | 
def emit(self, record):  | 
|
34  | 
self.records.append(record.getMessage())  | 
|
35  | 
||
36  | 
||
37  | 
def makeCollectingLogger():  | 
|
38  | 
"""I make a logger instance that collects its logs for programmatic analysis  | 
|
39  | 
    -> (logger, collector)"""
 | 
|
40  | 
logger=logging.Logger("collector")  | 
|
41  | 
handler=LogCollector()  | 
|
42  | 
handler.setFormatter(logging.Formatter("%(levelname)s: %(message)s"))  | 
|
43  | 
logger.addHandler(handler)  | 
|
44  | 
return logger, handler  | 
|
45  | 
||
46  | 
||
47  | 
def visitTests(suite, visitor):  | 
|
48  | 
"""A foreign method for visiting the tests in a test suite."""  | 
|
49  | 
for test in suite._tests:  | 
|
50  | 
        #Abusing types to avoid monkey patching unittest.TestCase. 
 | 
|
51  | 
        # Maybe that would be better?
 | 
|
52  | 
try:  | 
|
53  | 
test.visit(visitor)  | 
|
54  | 
except AttributeError:  | 
|
55  | 
if isinstance(test, unittest.TestCase):  | 
|
56  | 
visitor.visitCase(test)  | 
|
57  | 
elif isinstance(test, unittest.TestSuite):  | 
|
58  | 
visitor.visitSuite(test)  | 
|
59  | 
visitTests(test, visitor)  | 
|
60  | 
else:  | 
|
61  | 
print "unvisitable non-unittest.TestCase element %r (%r)" % (test, test.__class__)  | 
|
62  | 
||
63  | 
||
64  | 
class TestSuite(unittest.TestSuite):  | 
|
65  | 
"""I am an extended TestSuite with a visitor interface.  | 
|
66  | 
    This is primarily to allow filtering of tests - and suites or
 | 
|
67  | 
    more in the future. An iterator of just tests wouldn't scale..."""
 | 
|
68  | 
||
69  | 
def visit(self, visitor):  | 
|
70  | 
"""visit the composite. Visiting is depth-first.  | 
|
71  | 
        current callbacks are visitSuite and visitCase."""
 | 
|
72  | 
visitor.visitSuite(self)  | 
|
73  | 
visitTests(self, visitor)  | 
|
74  | 
||
75  | 
||
76  | 
class TestLoader(unittest.TestLoader):  | 
|
| 
2921.6.13
by Robert Collins
 * Modules can now customise their tests by defining a ``load_tests``  | 
77  | 
"""Custom TestLoader to extend the stock python one."""  | 
78  | 
||
| 
1185.1.29
by Robert Collins
 merge merge tweaks from aaron, which includes latest .dev  | 
79  | 
suiteClass = TestSuite  | 
80  | 
||
| 
1707.2.2
by Robert Collins
 Start on bench_add, an add benchtest.  | 
81  | 
def loadTestsFromModuleNames(self, names):  | 
82  | 
"""use a custom means to load tests from modules.  | 
|
83  | 
||
84  | 
        There is an undesirable glitch in the python TestLoader where a 
 | 
|
85  | 
        import error is ignore. We think this can be solved by ensuring the 
 | 
|
86  | 
        requested name is resolvable, if its not raising the original error.
 | 
|
87  | 
        """
 | 
|
88  | 
result = self.suiteClass()  | 
|
89  | 
for name in names:  | 
|
| 
2921.6.13
by Robert Collins
 * Modules can now customise their tests by defining a ``load_tests``  | 
90  | 
module = _load_module_by_name(name)  | 
91  | 
result.addTests(self.loadTestsFromModule(module))  | 
|
| 
1707.2.2
by Robert Collins
 Start on bench_add, an add benchtest.  | 
92  | 
return result  | 
93  | 
||
| 
2921.6.13
by Robert Collins
 * Modules can now customise their tests by defining a ``load_tests``  | 
94  | 
def loadTestsFromModule(self, module):  | 
95  | 
"""Load tests from a module object.  | 
|
96  | 
||
97  | 
        This extension of the python test loader looks for an attribute
 | 
|
98  | 
        load_tests in the module object, and if not found falls back to the
 | 
|
99  | 
        regular python loadTestsFromModule.
 | 
|
100  | 
||
101  | 
        If a load_tests attribute is found, it is called and the result is
 | 
|
102  | 
        returned. 
 | 
|
103  | 
||
104  | 
        load_tests should be defined like so:
 | 
|
105  | 
        >>> def load_tests(standard_tests, module, loader):
 | 
|
106  | 
        >>>    pass
 | 
|
107  | 
||
108  | 
        standard_tests is the tests found by the stock TestLoader in the
 | 
|
109  | 
        module, module and loader are the module and loader instances.
 | 
|
110  | 
||
111  | 
        For instance, to run every test twice, you might do:
 | 
|
112  | 
        >>> def load_tests(standard_tests, module, loader):
 | 
|
113  | 
        >>>     result = loader.suiteClass()
 | 
|
114  | 
        >>>     for test in iter_suite_tests(standard_tests):
 | 
|
115  | 
        >>>         result.addTests([test, test])
 | 
|
116  | 
        >>>     return result
 | 
|
117  | 
        """
 | 
|
118  | 
basic_tests = super(TestLoader, self).loadTestsFromModule(module)  | 
|
119  | 
load_tests = getattr(module, "load_tests", None)  | 
|
120  | 
if load_tests is not None:  | 
|
121  | 
return load_tests(basic_tests, module, self)  | 
|
122  | 
else:  | 
|
123  | 
return basic_tests  | 
|
124  | 
||
| 
1707.2.2
by Robert Collins
 Start on bench_add, an add benchtest.  | 
125  | 
|
126  | 
def _load_module_by_name(mod_name):  | 
|
127  | 
parts = mod_name.split('.')  | 
|
128  | 
module = __import__(mod_name)  | 
|
129  | 
del parts[0]  | 
|
130  | 
    # for historical reasons python returns the top-level module even though
 | 
|
131  | 
    # it loads the submodule; we need to walk down to get the one we want.
 | 
|
132  | 
while parts:  | 
|
133  | 
module = getattr(module, parts.pop(0))  | 
|
134  | 
return module  | 
|
135  | 
||
136  | 
||
| 
1185.1.29
by Robert Collins
 merge merge tweaks from aaron, which includes latest .dev  | 
137  | 
class TestVisitor(object):  | 
138  | 
"""A visitor for Tests"""  | 
|
139  | 
def visitSuite(self, aTestSuite):  | 
|
140  | 
        pass
 | 
|
141  | 
def visitCase(self, aTestCase):  | 
|
142  | 
        pass
 |