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
22
from bzrlib import merge_directive
23
from bzrlib.bundle import serializer
24
from bzrlib.bzrdir import BzrDir
25
from bzrlib import tests
28
def read_bundle(fileobj):
29
md = merge_directive.MergeDirective.from_lines(fileobj.readlines())
30
return serializer.read_bundle(StringIO(md.get_raw_bundle()))
33
class TestSend(tests.TestCaseWithTransport):
36
grandparent_tree = BzrDir.create_standalone_workingtree('grandparent')
37
self.build_tree_contents([('grandparent/file1', 'grandparent')])
38
grandparent_tree.add('file1')
39
grandparent_tree.commit('initial commit', rev_id='revision1')
40
parent_bzrdir = grandparent_tree.bzrdir.sprout('parent')
41
parent_tree = parent_bzrdir.open_workingtree()
42
parent_tree.commit('next commit', rev_id='revision2')
43
branch_tree = parent_tree.bzrdir.sprout('branch').open_workingtree()
44
self.build_tree_contents([('branch/file1', 'branch')])
45
branch_tree.commit('last commit', rev_id='revision3')
47
def test_uses_parent(self):
48
"""Parent location is used as a basis by default"""
50
os.chdir('grandparent')
51
errmsg = self.run_bzr('send -o-', retcode=3)[1]
52
self.assertContainsRe(errmsg, 'No submit branch known or specified')
54
stdout, stderr = self.run_bzr('send -o-')
55
self.assertEqual(stderr.count('Using saved location'), 1)
56
br = read_bundle(StringIO(stdout))
57
self.assertRevisions(br, ['revision3'])
59
def test_bundle(self):
60
"""Bundle works like send, except -o is not required"""
62
os.chdir('grandparent')
63
errmsg = self.run_bzr('bundle', retcode=3)[1]
64
self.assertContainsRe(errmsg, 'No submit branch known or specified')
66
stdout, stderr = self.run_bzr('bundle')
67
self.assertEqual(stderr.count('Using saved location'), 1)
68
br = read_bundle(StringIO(stdout))
69
self.assertRevisions(br, ['revision3'])
71
def assertRevisions(self, bi, expected):
72
self.assertEqual(set(r.revision_id for r in bi.revisions),
75
def test_uses_submit(self):
76
"""Submit location can be used and set"""
79
br = read_bundle(StringIO(self.run_bzr('send -o-')[0]))
80
self.assertRevisions(br, ['revision3'])
81
br = read_bundle(StringIO(self.run_bzr('send ../grandparent -o-')[0]))
82
self.assertRevisions(br, ['revision3', 'revision2'])
83
# submit location should be auto-remembered
84
br = read_bundle(StringIO(self.run_bzr('send -o-')[0]))
85
self.assertRevisions(br, ['revision3', 'revision2'])
86
self.run_bzr('send ../parent -o-')
87
br = read_bundle(StringIO(self.run_bzr('send -o-')[0]))
88
self.assertRevisions(br, ['revision3', 'revision2'])
89
self.run_bzr('send ../parent --remember -o-')
90
br = read_bundle(StringIO(self.run_bzr('send -o-')[0]))
91
self.assertRevisions(br, ['revision3'])
92
err = self.run_bzr('send --remember -o-', retcode=3)[1]
93
self.assertContainsRe(err,
94
'--remember requires a branch to be specified.')
96
def test_revision_branch_interaction(self):
99
bi = read_bundle(StringIO(self.run_bzr('send ../grandparent -o-')[0]))
100
self.assertRevisions(bi, ['revision3', 'revision2'])
101
out = StringIO(self.run_bzr('send ../grandparent -r -2 -o-')[0])
102
bi = read_bundle(out)
103
self.assertRevisions(bi, ['revision2'])
104
sio = StringIO(self.run_bzr('send -r -2..-1 -o-')[0])
105
md = merge_directive.MergeDirective.from_lines(sio.readlines())
106
self.assertEqual('revision2', md.base_revision_id)
107
self.assertEqual('revision3', md.revision_id)
109
bi = read_bundle(sio)
110
self.assertRevisions(bi, ['revision2', 'revision3'])
111
self.run_bzr('send ../grandparent -r -2..-1 -o-')
113
def test_output(self):
114
# check output for consistency
115
# win32 stdout converts LF to CRLF,
116
# which would break patch-based bundles
119
stdout = self.run_bzr_subprocess('send', '-o-')[0]
120
br = read_bundle(StringIO(stdout))
121
self.assertRevisions(br, ['revision3'])
123
def test_no_common_ancestor(self):
124
foo = self.make_branch_and_tree('foo')
126
bar = self.make_branch_and_tree('bar')
129
self.run_bzr('send ../bar -o-')
131
def send_directive(self, args):
132
sio = StringIO(self.run_bzr(['send', '-o-'] + args)[0])
133
return merge_directive.MergeDirective.from_lines(sio.readlines())
135
def test_content_options(self):
136
"""--no-patch and --no-bundle should work and be independant"""
139
md = self.send_directive([])
140
self.assertIsNot(None, md.bundle)
141
self.assertIsNot(None, md.patch)
143
md = self.send_directive(['--format=0.9'])
144
self.assertIsNot(None, md.bundle)
145
self.assertIsNot(None, md.patch)
147
md = self.send_directive(['--no-patch'])
148
self.assertIsNot(None, md.bundle)
149
self.assertIs(None, md.patch)
150
self.run_bzr_error(['Format 0.9 does not permit bundle with no patch'],
151
'send --no-patch --format=0.9 -o-')
153
md = self.send_directive(['--no-bundle', '.', '.'])
154
self.assertIs(None, md.bundle)
155
self.assertIsNot(None, md.patch)
157
md = self.send_directive(['--no-bundle', '--format=0.9', '../parent',
159
self.assertIs(None, md.bundle)
160
self.assertIsNot(None, md.patch)
162
md = self.send_directive(['--no-bundle', '--no-patch', '.', '.'])
163
self.assertIs(None, md.bundle)
164
self.assertIs(None, md.patch)
166
md = self.send_directive(['--no-bundle', '--no-patch', '--format=0.9',
168
self.assertIs(None, md.bundle)
169
self.assertIs(None, md.patch)
171
def test_from_option(self):
173
self.run_bzr('send', retcode=3)
174
md = self.send_directive(['--from', 'branch'])
175
self.assertEqual('revision3', md.revision_id)
176
md = self.send_directive(['-f', 'branch'])
177
self.assertEqual('revision3', md.revision_id)
179
def test_output_option(self):
181
stdout = self.run_bzr('send -f branch --output file1')[0]
182
self.assertEqual('', stdout)
183
md_file = open('file1', 'rb')
184
self.addCleanup(md_file.close)
185
self.assertContainsRe(md_file.read(), 'revision3')
186
stdout = self.run_bzr('send -f branch --output -')[0]
187
self.assertContainsRe(stdout, 'revision3')
189
def test_output_option_required(self):
191
self.run_bzr_error(('File must be specified with --output',),
194
def test_format(self):
196
s = StringIO(self.run_bzr('send -f branch -o- --format=4')[0])
197
md = merge_directive.MergeDirective.from_lines(s.readlines())
198
self.assertIs(merge_directive.MergeDirective2, md.__class__)
199
s = StringIO(self.run_bzr('send -f branch -o- --format=0.9')[0])
200
md = merge_directive.MergeDirective.from_lines(s.readlines())
201
self.assertContainsRe(md.get_raw_bundle().splitlines()[0],
202
'# Bazaar revision bundle v0.9')
203
s = StringIO(self.run_bzr('bundle -f branch -o- --format=0.9')[0])
204
md = merge_directive.MergeDirective.from_lines(s.readlines())
205
self.assertContainsRe(md.get_raw_bundle().splitlines()[0],
206
'# Bazaar revision bundle v0.9')
207
self.assertIs(merge_directive.MergeDirective, md.__class__)
208
self.run_bzr_error(['Bad value .* for option .format.'],
209
'send -f branch -o- --format=0.999')[0]