/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test_hooks.py

  • Committer: Jelmer Vernooij
  • Date: 2018-08-14 01:15:02 UTC
  • mto: This revision was merged to the branch mainline in revision 7078.
  • Revision ID: jelmer@jelmer.uk-20180814011502-5zaydaq02vc2qxo1
Fix tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007-2010 Canonical Ltd
 
1
# Copyright (C) 2007-2012, 2016 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
16
16
 
17
17
"""Tests for the core Hooks logic."""
18
18
 
19
 
from bzrlib import (
 
19
from .. import (
20
20
    branch,
21
21
    errors,
 
22
    hooks as _mod_hooks,
 
23
    pyutils,
22
24
    tests,
23
25
    )
24
 
from bzrlib.hooks import (
 
26
from ..hooks import (
25
27
    HookPoint,
26
28
    Hooks,
 
29
    UnknownHook,
 
30
    install_lazy_named_hook,
27
31
    known_hooks,
28
32
    known_hooks_key_to_object,
29
 
    known_hooks_key_to_parent_and_attribute,
30
33
    )
31
34
 
32
35
 
 
36
class TestErrors(tests.TestCase):
 
37
 
 
38
    def test_unknown_hook(self):
 
39
        error = UnknownHook("branch", "foo")
 
40
        self.assertEqualDiff("The branch hook 'foo' is unknown in this version"
 
41
            " of breezy.",
 
42
            str(error))
 
43
        error = UnknownHook("tree", "bar")
 
44
        self.assertEqualDiff("The tree hook 'bar' is unknown in this version"
 
45
            " of breezy.",
 
46
            str(error))
 
47
 
 
48
 
33
49
class TestHooks(tests.TestCase):
34
50
 
35
 
    def test_create_hook_first(self):
36
 
        hooks = Hooks()
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'])
42
 
 
43
 
    def test_create_hook_name_collision_errors(self):
44
 
        hooks = Hooks()
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'])
52
 
 
53
51
    def test_docs(self):
54
52
        """docs() should return something reasonable about the Hooks."""
55
53
        class MyHooks(Hooks):
56
54
            pass
57
 
        hooks = MyHooks()
 
55
        hooks = MyHooks("breezy.tests.test_hooks", "some_hooks")
58
56
        hooks['legacy'] = []
59
 
        hook1 = HookPoint('post_tip_change',
 
57
        hooks.add_hook('post_tip_change',
60
58
            "Invoked after the tip of a branch changes. Called with "
61
 
            "a ChangeBranchTipParams object.", (1, 4), None)
62
 
        hook2 = HookPoint('pre_tip_change',
 
59
            "a ChangeBranchTipParams object.", (1, 4))
 
60
        hooks.add_hook('pre_tip_change',
63
61
            "Invoked before the tip of a branch changes. Called with "
64
62
            "a ChangeBranchTipParams object. Hooks should raise "
65
63
            "TipChangeRejected to signal that a tip change is not permitted.",
66
64
            (1, 6), None)
67
 
        hooks.create_hook(hook1)
68
 
        hooks.create_hook(hook2)
69
65
        self.assertEqualDiff(
70
66
            "MyHooks\n"
71
67
            "-------\n"
93
89
            "signal that a tip change is not permitted.\n", hooks.docs())
94
90
 
95
91
    def test_install_named_hook_raises_unknown_hook(self):
96
 
        hooks = Hooks()
97
 
        self.assertRaises(errors.UnknownHook, hooks.install_named_hook, 'silly',
 
92
        hooks = Hooks("breezy.tests.test_hooks", "some_hooks")
 
93
        self.assertRaises(UnknownHook, hooks.install_named_hook, 'silly',
98
94
                          None, "")
99
95
 
100
96
    def test_install_named_hook_appends_known_hook(self):
101
 
        hooks = Hooks()
 
97
        hooks = Hooks("breezy.tests.test_hooks", "some_hooks")
102
98
        hooks['set_rh'] = []
103
99
        hooks.install_named_hook('set_rh', None, "demo")
104
100
        self.assertEqual(hooks['set_rh'], [None])
105
101
 
106
102
    def test_install_named_hook_and_retrieve_name(self):
107
 
        hooks = Hooks()
 
103
        hooks = Hooks("breezy.tests.test_hooks", "somehooks")
108
104
        hooks['set_rh'] = []
109
105
        hooks.install_named_hook('set_rh', None, "demo")
110
106
        self.assertEqual("demo", hooks.get_hook_name(None))
111
107
 
 
108
    def test_uninstall_named_hook(self):
 
109
        hooks = Hooks("breezy.tests.test_hooks", "some_hooks")
 
110
        hooks.add_hook('set_rh', "Set revision history", (2, 0))
 
111
        hooks.install_named_hook('set_rh', None, "demo")
 
112
        self.assertEqual(1, len(hooks["set_rh"]))
 
113
        hooks.uninstall_named_hook("set_rh", "demo")
 
114
        self.assertEqual(0, len(hooks["set_rh"]))
 
115
 
 
116
    def test_uninstall_multiple_named_hooks(self):
 
117
        # Multiple callbacks with the same label all get removed
 
118
        hooks = Hooks("breezy.tests.test_hooks", "some_hooks")
 
119
        hooks.add_hook('set_rh', "Set revision history", (2, 0))
 
120
        hooks.install_named_hook('set_rh', 1, "demo")
 
121
        hooks.install_named_hook('set_rh', 2, "demo")
 
122
        hooks.install_named_hook('set_rh', 3, "othername")
 
123
        self.assertEqual(3, len(hooks["set_rh"]))
 
124
        hooks.uninstall_named_hook("set_rh", "demo")
 
125
        self.assertEqual(1, len(hooks["set_rh"]))
 
126
 
 
127
    def test_uninstall_named_hook_unknown_callable(self):
 
128
        hooks = Hooks("breezy.tests.test_hooks", "some_hooks")
 
129
        hooks.add_hook('set_rh', "Set revision hsitory", (2, 0))
 
130
        self.assertRaises(KeyError, hooks.uninstall_named_hook, "set_rh",
 
131
            "demo")
 
132
 
 
133
    def test_uninstall_named_hook_raises_unknown_hook(self):
 
134
        hooks = Hooks("breezy.tests.test_hooks", "some_hooks")
 
135
        self.assertRaises(UnknownHook, hooks.uninstall_named_hook, 'silly', "")
 
136
 
 
137
    def test_uninstall_named_hook_old_style(self):
 
138
        hooks = Hooks("breezy.tests.test_hooks", "some_hooks")
 
139
        hooks["set_rh"] = []
 
140
        hooks.install_named_hook('set_rh', None, "demo")
 
141
        self.assertRaises(errors.UnsupportedOperation,
 
142
            hooks.uninstall_named_hook, "set_rh", "demo")
 
143
 
 
144
    hooks = Hooks("breezy.tests.test_hooks", "TestHooks.hooks")
 
145
 
 
146
    def test_install_lazy_named_hook(self):
 
147
        # When the hook points are not yet registered the hook is
 
148
        # added to the _lazy_hooks dictionary in breezy.hooks.
 
149
        self.hooks.add_hook('set_rh', "doc", (0, 15))
 
150
        set_rh = lambda: None
 
151
        install_lazy_named_hook('breezy.tests.test_hooks',
 
152
            'TestHooks.hooks', 'set_rh', set_rh, "demo")
 
153
        set_rh_lazy_hooks = _mod_hooks._lazy_hooks[
 
154
            ('breezy.tests.test_hooks', 'TestHooks.hooks', 'set_rh')]
 
155
        self.assertEqual(1, len(set_rh_lazy_hooks))
 
156
        self.assertEqual(set_rh, set_rh_lazy_hooks[0][0].get_obj())
 
157
        self.assertEqual("demo", set_rh_lazy_hooks[0][1])
 
158
        self.assertEqual(list(TestHooks.hooks['set_rh']), [set_rh])
 
159
 
 
160
    set_rh = lambda: None
 
161
 
 
162
    def test_install_named_hook_lazy(self):
 
163
        hooks = Hooks("breezy.tests.hooks", "some_hooks")
 
164
        hooks['set_rh'] = HookPoint("set_rh", "doc", (0, 15), None)
 
165
        hooks.install_named_hook_lazy('set_rh', 'breezy.tests.test_hooks',
 
166
            'TestHooks.set_rh', "demo")
 
167
        self.assertEqual(list(hooks['set_rh']), [TestHooks.set_rh])
 
168
 
 
169
    def test_install_named_hook_lazy_old(self):
 
170
        # An exception is raised if a lazy hook is raised for
 
171
        # an old style hook point.
 
172
        hooks = Hooks("breezy.tests.hooks", "some_hooks")
 
173
        hooks['set_rh'] = []
 
174
        self.assertRaises(errors.UnsupportedOperation,
 
175
            hooks.install_named_hook_lazy,
 
176
            'set_rh', 'breezy.tests.test_hooks', 'TestHooks.set_rh',
 
177
            "demo")
 
178
 
 
179
    def test_valid_lazy_hooks(self):
 
180
        # Make sure that all the registered lazy hooks are referring to existing
 
181
        # hook points which allow lazy registration.
 
182
        for key, callbacks in _mod_hooks._lazy_hooks.items():
 
183
            (module_name, member_name, hook_name) = key
 
184
            obj = pyutils.get_named_object(module_name, member_name)
 
185
            self.assertEqual(obj._module, module_name)
 
186
            self.assertEqual(obj._member_name, member_name)
 
187
            self.assertTrue(hook_name in obj)
 
188
            self.assertIs(callbacks, obj[hook_name]._callbacks)
 
189
 
112
190
 
113
191
class TestHook(tests.TestCase):
114
192
 
115
193
    def test___init__(self):
116
194
        doc = ("Invoked after changing the tip of a branch object. Called with"
117
 
            "a bzrlib.branch.PostChangeBranchTipParams object")
 
195
            "a breezy.branch.PostChangeBranchTipParams object")
118
196
        hook = HookPoint("post_tip_change", doc, (0, 15), None)
119
197
        self.assertEqual(doc, hook.__doc__)
120
198
        self.assertEqual("post_tip_change", hook.name)
124
202
 
125
203
    def test_docs(self):
126
204
        doc = ("Invoked after changing the tip of a branch object. Called with"
127
 
            " a bzrlib.branch.PostChangeBranchTipParams object")
 
205
            " a breezy.branch.PostChangeBranchTipParams object")
128
206
        hook = HookPoint("post_tip_change", doc, (0, 15), None)
129
207
        self.assertEqual("post_tip_change\n"
130
208
            "~~~~~~~~~~~~~~~\n"
132
210
            "Introduced in: 0.15\n"
133
211
            "\n"
134
212
            "Invoked after changing the tip of a branch object. Called with a\n"
135
 
            "bzrlib.branch.PostChangeBranchTipParams object\n", hook.docs())
 
213
            "breezy.branch.PostChangeBranchTipParams object\n", hook.docs())
136
214
 
137
215
    def test_hook(self):
138
216
        hook = HookPoint("foo", "no docs", None, None)
141
219
        hook.hook(callback, "my callback")
142
220
        self.assertEqual([callback], list(hook))
143
221
 
 
222
    def lazy_callback():
 
223
        pass
 
224
 
 
225
    def test_lazy_hook(self):
 
226
        hook = HookPoint("foo", "no docs", None, None)
 
227
        hook.hook_lazy(
 
228
            "breezy.tests.test_hooks", "TestHook.lazy_callback",
 
229
            "my callback")
 
230
        self.assertEqual([TestHook.lazy_callback], list(hook))
 
231
 
 
232
    def test_uninstall(self):
 
233
        hook = HookPoint("foo", "no docs", None, None)
 
234
        hook.hook_lazy(
 
235
            "breezy.tests.test_hooks", "TestHook.lazy_callback",
 
236
            "my callback")
 
237
        self.assertEqual([TestHook.lazy_callback], list(hook))
 
238
        hook.uninstall("my callback")
 
239
        self.assertEqual([], list(hook))
 
240
 
 
241
    def test_uninstall_unknown(self):
 
242
        hook = HookPoint("foo", "no docs", None, None)
 
243
        self.assertRaises(KeyError, hook.uninstall, "my callback")
 
244
 
144
245
    def test___repr(self):
145
246
        # The repr should list all the callbacks, with names.
146
247
        hook = HookPoint("foo", "no docs", None, None)
173
274
 
174
275
    def test_known_hooks_key_to_object(self):
175
276
        self.assertIs(branch.Branch.hooks,
176
 
            known_hooks_key_to_object(('bzrlib.branch', 'Branch.hooks')))
 
277
            known_hooks_key_to_object(('breezy.branch', 'Branch.hooks')))
177
278
 
178
279
    def test_known_hooks_key_to_parent_and_attribute(self):
179
280
        self.assertEqual((branch.Branch, 'hooks'),
180
 
            known_hooks_key_to_parent_and_attribute(
181
 
            ('bzrlib.branch', 'Branch.hooks')))
 
281
            known_hooks.key_to_parent_and_attribute(
 
282
            ('breezy.branch', 'Branch.hooks')))
182
283
        self.assertEqual((branch, 'Branch'),
183
 
            known_hooks_key_to_parent_and_attribute(
184
 
            ('bzrlib.branch', 'Branch')))
 
284
            known_hooks.key_to_parent_and_attribute(
 
285
            ('breezy.branch', 'Branch')))