/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1
# Copyright (C) 2006-2012, 2016 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
1614.2.9 by Olaf Conradi
Added testcases for using push with --remember. Moved remember code to
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
1614.2.9 by Olaf Conradi
Added testcases for using push with --remember. Moved remember code to
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
1614.2.9 by Olaf Conradi
Added testcases for using push with --remember. Moved remember code to
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
1614.2.9 by Olaf Conradi
Added testcases for using push with --remember. Moved remember code to
16
17
6622.1.29 by Jelmer Vernooij
Fix some more tests.
18
"""Black-box tests for brz push."""
1614.2.9 by Olaf Conradi
Added testcases for using push with --remember. Moved remember code to
19
3066.3.2 by jml at canonical
Add tests to check the handling of TooManyRedirections.
20
import re
1614.2.9 by Olaf Conradi
Added testcases for using push with --remember. Moved remember code to
21
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
22
from breezy import (
4420.1.1 by Vincent Ladeuil
Cleanup imports.
23
    branch,
6207.3.3 by jelmer at samba
Fix tests and the like.
24
    controldir,
7290.8.1 by Jelmer Vernooij
Add test for derefencing directories in --stacked-on.
25
    directory_service,
2227.3.1 by John Arbash Meinel
Allow push to create Branch when necessary, and add --use-existing
26
    errors,
4420.1.1 by Vincent Ladeuil
Cleanup imports.
27
    osutils,
28
    tests,
3878.4.4 by Vincent Ladeuil
Cleanup.
29
    transport,
4420.1.1 by Vincent Ladeuil
Cleanup imports.
30
    uncommit,
2220.2.9 by Martin Pool
Add specific tests for push -d and pull -d
31
    urlutils,
4420.1.1 by Vincent Ladeuil
Cleanup imports.
32
    workingtree
2227.3.1 by John Arbash Meinel
Allow push to create Branch when necessary, and add --use-existing
33
    )
6670.4.3 by Jelmer Vernooij
Fix more imports.
34
from breezy.bzr import (
35
    bzrdir,
36
    )
6670.4.5 by Jelmer Vernooij
Move breezy.repofmt contents to breezy.bzr.
37
from breezy.bzr import knitrepo
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
38
from breezy.tests import (
4925.1.1 by Jelmer Vernooij
Print a proper error when attempting to push to a foreign VCS for which
39
    http_server,
5559.2.2 by Martin Pool
Change to using standard load_tests_apply_scenarios.
40
    scenarios,
6175.2.4 by Vincent Ladeuil
Add script tests and documentation.
41
    script,
4925.1.1 by Jelmer Vernooij
Print a proper error when attempting to push to a foreign VCS for which
42
    test_foreign,
43
    )
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
44
from breezy.tests.matchers import ContainsNoVfsCalls
45
from breezy.transport import memory
4420.1.1 by Vincent Ladeuil
Cleanup imports.
46
47
5559.2.2 by Martin Pool
Change to using standard load_tests_apply_scenarios.
48
load_tests = scenarios.load_tests_apply_scenarios
4487.2.2 by Vincent Ladeuil
Add tests for push --strict against pending merges.
49
50
4420.1.1 by Vincent Ladeuil
Cleanup imports.
51
class TestPush(tests.TestCaseWithTransport):
1614.2.9 by Olaf Conradi
Added testcases for using push with --remember. Moved remember code to
52
4017.2.3 by Robert Collins
Review feedback.
53
    def test_push_error_on_vfs_http(self):
54
        """ pushing a branch to a HTTP server fails cleanly. """
55
        # the trunk is published on a web server
4420.1.1 by Vincent Ladeuil
Cleanup imports.
56
        self.transport_readonly_server = http_server.HttpServer
4017.2.3 by Robert Collins
Review feedback.
57
        self.make_branch('source')
58
        public_url = self.get_readonly_url('target')
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
59
        self.run_bzr_error(['http does not support mkdir'],
4017.2.3 by Robert Collins
Review feedback.
60
                           ['push', public_url],
61
                           working_dir='source')
62
6394.1.1 by Jelmer Vernooij
Suggest using :parent in "bzr push" if there was no push location specified or remembered.
63
    def test_push_suggests_parent_alias(self):
64
        """Push suggests using :parent if there is a known parent branch."""
65
        tree_a = self.make_branch_and_tree('a')
66
        tree_a.commit('this is a commit')
67
        tree_b = self.make_branch_and_tree('b')
68
69
        # If there is no parent location set, :parent isn't mentioned.
70
        out = self.run_bzr('push', working_dir='a', retcode=3)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
71
        self.assertEqual(out,
7143.15.2 by Jelmer Vernooij
Run autopep8.
72
                         ('', 'brz: ERROR: No push location known or specified.\n'))
6394.1.1 by Jelmer Vernooij
Suggest using :parent in "bzr push" if there was no push location specified or remembered.
73
74
        # If there is a parent location set, the error suggests :parent.
75
        tree_a.branch.set_parent(tree_b.branch.base)
76
        out = self.run_bzr('push', working_dir='a', retcode=3)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
77
        self.assertEqual(out,
7143.15.2 by Jelmer Vernooij
Run autopep8.
78
                         ('', 'brz: ERROR: No push location known or specified. '
79
                          'To push to the parent branch '
80
                          '(at %s), use \'brz push :parent\'.\n' %
81
                          urlutils.unescape_for_display(tree_b.branch.base, 'utf-8')))
6394.1.1 by Jelmer Vernooij
Suggest using :parent in "bzr push" if there was no push location specified or remembered.
82
1614.2.9 by Olaf Conradi
Added testcases for using push with --remember. Moved remember code to
83
    def test_push_remember(self):
84
        """Push changes from one branch to another and test push location."""
1614.2.16 by Olaf Conradi
Modified blackbox test cases to use bzrlib API.
85
        transport = self.get_transport()
86
        tree_a = self.make_branch_and_tree('branch_a')
87
        branch_a = tree_a.branch
88
        self.build_tree(['branch_a/a'])
89
        tree_a.add('a')
90
        tree_a.commit('commit a')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
91
        tree_b = branch_a.controldir.sprout('branch_b').open_workingtree()
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
92
        branch_b = tree_b.branch
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
93
        tree_c = branch_a.controldir.sprout('branch_c').open_workingtree()
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
94
        branch_c = tree_c.branch
1614.2.16 by Olaf Conradi
Modified blackbox test cases to use bzrlib API.
95
        self.build_tree(['branch_a/b'])
96
        tree_a.add('b')
97
        tree_a.commit('commit b')
98
        self.build_tree(['branch_b/c'])
99
        tree_b.add('c')
100
        tree_b.commit('commit c')
1614.2.9 by Olaf Conradi
Added testcases for using push with --remember. Moved remember code to
101
        # initial push location must be empty
1614.2.16 by Olaf Conradi
Modified blackbox test cases to use bzrlib API.
102
        self.assertEqual(None, branch_b.get_push_location())
1785.1.2 by John Arbash Meinel
Push should only save the location if it can actually connect (doesn't need to succeed)
103
1614.2.9 by Olaf Conradi
Added testcases for using push with --remember. Moved remember code to
104
        # test push for failure without push location set
4420.1.1 by Vincent Ladeuil
Cleanup imports.
105
        out = self.run_bzr('push', working_dir='branch_a', retcode=3)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
106
        self.assertEqual(out,
7143.15.2 by Jelmer Vernooij
Run autopep8.
107
                         ('', 'brz: ERROR: No push location known or specified.\n'))
1785.1.2 by John Arbash Meinel
Push should only save the location if it can actually connect (doesn't need to succeed)
108
109
        # test not remembered if cannot actually push
4420.1.1 by Vincent Ladeuil
Cleanup imports.
110
        self.run_bzr('push path/which/doesnt/exist',
111
                     working_dir='branch_a', retcode=3)
112
        out = self.run_bzr('push', working_dir='branch_a', retcode=3)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
113
        self.assertEqual(
7143.15.2 by Jelmer Vernooij
Run autopep8.
114
            ('', 'brz: ERROR: No push location known or specified.\n'),
115
            out)
1785.1.2 by John Arbash Meinel
Push should only save the location if it can actually connect (doesn't need to succeed)
116
1614.2.9 by Olaf Conradi
Added testcases for using push with --remember. Moved remember code to
117
        # test implicit --remember when no push location set, push fails
4420.1.1 by Vincent Ladeuil
Cleanup imports.
118
        out = self.run_bzr('push ../branch_b',
119
                           working_dir='branch_a', retcode=3)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
120
        self.assertEqual(out,
7143.15.2 by Jelmer Vernooij
Run autopep8.
121
                         ('', 'brz: ERROR: These branches have diverged.  '
122
                          'See "brz help diverged-branches" for more information.\n'))
6404.6.6 by Vincent Ladeuil
Use idioms coherently and add comments to make their purpose clearer.
123
        # Refresh the branch as 'push' modified it
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
124
        branch_a = branch_a.controldir.open_branch()
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
125
        self.assertEqual(osutils.abspath(branch_a.get_push_location()),
7143.15.2 by Jelmer Vernooij
Run autopep8.
126
                         osutils.abspath(branch_b.controldir.root_transport.base))
1785.1.2 by John Arbash Meinel
Push should only save the location if it can actually connect (doesn't need to succeed)
127
1614.2.9 by Olaf Conradi
Added testcases for using push with --remember. Moved remember code to
128
        # test implicit --remember after resolving previous failure
4420.1.1 by Vincent Ladeuil
Cleanup imports.
129
        uncommit.uncommit(branch=branch_b, tree=tree_b)
1614.2.16 by Olaf Conradi
Modified blackbox test cases to use bzrlib API.
130
        transport.delete('branch_b/c')
4420.1.1 by Vincent Ladeuil
Cleanup imports.
131
        out, err = self.run_bzr('push', working_dir='branch_a')
6404.6.6 by Vincent Ladeuil
Use idioms coherently and add comments to make their purpose clearer.
132
        # Refresh the branch as 'push' modified it
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
133
        branch_a = branch_a.controldir.open_branch()
1785.1.1 by John Arbash Meinel
Fix the output of 'bzr push' so that it prints the location correctly.
134
        path = branch_a.get_push_location()
2220.2.38 by Martin Pool
Tag conflicts from push go to stdout.
135
        self.assertEqual(err,
5998.1.3 by Jonathan Riddell
add a test for bzr push -q
136
                         'Using saved push location: %s\n'
3978.2.2 by Jelmer Vernooij
Write status messages during push to stderr rather than stdout.
137
                         'All changes applied successfully.\n'
5998.1.3 by Jonathan Riddell
add a test for bzr push -q
138
                         'Pushed up to revision 2.\n'
139
                         % urlutils.local_path_from_url(path))
1785.1.1 by John Arbash Meinel
Fix the output of 'bzr push' so that it prints the location correctly.
140
        self.assertEqual(path,
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
141
                         branch_b.controldir.root_transport.base)
1614.2.9 by Olaf Conradi
Added testcases for using push with --remember. Moved remember code to
142
        # test explicit --remember
4420.1.1 by Vincent Ladeuil
Cleanup imports.
143
        self.run_bzr('push ../branch_c --remember', working_dir='branch_a')
6404.6.6 by Vincent Ladeuil
Use idioms coherently and add comments to make their purpose clearer.
144
        # Refresh the branch as 'push' modified it
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
145
        branch_a = branch_a.controldir.open_branch()
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
146
        self.assertEqual(branch_a.get_push_location(),
7143.15.2 by Jelmer Vernooij
Run autopep8.
147
                         branch_c.controldir.root_transport.base)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
148
1692.3.1 by Robert Collins
Fix push to work with just a branch, no need for a working tree.
149
    def test_push_without_tree(self):
6622.1.29 by Jelmer Vernooij
Fix some more tests.
150
        # brz push from a branch that does not have a checkout should work.
1692.3.1 by Robert Collins
Fix push to work with just a branch, no need for a working tree.
151
        b = self.make_branch('.')
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
152
        out, err = self.run_bzr('push pushed-location')
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
153
        self.assertEqual('', out)
154
        self.assertEqual('Created new branch.\n', err)
4420.1.1 by Vincent Ladeuil
Cleanup imports.
155
        b2 = branch.Branch.open('pushed-location')
1692.3.1 by Robert Collins
Fix push to work with just a branch, no need for a working tree.
156
        self.assertEndsWith(b2.base, 'pushed-location/')
1692.3.6 by Robert Collins
Show the correct number of revisions pushed when pushing a new branch (Robert Collins).
157
5448.6.5 by Matthew Gordon
Added test for push --no-tree.
158
    def test_push_no_tree(self):
6622.1.29 by Jelmer Vernooij
Fix some more tests.
159
        # brz push --no-tree of a branch with working trees
5448.6.5 by Matthew Gordon
Added test for push --no-tree.
160
        b = self.make_branch_and_tree('push-from')
161
        self.build_tree(['push-from/file'])
162
        b.add('file')
163
        b.commit('commit 1')
164
        out, err = self.run_bzr('push --no-tree -d push-from push-to')
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
165
        self.assertEqual('', out)
166
        self.assertEqual('Created new branch.\n', err)
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
167
        self.assertPathDoesNotExist('push-to/file')
5448.6.5 by Matthew Gordon
Added test for push --no-tree.
168
1692.3.6 by Robert Collins
Show the correct number of revisions pushed when pushing a new branch (Robert Collins).
169
    def test_push_new_branch_revision_count(self):
6622.1.29 by Jelmer Vernooij
Fix some more tests.
170
        # brz push of a branch with revisions to a new location
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
171
        # should print the number of revisions equal to the length of the
1692.3.6 by Robert Collins
Show the correct number of revisions pushed when pushing a new branch (Robert Collins).
172
        # local branch.
173
        t = self.make_branch_and_tree('tree')
174
        self.build_tree(['tree/file'])
175
        t.add('file')
176
        t.commit('commit 1')
4420.1.1 by Vincent Ladeuil
Cleanup imports.
177
        out, err = self.run_bzr('push -d tree pushed-to')
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
178
        self.assertEqual('', out)
179
        self.assertEqual('Created new branch.\n', err)
1711.2.3 by John Arbash Meinel
Fix push to only push revisions in the current ancestry. (bug???)
180
5998.1.3 by Jonathan Riddell
add a test for bzr push -q
181
    def test_push_quiet(self):
182
        # test that using -q makes output quiet
183
        t = self.make_branch_and_tree('tree')
184
        self.build_tree(['tree/file'])
185
        t.add('file')
186
        t.commit('commit 1')
187
        self.run_bzr('push -d tree pushed-to')
6404.6.6 by Vincent Ladeuil
Use idioms coherently and add comments to make their purpose clearer.
188
        # Refresh the branch as 'push' modified it and get the push location
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
189
        push_loc = t.branch.controldir.open_branch().get_push_location()
5998.1.3 by Jonathan Riddell
add a test for bzr push -q
190
        out, err = self.run_bzr('push', working_dir="tree")
6112.4.3 by Jelmer Vernooij
Fix push tests.
191
        self.assertEqual('Using saved push location: %s\n'
192
                         'No new revisions or tags to push.\n' %
6404.6.6 by Vincent Ladeuil
Use idioms coherently and add comments to make their purpose clearer.
193
                         urlutils.local_path_from_url(push_loc), err)
5998.1.3 by Jonathan Riddell
add a test for bzr push -q
194
        out, err = self.run_bzr('push -q', working_dir="tree")
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
195
        self.assertEqual('', out)
196
        self.assertEqual('', err)
5998.1.3 by Jonathan Riddell
add a test for bzr push -q
197
1711.2.3 by John Arbash Meinel
Fix push to only push revisions in the current ancestry. (bug???)
198
    def test_push_only_pushes_history(self):
199
        # Knit branches should only push the history for the current revision.
4420.1.1 by Vincent Ladeuil
Cleanup imports.
200
        format = bzrdir.BzrDirMetaFormat1()
201
        format.repository_format = knitrepo.RepositoryFormatKnit1()
1711.2.3 by John Arbash Meinel
Fix push to only push revisions in the current ancestry. (bug???)
202
        shared_repo = self.make_repository('repo', format=format, shared=True)
203
        shared_repo.set_make_working_trees(True)
204
205
        def make_shared_tree(path):
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
206
            shared_repo.controldir.root_transport.mkdir(path)
6207.3.3 by jelmer at samba
Fix tests and the like.
207
            controldir.ControlDir.create_branch_convenience('repo/' + path)
4420.1.1 by Vincent Ladeuil
Cleanup imports.
208
            return workingtree.WorkingTree.open('repo/' + path)
1711.2.3 by John Arbash Meinel
Fix push to only push revisions in the current ancestry. (bug???)
209
        tree_a = make_shared_tree('a')
210
        self.build_tree(['repo/a/file'])
211
        tree_a.add('file')
6855.4.1 by Jelmer Vernooij
Yet more bees.
212
        tree_a.commit('commit a-1', rev_id=b'a-1')
1711.2.3 by John Arbash Meinel
Fix push to only push revisions in the current ancestry. (bug???)
213
        f = open('repo/a/file', 'ab')
6973.5.2 by Jelmer Vernooij
Add more bees.
214
        f.write(b'more stuff\n')
1711.2.3 by John Arbash Meinel
Fix push to only push revisions in the current ancestry. (bug???)
215
        f.close()
6855.4.1 by Jelmer Vernooij
Yet more bees.
216
        tree_a.commit('commit a-2', rev_id=b'a-2')
1711.2.3 by John Arbash Meinel
Fix push to only push revisions in the current ancestry. (bug???)
217
218
        tree_b = make_shared_tree('b')
219
        self.build_tree(['repo/b/file'])
220
        tree_b.add('file')
6855.4.1 by Jelmer Vernooij
Yet more bees.
221
        tree_b.commit('commit b-1', rev_id=b'b-1')
1711.2.3 by John Arbash Meinel
Fix push to only push revisions in the current ancestry. (bug???)
222
6973.5.2 by Jelmer Vernooij
Add more bees.
223
        self.assertTrue(shared_repo.has_revision(b'a-1'))
224
        self.assertTrue(shared_repo.has_revision(b'a-2'))
225
        self.assertTrue(shared_repo.has_revision(b'b-1'))
1711.2.3 by John Arbash Meinel
Fix push to only push revisions in the current ancestry. (bug???)
226
227
        # Now that we have a repository with shared files, make sure
228
        # that things aren't copied out by a 'push'
4420.1.1 by Vincent Ladeuil
Cleanup imports.
229
        self.run_bzr('push ../../push-b', working_dir='repo/b')
230
        pushed_tree = workingtree.WorkingTree.open('push-b')
1711.2.3 by John Arbash Meinel
Fix push to only push revisions in the current ancestry. (bug???)
231
        pushed_repo = pushed_tree.branch.repository
6973.5.2 by Jelmer Vernooij
Add more bees.
232
        self.assertFalse(pushed_repo.has_revision(b'a-1'))
233
        self.assertFalse(pushed_repo.has_revision(b'a-2'))
234
        self.assertTrue(pushed_repo.has_revision(b'b-1'))
1711.2.3 by John Arbash Meinel
Fix push to only push revisions in the current ancestry. (bug???)
235
1843.2.1 by Aaron Bentley
Add failing tests for funky ids
236
    def test_push_funky_id(self):
237
        t = self.make_branch_and_tree('tree')
4420.1.1 by Vincent Ladeuil
Cleanup imports.
238
        self.build_tree(['tree/filename'])
7045.1.14 by Jelmer Vernooij
More fixes.
239
        t.add('filename', b'funky-chars<>%&;"\'')
1843.2.1 by Aaron Bentley
Add failing tests for funky ids
240
        t.commit('commit filename')
4420.1.1 by Vincent Ladeuil
Cleanup imports.
241
        self.run_bzr('push -d tree new-tree')
2227.3.1 by John Arbash Meinel
Allow push to create Branch when necessary, and add --use-existing
242
2220.2.9 by Martin Pool
Add specific tests for push -d and pull -d
243
    def test_push_dash_d(self):
244
        t = self.make_branch_and_tree('from')
245
        t.commit(allow_pointless=True,
7143.15.2 by Jelmer Vernooij
Run autopep8.
246
                 message='first commit')
2530.3.1 by Martin Pool
Cleanup old variations on run_bzr in the test suite
247
        self.run_bzr('push -d from to-one')
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
248
        self.assertPathExists('to-one')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
249
        self.run_bzr('push -d %s %s'
7143.15.2 by Jelmer Vernooij
Run autopep8.
250
                     % tuple(map(urlutils.local_path_to_url, ['from', 'to-two'])))
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
251
        self.assertPathExists('to-two')
2279.3.1 by mbp at sourcefrog
Add a -d option to push, pull, merge (ported from tags branch)
252
5609.26.1 by John Arbash Meinel
Fix bug #465517, 'bzr push' to a target with a repo but no branch
253
    def test_push_repository_no_branch_doesnt_fetch_all_revs(self):
254
        # See https://bugs.launchpad.net/bzr/+bug/465517
255
        target_repo = self.make_repository('target')
256
        source = self.make_branch_builder('source')
257
        source.start_series()
6816.2.1 by Jelmer Vernooij
Migrate some build_snapshot code over to having revision_id as keyword argument.
258
        source.build_snapshot(None, [
6973.13.2 by Jelmer Vernooij
Fix some more tests.
259
            ('add', ('', b'root-id', 'directory', None))],
6855.4.1 by Jelmer Vernooij
Yet more bees.
260
            revision_id=b'A')
6973.13.2 by Jelmer Vernooij
Fix some more tests.
261
        source.build_snapshot([b'A'], [], revision_id=b'B')
262
        source.build_snapshot([b'A'], [], revision_id=b'C')
5609.26.1 by John Arbash Meinel
Fix bug #465517, 'bzr push' to a target with a repo but no branch
263
        source.finish_series()
264
        self.run_bzr('push target -d source')
265
        self.addCleanup(target_repo.lock_read().unlock)
266
        # We should have pushed 'C', but not 'B', since it isn't in the
267
        # ancestry
7143.15.2 by Jelmer Vernooij
Run autopep8.
268
        self.assertEqual([(b'A',), (b'C',)], sorted(
269
            target_repo.revisions.keys()))
5609.26.1 by John Arbash Meinel
Fix bug #465517, 'bzr push' to a target with a repo but no branch
270
4011.2.1 by Robert Collins
Add ratchet style blackbox effort tests for push over bzr+ssh. (Andrew Bennetts, Robert Collins)
271
    def test_push_smart_non_stacked_streaming_acceptance(self):
4017.2.1 by Robert Collins
Add BzrDirFormatMeta1 test for the amount of rpc calls made initializing over the network.
272
        self.setup_smart_server_with_call_log()
4011.2.1 by Robert Collins
Add ratchet style blackbox effort tests for push over bzr+ssh. (Andrew Bennetts, Robert Collins)
273
        t = self.make_branch_and_tree('from')
274
        t.commit(allow_pointless=True, message='first commit')
4017.2.1 by Robert Collins
Add BzrDirFormatMeta1 test for the amount of rpc calls made initializing over the network.
275
        self.reset_smart_call_log()
4011.2.1 by Robert Collins
Add ratchet style blackbox effort tests for push over bzr+ssh. (Andrew Bennetts, Robert Collins)
276
        self.run_bzr(['push', self.get_url('to-one')], working_dir='from')
4011.2.4 by Robert Collins
Make the ratchet aspect of the blackbox smart server push acceptance tests clearer.
277
        # This figure represent the amount of work to perform this use case. It
278
        # is entirely ok to reduce this number if a test fails due to rpc_count
279
        # being too low. If rpc_count increases, more network roundtrips have
280
        # become necessary for this use case. Please do not adjust this number
281
        # upwards without agreement from bzr's network support maintainers.
4307.2.2 by Robert Collins
Lock repositories created by BzrDirFormat.initialize_on_transport_ex.
282
        self.assertLength(9, self.hpss_calls)
6366.1.4 by Jelmer Vernooij
Test connection count calls for most blackbox commands.
283
        self.assertLength(1, self.hpss_connections)
284
        self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
4011.2.1 by Robert Collins
Add ratchet style blackbox effort tests for push over bzr+ssh. (Andrew Bennetts, Robert Collins)
285
286
    def test_push_smart_stacked_streaming_acceptance(self):
4017.2.1 by Robert Collins
Add BzrDirFormatMeta1 test for the amount of rpc calls made initializing over the network.
287
        self.setup_smart_server_with_call_log()
4011.2.1 by Robert Collins
Add ratchet style blackbox effort tests for push over bzr+ssh. (Andrew Bennetts, Robert Collins)
288
        parent = self.make_branch_and_tree('parent', format='1.9')
289
        parent.commit(message='first commit')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
290
        local = parent.controldir.sprout('local').open_workingtree()
4011.2.1 by Robert Collins
Add ratchet style blackbox effort tests for push over bzr+ssh. (Andrew Bennetts, Robert Collins)
291
        local.commit(message='local commit')
4017.2.1 by Robert Collins
Add BzrDirFormatMeta1 test for the amount of rpc calls made initializing over the network.
292
        self.reset_smart_call_log()
4011.2.1 by Robert Collins
Add ratchet style blackbox effort tests for push over bzr+ssh. (Andrew Bennetts, Robert Collins)
293
        self.run_bzr(['push', '--stacked', '--stacked-on', '../parent',
7143.15.2 by Jelmer Vernooij
Run autopep8.
294
                      self.get_url('public')], working_dir='local')
4011.2.4 by Robert Collins
Make the ratchet aspect of the blackbox smart server push acceptance tests clearer.
295
        # This figure represent the amount of work to perform this use case. It
296
        # is entirely ok to reduce this number if a test fails due to rpc_count
297
        # being too low. If rpc_count increases, more network roundtrips have
298
        # become necessary for this use case. Please do not adjust this number
299
        # upwards without agreement from bzr's network support maintainers.
6404.6.1 by Vincent Ladeuil
Tests passing for a first rough version of a cached branch config store. The changes here are too invasive and several parallel proposals have been made.
300
        self.assertLength(15, self.hpss_calls)
6366.1.4 by Jelmer Vernooij
Test connection count calls for most blackbox commands.
301
        self.assertLength(1, self.hpss_connections)
302
        self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
4420.1.1 by Vincent Ladeuil
Cleanup imports.
303
        remote = branch.Branch.open('public')
4011.2.1 by Robert Collins
Add ratchet style blackbox effort tests for push over bzr+ssh. (Andrew Bennetts, Robert Collins)
304
        self.assertEndsWith(remote.get_stacked_on_url(), '/parent')
305
4556.2.1 by Andrew Bennetts
Add Branch.set_tags_bytes RPC, with HPSS call count acceptance test. Also fixes serialisation of LockDir, and uses external_url() in LockDir's repr and contention message.
306
    def test_push_smart_tags_streaming_acceptance(self):
307
        self.setup_smart_server_with_call_log()
308
        t = self.make_branch_and_tree('from')
309
        rev_id = t.commit(allow_pointless=True, message='first commit')
310
        t.branch.tags.set_tag('new-tag', rev_id)
311
        self.reset_smart_call_log()
312
        self.run_bzr(['push', self.get_url('to-one')], working_dir='from')
313
        # This figure represent the amount of work to perform this use case. It
314
        # is entirely ok to reduce this number if a test fails due to rpc_count
315
        # being too low. If rpc_count increases, more network roundtrips have
316
        # become necessary for this use case. Please do not adjust this number
317
        # upwards without agreement from bzr's network support maintainers.
318
        self.assertLength(11, self.hpss_calls)
6366.1.4 by Jelmer Vernooij
Test connection count calls for most blackbox commands.
319
        self.assertLength(1, self.hpss_connections)
320
        self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
4556.2.1 by Andrew Bennetts
Add Branch.set_tags_bytes RPC, with HPSS call count acceptance test. Also fixes serialisation of LockDir, and uses external_url() in LockDir's repr and contention message.
321
4634.47.3 by Andrew Bennetts
Add a BzrDir.open_2.1 verb that indicates if there is a workingtree present. Removes the last 2 VFS calls from incremental pushes.
322
    def test_push_smart_incremental_acceptance(self):
323
        self.setup_smart_server_with_call_log()
324
        t = self.make_branch_and_tree('from')
325
        rev_id1 = t.commit(allow_pointless=True, message='first commit')
326
        rev_id2 = t.commit(allow_pointless=True, message='second commit')
327
        self.run_bzr(
328
            ['push', self.get_url('to-one'), '-r1'], working_dir='from')
329
        self.reset_smart_call_log()
330
        self.run_bzr(['push', self.get_url('to-one')], working_dir='from')
331
        # This figure represent the amount of work to perform this use case. It
332
        # is entirely ok to reduce this number if a test fails due to rpc_count
333
        # being too low. If rpc_count increases, more network roundtrips have
334
        # become necessary for this use case. Please do not adjust this number
335
        # upwards without agreement from bzr's network support maintainers.
336
        self.assertLength(11, self.hpss_calls)
6366.1.4 by Jelmer Vernooij
Test connection count calls for most blackbox commands.
337
        self.assertLength(1, self.hpss_connections)
338
        self.assertThat(self.hpss_calls, ContainsNoVfsCalls)
4634.47.3 by Andrew Bennetts
Add a BzrDir.open_2.1 verb that indicates if there is a workingtree present. Removes the last 2 VFS calls from incremental pushes.
339
4416.3.1 by Jonathan Lange
Add two tests, the second of which demonstrates bug 385132.
340
    def test_push_smart_with_default_stacking_url_path_segment(self):
4416.3.2 by Jonathan Lange
Comment the blackbox tests, for my own sanity.
341
        # If the default stacked-on location is a path element then branches
342
        # we push there over the smart server are stacked and their
343
        # stacked_on_url is that exact path segment. Added to nail bug 385132.
4416.3.1 by Jonathan Lange
Add two tests, the second of which demonstrates bug 385132.
344
        self.setup_smart_server_with_call_log()
345
        self.make_branch('stack-on', format='1.9')
6653.6.5 by Jelmer Vernooij
Rename make_bzrdir to make_controldir.
346
        self.make_controldir('.').get_config().set_default_stack_on(
4416.3.9 by Jonathan Lange
Put the test back the way it was.
347
            '/stack-on')
4416.3.1 by Jonathan Lange
Add two tests, the second of which demonstrates bug 385132.
348
        self.make_branch('from', format='1.9')
349
        out, err = self.run_bzr(['push', '-d', 'from', self.get_url('to')])
4416.3.10 by Jonathan Lange
Merge trunk
350
        b = branch.Branch.open(self.get_url('to'))
4416.3.12 by Jonathan Lange
This makes the test pass, but it's a bit ick.
351
        self.assertEqual('/extra/stack-on', b.get_stacked_on_url())
4416.3.1 by Jonathan Lange
Add two tests, the second of which demonstrates bug 385132.
352
353
    def test_push_smart_with_default_stacking_relative_path(self):
4416.3.2 by Jonathan Lange
Comment the blackbox tests, for my own sanity.
354
        # If the default stacked-on location is a relative path then branches
355
        # we push there over the smart server are stacked and their
356
        # stacked_on_url is a relative path. Added to nail bug 385132.
4416.3.1 by Jonathan Lange
Add two tests, the second of which demonstrates bug 385132.
357
        self.setup_smart_server_with_call_log()
358
        self.make_branch('stack-on', format='1.9')
6653.6.5 by Jelmer Vernooij
Rename make_bzrdir to make_controldir.
359
        self.make_controldir('.').get_config().set_default_stack_on('stack-on')
4416.3.1 by Jonathan Lange
Add two tests, the second of which demonstrates bug 385132.
360
        self.make_branch('from', format='1.9')
361
        out, err = self.run_bzr(['push', '-d', 'from', self.get_url('to')])
4416.3.10 by Jonathan Lange
Merge trunk
362
        b = branch.Branch.open(self.get_url('to'))
363
        self.assertEqual('../stack-on', b.get_stacked_on_url())
4416.3.1 by Jonathan Lange
Add two tests, the second of which demonstrates bug 385132.
364
2227.3.1 by John Arbash Meinel
Allow push to create Branch when necessary, and add --use-existing
365
    def create_simple_tree(self):
366
        tree = self.make_branch_and_tree('tree')
367
        self.build_tree(['tree/a'])
6855.4.1 by Jelmer Vernooij
Yet more bees.
368
        tree.add(['a'], [b'a-id'])
369
        tree.commit('one', rev_id=b'r1')
2227.3.1 by John Arbash Meinel
Allow push to create Branch when necessary, and add --use-existing
370
        return tree
371
372
    def test_push_create_prefix(self):
6622.1.29 by Jelmer Vernooij
Fix some more tests.
373
        """'brz push --create-prefix' will create leading directories."""
2227.3.1 by John Arbash Meinel
Allow push to create Branch when necessary, and add --use-existing
374
        tree = self.create_simple_tree()
375
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
376
        self.run_bzr_error(['Parent directory of ../new/tree does not exist'],
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
377
                           'push ../new/tree',
2227.3.1 by John Arbash Meinel
Allow push to create Branch when necessary, and add --use-existing
378
                           working_dir='tree')
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
379
        self.run_bzr('push ../new/tree --create-prefix',
2227.3.1 by John Arbash Meinel
Allow push to create Branch when necessary, and add --use-existing
380
                     working_dir='tree')
4420.1.1 by Vincent Ladeuil
Cleanup imports.
381
        new_tree = workingtree.WorkingTree.open('new/tree')
2227.3.1 by John Arbash Meinel
Allow push to create Branch when necessary, and add --use-existing
382
        self.assertEqual(tree.last_revision(), new_tree.last_revision())
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
383
        self.assertPathExists('new/tree/a')
2227.3.1 by John Arbash Meinel
Allow push to create Branch when necessary, and add --use-existing
384
385
    def test_push_use_existing(self):
6622.1.29 by Jelmer Vernooij
Fix some more tests.
386
        """'brz push --use-existing-dir' can push into an existing dir.
2227.3.1 by John Arbash Meinel
Allow push to create Branch when necessary, and add --use-existing
387
6622.1.29 by Jelmer Vernooij
Fix some more tests.
388
        By default, 'brz push' will not use an existing, non-versioned dir.
2227.3.1 by John Arbash Meinel
Allow push to create Branch when necessary, and add --use-existing
389
        """
390
        tree = self.create_simple_tree()
391
        self.build_tree(['target/'])
392
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
393
        self.run_bzr_error(['Target directory ../target already exists',
394
                            'Supply --use-existing-dir',
7143.15.2 by Jelmer Vernooij
Run autopep8.
395
                            ],
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
396
                           'push ../target', working_dir='tree')
2227.3.4 by John Arbash Meinel
Switch back to --use-existing-dir
397
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
398
        self.run_bzr('push --use-existing-dir ../target',
2227.3.1 by John Arbash Meinel
Allow push to create Branch when necessary, and add --use-existing
399
                     working_dir='tree')
400
4420.1.1 by Vincent Ladeuil
Cleanup imports.
401
        new_tree = workingtree.WorkingTree.open('target')
2227.3.1 by John Arbash Meinel
Allow push to create Branch when necessary, and add --use-existing
402
        self.assertEqual(tree.last_revision(), new_tree.last_revision())
403
        # The push should have created target/a
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
404
        self.assertPathExists('target/a')
2227.3.1 by John Arbash Meinel
Allow push to create Branch when necessary, and add --use-existing
405
4634.105.1 by Andrew Bennetts
Fix traceback when doing 'bzr push --use-existing-dir' into a dir with an invalid .bzr directory.
406
    def test_push_use_existing_into_empty_bzrdir(self):
6622.1.30 by Jelmer Vernooij
Some more test fixes.
407
        """'brz push --use-existing-dir' into a dir with an empty .bzr dir
4634.105.1 by Andrew Bennetts
Fix traceback when doing 'bzr push --use-existing-dir' into a dir with an invalid .bzr directory.
408
        fails.
409
        """
410
        tree = self.create_simple_tree()
411
        self.build_tree(['target/', 'target/.bzr/'])
412
        self.run_bzr_error(
6622.1.30 by Jelmer Vernooij
Some more test fixes.
413
            ['Target directory ../target already contains a .bzr directory, '
4634.105.2 by Andrew Bennetts
Clearer code and error message.
414
             'but it is not valid.'],
4634.105.1 by Andrew Bennetts
Fix traceback when doing 'bzr push --use-existing-dir' into a dir with an invalid .bzr directory.
415
            'push ../target --use-existing-dir', working_dir='tree')
416
2227.3.1 by John Arbash Meinel
Allow push to create Branch when necessary, and add --use-existing
417
    def test_push_onto_repo(self):
6622.1.29 by Jelmer Vernooij
Fix some more tests.
418
        """We should be able to 'brz push' into an existing bzrdir."""
2227.3.1 by John Arbash Meinel
Allow push to create Branch when necessary, and add --use-existing
419
        tree = self.create_simple_tree()
420
        repo = self.make_repository('repo', shared=True)
421
2552.2.3 by Vincent Ladeuil
Deprecate the varargs syntax and fix the tests.
422
        self.run_bzr('push ../repo',
2227.3.1 by John Arbash Meinel
Allow push to create Branch when necessary, and add --use-existing
423
                     working_dir='tree')
424
425
        # Pushing onto an existing bzrdir will create a repository and
426
        # branch as needed, but will only create a working tree if there was
427
        # no BzrDir before.
4420.1.1 by Vincent Ladeuil
Cleanup imports.
428
        self.assertRaises(errors.NoWorkingTree,
429
                          workingtree.WorkingTree.open, 'repo')
430
        new_branch = branch.Branch.open('repo')
2227.3.1 by John Arbash Meinel
Allow push to create Branch when necessary, and add --use-existing
431
        self.assertEqual(tree.last_revision(), new_branch.last_revision())
432
433
    def test_push_onto_just_bzrdir(self):
434
        """We don't handle when the target is just a bzrdir.
435
436
        Because you shouldn't be able to create *just* a bzrdir in the wild.
437
        """
438
        # TODO: jam 20070109 Maybe it would be better to create the repository
439
        #       if at this point
440
        tree = self.create_simple_tree()
6653.6.5 by Jelmer Vernooij
Rename make_bzrdir to make_controldir.
441
        a_controldir = self.make_controldir('dir')
2227.3.1 by John Arbash Meinel
Allow push to create Branch when necessary, and add --use-existing
442
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
443
        self.run_bzr_error(['At ../dir you have a valid .bzr control'],
7143.15.2 by Jelmer Vernooij
Run autopep8.
444
                           'push ../dir',
445
                           working_dir='tree')
3066.3.1 by jml at canonical
Catch redirects raised by mkdir() in the push command. This is primarily
446
3256.1.1 by Daniel Watkins
Added test for push with a revspec.
447
    def test_push_with_revisionspec(self):
3256.1.3 by Daniel Watkins
Clarified test intent.
448
        """We should be able to push a revision older than the tip."""
3256.1.1 by Daniel Watkins
Added test for push with a revspec.
449
        tree_from = self.make_branch_and_tree('from')
6973.5.2 by Jelmer Vernooij
Add more bees.
450
        tree_from.commit("One.", rev_id=b"from-1")
451
        tree_from.commit("Two.", rev_id=b"from-2")
3256.1.1 by Daniel Watkins
Added test for push with a revspec.
452
453
        self.run_bzr('push -r1 ../to', working_dir='from')
454
4420.1.1 by Vincent Ladeuil
Cleanup imports.
455
        tree_to = workingtree.WorkingTree.open('to')
3256.1.1 by Daniel Watkins
Added test for push with a revspec.
456
        repo_to = tree_to.branch.repository
6973.5.2 by Jelmer Vernooij
Add more bees.
457
        self.assertTrue(repo_to.has_revision(b'from-1'))
458
        self.assertFalse(repo_to.has_revision(b'from-2'))
459
        self.assertEqual(tree_to.branch.last_revision_info()[1], b'from-1')
6519.3.2 by Neil Martinsen-Burrell
add direct test as well
460
        self.assertFalse(
461
            tree_to.changes_from(tree_to.basis_tree()).has_changed())
3256.1.1 by Daniel Watkins
Added test for push with a revspec.
462
3256.1.4 by Daniel Watkins
Added test to ensure that passing a range of revisions errors.
463
        self.run_bzr_error(
6622.1.29 by Jelmer Vernooij
Fix some more tests.
464
            ['brz: ERROR: brz push --revision '
4325.4.6 by Vincent Ladeuil
Fixed as per John's and Markus reviews.
465
             'takes exactly one revision identifier\n'],
3256.1.4 by Daniel Watkins
Added test to ensure that passing a range of revisions errors.
466
            'push -r0..2 ../to', working_dir='from')
467
3221.11.13 by Robert Collins
Allow push --shallow to just work, and fix the testing HTTPServer to not be affected by chdir() calls.
468
    def create_trunk_and_feature_branch(self):
3221.11.12 by Robert Collins
Basic push --reference support, requires url, slow.
469
        # We have a mainline
470
        trunk_tree = self.make_branch_and_tree('target',
7143.15.2 by Jelmer Vernooij
Run autopep8.
471
                                               format='1.9')
3221.11.12 by Robert Collins
Basic push --reference support, requires url, slow.
472
        trunk_tree.commit('mainline')
473
        # and a branch from it
474
        branch_tree = self.make_branch_and_tree('branch',
7143.15.2 by Jelmer Vernooij
Run autopep8.
475
                                                format='1.9')
3221.11.12 by Robert Collins
Basic push --reference support, requires url, slow.
476
        branch_tree.pull(trunk_tree.branch)
3221.11.13 by Robert Collins
Allow push --shallow to just work, and fix the testing HTTPServer to not be affected by chdir() calls.
477
        branch_tree.branch.set_parent(trunk_tree.branch.base)
3221.11.12 by Robert Collins
Basic push --reference support, requires url, slow.
478
        # with some work on it
3221.11.13 by Robert Collins
Allow push --shallow to just work, and fix the testing HTTPServer to not be affected by chdir() calls.
479
        branch_tree.commit('moar work plz')
480
        return trunk_tree, branch_tree
481
3221.11.14 by Robert Collins
Refactor to reduce duplication.
482
    def assertPublished(self, branch_revid, stacked_on):
483
        """Assert that the branch 'published' has been published correctly."""
4420.1.1 by Vincent Ladeuil
Cleanup imports.
484
        published_branch = branch.Branch.open('published')
3221.11.14 by Robert Collins
Refactor to reduce duplication.
485
        # The published branch refers to the mainline
3537.3.1 by Martin Pool
Rename branch.get_stacked_on to get_stacked_on_url
486
        self.assertEqual(stacked_on, published_branch.get_stacked_on_url())
3221.11.14 by Robert Collins
Refactor to reduce duplication.
487
        # and the branch's work was pushed
488
        self.assertTrue(published_branch.repository.has_revision(branch_revid))
489
3549.1.1 by Martin Pool
rename push --reference to --stacked-on
490
    def test_push_new_branch_stacked_on(self):
491
        """Pushing a new branch with --stacked-on creates a stacked branch."""
3221.11.13 by Robert Collins
Allow push --shallow to just work, and fix the testing HTTPServer to not be affected by chdir() calls.
492
        trunk_tree, branch_tree = self.create_trunk_and_feature_branch()
493
        # we publish branch_tree with a reference to the mainline.
3549.1.1 by Martin Pool
rename push --reference to --stacked-on
494
        out, err = self.run_bzr(['push', '--stacked-on', trunk_tree.branch.base,
7143.15.2 by Jelmer Vernooij
Run autopep8.
495
                                 self.get_url('published')], working_dir='branch')
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
496
        self.assertEqual('', out)
497
        self.assertEqual('Created new stacked branch referring to %s.\n' %
7143.15.2 by Jelmer Vernooij
Run autopep8.
498
                         trunk_tree.branch.base, err)
3221.11.14 by Robert Collins
Refactor to reduce duplication.
499
        self.assertPublished(branch_tree.last_revision(),
7143.15.2 by Jelmer Vernooij
Run autopep8.
500
                             trunk_tree.branch.base)
3221.11.13 by Robert Collins
Allow push --shallow to just work, and fix the testing HTTPServer to not be affected by chdir() calls.
501
7290.8.1 by Jelmer Vernooij
Add test for derefencing directories in --stacked-on.
502
    def test_push_new_branch_stacked_on(self):
503
        """Pushing a new branch with --stacked-on can use directory URLs."""
504
        trunk_tree, branch_tree = self.create_trunk_and_feature_branch()
505
        class FooDirectory(object):
506
            def look_up(self, name, url, purpose=None):
507
                if url == 'foo:':
508
                    return trunk_tree.branch.base
509
                return url
510
        directory_service.directories.register('foo:', FooDirectory, 'Foo directory')
511
        self.addCleanup(directory_service.directories.remove, 'foo:')
512
        # we publish branch_tree with a reference to the mainline.
513
        out, err = self.run_bzr(['push', '--stacked-on', 'foo:',
514
                                 self.get_url('published')], working_dir='branch')
515
        self.assertEqual('', out)
516
        self.assertEqual('Created new stacked branch referring to %s.\n' %
517
                         trunk_tree.branch.base, err)
518
        self.assertPublished(branch_tree.last_revision(),
519
                             trunk_tree.branch.base)
520
3221.19.4 by Ian Clatworthy
shallow -> stacked
521
    def test_push_new_branch_stacked_uses_parent_when_no_public_url(self):
3221.11.17 by Robert Collins
no public location causes the parent to be used directly with push --shallow.
522
        """When the parent has no public url the parent is used as-is."""
523
        trunk_tree, branch_tree = self.create_trunk_and_feature_branch()
3221.19.4 by Ian Clatworthy
shallow -> stacked
524
        # now we do a stacked push, which should determine the public location
3221.11.17 by Robert Collins
no public location causes the parent to be used directly with push --shallow.
525
        # for us.
3221.19.4 by Ian Clatworthy
shallow -> stacked
526
        out, err = self.run_bzr(['push', '--stacked',
7143.15.2 by Jelmer Vernooij
Run autopep8.
527
                                 self.get_url('published')], working_dir='branch')
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
528
        self.assertEqual('', out)
529
        self.assertEqual('Created new stacked branch referring to %s.\n' %
7143.15.2 by Jelmer Vernooij
Run autopep8.
530
                         trunk_tree.branch.base, err)
4420.1.1 by Vincent Ladeuil
Cleanup imports.
531
        self.assertPublished(branch_tree.last_revision(),
532
                             trunk_tree.branch.base)
3221.11.17 by Robert Collins
no public location causes the parent to be used directly with push --shallow.
533
3221.19.4 by Ian Clatworthy
shallow -> stacked
534
    def test_push_new_branch_stacked_uses_parent_public(self):
535
        """Pushing a new branch with --stacked creates a stacked branch."""
3221.11.13 by Robert Collins
Allow push --shallow to just work, and fix the testing HTTPServer to not be affected by chdir() calls.
536
        trunk_tree, branch_tree = self.create_trunk_and_feature_branch()
537
        # the trunk is published on a web server
4420.1.1 by Vincent Ladeuil
Cleanup imports.
538
        self.transport_readonly_server = http_server.HttpServer
4241.6.8 by Robert Collins, John Arbash Meinel, Ian Clatworthy, Vincent Ladeuil
Add --development6-rich-root, disabling the legacy and unneeded development2 format, and activating the tests for CHK features disabled pending this format. (Robert Collins, John Arbash Meinel, Ian Clatworthy, Vincent Ladeuil)
539
        trunk_public = self.make_branch('public_trunk', format='1.9')
3221.11.13 by Robert Collins
Allow push --shallow to just work, and fix the testing HTTPServer to not be affected by chdir() calls.
540
        trunk_public.pull(trunk_tree.branch)
541
        trunk_public_url = self.get_readonly_url('public_trunk')
6404.6.1 by Vincent Ladeuil
Tests passing for a first rough version of a cached branch config store. The changes here are too invasive and several parallel proposals have been made.
542
        br = trunk_tree.branch
6404.6.7 by Vincent Ladeuil
Change set/remove to require a lock for the branch config files.
543
        br.set_public_branch(trunk_public_url)
3221.19.4 by Ian Clatworthy
shallow -> stacked
544
        # now we do a stacked push, which should determine the public location
3221.11.13 by Robert Collins
Allow push --shallow to just work, and fix the testing HTTPServer to not be affected by chdir() calls.
545
        # for us.
3221.19.4 by Ian Clatworthy
shallow -> stacked
546
        out, err = self.run_bzr(['push', '--stacked',
7143.15.2 by Jelmer Vernooij
Run autopep8.
547
                                 self.get_url('published')], working_dir='branch')
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
548
        self.assertEqual('', out)
549
        self.assertEqual('Created new stacked branch referring to %s.\n' %
7143.15.2 by Jelmer Vernooij
Run autopep8.
550
                         trunk_public_url, err)
3221.11.14 by Robert Collins
Refactor to reduce duplication.
551
        self.assertPublished(branch_tree.last_revision(), trunk_public_url)
3221.11.12 by Robert Collins
Basic push --reference support, requires url, slow.
552
3221.19.4 by Ian Clatworthy
shallow -> stacked
553
    def test_push_new_branch_stacked_no_parent(self):
554
        """Pushing with --stacked and no parent branch errors."""
4241.6.8 by Robert Collins, John Arbash Meinel, Ian Clatworthy, Vincent Ladeuil
Add --development6-rich-root, disabling the legacy and unneeded development2 format, and activating the tests for CHK features disabled pending this format. (Robert Collins, John Arbash Meinel, Ian Clatworthy, Vincent Ladeuil)
555
        branch = self.make_branch_and_tree('branch', format='1.9')
3221.19.4 by Ian Clatworthy
shallow -> stacked
556
        # now we do a stacked push, which should fail as the place to refer too
3221.11.15 by Robert Collins
no parent branch causes an error on push --shallow.
557
        # cannot be determined.
558
        out, err = self.run_bzr_error(
3221.19.4 by Ian Clatworthy
shallow -> stacked
559
            ['Could not determine branch to refer to\\.'], ['push', '--stacked',
7143.15.2 by Jelmer Vernooij
Run autopep8.
560
                                                            self.get_url('published')], working_dir='branch')
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
561
        self.assertEqual('', out)
3221.11.15 by Robert Collins
no parent branch causes an error on push --shallow.
562
        self.assertFalse(self.get_transport('published').has('.'))
563
3606.8.3 by John Arbash Meinel
push doesn't notify because it doesn't notice by default.
564
    def test_push_notifies_default_stacking(self):
3735.1.2 by Robert Collins
Remove 1.5 series dev formats and document development2 a little better.
565
        self.make_branch('stack_on', format='1.6')
6653.6.5 by Jelmer Vernooij
Rename make_bzrdir to make_controldir.
566
        self.make_controldir('.').get_config().set_default_stack_on('stack_on')
3735.1.2 by Robert Collins
Remove 1.5 series dev formats and document development2 a little better.
567
        self.make_branch('from', format='1.6')
3242.3.34 by Aaron Bentley
Add notification of default stacking
568
        out, err = self.run_bzr('push -d from to')
3641.1.1 by John Arbash Meinel
Merge in 1.6rc5 and revert disabling default stack on policy
569
        self.assertContainsRe(err,
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
570
                              'Using default stacking branch stack_on at .*')
3242.3.34 by Aaron Bentley
Add notification of default stacking
571
4165.2.1 by Robert Collins
Fix bzr failing to stack when a server requests it and the branch it is pushing from cannot stack but the branch it should stack on can.
572
    def test_push_stacks_with_default_stacking_if_target_is_stackable(self):
573
        self.make_branch('stack_on', format='1.6')
6653.6.5 by Jelmer Vernooij
Rename make_bzrdir to make_controldir.
574
        self.make_controldir('.').get_config().set_default_stack_on('stack_on')
4165.2.1 by Robert Collins
Fix bzr failing to stack when a server requests it and the branch it is pushing from cannot stack but the branch it should stack on can.
575
        self.make_branch('from', format='pack-0.92')
576
        out, err = self.run_bzr('push -d from to')
4420.1.1 by Vincent Ladeuil
Cleanup imports.
577
        b = branch.Branch.open('to')
578
        self.assertEqual('../stack_on', b.get_stacked_on_url())
4165.2.1 by Robert Collins
Fix bzr failing to stack when a server requests it and the branch it is pushing from cannot stack but the branch it should stack on can.
579
580
    def test_push_does_not_change_format_with_default_if_target_cannot(self):
581
        self.make_branch('stack_on', format='pack-0.92')
6653.6.5 by Jelmer Vernooij
Rename make_bzrdir to make_controldir.
582
        self.make_controldir('.').get_config().set_default_stack_on('stack_on')
4165.2.1 by Robert Collins
Fix bzr failing to stack when a server requests it and the branch it is pushing from cannot stack but the branch it should stack on can.
583
        self.make_branch('from', format='pack-0.92')
584
        out, err = self.run_bzr('push -d from to')
4420.1.1 by Vincent Ladeuil
Cleanup imports.
585
        b = branch.Branch.open('to')
6734.1.11 by Jelmer Vernooij
Move UnstackableBranchFormat.
586
        self.assertRaises(branch.UnstackableBranchFormat, b.get_stacked_on_url)
4165.2.1 by Robert Collins
Fix bzr failing to stack when a server requests it and the branch it is pushing from cannot stack but the branch it should stack on can.
587
3904.3.2 by Andrew Bennetts
Blackbox test that triggers the bug. Should get replaced with a unit test.
588
    def test_push_doesnt_create_broken_branch(self):
3904.3.7 by Andrew Bennetts
Comment the new tests.
589
        """Pushing a new standalone branch works even when there's a default
590
        stacking policy at the destination.
591
592
        The new branch will preserve the repo format (even if it isn't the
593
        default for the branch), and will be stacked when the repo format
594
        allows (which means that the branch format isn't necessarly preserved).
595
        """
3904.3.2 by Andrew Bennetts
Blackbox test that triggers the bug. Should get replaced with a unit test.
596
        self.make_repository('repo', shared=True, format='1.6')
597
        builder = self.make_branch_builder('repo/local', format='pack-0.92')
598
        builder.start_series()
6816.2.1 by Jelmer Vernooij
Migrate some build_snapshot code over to having revision_id as keyword argument.
599
        builder.build_snapshot(None, [
6973.13.2 by Jelmer Vernooij
Fix some more tests.
600
            ('add', ('', b'root-id', 'directory', '')),
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
601
            ('add', ('filename', b'f-id', 'file', b'content\n'))],
6855.4.1 by Jelmer Vernooij
Yet more bees.
602
            revision_id=b'rev-1')
6883.22.11 by Jelmer Vernooij
merge trunk
603
        builder.build_snapshot([b'rev-1'], [], revision_id=b'rev-2')
604
        builder.build_snapshot([b'rev-2'],
7143.15.2 by Jelmer Vernooij
Run autopep8.
605
                               [('modify', ('filename', b'new-content\n'))],
606
                               revision_id=b'rev-3')
3904.3.2 by Andrew Bennetts
Blackbox test that triggers the bug. Should get replaced with a unit test.
607
        builder.finish_series()
608
        branch = builder.get_branch()
3904.3.7 by Andrew Bennetts
Comment the new tests.
609
        # Push rev-1 to "trunk", so that we can stack on it.
3904.3.2 by Andrew Bennetts
Blackbox test that triggers the bug. Should get replaced with a unit test.
610
        self.run_bzr('push -d repo/local trunk -r 1')
3904.3.7 by Andrew Bennetts
Comment the new tests.
611
        # Set a default stacking policy so that new branches will automatically
612
        # stack on trunk.
6653.6.5 by Jelmer Vernooij
Rename make_bzrdir to make_controldir.
613
        self.make_controldir('.').get_config().set_default_stack_on('trunk')
3904.3.7 by Andrew Bennetts
Comment the new tests.
614
        # Push rev-2 to a new branch "remote".  It will be stacked on "trunk".
3904.3.2 by Andrew Bennetts
Blackbox test that triggers the bug. Should get replaced with a unit test.
615
        out, err = self.run_bzr('push -d repo/local remote -r 2')
616
        self.assertContainsRe(
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
617
            err, 'Using default stacking branch trunk at .*')
3904.3.7 by Andrew Bennetts
Comment the new tests.
618
        # Push rev-3 onto "remote".  If "remote" not stacked and is missing the
619
        # fulltext record for f-id @ rev-1, then this will fail.
3904.3.2 by Andrew Bennetts
Blackbox test that triggers the bug. Should get replaced with a unit test.
620
        out, err = self.run_bzr('push -d repo/local remote -r 3')
621
3848.1.19 by Aaron Bentley
Show log for non-initial push -v
622
    def test_push_verbose_shows_log(self):
623
        tree = self.make_branch_and_tree('source')
624
        tree.commit('rev1')
625
        out, err = self.run_bzr('push -v -d source target')
626
        # initial push contains log
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
627
        self.assertContainsRe(out, 'rev1')
3848.1.19 by Aaron Bentley
Show log for non-initial push -v
628
        tree.commit('rev2')
629
        out, err = self.run_bzr('push -v -d source target')
630
        # subsequent push contains log
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
631
        self.assertContainsRe(out, 'rev2')
3848.1.19 by Aaron Bentley
Show log for non-initial push -v
632
        # subsequent log is accurate
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
633
        self.assertNotContainsRe(out, 'rev1')
3848.1.19 by Aaron Bentley
Show log for non-initial push -v
634
4453.1.2 by Matthew Fuller
Add a test for pushing from subdir.
635
    def test_push_from_subdir(self):
636
        t = self.make_branch_and_tree('tree')
637
        self.build_tree(['tree/dir/', 'tree/dir/file'])
6973.13.2 by Jelmer Vernooij
Fix some more tests.
638
        t.add(['dir', 'dir/file'])
4453.1.2 by Matthew Fuller
Add a test for pushing from subdir.
639
        t.commit('r1')
640
        out, err = self.run_bzr('push ../../pushloc', working_dir='tree/dir')
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
641
        self.assertEqual('', out)
642
        self.assertEqual('Created new branch.\n', err)
4453.1.2 by Matthew Fuller
Add a test for pushing from subdir.
643
6159.2.1 by Jelmer Vernooij
Add some tests for 'bzr push --overwrite-tags'.
644
    def test_overwrite_tags(self):
645
        """--overwrite-tags only overwrites tags, not revisions."""
646
        from_tree = self.make_branch_and_tree('from')
7027.10.1 by Jelmer Vernooij
Various blackbox test fixes.
647
        from_tree.branch.tags.set_tag("mytag", b"somerevid")
6159.2.1 by Jelmer Vernooij
Add some tests for 'bzr push --overwrite-tags'.
648
        to_tree = self.make_branch_and_tree('to')
7027.10.1 by Jelmer Vernooij
Various blackbox test fixes.
649
        to_tree.branch.tags.set_tag("mytag", b"anotherrevid")
6159.2.1 by Jelmer Vernooij
Add some tests for 'bzr push --overwrite-tags'.
650
        revid1 = to_tree.commit('my commit')
651
        out = self.run_bzr(['push', '-d', 'from', 'to'])
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
652
        self.assertEqual(out,
7143.15.2 by Jelmer Vernooij
Run autopep8.
653
                         ('Conflicting tags:\n    mytag\n', 'No new revisions to push.\n'))
6159.2.1 by Jelmer Vernooij
Add some tests for 'bzr push --overwrite-tags'.
654
        out = self.run_bzr(['push', '-d', 'from', '--overwrite-tags', 'to'])
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
655
        self.assertEqual(out, ('', '1 tag updated.\n'))
656
        self.assertEqual(to_tree.branch.tags.lookup_tag('mytag'),
7143.15.2 by Jelmer Vernooij
Run autopep8.
657
                         b'somerevid')
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
658
        self.assertEqual(to_tree.branch.last_revision(), revid1)
6159.2.1 by Jelmer Vernooij
Add some tests for 'bzr push --overwrite-tags'.
659
3066.3.1 by jml at canonical
Catch redirects raised by mkdir() in the push command. This is primarily
660
4420.1.1 by Vincent Ladeuil
Cleanup imports.
661
class RedirectingMemoryTransport(memory.MemoryTransport):
3066.3.1 by jml at canonical
Catch redirects raised by mkdir() in the push command. This is primarily
662
3878.4.4 by Vincent Ladeuil
Cleanup.
663
    def mkdir(self, relpath, mode=None):
664
        if self._cwd == '/source/':
665
            raise errors.RedirectRequested(self.abspath(relpath),
666
                                           self.abspath('../target'),
667
                                           is_permanent=True)
668
        elif self._cwd == '/infinite-loop/':
669
            raise errors.RedirectRequested(self.abspath(relpath),
670
                                           self.abspath('../infinite-loop'),
671
                                           is_permanent=True)
3066.3.1 by jml at canonical
Catch redirects raised by mkdir() in the push command. This is primarily
672
        else:
673
            return super(RedirectingMemoryTransport, self).mkdir(
3878.4.4 by Vincent Ladeuil
Cleanup.
674
                relpath, mode)
675
4547.2.2 by Andrew Bennetts
Add test for read_mergeable_from_transport raising NotABundle when TooManyRedirections happens.
676
    def get(self, relpath):
677
        if self.clone(relpath)._cwd == '/infinite-loop/':
678
            raise errors.RedirectRequested(self.abspath(relpath),
679
                                           self.abspath('../infinite-loop'),
680
                                           is_permanent=True)
681
        else:
682
            return super(RedirectingMemoryTransport, self).get(relpath)
683
3878.4.5 by Vincent Ladeuil
Don't use the exception as a parameter for _redirected_to.
684
    def _redirected_to(self, source, target):
3878.4.4 by Vincent Ladeuil
Cleanup.
685
        # We do accept redirections
3878.4.5 by Vincent Ladeuil
Don't use the exception as a parameter for _redirected_to.
686
        return transport.get_transport(target)
3066.3.1 by jml at canonical
Catch redirects raised by mkdir() in the push command. This is primarily
687
688
5017.3.45 by Vincent Ladeuil
Move MemoryServer back into bzrlib.transport.memory as it's needed as soon as a MemoryTransport is used. Add a NEWS entry.
689
class RedirectingMemoryServer(memory.MemoryServer):
3066.3.1 by jml at canonical
Catch redirects raised by mkdir() in the push command. This is primarily
690
4934.3.3 by Martin Pool
Rename Server.setUp to Server.start_server
691
    def start_server(self):
3066.3.1 by jml at canonical
Catch redirects raised by mkdir() in the push command. This is primarily
692
        self._dirs = {'/': None}
693
        self._files = {}
694
        self._locks = {}
695
        self._scheme = 'redirecting-memory+%s:///' % id(self)
3878.4.4 by Vincent Ladeuil
Cleanup.
696
        transport.register_transport(self._scheme, self._memory_factory)
3066.3.3 by jml at canonical
Unregister the test transport in order to be clean and to make test_selftest pass.
697
698
    def _memory_factory(self, url):
699
        result = RedirectingMemoryTransport(url)
700
        result._dirs = self._dirs
701
        result._files = self._files
702
        result._locks = self._locks
703
        return result
704
4934.3.1 by Martin Pool
Rename Server.tearDown to .stop_server
705
    def stop_server(self):
3878.4.4 by Vincent Ladeuil
Cleanup.
706
        transport.unregister_transport(self._scheme, self._memory_factory)
3066.3.1 by jml at canonical
Catch redirects raised by mkdir() in the push command. This is primarily
707
708
4420.1.1 by Vincent Ladeuil
Cleanup imports.
709
class TestPushRedirect(tests.TestCaseWithTransport):
3066.3.1 by jml at canonical
Catch redirects raised by mkdir() in the push command. This is primarily
710
711
    def setUp(self):
6552.1.4 by Vincent Ladeuil
Remaining tests matching setup(self) that can be rewritten with super().
712
        super(TestPushRedirect, self).setUp()
3066.3.1 by jml at canonical
Catch redirects raised by mkdir() in the push command. This is primarily
713
        self.memory_server = RedirectingMemoryServer()
4659.1.2 by Robert Collins
Refactor creation and shutdown of test servers to use a common helper,
714
        self.start_server(self.memory_server)
3066.3.2 by jml at canonical
Add tests to check the handling of TooManyRedirections.
715
        # Make the branch and tree that we'll be pushing.
716
        t = self.make_branch_and_tree('tree')
717
        self.build_tree(['tree/file'])
718
        t.add('file')
719
        t.commit('commit 1')
720
3066.3.1 by jml at canonical
Catch redirects raised by mkdir() in the push command. This is primarily
721
    def test_push_redirects_on_mkdir(self):
722
        """If the push requires a mkdir, push respects redirect requests.
723
724
        This is added primarily to handle lp:/ URI support, so that users can
725
        push to new branches by specifying lp:/ URIs.
726
        """
727
        destination_url = self.memory_server.get_url() + 'source'
3878.4.4 by Vincent Ladeuil
Cleanup.
728
        self.run_bzr(['push', '-d', 'tree', destination_url])
3066.3.1 by jml at canonical
Catch redirects raised by mkdir() in the push command. This is primarily
729
4420.1.1 by Vincent Ladeuil
Cleanup imports.
730
        local_revision = branch.Branch.open('tree').last_revision()
731
        remote_revision = branch.Branch.open(
3066.3.1 by jml at canonical
Catch redirects raised by mkdir() in the push command. This is primarily
732
            self.memory_server.get_url() + 'target').last_revision()
733
        self.assertEqual(remote_revision, local_revision)
3066.3.2 by jml at canonical
Add tests to check the handling of TooManyRedirections.
734
735
    def test_push_gracefully_handles_too_many_redirects(self):
736
        """Push fails gracefully if the mkdir generates a large number of
737
        redirects.
738
        """
739
        destination_url = self.memory_server.get_url() + 'infinite-loop'
740
        out, err = self.run_bzr_error(
741
            ['Too many redirections trying to make %s\\.\n'
742
             % re.escape(destination_url)],
3878.4.4 by Vincent Ladeuil
Cleanup.
743
            ['push', '-d', 'tree', destination_url], retcode=3)
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
744
        self.assertEqual('', out)
4420.1.2 by Vincent Ladeuil
Fix bug #284038 by adding a --strict option to push.
745
746
4487.2.6 by Vincent Ladeuil
Fixed as per jam's review.
747
class TestPushStrictMixin(object):
4420.1.2 by Vincent Ladeuil
Fix bug #284038 by adding a --strict option to push.
748
749
    def make_local_branch_and_tree(self):
4487.2.2 by Vincent Ladeuil
Add tests for push --strict against pending merges.
750
        self.tree = self.make_branch_and_tree('local')
6855.4.1 by Jelmer Vernooij
Yet more bees.
751
        self.build_tree_contents([('local/file', b'initial')])
4487.2.2 by Vincent Ladeuil
Add tests for push --strict against pending merges.
752
        self.tree.add('file')
6855.4.1 by Jelmer Vernooij
Yet more bees.
753
        self.tree.commit('adding file', rev_id=b'added')
754
        self.build_tree_contents([('local/file', b'modified')])
755
        self.tree.commit('modify file', rev_id=b'modified')
4487.2.2 by Vincent Ladeuil
Add tests for push --strict against pending merges.
756
757
    def set_config_push_strict(self, value):
6404.6.1 by Vincent Ladeuil
Tests passing for a first rough version of a cached branch config store. The changes here are too invasive and several parallel proposals have been made.
758
        br = branch.Branch.open('local')
6404.6.7 by Vincent Ladeuil
Change set/remove to require a lock for the branch config files.
759
        br.get_config_stack().set('push_strict', value)
4420.1.5 by Vincent Ladeuil
Start implementing jam's review feedback.
760
4487.2.6 by Vincent Ladeuil
Fixed as per jam's review.
761
    _default_command = ['push', '../to']
762
    _default_wd = 'local'
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
763
    _default_errors = ['Working tree ".*/local/" has uncommitted '
7143.15.2 by Jelmer Vernooij
Run autopep8.
764
                       'changes \\(See brz status\\)\\.', ]
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
765
    _default_additional_error = 'Use --no-strict to force the push.\n'
766
    _default_additional_warning = 'Uncommitted changes will not be pushed.'
5171.2.3 by Vincent Ladeuil
Fixed as per Andrew's review.
767
4487.2.2 by Vincent Ladeuil
Add tests for push --strict against pending merges.
768
    def assertPushFails(self, args):
5171.2.3 by Vincent Ladeuil
Fixed as per Andrew's review.
769
        out, err = self.run_bzr_error(self._default_errors,
770
                                      self._default_command + args,
771
                                      working_dir=self._default_wd, retcode=3)
772
        self.assertContainsRe(err, self._default_additional_error)
4420.1.5 by Vincent Ladeuil
Start implementing jam's review feedback.
773
5171.2.3 by Vincent Ladeuil
Fixed as per Andrew's review.
774
    def assertPushSucceeds(self, args, with_warning=False, revid_to_push=None):
5147.2.1 by Vincent Ladeuil
Failing tests for bug #519319.
775
        if with_warning:
776
            error_regexes = self._default_errors
777
        else:
778
            error_regexes = []
5171.2.3 by Vincent Ladeuil
Fixed as per Andrew's review.
779
        out, err = self.run_bzr(self._default_command + args,
780
                                working_dir=self._default_wd,
781
                                error_regexes=error_regexes)
782
        if with_warning:
783
            self.assertContainsRe(err, self._default_additional_warning)
784
        else:
785
            self.assertNotContainsRe(err, self._default_additional_warning)
786
        branch_from = branch.Branch.open(self._default_wd)
787
        if revid_to_push is None:
788
            revid_to_push = branch_from.last_revision()
789
        branch_to = branch.Branch.open('to')
790
        repo_to = branch_to.repository
791
        self.assertTrue(repo_to.has_revision(revid_to_push))
792
        self.assertEqual(revid_to_push, branch_to.last_revision())
4420.1.2 by Vincent Ladeuil
Fix bug #284038 by adding a --strict option to push.
793
4487.2.2 by Vincent Ladeuil
Add tests for push --strict against pending merges.
794
4487.2.6 by Vincent Ladeuil
Fixed as per jam's review.
795
class TestPushStrictWithoutChanges(tests.TestCaseWithTransport,
796
                                   TestPushStrictMixin):
4487.2.2 by Vincent Ladeuil
Add tests for push --strict against pending merges.
797
798
    def setUp(self):
799
        super(TestPushStrictWithoutChanges, self).setUp()
800
        self.make_local_branch_and_tree()
801
802
    def test_push_default(self):
803
        self.assertPushSucceeds([])
804
805
    def test_push_strict(self):
806
        self.assertPushSucceeds(['--strict'])
807
808
    def test_push_no_strict(self):
809
        self.assertPushSucceeds(['--no-strict'])
810
811
    def test_push_config_var_strict(self):
812
        self.set_config_push_strict('true')
813
        self.assertPushSucceeds([])
814
815
    def test_push_config_var_no_strict(self):
816
        self.set_config_push_strict('false')
817
        self.assertPushSucceeds([])
818
819
5559.2.2 by Martin Pool
Change to using standard load_tests_apply_scenarios.
820
strict_push_change_scenarios = [
821
    ('uncommitted',
7143.15.2 by Jelmer Vernooij
Run autopep8.
822
        dict(_changes_type='_uncommitted_changes')),
5559.2.2 by Martin Pool
Change to using standard load_tests_apply_scenarios.
823
    ('pending-merges',
7143.15.2 by Jelmer Vernooij
Run autopep8.
824
        dict(_changes_type='_pending_merges')),
5559.2.2 by Martin Pool
Change to using standard load_tests_apply_scenarios.
825
    ('out-of-sync-trees',
7143.15.2 by Jelmer Vernooij
Run autopep8.
826
        dict(_changes_type='_out_of_sync_trees')),
5559.2.2 by Martin Pool
Change to using standard load_tests_apply_scenarios.
827
    ]
828
829
4487.2.6 by Vincent Ladeuil
Fixed as per jam's review.
830
class TestPushStrictWithChanges(tests.TestCaseWithTransport,
831
                                TestPushStrictMixin):
4487.2.2 by Vincent Ladeuil
Add tests for push --strict against pending merges.
832
7143.15.2 by Jelmer Vernooij
Run autopep8.
833
    scenarios = strict_push_change_scenarios
834
    _changes_type = None  # Set by load_tests
4487.2.2 by Vincent Ladeuil
Add tests for push --strict against pending merges.
835
836
    def setUp(self):
837
        super(TestPushStrictWithChanges, self).setUp()
4721.2.6 by Vincent Ladeuil
More agressive test sharing between push and dpush.
838
        # Apply the changes defined in load_tests: one of _uncommitted_changes,
839
        # _pending_merges or _out_of_sync_trees
4487.2.3 by Vincent Ladeuil
--strict is meaningless when --revision is specified.
840
        getattr(self, self._changes_type)()
4487.2.2 by Vincent Ladeuil
Add tests for push --strict against pending merges.
841
4487.2.3 by Vincent Ladeuil
--strict is meaningless when --revision is specified.
842
    def _uncommitted_changes(self):
4487.2.2 by Vincent Ladeuil
Add tests for push --strict against pending merges.
843
        self.make_local_branch_and_tree()
844
        # Make a change without committing it
6855.4.1 by Jelmer Vernooij
Yet more bees.
845
        self.build_tree_contents([('local/file', b'in progress')])
4487.2.2 by Vincent Ladeuil
Add tests for push --strict against pending merges.
846
4487.2.3 by Vincent Ladeuil
--strict is meaningless when --revision is specified.
847
    def _pending_merges(self):
4487.2.2 by Vincent Ladeuil
Add tests for push --strict against pending merges.
848
        self.make_local_branch_and_tree()
849
        # Create 'other' branch containing a new file
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
850
        other_bzrdir = self.tree.controldir.sprout('other')
4487.2.2 by Vincent Ladeuil
Add tests for push --strict against pending merges.
851
        other_tree = other_bzrdir.open_workingtree()
6855.4.1 by Jelmer Vernooij
Yet more bees.
852
        self.build_tree_contents([('other/other-file', b'other')])
4487.2.2 by Vincent Ladeuil
Add tests for push --strict against pending merges.
853
        other_tree.add('other-file')
6855.4.1 by Jelmer Vernooij
Yet more bees.
854
        other_tree.commit('other commit', rev_id=b'other')
4487.2.2 by Vincent Ladeuil
Add tests for push --strict against pending merges.
855
        # Merge and revert, leaving a pending merge
856
        self.tree.merge_from_branch(other_tree.branch)
857
        self.tree.revert(filenames=['other-file'], backups=False)
858
4487.2.6 by Vincent Ladeuil
Fixed as per jam's review.
859
    def _out_of_sync_trees(self):
860
        self.make_local_branch_and_tree()
861
        self.run_bzr(['checkout', '--lightweight', 'local', 'checkout'])
862
        # Make a change and commit it
6855.4.1 by Jelmer Vernooij
Yet more bees.
863
        self.build_tree_contents([('local/file', b'modified in local')])
864
        self.tree.commit('modify file', rev_id=b'modified-in-local')
4487.2.6 by Vincent Ladeuil
Fixed as per jam's review.
865
        # Exercise commands from the checkout directory
866
        self._default_wd = 'checkout'
867
        self._default_errors = ["Working tree is out of date, please run"
7143.15.2 by Jelmer Vernooij
Run autopep8.
868
                                " 'brz update'\\.", ]
4487.2.6 by Vincent Ladeuil
Fixed as per jam's review.
869
4487.2.2 by Vincent Ladeuil
Add tests for push --strict against pending merges.
870
    def test_push_default(self):
5147.2.1 by Vincent Ladeuil
Failing tests for bug #519319.
871
        self.assertPushSucceeds([], with_warning=True)
4420.1.5 by Vincent Ladeuil
Start implementing jam's review feedback.
872
4487.2.3 by Vincent Ladeuil
--strict is meaningless when --revision is specified.
873
    def test_push_with_revision(self):
7045.3.1 by Jelmer Vernooij
Fix another ~500 tests.
874
        self.assertPushSucceeds(['-r', 'revid:added'], revid_to_push=b'added')
4487.2.3 by Vincent Ladeuil
--strict is meaningless when --revision is specified.
875
876
    def test_push_no_strict(self):
4487.2.2 by Vincent Ladeuil
Add tests for push --strict against pending merges.
877
        self.assertPushSucceeds(['--no-strict'])
4420.1.2 by Vincent Ladeuil
Fix bug #284038 by adding a --strict option to push.
878
879
    def test_push_strict_with_changes(self):
4487.2.2 by Vincent Ladeuil
Add tests for push --strict against pending merges.
880
        self.assertPushFails(['--strict'])
4420.1.5 by Vincent Ladeuil
Start implementing jam's review feedback.
881
882
    def test_push_respect_config_var_strict(self):
4487.2.2 by Vincent Ladeuil
Add tests for push --strict against pending merges.
883
        self.set_config_push_strict('true')
884
        self.assertPushFails([])
4420.1.5 by Vincent Ladeuil
Start implementing jam's review feedback.
885
4420.1.6 by Vincent Ladeuil
Fixed as per John's review feedback.
886
    def test_push_bogus_config_var_ignored(self):
4487.2.2 by Vincent Ladeuil
Add tests for push --strict against pending merges.
887
        self.set_config_push_strict("I don't want you to be strict")
5147.2.1 by Vincent Ladeuil
Failing tests for bug #519319.
888
        self.assertPushSucceeds([], with_warning=True)
4420.1.6 by Vincent Ladeuil
Fixed as per John's review feedback.
889
4420.1.5 by Vincent Ladeuil
Start implementing jam's review feedback.
890
    def test_push_no_strict_command_line_override_config(self):
4487.2.2 by Vincent Ladeuil
Add tests for push --strict against pending merges.
891
        self.set_config_push_strict('yES')
892
        self.assertPushFails([])
893
        self.assertPushSucceeds(['--no-strict'])
4420.1.5 by Vincent Ladeuil
Start implementing jam's review feedback.
894
895
    def test_push_strict_command_line_override_config(self):
4487.2.2 by Vincent Ladeuil
Add tests for push --strict against pending merges.
896
        self.set_config_push_strict('oFF')
897
        self.assertPushFails(['--strict'])
898
        self.assertPushSucceeds([])
4925.1.1 by Jelmer Vernooij
Print a proper error when attempting to push to a foreign VCS for which
899
900
5283.4.5 by Martin Pool
Update remaining subclasses of ExternalBase
901
class TestPushForeign(tests.TestCaseWithTransport):
4925.1.1 by Jelmer Vernooij
Print a proper error when attempting to push to a foreign VCS for which
902
903
    def setUp(self):
904
        super(TestPushForeign, self).setUp()
905
        test_foreign.register_dummy_foreign_for_test(self)
906
907
    def make_dummy_builder(self, relpath):
908
        builder = self.make_branch_builder(
909
            relpath, format=test_foreign.DummyForeignVcsDirFormat())
6816.2.1 by Jelmer Vernooij
Migrate some build_snapshot code over to having revision_id as keyword argument.
910
        builder.build_snapshot(None,
7143.15.2 by Jelmer Vernooij
Run autopep8.
911
                               [('add', ('', b'TREE_ROOT', 'directory', None)),
912
                                ('add', ('foo', b'fooid', 'file', b'bar'))],
913
                               revision_id=b'revid')
4925.1.1 by Jelmer Vernooij
Print a proper error when attempting to push to a foreign VCS for which
914
        return builder
915
916
    def test_no_roundtripping(self):
917
        target_branch = self.make_dummy_builder('dp').get_branch()
918
        source_tree = self.make_branch_and_tree("dc")
919
        output, error = self.run_bzr("push -d dc dp", retcode=3)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
920
        self.assertEqual("", output)
7143.15.10 by Jelmer Vernooij
Merge trunk.
921
        self.assertEqual(
922
            error,
923
            "brz: ERROR: It is not possible to losslessly"
7143.13.2 by Jelmer Vernooij
Fix test.
924
            " push to dummy. You may want to use --lossy.\n")
6175.2.4 by Vincent Ladeuil
Add script tests and documentation.
925
926
927
class TestPushOutput(script.TestCaseWithTransportAndScript):
928
929
    def test_push_log_format(self):
930
        self.run_script("""
6622.1.29 by Jelmer Vernooij
Fix some more tests.
931
            $ brz init trunk
6175.2.4 by Vincent Ladeuil
Add script tests and documentation.
932
            Created a standalone tree (format: 2a)
933
            $ cd trunk
934
            $ echo foo > file
6622.1.29 by Jelmer Vernooij
Fix some more tests.
935
            $ brz add
6175.2.4 by Vincent Ladeuil
Add script tests and documentation.
936
            adding file
6622.1.29 by Jelmer Vernooij
Fix some more tests.
937
            $ brz commit -m 'we need some foo'
6175.2.4 by Vincent Ladeuil
Add script tests and documentation.
938
            2>Committing to:...trunk/
939
            2>added file
940
            2>Committed revision 1.
6622.1.29 by Jelmer Vernooij
Fix some more tests.
941
            $ brz init ../feature
6175.2.4 by Vincent Ladeuil
Add script tests and documentation.
942
            Created a standalone tree (format: 2a)
6622.1.29 by Jelmer Vernooij
Fix some more tests.
943
            $ brz push -v ../feature -Olog_format=line
6175.2.4 by Vincent Ladeuil
Add script tests and documentation.
944
            Added Revisions:
945
            1: jrandom@example.com ...we need some foo
946
            2>All changes applied successfully.
947
            2>Pushed up to revision 1.
948
            """)
6519.3.1 by Neil Martinsen-Burrell
when pushing specific revision, create working tree at that revision
949
950
    def test_push_with_revspec(self):
951
        self.run_script("""
7385.2.1 by Jelmer Vernooij
Rename init-repo to init-shared-repo.
952
            $ brz init-shared-repo .
6519.3.1 by Neil Martinsen-Burrell
when pushing specific revision, create working tree at that revision
953
            Shared repository with trees (format: 2a)
954
            Location:
955
              shared repository: .
6622.1.29 by Jelmer Vernooij
Fix some more tests.
956
            $ brz init trunk
6519.3.1 by Neil Martinsen-Burrell
when pushing specific revision, create working tree at that revision
957
            Created a repository tree (format: 2a)
958
            Using shared repository...
959
            $ cd trunk
6622.1.29 by Jelmer Vernooij
Fix some more tests.
960
            $ brz commit -m 'first rev' --unchanged
6519.3.1 by Neil Martinsen-Burrell
when pushing specific revision, create working tree at that revision
961
            2>Committing to:...trunk/
962
            2>Committed revision 1.
963
            $ echo foo > file
6622.1.29 by Jelmer Vernooij
Fix some more tests.
964
            $ brz add
6519.3.1 by Neil Martinsen-Burrell
when pushing specific revision, create working tree at that revision
965
            adding file
6622.1.29 by Jelmer Vernooij
Fix some more tests.
966
            $ brz commit -m 'we need some foo'
6519.3.1 by Neil Martinsen-Burrell
when pushing specific revision, create working tree at that revision
967
            2>Committing to:...trunk/
968
            2>added file
969
            2>Committed revision 2.
6622.1.29 by Jelmer Vernooij
Fix some more tests.
970
            $ brz push -r 1 ../other
6519.3.1 by Neil Martinsen-Burrell
when pushing specific revision, create working tree at that revision
971
            2>Created new branch.
6622.1.29 by Jelmer Vernooij
Fix some more tests.
972
            $ brz st ../other # checking that file is not created (#484516)
6519.3.1 by Neil Martinsen-Burrell
when pushing specific revision, create working tree at that revision
973
            """)