/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1
# Copyright (C) 2006, 2008-2012, 2016 Canonical Ltd
1911.4.1 by John Arbash Meinel
Creating a factory that can load modules on demand.
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1911.4.1 by John Arbash Meinel
Creating a factory that can load modules on demand.
16
1912.5.2 by Adeodato Simó
Morph John's LazyFactory into a generalized Registry class, and
17
"""Tests for the Registry classes"""
1911.4.1 by John Arbash Meinel
Creating a factory that can load modules on demand.
18
19
import os
20
import sys
21
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
22
from breezy import (
5436.2.1 by Andrew Bennetts
Add bzrlib.pyutils, which has get_named_object, a wrapper around __import__.
23
    branch,
4691.1.1 by Vincent Ladeuil
Respect items() protocol for registry objects.
24
    osutils,
1912.5.2 by Adeodato Simó
Morph John's LazyFactory into a generalized Registry class, and
25
    registry,
4691.1.1 by Vincent Ladeuil
Respect items() protocol for registry objects.
26
    tests,
1911.4.1 by John Arbash Meinel
Creating a factory that can load modules on demand.
27
    )
4691.1.1 by Vincent Ladeuil
Respect items() protocol for registry objects.
28
6929.10.6 by Jelmer Vernooij
tests
29
4691.1.1 by Vincent Ladeuil
Respect items() protocol for registry objects.
30
31
class TestRegistry(tests.TestCase):
1911.4.5 by John Arbash Meinel
Make a Registry look more like a dict, and allow anyone to register stuff lazily.
32
33
    def register_stuff(self, a_registry):
34
        a_registry.register('one', 1)
35
        a_registry.register('two', 2)
36
        a_registry.register('four', 4)
37
        a_registry.register('five', 5)
1912.5.2 by Adeodato Simó
Morph John's LazyFactory into a generalized Registry class, and
38
39
    def test_registry(self):
1911.4.5 by John Arbash Meinel
Make a Registry look more like a dict, and allow anyone to register stuff lazily.
40
        a_registry = registry.Registry()
41
        self.register_stuff(a_registry)
1912.5.2 by Adeodato Simó
Morph John's LazyFactory into a generalized Registry class, and
42
5784.1.1 by Martin Pool
Stop using failIf, failUnless, etc
43
        self.assertTrue(a_registry.default_key is None)
1912.5.2 by Adeodato Simó
Morph John's LazyFactory into a generalized Registry class, and
44
3376.2.11 by Martin Pool
Compare to None using is/is not not ==
45
        # test get() (self.default_key is None)
1911.4.5 by John Arbash Meinel
Make a Registry look more like a dict, and allow anyone to register stuff lazily.
46
        self.assertRaises(KeyError, a_registry.get)
47
        self.assertRaises(KeyError, a_registry.get, None)
48
        self.assertEqual(2, a_registry.get('two'))
49
        self.assertRaises(KeyError, a_registry.get, 'three')
1912.5.2 by Adeodato Simó
Morph John's LazyFactory into a generalized Registry class, and
50
51
        # test _set_default_key
1911.4.5 by John Arbash Meinel
Make a Registry look more like a dict, and allow anyone to register stuff lazily.
52
        a_registry.default_key = 'five'
5784.1.1 by Martin Pool
Stop using failIf, failUnless, etc
53
        self.assertTrue(a_registry.default_key == 'five')
1911.4.5 by John Arbash Meinel
Make a Registry look more like a dict, and allow anyone to register stuff lazily.
54
        self.assertEqual(5, a_registry.get())
55
        self.assertEqual(5, a_registry.get(None))
56
        # If they ask for a specific entry, they should get KeyError
57
        # not the default value. They can always pass None if they prefer
58
        self.assertRaises(KeyError, a_registry.get, 'six')
59
        self.assertRaises(KeyError, a_registry._set_default_key, 'six')
1912.5.2 by Adeodato Simó
Morph John's LazyFactory into a generalized Registry class, and
60
61
        # test keys()
1911.4.5 by John Arbash Meinel
Make a Registry look more like a dict, and allow anyone to register stuff lazily.
62
        self.assertEqual(['five', 'four', 'one', 'two'], a_registry.keys())
1912.5.2 by Adeodato Simó
Morph John's LazyFactory into a generalized Registry class, and
63
1911.4.11 by John Arbash Meinel
Remove extra dict-like members, simplfying the api
64
    def test_registry_funcs(self):
1911.4.5 by John Arbash Meinel
Make a Registry look more like a dict, and allow anyone to register stuff lazily.
65
        a_registry = registry.Registry()
66
        self.register_stuff(a_registry)
67
5784.1.1 by Martin Pool
Stop using failIf, failUnless, etc
68
        self.assertTrue('one' in a_registry)
1911.4.11 by John Arbash Meinel
Remove extra dict-like members, simplfying the api
69
        a_registry.remove('one')
5784.1.1 by Martin Pool
Stop using failIf, failUnless, etc
70
        self.assertFalse('one' in a_registry)
1911.4.5 by John Arbash Meinel
Make a Registry look more like a dict, and allow anyone to register stuff lazily.
71
        self.assertRaises(KeyError, a_registry.get, 'one')
72
1911.4.7 by John Arbash Meinel
Add help and info parameters, and tests for them
73
        a_registry.register('one', 'one')
1911.4.5 by John Arbash Meinel
Make a Registry look more like a dict, and allow anyone to register stuff lazily.
74
75
        self.assertEqual(['five', 'four', 'one', 'two'],
1911.4.11 by John Arbash Meinel
Remove extra dict-like members, simplfying the api
76
                         sorted(a_registry.keys()))
1911.4.5 by John Arbash Meinel
Make a Registry look more like a dict, and allow anyone to register stuff lazily.
77
        self.assertEqual([('five', 5), ('four', 4),
78
                          ('one', 'one'), ('two', 2)],
79
                         sorted(a_registry.iteritems()))
80
1911.4.7 by John Arbash Meinel
Add help and info parameters, and tests for them
81
    def test_register_override(self):
82
        a_registry = registry.Registry()
83
        a_registry.register('one', 'one')
84
        self.assertRaises(KeyError, a_registry.register, 'one', 'two')
85
        self.assertRaises(KeyError, a_registry.register, 'one', 'two',
7143.15.2 by Jelmer Vernooij
Run autopep8.
86
                          override_existing=False)
1911.4.7 by John Arbash Meinel
Add help and info parameters, and tests for them
87
88
        a_registry.register('one', 'two', override_existing=True)
89
        self.assertEqual('two', a_registry.get('one'))
90
91
        self.assertRaises(KeyError, a_registry.register_lazy,
92
                          'one', 'three', 'four')
93
94
        a_registry.register_lazy('one', 'module', 'member',
95
                                 override_existing=True)
96
97
    def test_registry_help(self):
98
        a_registry = registry.Registry()
99
        a_registry.register('one', 1, help='help text for one')
100
        # We should not have to import the module to return the help
101
        # information
102
        a_registry.register_lazy('two', 'nonexistent_module', 'member',
103
                                 help='help text for two')
104
105
        # We should be able to handle a callable to get information
1911.4.8 by John Arbash Meinel
Add Registry.iterhelp and Registry.iterinfo
106
        help_calls = []
7143.15.2 by Jelmer Vernooij
Run autopep8.
107
1911.4.9 by John Arbash Meinel
A help callable should take the registry as the first parameter
108
        def generic_help(reg, key):
1911.4.8 by John Arbash Meinel
Add Registry.iterhelp and Registry.iterinfo
109
            help_calls.append(key)
1911.4.7 by John Arbash Meinel
Add help and info parameters, and tests for them
110
            return 'generic help for %s' % (key,)
111
        a_registry.register('three', 3, help=generic_help)
112
        a_registry.register_lazy('four', 'nonexistent_module', 'member2',
113
                                 help=generic_help)
1911.4.8 by John Arbash Meinel
Add Registry.iterhelp and Registry.iterinfo
114
        a_registry.register('five', 5)
1911.4.7 by John Arbash Meinel
Add help and info parameters, and tests for them
115
1911.4.10 by John Arbash Meinel
A test which uses the callback to return information from the object.
116
        def help_from_object(reg, key):
117
            obj = reg.get(key)
118
            return obj.help()
119
120
        class SimpleObj(object):
121
            def help(self):
122
                return 'this is my help'
123
        a_registry.register('six', SimpleObj(), help=help_from_object)
124
1911.4.7 by John Arbash Meinel
Add help and info parameters, and tests for them
125
        self.assertEqual('help text for one', a_registry.get_help('one'))
126
        self.assertEqual('help text for two', a_registry.get_help('two'))
127
        self.assertEqual('generic help for three',
128
                         a_registry.get_help('three'))
1911.4.8 by John Arbash Meinel
Add Registry.iterhelp and Registry.iterinfo
129
        self.assertEqual(['three'], help_calls)
1911.4.7 by John Arbash Meinel
Add help and info parameters, and tests for them
130
        self.assertEqual('generic help for four',
131
                         a_registry.get_help('four'))
1911.4.8 by John Arbash Meinel
Add Registry.iterhelp and Registry.iterinfo
132
        self.assertEqual(['three', 'four'], help_calls)
133
        self.assertEqual(None, a_registry.get_help('five'))
1911.4.10 by John Arbash Meinel
A test which uses the callback to return information from the object.
134
        self.assertEqual('this is my help', a_registry.get_help('six'))
1911.4.7 by John Arbash Meinel
Add help and info parameters, and tests for them
135
136
        self.assertRaises(KeyError, a_registry.get_help, None)
1911.4.10 by John Arbash Meinel
A test which uses the callback to return information from the object.
137
        self.assertRaises(KeyError, a_registry.get_help, 'seven')
1911.4.7 by John Arbash Meinel
Add help and info parameters, and tests for them
138
139
        a_registry.default_key = 'one'
140
        self.assertEqual('help text for one', a_registry.get_help(None))
1911.4.10 by John Arbash Meinel
A test which uses the callback to return information from the object.
141
        self.assertRaises(KeyError, a_registry.get_help, 'seven')
1911.4.8 by John Arbash Meinel
Add Registry.iterhelp and Registry.iterinfo
142
143
        self.assertEqual([('five', None),
144
                          ('four', 'generic help for four'),
145
                          ('one', 'help text for one'),
1911.4.10 by John Arbash Meinel
A test which uses the callback to return information from the object.
146
                          ('six', 'this is my help'),
1911.4.8 by John Arbash Meinel
Add Registry.iterhelp and Registry.iterinfo
147
                          ('three', 'generic help for three'),
148
                          ('two', 'help text for two'),
7143.15.2 by Jelmer Vernooij
Run autopep8.
149
                          ], sorted((key, a_registry.get_help(key))
1911.4.11 by John Arbash Meinel
Remove extra dict-like members, simplfying the api
150
                                    for key in a_registry.keys()))
151
1911.4.8 by John Arbash Meinel
Add Registry.iterhelp and Registry.iterinfo
152
        # We don't know what order it was called in, but we should get
153
        # 2 more calls to three and four
154
        self.assertEqual(['four', 'four', 'three', 'three'],
155
                         sorted(help_calls))
1911.4.7 by John Arbash Meinel
Add help and info parameters, and tests for them
156
157
    def test_registry_info(self):
158
        a_registry = registry.Registry()
159
        a_registry.register('one', 1, info='string info')
160
        # We should not have to import the module to return the info
161
        a_registry.register_lazy('two', 'nonexistent_module', 'member',
162
                                 info=2)
163
164
        # We should be able to handle a callable to get information
165
        a_registry.register('three', 3, info=['a', 'list'])
166
        obj = object()
167
        a_registry.register_lazy('four', 'nonexistent_module', 'member2',
168
                                 info=obj)
1911.4.8 by John Arbash Meinel
Add Registry.iterhelp and Registry.iterinfo
169
        a_registry.register('five', 5)
1911.4.7 by John Arbash Meinel
Add help and info parameters, and tests for them
170
171
        self.assertEqual('string info', a_registry.get_info('one'))
172
        self.assertEqual(2, a_registry.get_info('two'))
173
        self.assertEqual(['a', 'list'], a_registry.get_info('three'))
174
        self.assertIs(obj, a_registry.get_info('four'))
1911.4.8 by John Arbash Meinel
Add Registry.iterhelp and Registry.iterinfo
175
        self.assertIs(None, a_registry.get_info('five'))
1911.4.7 by John Arbash Meinel
Add help and info parameters, and tests for them
176
177
        self.assertRaises(KeyError, a_registry.get_info, None)
1911.4.8 by John Arbash Meinel
Add Registry.iterhelp and Registry.iterinfo
178
        self.assertRaises(KeyError, a_registry.get_info, 'six')
1911.4.7 by John Arbash Meinel
Add help and info parameters, and tests for them
179
180
        a_registry.default_key = 'one'
181
        self.assertEqual('string info', a_registry.get_info(None))
1911.4.8 by John Arbash Meinel
Add Registry.iterhelp and Registry.iterinfo
182
        self.assertRaises(KeyError, a_registry.get_info, 'six')
1911.4.7 by John Arbash Meinel
Add help and info parameters, and tests for them
183
1911.4.8 by John Arbash Meinel
Add Registry.iterhelp and Registry.iterinfo
184
        self.assertEqual([('five', None),
185
                          ('four', obj),
186
                          ('one', 'string info'),
187
                          ('three', ['a', 'list']),
188
                          ('two', 2),
7143.15.2 by Jelmer Vernooij
Run autopep8.
189
                          ], sorted((key, a_registry.get_info(key))
1911.4.11 by John Arbash Meinel
Remove extra dict-like members, simplfying the api
190
                                    for key in a_registry.keys()))
191
3251.3.2 by Aaron Bentley
Add testing of Registry.get_prefix
192
    def test_get_prefix(self):
193
        my_registry = registry.Registry()
194
        http_object = object()
195
        sftp_object = object()
196
        my_registry.register('http:', http_object)
197
        my_registry.register('sftp:', sftp_object)
198
        found_object, suffix = my_registry.get_prefix('http://foo/bar')
199
        self.assertEqual('//foo/bar', suffix)
200
        self.assertIs(http_object, found_object)
201
        self.assertIsNot(sftp_object, found_object)
202
        found_object, suffix = my_registry.get_prefix('sftp://baz/qux')
203
        self.assertEqual('//baz/qux', suffix)
204
        self.assertIs(sftp_object, found_object)
205
6929.10.6 by Jelmer Vernooij
tests
206
    def test_registry_alias(self):
207
        a_registry = registry.Registry()
208
        a_registry.register('one', 1, info='string info')
209
        a_registry.register_alias('two', 'one')
210
        a_registry.register_alias('three', 'one', info='own info')
211
        self.assertEqual(a_registry.get('one'), a_registry.get('two'))
7143.15.2 by Jelmer Vernooij
Run autopep8.
212
        self.assertEqual(a_registry.get_help('one'),
213
                         a_registry.get_help('two'))
214
        self.assertEqual(a_registry.get_info('one'),
215
                         a_registry.get_info('two'))
6929.10.6 by Jelmer Vernooij
tests
216
        self.assertEqual('own info', a_registry.get_info('three'))
217
        self.assertEqual({'two': 'one', 'three': 'one'}, a_registry.aliases())
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
218
        self.assertEqual(
219
            {'one': ['three', 'two']},
7479.2.1 by Jelmer Vernooij
Drop python2 support.
220
            {k: sorted(v) for (k, v) in a_registry.alias_map().items()})
6929.10.6 by Jelmer Vernooij
tests
221
222
    def test_registry_alias_exists(self):
223
        a_registry = registry.Registry()
224
        a_registry.register('one', 1, info='string info')
225
        a_registry.register('two', 2)
226
        self.assertRaises(KeyError, a_registry.register_alias, 'one', 'one')
227
228
    def test_registry_alias_targetmissing(self):
229
        a_registry = registry.Registry()
230
        self.assertRaises(KeyError, a_registry.register_alias, 'one', 'two')
231
1911.4.5 by John Arbash Meinel
Make a Registry look more like a dict, and allow anyone to register stuff lazily.
232
4691.1.1 by Vincent Ladeuil
Respect items() protocol for registry objects.
233
class TestRegistryIter(tests.TestCase):
234
    """Test registry iteration behaviors.
235
236
    There are dark corner cases here when the registered objects trigger
237
    addition in the iterated registry.
238
    """
239
240
    def setUp(self):
241
        super(TestRegistryIter, self).setUp()
242
243
        # We create a registry with "official" objects and "hidden"
244
        # objects. The later represent the side effects that led to bug #277048
245
        # and #430510
5340.15.1 by John Arbash Meinel
supersede exc-info branch
246
        _registry = registry.Registry()
4691.1.1 by Vincent Ladeuil
Respect items() protocol for registry objects.
247
248
        def register_more():
7143.15.2 by Jelmer Vernooij
Run autopep8.
249
            _registry.register('hidden', None)
4691.1.1 by Vincent Ladeuil
Respect items() protocol for registry objects.
250
5340.15.1 by John Arbash Meinel
supersede exc-info branch
251
        # Avoid closing over self by binding local variable
252
        self.registry = _registry
4691.1.1 by Vincent Ladeuil
Respect items() protocol for registry objects.
253
        self.registry.register('passive', None)
254
        self.registry.register('active', register_more)
255
        self.registry.register('passive-too', None)
256
257
        class InvasiveGetter(registry._ObjectGetter):
258
259
            def get_obj(inner_self):
260
                # Surprise ! Getting a registered object (think lazy loaded
261
                # module) register yet another object !
5340.15.1 by John Arbash Meinel
supersede exc-info branch
262
                _registry.register('more hidden', None)
4691.1.1 by Vincent Ladeuil
Respect items() protocol for registry objects.
263
                return inner_self._obj
264
265
        self.registry.register('hacky', None)
266
        # We peek under the covers because the alternative is to use lazy
267
        # registration and create a module that can reference our test registry
268
        # it's too much work for such a corner case -- vila 090916
269
        self.registry._dict['hacky'] = InvasiveGetter(None)
270
271
    def _iter_them(self, iter_func_name):
272
        iter_func = getattr(self.registry, iter_func_name, None)
273
        self.assertIsNot(None, iter_func)
274
        count = 0
275
        for name, func in iter_func():
276
            count += 1
277
            self.assertFalse(name in ('hidden', 'more hidden'))
278
            if func is not None:
279
                # Using an object register another one as a side effect
280
                func()
281
        self.assertEqual(4, count)
282
283
    def test_iteritems(self):
284
        # the dict is modified during the iteration
285
        self.assertRaises(RuntimeError, self._iter_them, 'iteritems')
286
287
    def test_items(self):
288
        # we should be able to iterate even if one item modify the dict
289
        self._iter_them('items')
290
291
292
class TestRegistryWithDirs(tests.TestCaseInTempDir):
1911.4.5 by John Arbash Meinel
Make a Registry look more like a dict, and allow anyone to register stuff lazily.
293
    """Registry tests that require temporary dirs"""
1911.4.1 by John Arbash Meinel
Creating a factory that can load modules on demand.
294
295
    def create_plugin_file(self, contents):
1911.4.5 by John Arbash Meinel
Make a Registry look more like a dict, and allow anyone to register stuff lazily.
296
        """Create a file to be used as a plugin.
297
298
        This is created in a temporary directory, so that we
299
        are sure that it doesn't start in the plugin path.
300
        """
301
        os.mkdir('tmp')
1911.4.1 by John Arbash Meinel
Creating a factory that can load modules on demand.
302
        plugin_name = 'bzr_plugin_a_%s' % (osutils.rand_chars(4),)
7143.15.2 by Jelmer Vernooij
Run autopep8.
303
        with open('tmp/' + plugin_name + '.py', 'wb') as f:
304
            f.write(contents)
1911.4.1 by John Arbash Meinel
Creating a factory that can load modules on demand.
305
        return plugin_name
306
307
    def create_simple_plugin(self):
308
        return self.create_plugin_file(
7027.3.3 by Jelmer Vernooij
Add some more bees; support writing both bytes and unicode strings in build_tree_contents.
309
            b'object1 = "foo"\n'
310
            b'\n\n'
311
            b'def function(a,b,c):\n'
312
            b'    return a,b,c\n'
313
            b'\n\n'
314
            b'class MyClass(object):\n'
315
            b'    def __init__(self, a):\n'
316
            b'      self.a = a\n'
317
            b'\n\n'
1911.4.1 by John Arbash Meinel
Creating a factory that can load modules on demand.
318
        )
319
5436.2.1 by Andrew Bennetts
Add bzrlib.pyutils, which has get_named_object, a wrapper around __import__.
320
    def test_lazy_import_registry_foo(self):
321
        a_registry = registry.Registry()
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
322
        a_registry.register_lazy('foo', 'breezy.branch', 'Branch')
323
        a_registry.register_lazy('bar', 'breezy.branch', 'Branch.hooks')
5436.2.1 by Andrew Bennetts
Add bzrlib.pyutils, which has get_named_object, a wrapper around __import__.
324
        self.assertEqual(branch.Branch, a_registry.get('foo'))
325
        self.assertEqual(branch.Branch.hooks, a_registry.get('bar'))
326
1912.5.2 by Adeodato Simó
Morph John's LazyFactory into a generalized Registry class, and
327
    def test_lazy_import_registry(self):
1911.4.1 by John Arbash Meinel
Creating a factory that can load modules on demand.
328
        plugin_name = self.create_simple_plugin()
1911.4.5 by John Arbash Meinel
Make a Registry look more like a dict, and allow anyone to register stuff lazily.
329
        a_registry = registry.Registry()
330
        a_registry.register_lazy('obj', plugin_name, 'object1')
331
        a_registry.register_lazy('function', plugin_name, 'function')
332
        a_registry.register_lazy('klass', plugin_name, 'MyClass')
333
        a_registry.register_lazy('module', plugin_name, None)
1911.4.1 by John Arbash Meinel
Creating a factory that can load modules on demand.
334
335
        self.assertEqual(['function', 'klass', 'module', 'obj'],
1911.4.5 by John Arbash Meinel
Make a Registry look more like a dict, and allow anyone to register stuff lazily.
336
                         sorted(a_registry.keys()))
1911.4.1 by John Arbash Meinel
Creating a factory that can load modules on demand.
337
        # The plugin should not be loaded until we grab the first object
5784.1.1 by Martin Pool
Stop using failIf, failUnless, etc
338
        self.assertFalse(plugin_name in sys.modules)
1911.4.1 by John Arbash Meinel
Creating a factory that can load modules on demand.
339
340
        # By default the plugin won't be in the search path
1911.4.5 by John Arbash Meinel
Make a Registry look more like a dict, and allow anyone to register stuff lazily.
341
        self.assertRaises(ImportError, a_registry.get, 'obj')
1911.4.1 by John Arbash Meinel
Creating a factory that can load modules on demand.
342
6619.3.26 by Martin
Fix fallout from 2to3 getcwdu transformation and other test uses
343
        plugin_path = self.test_dir + '/tmp'
1911.4.5 by John Arbash Meinel
Make a Registry look more like a dict, and allow anyone to register stuff lazily.
344
        sys.path.append(plugin_path)
1911.4.1 by John Arbash Meinel
Creating a factory that can load modules on demand.
345
        try:
1911.4.5 by John Arbash Meinel
Make a Registry look more like a dict, and allow anyone to register stuff lazily.
346
            obj = a_registry.get('obj')
1911.4.1 by John Arbash Meinel
Creating a factory that can load modules on demand.
347
            self.assertEqual('foo', obj)
5784.1.1 by Martin Pool
Stop using failIf, failUnless, etc
348
            self.assertTrue(plugin_name in sys.modules)
1911.4.1 by John Arbash Meinel
Creating a factory that can load modules on demand.
349
350
            # Now grab another object
1911.4.5 by John Arbash Meinel
Make a Registry look more like a dict, and allow anyone to register stuff lazily.
351
            func = a_registry.get('function')
1911.4.1 by John Arbash Meinel
Creating a factory that can load modules on demand.
352
            self.assertEqual(plugin_name, func.__module__)
353
            self.assertEqual('function', func.__name__)
354
            self.assertEqual((1, [], '3'), func(1, [], '3'))
355
356
            # And finally a class
1911.4.5 by John Arbash Meinel
Make a Registry look more like a dict, and allow anyone to register stuff lazily.
357
            klass = a_registry.get('klass')
1911.4.1 by John Arbash Meinel
Creating a factory that can load modules on demand.
358
            self.assertEqual(plugin_name, klass.__module__)
359
            self.assertEqual('MyClass', klass.__name__)
360
361
            inst = klass(1)
362
            self.assertIsInstance(inst, klass)
363
            self.assertEqual(1, inst.a)
364
1911.4.5 by John Arbash Meinel
Make a Registry look more like a dict, and allow anyone to register stuff lazily.
365
            module = a_registry.get('module')
1911.4.1 by John Arbash Meinel
Creating a factory that can load modules on demand.
366
            self.assertIs(obj, module.object1)
367
            self.assertIs(func, module.function)
368
            self.assertIs(klass, module.MyClass)
369
        finally:
1911.4.5 by John Arbash Meinel
Make a Registry look more like a dict, and allow anyone to register stuff lazily.
370
            sys.path.remove(plugin_path)
371
5676.1.8 by Jelmer Vernooij
Add tests for Registry._get_module().
372
    def test_lazy_import_get_module(self):
373
        a_registry = registry.Registry()
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
374
        a_registry.register_lazy('obj', "breezy.tests.test_registry",
7143.15.2 by Jelmer Vernooij
Run autopep8.
375
                                 'object1')
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
376
        self.assertEqual("breezy.tests.test_registry",
7143.15.2 by Jelmer Vernooij
Run autopep8.
377
                         a_registry._get_module("obj"))
5676.1.8 by Jelmer Vernooij
Add tests for Registry._get_module().
378
379
    def test_normal_get_module(self):
380
        class AThing(object):
381
            """Something"""
382
        a_registry = registry.Registry()
383
        a_registry.register("obj", AThing())
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
384
        self.assertEqual("breezy.tests.test_registry",
7143.15.2 by Jelmer Vernooij
Run autopep8.
385
                         a_registry._get_module("obj"))