~bzr-pqm/bzr/bzr.dev

4988.10.5 by John Arbash Meinel
Merge bzr.dev 5021 to resolve NEWS
1
# Copyright (C) 2007-2010 Canonical Ltd
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
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
5010.2.5 by Vincent Ladeuil
Fix per_branch/test_hooks.py imports.
19
from bzrlib import (
20
    branch as _mod_branch,
21
    errors,
22
    remote,
23
    revision,
24
    tests,
25
    )
5017.3.31 by Vincent Ladeuil
-s bt.per_branch.test_hooks passing
26
from bzrlib.tests import test_server
5010.2.5 by Vincent Ladeuil
Fix per_branch/test_hooks.py imports.
27
28
class ChangeBranchTipTestCase(tests.TestCaseWithMemoryTransport):
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
29
    """Base TestCase for testing pre/post_change_branch_tip hooks."""
30
31
    def install_logging_hook(self, prefix):
32
        """Add a hook that logs calls made to it.
33
34
        :returns: the list that the calls will be appended to.
35
        """
36
        hook_calls = []
5010.2.5 by Vincent Ladeuil
Fix per_branch/test_hooks.py imports.
37
        _mod_branch.Branch.hooks.install_named_hook(
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
38
            prefix + '_change_branch_tip', hook_calls.append, None)
39
        return hook_calls
40
41
    def make_branch_with_revision_ids(self, *revision_ids):
42
        """Makes a branch with the given commits."""
43
        tree = self.make_branch_and_memory_tree('source')
44
        tree.lock_write()
45
        tree.add('')
46
        for revision_id in revision_ids:
47
            tree.commit(u'Message of ' + revision_id.decode('utf8'),
48
                        rev_id=revision_id)
49
        tree.unlock()
50
        branch = tree.branch
51
        return branch
52
53
    def assertHookCalls(self, expected_params, branch, hook_calls=None,
54
        pre=False):
55
        if hook_calls is None:
56
            hook_calls = self.hook_calls
5010.2.5 by Vincent Ladeuil
Fix per_branch/test_hooks.py imports.
57
        if isinstance(branch, remote.RemoteBranch):
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
58
            # For a remote branch, both the server and the client will raise
59
            # this hook, and we see both in the test environment. The remote
60
            # instance comes in between the clients - the client doe pre, the
61
            # server does pre, the server does post, the client does post.
62
            if pre:
63
                offset = 0
64
            else:
65
                offset = 1
66
            self.assertEqual(expected_params, hook_calls[offset])
67
            self.assertEqual(2, len(hook_calls))
68
        else:
69
            self.assertEqual([expected_params], hook_calls)
70
71
72
class TestSetRevisionHistoryHook(ChangeBranchTipTestCase):
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
73
74
    def setUp(self):
75
        self.hook_calls = []
5010.2.5 by Vincent Ladeuil
Fix per_branch/test_hooks.py imports.
76
        super(TestSetRevisionHistoryHook, self).setUp()
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
77
78
    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
79
        """Capture post set-rh hook calls to self.hook_calls.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
80
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
81
        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
82
        """
83
        self.hook_calls.append(
84
            ('set_rh', branch, rev_history, branch.is_locked()))
85
86
    def test_set_rh_empty_history(self):
87
        branch = self.make_branch('source')
5010.2.5 by Vincent Ladeuil
Fix per_branch/test_hooks.py imports.
88
        _mod_branch.Branch.hooks.install_named_hook(
89
            'set_rh', self.capture_set_rh_hook, None)
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
90
        branch.set_revision_history([])
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
91
        expected_params = ('set_rh', branch, [], True)
92
        self.assertHookCalls(expected_params, branch)
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
93
94
    def test_set_rh_nonempty_history(self):
2230.3.20 by Aaron Bentley
Add hooking for set_revision_history
95
        tree = self.make_branch_and_memory_tree('source')
96
        tree.lock_write()
97
        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
98
        tree.commit('another commit', rev_id='f\xc2\xb5')
2230.3.20 by Aaron Bentley
Add hooking for set_revision_history
99
        tree.commit('empty commit', rev_id='foo')
100
        tree.unlock()
101
        branch = tree.branch
5010.2.5 by Vincent Ladeuil
Fix per_branch/test_hooks.py imports.
102
        _mod_branch.Branch.hooks.install_named_hook(
103
            'set_rh', self.capture_set_rh_hook, 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
104
        # some branches require that their history be set to a revision in the
105
        # repository
2309.4.10 by John Arbash Meinel
(fixed) Fix the last few tests that were explicitly passing around unicode ids
106
        branch.set_revision_history(['f\xc2\xb5'])
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
107
        expected_params =('set_rh', branch, ['f\xc2\xb5'], True)
108
        self.assertHookCalls(expected_params, branch)
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
109
110
    def test_set_rh_branch_is_locked(self):
111
        branch = self.make_branch('source')
5010.2.5 by Vincent Ladeuil
Fix per_branch/test_hooks.py imports.
112
        _mod_branch.Branch.hooks.install_named_hook(
113
            'set_rh', self.capture_set_rh_hook, None)
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
114
        branch.set_revision_history([])
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
115
        expected_params = ('set_rh', branch, [], True)
116
        self.assertHookCalls(expected_params, branch)
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
117
118
    def test_set_rh_calls_all_hooks_no_errors(self):
119
        branch = self.make_branch('source')
5010.2.5 by Vincent Ladeuil
Fix per_branch/test_hooks.py imports.
120
        _mod_branch.Branch.hooks.install_named_hook(
121
            'set_rh', self.capture_set_rh_hook, None)
122
        _mod_branch.Branch.hooks.install_named_hook(
123
            'set_rh', self.capture_set_rh_hook, None)
2245.1.1 by Robert Collins
New Branch hooks facility, with one initial hook 'set_rh' which triggers
124
        branch.set_revision_history([])
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
125
        expected_calls = [('set_rh', branch, [], True),
126
            ('set_rh', branch, [], True),
127
            ]
5010.2.5 by Vincent Ladeuil
Fix per_branch/test_hooks.py imports.
128
        if isinstance(branch, remote.RemoteBranch):
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
129
            # For a remote branch, both the server and the client will raise
130
            # set_rh, and the server will do so first because that is where
131
            # the change takes place.
132
            self.assertEqual(expected_calls, self.hook_calls[2:])
133
            self.assertEqual(4, len(self.hook_calls))
134
        else:
135
            self.assertEqual(expected_calls, self.hook_calls)
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
136
137
5010.2.5 by Vincent Ladeuil
Fix per_branch/test_hooks.py imports.
138
class TestOpen(tests.TestCaseWithMemoryTransport):
3681.1.1 by Robert Collins
Create a new hook Branch.open. (Robert Collins)
139
140
    def capture_hook(self, branch):
141
        self.hook_calls.append(branch)
142
143
    def install_hook(self):
144
        self.hook_calls = []
5010.2.5 by Vincent Ladeuil
Fix per_branch/test_hooks.py imports.
145
        _mod_branch.Branch.hooks.install_named_hook(
146
            'open', self.capture_hook, None)
3681.1.1 by Robert Collins
Create a new hook Branch.open. (Robert Collins)
147
148
    def test_create(self):
149
        self.install_hook()
150
        b = self.make_branch('.')
5010.2.5 by Vincent Ladeuil
Fix per_branch/test_hooks.py imports.
151
        if isinstance(b, remote.RemoteBranch):
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
152
            # RemoteBranch creation:
5017.3.31 by Vincent Ladeuil
-s bt.per_branch.test_hooks passing
153
            if (self.transport_readonly_server
154
                == test_server.ReadonlySmartTCPServer_for_testing_v2_only):
4032.3.2 by Robert Collins
Create and use a RPC call to create branches on bzr servers rather than using VFS calls.
155
                # Older servers:
4084.2.3 by Andrew Bennetts
Adjust test_hooks.py for create_branch RPC.
156
                self.assertEqual(3, len(self.hook_calls))
157
                # creates the branch via the VFS (for older servers)
4032.3.2 by Robert Collins
Create and use a RPC call to create branches on bzr servers rather than using VFS calls.
158
                self.assertEqual(b._real_branch, self.hook_calls[0])
4084.2.3 by Andrew Bennetts
Adjust test_hooks.py for create_branch RPC.
159
                # creates a RemoteBranch object
160
                self.assertEqual(b, self.hook_calls[1])
161
                # get_stacked_on_url RPC
162
                self.assertRealBranch(self.hook_calls[2])
4032.3.2 by Robert Collins
Create and use a RPC call to create branches on bzr servers rather than using VFS calls.
163
            else:
4084.2.3 by Andrew Bennetts
Adjust test_hooks.py for create_branch RPC.
164
                self.assertEqual(2, len(self.hook_calls))
165
                # create_branch RPC
166
                self.assertRealBranch(self.hook_calls[0])
167
                # create RemoteBranch locally
168
                self.assertEqual(b, self.hook_calls[1])
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
169
        else:
170
            self.assertEqual([b], self.hook_calls)
3681.1.1 by Robert Collins
Create a new hook Branch.open. (Robert Collins)
171
172
    def test_open(self):
173
        branch_url = self.make_branch('.').bzrdir.root_transport.base
174
        self.install_hook()
5010.2.5 by Vincent Ladeuil
Fix per_branch/test_hooks.py imports.
175
        b = _mod_branch.Branch.open(branch_url)
176
        if isinstance(b, remote.RemoteBranch):
4084.2.3 by Andrew Bennetts
Adjust test_hooks.py for create_branch RPC.
177
            self.assertEqual(3, len(self.hook_calls))
178
            # open_branchV2 RPC
179
            self.assertRealBranch(self.hook_calls[0])
180
            # create RemoteBranch locally
181
            self.assertEqual(b, self.hook_calls[1])
182
            # get_stacked_on_url RPC
183
            self.assertRealBranch(self.hook_calls[2])
3681.1.3 by Robert Collins
Update branch open tests to accomodate stacking.
184
        else:
185
            self.assertEqual([b], self.hook_calls)
3681.1.1 by Robert Collins
Create a new hook Branch.open. (Robert Collins)
186
4084.2.3 by Andrew Bennetts
Adjust test_hooks.py for create_branch RPC.
187
    def assertRealBranch(self, b):
188
        # Branches opened on the server don't have comparable URLs, so we just
189
        # assert that it is not a RemoteBranch.
5010.2.5 by Vincent Ladeuil
Fix per_branch/test_hooks.py imports.
190
        self.assertIsInstance(b, _mod_branch.Branch)
191
        self.assertFalse(isinstance(b, remote.RemoteBranch))
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
192
3681.1.1 by Robert Collins
Create a new hook Branch.open. (Robert Collins)
193
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
194
class TestPreChangeBranchTip(ChangeBranchTipTestCase):
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
195
    """Tests for pre_change_branch_tip hook.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
196
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
197
    Most of these tests are very similar to the tests in
198
    TestPostChangeBranchTip.
199
    """
200
201
    def test_hook_runs_before_change(self):
202
        """The hook runs *before* the branch's last_revision_info has changed.
203
        """
204
        branch = self.make_branch_with_revision_ids('revid-one')
205
        def assertBranchAtRevision1(params):
206
            self.assertEquals(
207
                (1, 'revid-one'), params.branch.last_revision_info())
5010.2.5 by Vincent Ladeuil
Fix per_branch/test_hooks.py imports.
208
        _mod_branch.Branch.hooks.install_named_hook(
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
209
            'pre_change_branch_tip', assertBranchAtRevision1, None)
5010.2.5 by Vincent Ladeuil
Fix per_branch/test_hooks.py imports.
210
        branch.set_last_revision_info(0, revision.NULL_REVISION)
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
211
3577.1.1 by Andrew Bennetts
Cherry-pick TipChangeRejected changes from pre-branch-tip-changed-hook loom.
212
    def test_hook_failure_prevents_change(self):
4943.1.1 by Robert Collins
Do not fiddle with exceptions in the pre_change_branch_tip hook running code.
213
        """If a hook raises an exception, the change does not take effect."""
3517.2.2 by Andrew Bennetts
Add test for a pre_change_branch_tip hook rejecting a change.
214
        branch = self.make_branch_with_revision_ids(
215
            'one-\xc2\xb5', 'two-\xc2\xb5')
216
        class PearShapedError(Exception):
217
            pass
218
        def hook_that_raises(params):
219
            raise PearShapedError()
5010.2.5 by Vincent Ladeuil
Fix per_branch/test_hooks.py imports.
220
        _mod_branch.Branch.hooks.install_named_hook(
3517.2.2 by Andrew Bennetts
Add test for a pre_change_branch_tip hook rejecting a change.
221
            'pre_change_branch_tip', hook_that_raises, None)
3577.1.1 by Andrew Bennetts
Cherry-pick TipChangeRejected changes from pre-branch-tip-changed-hook loom.
222
        hook_failed_exc = self.assertRaises(
5010.2.5 by Vincent Ladeuil
Fix per_branch/test_hooks.py imports.
223
            PearShapedError,
224
            branch.set_last_revision_info, 0, revision.NULL_REVISION)
3517.2.2 by Andrew Bennetts
Add test for a pre_change_branch_tip hook rejecting a change.
225
        # The revision info is unchanged.
226
        self.assertEqual((2, 'two-\xc2\xb5'), branch.last_revision_info())
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
227
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
228
    def test_empty_history(self):
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
229
        branch = self.make_branch('source')
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
230
        hook_calls = self.install_logging_hook('pre')
5010.2.5 by Vincent Ladeuil
Fix per_branch/test_hooks.py imports.
231
        branch.set_last_revision_info(0, revision.NULL_REVISION)
232
        expected_params = _mod_branch.ChangeBranchTipParams(
233
            branch, 0, 0, revision.NULL_REVISION, revision.NULL_REVISION)
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
234
        self.assertHookCalls(expected_params, branch, hook_calls, pre=True)
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
235
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
236
    def test_nonempty_history(self):
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
237
        # some branches require that their history be set to a revision in the
238
        # repository, so we need to make a branch with non-empty history for
239
        # this test.
3517.2.2 by Andrew Bennetts
Add test for a pre_change_branch_tip hook rejecting a change.
240
        branch = self.make_branch_with_revision_ids(
241
            'one-\xc2\xb5', 'two-\xc2\xb5')
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
242
        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.
243
        branch.set_last_revision_info(1, 'one-\xc2\xb5')
5010.2.5 by Vincent Ladeuil
Fix per_branch/test_hooks.py imports.
244
        expected_params = _mod_branch.ChangeBranchTipParams(
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
245
            branch, 2, 1, 'two-\xc2\xb5', 'one-\xc2\xb5')
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
246
        self.assertHookCalls(expected_params, branch, hook_calls, pre=True)
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
247
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
248
    def test_branch_is_locked(self):
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
249
        branch = self.make_branch('source')
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
250
        def assertBranchIsLocked(params):
251
            self.assertTrue(params.branch.is_locked())
5010.2.5 by Vincent Ladeuil
Fix per_branch/test_hooks.py imports.
252
        _mod_branch.Branch.hooks.install_named_hook(
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
253
            'pre_change_branch_tip', assertBranchIsLocked, None)
5010.2.5 by Vincent Ladeuil
Fix per_branch/test_hooks.py imports.
254
        branch.set_last_revision_info(0, revision.NULL_REVISION)
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
255
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
256
    def test_calls_all_hooks_no_errors(self):
257
        """If multiple hooks are registered, all are called (if none raise
258
        errors).
259
        """
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
260
        branch = self.make_branch('source')
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
261
        hook_calls_1 = self.install_logging_hook('pre')
262
        hook_calls_2 = self.install_logging_hook('pre')
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
263
        self.assertIsNot(hook_calls_1, hook_calls_2)
5010.2.5 by Vincent Ladeuil
Fix per_branch/test_hooks.py imports.
264
        branch.set_last_revision_info(0, revision.NULL_REVISION)
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
265
        # Both hooks are called.
5010.2.5 by Vincent Ladeuil
Fix per_branch/test_hooks.py imports.
266
        if isinstance(branch, remote.RemoteBranch):
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
267
            count = 2
268
        else:
269
            count = 1
270
        self.assertEqual(len(hook_calls_1), count)
271
        self.assertEqual(len(hook_calls_2), count)
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
272
3577.1.1 by Andrew Bennetts
Cherry-pick TipChangeRejected changes from pre-branch-tip-changed-hook loom.
273
    def test_explicit_reject_by_hook(self):
274
        """If a hook raises TipChangeRejected, the change does not take effect.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
275
3577.1.1 by Andrew Bennetts
Cherry-pick TipChangeRejected changes from pre-branch-tip-changed-hook loom.
276
        TipChangeRejected exceptions are propagated, not wrapped in HookFailed.
277
        """
278
        branch = self.make_branch_with_revision_ids(
279
            'one-\xc2\xb5', 'two-\xc2\xb5')
280
        def hook_that_rejects(params):
5010.2.5 by Vincent Ladeuil
Fix per_branch/test_hooks.py imports.
281
            raise errors.TipChangeRejected('rejection message')
282
        _mod_branch.Branch.hooks.install_named_hook(
3577.1.1 by Andrew Bennetts
Cherry-pick TipChangeRejected changes from pre-branch-tip-changed-hook loom.
283
            'pre_change_branch_tip', hook_that_rejects, None)
284
        self.assertRaises(
5010.2.5 by Vincent Ladeuil
Fix per_branch/test_hooks.py imports.
285
            errors.TipChangeRejected,
286
            branch.set_last_revision_info, 0, revision.NULL_REVISION)
3577.1.1 by Andrew Bennetts
Cherry-pick TipChangeRejected changes from pre-branch-tip-changed-hook loom.
287
        # The revision info is unchanged.
288
        self.assertEqual((2, 'two-\xc2\xb5'), branch.last_revision_info())
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
289
3517.2.1 by Andrew Bennetts
Quick draft of pre_change_branch_tip hook.
290
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
291
class TestPostChangeBranchTip(ChangeBranchTipTestCase):
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
292
    """Tests for post_change_branch_tip hook.
293
294
    Most of these tests are very similar to the tests in
295
    TestPostChangeBranchTip.
296
    """
297
298
    def test_hook_runs_after_change(self):
299
        """The hook runs *after* the branch's last_revision_info has changed.
300
        """
301
        branch = self.make_branch_with_revision_ids('revid-one')
302
        def assertBranchAtRevision1(params):
303
            self.assertEquals(
5010.2.5 by Vincent Ladeuil
Fix per_branch/test_hooks.py imports.
304
                (0, revision.NULL_REVISION), params.branch.last_revision_info())
305
        _mod_branch.Branch.hooks.install_named_hook(
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
306
            'post_change_branch_tip', assertBranchAtRevision1, None)
5010.2.5 by Vincent Ladeuil
Fix per_branch/test_hooks.py imports.
307
        branch.set_last_revision_info(0, revision.NULL_REVISION)
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
308
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
309
    def test_empty_history(self):
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
310
        branch = self.make_branch('source')
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
311
        hook_calls = self.install_logging_hook('post')
5010.2.5 by Vincent Ladeuil
Fix per_branch/test_hooks.py imports.
312
        branch.set_last_revision_info(0, revision.NULL_REVISION)
313
        expected_params = _mod_branch.ChangeBranchTipParams(
314
            branch, 0, 0, revision.NULL_REVISION, revision.NULL_REVISION)
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
315
        self.assertHookCalls(expected_params, branch, hook_calls)
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
316
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
317
    def test_nonempty_history(self):
3331.1.2 by James Henstridge
Add calls to set_last_revision_info hook to both BzrBranch and
318
        # 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.
319
        # repository, so we need to make a branch with non-empty history for
320
        # this test.
321
        branch = self.make_branch_with_revision_ids(
322
            'one-\xc2\xb5', 'two-\xc2\xb5')
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
323
        hook_calls = self.install_logging_hook('post')
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
324
        branch.set_last_revision_info(1, 'one-\xc2\xb5')
5010.2.5 by Vincent Ladeuil
Fix per_branch/test_hooks.py imports.
325
        expected_params = _mod_branch.ChangeBranchTipParams(
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
326
            branch, 2, 1, 'two-\xc2\xb5', 'one-\xc2\xb5')
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
327
        self.assertHookCalls(expected_params, branch, hook_calls)
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
328
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
329
    def test_branch_is_locked(self):
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
330
        """The branch passed to the hook is locked."""
331
        branch = self.make_branch('source')
332
        def assertBranchIsLocked(params):
333
            self.assertTrue(params.branch.is_locked())
5010.2.5 by Vincent Ladeuil
Fix per_branch/test_hooks.py imports.
334
        _mod_branch.Branch.hooks.install_named_hook(
3517.2.4 by Andrew Bennetts
Fix typo.
335
            'post_change_branch_tip', assertBranchIsLocked, None)
5010.2.5 by Vincent Ladeuil
Fix per_branch/test_hooks.py imports.
336
        branch.set_last_revision_info(0, revision.NULL_REVISION)
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
337
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
338
    def test_calls_all_hooks_no_errors(self):
339
        """If multiple hooks are registered, all are called (if none raise
340
        errors).
341
        """
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
342
        branch = self.make_branch('source')
3517.2.5 by Andrew Bennetts
Reduce duplication in test_hooks a little.
343
        hook_calls_1 = self.install_logging_hook('post')
344
        hook_calls_2 = self.install_logging_hook('post')
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
345
        self.assertIsNot(hook_calls_1, hook_calls_2)
5010.2.5 by Vincent Ladeuil
Fix per_branch/test_hooks.py imports.
346
        branch.set_last_revision_info(0, revision.NULL_REVISION)
3517.2.3 by Andrew Bennetts
Better tests for {pre,post}_change_branch_tip hooks.
347
        # Both hooks are called.
5010.2.5 by Vincent Ladeuil
Fix per_branch/test_hooks.py imports.
348
        if isinstance(branch, remote.RemoteBranch):
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
349
            count = 2
350
        else:
351
            count = 1
352
        self.assertEqual(len(hook_calls_1), count)
353
        self.assertEqual(len(hook_calls_2), count)
3577.1.1 by Andrew Bennetts
Cherry-pick TipChangeRejected changes from pre-branch-tip-changed-hook loom.
354
355
356
class TestAllMethodsThatChangeTipWillRunHooks(ChangeBranchTipTestCase):
357
    """Every method of Branch that changes a branch tip will invoke the
358
    pre/post_change_branch_tip hooks.
359
    """
360
361
    def setUp(self):
362
        ChangeBranchTipTestCase.setUp(self)
363
        self.installPreAndPostHooks()
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
364
3577.1.1 by Andrew Bennetts
Cherry-pick TipChangeRejected changes from pre-branch-tip-changed-hook loom.
365
    def installPreAndPostHooks(self):
366
        self.pre_hook_calls = self.install_logging_hook('pre')
367
        self.post_hook_calls = self.install_logging_hook('post')
368
369
    def resetHookCalls(self):
370
        del self.pre_hook_calls[:], self.post_hook_calls[:]
371
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
372
    def assertPreAndPostHooksWereInvoked(self, branch, smart_enabled):
373
        """assert that both pre and post hooks were called
374
375
        :param smart_enabled: The method invoked is one that should be
376
            smart server ready.
377
        """
378
        # Check for the number of invocations expected. One invocation is
379
        # local, one is remote (if the branch is remote).
5010.2.5 by Vincent Ladeuil
Fix per_branch/test_hooks.py imports.
380
        if smart_enabled and isinstance(branch, remote.RemoteBranch):
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
381
            length = 2
382
        else:
383
            length = 1
384
        self.assertEqual(length, len(self.pre_hook_calls))
385
        self.assertEqual(length, len(self.post_hook_calls))
3577.1.1 by Andrew Bennetts
Cherry-pick TipChangeRejected changes from pre-branch-tip-changed-hook loom.
386
387
    def test_set_revision_history(self):
388
        branch = self.make_branch('')
389
        branch.set_revision_history([])
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
390
        self.assertPreAndPostHooksWereInvoked(branch, True)
3577.1.1 by Andrew Bennetts
Cherry-pick TipChangeRejected changes from pre-branch-tip-changed-hook loom.
391
392
    def test_set_last_revision_info(self):
393
        branch = self.make_branch('')
5010.2.5 by Vincent Ladeuil
Fix per_branch/test_hooks.py imports.
394
        branch.set_last_revision_info(0, revision.NULL_REVISION)
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
395
        self.assertPreAndPostHooksWereInvoked(branch, True)
3577.1.1 by Andrew Bennetts
Cherry-pick TipChangeRejected changes from pre-branch-tip-changed-hook loom.
396
397
    def test_generate_revision_history(self):
398
        branch = self.make_branch('')
5010.2.5 by Vincent Ladeuil
Fix per_branch/test_hooks.py imports.
399
        branch.generate_revision_history(revision.NULL_REVISION)
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
400
        # NB: for HPSS protocols < v3, the server does not invoke branch tip
401
        # change events on generate_revision_history, as the change is done
402
        # directly by the client over the VFS.
403
        self.assertPreAndPostHooksWereInvoked(branch, True)
3577.1.1 by Andrew Bennetts
Cherry-pick TipChangeRejected changes from pre-branch-tip-changed-hook loom.
404
405
    def test_pull(self):
406
        source_branch = self.make_branch_with_revision_ids('rev-1', 'rev-2')
407
        self.resetHookCalls()
408
        destination_branch = self.make_branch('destination')
409
        destination_branch.pull(source_branch)
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
410
        self.assertPreAndPostHooksWereInvoked(destination_branch, False)
3577.1.1 by Andrew Bennetts
Cherry-pick TipChangeRejected changes from pre-branch-tip-changed-hook loom.
411
412
    def test_push(self):
413
        source_branch = self.make_branch_with_revision_ids('rev-1', 'rev-2')
414
        self.resetHookCalls()
415
        destination_branch = self.make_branch('destination')
416
        source_branch.push(destination_branch)
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
417
        self.assertPreAndPostHooksWereInvoked(destination_branch, True)