/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2553.1.1 by Robert Collins
Give Hooks names.
1
# Copyright (C) 2007 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
"""Tests for the core Hooks logic."""
18
4098.2.1 by Robert Collins
Allow self documenting hooks.
19
from bzrlib import errors
2553.1.1 by Robert Collins
Give Hooks names.
20
from bzrlib.hooks import (
4098.2.2 by Robert Collins
Review feedback.
21
    HookPoint,
2553.1.1 by Robert Collins
Give Hooks names.
22
    Hooks,
23
    )
24
from bzrlib.errors import (
25
    UnknownHook,
26
    )
27
3256.2.30 by Daniel Watkins
Updated deprecation warnings and tests.
28
from bzrlib.symbol_versioning import one_five
2553.1.1 by Robert Collins
Give Hooks names.
29
from bzrlib.tests import TestCase
30
31
32
class TestHooks(TestCase):
33
4098.2.1 by Robert Collins
Allow self documenting hooks.
34
    def test_create_hook_first(self):
35
        hooks = Hooks()
36
        doc = ("Invoked after changing the tip of a branch object. Called with"
37
            "a bzrlib.branch.PostChangeBranchTipParams object")
4098.2.2 by Robert Collins
Review feedback.
38
        hook = HookPoint("post_tip_change", doc, (0, 15), None)
4098.2.1 by Robert Collins
Allow self documenting hooks.
39
        hooks.create_hook(hook)
40
        self.assertEqual(hook, hooks['post_tip_change'])
41
42
    def test_create_hook_name_collision_errors(self):
43
        hooks = Hooks()
44
        doc = ("Invoked after changing the tip of a branch object. Called with"
45
            "a bzrlib.branch.PostChangeBranchTipParams object")
4098.2.2 by Robert Collins
Review feedback.
46
        hook = HookPoint("post_tip_change", doc, (0, 15), None)
47
        hook2 = HookPoint("post_tip_change", None, None, None)
4098.2.1 by Robert Collins
Allow self documenting hooks.
48
        hooks.create_hook(hook)
49
        self.assertRaises(errors.DuplicateKey, hooks.create_hook, hook2)
50
        self.assertEqual(hook, hooks['post_tip_change'])
51
52
    def test_docs(self):
53
        """docs() should return something reasonable about the Hooks."""
54
        hooks = Hooks()
55
        hooks['legacy'] = []
4098.2.2 by Robert Collins
Review feedback.
56
        hook1 = HookPoint('post_tip_change',
4098.2.1 by Robert Collins
Allow self documenting hooks.
57
            "Invoked after the tip of a branch changes. Called with "
58
            "a ChangeBranchTipParams object.", (1, 4), None)
4098.2.2 by Robert Collins
Review feedback.
59
        hook2 = HookPoint('pre_tip_change',
4098.2.1 by Robert Collins
Allow self documenting hooks.
60
            "Invoked before the tip of a branch changes. Called with "
61
            "a ChangeBranchTipParams object. Hooks should raise "
62
            "TipChangeRejected to signal that a tip change is not permitted.",
63
            (1, 6), None)
64
        hooks.create_hook(hook1)
65
        hooks.create_hook(hook2)
66
        self.assertEqual(
67
            "legacy\n"
68
            "------\n"
69
            "\n"
70
            "An old-style hook. For documentation see the __init__ method of 'Hooks'\n"
71
            "\n"
72
            "post_tip_change\n"
73
            "---------------\n"
74
            "\n"
75
            "Introduced in: 1.4\n"
76
            "Deprecated in: Not deprecated\n"
77
            "\n"
78
            "Invoked after the tip of a branch changes. Called with a\n"
79
            "ChangeBranchTipParams object.\n"
80
            "\n"
81
            "pre_tip_change\n"
82
            "--------------\n"
83
            "\n"
84
            "Introduced in: 1.6\n"
85
            "Deprecated in: Not deprecated\n"
86
            "\n"
87
            "Invoked before the tip of a branch changes. Called with a\n"
88
            "ChangeBranchTipParams object. Hooks should raise TipChangeRejected to\n"
89
            "signal that a tip change is not permitted.\n", hooks.docs())
90
2553.1.1 by Robert Collins
Give Hooks names.
91
    def test_install_hook_raises_unknown_hook(self):
92
        """install_hook should raise UnknownHook if a hook is unknown."""
93
        hooks = Hooks()
3256.2.30 by Daniel Watkins
Updated deprecation warnings and tests.
94
        self.assertRaises(UnknownHook, self.applyDeprecated, one_five,
3256.2.20 by Daniel Watkins
Modified Hooks.install_hook tests to use applyDeprecated.
95
                          hooks.install_hook, 'silly', None)
2553.1.1 by Robert Collins
Give Hooks names.
96
97
    def test_install_hook_appends_known_hook(self):
98
        """install_hook should append the callable for known hooks."""
99
        hooks = Hooks()
3256.2.32 by Ian Clatworthy
tweak hook tests to init the set_rh test when needed
100
        hooks['set_rh'] = []
3256.2.30 by Daniel Watkins
Updated deprecation warnings and tests.
101
        self.applyDeprecated(one_five, hooks.install_hook, 'set_rh', None)
2553.1.1 by Robert Collins
Give Hooks names.
102
        self.assertEqual(hooks['set_rh'], [None])
103
3256.2.4 by Daniel Watkins
Modified tests to reflect install_named_hook becoming a separate method.
104
    def test_install_named_hook_raises_unknown_hook(self):
105
        hooks = Hooks()
106
        self.assertRaises(UnknownHook, hooks.install_named_hook, 'silly',
107
                          None, "")
108
109
    def test_install_named_hook_appends_known_hook(self):
110
        hooks = Hooks()
3256.2.32 by Ian Clatworthy
tweak hook tests to init the set_rh test when needed
111
        hooks['set_rh'] = []
3256.2.4 by Daniel Watkins
Modified tests to reflect install_named_hook becoming a separate method.
112
        hooks.install_named_hook('set_rh', None, "demo")
113
        self.assertEqual(hooks['set_rh'], [None])
114
115
    def test_install_named_hook_and_retrieve_name(self):
116
        hooks = Hooks()
3256.2.32 by Ian Clatworthy
tweak hook tests to init the set_rh test when needed
117
        hooks['set_rh'] = []
3256.2.4 by Daniel Watkins
Modified tests to reflect install_named_hook becoming a separate method.
118
        hooks.install_named_hook('set_rh', None, "demo")
3256.2.1 by Daniel Watkins
Added tests from Hooks.install_hook with an optional name parameter.
119
        self.assertEqual("demo", hooks.get_hook_name(None))
120
2553.1.1 by Robert Collins
Give Hooks names.
121
    def test_name_hook_and_retrieve_name(self):
122
        """name_hook puts the name in the names mapping."""
123
        hooks = Hooks()
3256.2.32 by Ian Clatworthy
tweak hook tests to init the set_rh test when needed
124
        hooks['set_rh'] = []
3256.2.30 by Daniel Watkins
Updated deprecation warnings and tests.
125
        self.applyDeprecated(one_five, hooks.install_hook, 'set_rh', None)
2553.1.1 by Robert Collins
Give Hooks names.
126
        hooks.name_hook(None, 'demo')
127
        self.assertEqual("demo", hooks.get_hook_name(None))
128
129
    def test_get_unnamed_hook_name_is_unnamed(self):
130
        hooks = Hooks()
3256.2.32 by Ian Clatworthy
tweak hook tests to init the set_rh test when needed
131
        hooks['set_rh'] = []
3256.2.30 by Daniel Watkins
Updated deprecation warnings and tests.
132
        self.applyDeprecated(one_five, hooks.install_hook, 'set_rh', None)
2553.1.1 by Robert Collins
Give Hooks names.
133
        self.assertEqual("No hook name", hooks.get_hook_name(None))
4098.2.1 by Robert Collins
Allow self documenting hooks.
134
135
136
class TestHook(TestCase):
137
138
    def test___init__(self):
139
        doc = ("Invoked after changing the tip of a branch object. Called with"
140
            "a bzrlib.branch.PostChangeBranchTipParams object")
4098.2.2 by Robert Collins
Review feedback.
141
        hook = HookPoint("post_tip_change", doc, (0, 15), None)
4098.2.1 by Robert Collins
Allow self documenting hooks.
142
        self.assertEqual(doc, hook.__doc__)
143
        self.assertEqual("post_tip_change", hook.name)
144
        self.assertEqual((0, 15), hook.introduced)
145
        self.assertEqual(None, hook.deprecated)
146
        self.assertEqual([], list(hook))
147
148
    def test_docs(self):
149
        doc = ("Invoked after changing the tip of a branch object. Called with"
150
            " a bzrlib.branch.PostChangeBranchTipParams object")
4098.2.2 by Robert Collins
Review feedback.
151
        hook = HookPoint("post_tip_change", doc, (0, 15), None)
4098.2.1 by Robert Collins
Allow self documenting hooks.
152
        self.assertEqual("post_tip_change\n"
153
            "---------------\n"
154
            "\n"
155
            "Introduced in: 0.15\n"
156
            "Deprecated in: Not deprecated\n"
157
            "\n"
158
            "Invoked after changing the tip of a branch object. Called with a\n"
159
            "bzrlib.branch.PostChangeBranchTipParams object\n", hook.docs())
160
161
    def test_hook(self):
4098.2.2 by Robert Collins
Review feedback.
162
        hook = HookPoint("foo", "no docs", None, None)
4098.2.1 by Robert Collins
Allow self documenting hooks.
163
        def callback():
164
            pass
165
        hook.hook(callback, "my callback")
166
        self.assertEqual([callback], list(hook))
167
168
    def test___repr(self):
169
        # The repr should list all the callbacks, with names.
4098.2.2 by Robert Collins
Review feedback.
170
        hook = HookPoint("foo", "no docs", None, None)
4098.2.1 by Robert Collins
Allow self documenting hooks.
171
        def callback():
172
            pass
173
        hook.hook(callback, "my callback")
174
        callback_repr = repr(callback)
175
        self.assertEqual(
4098.2.2 by Robert Collins
Review feedback.
176
            '<HookPoint(foo), callbacks=[%s(my callback)]>' %
4098.2.1 by Robert Collins
Allow self documenting hooks.
177
            callback_repr, repr(hook))