/brz/remove-bazaar

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