~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_registry.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-09-29 22:03:03 UTC
  • mfrom: (5416.2.6 jam-integration)
  • Revision ID: pqm@pqm.ubuntu.com-20100929220303-cr95h8iwtggco721
(mbp) Add 'break-lock --force'

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2008-2011 Canonical Ltd
 
1
# Copyright (C) 2006 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
20
20
import sys
21
21
 
22
22
from bzrlib import (
23
 
    branch,
 
23
    errors,
24
24
    osutils,
25
25
    registry,
26
26
    tests,
39
39
        a_registry = registry.Registry()
40
40
        self.register_stuff(a_registry)
41
41
 
42
 
        self.assertTrue(a_registry.default_key is None)
 
42
        self.failUnless(a_registry.default_key is None)
43
43
 
44
44
        # test get() (self.default_key is None)
45
45
        self.assertRaises(KeyError, a_registry.get)
49
49
 
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)
66
66
 
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')
71
71
 
72
72
        a_registry.register('one', 'one')
215
215
        # We create a registry with "official" objects and "hidden"
216
216
        # objects. The later represent the side effects that led to bug #277048
217
217
        # and #430510
218
 
        _registry = registry.Registry()
 
218
        self.registry =  registry.Registry()
219
219
 
220
220
        def register_more():
221
 
           _registry.register('hidden', None)
 
221
            self.registry.register('hidden', None)
222
222
 
223
 
        # Avoid closing over self by binding local variable
224
 
        self.registry = _registry
225
223
        self.registry.register('passive', None)
226
224
        self.registry.register('active', register_more)
227
225
        self.registry.register('passive-too', None)
231
229
            def get_obj(inner_self):
232
230
                # Surprise ! Getting a registered object (think lazy loaded
233
231
                # module) register yet another object !
234
 
                _registry.register('more hidden', None)
 
232
                self.registry.register('more hidden', None)
235
233
                return inner_self._obj
236
234
 
237
235
        self.registry.register('hacky', None)
272
270
        """
273
271
        os.mkdir('tmp')
274
272
        plugin_name = 'bzr_plugin_a_%s' % (osutils.rand_chars(4),)
275
 
        with open('tmp/'+plugin_name+'.py', 'wb') as f: f.write(contents)
 
273
        open('tmp/'+plugin_name+'.py', 'wb').write(contents)
276
274
        return plugin_name
277
275
 
278
276
    def create_simple_plugin(self):
288
286
            '\n\n'
289
287
        )
290
288
 
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'))
297
 
 
298
289
    def test_lazy_import_registry(self):
299
290
        plugin_name = self.create_simple_plugin()
300
291
        a_registry = registry.Registry()
306
297
        self.assertEqual(['function', 'klass', 'module', 'obj'],
307
298
                         sorted(a_registry.keys()))
308
299
        # The plugin should not be loaded until we grab the first object
309
 
        self.assertFalse(plugin_name in sys.modules)
 
300
        self.failIf(plugin_name in sys.modules)
310
301
 
311
302
        # By default the plugin won't be in the search path
312
303
        self.assertRaises(ImportError, a_registry.get, 'obj')
316
307
        try:
317
308
            obj = a_registry.get('obj')
318
309
            self.assertEqual('foo', obj)
319
 
            self.assertTrue(plugin_name in sys.modules)
 
310
            self.failUnless(plugin_name in sys.modules)
320
311
 
321
312
            # Now grab another object
322
313
            func = a_registry.get('function')
340
331
        finally:
341
332
            sys.path.remove(plugin_path)
342
333
 
343
 
    def test_lazy_import_get_module(self):
344
 
        a_registry = registry.Registry()
345
 
        a_registry.register_lazy('obj', "bzrlib.tests.test_registry",
346
 
            'object1')
347
 
        self.assertEquals("bzrlib.tests.test_registry",
348
 
            a_registry._get_module("obj"))
349
 
 
350
 
    def test_normal_get_module(self):
351
 
        class AThing(object):
352
 
            """Something"""
353
 
        a_registry = registry.Registry()
354
 
        a_registry.register("obj", AThing())
355
 
        self.assertEquals("bzrlib.tests.test_registry",
356
 
            a_registry._get_module("obj"))