/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5609.26.1 by John Arbash Meinel
Fix bug #465517, 'bzr push' to a target with a repo but no branch
1
# Copyright (C) 2007-2011 Canonical Ltd
2245.2.1 by Robert Collins
Split branch pushing out of branch pulling.
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.2.1 by Robert Collins
Split branch pushing out of branch pulling.
16
17
"""Tests for branch.push behaviour."""
18
7479.2.1 by Jelmer Vernooij
Drop python2 support.
19
from io import BytesIO
2245.2.1 by Robert Collins
Split branch pushing out of branch pulling.
20
import os
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
21
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
22
from ... import (
3703.3.7 by Andrew Bennetts
Move empty push effort tests to branch_implementations.
23
    branch,
24
    builtins,
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
25
    controldir,
4332.3.35 by Robert Collins
Fix failing tests.
26
    check,
3703.3.7 by Andrew Bennetts
Move empty push effort tests to branch_implementations.
27
    errors,
5010.2.18 by Vincent Ladeuil
Fix imports in per_branch/test_push.py.
28
    memorytree,
3904.3.5 by Andrew Bennetts
Improve the test; now 4/7 passing.
29
    push,
5010.2.18 by Vincent Ladeuil
Fix imports in per_branch/test_push.py.
30
    revision,
3703.3.7 by Andrew Bennetts
Move empty push effort tests to branch_implementations.
31
    tests,
5010.2.18 by Vincent Ladeuil
Fix imports in per_branch/test_push.py.
32
    transport,
33
    )
6670.4.1 by Jelmer Vernooij
Update imports.
34
from ...bzr import (
35
    branch as bzrbranch,
36
    )
6670.4.16 by Jelmer Vernooij
Move smart to breezy.bzr.
37
from ...bzr.smart import (
5010.2.18 by Vincent Ladeuil
Fix imports in per_branch/test_push.py.
38
    client,
39
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
40
from .. import (
5017.3.29 by Vincent Ladeuil
-s bt.per_branch.test_push passing
41
    per_branch,
42
    test_server,
43
    )
5010.2.18 by Vincent Ladeuil
Fix imports in per_branch/test_push.py.
44
45
46
class TestPush(per_branch.TestCaseWithBranch):
2245.2.1 by Robert Collins
Split branch pushing out of branch pulling.
47
48
    def test_push_convergence_simple(self):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
49
        # when revisions are pushed, the left-most accessible parents must
2245.2.1 by Robert Collins
Split branch pushing out of branch pulling.
50
        # become the revision-history.
51
        mine = self.make_branch_and_tree('mine')
6747.2.1 by Jelmer Vernooij
Avoid setting revision_ids.
52
        mine.commit('1st post', allow_pointless=True)
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
53
        other = mine.controldir.sprout('other').open_workingtree()
6747.2.1 by Jelmer Vernooij
Avoid setting revision_ids.
54
        m1 = other.commit('my change', allow_pointless=True)
2245.2.1 by Robert Collins
Split branch pushing out of branch pulling.
55
        mine.merge_from_branch(other.branch)
6747.2.1 by Jelmer Vernooij
Avoid setting revision_ids.
56
        p2 = mine.commit('merge my change')
2297.1.4 by Martin Pool
Push now returns a PushResult rather than just an integer.
57
        result = mine.branch.push(other.branch)
6747.2.2 by Jelmer Vernooij
Fix tests.
58
        self.assertEqual(p2, other.branch.last_revision())
2297.1.4 by Martin Pool
Push now returns a PushResult rather than just an integer.
59
        # result object contains some structured data
6747.2.1 by Jelmer Vernooij
Avoid setting revision_ids.
60
        self.assertEqual(result.old_revid, m1)
61
        self.assertEqual(result.new_revid, p2)
2245.2.1 by Robert Collins
Split branch pushing out of branch pulling.
62
63
    def test_push_merged_indirect(self):
64
        # it should be possible to do a push from one branch into another
65
        # when the tip of the target was merged into the source branch
66
        # via a third branch - so its buried in the ancestry and is not
67
        # directly accessible.
68
        mine = self.make_branch_and_tree('mine')
6747.2.1 by Jelmer Vernooij
Avoid setting revision_ids.
69
        mine.commit('1st post', allow_pointless=True)
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
70
        target = mine.controldir.sprout('target').open_workingtree()
6747.2.1 by Jelmer Vernooij
Avoid setting revision_ids.
71
        target.commit('my change', allow_pointless=True)
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
72
        other = mine.controldir.sprout('other').open_workingtree()
2245.2.1 by Robert Collins
Split branch pushing out of branch pulling.
73
        other.merge_from_branch(target.branch)
6747.2.1 by Jelmer Vernooij
Avoid setting revision_ids.
74
        other.commit('merge my change')
2245.2.1 by Robert Collins
Split branch pushing out of branch pulling.
75
        mine.merge_from_branch(other.branch)
6747.2.1 by Jelmer Vernooij
Avoid setting revision_ids.
76
        p2 = mine.commit('merge other')
2245.2.1 by Robert Collins
Split branch pushing out of branch pulling.
77
        mine.branch.push(target.branch)
6747.2.1 by Jelmer Vernooij
Avoid setting revision_ids.
78
        self.assertEqual(p2, target.branch.last_revision())
2245.2.1 by Robert Collins
Split branch pushing out of branch pulling.
79
80
    def test_push_to_checkout_updates_master(self):
81
        """Pushing into a checkout updates the checkout and the master branch"""
82
        master_tree = self.make_branch_and_tree('master')
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
83
        checkout = self.make_branch_and_tree('checkout')
84
        try:
85
            checkout.branch.bind(master_tree.branch)
86
        except errors.UpgradeRequired:
87
            # cant bind this format, the test is irrelevant.
88
            return
89
        rev1 = checkout.commit('master')
2245.2.1 by Robert Collins
Split branch pushing out of branch pulling.
90
7143.15.2 by Jelmer Vernooij
Run autopep8.
91
        other = master_tree.branch.controldir.sprout(
92
            'other').open_workingtree()
2245.2.1 by Robert Collins
Split branch pushing out of branch pulling.
93
        rev2 = other.commit('other commit')
94
        # now push, which should update both checkout and master.
95
        other.branch.push(checkout.branch)
6165.4.4 by Jelmer Vernooij
Avoid .revision_history().
96
        self.assertEqual(rev2, checkout.branch.last_revision())
97
        self.assertEqual(rev2, master_tree.branch.last_revision())
2245.2.1 by Robert Collins
Split branch pushing out of branch pulling.
98
99
    def test_push_raises_specific_error_on_master_connection_error(self):
100
        master_tree = self.make_branch_and_tree('master')
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
101
        checkout = self.make_branch_and_tree('checkout')
102
        try:
103
            checkout.branch.bind(master_tree.branch)
104
        except errors.UpgradeRequired:
105
            # cant bind this format, the test is irrelevant.
106
            return
7143.15.2 by Jelmer Vernooij
Run autopep8.
107
        other = master_tree.branch.controldir.sprout(
108
            'other').open_workingtree()
2245.2.1 by Robert Collins
Split branch pushing out of branch pulling.
109
        # move the branch out of the way on disk to cause a connection
110
        # error.
111
        os.rename('master', 'master_gone')
112
        # try to push, which should raise a BoundBranchConnectionFailure.
113
        self.assertRaises(errors.BoundBranchConnectionFailure,
7143.15.2 by Jelmer Vernooij
Run autopep8.
114
                          other.branch.push, checkout.branch)
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
115
5609.25.1 by Andrew Bennetts
Add failing test for bug #733350.
116
    def test_push_new_tag_to_bound_branch(self):
117
        master = self.make_branch('master')
118
        bound = self.make_branch('bound')
119
        try:
120
            bound.bind(master)
121
        except errors.UpgradeRequired:
122
            raise tests.TestNotApplicable(
123
                'Format does not support bound branches')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
124
        other = bound.controldir.sprout('other').open_branch()
5609.25.1 by Andrew Bennetts
Add failing test for bug #733350.
125
        try:
6973.9.1 by Jelmer Vernooij
More test fixes.
126
            other.tags.set_tag('new-tag', b'some-rev')
5609.25.1 by Andrew Bennetts
Add failing test for bug #733350.
127
        except errors.TagsNotSupported:
128
            raise tests.TestNotApplicable('Format does not support tags')
129
        other.push(bound)
6973.9.1 by Jelmer Vernooij
More test fixes.
130
        self.assertEqual({'new-tag': b'some-rev'}, bound.tags.get_tag_dict())
131
        self.assertEqual({'new-tag': b'some-rev'}, master.tags.get_tag_dict())
5609.25.1 by Andrew Bennetts
Add failing test for bug #733350.
132
2279.1.1 by John Arbash Meinel
Branch.push() only needs a read lock.
133
    def test_push_uses_read_lock(self):
134
        """Push should only need a read lock on the source side."""
135
        source = self.make_branch_and_tree('source')
136
        target = self.make_branch('target')
137
2381.1.3 by Robert Collins
Review feedback.
138
        self.build_tree(['source/a'])
2279.1.1 by John Arbash Meinel
Branch.push() only needs a read lock.
139
        source.add(['a'])
140
        source.commit('a')
141
7356.1.5 by Jelmer Vernooij
Use more ExitStacks.
142
        with source.branch.lock_read(), target.lock_write():
143
            source.branch.push(
144
                target, stop_revision=source.last_revision())
2279.1.1 by John Arbash Meinel
Branch.push() only needs a read lock.
145
2279.1.3 by John Arbash Meinel
Switch the test to being a branch_implementation test.
146
    def test_push_within_repository(self):
147
        """Push from one branch to another inside the same repository."""
148
        try:
149
            repo = self.make_repository('repo', shared=True)
150
        except (errors.IncompatibleFormat, errors.UninitializableFormat):
151
            # This Branch format cannot create shared repositories
152
            return
6145.2.1 by Jelmer Vernooij
Add RepositoryFormat.supports_nesting_repositories.
153
        if not repo._format.supports_nesting_repositories:
154
            return
2279.1.3 by John Arbash Meinel
Switch the test to being a branch_implementation test.
155
        # This is a little bit trickier because make_branch_and_tree will not
156
        # re-use a shared repository.
6653.6.5 by Jelmer Vernooij
Rename make_bzrdir to make_controldir.
157
        a_controldir = self.make_controldir('repo/tree')
2279.1.3 by John Arbash Meinel
Switch the test to being a branch_implementation test.
158
        try:
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
159
            a_branch = self.branch_format.initialize(a_controldir)
2279.1.3 by John Arbash Meinel
Switch the test to being a branch_implementation test.
160
        except (errors.UninitializableFormat):
161
            # Cannot create these branches
162
            return
2018.5.97 by Andrew Bennetts
Fix more tests.
163
        try:
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
164
            tree = a_branch.controldir.create_workingtree()
2018.5.97 by Andrew Bennetts
Fix more tests.
165
        except errors.NotLocalUrl:
5017.3.29 by Vincent Ladeuil
-s bt.per_branch.test_push passing
166
            if self.vfs_transport_factory is test_server.LocalURLServer:
2018.5.130 by Robert Collins
Make all branch_implementations tests pass.
167
                # the branch is colocated on disk, we cannot create a checkout.
168
                # hopefully callers will expect this.
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
169
                local_controldir = controldir.ControlDir.open(
5010.2.18 by Vincent Ladeuil
Fix imports in per_branch/test_push.py.
170
                    self.get_vfs_only_url('repo/tree'))
2018.5.130 by Robert Collins
Make all branch_implementations tests pass.
171
                tree = local_controldir.create_workingtree()
172
            else:
173
                tree = a_branch.create_checkout('repo/tree', lightweight=True)
2381.1.3 by Robert Collins
Review feedback.
174
        self.build_tree(['repo/tree/a'])
2279.1.3 by John Arbash Meinel
Switch the test to being a branch_implementation test.
175
        tree.add(['a'])
176
        tree.commit('a')
177
6653.6.5 by Jelmer Vernooij
Rename make_bzrdir to make_controldir.
178
        to_bzrdir = self.make_controldir('repo/branch')
2279.1.3 by John Arbash Meinel
Switch the test to being a branch_implementation test.
179
        to_branch = self.branch_format.initialize(to_bzrdir)
180
        tree.branch.push(to_branch)
181
182
        self.assertEqual(tree.branch.last_revision(),
183
                         to_branch.last_revision())
184
5611.2.1 by Jelmer Vernooij
Fix 'bzr push --overwrite -rOLD_MAINLINE_REV'.
185
    def test_push_overwrite_with_older_mainline_rev(self):
186
        """Pushing an older mainline revision with overwrite.
187
188
        This was <https://bugs.launchpad.net/bzr/+bug/386576>.
189
        """
190
        source = self.make_branch_and_tree('source')
191
        target = self.make_branch('target')
192
193
        source.commit('1st commit')
6747.2.1 by Jelmer Vernooij
Avoid setting revision_ids.
194
        rev2 = source.commit('2nd commit')
5611.2.1 by Jelmer Vernooij
Fix 'bzr push --overwrite -rOLD_MAINLINE_REV'.
195
        source.commit('3rd commit')
196
        source.branch.push(target)
6747.2.1 by Jelmer Vernooij
Avoid setting revision_ids.
197
        source.branch.push(target, stop_revision=rev2, overwrite=True)
198
        self.assertEqual(rev2, target.last_revision())
5611.2.1 by Jelmer Vernooij
Fix 'bzr push --overwrite -rOLD_MAINLINE_REV'.
199
3449.1.2 by Andrew Bennetts
Add test and NEWS entry.
200
    def test_push_overwrite_of_non_tip_with_stop_revision(self):
201
        """Combining the stop_revision and overwrite options works.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
202
3449.1.2 by Andrew Bennetts
Add test and NEWS entry.
203
        This was <https://bugs.launchpad.net/bzr/+bug/234229>.
204
        """
205
        source = self.make_branch_and_tree('source')
206
        target = self.make_branch('target')
207
208
        source.commit('1st commit')
209
        source.branch.push(target)
6747.2.1 by Jelmer Vernooij
Avoid setting revision_ids.
210
        rev2 = source.commit('2nd commit')
3449.1.2 by Andrew Bennetts
Add test and NEWS entry.
211
        source.commit('3rd commit')
212
6747.2.1 by Jelmer Vernooij
Avoid setting revision_ids.
213
        source.branch.push(target, stop_revision=rev2, overwrite=True)
214
        self.assertEqual(rev2, target.last_revision())
3449.1.2 by Andrew Bennetts
Add test and NEWS entry.
215
5609.26.1 by John Arbash Meinel
Fix bug #465517, 'bzr push' to a target with a repo but no branch
216
    def test_push_repository_no_branch_doesnt_fetch_all_revs(self):
217
        # See https://bugs.launchpad.net/bzr/+bug/465517
218
        t = self.get_transport('target')
219
        t.ensure_base()
6862.2.1 by Jelmer Vernooij
Allow bzrdir format to be uninitializeable.
220
        try:
221
            bzrdir = self.bzrdir_format.initialize_on_transport(t)
222
        except errors.UninitializableFormat:
223
            raise tests.TestNotApplicable('cannot initialize this format')
5609.26.1 by John Arbash Meinel
Fix bug #465517, 'bzr push' to a target with a repo but no branch
224
        try:
225
            bzrdir.open_branch()
226
        except errors.NotBranchError:
227
            pass
228
        else:
6862.2.1 by Jelmer Vernooij
Allow bzrdir format to be uninitializeable.
229
            raise tests.TestNotApplicable('some formats can\'t have a repo'
5609.26.1 by John Arbash Meinel
Fix bug #465517, 'bzr push' to a target with a repo but no branch
230
                                          ' without a branch')
231
        try:
232
            source = self.make_branch_builder('source',
233
                                              format=self.bzrdir_format)
234
        except errors.UninitializableFormat:
235
            raise tests.TestNotApplicable('cannot initialize this format')
236
        source.start_series()
6829.2.1 by Jelmer Vernooij
Avoid passing in file_ids/revision_ids in a few more places.
237
        revid_a = source.build_snapshot(None, [
6963.2.4 by Jelmer Vernooij
Add bees.
238
            ('add', ('', b'root-id', 'directory', None))])
6829.2.1 by Jelmer Vernooij
Avoid passing in file_ids/revision_ids in a few more places.
239
        revid_b = source.build_snapshot([revid_a], [])
240
        revid_c = source.build_snapshot([revid_a], [])
5609.26.1 by John Arbash Meinel
Fix bug #465517, 'bzr push' to a target with a repo but no branch
241
        source.finish_series()
242
        b = source.get_branch()
243
        # Note: We can't read lock the source branch. Some formats take a write
244
        # lock to 'set_push_location', which breaks
245
        self.addCleanup(b.lock_write().unlock)
246
        repo = bzrdir.create_repository()
247
        # This means 'push the source branch into this dir'
248
        bzrdir.push_branch(b)
249
        self.addCleanup(repo.lock_read().unlock)
6829.2.1 by Jelmer Vernooij
Avoid passing in file_ids/revision_ids in a few more places.
250
        # We should have pushed revid_c, but not revid_b, since it isn't in the
5609.26.1 by John Arbash Meinel
Fix bug #465517, 'bzr push' to a target with a repo but no branch
251
        # ancestry
6831 by Jelmer Vernooij
Merge lp:~jelmer/brz/misc-foreign3.
252
        self.assertEqual(set([revid_a, revid_c]), set(repo.all_revision_ids()))
5609.26.1 by John Arbash Meinel
Fix bug #465517, 'bzr push' to a target with a repo but no branch
253
3904.3.5 by Andrew Bennetts
Improve the test; now 4/7 passing.
254
    def test_push_with_default_stacking_does_not_create_broken_branch(self):
3904.3.7 by Andrew Bennetts
Comment the new tests.
255
        """Pushing a new standalone branch works even when there's a default
256
        stacking policy at the destination.
257
258
        The new branch will preserve the repo format (even if it isn't the
259
        default for the branch), and will be stacked when the repo format
260
        allows (which means that the branch format isn't necessarly preserved).
261
        """
5673.1.3 by Jelmer Vernooij
Change flexible_components to fixed_components.
262
        if self.bzrdir_format.fixed_components:
3904.3.6 by Andrew Bennetts
Skip test for two formats, and fix format 5 by avoiding a full history sync with non-format5 branches.
263
            raise tests.TestNotApplicable('Not a metadir format.')
6653.1.2 by Jelmer Vernooij
Fix imports.
264
        if isinstance(self.branch_format, bzrbranch.BranchReferenceFormat):
3904.3.7 by Andrew Bennetts
Comment the new tests.
265
            # This test could in principle apply to BranchReferenceFormat, but
266
            # make_branch_builder doesn't support it.
3904.3.6 by Andrew Bennetts
Skip test for two formats, and fix format 5 by avoiding a full history sync with non-format5 branches.
267
            raise tests.TestSkipped(
268
                "BranchBuilder can't make reference branches.")
3904.3.7 by Andrew Bennetts
Comment the new tests.
269
        # Make a branch called "local" in a stackable repository
270
        # The branch has 3 revisions:
271
        #   - rev-1, adds a file
272
        #   - rev-2, no changes
273
        #   - rev-3, modifies the file.
3904.3.5 by Andrew Bennetts
Improve the test; now 4/7 passing.
274
        repo = self.make_repository('repo', shared=True, format='1.6')
275
        builder = self.make_branch_builder('repo/local')
3904.3.4 by Andrew Bennetts
First cut of a branch_implementations test. It fails.
276
        builder.start_series()
6829.2.1 by Jelmer Vernooij
Avoid passing in file_ids/revision_ids in a few more places.
277
        revid1 = builder.build_snapshot(None, [
6963.2.4 by Jelmer Vernooij
Add bees.
278
            ('add', ('', b'root-id', 'directory', b'')),
279
            ('add', ('filename', b'f-id', 'file', b'content\n'))])
6829.2.1 by Jelmer Vernooij
Avoid passing in file_ids/revision_ids in a few more places.
280
        revid2 = builder.build_snapshot([revid1], [])
281
        revid3 = builder.build_snapshot([revid2],
7143.15.2 by Jelmer Vernooij
Run autopep8.
282
                                        [('modify', ('filename', b'new-content\n'))])
3904.3.4 by Andrew Bennetts
First cut of a branch_implementations test. It fails.
283
        builder.finish_series()
3904.3.6 by Andrew Bennetts
Skip test for two formats, and fix format 5 by avoiding a full history sync with non-format5 branches.
284
        trunk = builder.get_branch()
3904.3.7 by Andrew Bennetts
Comment the new tests.
285
        # Sprout rev-1 to "trunk", so that we can stack on it.
6829.2.1 by Jelmer Vernooij
Avoid passing in file_ids/revision_ids in a few more places.
286
        trunk.controldir.sprout(self.get_url('trunk'), revision_id=revid1)
3904.3.7 by Andrew Bennetts
Comment the new tests.
287
        # Set a default stacking policy so that new branches will automatically
288
        # stack on trunk.
6653.6.5 by Jelmer Vernooij
Rename make_bzrdir to make_controldir.
289
        self.make_controldir('.').get_config().set_default_stack_on('trunk')
3904.3.7 by Andrew Bennetts
Comment the new tests.
290
        # 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
291
        output = BytesIO()
6829.2.1 by Jelmer Vernooij
Avoid passing in file_ids/revision_ids in a few more places.
292
        push._show_push_branch(trunk, revid2, self.get_url('remote'), output)
3904.3.7 by Andrew Bennetts
Comment the new tests.
293
        # Push rev-3 onto "remote".  If "remote" not stacked and is missing the
294
        # fulltext record for f-id @ rev-1, then this will fail.
5010.2.18 by Vincent Ladeuil
Fix imports in per_branch/test_push.py.
295
        remote_branch = branch.Branch.open(self.get_url('remote'))
3904.3.6 by Andrew Bennetts
Skip test for two formats, and fix format 5 by avoiding a full history sync with non-format5 branches.
296
        trunk.push(remote_branch)
4332.3.35 by Robert Collins
Fix failing tests.
297
        check.check_dwim(remote_branch.base, False, True, True)
3904.3.4 by Andrew Bennetts
First cut of a branch_implementations test. It fails.
298
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
299
5010.2.18 by Vincent Ladeuil
Fix imports in per_branch/test_push.py.
300
class TestPushHook(per_branch.TestCaseWithBranch):
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
301
302
    def setUp(self):
303
        self.hook_calls = []
5010.2.18 by Vincent Ladeuil
Fix imports in per_branch/test_push.py.
304
        super(TestPushHook, self).setUp()
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
305
2297.1.4 by Martin Pool
Push now returns a PushResult rather than just an integer.
306
    def capture_post_push_hook(self, result):
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
307
        """Capture post push hook calls to self.hook_calls.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
308
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
309
        The call is logged, as is some state of the two branches.
310
        """
2297.1.6 by Martin Pool
Add docs for Results, give some members cleaner names
311
        if result.local_branch:
312
            local_locked = result.local_branch.is_locked()
313
            local_base = result.local_branch.base
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
314
        else:
315
            local_locked = None
316
            local_base = None
317
        self.hook_calls.append(
2297.1.6 by Martin Pool
Add docs for Results, give some members cleaner names
318
            ('post_push', result.source_branch, local_base,
319
             result.master_branch.base,
2297.1.4 by Martin Pool
Push now returns a PushResult rather than just an integer.
320
             result.old_revno, result.old_revid,
2297.1.6 by Martin Pool
Add docs for Results, give some members cleaner names
321
             result.new_revno, result.new_revid,
322
             result.source_branch.is_locked(), local_locked,
323
             result.master_branch.is_locked()))
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
324
325
    def test_post_push_empty_history(self):
326
        target = self.make_branch('target')
327
        source = self.make_branch('source')
5010.2.18 by Vincent Ladeuil
Fix imports in per_branch/test_push.py.
328
        branch.Branch.hooks.install_named_hook(
329
            'post_push', self.capture_post_push_hook, None)
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
330
        source.push(target)
331
        # with nothing there we should still get a notification, and
332
        # have both branches locked at the notification time.
333
        self.assertEqual([
5010.2.18 by Vincent Ladeuil
Fix imports in per_branch/test_push.py.
334
            ('post_push', source, None, target.base, 0, revision.NULL_REVISION,
335
             0, revision.NULL_REVISION, True, None, True)
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
336
            ],
337
            self.hook_calls)
338
339
    def test_post_push_bound_branch(self):
340
        # pushing to a bound branch should pass in the master branch to the
341
        # hook, allowing the correct number of emails to be sent, while still
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
342
        # allowing hooks that want to modify the target to do so to both
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
343
        # instances.
344
        target = self.make_branch('target')
345
        local = self.make_branch('local')
346
        try:
347
            local.bind(target)
348
        except errors.UpgradeRequired:
2477.1.2 by Martin Pool
Rename push/pull back to 'run_hooks' (jameinel)
349
            # We can't bind this format to itself- typically it is the local
350
            # branch that doesn't support binding.  As of May 2007
351
            # remotebranches can't be bound.  Let's instead make a new local
352
            # branch of the default type, which does allow binding.
353
            # See https://bugs.launchpad.net/bzr/+bug/112020
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
354
            local = controldir.ControlDir.create_branch_convenience('local2')
2477.1.9 by Martin Pool
Review cleanups from John, mostly docs
355
            local.bind(target)
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
356
        source = self.make_branch('source')
5010.2.18 by Vincent Ladeuil
Fix imports in per_branch/test_push.py.
357
        branch.Branch.hooks.install_named_hook(
358
            'post_push', self.capture_post_push_hook, None)
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
359
        source.push(local)
360
        # with nothing there we should still get a notification, and
361
        # have both branches locked at the notification time.
362
        self.assertEqual([
5010.2.18 by Vincent Ladeuil
Fix imports in per_branch/test_push.py.
363
            ('post_push', source, local.base, target.base, 0,
364
             revision.NULL_REVISION, 0, revision.NULL_REVISION,
365
             True, True, True)
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
366
            ],
367
            self.hook_calls)
368
369
    def test_post_push_nonempty_history(self):
370
        target = self.make_branch_and_memory_tree('target')
371
        target.lock_write()
372
        target.add('')
373
        rev1 = target.commit('rev 1')
374
        target.unlock()
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
375
        sourcedir = target.controldir.clone(self.get_url('source'))
7143.15.2 by Jelmer Vernooij
Run autopep8.
376
        source = memorytree.MemoryTree.create_on_branch(
377
            sourcedir.open_branch())
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
378
        rev2 = source.commit('rev 2')
5010.2.18 by Vincent Ladeuil
Fix imports in per_branch/test_push.py.
379
        branch.Branch.hooks.install_named_hook(
380
            'post_push', self.capture_post_push_hook, None)
2246.1.3 by Robert Collins
New branch hooks: post_push, post_pull, post_commit, post_uncommit. These
381
        source.branch.push(target.branch)
382
        # with nothing there we should still get a notification, and
383
        # have both branches locked at the notification time.
384
        self.assertEqual([
385
            ('post_push', source.branch, None, target.branch.base, 1, rev1,
386
             2, rev2, True, None, True)
387
            ],
388
            self.hook_calls)
3703.3.7 by Andrew Bennetts
Move empty push effort tests to branch_implementations.
389
390
5010.2.18 by Vincent Ladeuil
Fix imports in per_branch/test_push.py.
391
class EmptyPushSmartEffortTests(per_branch.TestCaseWithBranch):
3703.3.7 by Andrew Bennetts
Move empty push effort tests to branch_implementations.
392
    """Tests that a push of 0 revisions should make a limited number of smart
393
    protocol RPCs.
394
    """
395
396
    def setUp(self):
397
        # Skip some scenarios that don't apply to these tests.
7143.15.2 by Jelmer Vernooij
Run autopep8.
398
        if (self.transport_server is not None and
399
            issubclass(self.transport_server,
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
400
                       test_server.SmartTCPServer_for_testing)):
3703.3.7 by Andrew Bennetts
Move empty push effort tests to branch_implementations.
401
            raise tests.TestNotApplicable(
402
                'Does not apply when remote backing branch is also '
403
                'a smart branch')
5674.1.1 by Jelmer Vernooij
Add supports_leave_lock flag to BranchFormat and RepositoryFormat.
404
        if not self.branch_format.supports_leaving_lock():
3703.3.7 by Andrew Bennetts
Move empty push effort tests to branch_implementations.
405
            raise tests.TestNotApplicable(
5674.1.1 by Jelmer Vernooij
Add supports_leave_lock flag to BranchFormat and RepositoryFormat.
406
                'Branch format is not usable via HPSS.')
3703.3.7 by Andrew Bennetts
Move empty push effort tests to branch_implementations.
407
        super(EmptyPushSmartEffortTests, self).setUp()
408
        # Create a smart server that publishes whatever the backing VFS server
409
        # does.
5017.3.29 by Vincent Ladeuil
-s bt.per_branch.test_push passing
410
        self.smart_server = test_server.SmartTCPServer_for_testing()
4659.1.2 by Robert Collins
Refactor creation and shutdown of test servers to use a common helper,
411
        self.start_server(self.smart_server, self.get_server())
3703.3.7 by Andrew Bennetts
Move empty push effort tests to branch_implementations.
412
        # Make two empty branches, 'empty' and 'target'.
413
        self.empty_branch = self.make_branch('empty')
414
        self.make_branch('target')
415
        # Log all HPSS calls into self.hpss_calls.
416
        client._SmartClient.hooks.install_named_hook(
417
            'call', self.capture_hpss_call, None)
418
        self.hpss_calls = []
419
420
    def capture_hpss_call(self, params):
421
        self.hpss_calls.append(params.method)
422
423
    def test_empty_branch_api(self):
424
        """The branch_obj.push API should make a limited number of HPSS calls.
425
        """
7143.15.2 by Jelmer Vernooij
Run autopep8.
426
        t = transport.get_transport_from_url(
427
            self.smart_server.get_url()).clone('target')
5010.2.18 by Vincent Ladeuil
Fix imports in per_branch/test_push.py.
428
        target = branch.Branch.open_from_transport(t)
3703.3.7 by Andrew Bennetts
Move empty push effort tests to branch_implementations.
429
        self.empty_branch.push(target)
430
        self.assertEqual(
6963.2.4 by Jelmer Vernooij
Add bees.
431
            [b'BzrDir.open_2.1',
432
             b'BzrDir.open_branchV3',
433
             b'BzrDir.find_repositoryV3',
434
             b'Branch.get_stacked_on_url',
435
             b'Branch.lock_write',
436
             b'Branch.last_revision_info',
437
             b'Branch.unlock'],
3703.3.7 by Andrew Bennetts
Move empty push effort tests to branch_implementations.
438
            self.hpss_calls)
439
440
    def test_empty_branch_command(self):
441
        """The 'bzr push' command should make a limited number of HPSS calls.
442
        """
443
        cmd = builtins.cmd_push()
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
444
        cmd.outf = BytesIO()
3703.3.7 by Andrew Bennetts
Move empty push effort tests to branch_implementations.
445
        cmd.run(
4420.1.2 by Vincent Ladeuil
Fix bug #284038 by adding a --strict option to push.
446
            directory=self.get_url('empty'),
4420.1.4 by Vincent Ladeuil
Cleanup.
447
            location=self.smart_server.get_url() + 'target')
3703.3.7 by Andrew Bennetts
Move empty push effort tests to branch_implementations.
448
        # HPSS calls as of 2008/09/22:
449
        # [BzrDir.open, BzrDir.open_branch, BzrDir.find_repositoryV2,
450
        # Branch.get_stacked_on_url, get, get, Branch.lock_write,
451
        # Branch.last_revision_info, Branch.unlock]
452
        self.assertTrue(len(self.hpss_calls) <= 9, self.hpss_calls)
453
454
5010.2.18 by Vincent Ladeuil
Fix imports in per_branch/test_push.py.
455
class TestLossyPush(per_branch.TestCaseWithBranch):
4347.3.2 by Jelmer Vernooij
Add some basic tests for lossy_push.
456
457
    def setUp(self):
458
        self.hook_calls = []
5010.2.18 by Vincent Ladeuil
Fix imports in per_branch/test_push.py.
459
        super(TestLossyPush, self).setUp()
4347.3.2 by Jelmer Vernooij
Add some basic tests for lossy_push.
460
461
    def test_lossy_push_raises_same_vcs(self):
462
        target = self.make_branch('target')
463
        source = self.make_branch('source')
7143.15.2 by Jelmer Vernooij
Run autopep8.
464
        self.assertRaises(errors.LossyPushToSameVCS,
465
                          source.push, target, lossy=True)