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