/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
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
2245.1.2 by Robert Collins
Remove the static DefaultHooks method from Branch, replacing it with a derived dict BranchHooks object, which is easier to use and provides a place to put the policy-checking add method discussed on list.
17
"""Tests that branch classes implement hook callouts correctly."""
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
18
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
19
from bzrlib.branch import Branch, ChangeBranchTipParams
3331.1.2 by James Henstridge
Add calls to set_last_revision_info hook to both BzrBranch and
20
from bzrlib.revision import NULL_REVISION
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
21
from bzrlib.tests import TestCaseWithMemoryTransport
22
23
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
24
class TestSetRevisionHistoryHook(TestCaseWithMemoryTransport):
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
25
26
    def setUp(self):
27
        self.hook_calls = []
28
        TestCaseWithMemoryTransport.setUp(self)
29
30
    def capture_set_rh_hook(self, branch, rev_history):
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
31
        """Capture post set-rh hook calls to self.hook_calls.
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
32
        
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
33
        The call is logged, as is some state of the branch.
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
34
        """
35
        self.hook_calls.append(
36
            ('set_rh', branch, rev_history, branch.is_locked()))
37
38
    def test_set_rh_empty_history(self):
39
        branch = self.make_branch('source')
3256.2.15 by Daniel Watkins
Updated uses of Hooks.install_hook to Hooks.install_named_hook in tests.branch_implementation.test_hooks.
40
        Branch.hooks.install_named_hook('set_rh', self.capture_set_rh_hook,
41
                                        None)
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
42
        branch.set_revision_history([])
43
        self.assertEqual(self.hook_calls,
44
            [('set_rh', branch, [], True)])
45
46
    def test_set_rh_nonempty_history(self):
2230.3.20 by Aaron Bentley
Add hooking for set_revision_history
47
        tree = self.make_branch_and_memory_tree('source')
48
        tree.lock_write()
49
        tree.add('')
2696.3.7 by Martin Pool
Update hook test to cope with branches that can't set their last revision to one that's not present
50
        tree.commit('another commit', rev_id='f\xc2\xb5')
2230.3.20 by Aaron Bentley
Add hooking for set_revision_history
51
        tree.commit('empty commit', rev_id='foo')
52
        tree.unlock()
53
        branch = tree.branch
3256.2.15 by Daniel Watkins
Updated uses of Hooks.install_hook to Hooks.install_named_hook in tests.branch_implementation.test_hooks.
54
        Branch.hooks.install_named_hook('set_rh', self.capture_set_rh_hook,
55
                                        None)
2696.3.7 by Martin Pool
Update hook test to cope with branches that can't set their last revision to one that's not present
56
        # some branches require that their history be set to a revision in the
57
        # repository
2309.4.10 by John Arbash Meinel
(fixed) Fix the last few tests that were explicitly passing around unicode ids
58
        branch.set_revision_history(['f\xc2\xb5'])
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
59
        self.assertEqual(self.hook_calls,
2309.4.10 by John Arbash Meinel
(fixed) Fix the last few tests that were explicitly passing around unicode ids
60
            [('set_rh', branch, ['f\xc2\xb5'], True)])
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
61
62
    def test_set_rh_branch_is_locked(self):
63
        branch = self.make_branch('source')
3256.2.15 by Daniel Watkins
Updated uses of Hooks.install_hook to Hooks.install_named_hook in tests.branch_implementation.test_hooks.
64
        Branch.hooks.install_named_hook('set_rh', self.capture_set_rh_hook,
65
                                        None)
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
66
        branch.set_revision_history([])
67
        self.assertEqual(self.hook_calls,
68
            [('set_rh', branch, [], True)])
69
70
    def test_set_rh_calls_all_hooks_no_errors(self):
71
        branch = self.make_branch('source')
3256.2.15 by Daniel Watkins
Updated uses of Hooks.install_hook to Hooks.install_named_hook in tests.branch_implementation.test_hooks.
72
        Branch.hooks.install_named_hook('set_rh', self.capture_set_rh_hook,
73
                                        None)
74
        Branch.hooks.install_named_hook('set_rh', self.capture_set_rh_hook,
75
                                        None)
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
76
        branch.set_revision_history([])
77
        self.assertEqual(self.hook_calls,
78
            [('set_rh', branch, [], True),
79
             ('set_rh', branch, [], True),
80
            ])
3331.1.2 by James Henstridge
Add calls to set_last_revision_info hook to both BzrBranch and
81
82
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
83
class ChangeBranchTipTestCase(TestCaseWithMemoryTransport):
84
    """Base TestCase for testing pre/post_change_branch_tip hooks."""
85
86
    def install_logging_hook(self, prefix):
87
        """Add a hook that logs calls made to it.
88
        
89
        :returns: the list that the calls will be appended to.
90
        """
91
        hook_calls = []
92
        Branch.hooks.install_named_hook(
93
            'pre_change_branch_tip', hook_calls.append, None)
94
        return hook_calls
95
96
    def make_branch_with_revision_ids(self, *revision_ids):
97
        """Makes a branch with the given commits."""
98
        tree = self.make_branch_and_memory_tree('source')
99
        tree.lock_write()
100
        tree.add('')
101
        for revision_id in revision_ids:
3557.1.1 by John Arbash Meinel
Fix bug #247585: decode from utf8 to Unicode when giving a commit message
102
            tree.commit(u'Message of ' + revision_id.decode('utf8'),
103
                        rev_id=revision_id)
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
104
        tree.unlock()
105
        branch = tree.branch
106
        return branch
107
108
109
class TestPreChangeBranchTip(ChangeBranchTipTestCase):
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
110
    """Tests for pre_change_branch_tip hook.
111
    
112
    Most of these tests are very similar to the tests in
113
    TestPostChangeBranchTip.
114
    """
115
116
    def test_hook_runs_before_change(self):
117
        """The hook runs *before* the branch's last_revision_info has changed.
118
        """
119
        branch = self.make_branch_with_revision_ids('revid-one')
120
        def assertBranchAtRevision1(params):
121
            self.assertEquals(
122
                (1, 'revid-one'), params.branch.last_revision_info())
123
        Branch.hooks.install_named_hook(
124
            'pre_change_branch_tip', assertBranchAtRevision1, None)
125
        branch.set_last_revision_info(0, NULL_REVISION)
126
3517.2.2 by Andrew Bennetts
Add test for a pre_change_branch_tip hook rejecting a change.
127
    def test_reject_by_hook(self):
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
128
        """If a hook raises an exception, the change does not take effect.
129
        
130
        Also, the exception will be propogated.
131
        """
3517.2.2 by Andrew Bennetts
Add test for a pre_change_branch_tip hook rejecting a change.
132
        branch = self.make_branch_with_revision_ids(
133
            'one-\xc2\xb5', 'two-\xc2\xb5')
134
        class PearShapedError(Exception):
135
            pass
136
        def hook_that_raises(params):
137
            raise PearShapedError()
138
        Branch.hooks.install_named_hook(
139
            'pre_change_branch_tip', hook_that_raises, None)
140
        self.assertRaises(
141
            PearShapedError, branch.set_last_revision_info, 0, NULL_REVISION)
142
        # The revision info is unchanged.
143
        self.assertEqual((2, 'two-\xc2\xb5'), branch.last_revision_info())
144
        
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
145
    def test_empty_history(self):
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
146
        branch = self.make_branch('source')
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
147
        hook_calls = self.install_logging_hook('pre')
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
148
        branch.set_last_revision_info(0, NULL_REVISION)
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
149
        expected_params = ChangeBranchTipParams(
150
            branch, 0, 0, NULL_REVISION, NULL_REVISION)
151
        self.assertEqual([expected_params], hook_calls)
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
152
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
153
    def test_nonempty_history(self):
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
154
        # some branches require that their history be set to a revision in the
155
        # repository, so we need to make a branch with non-empty history for
156
        # this test.
3517.2.2 by Andrew Bennetts
Add test for a pre_change_branch_tip hook rejecting a change.
157
        branch = self.make_branch_with_revision_ids(
158
            'one-\xc2\xb5', 'two-\xc2\xb5')
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
159
        hook_calls = self.install_logging_hook('pre')
3517.2.2 by Andrew Bennetts
Add test for a pre_change_branch_tip hook rejecting a change.
160
        branch.set_last_revision_info(1, 'one-\xc2\xb5')
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
161
        expected_params = ChangeBranchTipParams(
162
            branch, 2, 1, 'two-\xc2\xb5', 'one-\xc2\xb5')
163
        self.assertEqual([expected_params], hook_calls)
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
164
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
165
    def test_branch_is_locked(self):
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
166
        branch = self.make_branch('source')
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
167
        def assertBranchIsLocked(params):
168
            self.assertTrue(params.branch.is_locked())
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
169
        Branch.hooks.install_named_hook(
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
170
            'pre_change_branch_tip', assertBranchIsLocked, None)
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
171
        branch.set_last_revision_info(0, NULL_REVISION)
172
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
173
    def test_calls_all_hooks_no_errors(self):
174
        """If multiple hooks are registered, all are called (if none raise
175
        errors).
176
        """
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
177
        branch = self.make_branch('source')
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
178
        hook_calls_1 = self.install_logging_hook('pre')
179
        hook_calls_2 = self.install_logging_hook('pre')
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
180
        self.assertIsNot(hook_calls_1, hook_calls_2)
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
181
        branch.set_last_revision_info(0, NULL_REVISION)
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
182
        # Both hooks are called.
183
        self.assertEqual(len(hook_calls_1), 1)
184
        self.assertEqual(len(hook_calls_2), 1)
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
185
186
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
187
class TestPostChangeBranchTip(ChangeBranchTipTestCase):
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
188
    """Tests for post_change_branch_tip hook.
189
190
    Most of these tests are very similar to the tests in
191
    TestPostChangeBranchTip.
192
    """
193
194
    def test_hook_runs_after_change(self):
195
        """The hook runs *after* the branch's last_revision_info has changed.
196
        """
197
        branch = self.make_branch_with_revision_ids('revid-one')
198
        def assertBranchAtRevision1(params):
199
            self.assertEquals(
200
                (0, NULL_REVISION), params.branch.last_revision_info())
3256.2.26 by Daniel Watkins
Updated tests to use install_named_hook.
201
        Branch.hooks.install_named_hook(
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
202
            'post_change_branch_tip', assertBranchAtRevision1, None)
203
        branch.set_last_revision_info(0, NULL_REVISION)
204
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
205
    def test_empty_history(self):
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
206
        branch = self.make_branch('source')
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
207
        hook_calls = self.install_logging_hook('post')
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
208
        branch.set_last_revision_info(0, NULL_REVISION)
209
        expected_params = ChangeBranchTipParams(
210
            branch, 0, 0, NULL_REVISION, NULL_REVISION)
211
        self.assertEqual([expected_params], hook_calls)
212
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
213
    def test_nonempty_history(self):
3331.1.2 by James Henstridge
Add calls to set_last_revision_info hook to both BzrBranch and
214
        # some branches require that their history be set to a revision in the
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
215
        # repository, so we need to make a branch with non-empty history for
216
        # this test.
217
        branch = self.make_branch_with_revision_ids(
218
            'one-\xc2\xb5', 'two-\xc2\xb5')
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
219
        hook_calls = self.install_logging_hook('post')
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
220
        branch.set_last_revision_info(1, 'one-\xc2\xb5')
221
        expected_params = ChangeBranchTipParams(
222
            branch, 2, 1, 'two-\xc2\xb5', 'one-\xc2\xb5')
223
        self.assertEqual([expected_params], hook_calls)
224
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
225
    def test_branch_is_locked(self):
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
226
        """The branch passed to the hook is locked."""
227
        branch = self.make_branch('source')
228
        def assertBranchIsLocked(params):
229
            self.assertTrue(params.branch.is_locked())
230
        Branch.hooks.install_named_hook(
3517.2.4 by Andrew Bennetts
Fix typo.
231
            'post_change_branch_tip', assertBranchIsLocked, None)
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
232
        branch.set_last_revision_info(0, NULL_REVISION)
233
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
234
    def test_calls_all_hooks_no_errors(self):
235
        """If multiple hooks are registered, all are called (if none raise
236
        errors).
237
        """
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
238
        branch = self.make_branch('source')
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
239
        hook_calls_1 = self.install_logging_hook('post')
240
        hook_calls_2 = self.install_logging_hook('post')
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
241
        self.assertIsNot(hook_calls_1, hook_calls_2)
242
        branch.set_last_revision_info(0, NULL_REVISION)
243
        # Both hooks are called.
244
        self.assertEqual(len(hook_calls_1), 1)
245
        self.assertEqual(len(hook_calls_2), 1)