13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
"""Tests for the core Hooks logic."""
19
from bzrlib import branch, errors
19
20
from bzrlib.hooks import (
24
known_hooks_key_to_object,
25
known_hooks_key_to_parent_and_attribute,
22
27
from bzrlib.errors import (
31
from bzrlib.symbol_versioning import one_five
26
32
from bzrlib.tests import TestCase
29
35
class TestHooks(TestCase):
37
def test_create_hook_first(self):
39
doc = ("Invoked after changing the tip of a branch object. Called with"
40
"a bzrlib.branch.PostChangeBranchTipParams object")
41
hook = HookPoint("post_tip_change", doc, (0, 15), None)
42
hooks.create_hook(hook)
43
self.assertEqual(hook, hooks['post_tip_change'])
45
def test_create_hook_name_collision_errors(self):
47
doc = ("Invoked after changing the tip of a branch object. Called with"
48
"a bzrlib.branch.PostChangeBranchTipParams object")
49
hook = HookPoint("post_tip_change", doc, (0, 15), None)
50
hook2 = HookPoint("post_tip_change", None, None, None)
51
hooks.create_hook(hook)
52
self.assertRaises(errors.DuplicateKey, hooks.create_hook, hook2)
53
self.assertEqual(hook, hooks['post_tip_change'])
56
"""docs() should return something reasonable about the Hooks."""
61
hook1 = HookPoint('post_tip_change',
62
"Invoked after the tip of a branch changes. Called with "
63
"a ChangeBranchTipParams object.", (1, 4), None)
64
hook2 = HookPoint('pre_tip_change',
65
"Invoked before the tip of a branch changes. Called with "
66
"a ChangeBranchTipParams object. Hooks should raise "
67
"TipChangeRejected to signal that a tip change is not permitted.",
69
hooks.create_hook(hook1)
70
hooks.create_hook(hook2)
78
"An old-style hook. For documentation see the __init__ method of 'MyHooks'\n"
83
"Introduced in: 1.4\n"
84
"Deprecated in: Not deprecated\n"
86
"Invoked after the tip of a branch changes. Called with a\n"
87
"ChangeBranchTipParams object.\n"
92
"Introduced in: 1.6\n"
93
"Deprecated in: Not deprecated\n"
95
"Invoked before the tip of a branch changes. Called with a\n"
96
"ChangeBranchTipParams object. Hooks should raise TipChangeRejected to\n"
97
"signal that a tip change is not permitted.\n", hooks.docs())
31
99
def test_install_hook_raises_unknown_hook(self):
32
100
"""install_hook should raise UnknownHook if a hook is unknown."""
34
self.assertRaises(UnknownHook, hooks.install_hook, 'silly', None)
102
self.assertRaises(UnknownHook, self.applyDeprecated, one_five,
103
hooks.install_hook, 'silly', None)
36
105
def test_install_hook_appends_known_hook(self):
37
106
"""install_hook should append the callable for known hooks."""
39
108
hooks['set_rh'] = []
40
hooks.install_hook('set_rh', None)
41
self.assertEqual(hooks['set_rh'], [None])
109
self.applyDeprecated(one_five, hooks.install_hook, 'set_rh', None)
110
self.assertEqual(hooks['set_rh'], [None])
112
def test_install_named_hook_raises_unknown_hook(self):
114
self.assertRaises(UnknownHook, hooks.install_named_hook, 'silly',
117
def test_install_named_hook_appends_known_hook(self):
120
hooks.install_named_hook('set_rh', None, "demo")
121
self.assertEqual(hooks['set_rh'], [None])
123
def test_install_named_hook_and_retrieve_name(self):
126
hooks.install_named_hook('set_rh', None, "demo")
127
self.assertEqual("demo", hooks.get_hook_name(None))
43
129
def test_name_hook_and_retrieve_name(self):
44
130
"""name_hook puts the name in the names mapping."""
46
132
hooks['set_rh'] = []
47
hooks.install_hook('set_rh', None)
133
self.applyDeprecated(one_five, hooks.install_hook, 'set_rh', None)
48
134
hooks.name_hook(None, 'demo')
49
135
self.assertEqual("demo", hooks.get_hook_name(None))
51
137
def test_get_unnamed_hook_name_is_unnamed(self):
53
139
hooks['set_rh'] = []
54
hooks.install_hook('set_rh', None)
140
self.applyDeprecated(one_five, hooks.install_hook, 'set_rh', None)
55
141
self.assertEqual("No hook name", hooks.get_hook_name(None))
144
class TestHook(TestCase):
146
def test___init__(self):
147
doc = ("Invoked after changing the tip of a branch object. Called with"
148
"a bzrlib.branch.PostChangeBranchTipParams object")
149
hook = HookPoint("post_tip_change", doc, (0, 15), None)
150
self.assertEqual(doc, hook.__doc__)
151
self.assertEqual("post_tip_change", hook.name)
152
self.assertEqual((0, 15), hook.introduced)
153
self.assertEqual(None, hook.deprecated)
154
self.assertEqual([], list(hook))
157
doc = ("Invoked after changing the tip of a branch object. Called with"
158
" a bzrlib.branch.PostChangeBranchTipParams object")
159
hook = HookPoint("post_tip_change", doc, (0, 15), None)
160
self.assertEqual("post_tip_change\n"
163
"Introduced in: 0.15\n"
164
"Deprecated in: Not deprecated\n"
166
"Invoked after changing the tip of a branch object. Called with a\n"
167
"bzrlib.branch.PostChangeBranchTipParams object\n", hook.docs())
170
hook = HookPoint("foo", "no docs", None, None)
173
hook.hook(callback, "my callback")
174
self.assertEqual([callback], list(hook))
176
def test___repr(self):
177
# The repr should list all the callbacks, with names.
178
hook = HookPoint("foo", "no docs", None, None)
181
hook.hook(callback, "my callback")
182
callback_repr = repr(callback)
184
'<HookPoint(foo), callbacks=[%s(my callback)]>' %
185
callback_repr, repr(hook))
188
class TestHookRegistry(TestCase):
190
def test_items_are_reasonable_keys(self):
191
# All the items in the known_hooks registry need to map from
192
# (module_name, member_name) tuples to the callable used to get an
193
# empty Hooks of for that attribute. This is used to support the test
194
# suite which needs to generate empty hooks (and HookPoints) to ensure
195
# isolation and prevent tests failing spuriously.
196
for key, factory in known_hooks.items():
197
self.assertTrue(callable(factory),
198
"The factory(%r) for %r is not callable" % (factory, key))
199
obj = known_hooks_key_to_object(key)
200
self.assertIsInstance(obj, Hooks)
201
new_hooks = factory()
202
self.assertIsInstance(obj, Hooks)
203
self.assertEqual(type(obj), type(new_hooks))
205
def test_known_hooks_key_to_object(self):
206
self.assertIs(branch.Branch.hooks,
207
known_hooks_key_to_object(('bzrlib.branch', 'Branch.hooks')))
209
def test_known_hooks_key_to_parent_and_attribute(self):
210
self.assertEqual((branch.Branch, 'hooks'),
211
known_hooks_key_to_parent_and_attribute(
212
('bzrlib.branch', 'Branch.hooks')))
213
self.assertEqual((branch, 'Branch'),
214
known_hooks_key_to_parent_and_attribute(
215
('bzrlib.branch', 'Branch')))