/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/TestUtil.py

  • Committer: Gustav Hartvigsson
  • Date: 2021-01-09 21:36:27 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20210109213627-h1xwcutzy9m7a99b
Added 'Case Preserving Working Tree Use Cases' from Canonical Wiki

* Addod a page from the Canonical Bazaar wiki
  with information on the scmeatics of case
  perserving filesystems an a case insensitive
  filesystem works.
  
  * Needs re-work, but this will do as it is the
    same inforamoton as what was on the linked
    page in the currint documentation.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (c) 2004 Canonical Limited
 
1
# Copyright (C) 2005-2011 Canonical Ltd
2
2
#       Author: Robert Collins <robert.collins@canonical.com>
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
13
13
#
14
14
# You should have received a copy of the GNU General Public License
15
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
 
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
#
18
18
 
19
 
import sys
20
19
import logging
21
20
import unittest
 
21
import weakref
 
22
 
 
23
from .. import pyutils
 
24
 
 
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
22
29
 
23
30
 
24
31
class LogCollector(logging.Handler):
 
32
 
25
33
    def __init__(self):
26
34
        logging.Handler.__init__(self)
27
 
        self.records=[]
 
35
        self.records = []
 
36
 
28
37
    def emit(self, record):
29
38
        self.records.append(record.getMessage())
30
39
 
32
41
def makeCollectingLogger():
33
42
    """I make a logger instance that collects its logs for programmatic analysis
34
43
    -> (logger, collector)"""
35
 
    logger=logging.Logger("collector")
36
 
    handler=LogCollector()
 
44
    logger = logging.Logger("collector")
 
45
    handler = LogCollector()
37
46
    handler.setFormatter(logging.Formatter("%(levelname)s: %(message)s"))
38
47
    logger.addHandler(handler)
39
48
    return logger, handler
42
51
def visitTests(suite, visitor):
43
52
    """A foreign method for visiting the tests in a test suite."""
44
53
    for test in suite._tests:
45
 
        #Abusing types to avoid monkey patching unittest.TestCase. 
 
54
        # Abusing types to avoid monkey patching unittest.TestCase.
46
55
        # Maybe that would be better?
47
56
        try:
48
57
            test.visit(visitor)
53
62
                visitor.visitSuite(test)
54
63
                visitTests(test, visitor)
55
64
            else:
56
 
                print "unvisitable non-unittest.TestCase element %r (%r)" % (test, test.__class__)
57
 
    
 
65
                print("unvisitable non-unittest.TestCase element %r (%r)" % (
 
66
                    test, test.__class__))
 
67
 
 
68
 
 
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
 
58
86
 
59
87
class TestSuite(unittest.TestSuite):
60
88
    """I am an extended TestSuite with a visitor interface.
67
95
        visitor.visitSuite(self)
68
96
        visitTests(self, visitor)
69
97
 
 
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 = []
 
103
        stored_count = 0
 
104
        count_stored_tests = getattr(result, "_count_stored_tests", int)
 
105
        from breezy.tests import selftest_debug_flags
 
106
        notify = "uncollected_cases" in selftest_debug_flags
 
107
        while tests:
 
108
            if result.shouldStop:
 
109
                self._tests = reversed(tests)
 
110
                break
 
111
            case = _run_and_collect_case(tests.pop(), result)()
 
112
            new_stored_count = count_stored_tests()
 
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
 
116
                    FailedCollectionCase(case).run(result)
 
117
                    # Adding a new failure so need to reupdate the count
 
118
                    new_stored_count = count_stored_tests()
 
119
                # GZ 2011-09-16: Previously zombied the case at this point by
 
120
                #                clearing the dict as fallback, skip for now.
 
121
            stored_count = new_stored_count
 
122
        return result
 
123
 
 
124
 
 
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
 
70
130
 
71
131
class TestLoader(unittest.TestLoader):
72
 
    """Custome TestLoader to set the right TestSuite class."""
 
132
    """Custom TestLoader to extend the stock python one."""
 
133
 
73
134
    suiteClass = TestSuite
 
135
    # Memoize test names by test class dict
 
136
    test_func_names = {}
 
137
 
 
138
    def loadTestsFromModuleNames(self, names):
 
139
        """use a custom means to load tests from modules.
 
140
 
 
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
 
143
        requested name is resolvable, if its not raising the original error.
 
144
        """
 
145
        result = self.suiteClass()
 
146
        for name in names:
 
147
            result.addTests(self.loadTestsFromModuleName(name))
 
148
        return result
 
149
 
 
150
    def loadTestsFromModuleName(self, name):
 
151
        result = self.suiteClass()
 
152
        module = pyutils.get_named_object(name)
 
153
 
 
154
        result.addTests(self.loadTestsFromModule(module))
 
155
        return result
 
156
 
 
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:
 
160
            # We already know them
 
161
            return test_fn_names
 
162
 
 
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
 
167
 
 
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
 
74
187
 
75
188
class TestVisitor(object):
76
189
    """A visitor for Tests"""
 
190
 
77
191
    def visitSuite(self, aTestSuite):
78
192
        pass
 
193
 
79
194
    def visitCase(self, aTestCase):
80
195
        pass