17
17
"""Tests for the core Hooks logic."""
24
from bzrlib.hooks import (
29
install_lazy_named_hook,
28
31
known_hooks_key_to_object,
29
known_hooks_key_to_parent_and_attribute,
33
35
class TestHooks(tests.TestCase):
35
def test_create_hook_first(self):
37
doc = ("Invoked after changing the tip of a branch object. Called with"
38
"a bzrlib.branch.PostChangeBranchTipParams object")
39
hook = HookPoint("post_tip_change", doc, (0, 15), None)
40
hooks.create_hook(hook)
41
self.assertEqual(hook, hooks['post_tip_change'])
43
def test_create_hook_name_collision_errors(self):
45
doc = ("Invoked after changing the tip of a branch object. Called with"
46
"a bzrlib.branch.PostChangeBranchTipParams object")
47
hook = HookPoint("post_tip_change", doc, (0, 15), None)
48
hook2 = HookPoint("post_tip_change", None, None, None)
49
hooks.create_hook(hook)
50
self.assertRaises(errors.DuplicateKey, hooks.create_hook, hook2)
51
self.assertEqual(hook, hooks['post_tip_change'])
53
37
def test_docs(self):
54
38
"""docs() should return something reasonable about the Hooks."""
55
39
class MyHooks(Hooks):
41
hooks = MyHooks("breezy.tests.test_hooks", "some_hooks")
58
42
hooks['legacy'] = []
59
hook1 = HookPoint('post_tip_change',
43
hooks.add_hook('post_tip_change',
60
44
"Invoked after the tip of a branch changes. Called with "
61
"a ChangeBranchTipParams object.", (1, 4), None)
62
hook2 = HookPoint('pre_tip_change',
45
"a ChangeBranchTipParams object.", (1, 4))
46
hooks.add_hook('pre_tip_change',
63
47
"Invoked before the tip of a branch changes. Called with "
64
48
"a ChangeBranchTipParams object. Hooks should raise "
65
49
"TipChangeRejected to signal that a tip change is not permitted.",
67
hooks.create_hook(hook1)
68
hooks.create_hook(hook2)
69
51
self.assertEqualDiff(
93
75
"signal that a tip change is not permitted.\n", hooks.docs())
95
77
def test_install_named_hook_raises_unknown_hook(self):
78
hooks = Hooks("breezy.tests.test_hooks", "some_hooks")
97
79
self.assertRaises(errors.UnknownHook, hooks.install_named_hook, 'silly',
100
82
def test_install_named_hook_appends_known_hook(self):
83
hooks = Hooks("breezy.tests.test_hooks", "some_hooks")
102
84
hooks['set_rh'] = []
103
85
hooks.install_named_hook('set_rh', None, "demo")
104
86
self.assertEqual(hooks['set_rh'], [None])
106
88
def test_install_named_hook_and_retrieve_name(self):
89
hooks = Hooks("breezy.tests.test_hooks", "somehooks")
108
90
hooks['set_rh'] = []
109
91
hooks.install_named_hook('set_rh', None, "demo")
110
92
self.assertEqual("demo", hooks.get_hook_name(None))
94
def test_uninstall_named_hook(self):
95
hooks = Hooks("breezy.tests.test_hooks", "some_hooks")
96
hooks.add_hook('set_rh', "Set revision history", (2, 0))
97
hooks.install_named_hook('set_rh', None, "demo")
98
self.assertEqual(1, len(hooks["set_rh"]))
99
hooks.uninstall_named_hook("set_rh", "demo")
100
self.assertEqual(0, len(hooks["set_rh"]))
102
def test_uninstall_multiple_named_hooks(self):
103
# Multiple callbacks with the same label all get removed
104
hooks = Hooks("breezy.tests.test_hooks", "some_hooks")
105
hooks.add_hook('set_rh', "Set revision history", (2, 0))
106
hooks.install_named_hook('set_rh', 1, "demo")
107
hooks.install_named_hook('set_rh', 2, "demo")
108
hooks.install_named_hook('set_rh', 3, "othername")
109
self.assertEqual(3, len(hooks["set_rh"]))
110
hooks.uninstall_named_hook("set_rh", "demo")
111
self.assertEqual(1, len(hooks["set_rh"]))
113
def test_uninstall_named_hook_unknown_callable(self):
114
hooks = Hooks("breezy.tests.test_hooks", "some_hooks")
115
hooks.add_hook('set_rh', "Set revision hsitory", (2, 0))
116
self.assertRaises(KeyError, hooks.uninstall_named_hook, "set_rh",
119
def test_uninstall_named_hook_raises_unknown_hook(self):
120
hooks = Hooks("breezy.tests.test_hooks", "some_hooks")
121
self.assertRaises(errors.UnknownHook, hooks.uninstall_named_hook,
124
def test_uninstall_named_hook_old_style(self):
125
hooks = Hooks("breezy.tests.test_hooks", "some_hooks")
127
hooks.install_named_hook('set_rh', None, "demo")
128
self.assertRaises(errors.UnsupportedOperation,
129
hooks.uninstall_named_hook, "set_rh", "demo")
131
hooks = Hooks("breezy.tests.test_hooks", "TestHooks.hooks")
133
def test_install_lazy_named_hook(self):
134
# When the hook points are not yet registered the hook is
135
# added to the _lazy_hooks dictionary in breezy.hooks.
136
self.hooks.add_hook('set_rh', "doc", (0, 15))
137
set_rh = lambda: None
138
install_lazy_named_hook('breezy.tests.test_hooks',
139
'TestHooks.hooks', 'set_rh', set_rh, "demo")
140
set_rh_lazy_hooks = _mod_hooks._lazy_hooks[
141
('breezy.tests.test_hooks', 'TestHooks.hooks', 'set_rh')]
142
self.assertEqual(1, len(set_rh_lazy_hooks))
143
self.assertEqual(set_rh, set_rh_lazy_hooks[0][0].get_obj())
144
self.assertEqual("demo", set_rh_lazy_hooks[0][1])
145
self.assertEqual(list(TestHooks.hooks['set_rh']), [set_rh])
147
set_rh = lambda: None
149
def test_install_named_hook_lazy(self):
150
hooks = Hooks("breezy.tests.hooks", "some_hooks")
151
hooks['set_rh'] = HookPoint("set_rh", "doc", (0, 15), None)
152
hooks.install_named_hook_lazy('set_rh', 'breezy.tests.test_hooks',
153
'TestHooks.set_rh', "demo")
154
self.assertEqual(list(hooks['set_rh']), [TestHooks.set_rh])
156
def test_install_named_hook_lazy_old(self):
157
# An exception is raised if a lazy hook is raised for
158
# an old style hook point.
159
hooks = Hooks("breezy.tests.hooks", "some_hooks")
161
self.assertRaises(errors.UnsupportedOperation,
162
hooks.install_named_hook_lazy,
163
'set_rh', 'breezy.tests.test_hooks', 'TestHooks.set_rh',
166
def test_valid_lazy_hooks(self):
167
# Make sure that all the registered lazy hooks are referring to existing
168
# hook points which allow lazy registration.
169
for key, callbacks in _mod_hooks._lazy_hooks.iteritems():
170
(module_name, member_name, hook_name) = key
171
obj = pyutils.get_named_object(module_name, member_name)
172
self.assertEqual(obj._module, module_name)
173
self.assertEqual(obj._member_name, member_name)
174
self.assertTrue(hook_name in obj)
175
self.assertIs(callbacks, obj[hook_name]._callbacks)
113
178
class TestHook(tests.TestCase):
115
180
def test___init__(self):
116
181
doc = ("Invoked after changing the tip of a branch object. Called with"
117
"a bzrlib.branch.PostChangeBranchTipParams object")
182
"a breezy.branch.PostChangeBranchTipParams object")
118
183
hook = HookPoint("post_tip_change", doc, (0, 15), None)
119
184
self.assertEqual(doc, hook.__doc__)
120
185
self.assertEqual("post_tip_change", hook.name)
141
206
hook.hook(callback, "my callback")
142
207
self.assertEqual([callback], list(hook))
212
def test_lazy_hook(self):
213
hook = HookPoint("foo", "no docs", None, None)
215
"breezy.tests.test_hooks", "TestHook.lazy_callback",
217
self.assertEqual([TestHook.lazy_callback], list(hook))
219
def test_uninstall(self):
220
hook = HookPoint("foo", "no docs", None, None)
222
"breezy.tests.test_hooks", "TestHook.lazy_callback",
224
self.assertEqual([TestHook.lazy_callback], list(hook))
225
hook.uninstall("my callback")
226
self.assertEqual([], list(hook))
228
def test_uninstall_unknown(self):
229
hook = HookPoint("foo", "no docs", None, None)
230
self.assertRaises(KeyError, hook.uninstall, "my callback")
144
232
def test___repr(self):
145
233
# The repr should list all the callbacks, with names.
146
234
hook = HookPoint("foo", "no docs", None, None)