/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 bzrlib/tests/test_hooks.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-02-11 04:02:41 UTC
  • mfrom: (5017.2.2 tariff)
  • Revision ID: pqm@pqm.ubuntu.com-20100211040241-w6n021dz0uus341n
(mbp) add import-tariff tests

Show diffs side-by-side

added added

removed removed

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