/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4763.2.4 by John Arbash Meinel
merge bzr.2.1 in preparation for NEWS entry.
1
# Copyright (C) 2009, 2010 Canonical Ltd
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
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
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
17
"""Tests for branch.push behaviour."""
18
5317.1.1 by Robert Collins
Make the interbranch test_no_get_parent_map_after_insert_stream test work for looms - a little ugly, will want a generic hook-out to foreign formats in some form, at some point.
19
from testtools.matchers import (
20
    Equals,
21
    MatchesAny,
22
    )
23
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
24
from ... import (
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
25
    branch,
4332.3.35 by Robert Collins
Fix failing tests.
26
    check,
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
27
    controldir,
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
28
    errors,
29
    push,
30
    tests,
6670.4.1 by Jelmer Vernooij
Update imports.
31
    )
32
from ...bzr import (
33
    branch as bzrbranch,
5863.4.2 by Jelmer Vernooij
Fix test.
34
    vf_repository,
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
35
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
36
from ...branch import Branch
37
from ...controldir import ControlDir
38
from ...memorytree import MemoryTree
39
from ...revision import NULL_REVISION
40
from ...sixish import (
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
41
    BytesIO,
42
    )
6670.4.16 by Jelmer Vernooij
Move smart to breezy.bzr.
43
from ...bzr.smart.repository import SmartServerRepositoryGetParentMap
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
44
from . import (
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
45
    TestCaseWithInterBranch,
46
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
47
from .. import test_server
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
48
49
7143.15.2 by Jelmer Vernooij
Run autopep8.
50
# These tests are based on similar tests in
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
51
# breezy.tests.per_branch.test_push.
4211.1.6 by Jelmer Vernooij
Review from Ian.
52
53
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
54
class TestPush(TestCaseWithInterBranch):
55
56
    def test_push_convergence_simple(self):
57
        # when revisions are pushed, the left-most accessible parents must
58
        # become the revision-history.
59
        mine = self.make_from_branch_and_tree('mine')
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
60
        mine.commit('1st post', allow_pointless=True)
6883.21.1 by Jelmer Vernooij
Allow InterBranch formats to raise NoRoundtrippingSupport.
61
        try:
62
            other = self.sprout_to(mine.controldir, 'other').open_workingtree()
63
        except errors.NoRoundtrippingSupport:
64
            raise tests.TestNotApplicable(
65
                'lossless push between %r and %r not supported' %
66
                (self.branch_format_from, self.branch_format_to))
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
67
        m1 = other.commit('my change', allow_pointless=True)
6883.21.1 by Jelmer Vernooij
Allow InterBranch formats to raise NoRoundtrippingSupport.
68
        try:
69
            mine.merge_from_branch(other.branch)
70
        except errors.NoRoundtrippingSupport:
71
            raise tests.TestNotApplicable(
72
                'lossless push between %r and %r not supported' %
73
                (self.branch_format_from, self.branch_format_to))
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
74
        p2 = mine.commit('merge my change')
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
75
        result = mine.branch.push(other.branch)
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
76
        self.assertEqual(p2, other.branch.last_revision())
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
77
        # result object contains some structured data
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
78
        self.assertEqual(result.old_revid, m1)
79
        self.assertEqual(result.new_revid, p2)
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
80
81
    def test_push_merged_indirect(self):
82
        # it should be possible to do a push from one branch into another
83
        # when the tip of the target was merged into the source branch
84
        # via a third branch - so its buried in the ancestry and is not
85
        # directly accessible.
86
        mine = self.make_from_branch_and_tree('mine')
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
87
        p1 = mine.commit('1st post', allow_pointless=True)
6883.21.1 by Jelmer Vernooij
Allow InterBranch formats to raise NoRoundtrippingSupport.
88
        try:
7143.15.2 by Jelmer Vernooij
Run autopep8.
89
            target = self.sprout_to(
90
                mine.controldir, 'target').open_workingtree()
6883.21.1 by Jelmer Vernooij
Allow InterBranch formats to raise NoRoundtrippingSupport.
91
        except errors.NoRoundtrippingSupport:
92
            raise tests.TestNotApplicable(
93
                'lossless push between %r and %r not supported' %
94
                (self.branch_format_from, self.branch_format_to))
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
95
        m1 = target.commit('my change', allow_pointless=True)
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
96
        other = self.sprout_to(mine.controldir, 'other').open_workingtree()
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
97
        other.merge_from_branch(target.branch)
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
98
        o2 = other.commit('merge my change')
6883.21.1 by Jelmer Vernooij
Allow InterBranch formats to raise NoRoundtrippingSupport.
99
        try:
100
            mine.merge_from_branch(other.branch)
101
        except errors.NoRoundtrippingSupport:
102
            raise tests.TestNotApplicable(
103
                'lossless push between %r and %r not supported' %
104
                (self.branch_format_from, self.branch_format_to))
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
105
        p2 = mine.commit('merge other')
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
106
        mine.branch.push(target.branch)
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
107
        self.assertEqual(p2, target.branch.last_revision())
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
108
109
    def test_push_to_checkout_updates_master(self):
110
        """Pushing into a checkout updates the checkout and the master branch"""
111
        master_tree = self.make_to_branch_and_tree('master')
112
        checkout = self.make_to_branch_and_tree('checkout')
113
        try:
114
            checkout.branch.bind(master_tree.branch)
115
        except errors.UpgradeRequired:
116
            # cant bind this format, the test is irrelevant.
117
            return
118
        rev1 = checkout.commit('master')
119
6883.21.1 by Jelmer Vernooij
Allow InterBranch formats to raise NoRoundtrippingSupport.
120
        try:
7143.15.2 by Jelmer Vernooij
Run autopep8.
121
            other_bzrdir = self.sprout_from(
122
                master_tree.branch.controldir, 'other')
6883.21.1 by Jelmer Vernooij
Allow InterBranch formats to raise NoRoundtrippingSupport.
123
        except errors.NoRoundtrippingSupport:
124
            raise tests.TestNotApplicable(
125
                'lossless push between %r and %r not supported' %
126
                (self.branch_format_from, self.branch_format_to))
4211.1.5 by Jelmer Vernooij
Fix copyright year, number of columns used.
127
        other = other_bzrdir.open_workingtree()
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
128
        rev2 = other.commit('other commit')
129
        # now push, which should update both checkout and master.
130
        other.branch.push(checkout.branch)
6165.4.6 by Jelmer Vernooij
Avoid more uses of revision_history.
131
        self.assertEqual(rev2, checkout.branch.last_revision())
132
        self.assertEqual(rev2, master_tree.branch.last_revision())
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
133
134
    def test_push_raises_specific_error_on_master_connection_error(self):
135
        master_tree = self.make_to_branch_and_tree('master')
136
        checkout = self.make_to_branch_and_tree('checkout')
137
        try:
138
            checkout.branch.bind(master_tree.branch)
139
        except errors.UpgradeRequired:
140
            # cant bind this format, the test is irrelevant.
141
            return
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
142
        other_bzrdir = self.sprout_from(master_tree.branch.controldir, 'other')
4211.1.5 by Jelmer Vernooij
Fix copyright year, number of columns used.
143
        other = other_bzrdir.open_workingtree()
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
144
        # move the branch out of the way on disk to cause a connection
145
        # error.
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
146
        master_tree.controldir.destroy_branch()
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
147
        # try to push, which should raise a BoundBranchConnectionFailure.
148
        self.assertRaises(errors.BoundBranchConnectionFailure,
7143.15.2 by Jelmer Vernooij
Run autopep8.
149
                          other.branch.push, checkout.branch)
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
150
151
    def test_push_uses_read_lock(self):
152
        """Push should only need a read lock on the source side."""
153
        source = self.make_from_branch_and_tree('source')
154
        target = self.make_to_branch('target')
155
156
        self.build_tree(['source/a'])
157
        source.add(['a'])
158
        source.commit('a')
159
160
        try:
6883.21.1 by Jelmer Vernooij
Allow InterBranch formats to raise NoRoundtrippingSupport.
161
            with source.branch.lock_read(), target.lock_write():
7143.15.2 by Jelmer Vernooij
Run autopep8.
162
                source.branch.push(
163
                    target, stop_revision=source.last_revision())
6883.21.1 by Jelmer Vernooij
Allow InterBranch formats to raise NoRoundtrippingSupport.
164
        except errors.NoRoundtrippingSupport:
165
            raise tests.TestNotApplicable(
166
                'lossless push between %r and %r not supported' %
167
                (self.branch_format_from, self.branch_format_to))
168
169
    def test_push_uses_read_lock_lossy(self):
170
        """Push should only need a read lock on the source side."""
171
        source = self.make_from_branch_and_tree('source')
172
        target = self.make_to_branch('target')
173
174
        self.build_tree(['source/a'])
175
        source.add(['a'])
176
        source.commit('a')
177
178
        try:
179
            with source.branch.lock_read(), target.lock_write():
7143.15.2 by Jelmer Vernooij
Run autopep8.
180
                source.branch.push(
181
                    target, stop_revision=source.last_revision(), lossy=True)
6883.21.1 by Jelmer Vernooij
Allow InterBranch formats to raise NoRoundtrippingSupport.
182
        except errors.LossyPushToSameVCS:
183
            raise tests.TestNotApplicable(
184
                'push between branches of same format')
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
185
7141.5.1 by Jelmer Vernooij
Fix fetching between non-default git branches.
186
    def test_between_colocated(self):
187
        """Pushing from one colocated branch to another doesn't change the active branch."""
188
        source = self.make_from_branch_and_tree('source')
189
        target = self.make_to_branch('target')
190
191
        self.build_tree(['source/a'])
192
        source.add(['a'])
193
        revid1 = source.commit('a')
194
195
        self.build_tree(['source/b'])
196
        source.add(['b'])
197
        revid2 = source.commit('b')
198
199
        source_colo = source.controldir.create_branch('colo')
200
        source_colo.generate_revision_history(revid1)
201
        try:
202
            source_colo.push(target)
203
        except errors.NoRoundtrippingSupport:
204
            raise tests.TestNotApplicable(
205
                'push between branches of different format')
206
        self.assertEqual(source_colo.last_revision(), revid1)
207
        self.assertEqual(source.last_revision(), revid2)
208
        self.assertEqual(target.last_revision(), revid1)
209
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
210
    def test_push_within_repository(self):
211
        """Push from one branch to another inside the same repository."""
212
        try:
213
            repo = self.make_repository('repo', shared=True)
214
        except (errors.IncompatibleFormat, errors.UninitializableFormat):
215
            # This Branch format cannot create shared repositories
216
            return
5297.2.2 by Robert Collins
Fixup tests in per_interbranch not being strict about making the from format the configured one.
217
        # This is a little bit trickier because make_from_branch_and_tree will not
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
218
        # re-use a shared repository.
219
        try:
220
            a_branch = self.make_from_branch('repo/tree')
221
        except (errors.UninitializableFormat):
222
            # Cannot create these branches
223
            return
224
        try:
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
225
            tree = a_branch.controldir.create_workingtree()
6127.1.9 by Jelmer Vernooij
Add lightweight option to _get_checkout_format().
226
        except errors.UnsupportedOperation:
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
227
            self.assertFalse(a_branch.controldir._format.supports_workingtrees)
6127.1.9 by Jelmer Vernooij
Add lightweight option to _get_checkout_format().
228
            tree = a_branch.create_checkout('repo/tree', lightweight=True)
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
229
        except errors.NotLocalUrl:
5017.3.41 by Vincent Ladeuil
-s bt.per_interbranch passing
230
            if self.vfs_transport_factory is test_server.LocalURLServer:
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
231
                # the branch is colocated on disk, we cannot create a checkout.
232
                # hopefully callers will expect this.
7143.15.2 by Jelmer Vernooij
Run autopep8.
233
                local_controldir = controldir.ControlDir.open(
234
                    self.get_vfs_only_url('repo/tree'))
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
235
                tree = local_controldir.create_workingtree()
236
            else:
237
                tree = a_branch.create_checkout('repo/tree', lightweight=True)
238
        self.build_tree(['repo/tree/a'])
239
        tree.add(['a'])
240
        tree.commit('a')
241
242
        to_branch = self.make_to_branch('repo/branch')
6883.21.1 by Jelmer Vernooij
Allow InterBranch formats to raise NoRoundtrippingSupport.
243
        try:
244
            tree.branch.push(to_branch)
245
        except errors.NoRoundtrippingSupport:
246
            tree.branch.push(to_branch, lossy=True)
247
        else:
248
            self.assertEqual(tree.branch.last_revision(),
249
                             to_branch.last_revision())
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
250
251
    def test_push_overwrite_of_non_tip_with_stop_revision(self):
252
        """Combining the stop_revision and overwrite options works.
253
254
        This was <https://bugs.launchpad.net/bzr/+bug/234229>.
255
        """
256
        source = self.make_from_branch_and_tree('source')
257
        target = self.make_to_branch('target')
258
259
        source.commit('1st commit')
6883.21.1 by Jelmer Vernooij
Allow InterBranch formats to raise NoRoundtrippingSupport.
260
        try:
261
            source.branch.push(target)
262
        except errors.NoRoundtrippingSupport:
263
            raise tests.TestNotApplicable(
264
                'lossless push between %r and %r not supported' %
265
                (self.branch_format_from, self.branch_format_to))
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
266
        rev2 = source.commit('2nd commit')
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
267
        source.commit('3rd commit')
268
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
269
        source.branch.push(target, stop_revision=rev2, overwrite=True)
270
        self.assertEqual(rev2, target.last_revision())
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
271
272
    def test_push_with_default_stacking_does_not_create_broken_branch(self):
273
        """Pushing a new standalone branch works even when there's a default
274
        stacking policy at the destination.
275
276
        The new branch will preserve the repo format (even if it isn't the
277
        default for the branch), and will be stacked when the repo format
278
        allows (which means that the branch format isn't necessarly preserved).
279
        """
6653.1.1 by Jelmer Vernooij
Split bzr branch code out into breezy.bzrbranch.
280
        if isinstance(self.branch_format_from, bzrbranch.BranchReferenceFormat):
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
281
            # This test could in principle apply to BranchReferenceFormat, but
282
            # make_branch_builder doesn't support it.
283
            raise tests.TestSkipped(
284
                "BranchBuilder can't make reference branches.")
285
        # Make a branch called "local" in a stackable repository
286
        # The branch has 3 revisions:
287
        #   - rev-1, adds a file
288
        #   - rev-2, no changes
289
        #   - rev-3, modifies the file.
290
        repo = self.make_repository('repo', shared=True, format='1.6')
6862.2.2 by Jelmer Vernooij
Skip another test if format can't be initialized.
291
        try:
292
            builder = self.make_from_branch_builder('repo/local')
293
        except errors.UninitializableFormat:
294
            raise tests.TestNotApplicable(
295
                'BranchBuilder can not initialize some formats')
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
296
        builder.start_series()
6821.2.2 by Jelmer Vernooij
More foreign fixes.
297
        revid1 = builder.build_snapshot(None, [
6883.22.13 by Jelmer Vernooij
Actually pass in None file-ids to BranchBuilder.
298
            ('add', ('', None, 'directory', '')),
6973.6.2 by Jelmer Vernooij
Fix more tests.
299
            ('add', ('filename', None, 'file', b'content\n'))])
6821.2.2 by Jelmer Vernooij
More foreign fixes.
300
        revid2 = builder.build_snapshot([revid1], [])
301
        revid3 = builder.build_snapshot([revid2],
7143.15.2 by Jelmer Vernooij
Run autopep8.
302
                                        [('modify', ('filename', b'new-content\n'))])
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
303
        builder.finish_series()
304
        trunk = builder.get_branch()
305
        # Sprout rev-1 to "trunk", so that we can stack on it.
6821.2.2 by Jelmer Vernooij
More foreign fixes.
306
        trunk.controldir.sprout(self.get_url('trunk'), revision_id=revid1)
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
307
        # Set a default stacking policy so that new branches will automatically
308
        # stack on trunk.
6653.6.5 by Jelmer Vernooij
Rename make_bzrdir to make_controldir.
309
        self.make_controldir('.').get_config().set_default_stack_on('trunk')
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
310
        # Push rev-2 to a new branch "remote".  It will be stacked on "trunk".
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
311
        output = BytesIO()
6821.2.2 by Jelmer Vernooij
More foreign fixes.
312
        push._show_push_branch(trunk, revid2, self.get_url('remote'), output)
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
313
        # Push rev-3 onto "remote".  If "remote" not stacked and is missing the
314
        # fulltext record for f-id @ rev-1, then this will fail.
315
        remote_branch = Branch.open(self.get_url('remote'))
316
        trunk.push(remote_branch)
4332.3.35 by Robert Collins
Fix failing tests.
317
        check.check_dwim(remote_branch.base, False, True, True)
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
318
319
    def test_no_get_parent_map_after_insert_stream(self):
320
        # Effort test for bug 331823
321
        self.setup_smart_server_with_call_log()
322
        # Make a local branch with four revisions.  Four revisions because:
323
        # one to push, one there for _walk_to_common_revisions to find, one we
324
        # don't want to access, one for luck :)
6653.1.1 by Jelmer Vernooij
Split bzr branch code out into breezy.bzrbranch.
325
        if isinstance(self.branch_format_from, bzrbranch.BranchReferenceFormat):
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
326
            # This test could in principle apply to BranchReferenceFormat, but
327
            # make_branch_builder doesn't support it.
328
            raise tests.TestSkipped(
329
                "BranchBuilder can't make reference branches.")
330
        try:
331
            builder = self.make_from_branch_builder('local')
332
        except (errors.TransportNotPossible, errors.UninitializableFormat):
333
            raise tests.TestNotApplicable('format not directly constructable')
334
        builder.start_series()
6821.2.2 by Jelmer Vernooij
More foreign fixes.
335
        first = builder.build_snapshot(None, [
6883.22.13 by Jelmer Vernooij
Actually pass in None file-ids to BranchBuilder.
336
            ('add', ('', None, 'directory', ''))])
6821.2.2 by Jelmer Vernooij
More foreign fixes.
337
        second = builder.build_snapshot([first], [])
338
        third = builder.build_snapshot([second], [])
339
        fourth = builder.build_snapshot([third], [])
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
340
        builder.finish_series()
341
        local = branch.Branch.open(self.get_vfs_only_url('local'))
342
        # Initial push of three revisions
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
343
        remote_bzrdir = local.controldir.sprout(
6821.2.2 by Jelmer Vernooij
More foreign fixes.
344
            self.get_url('remote'), revision_id=third)
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
345
        remote = remote_bzrdir.open_branch()
6876.7.1 by Jelmer Vernooij
Only check for certain HPSS behaviour for vf repositories.
346
        if not remote.repository._format.supports_full_versioned_files:
347
            raise tests.TestNotApplicable(
348
                'remote is not a VersionedFile repository')
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
349
        # Push fourth revision
350
        self.reset_smart_call_log()
351
        self.disableOptimisticGetParentMap()
352
        self.assertFalse(local.is_locked())
353
        local.push(remote)
354
        hpss_call_names = [item.call.method for item in self.hpss_calls]
7045.4.1 by Jelmer Vernooij
Some brz-git fixes.
355
        self.assertIn(b'Repository.insert_stream_1.19', hpss_call_names)
4476.3.18 by Andrew Bennetts
Update some tests that were expecting the pre-1.17 insert_stream verb.
356
        insert_stream_idx = hpss_call_names.index(
7045.4.1 by Jelmer Vernooij
Some brz-git fixes.
357
            b'Repository.insert_stream_1.19')
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
358
        calls_after_insert_stream = hpss_call_names[insert_stream_idx:]
359
        # After inserting the stream the client has no reason to query the
360
        # remote graph any further.
5317.1.1 by Robert Collins
Make the interbranch test_no_get_parent_map_after_insert_stream test work for looms - a little ugly, will want a generic hook-out to foreign formats in some form, at some point.
361
        bzr_core_trace = Equals(
7045.4.1 by Jelmer Vernooij
Some brz-git fixes.
362
            [b'Repository.insert_stream_1.19', b'Repository.insert_stream_1.19',
363
             b'Branch.set_last_revision_info', b'Branch.unlock'])
5317.1.1 by Robert Collins
Make the interbranch test_no_get_parent_map_after_insert_stream test work for looms - a little ugly, will want a generic hook-out to foreign formats in some form, at some point.
364
        bzr_loom_trace = Equals(
7045.4.1 by Jelmer Vernooij
Some brz-git fixes.
365
            [b'Repository.insert_stream_1.19', b'Repository.insert_stream_1.19',
366
             b'Branch.set_last_revision_info', b'get', b'Branch.unlock'])
5317.1.1 by Robert Collins
Make the interbranch test_no_get_parent_map_after_insert_stream test work for looms - a little ugly, will want a generic hook-out to foreign formats in some form, at some point.
367
        self.assertThat(calls_after_insert_stream,
7143.15.2 by Jelmer Vernooij
Run autopep8.
368
                        MatchesAny(bzr_core_trace, bzr_loom_trace))
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
369
370
    def disableOptimisticGetParentMap(self):
371
        # Tweak some class variables to stop remote get_parent_map calls asking
372
        # for or receiving more data than the caller asked for.
5863.4.2 by Jelmer Vernooij
Fix test.
373
        self.overrideAttr(vf_repository.InterVersionedFileRepository,
4985.1.5 by Vincent Ladeuil
Deploying the new overrideAttr facility further reduces the complexity
374
                          '_walk_to_common_revisions_batch_size', 1)
375
        self.overrideAttr(SmartServerRepositoryGetParentMap,
7143.15.2 by Jelmer Vernooij
Run autopep8.
376
                          'no_extra_results', True)
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
377
378
379
class TestPushHook(TestCaseWithInterBranch):
380
381
    def setUp(self):
382
        self.hook_calls = []
6552.1.4 by Vincent Ladeuil
Remaining tests matching setup(self) that can be rewritten with super().
383
        super(TestPushHook, self).setUp()
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
384
385
    def capture_post_push_hook(self, result):
386
        """Capture post push hook calls to self.hook_calls.
387
388
        The call is logged, as is some state of the two branches.
389
        """
390
        if result.local_branch:
391
            local_locked = result.local_branch.is_locked()
392
            local_base = result.local_branch.base
393
        else:
394
            local_locked = None
395
            local_base = None
396
        self.hook_calls.append(
397
            ('post_push', result.source_branch, local_base,
398
             result.master_branch.base,
399
             result.old_revno, result.old_revid,
400
             result.new_revno, result.new_revid,
401
             result.source_branch.is_locked(), local_locked,
402
             result.master_branch.is_locked()))
403
404
    def test_post_push_empty_history(self):
405
        target = self.make_to_branch('target')
406
        source = self.make_from_branch('source')
407
        Branch.hooks.install_named_hook('post_push',
408
                                        self.capture_post_push_hook, None)
409
        source.push(target)
410
        # with nothing there we should still get a notification, and
411
        # have both branches locked at the notification time.
412
        self.assertEqual([
413
            ('post_push', source, None, target.base, 0, NULL_REVISION,
414
             0, NULL_REVISION, True, None, True)
415
            ],
416
            self.hook_calls)
417
418
    def test_post_push_bound_branch(self):
419
        # pushing to a bound branch should pass in the master branch to the
420
        # hook, allowing the correct number of emails to be sent, while still
421
        # allowing hooks that want to modify the target to do so to both
422
        # instances.
423
        target = self.make_to_branch('target')
424
        local = self.make_from_branch('local')
425
        try:
426
            local.bind(target)
427
        except errors.UpgradeRequired:
428
            # We can't bind this format to itself- typically it is the local
429
            # branch that doesn't support binding.  As of May 2007
430
            # remotebranches can't be bound.  Let's instead make a new local
431
            # branch of the default type, which does allow binding.
432
            # See https://bugs.launchpad.net/bzr/+bug/112020
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
433
            local = ControlDir.create_branch_convenience('local2')
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
434
            local.bind(target)
435
        source = self.make_from_branch('source')
436
        Branch.hooks.install_named_hook('post_push',
437
                                        self.capture_post_push_hook, None)
438
        source.push(local)
439
        # with nothing there we should still get a notification, and
440
        # have both branches locked at the notification time.
441
        self.assertEqual([
442
            ('post_push', source, local.base, target.base, 0, NULL_REVISION,
443
             0, NULL_REVISION, True, True, True)
444
            ],
445
            self.hook_calls)
446
447
    def test_post_push_nonempty_history(self):
448
        target = self.make_to_branch_and_tree('target')
449
        target.lock_write()
450
        target.add('')
451
        rev1 = target.commit('rev 1')
452
        target.unlock()
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
453
        sourcedir = target.branch.controldir.clone(self.get_url('source'))
4211.1.4 by Jelmer Vernooij
add InterBranch.push() tests.
454
        source = MemoryTree.create_on_branch(sourcedir.open_branch())
455
        rev2 = source.commit('rev 2')
456
        Branch.hooks.install_named_hook('post_push',
457
                                        self.capture_post_push_hook, None)
458
        source.branch.push(target.branch)
459
        # with nothing there we should still get a notification, and
460
        # have both branches locked at the notification time.
461
        self.assertEqual([
462
            ('post_push', source.branch, None, target.branch.base, 1, rev1,
463
             2, rev2, True, None, True)
464
            ],
465
            self.hook_calls)