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