19
19
from bzrlib import (
24
26
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
from bzrlib.symbol_versioning import (
33
38
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
40
def test_docs(self):
54
41
"""docs() should return something reasonable about the Hooks."""
55
42
class MyHooks(Hooks):
44
hooks = MyHooks("bzrlib.tests.test_hooks", "some_hooks")
58
45
hooks['legacy'] = []
59
hook1 = HookPoint('post_tip_change',
46
hooks.add_hook('post_tip_change',
60
47
"Invoked after the tip of a branch changes. Called with "
61
"a ChangeBranchTipParams object.", (1, 4), None)
62
hook2 = HookPoint('pre_tip_change',
48
"a ChangeBranchTipParams object.", (1, 4))
49
hooks.add_hook('pre_tip_change',
63
50
"Invoked before the tip of a branch changes. Called with "
64
51
"a ChangeBranchTipParams object. Hooks should raise "
65
52
"TipChangeRejected to signal that a tip change is not permitted.",
67
hooks.create_hook(hook1)
68
hooks.create_hook(hook2)
69
54
self.assertEqualDiff(
93
78
"signal that a tip change is not permitted.\n", hooks.docs())
95
80
def test_install_named_hook_raises_unknown_hook(self):
81
hooks = Hooks("bzrlib.tests.test_hooks", "some_hooks")
97
82
self.assertRaises(errors.UnknownHook, hooks.install_named_hook, 'silly',
100
85
def test_install_named_hook_appends_known_hook(self):
86
hooks = Hooks("bzrlib.tests.test_hooks", "some_hooks")
102
87
hooks['set_rh'] = []
103
88
hooks.install_named_hook('set_rh', None, "demo")
104
89
self.assertEqual(hooks['set_rh'], [None])
106
91
def test_install_named_hook_and_retrieve_name(self):
92
hooks = Hooks("bzrlib.tests.test_hooks", "somehooks")
108
93
hooks['set_rh'] = []
109
94
hooks.install_named_hook('set_rh', None, "demo")
110
95
self.assertEqual("demo", hooks.get_hook_name(None))
97
def test_uninstall_named_hook(self):
98
hooks = Hooks("bzrlib.tests.test_hooks", "some_hooks")
99
hooks.add_hook('set_rh', "Set revision history", (2, 0))
100
hooks.install_named_hook('set_rh', None, "demo")
101
self.assertEqual(1, len(hooks["set_rh"]))
102
hooks.uninstall_named_hook("set_rh", "demo")
103
self.assertEqual(0, len(hooks["set_rh"]))
105
def test_uninstall_multiple_named_hooks(self):
106
# Multiple callbacks with the same label all get removed
107
hooks = Hooks("bzrlib.tests.test_hooks", "some_hooks")
108
hooks.add_hook('set_rh', "Set revision history", (2, 0))
109
hooks.install_named_hook('set_rh', 1, "demo")
110
hooks.install_named_hook('set_rh', 2, "demo")
111
hooks.install_named_hook('set_rh', 3, "othername")
112
self.assertEqual(3, len(hooks["set_rh"]))
113
hooks.uninstall_named_hook("set_rh", "demo")
114
self.assertEqual(1, len(hooks["set_rh"]))
116
def test_uninstall_named_hook_unknown_callable(self):
117
hooks = Hooks("bzrlib.tests.test_hooks", "some_hooks")
118
hooks.add_hook('set_rh', "Set revision hsitory", (2, 0))
119
self.assertRaises(KeyError, hooks.uninstall_named_hook, "set_rh",
122
def test_uninstall_named_hook_raises_unknown_hook(self):
123
hooks = Hooks("bzrlib.tests.test_hooks", "some_hooks")
124
self.assertRaises(errors.UnknownHook, hooks.uninstall_named_hook,
127
def test_uninstall_named_hook_old_style(self):
128
hooks = Hooks("bzrlib.tests.test_hooks", "some_hooks")
130
hooks.install_named_hook('set_rh', None, "demo")
131
self.assertRaises(errors.UnsupportedOperation,
132
hooks.uninstall_named_hook, "set_rh", "demo")
134
hooks = Hooks("bzrlib.tests.test_hooks", "TestHooks.hooks")
136
def test_install_lazy_named_hook(self):
137
# When the hook points are not yet registered the hook is
138
# added to the _lazy_hooks dictionary in bzrlib.hooks.
139
self.hooks.add_hook('set_rh', "doc", (0, 15))
140
set_rh = lambda: None
141
install_lazy_named_hook('bzrlib.tests.test_hooks',
142
'TestHooks.hooks', 'set_rh', set_rh, "demo")
143
set_rh_lazy_hooks = _mod_hooks._lazy_hooks[
144
('bzrlib.tests.test_hooks', 'TestHooks.hooks', 'set_rh')]
145
self.assertEqual(1, len(set_rh_lazy_hooks))
146
self.assertEqual(set_rh, set_rh_lazy_hooks[0][0].get_obj())
147
self.assertEqual("demo", set_rh_lazy_hooks[0][1])
148
self.assertEqual(list(TestHooks.hooks['set_rh']), [set_rh])
150
set_rh = lambda: None
152
def test_install_named_hook_lazy(self):
153
hooks = Hooks("bzrlib.tests.hooks", "some_hooks")
154
hooks['set_rh'] = HookPoint("set_rh", "doc", (0, 15), None)
155
hooks.install_named_hook_lazy('set_rh', 'bzrlib.tests.test_hooks',
156
'TestHooks.set_rh', "demo")
157
self.assertEqual(list(hooks['set_rh']), [TestHooks.set_rh])
159
def test_install_named_hook_lazy_old(self):
160
# An exception is raised if a lazy hook is raised for
161
# an old style hook point.
162
hooks = Hooks("bzrlib.tests.hooks", "some_hooks")
164
self.assertRaises(errors.UnsupportedOperation,
165
hooks.install_named_hook_lazy,
166
'set_rh', 'bzrlib.tests.test_hooks', 'TestHooks.set_rh',
169
def test_valid_lazy_hooks(self):
170
# Make sure that all the registered lazy hooks are referring to existing
171
# hook points which allow lazy registration.
172
for key, callbacks in _mod_hooks._lazy_hooks.iteritems():
173
(module_name, member_name, hook_name) = key
174
obj = pyutils.get_named_object(module_name, member_name)
175
self.assertEqual(obj._module, module_name)
176
self.assertEqual(obj._member_name, member_name)
177
self.assertTrue(hook_name in obj)
178
self.assertIs(callbacks, obj[hook_name]._callbacks)
113
181
class TestHook(tests.TestCase):
141
209
hook.hook(callback, "my callback")
142
210
self.assertEqual([callback], list(hook))
215
def test_lazy_hook(self):
216
hook = HookPoint("foo", "no docs", None, None)
218
"bzrlib.tests.test_hooks", "TestHook.lazy_callback",
220
self.assertEqual([TestHook.lazy_callback], list(hook))
222
def test_uninstall(self):
223
hook = HookPoint("foo", "no docs", None, None)
225
"bzrlib.tests.test_hooks", "TestHook.lazy_callback",
227
self.assertEqual([TestHook.lazy_callback], list(hook))
228
hook.uninstall("my callback")
229
self.assertEqual([], list(hook))
231
def test_uninstall_unknown(self):
232
hook = HookPoint("foo", "no docs", None, None)
233
self.assertRaises(KeyError, hook.uninstall, "my callback")
144
235
def test___repr(self):
145
236
# The repr should list all the callbacks, with names.
146
237
hook = HookPoint("foo", "no docs", None, None)