13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
17
"""Tests for the Registry classes"""
39
39
a_registry = registry.Registry()
40
40
self.register_stuff(a_registry)
42
self.assertTrue(a_registry.default_key is None)
42
self.failUnless(a_registry.default_key is None)
44
# test get() (self.default_key is None)
44
# test get() (self.default_key == None)
45
45
self.assertRaises(KeyError, a_registry.get)
46
46
self.assertRaises(KeyError, a_registry.get, None)
47
47
self.assertEqual(2, a_registry.get('two'))
187
187
], sorted((key, a_registry.get_info(key))
188
188
for key in a_registry.keys()))
190
def test_get_prefix(self):
191
my_registry = registry.Registry()
192
http_object = object()
193
sftp_object = object()
194
my_registry.register('http:', http_object)
195
my_registry.register('sftp:', sftp_object)
196
found_object, suffix = my_registry.get_prefix('http://foo/bar')
197
self.assertEqual('//foo/bar', suffix)
198
self.assertIs(http_object, found_object)
199
self.assertIsNot(sftp_object, found_object)
200
found_object, suffix = my_registry.get_prefix('sftp://baz/qux')
201
self.assertEqual('//baz/qux', suffix)
202
self.assertIs(sftp_object, found_object)
205
class TestRegistryIter(tests.TestCase):
206
"""Test registry iteration behaviors.
208
There are dark corner cases here when the registered objects trigger
209
addition in the iterated registry.
213
super(TestRegistryIter, self).setUp()
215
# We create a registry with "official" objects and "hidden"
216
# objects. The later represent the side effects that led to bug #277048
218
_registry = registry.Registry()
221
_registry.register('hidden', None)
223
# Avoid closing over self by binding local variable
224
self.registry = _registry
225
self.registry.register('passive', None)
226
self.registry.register('active', register_more)
227
self.registry.register('passive-too', None)
229
class InvasiveGetter(registry._ObjectGetter):
231
def get_obj(inner_self):
232
# Surprise ! Getting a registered object (think lazy loaded
233
# module) register yet another object !
234
_registry.register('more hidden', None)
235
return inner_self._obj
237
self.registry.register('hacky', None)
238
# We peek under the covers because the alternative is to use lazy
239
# registration and create a module that can reference our test registry
240
# it's too much work for such a corner case -- vila 090916
241
self.registry._dict['hacky'] = InvasiveGetter(None)
243
def _iter_them(self, iter_func_name):
244
iter_func = getattr(self.registry, iter_func_name, None)
245
self.assertIsNot(None, iter_func)
247
for name, func in iter_func():
249
self.assertFalse(name in ('hidden', 'more hidden'))
251
# Using an object register another one as a side effect
253
self.assertEqual(4, count)
255
def test_iteritems(self):
256
# the dict is modified during the iteration
257
self.assertRaises(RuntimeError, self._iter_them, 'iteritems')
259
def test_items(self):
260
# we should be able to iterate even if one item modify the dict
261
self._iter_them('items')
264
class TestRegistryWithDirs(tests.TestCaseInTempDir):
191
class TestRegistryWithDirs(TestCaseInTempDir):
265
192
"""Registry tests that require temporary dirs"""
267
194
def create_plugin_file(self, contents):
291
def test_lazy_import_registry_foo(self):
292
a_registry = registry.Registry()
293
a_registry.register_lazy('foo', 'bzrlib.branch', 'Branch')
294
a_registry.register_lazy('bar', 'bzrlib.branch', 'Branch.hooks')
295
self.assertEqual(branch.Branch, a_registry.get('foo'))
296
self.assertEqual(branch.Branch.hooks, a_registry.get('bar'))
298
218
def test_lazy_import_registry(self):
299
219
plugin_name = self.create_simple_plugin()
300
220
a_registry = registry.Registry()
306
226
self.assertEqual(['function', 'klass', 'module', 'obj'],
307
227
sorted(a_registry.keys()))
308
228
# The plugin should not be loaded until we grab the first object
309
self.assertFalse(plugin_name in sys.modules)
229
self.failIf(plugin_name in sys.modules)
311
231
# By default the plugin won't be in the search path
312
232
self.assertRaises(ImportError, a_registry.get, 'obj')
341
261
sys.path.remove(plugin_path)
343
def test_lazy_import_get_module(self):
344
a_registry = registry.Registry()
345
a_registry.register_lazy('obj', "bzrlib.tests.test_registry",
347
self.assertEquals("bzrlib.tests.test_registry",
348
a_registry._get_module("obj"))
350
def test_normal_get_module(self):
351
class AThing(object):
353
a_registry = registry.Registry()
354
a_registry.register("obj", AThing())
355
self.assertEquals("bzrlib.tests.test_registry",
356
a_registry._get_module("obj"))