~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_hooks.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2006-07-12 12:36:57 UTC
  • mfrom: (1732.3.4 bzr.revnoX)
  • Revision ID: pqm@pqm.ubuntu.com-20060712123657-365eeb32b69308bf
(matthieu) revno:x:url revision spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007-2010 Canonical Ltd
2
 
#
3
 
# This program is free software; you can redistribute it and/or modify
4
 
# it under the terms of the GNU General Public License as published by
5
 
# the Free Software Foundation; either version 2 of the License, or
6
 
# (at your option) any later version.
7
 
#
8
 
# This program is distributed in the hope that it will be useful,
9
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
# GNU General Public License for more details.
12
 
#
13
 
# You should have received a copy of the GNU General Public License
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
16
 
 
17
 
"""Tests for the core Hooks logic."""
18
 
 
19
 
from bzrlib import (
20
 
    branch,
21
 
    errors,
22
 
    tests,
23
 
    )
24
 
from bzrlib.hooks import (
25
 
    HookPoint,
26
 
    Hooks,
27
 
    known_hooks,
28
 
    known_hooks_key_to_object,
29
 
    known_hooks_key_to_parent_and_attribute,
30
 
    )
31
 
from bzrlib.symbol_versioning import (
32
 
    deprecated_in,
33
 
    )
34
 
 
35
 
 
36
 
class TestHooks(tests.TestCase):
37
 
 
38
 
    def test_create_hook_first(self):
39
 
        hooks = Hooks()
40
 
        doc = ("Invoked after changing the tip of a branch object. Called with"
41
 
            "a bzrlib.branch.PostChangeBranchTipParams object")
42
 
        hook = HookPoint("post_tip_change", doc, (0, 15), None)
43
 
        hooks.create_hook(hook)
44
 
        self.assertEqual(hook, hooks['post_tip_change'])
45
 
 
46
 
    def test_create_hook_name_collision_errors(self):
47
 
        hooks = Hooks()
48
 
        doc = ("Invoked after changing the tip of a branch object. Called with"
49
 
            "a bzrlib.branch.PostChangeBranchTipParams object")
50
 
        hook = HookPoint("post_tip_change", doc, (0, 15), None)
51
 
        hook2 = HookPoint("post_tip_change", None, None, None)
52
 
        hooks.create_hook(hook)
53
 
        self.assertRaises(errors.DuplicateKey, hooks.create_hook, hook2)
54
 
        self.assertEqual(hook, hooks['post_tip_change'])
55
 
 
56
 
    def test_docs(self):
57
 
        """docs() should return something reasonable about the Hooks."""
58
 
        class MyHooks(Hooks):
59
 
            pass
60
 
        hooks = MyHooks()
61
 
        hooks['legacy'] = []
62
 
        hook1 = HookPoint('post_tip_change',
63
 
            "Invoked after the tip of a branch changes. Called with "
64
 
            "a ChangeBranchTipParams object.", (1, 4), None)
65
 
        hook2 = HookPoint('pre_tip_change',
66
 
            "Invoked before the tip of a branch changes. Called with "
67
 
            "a ChangeBranchTipParams object. Hooks should raise "
68
 
            "TipChangeRejected to signal that a tip change is not permitted.",
69
 
            (1, 6), None)
70
 
        hooks.create_hook(hook1)
71
 
        hooks.create_hook(hook2)
72
 
        self.assertEqualDiff(
73
 
            "MyHooks\n"
74
 
            "-------\n"
75
 
            "\n"
76
 
            "legacy\n"
77
 
            "~~~~~~\n"
78
 
            "\n"
79
 
            "An old-style hook. For documentation see the __init__ method of 'MyHooks'\n"
80
 
            "\n"
81
 
            "post_tip_change\n"
82
 
            "~~~~~~~~~~~~~~~\n"
83
 
            "\n"
84
 
            "Introduced in: 1.4\n"
85
 
            "\n"
86
 
            "Invoked after the tip of a branch changes. Called with a\n"
87
 
            "ChangeBranchTipParams object.\n"
88
 
            "\n"
89
 
            "pre_tip_change\n"
90
 
            "~~~~~~~~~~~~~~\n"
91
 
            "\n"
92
 
            "Introduced in: 1.6\n"
93
 
            "\n"
94
 
            "Invoked before the tip of a branch changes. Called with a\n"
95
 
            "ChangeBranchTipParams object. Hooks should raise TipChangeRejected to\n"
96
 
            "signal that a tip change is not permitted.\n", hooks.docs())
97
 
 
98
 
    def test_install_named_hook_raises_unknown_hook(self):
99
 
        hooks = Hooks()
100
 
        self.assertRaises(errors.UnknownHook, hooks.install_named_hook, 'silly',
101
 
                          None, "")
102
 
 
103
 
    def test_install_named_hook_appends_known_hook(self):
104
 
        hooks = Hooks()
105
 
        hooks['set_rh'] = []
106
 
        hooks.install_named_hook('set_rh', None, "demo")
107
 
        self.assertEqual(hooks['set_rh'], [None])
108
 
 
109
 
    def test_install_named_hook_and_retrieve_name(self):
110
 
        hooks = Hooks()
111
 
        hooks['set_rh'] = []
112
 
        hooks.install_named_hook('set_rh', None, "demo")
113
 
        self.assertEqual("demo", hooks.get_hook_name(None))
114
 
 
115
 
 
116
 
class TestHook(tests.TestCase):
117
 
 
118
 
    def test___init__(self):
119
 
        doc = ("Invoked after changing the tip of a branch object. Called with"
120
 
            "a bzrlib.branch.PostChangeBranchTipParams object")
121
 
        hook = HookPoint("post_tip_change", doc, (0, 15), None)
122
 
        self.assertEqual(doc, hook.__doc__)
123
 
        self.assertEqual("post_tip_change", hook.name)
124
 
        self.assertEqual((0, 15), hook.introduced)
125
 
        self.assertEqual(None, hook.deprecated)
126
 
        self.assertEqual([], list(hook))
127
 
 
128
 
    def test_docs(self):
129
 
        doc = ("Invoked after changing the tip of a branch object. Called with"
130
 
            " a bzrlib.branch.PostChangeBranchTipParams object")
131
 
        hook = HookPoint("post_tip_change", doc, (0, 15), None)
132
 
        self.assertEqual("post_tip_change\n"
133
 
            "~~~~~~~~~~~~~~~\n"
134
 
            "\n"
135
 
            "Introduced in: 0.15\n"
136
 
            "\n"
137
 
            "Invoked after changing the tip of a branch object. Called with a\n"
138
 
            "bzrlib.branch.PostChangeBranchTipParams object\n", hook.docs())
139
 
 
140
 
    def test_hook(self):
141
 
        hook = HookPoint("foo", "no docs", None, None)
142
 
        def callback():
143
 
            pass
144
 
        hook.hook(callback, "my callback")
145
 
        self.assertEqual([callback], list(hook))
146
 
 
147
 
    def test___repr(self):
148
 
        # The repr should list all the callbacks, with names.
149
 
        hook = HookPoint("foo", "no docs", None, None)
150
 
        def callback():
151
 
            pass
152
 
        hook.hook(callback, "my callback")
153
 
        callback_repr = repr(callback)
154
 
        self.assertEqual(
155
 
            '<HookPoint(foo), callbacks=[%s(my callback)]>' %
156
 
            callback_repr, repr(hook))
157
 
 
158
 
 
159
 
class TestHookRegistry(tests.TestCase):
160
 
 
161
 
    def test_items_are_reasonable_keys(self):
162
 
        # All the items in the known_hooks registry need to map from
163
 
        # (module_name, member_name) tuples to the callable used to get an
164
 
        # empty Hooks for that attribute. This is used to support the test
165
 
        # suite which needs to generate empty hooks (and HookPoints) to ensure
166
 
        # isolation and prevent tests failing spuriously.
167
 
        for key, factory in known_hooks.items():
168
 
            self.assertTrue(callable(factory),
169
 
                "The factory(%r) for %r is not callable" % (factory, key))
170
 
            obj = known_hooks_key_to_object(key)
171
 
            self.assertIsInstance(obj, Hooks)
172
 
            new_hooks = factory()
173
 
            self.assertIsInstance(obj, Hooks)
174
 
            self.assertEqual(type(obj), type(new_hooks))
175
 
            self.assertEqual("No hook name", new_hooks.get_hook_name(None))
176
 
 
177
 
    def test_known_hooks_key_to_object(self):
178
 
        self.assertIs(branch.Branch.hooks,
179
 
            known_hooks_key_to_object(('bzrlib.branch', 'Branch.hooks')))
180
 
 
181
 
    def test_known_hooks_key_to_parent_and_attribute_deprecated(self):
182
 
        self.assertEqual((branch.Branch, 'hooks'),
183
 
            self.applyDeprecated(deprecated_in((2,3)),
184
 
                known_hooks_key_to_parent_and_attribute,
185
 
                ('bzrlib.branch', 'Branch.hooks')))
186
 
        self.assertEqual((branch, 'Branch'),
187
 
            self.applyDeprecated(deprecated_in((2,3)),
188
 
                known_hooks_key_to_parent_and_attribute,
189
 
                ('bzrlib.branch', 'Branch')))
190
 
 
191
 
    def test_known_hooks_key_to_parent_and_attribute(self):
192
 
        self.assertEqual((branch.Branch, 'hooks'),
193
 
            known_hooks.key_to_parent_and_attribute(
194
 
            ('bzrlib.branch', 'Branch.hooks')))
195
 
        self.assertEqual((branch, 'Branch'),
196
 
            known_hooks.key_to_parent_and_attribute(
197
 
            ('bzrlib.branch', 'Branch')))