/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: 2017-07-23 22:06:41 UTC
  • mfrom: (6738 trunk)
  • mto: This revision was merged to the branch mainline in revision 6739.
  • Revision ID: jelmer@jelmer.uk-20170723220641-69eczax9bmv8d6kk
Merge trunk, address review comments.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
from ..hooks import (
27
27
    HookPoint,
28
28
    Hooks,
29
 
    UnknownHook,
30
29
    install_lazy_named_hook,
31
30
    known_hooks,
32
31
    known_hooks_key_to_object,
33
32
    )
34
33
 
35
34
 
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
35
class TestHooks(tests.TestCase):
50
36
 
51
37
    def test_docs(self):
55
41
        hooks = MyHooks("breezy.tests.test_hooks", "some_hooks")
56
42
        hooks['legacy'] = []
57
43
        hooks.add_hook('post_tip_change',
58
 
                       "Invoked after the tip of a branch changes. Called with "
59
 
                       "a ChangeBranchTipParams object.", (1, 4))
 
44
            "Invoked after the tip of a branch changes. Called with "
 
45
            "a ChangeBranchTipParams object.", (1, 4))
60
46
        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)
 
47
            "Invoked before the tip of a branch changes. Called with "
 
48
            "a ChangeBranchTipParams object. Hooks should raise "
 
49
            "TipChangeRejected to signal that a tip change is not permitted.",
 
50
            (1, 6), None)
65
51
        self.assertEqualDiff(
66
52
            "MyHooks\n"
67
53
            "-------\n"
90
76
 
91
77
    def test_install_named_hook_raises_unknown_hook(self):
92
78
        hooks = Hooks("breezy.tests.test_hooks", "some_hooks")
93
 
        self.assertRaises(UnknownHook, hooks.install_named_hook, 'silly',
 
79
        self.assertRaises(errors.UnknownHook, hooks.install_named_hook, 'silly',
94
80
                          None, "")
95
81
 
96
82
    def test_install_named_hook_appends_known_hook(self):
128
114
        hooks = Hooks("breezy.tests.test_hooks", "some_hooks")
129
115
        hooks.add_hook('set_rh', "Set revision hsitory", (2, 0))
130
116
        self.assertRaises(KeyError, hooks.uninstall_named_hook, "set_rh",
131
 
                          "demo")
 
117
            "demo")
132
118
 
133
119
    def test_uninstall_named_hook_raises_unknown_hook(self):
134
120
        hooks = Hooks("breezy.tests.test_hooks", "some_hooks")
135
 
        self.assertRaises(UnknownHook, hooks.uninstall_named_hook, 'silly', "")
 
121
        self.assertRaises(errors.UnknownHook, hooks.uninstall_named_hook,
 
122
            'silly', "")
136
123
 
137
124
    def test_uninstall_named_hook_old_style(self):
138
125
        hooks = Hooks("breezy.tests.test_hooks", "some_hooks")
139
126
        hooks["set_rh"] = []
140
127
        hooks.install_named_hook('set_rh', None, "demo")
141
128
        self.assertRaises(errors.UnsupportedOperation,
142
 
                          hooks.uninstall_named_hook, "set_rh", "demo")
 
129
            hooks.uninstall_named_hook, "set_rh", "demo")
143
130
 
144
131
    hooks = Hooks("breezy.tests.test_hooks", "TestHooks.hooks")
145
132
 
147
134
        # When the hook points are not yet registered the hook is
148
135
        # added to the _lazy_hooks dictionary in breezy.hooks.
149
136
        self.hooks.add_hook('set_rh', "doc", (0, 15))
150
 
 
151
 
        def set_rh(): return None
 
137
        set_rh = lambda: None
152
138
        install_lazy_named_hook('breezy.tests.test_hooks',
153
 
                                'TestHooks.hooks', 'set_rh', set_rh, "demo")
 
139
            'TestHooks.hooks', 'set_rh', set_rh, "demo")
154
140
        set_rh_lazy_hooks = _mod_hooks._lazy_hooks[
155
141
            ('breezy.tests.test_hooks', 'TestHooks.hooks', 'set_rh')]
156
142
        self.assertEqual(1, len(set_rh_lazy_hooks))
158
144
        self.assertEqual("demo", set_rh_lazy_hooks[0][1])
159
145
        self.assertEqual(list(TestHooks.hooks['set_rh']), [set_rh])
160
146
 
161
 
    def set_rh(): return None
 
147
    set_rh = lambda: None
162
148
 
163
149
    def test_install_named_hook_lazy(self):
164
150
        hooks = Hooks("breezy.tests.hooks", "some_hooks")
165
151
        hooks['set_rh'] = HookPoint("set_rh", "doc", (0, 15), None)
166
152
        hooks.install_named_hook_lazy('set_rh', 'breezy.tests.test_hooks',
167
 
                                      'TestHooks.set_rh', "demo")
 
153
            'TestHooks.set_rh', "demo")
168
154
        self.assertEqual(list(hooks['set_rh']), [TestHooks.set_rh])
169
155
 
170
156
    def test_install_named_hook_lazy_old(self):
173
159
        hooks = Hooks("breezy.tests.hooks", "some_hooks")
174
160
        hooks['set_rh'] = []
175
161
        self.assertRaises(errors.UnsupportedOperation,
176
 
                          hooks.install_named_hook_lazy,
177
 
                          'set_rh', 'breezy.tests.test_hooks', 'TestHooks.set_rh',
178
 
                          "demo")
 
162
            hooks.install_named_hook_lazy,
 
163
            'set_rh', 'breezy.tests.test_hooks', 'TestHooks.set_rh',
 
164
            "demo")
179
165
 
180
166
    def test_valid_lazy_hooks(self):
181
167
        # Make sure that all the registered lazy hooks are referring to existing
182
168
        # hook points which allow lazy registration.
183
 
        for key, callbacks in _mod_hooks._lazy_hooks.items():
 
169
        for key, callbacks in _mod_hooks._lazy_hooks.iteritems():
184
170
            (module_name, member_name, hook_name) = key
185
171
            obj = pyutils.get_named_object(module_name, member_name)
186
172
            self.assertEqual(obj._module, module_name)
193
179
 
194
180
    def test___init__(self):
195
181
        doc = ("Invoked after changing the tip of a branch object. Called with"
196
 
               "a breezy.branch.PostChangeBranchTipParams object")
 
182
            "a breezy.branch.PostChangeBranchTipParams object")
197
183
        hook = HookPoint("post_tip_change", doc, (0, 15), None)
198
184
        self.assertEqual(doc, hook.__doc__)
199
185
        self.assertEqual("post_tip_change", hook.name)
203
189
 
204
190
    def test_docs(self):
205
191
        doc = ("Invoked after changing the tip of a branch object. Called with"
206
 
               " a breezy.branch.PostChangeBranchTipParams object")
 
192
            " a breezy.branch.PostChangeBranchTipParams object")
207
193
        hook = HookPoint("post_tip_change", doc, (0, 15), None)
208
194
        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())
 
195
            "~~~~~~~~~~~~~~~\n"
 
196
            "\n"
 
197
            "Introduced in: 0.15\n"
 
198
            "\n"
 
199
            "Invoked after changing the tip of a branch object. Called with a\n"
 
200
            "breezy.branch.PostChangeBranchTipParams object\n", hook.docs())
215
201
 
216
202
    def test_hook(self):
217
203
        hook = HookPoint("foo", "no docs", None, None)
218
 
 
219
204
        def callback():
220
205
            pass
221
206
        hook.hook(callback, "my callback")
247
232
    def test___repr(self):
248
233
        # The repr should list all the callbacks, with names.
249
234
        hook = HookPoint("foo", "no docs", None, None)
250
 
 
251
235
        def callback():
252
236
            pass
253
237
        hook.hook(callback, "my callback")
267
251
        # isolation and prevent tests failing spuriously.
268
252
        for key, factory in known_hooks.items():
269
253
            self.assertTrue(callable(factory),
270
 
                            "The factory(%r) for %r is not callable" % (factory, key))
 
254
                "The factory(%r) for %r is not callable" % (factory, key))
271
255
            obj = known_hooks_key_to_object(key)
272
256
            self.assertIsInstance(obj, Hooks)
273
257
            new_hooks = factory()
277
261
 
278
262
    def test_known_hooks_key_to_object(self):
279
263
        self.assertIs(branch.Branch.hooks,
280
 
                      known_hooks_key_to_object(('breezy.branch', 'Branch.hooks')))
 
264
            known_hooks_key_to_object(('breezy.branch', 'Branch.hooks')))
281
265
 
282
266
    def test_known_hooks_key_to_parent_and_attribute(self):
283
267
        self.assertEqual((branch.Branch, 'hooks'),
284
 
                         known_hooks.key_to_parent_and_attribute(
 
268
            known_hooks.key_to_parent_and_attribute(
285
269
            ('breezy.branch', 'Branch.hooks')))
286
270
        self.assertEqual((branch, 'Branch'),
287
 
                         known_hooks.key_to_parent_and_attribute(
 
271
            known_hooks.key_to_parent_and_attribute(
288
272
            ('breezy.branch', 'Branch')))