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
grandparent_tree.commit('initial commit', rev_id='revision1')
38
parent_bzrdir = grandparent_tree.bzrdir.sprout('parent')
39
parent_tree = parent_bzrdir.open_workingtree()
40
parent_tree.commit('next commit', rev_id='revision2')
41
branch_tree = parent_tree.bzrdir.sprout('branch').open_workingtree()
42
branch_tree.commit('last commit', rev_id='revision3')
44
def test_uses_parent(self):
45
"""Parent location is used as a basis by default"""
47
os.chdir('grandparent')
48
errmsg = self.run_bzr('send -o-', retcode=3)[1]
49
self.assertContainsRe(errmsg, 'No submit branch known or specified')
51
stdout, stderr = self.run_bzr('send -o-')
52
self.assertEqual(stderr.count('Using saved location'), 1)
53
br = read_bundle(StringIO(stdout))
54
self.assertRevisions(br, ['revision3'])
56
def assertRevisions(self, bi, expected):
57
self.assertEqual(set(r.revision_id for r in bi.revisions),
60
def test_uses_submit(self):
61
"""Submit location can be used and set"""
64
br = read_bundle(StringIO(self.run_bzr('send -o-')[0]))
65
self.assertRevisions(br, ['revision3'])
66
br = read_bundle(StringIO(self.run_bzr('send ../grandparent -o-')[0]))
67
self.assertRevisions(br, ['revision3', 'revision2'])
68
# submit location should be auto-remembered
69
br = read_bundle(StringIO(self.run_bzr('send -o-')[0]))
70
self.assertRevisions(br, ['revision3', 'revision2'])
71
self.run_bzr('send ../parent -o-')
72
br = read_bundle(StringIO(self.run_bzr('send -o-')[0]))
73
self.assertRevisions(br, ['revision3', 'revision2'])
74
self.run_bzr('send ../parent --remember -o-')
75
br = read_bundle(StringIO(self.run_bzr('send -o-')[0]))
76
self.assertRevisions(br, ['revision3'])
77
err = self.run_bzr('send --remember -o-', retcode=3)[1]
78
self.assertContainsRe(err,
79
'--remember requires a branch to be specified.')
81
def test_revision_branch_interaction(self):
84
bi = read_bundle(StringIO(self.run_bzr('send ../grandparent -o-')[0]))
85
self.assertRevisions(bi, ['revision3', 'revision2'])
86
out = StringIO(self.run_bzr('send ../grandparent -r -2 -o-')[0])
88
self.assertRevisions(bi, ['revision2'])
89
sio = StringIO(self.run_bzr('send -r -2..-1 -o-')[0])
90
md = merge_directive.MergeDirective.from_lines(sio.readlines())
91
self.assertEqual('revision2', md.base_revision_id)
92
self.assertEqual('revision3', md.revision_id)
95
self.assertRevisions(bi, ['revision2', 'revision3'])
96
self.run_bzr('send ../grandparent -r -2..-1 -o-')
98
def test_output(self):
99
# check output for consistency
100
# win32 stdout converts LF to CRLF,
101
# which would break patch-based bundles
104
stdout = self.run_bzr_subprocess('send', '-o-')[0]
105
br = read_bundle(StringIO(stdout))
106
self.assertRevisions(br, ['revision3'])
108
def test_no_common_ancestor(self):
109
foo = self.make_branch_and_tree('foo')
111
bar = self.make_branch_and_tree('bar')
114
self.run_bzr('send ../bar -o-')
116
def send_directive(self, args):
117
sio = StringIO(self.run_bzr(['send', '-o-'] + args)[0])
118
return merge_directive.MergeDirective.from_lines(sio.readlines())
120
def test_content_options(self):
121
"""--no-patch and --no-bundle should work and be independant"""
124
md = self.send_directive([])
125
self.assertIsNot(None, md.bundle)
126
self.assertIsNot(None, md.patch)
127
md = self.send_directive(['--no-patch'])
128
self.assertIsNot(None, md.bundle)
129
self.assertIs(None, md.patch)
130
md = self.send_directive(['--no-bundle', '.', '.'])
131
self.assertIs(None, md.bundle)
132
self.assertIsNot(None, md.patch)
133
md = self.send_directive(['--no-bundle', '--no-patch', '.', '.'])
134
self.assertIs(None, md.bundle)
135
self.assertIs(None, md.patch)
137
def test_from_option(self):
139
self.run_bzr('send', retcode=3)
140
md = self.send_directive(['--from', 'branch'])
141
self.assertEqual('revision3', md.revision_id)
142
md = self.send_directive(['-f', 'branch'])
143
self.assertEqual('revision3', md.revision_id)
145
def test_output_option(self):
147
stdout = self.run_bzr('send -f branch --output file1')[0]
148
self.assertEqual('', stdout)
149
md_file = open('file1', 'rb')
150
self.addCleanup(md_file.close)
151
self.assertContainsRe(md_file.read(), 'revision3')
152
stdout = self.run_bzr('send -f branch --output -')[0]
153
self.assertContainsRe(stdout, 'revision3')
155
def test_output_option_required(self):
157
self.run_bzr_error(('File must be specified with --output',),