1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
# Copyright (C) 2007-2010 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
"""Tests for the core Hooks logic."""
from bzrlib import (
branch,
errors,
tests,
)
from bzrlib.hooks import (
HookPoint,
Hooks,
known_hooks,
known_hooks_key_to_object,
known_hooks_key_to_parent_and_attribute,
)
from bzrlib.symbol_versioning import (
deprecated_in,
)
class TestHooks(tests.TestCase):
def test_create_hook_first(self):
hooks = Hooks()
doc = ("Invoked after changing the tip of a branch object. Called with"
"a bzrlib.branch.PostChangeBranchTipParams object")
hook = HookPoint("post_tip_change", doc, (0, 15), None)
hooks.create_hook(hook)
self.assertEqual(hook, hooks['post_tip_change'])
def test_create_hook_name_collision_errors(self):
hooks = Hooks()
doc = ("Invoked after changing the tip of a branch object. Called with"
"a bzrlib.branch.PostChangeBranchTipParams object")
hook = HookPoint("post_tip_change", doc, (0, 15), None)
hook2 = HookPoint("post_tip_change", None, None, None)
hooks.create_hook(hook)
self.assertRaises(errors.DuplicateKey, hooks.create_hook, hook2)
self.assertEqual(hook, hooks['post_tip_change'])
def test_docs(self):
"""docs() should return something reasonable about the Hooks."""
class MyHooks(Hooks):
pass
hooks = MyHooks()
hooks['legacy'] = []
hook1 = HookPoint('post_tip_change',
"Invoked after the tip of a branch changes. Called with "
"a ChangeBranchTipParams object.", (1, 4), None)
hook2 = HookPoint('pre_tip_change',
"Invoked before the tip of a branch changes. Called with "
"a ChangeBranchTipParams object. Hooks should raise "
"TipChangeRejected to signal that a tip change is not permitted.",
(1, 6), None)
hooks.create_hook(hook1)
hooks.create_hook(hook2)
self.assertEqualDiff(
"MyHooks\n"
"-------\n"
"\n"
"legacy\n"
"~~~~~~\n"
"\n"
"An old-style hook. For documentation see the __init__ method of 'MyHooks'\n"
"\n"
"post_tip_change\n"
"~~~~~~~~~~~~~~~\n"
"\n"
"Introduced in: 1.4\n"
"\n"
"Invoked after the tip of a branch changes. Called with a\n"
"ChangeBranchTipParams object.\n"
"\n"
"pre_tip_change\n"
"~~~~~~~~~~~~~~\n"
"\n"
"Introduced in: 1.6\n"
"\n"
"Invoked before the tip of a branch changes. Called with a\n"
"ChangeBranchTipParams object. Hooks should raise TipChangeRejected to\n"
"signal that a tip change is not permitted.\n", hooks.docs())
def test_install_named_hook_raises_unknown_hook(self):
hooks = Hooks()
self.assertRaises(errors.UnknownHook, hooks.install_named_hook, 'silly',
None, "")
def test_install_named_hook_appends_known_hook(self):
hooks = Hooks()
hooks['set_rh'] = []
hooks.install_named_hook('set_rh', None, "demo")
self.assertEqual(hooks['set_rh'], [None])
def test_install_named_hook_and_retrieve_name(self):
hooks = Hooks()
hooks['set_rh'] = []
hooks.install_named_hook('set_rh', None, "demo")
self.assertEqual("demo", hooks.get_hook_name(None))
class TestHook(tests.TestCase):
def test___init__(self):
doc = ("Invoked after changing the tip of a branch object. Called with"
"a bzrlib.branch.PostChangeBranchTipParams object")
hook = HookPoint("post_tip_change", doc, (0, 15), None)
self.assertEqual(doc, hook.__doc__)
self.assertEqual("post_tip_change", hook.name)
self.assertEqual((0, 15), hook.introduced)
self.assertEqual(None, hook.deprecated)
self.assertEqual([], list(hook))
def test_docs(self):
doc = ("Invoked after changing the tip of a branch object. Called with"
" a bzrlib.branch.PostChangeBranchTipParams object")
hook = HookPoint("post_tip_change", doc, (0, 15), None)
self.assertEqual("post_tip_change\n"
"~~~~~~~~~~~~~~~\n"
"\n"
"Introduced in: 0.15\n"
"\n"
"Invoked after changing the tip of a branch object. Called with a\n"
"bzrlib.branch.PostChangeBranchTipParams object\n", hook.docs())
def test_hook(self):
hook = HookPoint("foo", "no docs", None, None)
def callback():
pass
hook.hook(callback, "my callback")
self.assertEqual([callback], list(hook))
def test___repr(self):
# The repr should list all the callbacks, with names.
hook = HookPoint("foo", "no docs", None, None)
def callback():
pass
hook.hook(callback, "my callback")
callback_repr = repr(callback)
self.assertEqual(
'<HookPoint(foo), callbacks=[%s(my callback)]>' %
callback_repr, repr(hook))
class TestHookRegistry(tests.TestCase):
def test_items_are_reasonable_keys(self):
# All the items in the known_hooks registry need to map from
# (module_name, member_name) tuples to the callable used to get an
# empty Hooks for that attribute. This is used to support the test
# suite which needs to generate empty hooks (and HookPoints) to ensure
# isolation and prevent tests failing spuriously.
for key, factory in known_hooks.items():
self.assertTrue(callable(factory),
"The factory(%r) for %r is not callable" % (factory, key))
obj = known_hooks_key_to_object(key)
self.assertIsInstance(obj, Hooks)
new_hooks = factory()
self.assertIsInstance(obj, Hooks)
self.assertEqual(type(obj), type(new_hooks))
self.assertEqual("No hook name", new_hooks.get_hook_name(None))
def test_known_hooks_key_to_object(self):
self.assertIs(branch.Branch.hooks,
known_hooks_key_to_object(('bzrlib.branch', 'Branch.hooks')))
def test_known_hooks_key_to_parent_and_attribute_deprecated(self):
self.assertEqual((branch.Branch, 'hooks'),
self.applyDeprecated(deprecated_in((2,3)),
known_hooks_key_to_parent_and_attribute,
('bzrlib.branch', 'Branch.hooks')))
self.assertEqual((branch, 'Branch'),
self.applyDeprecated(deprecated_in((2,3)),
known_hooks_key_to_parent_and_attribute,
('bzrlib.branch', 'Branch')))
def test_known_hooks_key_to_parent_and_attribute(self):
self.assertEqual((branch.Branch, 'hooks'),
known_hooks.key_to_parent_and_attribute(
('bzrlib.branch', 'Branch.hooks')))
self.assertEqual((branch, 'Branch'),
known_hooks.key_to_parent_and_attribute(
('bzrlib.branch', 'Branch')))
|