20
21
from StringIO import StringIO
22
from bzrlib.bundle.serializer import read_bundle
24
branch as _mod_branch,
27
from bzrlib.bundle import serializer
23
28
from bzrlib.bzrdir import BzrDir
24
29
from bzrlib import tests
27
class TestBundle(tests.TestCaseWithTransport):
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):
29
39
def make_trees(self):
30
40
grandparent_tree = BzrDir.create_standalone_workingtree('grandparent')
41
self.build_tree_contents([('grandparent/file1', 'grandparent')])
42
grandparent_tree.add('file1')
31
43
grandparent_tree.commit('initial commit', rev_id='revision1')
32
44
parent_bzrdir = grandparent_tree.bzrdir.sprout('parent')
33
45
parent_tree = parent_bzrdir.open_workingtree()
34
46
parent_tree.commit('next commit', rev_id='revision2')
35
47
branch_tree = parent_tree.bzrdir.sprout('branch').open_workingtree()
48
self.build_tree_contents([('branch/file1', 'branch')])
36
49
branch_tree.commit('last commit', rev_id='revision3')
38
51
def test_uses_parent(self):
39
52
"""Parent location is used as a basis by default"""
41
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 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')
42
67
errmsg = self.run_bzr('bundle', retcode=3)[1]
43
self.assertContainsRe(errmsg, 'No base branch known or specified')
68
self.assertContainsRe(errmsg, 'No submit branch known or specified')
44
69
os.chdir('../branch')
45
70
stdout, stderr = self.run_bzr('bundle')
46
self.assertEqual(stderr.count('Using saved location'), 1)
71
self.assertEqual(stderr.count('Using saved parent location'), 1)
47
72
br = read_bundle(StringIO(stdout))
48
73
self.assertRevisions(br, ['revision3'])
50
75
def assertRevisions(self, bi, expected):
51
self.assertEqual([r.revision_id for r in bi.revisions], expected)
76
self.assertEqual(set(r.revision_id for r in bi.revisions),
53
79
def test_uses_submit(self):
54
80
"""Submit location can be used and set"""
57
br = read_bundle(StringIO(self.run_bzr('bundle')[0]))
83
br = read_bundle(StringIO(self.run_bzr('send -o-')[0]))
58
84
self.assertRevisions(br, ['revision3'])
59
br = read_bundle(StringIO(self.run_bzr('bundle ../grandparent')[0]))
85
br = read_bundle(StringIO(self.run_bzr('send ../grandparent -o-')[0]))
60
86
self.assertRevisions(br, ['revision3', 'revision2'])
61
87
# submit location should be auto-remembered
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]))
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]))
69
95
self.assertRevisions(br, ['revision3'])
70
err = self.run_bzr('bundle --remember', retcode=3)[1]
71
self.assertContainsRe(err,
96
err = self.run_bzr('send --remember -o-', retcode=3)[1]
97
self.assertContainsRe(err,
72
98
'--remember requires a branch to be specified.')
74
100
def test_revision_branch_interaction(self):
76
102
os.chdir('branch')
77
bi = read_bundle(StringIO(self.run_bzr('bundle ../grandparent')[0]))
103
bi = read_bundle(StringIO(self.run_bzr('send ../grandparent -o-')[0]))
78
104
self.assertRevisions(bi, ['revision3', 'revision2'])
79
out = StringIO(self.run_bzr('bundle ../grandparent -r -2')[0])
105
out = StringIO(self.run_bzr('send ../grandparent -r -2 -o-')[0])
80
106
bi = read_bundle(out)
81
107
self.assertRevisions(bi, ['revision2'])
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)
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-')
86
117
def test_output(self):
87
118
# check output for consistency
88
119
# win32 stdout converts LF to CRLF,
89
# and this is breaks the created bundle
120
# which would break patch-based bundles
91
122
os.chdir('branch')
92
stdout = self.run_bzr_subprocess('bundle')[0]
123
stdout = self.run_bzr_subprocess('send -o-')[0]
93
124
br = read_bundle(StringIO(stdout))
94
125
self.assertRevisions(br, ['revision3'])
96
127
def test_no_common_ancestor(self):
97
128
foo = self.make_branch_and_tree('foo')
98
130
bar = self.make_branch_and_tree('bar')
100
self.run_bzr('bundle ../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_mailto_child_option(self):
207
"""Make sure that child_submit_to is used."""
209
branch = _mod_branch.Branch.open('branch')
210
branch.get_config().set_user_option('mail_client', 'bogus')
211
parent = _mod_branch.Branch.open('parent')
212
parent.get_config().set_user_option('child_submit_to',
213
'somebody@example.org')
214
self.run_bzr_error(('Unknown mail client: bogus',),
217
def test_format(self):
219
s = StringIO(self.run_bzr('send -f branch -o- --format=4')[0])
220
md = merge_directive.MergeDirective.from_lines(s.readlines())
221
self.assertIs(merge_directive.MergeDirective2, md.__class__)
222
s = StringIO(self.run_bzr('send -f branch -o- --format=0.9')[0])
223
md = merge_directive.MergeDirective.from_lines(s.readlines())
224
self.assertContainsRe(md.get_raw_bundle().splitlines()[0],
225
'# Bazaar revision bundle v0.9')
226
s = StringIO(self.run_bzr('bundle -f branch -o- --format=0.9')[0])
227
md = merge_directive.MergeDirective.from_lines(s.readlines())
228
self.assertContainsRe(md.get_raw_bundle().splitlines()[0],
229
'# Bazaar revision bundle v0.9')
230
self.assertIs(merge_directive.MergeDirective, md.__class__)
231
self.run_bzr_error(['Bad value .* for option .format.'],
232
'send -f branch -o- --format=0.999')[0]
234
def test_message_option(self):
236
self.run_bzr('send', retcode=3)
237
md = self.send_directive(['--from', 'branch'])
238
self.assertIs(None, md.message)
239
md = self.send_directive(['--from', 'branch', '-m', 'my message'])
240
self.assertEqual('my message', md.message)
242
def test_omitted_revision(self):
244
md = self.send_directive(['-r-2..', '--from', 'branch'])
245
self.assertEqual('revision2', md.base_revision_id)
246
self.assertEqual('revision3', md.revision_id)
247
md = self.send_directive(['-r..3', '--from', 'branch',
249
self.assertEqual('revision1', md.base_revision_id)
250
self.assertEqual('revision3', md.revision_id)
252
def test_nonexistant_branch(self):
253
if sys.platform == "win32":
254
location = "C:/i/do/not/exist/"
256
location = "/i/do/not/exist/"
257
out, err = self.run_bzr(["send", "--from", location], retcode=3)
258
self.assertEqual(out, '')
259
self.assertEqual(err, 'bzr: ERROR: Not a branch: "%s".\n' % location)