1
# Copyright (C) 2006-2011 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
from cStringIO import StringIO
27
from bzrlib.bundle import serializer
28
from bzrlib.transport import memory
29
from bzrlib.tests import (
32
from bzrlib.tests.matchers import ContainsNoVfsCalls
35
load_tests = scenarios.load_tests_apply_scenarios
38
class TestSendMixin(object):
40
_default_command = ['send', '-o-']
41
_default_wd = 'branch'
43
def run_send(self, args, cmd=None, rc=0, wd=None, err_re=None):
44
if cmd is None: cmd = self._default_command
45
if wd is None: wd = self._default_wd
46
if err_re is None: err_re = []
47
return self.run_bzr(cmd + args, retcode=rc,
51
def get_MD(self, args, cmd=None, wd='branch'):
52
out = StringIO(self.run_send(args, cmd=cmd, wd=wd)[0])
53
return merge_directive.MergeDirective.from_lines(out)
55
def assertBundleContains(self, revs, args, cmd=None, wd='branch'):
56
md = self.get_MD(args, cmd=cmd, wd=wd)
57
br = serializer.read_bundle(StringIO(md.get_raw_bundle()))
58
self.assertEqual(set(revs), set(r.revision_id for r in br.revisions))
61
class TestSend(tests.TestCaseWithTransport, TestSendMixin):
64
super(TestSend, self).setUp()
65
grandparent_tree = bzrdir.BzrDir.create_standalone_workingtree(
67
self.build_tree_contents([('grandparent/file1', 'grandparent')])
68
grandparent_tree.add('file1')
69
grandparent_tree.commit('initial commit', rev_id='rev1')
71
parent_bzrdir = grandparent_tree.bzrdir.sprout('parent')
72
parent_tree = parent_bzrdir.open_workingtree()
73
parent_tree.commit('next commit', rev_id='rev2')
75
branch_tree = parent_tree.bzrdir.sprout('branch').open_workingtree()
76
self.build_tree_contents([('branch/file1', 'branch')])
77
branch_tree.commit('last commit', rev_id='rev3')
79
def assertFormatIs(self, fmt_string, md):
80
self.assertEqual(fmt_string, md.get_raw_bundle().splitlines()[0])
82
def test_uses_parent(self):
83
"""Parent location is used as a basis by default"""
84
errmsg = self.run_send([], rc=3, wd='grandparent')[1]
85
self.assertContainsRe(errmsg, 'No submit branch known or specified')
86
stdout, stderr = self.run_send([])
87
self.assertEqual(stderr.count('Using saved parent location'), 1)
88
self.assertBundleContains(['rev3'], [])
90
def test_bundle(self):
91
"""Bundle works like send, except -o is not required"""
92
errmsg = self.run_send([], cmd=['bundle'], rc=3, wd='grandparent')[1]
93
self.assertContainsRe(errmsg, 'No submit branch known or specified')
94
stdout, stderr = self.run_send([], cmd=['bundle'])
95
self.assertEqual(stderr.count('Using saved parent location'), 1)
96
self.assertBundleContains(['rev3'], [], cmd=['bundle'])
98
def test_uses_submit(self):
99
"""Submit location can be used and set"""
100
self.assertBundleContains(['rev3'], [])
101
self.assertBundleContains(['rev3', 'rev2'], ['../grandparent'])
102
# submit location should be auto-remembered
103
self.assertBundleContains(['rev3', 'rev2'], [])
105
self.run_send(['../parent'])
106
# We still point to ../grandparent
107
self.assertBundleContains(['rev3', 'rev2'], [])
108
# Remember parent now
109
self.run_send(['../parent', '--remember'])
110
# Now we point to parent
111
self.assertBundleContains(['rev3'], [])
113
err = self.run_send(['--remember'], rc=3)[1]
114
self.assertContainsRe(err,
115
'--remember requires a branch to be specified.')
117
def test_revision_branch_interaction(self):
118
self.assertBundleContains(['rev3', 'rev2'], ['../grandparent'])
119
self.assertBundleContains(['rev2'], ['../grandparent', '-r-2'])
120
self.assertBundleContains(['rev3', 'rev2'],
121
['../grandparent', '-r-2..-1'])
122
md = self.get_MD(['-r-2..-1'])
123
self.assertEqual('rev2', md.base_revision_id)
124
self.assertEqual('rev3', md.revision_id)
126
def test_output(self):
127
# check output for consistency
128
# win32 stdout converts LF to CRLF,
129
# which would break patch-based bundles
130
self.assertBundleContains(['rev3'], [])
132
def test_no_common_ancestor(self):
133
foo = self.make_branch_and_tree('foo')
135
bar = self.make_branch_and_tree('bar')
137
self.run_send(['--from', 'foo', '../bar'], wd='foo')
139
def test_content_options(self):
140
"""--no-patch and --no-bundle should work and be independant"""
142
self.assertIsNot(None, md.bundle)
143
self.assertIsNot(None, md.patch)
145
md = self.get_MD(['--format=0.9'])
146
self.assertIsNot(None, md.bundle)
147
self.assertIsNot(None, md.patch)
149
md = self.get_MD(['--no-patch'])
150
self.assertIsNot(None, md.bundle)
151
self.assertIs(None, md.patch)
152
self.run_bzr_error(['Format 0.9 does not permit bundle with no patch'],
153
['send', '--no-patch', '--format=0.9', '-o-'],
154
working_dir='branch')
155
md = self.get_MD(['--no-bundle', '.', '.'])
156
self.assertIs(None, md.bundle)
157
self.assertIsNot(None, md.patch)
159
md = self.get_MD(['--no-bundle', '--format=0.9', '../parent',
161
self.assertIs(None, md.bundle)
162
self.assertIsNot(None, md.patch)
164
md = self.get_MD(['--no-bundle', '--no-patch', '.', '.'])
165
self.assertIs(None, md.bundle)
166
self.assertIs(None, md.patch)
168
md = self.get_MD(['--no-bundle', '--no-patch', '--format=0.9',
170
self.assertIs(None, md.bundle)
171
self.assertIs(None, md.patch)
173
def test_from_option(self):
174
self.run_bzr('send', retcode=3)
175
md = self.get_MD(['--from', 'branch'])
176
self.assertEqual('rev3', md.revision_id)
177
md = self.get_MD(['-f', 'branch'])
178
self.assertEqual('rev3', md.revision_id)
180
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(), 'rev3')
186
stdout = self.run_bzr('send -f branch --output -')[0]
187
self.assertContainsRe(stdout, 'rev3')
189
def test_note_revisions(self):
190
stderr = self.run_send([])[1]
191
self.assertEndsWith(stderr, '\nBundling 1 revision.\n')
193
def test_mailto_option(self):
194
b = branch.Branch.open('branch')
195
b.get_config().set_user_option('mail_client', 'editor')
197
('No mail-to address \\(--mail-to\\) or output \\(-o\\) specified',
199
b.get_config().set_user_option('mail_client', 'bogus')
201
self.run_bzr_error(('Unknown mail client: bogus',),
202
'send -f branch --mail-to jrandom@example.org')
203
b.get_config().set_user_option('submit_to', 'jrandom@example.org')
204
self.run_bzr_error(('Unknown mail client: bogus',),
207
def test_mailto_child_option(self):
208
"""Make sure that child_submit_to is used."""
209
b = branch.Branch.open('branch')
210
b.get_config().set_user_option('mail_client', 'bogus')
211
parent = 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):
218
md = self.get_MD(['--format=4'])
219
self.assertIs(merge_directive.MergeDirective2, md.__class__)
220
self.assertFormatIs('# Bazaar revision bundle v4', md)
222
md = self.get_MD(['--format=0.9'])
223
self.assertFormatIs('# Bazaar revision bundle v0.9', md)
225
md = self.get_MD(['--format=0.9'], cmd=['bundle'])
226
self.assertFormatIs('# Bazaar revision bundle v0.9', md)
227
self.assertIs(merge_directive.MergeDirective, md.__class__)
229
self.run_bzr_error(['Bad value .* for option .format.'],
230
'send -f branch -o- --format=0.999')[0]
232
def test_format_child_option(self):
233
parent_config = branch.Branch.open('parent').get_config()
234
parent_config.set_user_option('child_submit_format', '4')
236
self.assertIs(merge_directive.MergeDirective2, md.__class__)
238
parent_config.set_user_option('child_submit_format', '0.9')
240
self.assertFormatIs('# Bazaar revision bundle v0.9', md)
242
md = self.get_MD([], cmd=['bundle'])
243
self.assertFormatIs('# Bazaar revision bundle v0.9', md)
244
self.assertIs(merge_directive.MergeDirective, md.__class__)
246
parent_config.set_user_option('child_submit_format', '0.999')
247
self.run_bzr_error(["No such send format '0.999'"],
248
'send -f branch -o-')[0]
250
def test_message_option(self):
251
self.run_bzr('send', retcode=3)
253
self.assertIs(None, md.message)
254
md = self.get_MD(['-m', 'my message'])
255
self.assertEqual('my message', md.message)
257
def test_omitted_revision(self):
258
md = self.get_MD(['-r-2..'])
259
self.assertEqual('rev2', md.base_revision_id)
260
self.assertEqual('rev3', md.revision_id)
261
md = self.get_MD(['-r..3', '--from', 'branch', 'grandparent'], wd='.')
262
self.assertEqual('rev1', md.base_revision_id)
263
self.assertEqual('rev3', md.revision_id)
265
def test_nonexistant_branch(self):
266
self.vfs_transport_factory = memory.MemoryServer
267
location = self.get_url('absentdir/')
268
out, err = self.run_bzr(["send", "--from", location], retcode=3)
269
self.assertEqual(out, '')
270
self.assertEqual(err, 'bzr: ERROR: Not a branch: "%s".\n' % location)
273
class TestSendStrictMixin(TestSendMixin):
275
def make_parent_and_local_branches(self):
276
# Create a 'parent' branch as the base
277
self.parent_tree = bzrdir.BzrDir.create_standalone_workingtree('parent')
278
self.build_tree_contents([('parent/file', 'parent')])
279
self.parent_tree.add('file')
280
self.parent_tree.commit('first commit', rev_id='parent')
281
# Branch 'local' from parent and do a change
282
local_bzrdir = self.parent_tree.bzrdir.sprout('local')
283
self.local_tree = local_bzrdir.open_workingtree()
284
self.build_tree_contents([('local/file', 'local')])
285
self.local_tree.commit('second commit', rev_id='local')
287
_default_command = ['send', '-o-', '../parent']
288
_default_wd = 'local'
289
_default_sent_revs = ['local']
290
_default_errors = ['Working tree ".*/local/" has uncommitted '
291
'changes \(See bzr status\)\.',]
292
_default_additional_error = 'Use --no-strict to force the send.\n'
293
_default_additional_warning = 'Uncommitted changes will not be sent.'
295
def set_config_send_strict(self, value):
296
# set config var (any of bazaar.conf, locations.conf, branch.conf
298
conf = self.local_tree.branch.get_config_stack()
299
conf.set('send_strict', value)
301
def assertSendFails(self, args):
302
out, err = self.run_send(args, rc=3, err_re=self._default_errors)
303
self.assertContainsRe(err, self._default_additional_error)
305
def assertSendSucceeds(self, args, revs=None, with_warning=False):
307
err_re = self._default_errors
311
revs = self._default_sent_revs
312
out, err = self.run_send(args, err_re=err_re)
314
bundling_revs = 'Bundling %d revision.\n'% len(revs)
316
bundling_revs = 'Bundling %d revisions.\n' % len(revs)
318
self.assertContainsRe(err, self._default_additional_warning)
319
self.assertEndsWith(err, bundling_revs)
321
self.assertEquals(bundling_revs, err)
322
md = merge_directive.MergeDirective.from_lines(StringIO(out))
323
self.assertEqual('parent', md.base_revision_id)
324
br = serializer.read_bundle(StringIO(md.get_raw_bundle()))
325
self.assertEqual(set(revs), set(r.revision_id for r in br.revisions))
328
class TestSendStrictWithoutChanges(tests.TestCaseWithTransport,
329
TestSendStrictMixin):
332
super(TestSendStrictWithoutChanges, self).setUp()
333
self.make_parent_and_local_branches()
335
def test_send_default(self):
336
self.assertSendSucceeds([])
338
def test_send_strict(self):
339
self.assertSendSucceeds(['--strict'])
341
def test_send_no_strict(self):
342
self.assertSendSucceeds(['--no-strict'])
344
def test_send_config_var_strict(self):
345
self.set_config_send_strict('true')
346
self.assertSendSucceeds([])
348
def test_send_config_var_no_strict(self):
349
self.set_config_send_strict('false')
350
self.assertSendSucceeds([])
353
class TestSendStrictWithChanges(tests.TestCaseWithTransport,
354
TestSendStrictMixin):
356
# These are textually the same as test_push.strict_push_change_scenarios,
357
# but since the functions are reimplemented here, the definitions are left
361
dict(_changes_type='_uncommitted_changes')),
363
dict(_changes_type='_pending_merges')),
364
('out-of-sync-trees',
365
dict(_changes_type='_out_of_sync_trees')),
368
_changes_type = None # Set by load_tests
371
super(TestSendStrictWithChanges, self).setUp()
372
# load tests set _changes_types to the name of the method we want to
374
do_changes_func = getattr(self, self._changes_type)
377
def _uncommitted_changes(self):
378
self.make_parent_and_local_branches()
379
# Make a change without committing it
380
self.build_tree_contents([('local/file', 'modified')])
382
def _pending_merges(self):
383
self.make_parent_and_local_branches()
384
# Create 'other' branch containing a new file
385
other_bzrdir = self.parent_tree.bzrdir.sprout('other')
386
other_tree = other_bzrdir.open_workingtree()
387
self.build_tree_contents([('other/other-file', 'other')])
388
other_tree.add('other-file')
389
other_tree.commit('other commit', rev_id='other')
390
# Merge and revert, leaving a pending merge
391
self.local_tree.merge_from_branch(other_tree.branch)
392
self.local_tree.revert(filenames=['other-file'], backups=False)
394
def _out_of_sync_trees(self):
395
self.make_parent_and_local_branches()
396
self.run_bzr(['checkout', '--lightweight', 'local', 'checkout'])
397
# Make a change and commit it
398
self.build_tree_contents([('local/file', 'modified in local')])
399
self.local_tree.commit('modify file', rev_id='modified-in-local')
400
# Exercise commands from the checkout directory
401
self._default_wd = 'checkout'
402
self._default_errors = ["Working tree is out of date, please run"
404
self._default_sent_revs = ['modified-in-local', 'local']
406
def test_send_default(self):
407
self.assertSendSucceeds([], with_warning=True)
409
def test_send_with_revision(self):
410
self.assertSendSucceeds(['-r', 'revid:local'], revs=['local'])
412
def test_send_no_strict(self):
413
self.assertSendSucceeds(['--no-strict'])
415
def test_send_strict_with_changes(self):
416
self.assertSendFails(['--strict'])
418
def test_send_respect_config_var_strict(self):
419
self.set_config_send_strict('true')
420
self.assertSendFails([])
421
self.assertSendSucceeds(['--no-strict'])
423
def test_send_bogus_config_var_ignored(self):
424
self.set_config_send_strict("I'm unsure")
425
self.assertSendSucceeds([], with_warning=True)
427
def test_send_no_strict_command_line_override_config(self):
428
self.set_config_send_strict('true')
429
self.assertSendFails([])
430
self.assertSendSucceeds(['--no-strict'])
432
def test_send_strict_command_line_override_config(self):
433
self.set_config_send_strict('false')
434
self.assertSendSucceeds([])
435
self.assertSendFails(['--strict'])
438
class TestBundleStrictWithoutChanges(TestSendStrictWithoutChanges):
440
_default_command = ['bundle-revisions', '../parent']
443
class TestSmartServerSend(tests.TestCaseWithTransport):
446
self.setup_smart_server_with_call_log()
447
t = self.make_branch_and_tree('branch')
448
self.build_tree_contents([('branch/foo', 'thecontents')])
451
local = t.bzrdir.sprout('local-branch').open_workingtree()
452
self.build_tree_contents([('branch/foo', 'thenewcontents')])
453
local.commit("anothermessage")
454
self.reset_smart_call_log()
455
out, err = self.run_bzr(
456
['send', '-o', 'x.diff', self.get_url('branch')], working_dir='local-branch')
457
# This figure represent the amount of work to perform this use case. It
458
# is entirely ok to reduce this number if a test fails due to rpc_count
459
# being too low. If rpc_count increases, more network roundtrips have
460
# become necessary for this use case. Please do not adjust this number
461
# upwards without agreement from bzr's network support maintainers.
462
self.assertLength(9, self.hpss_calls)
463
self.assertThat(self.hpss_calls, ContainsNoVfsCalls)