~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_hooks.py

  • Committer: mbp at sourcefrog
  • Date: 2005-03-26 03:10:07 UTC
  • Revision ID: mbp@sourcefrog.net-20050326031007-ae4809099d7e6eca
update for release 0.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007, 2009 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 branch, errors
20
 
from bzrlib.hooks import (
21
 
    HookPoint,
22
 
    Hooks,
23
 
    known_hooks,
24
 
    known_hooks_key_to_object,
25
 
    known_hooks_key_to_parent_and_attribute,
26
 
    )
27
 
from bzrlib.errors import (
28
 
    UnknownHook,
29
 
    )
30
 
 
31
 
from bzrlib.tests import TestCase
32
 
 
33
 
 
34
 
class TestHooks(TestCase):
35
 
 
36
 
    def test_create_hook_first(self):
37
 
        hooks = Hooks()
38
 
        doc = ("Invoked after changing the tip of a branch object. Called with"
39
 
            "a bzrlib.branch.PostChangeBranchTipParams object")
40
 
        hook = HookPoint("post_tip_change", doc, (0, 15), None)
41
 
        hooks.create_hook(hook)
42
 
        self.assertEqual(hook, hooks['post_tip_change'])
43
 
 
44
 
    def test_create_hook_name_collision_errors(self):
45
 
        hooks = Hooks()
46
 
        doc = ("Invoked after changing the tip of a branch object. Called with"
47
 
            "a bzrlib.branch.PostChangeBranchTipParams object")
48
 
        hook = HookPoint("post_tip_change", doc, (0, 15), None)
49
 
        hook2 = HookPoint("post_tip_change", None, None, None)
50
 
        hooks.create_hook(hook)
51
 
        self.assertRaises(errors.DuplicateKey, hooks.create_hook, hook2)
52
 
        self.assertEqual(hook, hooks['post_tip_change'])
53
 
 
54
 
    def test_docs(self):
55
 
        """docs() should return something reasonable about the Hooks."""
56
 
        class MyHooks(Hooks):
57
 
            pass
58
 
        hooks = MyHooks()
59
 
        hooks['legacy'] = []
60
 
        hook1 = HookPoint('post_tip_change',
61
 
            "Invoked after the tip of a branch changes. Called with "
62
 
            "a ChangeBranchTipParams object.", (1, 4), None)
63
 
        hook2 = HookPoint('pre_tip_change',
64
 
            "Invoked before the tip of a branch changes. Called with "
65
 
            "a ChangeBranchTipParams object. Hooks should raise "
66
 
            "TipChangeRejected to signal that a tip change is not permitted.",
67
 
            (1, 6), None)
68
 
        hooks.create_hook(hook1)
69
 
        hooks.create_hook(hook2)
70
 
        self.assertEqualDiff(
71
 
            "MyHooks\n"
72
 
            "-------\n"
73
 
            "\n"
74
 
            "legacy\n"
75
 
            "~~~~~~\n"
76
 
            "\n"
77
 
            "An old-style hook. For documentation see the __init__ method of 'MyHooks'\n"
78
 
            "\n"
79
 
            "post_tip_change\n"
80
 
            "~~~~~~~~~~~~~~~\n"
81
 
            "\n"
82
 
            "Introduced in: 1.4\n"
83
 
            "\n"
84
 
            "Invoked after the tip of a branch changes. Called with a\n"
85
 
            "ChangeBranchTipParams object.\n"
86
 
            "\n"
87
 
            "pre_tip_change\n"
88
 
            "~~~~~~~~~~~~~~\n"
89
 
            "\n"
90
 
            "Introduced in: 1.6\n"
91
 
            "\n"
92
 
            "Invoked before the tip of a branch changes. Called with a\n"
93
 
            "ChangeBranchTipParams object. Hooks should raise TipChangeRejected to\n"
94
 
            "signal that a tip change is not permitted.\n", hooks.docs())
95
 
 
96
 
    def test_install_named_hook_raises_unknown_hook(self):
97
 
        hooks = Hooks()
98
 
        self.assertRaises(UnknownHook, hooks.install_named_hook, 'silly',
99
 
                          None, "")
100
 
 
101
 
    def test_install_named_hook_appends_known_hook(self):
102
 
        hooks = Hooks()
103
 
        hooks['set_rh'] = []
104
 
        hooks.install_named_hook('set_rh', None, "demo")
105
 
        self.assertEqual(hooks['set_rh'], [None])
106
 
 
107
 
    def test_install_named_hook_and_retrieve_name(self):
108
 
        hooks = Hooks()
109
 
        hooks['set_rh'] = []
110
 
        hooks.install_named_hook('set_rh', None, "demo")
111
 
        self.assertEqual("demo", hooks.get_hook_name(None))
112
 
 
113
 
 
114
 
class TestHook(TestCase):
115
 
 
116
 
    def test___init__(self):
117
 
        doc = ("Invoked after changing the tip of a branch object. Called with"
118
 
            "a bzrlib.branch.PostChangeBranchTipParams object")
119
 
        hook = HookPoint("post_tip_change", doc, (0, 15), None)
120
 
        self.assertEqual(doc, hook.__doc__)
121
 
        self.assertEqual("post_tip_change", hook.name)
122
 
        self.assertEqual((0, 15), hook.introduced)
123
 
        self.assertEqual(None, hook.deprecated)
124
 
        self.assertEqual([], list(hook))
125
 
 
126
 
    def test_docs(self):
127
 
        doc = ("Invoked after changing the tip of a branch object. Called with"
128
 
            " a bzrlib.branch.PostChangeBranchTipParams object")
129
 
        hook = HookPoint("post_tip_change", doc, (0, 15), None)
130
 
        self.assertEqual("post_tip_change\n"
131
 
            "~~~~~~~~~~~~~~~\n"
132
 
            "\n"
133
 
            "Introduced in: 0.15\n"
134
 
            "\n"
135
 
            "Invoked after changing the tip of a branch object. Called with a\n"
136
 
            "bzrlib.branch.PostChangeBranchTipParams object\n", hook.docs())
137
 
 
138
 
    def test_hook(self):
139
 
        hook = HookPoint("foo", "no docs", None, None)
140
 
        def callback():
141
 
            pass
142
 
        hook.hook(callback, "my callback")
143
 
        self.assertEqual([callback], list(hook))
144
 
 
145
 
    def test___repr(self):
146
 
        # The repr should list all the callbacks, with names.
147
 
        hook = HookPoint("foo", "no docs", None, None)
148
 
        def callback():
149
 
            pass
150
 
        hook.hook(callback, "my callback")
151
 
        callback_repr = repr(callback)
152
 
        self.assertEqual(
153
 
            '<HookPoint(foo), callbacks=[%s(my callback)]>' %
154
 
            callback_repr, repr(hook))
155
 
 
156
 
 
157
 
class TestHookRegistry(TestCase):
158
 
 
159
 
    def test_items_are_reasonable_keys(self):
160
 
        # All the items in the known_hooks registry need to map from
161
 
        # (module_name, member_name) tuples to the callable used to get an
162
 
        # empty Hooks for that attribute. This is used to support the test
163
 
        # suite which needs to generate empty hooks (and HookPoints) to ensure
164
 
        # isolation and prevent tests failing spuriously.
165
 
        for key, factory in known_hooks.items():
166
 
            self.assertTrue(callable(factory),
167
 
                "The factory(%r) for %r is not callable" % (factory, key))
168
 
            obj = known_hooks_key_to_object(key)
169
 
            self.assertIsInstance(obj, Hooks)
170
 
            new_hooks = factory()
171
 
            self.assertIsInstance(obj, Hooks)
172
 
            self.assertEqual(type(obj), type(new_hooks))
173
 
 
174
 
    def test_known_hooks_key_to_object(self):
175
 
        self.assertIs(branch.Branch.hooks,
176
 
            known_hooks_key_to_object(('bzrlib.branch', 'Branch.hooks')))
177
 
 
178
 
    def test_known_hooks_key_to_parent_and_attribute(self):
179
 
        self.assertEqual((branch.Branch, 'hooks'),
180
 
            known_hooks_key_to_parent_and_attribute(
181
 
            ('bzrlib.branch', 'Branch.hooks')))
182
 
        self.assertEqual((branch, 'Branch'),
183
 
            known_hooks_key_to_parent_and_attribute(
184
 
            ('bzrlib.branch', 'Branch')))