~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_hooks.py

  • Committer: John Arbash Meinel
  • Author(s): Mark Hammond
  • Date: 2008-09-09 17:02:21 UTC
  • mto: This revision was merged to the branch mainline in revision 3697.
  • Revision ID: john@arbash-meinel.com-20080909170221-svim3jw2mrz0amp3
An updated transparent icon for bzr.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007-2012, 2016 Canonical Ltd
 
1
# Copyright (C) 2007 Canonical Ltd
2
2
#
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
12
12
#
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
16
16
 
17
17
"""Tests for the core Hooks logic."""
18
18
 
19
 
from bzrlib import (
20
 
    branch,
21
 
    errors,
22
 
    hooks as _mod_hooks,
23
 
    pyutils,
24
 
    tests,
25
 
    )
26
19
from bzrlib.hooks import (
27
 
    HookPoint,
28
20
    Hooks,
29
 
    install_lazy_named_hook,
30
 
    known_hooks,
31
 
    known_hooks_key_to_object,
32
 
    )
33
 
from bzrlib.symbol_versioning import (
34
 
    deprecated_in,
35
 
    )
36
 
 
37
 
 
38
 
class TestHooks(tests.TestCase):
39
 
 
40
 
    def test_docs(self):
41
 
        """docs() should return something reasonable about the Hooks."""
42
 
        class MyHooks(Hooks):
43
 
            pass
44
 
        hooks = MyHooks("bzrlib.tests.test_hooks", "some_hooks")
45
 
        hooks['legacy'] = []
46
 
        hooks.add_hook('post_tip_change',
47
 
            "Invoked after the tip of a branch changes. Called with "
48
 
            "a ChangeBranchTipParams object.", (1, 4))
49
 
        hooks.add_hook('pre_tip_change',
50
 
            "Invoked before the tip of a branch changes. Called with "
51
 
            "a ChangeBranchTipParams object. Hooks should raise "
52
 
            "TipChangeRejected to signal that a tip change is not permitted.",
53
 
            (1, 6), None)
54
 
        self.assertEqualDiff(
55
 
            "MyHooks\n"
56
 
            "-------\n"
57
 
            "\n"
58
 
            "legacy\n"
59
 
            "~~~~~~\n"
60
 
            "\n"
61
 
            "An old-style hook. For documentation see the __init__ method of 'MyHooks'\n"
62
 
            "\n"
63
 
            "post_tip_change\n"
64
 
            "~~~~~~~~~~~~~~~\n"
65
 
            "\n"
66
 
            "Introduced in: 1.4\n"
67
 
            "\n"
68
 
            "Invoked after the tip of a branch changes. Called with a\n"
69
 
            "ChangeBranchTipParams object.\n"
70
 
            "\n"
71
 
            "pre_tip_change\n"
72
 
            "~~~~~~~~~~~~~~\n"
73
 
            "\n"
74
 
            "Introduced in: 1.6\n"
75
 
            "\n"
76
 
            "Invoked before the tip of a branch changes. Called with a\n"
77
 
            "ChangeBranchTipParams object. Hooks should raise TipChangeRejected to\n"
78
 
            "signal that a tip change is not permitted.\n", hooks.docs())
 
21
    )
 
22
from bzrlib.errors import (
 
23
    UnknownHook,
 
24
    )
 
25
 
 
26
from bzrlib.symbol_versioning import one_five
 
27
from bzrlib.tests import TestCase
 
28
 
 
29
 
 
30
class TestHooks(TestCase):
 
31
 
 
32
    def test_install_hook_raises_unknown_hook(self):
 
33
        """install_hook should raise UnknownHook if a hook is unknown."""
 
34
        hooks = Hooks()
 
35
        self.assertRaises(UnknownHook, self.applyDeprecated, one_five,
 
36
                          hooks.install_hook, 'silly', None)
 
37
 
 
38
    def test_install_hook_appends_known_hook(self):
 
39
        """install_hook should append the callable for known hooks."""
 
40
        hooks = Hooks()
 
41
        hooks['set_rh'] = []
 
42
        self.applyDeprecated(one_five, hooks.install_hook, 'set_rh', None)
 
43
        self.assertEqual(hooks['set_rh'], [None])
79
44
 
80
45
    def test_install_named_hook_raises_unknown_hook(self):
81
 
        hooks = Hooks("bzrlib.tests.test_hooks", "some_hooks")
82
 
        self.assertRaises(errors.UnknownHook, hooks.install_named_hook, 'silly',
 
46
        hooks = Hooks()
 
47
        self.assertRaises(UnknownHook, hooks.install_named_hook, 'silly',
83
48
                          None, "")
84
49
 
85
50
    def test_install_named_hook_appends_known_hook(self):
86
 
        hooks = Hooks("bzrlib.tests.test_hooks", "some_hooks")
 
51
        hooks = Hooks()
87
52
        hooks['set_rh'] = []
88
53
        hooks.install_named_hook('set_rh', None, "demo")
89
54
        self.assertEqual(hooks['set_rh'], [None])
90
55
 
91
56
    def test_install_named_hook_and_retrieve_name(self):
92
 
        hooks = Hooks("bzrlib.tests.test_hooks", "somehooks")
93
 
        hooks['set_rh'] = []
94
 
        hooks.install_named_hook('set_rh', None, "demo")
95
 
        self.assertEqual("demo", hooks.get_hook_name(None))
96
 
 
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"]))
104
 
 
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"]))
115
 
 
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",
120
 
            "demo")
121
 
 
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,
125
 
            'silly', "")
126
 
 
127
 
    def test_uninstall_named_hook_old_style(self):
128
 
        hooks = Hooks("bzrlib.tests.test_hooks", "some_hooks")
129
 
        hooks["set_rh"] = []
130
 
        hooks.install_named_hook('set_rh', None, "demo")
131
 
        self.assertRaises(errors.UnsupportedOperation,
132
 
            hooks.uninstall_named_hook, "set_rh", "demo")
133
 
 
134
 
    hooks = Hooks("bzrlib.tests.test_hooks", "TestHooks.hooks")
135
 
 
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])
149
 
 
150
 
    set_rh = lambda: None
151
 
 
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])
158
 
 
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")
163
 
        hooks['set_rh'] = []
164
 
        self.assertRaises(errors.UnsupportedOperation,
165
 
            hooks.install_named_hook_lazy,
166
 
            'set_rh', 'bzrlib.tests.test_hooks', 'TestHooks.set_rh',
167
 
            "demo")
168
 
 
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)
179
 
 
180
 
 
181
 
class TestHook(tests.TestCase):
182
 
 
183
 
    def test___init__(self):
184
 
        doc = ("Invoked after changing the tip of a branch object. Called with"
185
 
            "a bzrlib.branch.PostChangeBranchTipParams object")
186
 
        hook = HookPoint("post_tip_change", doc, (0, 15), None)
187
 
        self.assertEqual(doc, hook.__doc__)
188
 
        self.assertEqual("post_tip_change", hook.name)
189
 
        self.assertEqual((0, 15), hook.introduced)
190
 
        self.assertEqual(None, hook.deprecated)
191
 
        self.assertEqual([], list(hook))
192
 
 
193
 
    def test_docs(self):
194
 
        doc = ("Invoked after changing the tip of a branch object. Called with"
195
 
            " a bzrlib.branch.PostChangeBranchTipParams object")
196
 
        hook = HookPoint("post_tip_change", doc, (0, 15), None)
197
 
        self.assertEqual("post_tip_change\n"
198
 
            "~~~~~~~~~~~~~~~\n"
199
 
            "\n"
200
 
            "Introduced in: 0.15\n"
201
 
            "\n"
202
 
            "Invoked after changing the tip of a branch object. Called with a\n"
203
 
            "bzrlib.branch.PostChangeBranchTipParams object\n", hook.docs())
204
 
 
205
 
    def test_hook(self):
206
 
        hook = HookPoint("foo", "no docs", None, None)
207
 
        def callback():
208
 
            pass
209
 
        hook.hook(callback, "my callback")
210
 
        self.assertEqual([callback], list(hook))
211
 
 
212
 
    def lazy_callback():
213
 
        pass
214
 
 
215
 
    def test_lazy_hook(self):
216
 
        hook = HookPoint("foo", "no docs", None, None)
217
 
        hook.hook_lazy(
218
 
            "bzrlib.tests.test_hooks", "TestHook.lazy_callback",
219
 
            "my callback")
220
 
        self.assertEqual([TestHook.lazy_callback], list(hook))
221
 
 
222
 
    def test_uninstall(self):
223
 
        hook = HookPoint("foo", "no docs", None, None)
224
 
        hook.hook_lazy(
225
 
            "bzrlib.tests.test_hooks", "TestHook.lazy_callback",
226
 
            "my callback")
227
 
        self.assertEqual([TestHook.lazy_callback], list(hook))
228
 
        hook.uninstall("my callback")
229
 
        self.assertEqual([], list(hook))
230
 
 
231
 
    def test_uninstall_unknown(self):
232
 
        hook = HookPoint("foo", "no docs", None, None)
233
 
        self.assertRaises(KeyError, hook.uninstall, "my callback")
234
 
 
235
 
    def test___repr(self):
236
 
        # The repr should list all the callbacks, with names.
237
 
        hook = HookPoint("foo", "no docs", None, None)
238
 
        def callback():
239
 
            pass
240
 
        hook.hook(callback, "my callback")
241
 
        callback_repr = repr(callback)
242
 
        self.assertEqual(
243
 
            '<HookPoint(foo), callbacks=[%s(my callback)]>' %
244
 
            callback_repr, repr(hook))
245
 
 
246
 
 
247
 
class TestHookRegistry(tests.TestCase):
248
 
 
249
 
    def test_items_are_reasonable_keys(self):
250
 
        # All the items in the known_hooks registry need to map from
251
 
        # (module_name, member_name) tuples to the callable used to get an
252
 
        # empty Hooks for that attribute. This is used to support the test
253
 
        # suite which needs to generate empty hooks (and HookPoints) to ensure
254
 
        # isolation and prevent tests failing spuriously.
255
 
        for key, factory in known_hooks.items():
256
 
            self.assertTrue(callable(factory),
257
 
                "The factory(%r) for %r is not callable" % (factory, key))
258
 
            obj = known_hooks_key_to_object(key)
259
 
            self.assertIsInstance(obj, Hooks)
260
 
            new_hooks = factory()
261
 
            self.assertIsInstance(obj, Hooks)
262
 
            self.assertEqual(type(obj), type(new_hooks))
263
 
            self.assertEqual("No hook name", new_hooks.get_hook_name(None))
264
 
 
265
 
    def test_known_hooks_key_to_object(self):
266
 
        self.assertIs(branch.Branch.hooks,
267
 
            known_hooks_key_to_object(('bzrlib.branch', 'Branch.hooks')))
268
 
 
269
 
    def test_known_hooks_key_to_parent_and_attribute(self):
270
 
        self.assertEqual((branch.Branch, 'hooks'),
271
 
            known_hooks.key_to_parent_and_attribute(
272
 
            ('bzrlib.branch', 'Branch.hooks')))
273
 
        self.assertEqual((branch, 'Branch'),
274
 
            known_hooks.key_to_parent_and_attribute(
275
 
            ('bzrlib.branch', 'Branch')))
 
57
        hooks = Hooks()
 
58
        hooks['set_rh'] = []
 
59
        hooks.install_named_hook('set_rh', None, "demo")
 
60
        self.assertEqual("demo", hooks.get_hook_name(None))
 
61
 
 
62
    def test_name_hook_and_retrieve_name(self):
 
63
        """name_hook puts the name in the names mapping."""
 
64
        hooks = Hooks()
 
65
        hooks['set_rh'] = []
 
66
        self.applyDeprecated(one_five, hooks.install_hook, 'set_rh', None)
 
67
        hooks.name_hook(None, 'demo')
 
68
        self.assertEqual("demo", hooks.get_hook_name(None))
 
69
 
 
70
    def test_get_unnamed_hook_name_is_unnamed(self):
 
71
        hooks = Hooks()
 
72
        hooks['set_rh'] = []
 
73
        self.applyDeprecated(one_five, hooks.install_hook, 'set_rh', None)
 
74
        self.assertEqual("No hook name", hooks.get_hook_name(None))