~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: 2009-03-12 00:16:49 UTC
  • mfrom: (4111.1.1 VersionedFiles.add_api)
  • Revision ID: pqm@pqm.ubuntu.com-20090312001649-6tvc2mmeyw992st3
(robertc) Add a groupcompress sort order for get_record_stream.
        (Robert Collins, John Arbash Meinel)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007-2010 Canonical Ltd
 
1
# Copyright (C) 2007 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
"""Tests for the core Hooks logic."""
18
18
 
19
 
from bzrlib import (
20
 
    branch,
21
 
    errors,
22
 
    tests,
23
 
    )
 
19
from bzrlib import errors
24
20
from bzrlib.hooks import (
25
21
    HookPoint,
26
22
    Hooks,
27
 
    known_hooks,
28
 
    known_hooks_key_to_object,
29
 
    known_hooks_key_to_parent_and_attribute,
30
 
    )
31
 
 
32
 
 
33
 
class TestHooks(tests.TestCase):
 
23
    )
 
24
from bzrlib.errors import (
 
25
    UnknownHook,
 
26
    )
 
27
 
 
28
from bzrlib.symbol_versioning import one_five
 
29
from bzrlib.tests import TestCase
 
30
 
 
31
 
 
32
class TestHooks(TestCase):
34
33
 
35
34
    def test_create_hook_first(self):
36
35
        hooks = Hooks()
68
67
        hooks.create_hook(hook2)
69
68
        self.assertEqualDiff(
70
69
            "MyHooks\n"
71
 
            "-------\n"
 
70
            "=======\n"
72
71
            "\n"
73
72
            "legacy\n"
74
 
            "~~~~~~\n"
 
73
            "------\n"
75
74
            "\n"
76
75
            "An old-style hook. For documentation see the __init__ method of 'MyHooks'\n"
77
76
            "\n"
78
77
            "post_tip_change\n"
79
 
            "~~~~~~~~~~~~~~~\n"
 
78
            "---------------\n"
80
79
            "\n"
81
80
            "Introduced in: 1.4\n"
 
81
            "Deprecated in: Not deprecated\n"
82
82
            "\n"
83
83
            "Invoked after the tip of a branch changes. Called with a\n"
84
84
            "ChangeBranchTipParams object.\n"
85
85
            "\n"
86
86
            "pre_tip_change\n"
87
 
            "~~~~~~~~~~~~~~\n"
 
87
            "--------------\n"
88
88
            "\n"
89
89
            "Introduced in: 1.6\n"
 
90
            "Deprecated in: Not deprecated\n"
90
91
            "\n"
91
92
            "Invoked before the tip of a branch changes. Called with a\n"
92
93
            "ChangeBranchTipParams object. Hooks should raise TipChangeRejected to\n"
93
94
            "signal that a tip change is not permitted.\n", hooks.docs())
94
95
 
 
96
    def test_install_hook_raises_unknown_hook(self):
 
97
        """install_hook should raise UnknownHook if a hook is unknown."""
 
98
        hooks = Hooks()
 
99
        self.assertRaises(UnknownHook, self.applyDeprecated, one_five,
 
100
                          hooks.install_hook, 'silly', None)
 
101
 
 
102
    def test_install_hook_appends_known_hook(self):
 
103
        """install_hook should append the callable for known hooks."""
 
104
        hooks = Hooks()
 
105
        hooks['set_rh'] = []
 
106
        self.applyDeprecated(one_five, hooks.install_hook, 'set_rh', None)
 
107
        self.assertEqual(hooks['set_rh'], [None])
 
108
 
95
109
    def test_install_named_hook_raises_unknown_hook(self):
96
110
        hooks = Hooks()
97
 
        self.assertRaises(errors.UnknownHook, hooks.install_named_hook, 'silly',
 
111
        self.assertRaises(UnknownHook, hooks.install_named_hook, 'silly',
98
112
                          None, "")
99
113
 
100
114
    def test_install_named_hook_appends_known_hook(self):
109
123
        hooks.install_named_hook('set_rh', None, "demo")
110
124
        self.assertEqual("demo", hooks.get_hook_name(None))
111
125
 
112
 
 
113
 
class TestHook(tests.TestCase):
 
126
    def test_name_hook_and_retrieve_name(self):
 
127
        """name_hook puts the name in the names mapping."""
 
128
        hooks = Hooks()
 
129
        hooks['set_rh'] = []
 
130
        self.applyDeprecated(one_five, hooks.install_hook, 'set_rh', None)
 
131
        hooks.name_hook(None, 'demo')
 
132
        self.assertEqual("demo", hooks.get_hook_name(None))
 
133
 
 
134
    def test_get_unnamed_hook_name_is_unnamed(self):
 
135
        hooks = Hooks()
 
136
        hooks['set_rh'] = []
 
137
        self.applyDeprecated(one_five, hooks.install_hook, 'set_rh', None)
 
138
        self.assertEqual("No hook name", hooks.get_hook_name(None))
 
139
 
 
140
 
 
141
class TestHook(TestCase):
114
142
 
115
143
    def test___init__(self):
116
144
        doc = ("Invoked after changing the tip of a branch object. Called with"
127
155
            " a bzrlib.branch.PostChangeBranchTipParams object")
128
156
        hook = HookPoint("post_tip_change", doc, (0, 15), None)
129
157
        self.assertEqual("post_tip_change\n"
130
 
            "~~~~~~~~~~~~~~~\n"
 
158
            "---------------\n"
131
159
            "\n"
132
160
            "Introduced in: 0.15\n"
 
161
            "Deprecated in: Not deprecated\n"
133
162
            "\n"
134
163
            "Invoked after changing the tip of a branch object. Called with a\n"
135
164
            "bzrlib.branch.PostChangeBranchTipParams object\n", hook.docs())
151
180
        self.assertEqual(
152
181
            '<HookPoint(foo), callbacks=[%s(my callback)]>' %
153
182
            callback_repr, repr(hook))
154
 
 
155
 
 
156
 
class TestHookRegistry(tests.TestCase):
157
 
 
158
 
    def test_items_are_reasonable_keys(self):
159
 
        # All the items in the known_hooks registry need to map from
160
 
        # (module_name, member_name) tuples to the callable used to get an
161
 
        # empty Hooks for that attribute. This is used to support the test
162
 
        # suite which needs to generate empty hooks (and HookPoints) to ensure
163
 
        # isolation and prevent tests failing spuriously.
164
 
        for key, factory in known_hooks.items():
165
 
            self.assertTrue(callable(factory),
166
 
                "The factory(%r) for %r is not callable" % (factory, key))
167
 
            obj = known_hooks_key_to_object(key)
168
 
            self.assertIsInstance(obj, Hooks)
169
 
            new_hooks = factory()
170
 
            self.assertIsInstance(obj, Hooks)
171
 
            self.assertEqual(type(obj), type(new_hooks))
172
 
            self.assertEqual("No hook name", new_hooks.get_hook_name(None))
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')))