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"""
22
22
from bzrlib import (
30
class TestRegistry(tests.TestCase):
27
from bzrlib.tests import TestCase, TestCaseInTempDir
30
class TestRegistry(TestCase):
32
32
def register_stuff(self, a_registry):
33
33
a_registry.register('one', 1)
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
44
# test get() (self.default_key is None)
45
45
self.assertRaises(KeyError, a_registry.get)
50
50
# test _set_default_key
51
51
a_registry.default_key = 'five'
52
self.assertTrue(a_registry.default_key == 'five')
52
self.failUnless(a_registry.default_key == 'five')
53
53
self.assertEqual(5, a_registry.get())
54
54
self.assertEqual(5, a_registry.get(None))
55
55
# If they ask for a specific entry, they should get KeyError
64
64
a_registry = registry.Registry()
65
65
self.register_stuff(a_registry)
67
self.assertTrue('one' in a_registry)
67
self.failUnless('one' in a_registry)
68
68
a_registry.remove('one')
69
self.assertFalse('one' in a_registry)
69
self.failIf('one' in a_registry)
70
70
self.assertRaises(KeyError, a_registry.get, 'one')
72
72
a_registry.register('one', 'one')
202
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):
205
class TestRegistryWithDirs(TestCaseInTempDir):
265
206
"""Registry tests that require temporary dirs"""
267
208
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
232
def test_lazy_import_registry(self):
299
233
plugin_name = self.create_simple_plugin()
300
234
a_registry = registry.Registry()
306
240
self.assertEqual(['function', 'klass', 'module', 'obj'],
307
241
sorted(a_registry.keys()))
308
242
# The plugin should not be loaded until we grab the first object
309
self.assertFalse(plugin_name in sys.modules)
243
self.failIf(plugin_name in sys.modules)
311
245
# By default the plugin won't be in the search path
312
246
self.assertRaises(ImportError, a_registry.get, 'obj')
317
251
obj = a_registry.get('obj')
318
252
self.assertEqual('foo', obj)
319
self.assertTrue(plugin_name in sys.modules)
253
self.failUnless(plugin_name in sys.modules)
321
255
# Now grab another object
322
256
func = a_registry.get('function')
341
275
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.assertEqual("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.assertEqual("bzrlib.tests.test_registry",
356
a_registry._get_module("obj"))