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