~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_hooks.py

  • Committer: Sabin Iacob
  • Date: 2009-03-23 14:59:43 UTC
  • mto: (4189.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 4193.
  • Revision ID: iacobs@m0n5t3r.info-20090323145943-3s3p1px5q1rkh2e5
update FSF mailing address

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""Tests for the core Hooks logic."""
18
18
 
 
19
from bzrlib import branch, errors
19
20
from bzrlib.hooks import (
 
21
    HookPoint,
20
22
    Hooks,
 
23
    known_hooks,
 
24
    known_hooks_key_to_object,
 
25
    known_hooks_key_to_parent_and_attribute,
21
26
    )
22
27
from bzrlib.errors import (
23
28
    UnknownHook,
24
29
    )
25
30
 
 
31
from bzrlib.symbol_versioning import one_five
26
32
from bzrlib.tests import TestCase
27
33
 
28
34
 
29
35
class TestHooks(TestCase):
30
36
 
 
37
    def test_create_hook_first(self):
 
38
        hooks = Hooks()
 
39
        doc = ("Invoked after changing the tip of a branch object. Called with"
 
40
            "a bzrlib.branch.PostChangeBranchTipParams object")
 
41
        hook = HookPoint("post_tip_change", doc, (0, 15), None)
 
42
        hooks.create_hook(hook)
 
43
        self.assertEqual(hook, hooks['post_tip_change'])
 
44
 
 
45
    def test_create_hook_name_collision_errors(self):
 
46
        hooks = Hooks()
 
47
        doc = ("Invoked after changing the tip of a branch object. Called with"
 
48
            "a bzrlib.branch.PostChangeBranchTipParams object")
 
49
        hook = HookPoint("post_tip_change", doc, (0, 15), None)
 
50
        hook2 = HookPoint("post_tip_change", None, None, None)
 
51
        hooks.create_hook(hook)
 
52
        self.assertRaises(errors.DuplicateKey, hooks.create_hook, hook2)
 
53
        self.assertEqual(hook, hooks['post_tip_change'])
 
54
 
 
55
    def test_docs(self):
 
56
        """docs() should return something reasonable about the Hooks."""
 
57
        class MyHooks(Hooks):
 
58
            pass
 
59
        hooks = MyHooks()
 
60
        hooks['legacy'] = []
 
61
        hook1 = HookPoint('post_tip_change',
 
62
            "Invoked after the tip of a branch changes. Called with "
 
63
            "a ChangeBranchTipParams object.", (1, 4), None)
 
64
        hook2 = HookPoint('pre_tip_change',
 
65
            "Invoked before the tip of a branch changes. Called with "
 
66
            "a ChangeBranchTipParams object. Hooks should raise "
 
67
            "TipChangeRejected to signal that a tip change is not permitted.",
 
68
            (1, 6), None)
 
69
        hooks.create_hook(hook1)
 
70
        hooks.create_hook(hook2)
 
71
        self.assertEqualDiff(
 
72
            "MyHooks\n"
 
73
            "-------\n"
 
74
            "\n"
 
75
            "legacy\n"
 
76
            "~~~~~~\n"
 
77
            "\n"
 
78
            "An old-style hook. For documentation see the __init__ method of 'MyHooks'\n"
 
79
            "\n"
 
80
            "post_tip_change\n"
 
81
            "~~~~~~~~~~~~~~~\n"
 
82
            "\n"
 
83
            "Introduced in: 1.4\n"
 
84
            "Deprecated in: Not deprecated\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
            "Deprecated in: Not deprecated\n"
 
94
            "\n"
 
95
            "Invoked before the tip of a branch changes. Called with a\n"
 
96
            "ChangeBranchTipParams object. Hooks should raise TipChangeRejected to\n"
 
97
            "signal that a tip change is not permitted.\n", hooks.docs())
 
98
 
31
99
    def test_install_hook_raises_unknown_hook(self):
32
100
        """install_hook should raise UnknownHook if a hook is unknown."""
33
101
        hooks = Hooks()
34
 
        self.assertRaises(UnknownHook, hooks.install_hook, 'silly', None)
 
102
        self.assertRaises(UnknownHook, self.applyDeprecated, one_five,
 
103
                          hooks.install_hook, 'silly', None)
35
104
 
36
105
    def test_install_hook_appends_known_hook(self):
37
106
        """install_hook should append the callable for known hooks."""
38
107
        hooks = Hooks()
39
108
        hooks['set_rh'] = []
40
 
        hooks.install_hook('set_rh', None)
41
 
        self.assertEqual(hooks['set_rh'], [None])
 
109
        self.applyDeprecated(one_five, hooks.install_hook, 'set_rh', None)
 
110
        self.assertEqual(hooks['set_rh'], [None])
 
111
 
 
112
    def test_install_named_hook_raises_unknown_hook(self):
 
113
        hooks = Hooks()
 
114
        self.assertRaises(UnknownHook, hooks.install_named_hook, 'silly',
 
115
                          None, "")
 
116
 
 
117
    def test_install_named_hook_appends_known_hook(self):
 
118
        hooks = Hooks()
 
119
        hooks['set_rh'] = []
 
120
        hooks.install_named_hook('set_rh', None, "demo")
 
121
        self.assertEqual(hooks['set_rh'], [None])
 
122
 
 
123
    def test_install_named_hook_and_retrieve_name(self):
 
124
        hooks = Hooks()
 
125
        hooks['set_rh'] = []
 
126
        hooks.install_named_hook('set_rh', None, "demo")
 
127
        self.assertEqual("demo", hooks.get_hook_name(None))
42
128
 
43
129
    def test_name_hook_and_retrieve_name(self):
44
130
        """name_hook puts the name in the names mapping."""
45
131
        hooks = Hooks()
46
132
        hooks['set_rh'] = []
47
 
        hooks.install_hook('set_rh', None)
 
133
        self.applyDeprecated(one_five, hooks.install_hook, 'set_rh', None)
48
134
        hooks.name_hook(None, 'demo')
49
135
        self.assertEqual("demo", hooks.get_hook_name(None))
50
136
 
51
137
    def test_get_unnamed_hook_name_is_unnamed(self):
52
138
        hooks = Hooks()
53
139
        hooks['set_rh'] = []
54
 
        hooks.install_hook('set_rh', None)
 
140
        self.applyDeprecated(one_five, hooks.install_hook, 'set_rh', None)
55
141
        self.assertEqual("No hook name", hooks.get_hook_name(None))
 
142
 
 
143
 
 
144
class TestHook(TestCase):
 
145
 
 
146
    def test___init__(self):
 
147
        doc = ("Invoked after changing the tip of a branch object. Called with"
 
148
            "a bzrlib.branch.PostChangeBranchTipParams object")
 
149
        hook = HookPoint("post_tip_change", doc, (0, 15), None)
 
150
        self.assertEqual(doc, hook.__doc__)
 
151
        self.assertEqual("post_tip_change", hook.name)
 
152
        self.assertEqual((0, 15), hook.introduced)
 
153
        self.assertEqual(None, hook.deprecated)
 
154
        self.assertEqual([], list(hook))
 
155
 
 
156
    def test_docs(self):
 
157
        doc = ("Invoked after changing the tip of a branch object. Called with"
 
158
            " a bzrlib.branch.PostChangeBranchTipParams object")
 
159
        hook = HookPoint("post_tip_change", doc, (0, 15), None)
 
160
        self.assertEqual("post_tip_change\n"
 
161
            "~~~~~~~~~~~~~~~\n"
 
162
            "\n"
 
163
            "Introduced in: 0.15\n"
 
164
            "Deprecated in: Not deprecated\n"
 
165
            "\n"
 
166
            "Invoked after changing the tip of a branch object. Called with a\n"
 
167
            "bzrlib.branch.PostChangeBranchTipParams object\n", hook.docs())
 
168
 
 
169
    def test_hook(self):
 
170
        hook = HookPoint("foo", "no docs", None, None)
 
171
        def callback():
 
172
            pass
 
173
        hook.hook(callback, "my callback")
 
174
        self.assertEqual([callback], list(hook))
 
175
 
 
176
    def test___repr(self):
 
177
        # The repr should list all the callbacks, with names.
 
178
        hook = HookPoint("foo", "no docs", None, None)
 
179
        def callback():
 
180
            pass
 
181
        hook.hook(callback, "my callback")
 
182
        callback_repr = repr(callback)
 
183
        self.assertEqual(
 
184
            '<HookPoint(foo), callbacks=[%s(my callback)]>' %
 
185
            callback_repr, repr(hook))
 
186
 
 
187
 
 
188
class TestHookRegistry(TestCase):
 
189
 
 
190
    def test_items_are_reasonable_keys(self):
 
191
        # All the items in the known_hooks registry need to map from
 
192
        # (module_name, member_name) tuples to the callable used to get an
 
193
        # empty Hooks of for that attribute. This is used to support the test
 
194
        # suite which needs to generate empty hooks (and HookPoints) to ensure
 
195
        # isolation and prevent tests failing spuriously.
 
196
        for key, factory in known_hooks.items():
 
197
            self.assertTrue(callable(factory),
 
198
                "The factory(%r) for %r is not callable" % (factory, key))
 
199
            obj = known_hooks_key_to_object(key)
 
200
            self.assertIsInstance(obj, Hooks)
 
201
            new_hooks = factory()
 
202
            self.assertIsInstance(obj, Hooks)
 
203
            self.assertEqual(type(obj), type(new_hooks))
 
204
 
 
205
    def test_known_hooks_key_to_object(self):
 
206
        self.assertIs(branch.Branch.hooks,
 
207
            known_hooks_key_to_object(('bzrlib.branch', 'Branch.hooks')))
 
208
 
 
209
    def test_known_hooks_key_to_parent_and_attribute(self):
 
210
        self.assertEqual((branch.Branch, 'hooks'),
 
211
            known_hooks_key_to_parent_and_attribute(
 
212
            ('bzrlib.branch', 'Branch.hooks')))
 
213
        self.assertEqual((branch, 'Branch'),
 
214
            known_hooks_key_to_parent_and_attribute(
 
215
            ('bzrlib.branch', 'Branch')))