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 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 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_format(self):
208
s = StringIO(self.run_bzr('send -f branch -o- --format=4')[0])
209
md = merge_directive.MergeDirective.from_lines(s.readlines())
210
self.assertIs(merge_directive.MergeDirective2, md.__class__)
211
s = StringIO(self.run_bzr('send -f branch -o- --format=0.9')[0])
212
md = merge_directive.MergeDirective.from_lines(s.readlines())
213
self.assertContainsRe(md.get_raw_bundle().splitlines()[0],
214
'# Bazaar revision bundle v0.9')
215
s = StringIO(self.run_bzr('bundle -f branch -o- --format=0.9')[0])
216
md = merge_directive.MergeDirective.from_lines(s.readlines())
217
self.assertContainsRe(md.get_raw_bundle().splitlines()[0],
218
'# Bazaar revision bundle v0.9')
219
self.assertIs(merge_directive.MergeDirective, md.__class__)
220
self.run_bzr_error(['Bad value .* for option .format.'],
221
'send -f branch -o- --format=0.999')[0]
223
def test_message_option(self):
225
self.run_bzr('send', retcode=3)
226
md = self.send_directive(['--from', 'branch'])
227
self.assertIs(None, md.message)
228
md = self.send_directive(['--from', 'branch', '-m', 'my message'])
229
self.assertEqual('my message', md.message)
231
def test_omitted_revision(self):
233
md = self.send_directive(['-r-2..', '--from', 'branch'])
234
self.assertEqual('revision2', md.base_revision_id)
235
self.assertEqual('revision3', md.revision_id)
236
md = self.send_directive(['-r..3', '--from', 'branch',
238
self.assertEqual('revision1', md.base_revision_id)
239
self.assertEqual('revision3', md.revision_id)
241
def test_nonexistant_branch(self):
242
if sys.platform == "win32":
243
location = "C:/i/do/not/exist/"
245
location = "/i/do/not/exist/"
246
out, err = self.run_bzr(["send", "--from", location], retcode=3)
247
self.assertEqual(out, '')
248
self.assertEqual(err, 'bzr: ERROR: Not a branch: "%s".\n' % location)