/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: 2020-03-22 01:35:14 UTC
  • mfrom: (7490.7.6 work)
  • mto: This revision was merged to the branch mainline in revision 7499.
  • Revision ID: jelmer@jelmer.uk-20200322013514-7vw1ntwho04rcuj3
merge lp:brz/3.1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
    def test_unknown_hook(self):
39
39
        error = UnknownHook("branch", "foo")
40
40
        self.assertEqualDiff("The branch hook 'foo' is unknown in this version"
41
 
            " of breezy.",
42
 
            str(error))
 
41
                             " of breezy.",
 
42
                             str(error))
43
43
        error = UnknownHook("tree", "bar")
44
44
        self.assertEqualDiff("The tree hook 'bar' is unknown in this version"
45
 
            " of breezy.",
46
 
            str(error))
 
45
                             " of breezy.",
 
46
                             str(error))
47
47
 
48
48
 
49
49
class TestHooks(tests.TestCase):
55
55
        hooks = MyHooks("breezy.tests.test_hooks", "some_hooks")
56
56
        hooks['legacy'] = []
57
57
        hooks.add_hook('post_tip_change',
58
 
            "Invoked after the tip of a branch changes. Called with "
59
 
            "a ChangeBranchTipParams object.", (1, 4))
 
58
                       "Invoked after the tip of a branch changes. Called with "
 
59
                       "a ChangeBranchTipParams object.", (1, 4))
60
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)
 
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)
65
65
        self.assertEqualDiff(
66
66
            "MyHooks\n"
67
67
            "-------\n"
128
128
        hooks = Hooks("breezy.tests.test_hooks", "some_hooks")
129
129
        hooks.add_hook('set_rh', "Set revision hsitory", (2, 0))
130
130
        self.assertRaises(KeyError, hooks.uninstall_named_hook, "set_rh",
131
 
            "demo")
 
131
                          "demo")
132
132
 
133
133
    def test_uninstall_named_hook_raises_unknown_hook(self):
134
134
        hooks = Hooks("breezy.tests.test_hooks", "some_hooks")
139
139
        hooks["set_rh"] = []
140
140
        hooks.install_named_hook('set_rh', None, "demo")
141
141
        self.assertRaises(errors.UnsupportedOperation,
142
 
            hooks.uninstall_named_hook, "set_rh", "demo")
 
142
                          hooks.uninstall_named_hook, "set_rh", "demo")
143
143
 
144
144
    hooks = Hooks("breezy.tests.test_hooks", "TestHooks.hooks")
145
145
 
147
147
        # When the hook points are not yet registered the hook is
148
148
        # added to the _lazy_hooks dictionary in breezy.hooks.
149
149
        self.hooks.add_hook('set_rh', "doc", (0, 15))
150
 
        set_rh = lambda: None
 
150
 
 
151
        def set_rh(): return None
151
152
        install_lazy_named_hook('breezy.tests.test_hooks',
152
 
            'TestHooks.hooks', 'set_rh', set_rh, "demo")
 
153
                                'TestHooks.hooks', 'set_rh', set_rh, "demo")
153
154
        set_rh_lazy_hooks = _mod_hooks._lazy_hooks[
154
155
            ('breezy.tests.test_hooks', 'TestHooks.hooks', 'set_rh')]
155
156
        self.assertEqual(1, len(set_rh_lazy_hooks))
157
158
        self.assertEqual("demo", set_rh_lazy_hooks[0][1])
158
159
        self.assertEqual(list(TestHooks.hooks['set_rh']), [set_rh])
159
160
 
160
 
    set_rh = lambda: None
 
161
    def set_rh(): return None
161
162
 
162
163
    def test_install_named_hook_lazy(self):
163
164
        hooks = Hooks("breezy.tests.hooks", "some_hooks")
164
165
        hooks['set_rh'] = HookPoint("set_rh", "doc", (0, 15), None)
165
166
        hooks.install_named_hook_lazy('set_rh', 'breezy.tests.test_hooks',
166
 
            'TestHooks.set_rh', "demo")
 
167
                                      'TestHooks.set_rh', "demo")
167
168
        self.assertEqual(list(hooks['set_rh']), [TestHooks.set_rh])
168
169
 
169
170
    def test_install_named_hook_lazy_old(self):
172
173
        hooks = Hooks("breezy.tests.hooks", "some_hooks")
173
174
        hooks['set_rh'] = []
174
175
        self.assertRaises(errors.UnsupportedOperation,
175
 
            hooks.install_named_hook_lazy,
176
 
            'set_rh', 'breezy.tests.test_hooks', 'TestHooks.set_rh',
177
 
            "demo")
 
176
                          hooks.install_named_hook_lazy,
 
177
                          'set_rh', 'breezy.tests.test_hooks', 'TestHooks.set_rh',
 
178
                          "demo")
178
179
 
179
180
    def test_valid_lazy_hooks(self):
180
181
        # Make sure that all the registered lazy hooks are referring to existing
192
193
 
193
194
    def test___init__(self):
194
195
        doc = ("Invoked after changing the tip of a branch object. Called with"
195
 
            "a breezy.branch.PostChangeBranchTipParams object")
 
196
               "a breezy.branch.PostChangeBranchTipParams object")
196
197
        hook = HookPoint("post_tip_change", doc, (0, 15), None)
197
198
        self.assertEqual(doc, hook.__doc__)
198
199
        self.assertEqual("post_tip_change", hook.name)
202
203
 
203
204
    def test_docs(self):
204
205
        doc = ("Invoked after changing the tip of a branch object. Called with"
205
 
            " a breezy.branch.PostChangeBranchTipParams object")
 
206
               " a breezy.branch.PostChangeBranchTipParams object")
206
207
        hook = HookPoint("post_tip_change", doc, (0, 15), None)
207
208
        self.assertEqual("post_tip_change\n"
208
 
            "~~~~~~~~~~~~~~~\n"
209
 
            "\n"
210
 
            "Introduced in: 0.15\n"
211
 
            "\n"
212
 
            "Invoked after changing the tip of a branch object. Called with a\n"
213
 
            "breezy.branch.PostChangeBranchTipParams object\n", hook.docs())
 
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())
214
215
 
215
216
    def test_hook(self):
216
217
        hook = HookPoint("foo", "no docs", None, None)
 
218
 
217
219
        def callback():
218
220
            pass
219
221
        hook.hook(callback, "my callback")
245
247
    def test___repr(self):
246
248
        # The repr should list all the callbacks, with names.
247
249
        hook = HookPoint("foo", "no docs", None, None)
 
250
 
248
251
        def callback():
249
252
            pass
250
253
        hook.hook(callback, "my callback")
264
267
        # isolation and prevent tests failing spuriously.
265
268
        for key, factory in known_hooks.items():
266
269
            self.assertTrue(callable(factory),
267
 
                "The factory(%r) for %r is not callable" % (factory, key))
 
270
                            "The factory(%r) for %r is not callable" % (factory, key))
268
271
            obj = known_hooks_key_to_object(key)
269
272
            self.assertIsInstance(obj, Hooks)
270
273
            new_hooks = factory()
274
277
 
275
278
    def test_known_hooks_key_to_object(self):
276
279
        self.assertIs(branch.Branch.hooks,
277
 
            known_hooks_key_to_object(('breezy.branch', 'Branch.hooks')))
 
280
                      known_hooks_key_to_object(('breezy.branch', 'Branch.hooks')))
278
281
 
279
282
    def test_known_hooks_key_to_parent_and_attribute(self):
280
283
        self.assertEqual((branch.Branch, 'hooks'),
281
 
            known_hooks.key_to_parent_and_attribute(
 
284
                         known_hooks.key_to_parent_and_attribute(
282
285
            ('breezy.branch', 'Branch.hooks')))
283
286
        self.assertEqual((branch, 'Branch'),
284
 
            known_hooks.key_to_parent_and_attribute(
 
287
                         known_hooks.key_to_parent_and_attribute(
285
288
            ('breezy.branch', 'Branch')))