~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_hooks.py

  • Committer: Vincent Ladeuil
  • Date: 2009-07-02 13:07:14 UTC
  • mto: (4524.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 4525.
  • Revision ID: v.ladeuil+lp@free.fr-20090702130714-hsyqfusi8vn3a11m
Use tree.has_changes() where appropriate (the test suite caught a
bug in has_changes() (not filtering out the root) in an impressive
number of tests)

* bzrlib/send.py:
(send): Use tree.has_changes() instead of tree.changes_from().

* bzrlib/reconfigure.py:
(Reconfigure._check): Use tree.has_changes() instead of
tree.changes_from().

* bzrlib/merge.py:
(Merger.ensure_revision_trees, Merger.compare_basis): Use
tree.has_changes() instead of tree.changes_from().

* bzrlib/builtins.py:
(cmd_remove_tree.run, cmd_push.run, cmd_merge.run): Use
tree.has_changes() instead of tree.changes_from().

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007 Canonical Ltd
 
1
# Copyright (C) 2007, 2009 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., 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
 
26
 
from bzrlib.symbol_versioning import one_five
27
31
from bzrlib.tests import TestCase
28
32
 
29
33
 
30
34
class TestHooks(TestCase):
31
35
 
32
 
    def test_install_hook_raises_unknown_hook(self):
33
 
        """install_hook should raise UnknownHook if a hook is unknown."""
34
 
        hooks = Hooks()
35
 
        self.assertRaises(UnknownHook, self.applyDeprecated, one_five,
36
 
                          hooks.install_hook, 'silly', None)
37
 
 
38
 
    def test_install_hook_appends_known_hook(self):
39
 
        """install_hook should append the callable for known hooks."""
40
 
        hooks = Hooks()
41
 
        hooks['set_rh'] = []
42
 
        self.applyDeprecated(one_five, hooks.install_hook, 'set_rh', None)
43
 
        self.assertEqual(hooks['set_rh'], [None])
 
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
            "Deprecated in: Not deprecated\n"
 
84
            "\n"
 
85
            "Invoked after the tip of a branch changes. Called with a\n"
 
86
            "ChangeBranchTipParams object.\n"
 
87
            "\n"
 
88
            "pre_tip_change\n"
 
89
            "~~~~~~~~~~~~~~\n"
 
90
            "\n"
 
91
            "Introduced in: 1.6\n"
 
92
            "Deprecated in: Not deprecated\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())
44
97
 
45
98
    def test_install_named_hook_raises_unknown_hook(self):
46
99
        hooks = Hooks()
59
112
        hooks.install_named_hook('set_rh', None, "demo")
60
113
        self.assertEqual("demo", hooks.get_hook_name(None))
61
114
 
62
 
    def test_name_hook_and_retrieve_name(self):
63
 
        """name_hook puts the name in the names mapping."""
64
 
        hooks = Hooks()
65
 
        hooks['set_rh'] = []
66
 
        self.applyDeprecated(one_five, hooks.install_hook, 'set_rh', None)
67
 
        hooks.name_hook(None, 'demo')
68
 
        self.assertEqual("demo", hooks.get_hook_name(None))
69
 
 
70
 
    def test_get_unnamed_hook_name_is_unnamed(self):
71
 
        hooks = Hooks()
72
 
        hooks['set_rh'] = []
73
 
        self.applyDeprecated(one_five, hooks.install_hook, 'set_rh', None)
74
 
        self.assertEqual("No hook name", hooks.get_hook_name(None))
 
115
 
 
116
class TestHook(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
            "Deprecated in: Not deprecated\n"
 
137
            "\n"
 
138
            "Invoked after changing the tip of a branch object. Called with a\n"
 
139
            "bzrlib.branch.PostChangeBranchTipParams object\n", hook.docs())
 
140
 
 
141
    def test_hook(self):
 
142
        hook = HookPoint("foo", "no docs", None, None)
 
143
        def callback():
 
144
            pass
 
145
        hook.hook(callback, "my callback")
 
146
        self.assertEqual([callback], list(hook))
 
147
 
 
148
    def test___repr(self):
 
149
        # The repr should list all the callbacks, with names.
 
150
        hook = HookPoint("foo", "no docs", None, None)
 
151
        def callback():
 
152
            pass
 
153
        hook.hook(callback, "my callback")
 
154
        callback_repr = repr(callback)
 
155
        self.assertEqual(
 
156
            '<HookPoint(foo), callbacks=[%s(my callback)]>' %
 
157
            callback_repr, repr(hook))
 
158
 
 
159
 
 
160
class TestHookRegistry(TestCase):
 
161
 
 
162
    def test_items_are_reasonable_keys(self):
 
163
        # All the items in the known_hooks registry need to map from
 
164
        # (module_name, member_name) tuples to the callable used to get an
 
165
        # empty Hooks for that attribute. This is used to support the test
 
166
        # suite which needs to generate empty hooks (and HookPoints) to ensure
 
167
        # isolation and prevent tests failing spuriously.
 
168
        for key, factory in known_hooks.items():
 
169
            self.assertTrue(callable(factory),
 
170
                "The factory(%r) for %r is not callable" % (factory, key))
 
171
            obj = known_hooks_key_to_object(key)
 
172
            self.assertIsInstance(obj, Hooks)
 
173
            new_hooks = factory()
 
174
            self.assertIsInstance(obj, Hooks)
 
175
            self.assertEqual(type(obj), type(new_hooks))
 
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(self):
 
182
        self.assertEqual((branch.Branch, 'hooks'),
 
183
            known_hooks_key_to_parent_and_attribute(
 
184
            ('bzrlib.branch', 'Branch.hooks')))
 
185
        self.assertEqual((branch, 'Branch'),
 
186
            known_hooks_key_to_parent_and_attribute(
 
187
            ('bzrlib.branch', 'Branch')))