/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 bzrlib/tests/TestUtil.py

Add bzrlib.pyutils, which has get_named_object, a wrapper around __import__.

This is used to replace various ad hoc implementations of the same logic,
notably the version used in registry's _LazyObjectGetter which had a bug when
getting a module without also getting a member.  And of course, this new
function has unit tests, unlike the replaced code.

This also adds a KnownHooksRegistry subclass to provide a more natural home for
some other logic.

I'm not thrilled about the name of the new module or the new functions, but it's
hard to think of good names for such generic functionality.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
import logging
21
21
import unittest
22
22
 
 
23
from bzrlib import pyutils
 
24
 
23
25
# Mark this python module as being part of the implementation
24
26
# of unittest: this gives us better tracebacks where the last
25
27
# shown frame is the test code, not our assertXYZ.
106
108
 
107
109
    def loadTestsFromModuleName(self, name):
108
110
        result = self.suiteClass()
109
 
        module = _load_module_by_name(name)
 
111
        module = pyutils.get_named_object(name)
110
112
 
111
113
        result.addTests(self.loadTestsFromModule(module))
112
114
        return result
135
137
        >>>         result.addTests([test, test])
136
138
        >>>     return result
137
139
        """
138
 
        basic_tests = super(TestLoader, self).loadTestsFromModule(module)
 
140
        if sys.version_info < (2, 7):
 
141
            basic_tests = super(TestLoader, self).loadTestsFromModule(module)
 
142
        else:
 
143
            # GZ 2010-07-19: Python 2.7 unittest also uses load_tests but with
 
144
            #                a different and incompatible signature
 
145
            basic_tests = super(TestLoader, self).loadTestsFromModule(module,
 
146
                use_load_tests=False)
139
147
        load_tests = getattr(module, "load_tests", None)
140
148
        if load_tests is not None:
141
149
            return load_tests(basic_tests, module, self)
173
181
            return self.suiteClass()
174
182
 
175
183
 
176
 
def _load_module_by_name(mod_name):
177
 
    parts = mod_name.split('.')
178
 
    module = __import__(mod_name)
179
 
    del parts[0]
180
 
    # for historical reasons python returns the top-level module even though
181
 
    # it loads the submodule; we need to walk down to get the one we want.
182
 
    while parts:
183
 
        module = getattr(module, parts.pop(0))
184
 
    return module
185
 
 
186
 
 
187
184
class TestVisitor(object):
188
185
    """A visitor for Tests"""
189
186
    def visitSuite(self, aTestSuite):