1
# Copyright (C) 2006, 2007 Canonical Ltd
2
# Authors: Aaron Bentley
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
from StringIO import StringIO
24
branch as _mod_branch,
27
from bzrlib.bundle import serializer
28
from bzrlib.bzrdir import BzrDir
29
from bzrlib import tests
32
def read_bundle(fileobj):
33
md = merge_directive.MergeDirective.from_lines(fileobj.readlines())
34
return serializer.read_bundle(StringIO(md.get_raw_bundle()))
37
class TestSend(tests.TestCaseWithTransport):
40
grandparent_tree = BzrDir.create_standalone_workingtree('grandparent')
41
self.build_tree_contents([('grandparent/file1', 'grandparent')])
42
grandparent_tree.add('file1')
43
grandparent_tree.commit('initial commit', rev_id='revision1')
44
parent_bzrdir = grandparent_tree.bzrdir.sprout('parent')
45
parent_tree = parent_bzrdir.open_workingtree()
46
parent_tree.commit('next commit', rev_id='revision2')
47
branch_tree = parent_tree.bzrdir.sprout('branch').open_workingtree()
48
self.build_tree_contents([('branch/file1', 'branch')])
49
branch_tree.commit('last commit', rev_id='revision3')
51
def test_uses_parent(self):
52
"""Parent location is used as a basis by default"""
54
os.chdir('grandparent')
55
errmsg = self.run_bzr('send -o-', retcode=3)[1]
56
self.assertContainsRe(errmsg, 'No submit branch known or specified')
58
stdout, stderr = self.run_bzr('send -o-')
59
self.assertEqual(stderr.count('Using saved parent location'), 1)
60
br = read_bundle(StringIO(stdout))
61
self.assertRevisions(br, ['revision3'])
63
def test_bundle(self):
64
"""Bundle works like send, except -o is not required"""
66
os.chdir('grandparent')
67
errmsg = self.run_bzr('bundle', retcode=3)[1]
68
self.assertContainsRe(errmsg, 'No submit branch known or specified')
70
stdout, stderr = self.run_bzr('bundle')
71
self.assertEqual(stderr.count('Using saved parent location'), 1)
72
br = read_bundle(StringIO(stdout))
73
self.assertRevisions(br, ['revision3'])
75
def assertRevisions(self, bi, expected):
76
self.assertEqual(set(r.revision_id for r in bi.revisions),
79
def test_uses_submit(self):
80
"""Submit location can be used and set"""
83
br = read_bundle(StringIO(self.run_bzr('send -o-')[0]))
84
self.assertRevisions(br, ['revision3'])
85
br = read_bundle(StringIO(self.run_bzr('send ../grandparent -o-')[0]))
86
self.assertRevisions(br, ['revision3', 'revision2'])
87
# submit location should be auto-remembered
88
br = read_bundle(StringIO(self.run_bzr('send -o-')[0]))
89
self.assertRevisions(br, ['revision3', 'revision2'])
90
self.run_bzr('send ../parent -o-')
91
br = read_bundle(StringIO(self.run_bzr('send -o-')[0]))
92
self.assertRevisions(br, ['revision3', 'revision2'])
93
self.run_bzr('send ../parent --remember -o-')
94
br = read_bundle(StringIO(self.run_bzr('send -o-')[0]))
95
self.assertRevisions(br, ['revision3'])
96
err = self.run_bzr('send --remember -o-', retcode=3)[1]
97
self.assertContainsRe(err,
98
'--remember requires a branch to be specified.')
100
def test_revision_branch_interaction(self):
103
bi = read_bundle(StringIO(self.run_bzr('send ../grandparent -o-')[0]))
104
self.assertRevisions(bi, ['revision3', 'revision2'])
105
out = StringIO(self.run_bzr('send ../grandparent -r -2 -o-')[0])
106
bi = read_bundle(out)
107
self.assertRevisions(bi, ['revision2'])
108
sio = StringIO(self.run_bzr('send -r -2..-1 -o-')[0])
109
md = merge_directive.MergeDirective.from_lines(sio.readlines())
110
self.assertEqual('revision2', md.base_revision_id)
111
self.assertEqual('revision3', md.revision_id)
113
bi = read_bundle(sio)
114
self.assertRevisions(bi, ['revision2', 'revision3'])
115
self.run_bzr('send ../grandparent -r -2..-1 -o-')
117
def test_output(self):
118
# check output for consistency
119
# win32 stdout converts LF to CRLF,
120
# which would break patch-based bundles
123
stdout = self.run_bzr_subprocess('send -o-')[0]
124
br = read_bundle(StringIO(stdout))
125
self.assertRevisions(br, ['revision3'])
127
def test_no_common_ancestor(self):
128
foo = self.make_branch_and_tree('foo')
130
bar = self.make_branch_and_tree('bar')
133
self.run_bzr('send ../bar -o-')
135
def send_directive(self, args):
136
sio = StringIO(self.run_bzr(['send', '-o-'] + args)[0])
137
return merge_directive.MergeDirective.from_lines(sio.readlines())
139
def test_content_options(self):
140
"""--no-patch and --no-bundle should work and be independant"""
143
md = self.send_directive([])
144
self.assertIsNot(None, md.bundle)
145
self.assertIsNot(None, md.patch)
147
md = self.send_directive(['--format=0.9'])
148
self.assertIsNot(None, md.bundle)
149
self.assertIsNot(None, md.patch)
151
md = self.send_directive(['--no-patch'])
152
self.assertIsNot(None, md.bundle)
153
self.assertIs(None, md.patch)
154
self.run_bzr_error(['Format 0.9 does not permit bundle with no patch'],
155
'send --no-patch --format=0.9 -o-')
157
md = self.send_directive(['--no-bundle', '.', '.'])
158
self.assertIs(None, md.bundle)
159
self.assertIsNot(None, md.patch)
161
md = self.send_directive(['--no-bundle', '--format=0.9', '../parent',
163
self.assertIs(None, md.bundle)
164
self.assertIsNot(None, md.patch)
166
md = self.send_directive(['--no-bundle', '--no-patch', '.', '.'])
167
self.assertIs(None, md.bundle)
168
self.assertIs(None, md.patch)
170
md = self.send_directive(['--no-bundle', '--no-patch', '--format=0.9',
172
self.assertIs(None, md.bundle)
173
self.assertIs(None, md.patch)
175
def test_from_option(self):
177
self.run_bzr('send', retcode=3)
178
md = self.send_directive(['--from', 'branch'])
179
self.assertEqual('revision3', md.revision_id)
180
md = self.send_directive(['-f', 'branch'])
181
self.assertEqual('revision3', md.revision_id)
183
def test_output_option(self):
185
stdout = self.run_bzr('send -f branch --output file1')[0]
186
self.assertEqual('', stdout)
187
md_file = open('file1', 'rb')
188
self.addCleanup(md_file.close)
189
self.assertContainsRe(md_file.read(), 'revision3')
190
stdout = self.run_bzr('send -f branch --output -')[0]
191
self.assertContainsRe(stdout, 'revision3')
193
def test_mailto_option(self):
195
branch = _mod_branch.Branch.open('branch')
196
branch.get_config().set_user_option('mail_client', 'editor')
197
self.run_bzr_error(('No mail-to address specified',), 'send -f branch')
198
branch.get_config().set_user_option('mail_client', 'bogus')
199
self.run_bzr('send -f branch -o-')
200
self.run_bzr_error(('Unknown mail client: bogus',),
201
'send -f branch --mail-to jrandom@example.org')
202
branch.get_config().set_user_option('submit_to', 'jrandom@example.org')
203
self.run_bzr_error(('Unknown mail client: bogus',),
206
def test_mailto_child_option(self):
207
"""Make sure that child_submit_to is used."""
209
branch = _mod_branch.Branch.open('branch')
210
branch.get_config().set_user_option('mail_client', 'bogus')
211
parent = _mod_branch.Branch.open('parent')
212
parent.get_config().set_user_option('child_submit_to',
213
'somebody@example.org')
214
self.run_bzr_error(('Unknown mail client: bogus',),
217
def test_format(self):
219
s = StringIO(self.run_bzr('send -f branch -o- --format=4')[0])
220
md = merge_directive.MergeDirective.from_lines(s.readlines())
221
self.assertIs(merge_directive.MergeDirective2, md.__class__)
222
s = StringIO(self.run_bzr('send -f branch -o- --format=0.9')[0])
223
md = merge_directive.MergeDirective.from_lines(s.readlines())
224
self.assertContainsRe(md.get_raw_bundle().splitlines()[0],
225
'# Bazaar revision bundle v0.9')
226
s = StringIO(self.run_bzr('bundle -f branch -o- --format=0.9')[0])
227
md = merge_directive.MergeDirective.from_lines(s.readlines())
228
self.assertContainsRe(md.get_raw_bundle().splitlines()[0],
229
'# Bazaar revision bundle v0.9')
230
self.assertIs(merge_directive.MergeDirective, md.__class__)
231
self.run_bzr_error(['Bad value .* for option .format.'],
232
'send -f branch -o- --format=0.999')[0]
234
def test_message_option(self):
236
self.run_bzr('send', retcode=3)
237
md = self.send_directive(['--from', 'branch'])
238
self.assertIs(None, md.message)
239
md = self.send_directive(['--from', 'branch', '-m', 'my message'])
240
self.assertEqual('my message', md.message)
242
def test_omitted_revision(self):
244
md = self.send_directive(['-r-2..', '--from', 'branch'])
245
self.assertEqual('revision2', md.base_revision_id)
246
self.assertEqual('revision3', md.revision_id)
247
md = self.send_directive(['-r..3', '--from', 'branch',
249
self.assertEqual('revision1', md.base_revision_id)
250
self.assertEqual('revision3', md.revision_id)
252
def test_nonexistant_branch(self):
253
if sys.platform == "win32":
254
location = "C:/i/do/not/exist/"
256
location = "/i/do/not/exist/"
257
out, err = self.run_bzr(["send", "--from", location], retcode=3)
258
self.assertEqual(out, '')
259
self.assertEqual(err, 'bzr: ERROR: Not a branch: "%s".\n' % location)