14
14
# You should have received a copy of the GNU General Public License
15
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
20
from StringIO import StringIO
24
branch as _mod_branch,
27
from bzrlib.bundle import serializer
22
from bzrlib.bundle.serializer import read_bundle
28
23
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):
24
from bzrlib.tests import TestCaseInTempDir
27
class TestBundle(TestCaseInTempDir):
39
29
def make_trees(self):
40
30
grandparent_tree = BzrDir.create_standalone_workingtree('grandparent')
41
self.build_tree_contents([('grandparent/file1', 'grandparent')])
42
grandparent_tree.add('file1')
43
31
grandparent_tree.commit('initial commit', rev_id='revision1')
44
32
parent_bzrdir = grandparent_tree.bzrdir.sprout('parent')
45
33
parent_tree = parent_bzrdir.open_workingtree()
46
34
parent_tree.commit('next commit', rev_id='revision2')
47
35
branch_tree = parent_tree.bzrdir.sprout('branch').open_workingtree()
48
self.build_tree_contents([('branch/file1', 'branch')])
49
36
branch_tree.commit('last commit', rev_id='revision3')
51
38
def test_uses_parent(self):
52
39
"""Parent location is used as a basis by default"""
54
41
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
42
errmsg = self.run_bzr('bundle', retcode=3)[1]
68
self.assertContainsRe(errmsg, 'No submit branch known or specified')
43
self.assertContainsRe(errmsg, 'No base branch known or specified')
69
44
os.chdir('../branch')
70
45
stdout, stderr = self.run_bzr('bundle')
71
self.assertEqual(stderr.count('Using saved parent location'), 1)
46
self.assertEqual(stderr.count('Using saved location'), 1)
72
47
br = read_bundle(StringIO(stdout))
73
48
self.assertRevisions(br, ['revision3'])
75
50
def assertRevisions(self, bi, expected):
76
self.assertEqual(set(r.revision_id for r in bi.revisions),
51
self.assertEqual([r.revision_id for r in bi.revisions], expected)
79
53
def test_uses_submit(self):
80
54
"""Submit location can be used and set"""
83
br = read_bundle(StringIO(self.run_bzr('send -o-')[0]))
57
br = read_bundle(StringIO(self.run_bzr('bundle')[0]))
84
58
self.assertRevisions(br, ['revision3'])
85
br = read_bundle(StringIO(self.run_bzr('send ../grandparent -o-')[0]))
59
br = read_bundle(StringIO(self.run_bzr('bundle', '../grandparent')[0]))
86
60
self.assertRevisions(br, ['revision3', 'revision2'])
87
61
# 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]))
62
br = read_bundle(StringIO(self.run_bzr('bundle')[0]))
63
self.assertRevisions(br, ['revision3', 'revision2'])
64
self.run_bzr('bundle', '../parent')
65
br = read_bundle(StringIO(self.run_bzr('bundle')[0]))
66
self.assertRevisions(br, ['revision3', 'revision2'])
67
self.run_bzr('bundle', '../parent', '--remember')
68
br = read_bundle(StringIO(self.run_bzr('bundle')[0]))
95
69
self.assertRevisions(br, ['revision3'])
96
err = self.run_bzr('send --remember -o-', retcode=3)[1]
97
self.assertContainsRe(err,
70
err = self.run_bzr('bundle', '--remember', retcode=3)[1]
71
self.assertContainsRe(err,
98
72
'--remember requires a branch to be specified.')
100
74
def test_revision_branch_interaction(self):
102
76
os.chdir('branch')
103
bi = read_bundle(StringIO(self.run_bzr('send ../grandparent -o-')[0]))
77
bi = read_bundle(StringIO(self.run_bzr('bundle', '../grandparent')[0]))
104
78
self.assertRevisions(bi, ['revision3', 'revision2'])
105
out = StringIO(self.run_bzr('send ../grandparent -r -2 -o-')[0])
79
out = StringIO(self.run_bzr('bundle', '../grandparent', '-r', '-2')[0])
106
80
bi = read_bundle(out)
107
81
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-')
82
bi = read_bundle(StringIO(self.run_bzr('bundle', '-r', '-2..-1')[0]))
83
self.assertRevisions(bi, ['revision3'])
84
self.run_bzr('bundle', '../grandparent', '-r', '-2..-1', retcode=3)
117
86
def test_output(self):
118
87
# check output for consistency
119
88
# win32 stdout converts LF to CRLF,
120
# which would break patch-based bundles
89
# and this is breaks the created bundle
122
91
os.chdir('branch')
123
stdout = self.run_bzr_subprocess('send -o-')[0]
92
stdout = self.run_bzr_subprocess('bundle')[0]
124
93
br = read_bundle(StringIO(stdout))
125
94
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_note_revisions(self):
195
stderr = self.run_bzr('send -f branch --output file1')[1]
196
self.assertContainsRe(stderr, r'Bundling 1 revision\(s\).\n')
198
def test_mailto_option(self):
200
branch = _mod_branch.Branch.open('branch')
201
branch.get_config().set_user_option('mail_client', 'editor')
203
('No mail-to address \\(--mail-to\\) or output \\(-o\\) specified',
205
branch.get_config().set_user_option('mail_client', 'bogus')
206
self.run_bzr('send -f branch -o-')
207
self.run_bzr_error(('Unknown mail client: bogus',),
208
'send -f branch --mail-to jrandom@example.org')
209
branch.get_config().set_user_option('submit_to', 'jrandom@example.org')
210
self.run_bzr_error(('Unknown mail client: bogus',),
213
def test_mailto_child_option(self):
214
"""Make sure that child_submit_to is used."""
216
branch = _mod_branch.Branch.open('branch')
217
branch.get_config().set_user_option('mail_client', 'bogus')
218
parent = _mod_branch.Branch.open('parent')
219
parent.get_config().set_user_option('child_submit_to',
220
'somebody@example.org')
221
self.run_bzr_error(('Unknown mail client: bogus',),
224
def test_format(self):
226
s = StringIO(self.run_bzr('send -f branch -o- --format=4')[0])
227
md = merge_directive.MergeDirective.from_lines(s.readlines())
228
self.assertIs(merge_directive.MergeDirective2, md.__class__)
229
s = StringIO(self.run_bzr('send -f branch -o- --format=0.9')[0])
230
md = merge_directive.MergeDirective.from_lines(s.readlines())
231
self.assertContainsRe(md.get_raw_bundle().splitlines()[0],
232
'# Bazaar revision bundle v0.9')
233
s = StringIO(self.run_bzr('bundle -f branch -o- --format=0.9')[0])
234
md = merge_directive.MergeDirective.from_lines(s.readlines())
235
self.assertContainsRe(md.get_raw_bundle().splitlines()[0],
236
'# Bazaar revision bundle v0.9')
237
self.assertIs(merge_directive.MergeDirective, md.__class__)
238
self.run_bzr_error(['Bad value .* for option .format.'],
239
'send -f branch -o- --format=0.999')[0]
241
def test_message_option(self):
243
self.run_bzr('send', retcode=3)
244
md = self.send_directive(['--from', 'branch'])
245
self.assertIs(None, md.message)
246
md = self.send_directive(['--from', 'branch', '-m', 'my message'])
247
self.assertEqual('my message', md.message)
249
def test_omitted_revision(self):
251
md = self.send_directive(['-r-2..', '--from', 'branch'])
252
self.assertEqual('revision2', md.base_revision_id)
253
self.assertEqual('revision3', md.revision_id)
254
md = self.send_directive(['-r..3', '--from', 'branch',
256
self.assertEqual('revision1', md.base_revision_id)
257
self.assertEqual('revision3', md.revision_id)
259
def test_nonexistant_branch(self):
260
if sys.platform == "win32":
261
location = "C:/i/do/not/exist/"
263
location = "/i/do/not/exist/"
264
out, err = self.run_bzr(["send", "--from", location], retcode=3)
265
self.assertEqual(out, '')
266
self.assertEqual(err, 'bzr: ERROR: Not a branch: "%s".\n' % location)