~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_registry.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 sys
21
21
 
22
22
from bzrlib import (
 
23
    branch,
23
24
    errors,
24
25
    osutils,
25
26
    registry,
286
287
            '\n\n'
287
288
        )
288
289
 
 
290
    def test_lazy_import_registry_foo(self):
 
291
        a_registry = registry.Registry()
 
292
        a_registry.register_lazy('foo', 'bzrlib.branch', 'Branch')
 
293
        a_registry.register_lazy('bar', 'bzrlib.branch', 'Branch.hooks')
 
294
        self.assertEqual(branch.Branch, a_registry.get('foo'))
 
295
        self.assertEqual(branch.Branch.hooks, a_registry.get('bar'))
 
296
 
289
297
    def test_lazy_import_registry(self):
290
298
        plugin_name = self.create_simple_plugin()
291
299
        a_registry = registry.Registry()