28
28
from bzrlib.bundle import serializer
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):
31
def load_tests(standard_tests, module, loader):
32
"""Multiply tests for the send command."""
33
result = loader.suiteClass()
35
# one for each king of change
36
changes_tests, remaining_tests = tests.split_suite_by_condition(
37
standard_tests, tests.condition_isinstance((
38
TestSendStrictWithChanges,
42
dict(_changes_type='_uncommitted_changes')),
44
dict(_changes_type='_pending_merges')),
46
dict(_changes_type='_out_of_sync_trees')),
48
tests.multiply_tests(changes_tests, changes_scenarios, result)
49
# No parametrization for the remaining tests
50
result.addTests(remaining_tests)
55
class TestSendMixin(object):
57
_default_command = ['send', '-o-']
58
_default_wd = 'branch'
60
def run_send(self, args, cmd=None, rc=0, wd=None, err_re=None):
61
if cmd is None: cmd = self._default_command
62
if wd is None: wd = self._default_wd
63
if err_re is None: err_re = []
64
return self.run_bzr(cmd + args, retcode=rc,
68
def get_MD(self, args, cmd=None, wd='branch'):
69
out = StringIO(self.run_send(args, cmd=cmd, wd=wd)[0])
70
return merge_directive.MergeDirective.from_lines(out.readlines())
72
def assertBundleContains(self, revs, args, cmd=None, wd='branch'):
73
md = self.get_MD(args, cmd=cmd, wd=wd)
74
br = serializer.read_bundle(StringIO(md.get_raw_bundle()))
75
self.assertEqual(set(revs), set(r.revision_id for r in br.revisions))
78
class TestSend(tests.TestCaseWithTransport, TestSendMixin):
39
81
super(TestSend, self).setUp()
51
93
self.build_tree_contents([('branch/file1', 'branch')])
52
94
branch_tree.commit('last commit', rev_id='rev3')
54
def run_send(self, args, cmd=None, rc=0, wd='branch'):
57
return self.run_bzr(cmd + args, retcode=rc, working_dir=wd)
59
def get_MD(self, args, cmd=None, wd='branch'):
60
out = StringIO(self.run_send(args, cmd=cmd, wd=wd)[0])
61
return merge_directive.MergeDirective.from_lines(out.readlines())
63
def get_bundle(self, args, cmd=None, wd='branch'):
64
md = self.get_MD(args, cmd=cmd, wd=wd)
65
return serializer.read_bundle(StringIO(md.get_raw_bundle()))
67
def assertBundleContains(self, revs, args, cmd=None, wd='branch'):
68
out = self.run_send(args, cmd=cmd, wd=wd)[0]
69
br = read_bundle(StringIO(out))
70
self.assertEqual(set(revs), set(r.revision_id for r in br.revisions))
72
96
def assertFormatIs(self, fmt_string, md):
73
97
self.assertEqual(fmt_string, md.get_raw_bundle().splitlines()[0])
263
287
out, err = self.run_bzr(["send", "--from", location], retcode=3)
264
288
self.assertEqual(out, '')
265
289
self.assertEqual(err, 'bzr: ERROR: Not a branch: "%s".\n' % location)
292
class TestSendStrictMixin(TestSendMixin):
294
def make_parent_and_local_branches(self):
295
# Create a 'parent' branch as the base
296
self.parent_tree = bzrdir.BzrDir.create_standalone_workingtree('parent')
297
self.build_tree_contents([('parent/file', 'parent')])
298
self.parent_tree.add('file')
299
self.parent_tree.commit('first commit', rev_id='parent')
300
# Branch 'local' from parent and do a change
301
local_bzrdir = self.parent_tree.bzrdir.sprout('local')
302
self.local_tree = local_bzrdir.open_workingtree()
303
self.build_tree_contents([('local/file', 'local')])
304
self.local_tree.commit('second commit', rev_id='local')
306
_default_command = ['send', '-o-', '../parent']
307
_default_wd = 'local'
308
_default_sent_revs = ['local']
309
_default_errors = ['Working tree ".*/local/" has uncommitted '
310
'changes \(See bzr status\)\.',]
312
def set_config_send_strict(self, value):
313
# set config var (any of bazaar.conf, locations.conf, branch.conf
315
conf = self.local_tree.branch.get_config()
316
conf.set_user_option('send_strict', value)
318
def assertSendFails(self, args):
319
self.run_send(args, rc=3, err_re=self._default_errors)
321
def assertSendSucceeds(self, args, revs=None):
323
revs = self._default_sent_revs
324
out, err = self.run_send(args)
326
'Bundling %d revision(s).\n' % len(revs), err)
327
md = merge_directive.MergeDirective.from_lines(
328
StringIO(out).readlines())
329
self.assertEqual('parent', md.base_revision_id)
330
br = serializer.read_bundle(StringIO(md.get_raw_bundle()))
331
self.assertEqual(set(revs), set(r.revision_id for r in br.revisions))
334
class TestSendStrictWithoutChanges(tests.TestCaseWithTransport,
335
TestSendStrictMixin):
338
super(TestSendStrictWithoutChanges, self).setUp()
339
self.make_parent_and_local_branches()
341
def test_send_default(self):
342
self.assertSendSucceeds([])
344
def test_send_strict(self):
345
self.assertSendSucceeds(['--strict'])
347
def test_send_no_strict(self):
348
self.assertSendSucceeds(['--no-strict'])
350
def test_send_config_var_strict(self):
351
self.set_config_send_strict('true')
352
self.assertSendSucceeds([])
354
def test_send_config_var_no_strict(self):
355
self.set_config_send_strict('false')
356
self.assertSendSucceeds([])
359
class TestSendStrictWithChanges(tests.TestCaseWithTransport,
360
TestSendStrictMixin):
362
_changes_type = None # Set by load_tests
365
super(TestSendStrictWithChanges, self).setUp()
366
# load tests set _changes_types to the name of the method we want to
368
do_changes_func = getattr(self, self._changes_type)
371
def _uncommitted_changes(self):
372
self.make_parent_and_local_branches()
373
# Make a change without committing it
374
self.build_tree_contents([('local/file', 'modified')])
376
def _pending_merges(self):
377
self.make_parent_and_local_branches()
378
# Create 'other' branch containing a new file
379
other_bzrdir = self.parent_tree.bzrdir.sprout('other')
380
other_tree = other_bzrdir.open_workingtree()
381
self.build_tree_contents([('other/other-file', 'other')])
382
other_tree.add('other-file')
383
other_tree.commit('other commit', rev_id='other')
384
# Merge and revert, leaving a pending merge
385
self.local_tree.merge_from_branch(other_tree.branch)
386
self.local_tree.revert(filenames=['other-file'], backups=False)
388
def _out_of_sync_trees(self):
389
self.make_parent_and_local_branches()
390
self.run_bzr(['checkout', '--lightweight', 'local', 'checkout'])
391
# Make a change and commit it
392
self.build_tree_contents([('local/file', 'modified in local')])
393
self.local_tree.commit('modify file', rev_id='modified-in-local')
394
# Exercise commands from the checkout directory
395
self._default_wd = 'checkout'
396
self._default_errors = ["Working tree is out of date, please run"
398
self._default_sent_revs = ['modified-in-local', 'local']
400
def test_send_default(self):
401
self.assertSendFails([])
403
def test_send_with_revision(self):
404
self.assertSendSucceeds(['-r', 'revid:local'], revs=['local'])
406
def test_send_no_strict(self):
407
self.assertSendSucceeds(['--no-strict'])
409
def test_send_strict_with_changes(self):
410
self.assertSendFails(['--strict'])
412
def test_send_respect_config_var_strict(self):
413
self.set_config_send_strict('true')
414
self.assertSendFails([])
415
self.assertSendSucceeds(['--no-strict'])
418
def test_send_bogus_config_var_ignored(self):
419
self.set_config_send_strict("I'm unsure")
420
self.assertSendFails([])
423
def test_send_no_strict_command_line_override_config(self):
424
self.set_config_send_strict('true')
425
self.assertSendFails([])
426
self.assertSendSucceeds(['--no-strict'])
428
def test_push_strict_command_line_override_config(self):
429
self.set_config_send_strict('false')
430
self.assertSendSucceeds([])
431
self.assertSendFails(['--strict'])
434
class TestBundleStrictWithoutChanges(TestSendStrictWithoutChanges):
436
_default_command = ['bundle-revisions', '../parent']