~bzr-pqm/bzr/bzr.dev

« 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
179
181
            return self.suiteClass()
180
182
 
181
183
 
182
 
def _load_module_by_name(mod_name):
183
 
    parts = mod_name.split('.')
184
 
    module = __import__(mod_name)
185
 
    del parts[0]
186
 
    # for historical reasons python returns the top-level module even though
187
 
    # it loads the submodule; we need to walk down to get the one we want.
188
 
    while parts:
189
 
        module = getattr(module, parts.pop(0))
190
 
    return module
191
 
 
192
 
 
193
184
class TestVisitor(object):
194
185
    """A visitor for Tests"""
195
186
    def visitSuite(self, aTestSuite):