19
19
from bzrlib import (
26
24
from bzrlib.hooks import (
29
install_lazy_named_hook,
31
28
known_hooks_key_to_object,
32
29
known_hooks_key_to_parent_and_attribute,
34
from bzrlib.symbol_versioning import (
39
33
class TestHooks(tests.TestCase):
41
35
def test_create_hook_first(self):
42
hooks = Hooks("bzrlib.tests.test_hooks", "some_hooks")
43
37
doc = ("Invoked after changing the tip of a branch object. Called with"
44
38
"a bzrlib.branch.PostChangeBranchTipParams object")
45
39
hook = HookPoint("post_tip_change", doc, (0, 15), None)
46
self.applyDeprecated(deprecated_in((2, 4)), hooks.create_hook, hook)
40
hooks.create_hook(hook)
47
41
self.assertEqual(hook, hooks['post_tip_change'])
49
43
def test_create_hook_name_collision_errors(self):
50
hooks = Hooks("bzrlib.tests.test_hooks", "some_hooks")
51
45
doc = ("Invoked after changing the tip of a branch object. Called with"
52
46
"a bzrlib.branch.PostChangeBranchTipParams object")
53
47
hook = HookPoint("post_tip_change", doc, (0, 15), None)
54
48
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)
49
hooks.create_hook(hook)
50
self.assertRaises(errors.DuplicateKey, hooks.create_hook, hook2)
58
51
self.assertEqual(hook, hooks['post_tip_change'])
60
53
def test_docs(self):
61
54
"""docs() should return something reasonable about the Hooks."""
62
55
class MyHooks(Hooks):
64
hooks = MyHooks("bzrlib.tests.test_hooks", "some_hooks")
65
58
hooks['legacy'] = []
66
hooks.add_hook('post_tip_change',
59
hook1 = HookPoint('post_tip_change',
67
60
"Invoked after the tip of a branch changes. Called with "
68
"a ChangeBranchTipParams object.", (1, 4))
69
hooks.add_hook('pre_tip_change',
61
"a ChangeBranchTipParams object.", (1, 4), None)
62
hook2 = HookPoint('pre_tip_change',
70
63
"Invoked before the tip of a branch changes. Called with "
71
64
"a ChangeBranchTipParams object. Hooks should raise "
72
65
"TipChangeRejected to signal that a tip change is not permitted.",
67
hooks.create_hook(hook1)
68
hooks.create_hook(hook2)
74
69
self.assertEqualDiff(
98
93
"signal that a tip change is not permitted.\n", hooks.docs())
100
95
def test_install_named_hook_raises_unknown_hook(self):
101
hooks = Hooks("bzrlib.tests.test_hooks", "some_hooks")
102
97
self.assertRaises(errors.UnknownHook, hooks.install_named_hook, 'silly',
105
100
def test_install_named_hook_appends_known_hook(self):
106
hooks = Hooks("bzrlib.tests.test_hooks", "some_hooks")
107
102
hooks['set_rh'] = []
108
103
hooks.install_named_hook('set_rh', None, "demo")
109
104
self.assertEqual(hooks['set_rh'], [None])
111
106
def test_install_named_hook_and_retrieve_name(self):
112
hooks = Hooks("bzrlib.tests.test_hooks", "somehooks")
113
108
hooks['set_rh'] = []
114
109
hooks.install_named_hook('set_rh', None, "demo")
115
110
self.assertEqual("demo", hooks.get_hook_name(None))
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"]))
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"]))
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",
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,
147
def test_uninstall_named_hook_old_style(self):
148
hooks = Hooks("bzrlib.tests.test_hooks", "some_hooks")
150
hooks.install_named_hook('set_rh', None, "demo")
151
self.assertRaises(errors.UnsupportedOperation,
152
hooks.uninstall_named_hook, "set_rh", "demo")
154
hooks = Hooks("bzrlib.tests.test_hooks", "TestHooks.hooks")
156
def test_install_lazy_named_hook(self):
157
# When the hook points are not yet registered the hook is
158
# added to the _lazy_hooks dictionary in bzrlib.hooks.
159
self.hooks.add_hook('set_rh', "doc", (0, 15))
160
set_rh = lambda: None
161
install_lazy_named_hook('bzrlib.tests.test_hooks',
162
'TestHooks.hooks', 'set_rh', set_rh, "demo")
163
set_rh_lazy_hooks = _mod_hooks._lazy_hooks[
164
('bzrlib.tests.test_hooks', 'TestHooks.hooks', 'set_rh')]
165
self.assertEquals(1, len(set_rh_lazy_hooks))
166
self.assertEquals(set_rh, set_rh_lazy_hooks[0][0].get_obj())
167
self.assertEquals("demo", set_rh_lazy_hooks[0][1])
168
self.assertEqual(list(TestHooks.hooks['set_rh']), [set_rh])
170
set_rh = lambda: None
172
def test_install_named_hook_lazy(self):
173
hooks = Hooks("bzrlib.tests.hooks", "some_hooks")
174
hooks['set_rh'] = HookPoint("set_rh", "doc", (0, 15), None)
175
hooks.install_named_hook_lazy('set_rh', 'bzrlib.tests.test_hooks',
176
'TestHooks.set_rh', "demo")
177
self.assertEqual(list(hooks['set_rh']), [TestHooks.set_rh])
179
def test_install_named_hook_lazy_old(self):
180
# An exception is raised if a lazy hook is raised for
181
# an old style hook point.
182
hooks = Hooks("bzrlib.tests.hooks", "some_hooks")
184
self.assertRaises(errors.UnsupportedOperation,
185
hooks.install_named_hook_lazy,
186
'set_rh', 'bzrlib.tests.test_hooks', 'TestHooks.set_rh',
189
def test_valid_lazy_hooks(self):
190
# Make sure that all the registered lazy hooks are referring to existing
191
# hook points which allow lazy registration.
192
for key, callbacks in _mod_hooks._lazy_hooks.iteritems():
193
(module_name, member_name, hook_name) = key
194
obj = pyutils.get_named_object(module_name, member_name)
195
self.assertEquals(obj._module, module_name)
196
self.assertEquals(obj._member_name, member_name)
197
self.assertTrue(hook_name in obj)
198
self.assertIs(callbacks, obj[hook_name]._callbacks)
201
113
class TestHook(tests.TestCase):
229
141
hook.hook(callback, "my callback")
230
142
self.assertEqual([callback], list(hook))
235
def test_lazy_hook(self):
236
hook = HookPoint("foo", "no docs", None, None)
238
"bzrlib.tests.test_hooks", "TestHook.lazy_callback",
240
self.assertEqual([TestHook.lazy_callback], list(hook))
242
def test_uninstall(self):
243
hook = HookPoint("foo", "no docs", None, None)
245
"bzrlib.tests.test_hooks", "TestHook.lazy_callback",
247
self.assertEqual([TestHook.lazy_callback], list(hook))
248
hook.uninstall("my callback")
249
self.assertEqual([], list(hook))
251
def test_uninstall_unknown(self):
252
hook = HookPoint("foo", "no docs", None, None)
253
self.assertRaises(KeyError, hook.uninstall, "my callback")
255
144
def test___repr(self):
256
145
# The repr should list all the callbacks, with names.
257
146
hook = HookPoint("foo", "no docs", None, None)
286
175
self.assertIs(branch.Branch.hooks,
287
176
known_hooks_key_to_object(('bzrlib.branch', 'Branch.hooks')))
289
def test_known_hooks_key_to_parent_and_attribute_deprecated(self):
290
self.assertEqual((branch.Branch, 'hooks'),
291
self.applyDeprecated(deprecated_in((2,3)),
292
known_hooks_key_to_parent_and_attribute,
293
('bzrlib.branch', 'Branch.hooks')))
294
self.assertEqual((branch, 'Branch'),
295
self.applyDeprecated(deprecated_in((2,3)),
296
known_hooks_key_to_parent_and_attribute,
297
('bzrlib.branch', 'Branch')))
299
178
def test_known_hooks_key_to_parent_and_attribute(self):
300
179
self.assertEqual((branch.Branch, 'hooks'),
301
known_hooks.key_to_parent_and_attribute(
180
known_hooks_key_to_parent_and_attribute(
302
181
('bzrlib.branch', 'Branch.hooks')))
303
182
self.assertEqual((branch, 'Branch'),
304
known_hooks.key_to_parent_and_attribute(
183
known_hooks_key_to_parent_and_attribute(
305
184
('bzrlib.branch', 'Branch')))