1
# Copyright (C) 2007-2010 Canonical Ltd
1
# Copyright (C) 2007 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
17
"""Tests for the core Hooks logic."""
19
from bzrlib import errors
24
20
from bzrlib.hooks import (
28
known_hooks_key_to_object,
29
known_hooks_key_to_parent_and_attribute,
33
class TestHooks(tests.TestCase):
24
from bzrlib.errors import (
28
from bzrlib.symbol_versioning import one_five
29
from bzrlib.tests import TestCase
32
class TestHooks(TestCase):
35
34
def test_create_hook_first(self):
67
64
hooks.create_hook(hook1)
68
65
hooks.create_hook(hook2)
76
"An old-style hook. For documentation see the __init__ method of 'MyHooks'\n"
70
"An old-style hook. For documentation see the __init__ method of 'Hooks'\n"
78
72
"post_tip_change\n"
81
75
"Introduced in: 1.4\n"
76
"Deprecated in: Not deprecated\n"
83
78
"Invoked after the tip of a branch changes. Called with a\n"
84
79
"ChangeBranchTipParams object.\n"
89
84
"Introduced in: 1.6\n"
85
"Deprecated in: Not deprecated\n"
91
87
"Invoked before the tip of a branch changes. Called with a\n"
92
88
"ChangeBranchTipParams object. Hooks should raise TipChangeRejected to\n"
93
89
"signal that a tip change is not permitted.\n", hooks.docs())
91
def test_install_hook_raises_unknown_hook(self):
92
"""install_hook should raise UnknownHook if a hook is unknown."""
94
self.assertRaises(UnknownHook, self.applyDeprecated, one_five,
95
hooks.install_hook, 'silly', None)
97
def test_install_hook_appends_known_hook(self):
98
"""install_hook should append the callable for known hooks."""
101
self.applyDeprecated(one_five, hooks.install_hook, 'set_rh', None)
102
self.assertEqual(hooks['set_rh'], [None])
95
104
def test_install_named_hook_raises_unknown_hook(self):
97
self.assertRaises(errors.UnknownHook, hooks.install_named_hook, 'silly',
106
self.assertRaises(UnknownHook, hooks.install_named_hook, 'silly',
100
109
def test_install_named_hook_appends_known_hook(self):
109
118
hooks.install_named_hook('set_rh', None, "demo")
110
119
self.assertEqual("demo", hooks.get_hook_name(None))
113
class TestHook(tests.TestCase):
121
def test_name_hook_and_retrieve_name(self):
122
"""name_hook puts the name in the names mapping."""
125
self.applyDeprecated(one_five, hooks.install_hook, 'set_rh', None)
126
hooks.name_hook(None, 'demo')
127
self.assertEqual("demo", hooks.get_hook_name(None))
129
def test_get_unnamed_hook_name_is_unnamed(self):
132
self.applyDeprecated(one_five, hooks.install_hook, 'set_rh', None)
133
self.assertEqual("No hook name", hooks.get_hook_name(None))
136
class TestHook(TestCase):
115
138
def test___init__(self):
116
139
doc = ("Invoked after changing the tip of a branch object. Called with"
127
150
" a bzrlib.branch.PostChangeBranchTipParams object")
128
151
hook = HookPoint("post_tip_change", doc, (0, 15), None)
129
152
self.assertEqual("post_tip_change\n"
132
155
"Introduced in: 0.15\n"
156
"Deprecated in: Not deprecated\n"
134
158
"Invoked after changing the tip of a branch object. Called with a\n"
135
159
"bzrlib.branch.PostChangeBranchTipParams object\n", hook.docs())
151
175
self.assertEqual(
152
176
'<HookPoint(foo), callbacks=[%s(my callback)]>' %
153
177
callback_repr, repr(hook))
156
class TestHookRegistry(tests.TestCase):
158
def test_items_are_reasonable_keys(self):
159
# All the items in the known_hooks registry need to map from
160
# (module_name, member_name) tuples to the callable used to get an
161
# empty Hooks for that attribute. This is used to support the test
162
# suite which needs to generate empty hooks (and HookPoints) to ensure
163
# isolation and prevent tests failing spuriously.
164
for key, factory in known_hooks.items():
165
self.assertTrue(callable(factory),
166
"The factory(%r) for %r is not callable" % (factory, key))
167
obj = known_hooks_key_to_object(key)
168
self.assertIsInstance(obj, Hooks)
169
new_hooks = factory()
170
self.assertIsInstance(obj, Hooks)
171
self.assertEqual(type(obj), type(new_hooks))
172
self.assertEqual("No hook name", new_hooks.get_hook_name(None))
174
def test_known_hooks_key_to_object(self):
175
self.assertIs(branch.Branch.hooks,
176
known_hooks_key_to_object(('bzrlib.branch', 'Branch.hooks')))
178
def test_known_hooks_key_to_parent_and_attribute(self):
179
self.assertEqual((branch.Branch, 'hooks'),
180
known_hooks_key_to_parent_and_attribute(
181
('bzrlib.branch', 'Branch.hooks')))
182
self.assertEqual((branch, 'Branch'),
183
known_hooks_key_to_parent_and_attribute(
184
('bzrlib.branch', 'Branch')))