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