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