20
20
from StringIO import StringIO
22
from bzrlib.bundle.serializer import read_bundle
23
branch as _mod_branch,
26
from bzrlib.bundle import serializer
23
27
from bzrlib.bzrdir import BzrDir
24
28
from bzrlib import tests
27
class TestBundle(tests.TestCaseWithTransport):
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):
29
38
def make_trees(self):
30
39
grandparent_tree = BzrDir.create_standalone_workingtree('grandparent')
40
self.build_tree_contents([('grandparent/file1', 'grandparent')])
41
grandparent_tree.add('file1')
31
42
grandparent_tree.commit('initial commit', rev_id='revision1')
32
43
parent_bzrdir = grandparent_tree.bzrdir.sprout('parent')
33
44
parent_tree = parent_bzrdir.open_workingtree()
34
45
parent_tree.commit('next commit', rev_id='revision2')
35
46
branch_tree = parent_tree.bzrdir.sprout('branch').open_workingtree()
47
self.build_tree_contents([('branch/file1', 'branch')])
36
48
branch_tree.commit('last commit', rev_id='revision3')
38
50
def test_uses_parent(self):
39
51
"""Parent location is used as a basis by default"""
41
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')
42
66
errmsg = self.run_bzr('bundle', retcode=3)[1]
43
self.assertContainsRe(errmsg, 'No base branch known or specified')
67
self.assertContainsRe(errmsg, 'No submit branch known or specified')
44
68
os.chdir('../branch')
45
69
stdout, stderr = self.run_bzr('bundle')
46
70
self.assertEqual(stderr.count('Using saved location'), 1)
48
72
self.assertRevisions(br, ['revision3'])
50
74
def assertRevisions(self, bi, expected):
51
self.assertEqual([r.revision_id for r in bi.revisions], expected)
75
self.assertEqual(set(r.revision_id for r in bi.revisions),
53
78
def test_uses_submit(self):
54
79
"""Submit location can be used and set"""
57
br = read_bundle(StringIO(self.run_bzr('bundle')[0]))
82
br = read_bundle(StringIO(self.run_bzr('send -o-')[0]))
58
83
self.assertRevisions(br, ['revision3'])
59
br = read_bundle(StringIO(self.run_bzr('bundle ../grandparent')[0]))
84
br = read_bundle(StringIO(self.run_bzr('send ../grandparent -o-')[0]))
60
85
self.assertRevisions(br, ['revision3', 'revision2'])
61
86
# 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]))
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]))
69
94
self.assertRevisions(br, ['revision3'])
70
err = self.run_bzr('bundle --remember', retcode=3)[1]
71
self.assertContainsRe(err,
95
err = self.run_bzr('send --remember -o-', retcode=3)[1]
96
self.assertContainsRe(err,
72
97
'--remember requires a branch to be specified.')
74
99
def test_revision_branch_interaction(self):
76
101
os.chdir('branch')
77
bi = read_bundle(StringIO(self.run_bzr('bundle ../grandparent')[0]))
102
bi = read_bundle(StringIO(self.run_bzr('send ../grandparent -o-')[0]))
78
103
self.assertRevisions(bi, ['revision3', 'revision2'])
79
out = StringIO(self.run_bzr('bundle ../grandparent -r -2')[0])
104
out = StringIO(self.run_bzr('send ../grandparent -r -2 -o-')[0])
80
105
bi = read_bundle(out)
81
106
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)
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-')
86
116
def test_output(self):
87
117
# check output for consistency
88
118
# win32 stdout converts LF to CRLF,
89
# and this is breaks the created bundle
119
# which would break patch-based bundles
91
121
os.chdir('branch')
92
stdout = self.run_bzr_subprocess('bundle')[0]
122
stdout = self.run_bzr_subprocess('send -o-')[0]
93
123
br = read_bundle(StringIO(stdout))
94
124
self.assertRevisions(br, ['revision3'])
96
126
def test_no_common_ancestor(self):
97
127
foo = self.make_branch_and_tree('foo')
98
129
bar = self.make_branch_and_tree('bar')
100
self.run_bzr('bundle ../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)