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
20
from StringIO import StringIO
23
branch as _mod_branch,
26
from bzrlib.bundle import serializer
27
from bzrlib.bzrdir import BzrDir
28
from bzrlib import tests
31
def read_bundle(fileobj):
32
md = merge_directive.MergeDirective.from_lines(fileobj.readlines())
33
return serializer.read_bundle(StringIO(md.get_raw_bundle()))
36
class TestSend(tests.TestCaseWithTransport):
39
grandparent_tree = BzrDir.create_standalone_workingtree('grandparent')
40
self.build_tree_contents([('grandparent/file1', 'grandparent')])
41
grandparent_tree.add('file1')
42
grandparent_tree.commit('initial commit', rev_id='revision1')
43
parent_bzrdir = grandparent_tree.bzrdir.sprout('parent')
44
parent_tree = parent_bzrdir.open_workingtree()
45
parent_tree.commit('next commit', rev_id='revision2')
46
branch_tree = parent_tree.bzrdir.sprout('branch').open_workingtree()
47
self.build_tree_contents([('branch/file1', 'branch')])
48
branch_tree.commit('last commit', rev_id='revision3')
50
def test_uses_parent(self):
51
"""Parent location is used as a basis by default"""
53
os.chdir('grandparent')
54
errmsg = self.run_bzr('send -o-', retcode=3)[1]
55
self.assertContainsRe(errmsg, 'No submit branch known or specified')
57
stdout, stderr = self.run_bzr('send -o-')
58
self.assertEqual(stderr.count('Using saved location'), 1)
59
br = read_bundle(StringIO(stdout))
60
self.assertRevisions(br, ['revision3'])
62
def test_bundle(self):
63
"""Bundle works like send, except -o is not required"""
65
os.chdir('grandparent')
66
errmsg = self.run_bzr('bundle', retcode=3)[1]
67
self.assertContainsRe(errmsg, 'No submit branch known or specified')
69
stdout, stderr = self.run_bzr('bundle')
70
self.assertEqual(stderr.count('Using saved location'), 1)
71
br = read_bundle(StringIO(stdout))
72
self.assertRevisions(br, ['revision3'])
74
def assertRevisions(self, bi, expected):
75
self.assertEqual(set(r.revision_id for r in bi.revisions),
78
def test_uses_submit(self):
79
"""Submit location can be used and set"""
82
br = read_bundle(StringIO(self.run_bzr('send -o-')[0]))
83
self.assertRevisions(br, ['revision3'])
84
br = read_bundle(StringIO(self.run_bzr('send ../grandparent -o-')[0]))
85
self.assertRevisions(br, ['revision3', 'revision2'])
86
# submit location should be auto-remembered
87
br = read_bundle(StringIO(self.run_bzr('send -o-')[0]))
88
self.assertRevisions(br, ['revision3', 'revision2'])
89
self.run_bzr('send ../parent -o-')
90
br = read_bundle(StringIO(self.run_bzr('send -o-')[0]))
91
self.assertRevisions(br, ['revision3', 'revision2'])
92
self.run_bzr('send ../parent --remember -o-')
93
br = read_bundle(StringIO(self.run_bzr('send -o-')[0]))
94
self.assertRevisions(br, ['revision3'])
95
err = self.run_bzr('send --remember -o-', retcode=3)[1]
96
self.assertContainsRe(err,
97
'--remember requires a branch to be specified.')
99
def test_revision_branch_interaction(self):
102
bi = read_bundle(StringIO(self.run_bzr('send ../grandparent -o-')[0]))
103
self.assertRevisions(bi, ['revision3', 'revision2'])
104
out = StringIO(self.run_bzr('send ../grandparent -r -2 -o-')[0])
105
bi = read_bundle(out)
106
self.assertRevisions(bi, ['revision2'])
107
sio = StringIO(self.run_bzr('send -r -2..-1 -o-')[0])
108
md = merge_directive.MergeDirective.from_lines(sio.readlines())
109
self.assertEqual('revision2', md.base_revision_id)
110
self.assertEqual('revision3', md.revision_id)
112
bi = read_bundle(sio)
113
self.assertRevisions(bi, ['revision2', 'revision3'])
114
self.run_bzr('send ../grandparent -r -2..-1 -o-')
116
def test_output(self):
117
# check output for consistency
118
# win32 stdout converts LF to CRLF,
119
# which would break patch-based bundles
122
stdout = self.run_bzr_subprocess('send -o-')[0]
123
br = read_bundle(StringIO(stdout))
124
self.assertRevisions(br, ['revision3'])
126
def test_no_common_ancestor(self):
127
foo = self.make_branch_and_tree('foo')
129
bar = self.make_branch_and_tree('bar')
132
self.run_bzr('send ../bar -o-')
134
def send_directive(self, args):
135
sio = StringIO(self.run_bzr(['send', '-o-'] + args)[0])
136
return merge_directive.MergeDirective.from_lines(sio.readlines())
138
def test_content_options(self):
139
"""--no-patch and --no-bundle should work and be independant"""
142
md = self.send_directive([])
143
self.assertIsNot(None, md.bundle)
144
self.assertIsNot(None, md.patch)
146
md = self.send_directive(['--format=0.9'])
147
self.assertIsNot(None, md.bundle)
148
self.assertIsNot(None, md.patch)
150
md = self.send_directive(['--no-patch'])
151
self.assertIsNot(None, md.bundle)
152
self.assertIs(None, md.patch)
153
self.run_bzr_error(['Format 0.9 does not permit bundle with no patch'],
154
'send --no-patch --format=0.9 -o-')
156
md = self.send_directive(['--no-bundle', '.', '.'])
157
self.assertIs(None, md.bundle)
158
self.assertIsNot(None, md.patch)
160
md = self.send_directive(['--no-bundle', '--format=0.9', '../parent',
162
self.assertIs(None, md.bundle)
163
self.assertIsNot(None, md.patch)
165
md = self.send_directive(['--no-bundle', '--no-patch', '.', '.'])
166
self.assertIs(None, md.bundle)
167
self.assertIs(None, md.patch)
169
md = self.send_directive(['--no-bundle', '--no-patch', '--format=0.9',
171
self.assertIs(None, md.bundle)
172
self.assertIs(None, md.patch)
174
def test_from_option(self):
176
self.run_bzr('send', retcode=3)
177
md = self.send_directive(['--from', 'branch'])
178
self.assertEqual('revision3', md.revision_id)
179
md = self.send_directive(['-f', 'branch'])
180
self.assertEqual('revision3', md.revision_id)
182
def test_output_option(self):
184
stdout = self.run_bzr('send -f branch --output file1')[0]
185
self.assertEqual('', stdout)
186
md_file = open('file1', 'rb')
187
self.addCleanup(md_file.close)
188
self.assertContainsRe(md_file.read(), 'revision3')
189
stdout = self.run_bzr('send -f branch --output -')[0]
190
self.assertContainsRe(stdout, 'revision3')
192
def test_mailto_option(self):
194
branch = _mod_branch.Branch.open('branch')
195
branch.get_config().set_user_option('mail_client', 'bogus')
196
self.run_bzr_error(('No mail-to address specified',), 'send -f branch')
197
self.run_bzr('send -f branch -o-')
198
self.run_bzr_error(('Unknown mail client: bogus',),
199
'send -f branch --mail-to jrandom@example.org')
200
branch.get_config().set_user_option('submit_to', 'jrandom@example.org')
201
self.run_bzr_error(('Unknown mail client: bogus',),
204
def test_format(self):
206
s = StringIO(self.run_bzr('send -f branch -o- --format=4')[0])
207
md = merge_directive.MergeDirective.from_lines(s.readlines())
208
self.assertIs(merge_directive.MergeDirective2, md.__class__)
209
s = StringIO(self.run_bzr('send -f branch -o- --format=0.9')[0])
210
md = merge_directive.MergeDirective.from_lines(s.readlines())
211
self.assertContainsRe(md.get_raw_bundle().splitlines()[0],
212
'# Bazaar revision bundle v0.9')
213
s = StringIO(self.run_bzr('bundle -f branch -o- --format=0.9')[0])
214
md = merge_directive.MergeDirective.from_lines(s.readlines())
215
self.assertContainsRe(md.get_raw_bundle().splitlines()[0],
216
'# Bazaar revision bundle v0.9')
217
self.assertIs(merge_directive.MergeDirective, md.__class__)
218
self.run_bzr_error(['Bad value .* for option .format.'],
219
'send -f branch -o- --format=0.999')[0]
221
def test_message_option(self):
223
self.run_bzr('send', retcode=3)
224
md = self.send_directive(['--from', 'branch'])
225
self.assertIs(None, md.message)
226
md = self.send_directive(['--from', 'branch', '-m', 'my message'])
227
self.assertEqual('my message', md.message)
229
def test_omitted_revision(self):
231
md = self.send_directive(['-r-2..', '--from', 'branch'])
232
self.assertEqual('revision2', md.base_revision_id)
233
self.assertEqual('revision3', md.revision_id)
234
md = self.send_directive(['-r..3', '--from', 'branch',
236
self.assertEqual('revision1', md.base_revision_id)
237
self.assertEqual('revision3', md.revision_id)