~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_hooks.py

(gz) Fix test failure on alpha by correcting format string for
 gc_chk_sha1_record (Martin [gz])

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
from bzrlib import (
20
20
    branch,
21
21
    errors,
22
 
    hooks as _mod_hooks,
23
22
    pyutils,
24
23
    tests,
25
24
    )
30
29
    known_hooks,
31
30
    known_hooks_key_to_object,
32
31
    known_hooks_key_to_parent_and_attribute,
 
32
    _lazy_hooks,
33
33
    )
34
34
from bzrlib.symbol_versioning import (
35
35
    deprecated_in,
39
39
class TestHooks(tests.TestCase):
40
40
 
41
41
    def test_create_hook_first(self):
42
 
        hooks = Hooks("bzrlib.tests.test_hooks", "some_hooks")
 
42
        hooks = Hooks()
43
43
        doc = ("Invoked after changing the tip of a branch object. Called with"
44
44
            "a bzrlib.branch.PostChangeBranchTipParams object")
45
45
        hook = HookPoint("post_tip_change", doc, (0, 15), None)
46
 
        self.applyDeprecated(deprecated_in((2, 4)), hooks.create_hook, hook)
 
46
        hooks.create_hook(hook)
47
47
        self.assertEqual(hook, hooks['post_tip_change'])
48
48
 
49
49
    def test_create_hook_name_collision_errors(self):
50
 
        hooks = Hooks("bzrlib.tests.test_hooks", "some_hooks")
 
50
        hooks = Hooks()
51
51
        doc = ("Invoked after changing the tip of a branch object. Called with"
52
52
            "a bzrlib.branch.PostChangeBranchTipParams object")
53
53
        hook = HookPoint("post_tip_change", doc, (0, 15), None)
54
54
        hook2 = HookPoint("post_tip_change", None, None, None)
55
 
        self.applyDeprecated(deprecated_in((2, 4)), hooks.create_hook, hook)
56
 
        self.assertRaises(errors.DuplicateKey, self.applyDeprecated,
57
 
            deprecated_in((2, 4, 0)), hooks.create_hook, hook2)
 
55
        hooks.create_hook(hook)
 
56
        self.assertRaises(errors.DuplicateKey, hooks.create_hook, hook2)
58
57
        self.assertEqual(hook, hooks['post_tip_change'])
59
58
 
60
59
    def test_docs(self):
61
60
        """docs() should return something reasonable about the Hooks."""
62
61
        class MyHooks(Hooks):
63
62
            pass
64
 
        hooks = MyHooks("bzrlib.tests.test_hooks", "some_hooks")
 
63
        hooks = MyHooks()
65
64
        hooks['legacy'] = []
66
 
        hooks.add_hook('post_tip_change',
 
65
        hook1 = HookPoint('post_tip_change',
67
66
            "Invoked after the tip of a branch changes. Called with "
68
 
            "a ChangeBranchTipParams object.", (1, 4))
69
 
        hooks.add_hook('pre_tip_change',
 
67
            "a ChangeBranchTipParams object.", (1, 4), None)
 
68
        hook2 = HookPoint('pre_tip_change',
70
69
            "Invoked before the tip of a branch changes. Called with "
71
70
            "a ChangeBranchTipParams object. Hooks should raise "
72
71
            "TipChangeRejected to signal that a tip change is not permitted.",
73
72
            (1, 6), None)
 
73
        hooks.create_hook(hook1)
 
74
        hooks.create_hook(hook2)
74
75
        self.assertEqualDiff(
75
76
            "MyHooks\n"
76
77
            "-------\n"
98
99
            "signal that a tip change is not permitted.\n", hooks.docs())
99
100
 
100
101
    def test_install_named_hook_raises_unknown_hook(self):
101
 
        hooks = Hooks("bzrlib.tests.test_hooks", "some_hooks")
 
102
        hooks = Hooks()
102
103
        self.assertRaises(errors.UnknownHook, hooks.install_named_hook, 'silly',
103
104
                          None, "")
104
105
 
105
106
    def test_install_named_hook_appends_known_hook(self):
106
 
        hooks = Hooks("bzrlib.tests.test_hooks", "some_hooks")
 
107
        hooks = Hooks()
107
108
        hooks['set_rh'] = []
108
109
        hooks.install_named_hook('set_rh', None, "demo")
109
110
        self.assertEqual(hooks['set_rh'], [None])
110
111
 
111
112
    def test_install_named_hook_and_retrieve_name(self):
112
 
        hooks = Hooks("bzrlib.tests.test_hooks", "somehooks")
 
113
        hooks = Hooks()
113
114
        hooks['set_rh'] = []
114
115
        hooks.install_named_hook('set_rh', None, "demo")
115
116
        self.assertEqual("demo", hooks.get_hook_name(None))
116
117
 
117
 
    def test_uninstall_named_hook(self):
118
 
        hooks = Hooks("bzrlib.tests.test_hooks", "some_hooks")
119
 
        hooks.add_hook('set_rh', "Set revision history", (2, 0))
120
 
        hooks.install_named_hook('set_rh', None, "demo")
121
 
        self.assertEqual(1, len(hooks["set_rh"]))
122
 
        hooks.uninstall_named_hook("set_rh", "demo")
123
 
        self.assertEqual(0, len(hooks["set_rh"]))
124
 
 
125
 
    def test_uninstall_multiple_named_hooks(self):
126
 
        # Multiple callbacks with the same label all get removed
127
 
        hooks = Hooks("bzrlib.tests.test_hooks", "some_hooks")
128
 
        hooks.add_hook('set_rh', "Set revision history", (2, 0))
129
 
        hooks.install_named_hook('set_rh', 1, "demo")
130
 
        hooks.install_named_hook('set_rh', 2, "demo")
131
 
        hooks.install_named_hook('set_rh', 3, "othername")
132
 
        self.assertEqual(3, len(hooks["set_rh"]))
133
 
        hooks.uninstall_named_hook("set_rh", "demo")
134
 
        self.assertEqual(1, len(hooks["set_rh"]))
135
 
 
136
 
    def test_uninstall_named_hook_unknown_callable(self):
137
 
        hooks = Hooks("bzrlib.tests.test_hooks", "some_hooks")
138
 
        hooks.add_hook('set_rh', "Set revision hsitory", (2, 0))
139
 
        self.assertRaises(KeyError, hooks.uninstall_named_hook, "set_rh",
140
 
            "demo")
141
 
 
142
 
    def test_uninstall_named_hook_raises_unknown_hook(self):
143
 
        hooks = Hooks("bzrlib.tests.test_hooks", "some_hooks")
144
 
        self.assertRaises(errors.UnknownHook, hooks.uninstall_named_hook,
145
 
            'silly', "")
146
 
 
147
 
    def test_uninstall_named_hook_old_style(self):
148
 
        hooks = Hooks("bzrlib.tests.test_hooks", "some_hooks")
149
 
        hooks["set_rh"] = []
150
 
        hooks.install_named_hook('set_rh', None, "demo")
151
 
        self.assertRaises(errors.UnsupportedOperation,
152
 
            hooks.uninstall_named_hook, "set_rh", "demo")
153
 
 
154
118
    hooks = Hooks("bzrlib.tests.test_hooks", "TestHooks.hooks")
155
119
 
156
120
    def test_install_lazy_named_hook(self):
160
124
        set_rh = lambda: None
161
125
        install_lazy_named_hook('bzrlib.tests.test_hooks',
162
126
            'TestHooks.hooks', 'set_rh', set_rh, "demo")
163
 
        set_rh_lazy_hooks = _mod_hooks._lazy_hooks[
 
127
        set_rh_lazy_hooks = _lazy_hooks[
164
128
            ('bzrlib.tests.test_hooks', 'TestHooks.hooks', 'set_rh')]
165
129
        self.assertEquals(1, len(set_rh_lazy_hooks))
166
130
        self.assertEquals(set_rh, set_rh_lazy_hooks[0][0].get_obj())
170
134
    set_rh = lambda: None
171
135
 
172
136
    def test_install_named_hook_lazy(self):
173
 
        hooks = Hooks("bzrlib.tests.hooks", "some_hooks")
 
137
        hooks = Hooks()
174
138
        hooks['set_rh'] = HookPoint("set_rh", "doc", (0, 15), None)
175
139
        hooks.install_named_hook_lazy('set_rh', 'bzrlib.tests.test_hooks',
176
140
            'TestHooks.set_rh', "demo")
179
143
    def test_install_named_hook_lazy_old(self):
180
144
        # An exception is raised if a lazy hook is raised for
181
145
        # an old style hook point.
182
 
        hooks = Hooks("bzrlib.tests.hooks", "some_hooks")
 
146
        hooks = Hooks()
183
147
        hooks['set_rh'] = []
184
148
        self.assertRaises(errors.UnsupportedOperation,
185
149
            hooks.install_named_hook_lazy,
189
153
    def test_valid_lazy_hooks(self):
190
154
        # Make sure that all the registered lazy hooks are referring to existing
191
155
        # hook points which allow lazy registration.
192
 
        for key, callbacks in _mod_hooks._lazy_hooks.iteritems():
 
156
        for key, callbacks in _lazy_hooks.iteritems():
193
157
            (module_name, member_name, hook_name) = key
194
158
            obj = pyutils.get_named_object(module_name, member_name)
195
159
            self.assertEquals(obj._module, module_name)
239
203
            "my callback")
240
204
        self.assertEqual([TestHook.lazy_callback], list(hook))
241
205
 
242
 
    def test_uninstall(self):
243
 
        hook = HookPoint("foo", "no docs", None, None)
244
 
        hook.hook_lazy(
245
 
            "bzrlib.tests.test_hooks", "TestHook.lazy_callback",
246
 
            "my callback")
247
 
        self.assertEqual([TestHook.lazy_callback], list(hook))
248
 
        hook.uninstall("my callback")
249
 
        self.assertEqual([], list(hook))
250
 
 
251
 
    def test_uninstall_unknown(self):
252
 
        hook = HookPoint("foo", "no docs", None, None)
253
 
        self.assertRaises(KeyError, hook.uninstall, "my callback")
254
 
 
255
206
    def test___repr(self):
256
207
        # The repr should list all the callbacks, with names.
257
208
        hook = HookPoint("foo", "no docs", None, None)