~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/blackbox/test_merge.py

  • Committer: Jelmer Vernooij
  • Date: 2011-12-16 16:40:10 UTC
  • mto: This revision was merged to the branch mainline in revision 6391.
  • Revision ID: jelmer@samba.org-20111216164010-z3hy00xrnclnkf7a
Update tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2006-2011 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
19
19
"""Black-box tests for bzr merge.
20
20
"""
21
21
 
 
22
import doctest
22
23
import os
23
24
 
 
25
from testtools import matchers
 
26
 
24
27
from bzrlib import (
25
28
    branch,
26
29
    bzrdir,
27
30
    conflicts,
28
 
    errors,
29
31
    merge_directive,
30
32
    osutils,
31
33
    tests,
32
34
    urlutils,
33
35
    workingtree,
34
36
    )
 
37
from bzrlib.tests import (
 
38
    scenarios,
 
39
    script,
 
40
    )
 
41
 
 
42
 
 
43
load_tests = scenarios.load_tests_apply_scenarios
35
44
 
36
45
 
37
46
class TestMerge(tests.TestCaseWithTransport):
102
111
        # Merging a branch pulls its revision into the tree
103
112
        b = branch.Branch.open('../b')
104
113
        b_tip = b.last_revision()
105
 
        self.failUnless(a.branch.repository.has_revision(b_tip))
 
114
        self.assertTrue(a.branch.repository.has_revision(b_tip))
106
115
        self.assertEqual([a_tip, b_tip], a.get_parent_ids())
107
116
        a_tree.revert(backups=False)
108
117
        out, err = self.run_bzr('merge -r revno:1:./hello', retcode=3)
204
213
        b_tree.commit(message='Modified a.txt')
205
214
        os.chdir('b')
206
215
        self.run_bzr('merge ../a/', retcode=1)
207
 
        self.failUnlessExists('sub/a.txt.THIS')
208
 
        self.failUnlessExists('sub/a.txt.BASE')
 
216
        self.assertPathExists('sub/a.txt.THIS')
 
217
        self.assertPathExists('sub/a.txt.BASE')
209
218
        os.chdir('../a')
210
219
        self.run_bzr('merge ../b/', retcode=1)
211
 
        self.failUnlessExists('sub/a.txt.OTHER')
212
 
        self.failUnlessExists('sub/a.txt.BASE')
 
220
        self.assertPathExists('sub/a.txt.OTHER')
 
221
        self.assertPathExists('sub/a.txt.BASE')
213
222
 
214
223
    def test_conflict_leaves_base_this_other_files(self):
215
224
        tree, other = self.create_conflicting_branches()
326
335
        tree_a.add(['file_1', 'file_2'])
327
336
        tree_a.commit('commit 1')
328
337
        tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
329
 
        self.failUnlessExists('b/file_1')
 
338
        self.assertPathExists('b/file_1')
330
339
        tree_a.rename_one('file_1', 'file_i')
331
340
        tree_a.commit('commit 2')
332
341
        tree_a.rename_one('file_2', 'file_ii')
333
342
        ## os.chdir('b')
334
343
        self.run_bzr('merge a --uncommitted -d b')
335
 
        self.failUnlessExists('b/file_1')
336
 
        self.failUnlessExists('b/file_ii')
 
344
        self.assertPathExists('b/file_1')
 
345
        self.assertPathExists('b/file_ii')
337
346
        tree_b.revert()
338
347
        self.run_bzr_error(('Cannot use --uncommitted and --revision',),
339
348
                           'merge /a --uncommitted -r1 -d b')
347
356
        tree_a.add(['file1', 'file2'])
348
357
        os.chdir('tree_b')
349
358
        self.run_bzr(['merge', '--uncommitted', '../tree_a/file1'])
350
 
        self.failUnlessExists('file1')
351
 
        self.failIfExists('file2')
 
359
        self.assertPathExists('file1')
 
360
        self.assertPathDoesNotExist('file2')
 
361
 
 
362
    def test_merge_nonexistent_file(self):
 
363
        """It should not be possible to merge changes from a file which
 
364
        does not exist."""
 
365
        tree_a = self.make_branch_and_tree('tree_a')
 
366
        self.build_tree_contents([('tree_a/file', 'bar\n')])
 
367
        tree_a.add(['file'])
 
368
        tree_a.commit('commit 1')
 
369
        os.chdir('tree_a')
 
370
        self.run_bzr_error(('Path\(s\) do not exist: non/existing',),
 
371
                           ['merge', 'non/existing'])
352
372
 
353
373
    def pullable_branch(self):
354
374
        tree_a = self.make_branch_and_tree('a')
355
 
        self.build_tree(['a/file'])
 
375
        self.build_tree_contents([('a/file', 'bar\n')])
356
376
        tree_a.add(['file'])
357
377
        self.id1 = tree_a.commit('commit 1')
358
378
 
359
379
        tree_b = self.make_branch_and_tree('b')
360
380
        tree_b.pull(tree_a.branch)
361
 
        file('b/file', 'wb').write('foo')
 
381
        self.build_tree_contents([('b/file', 'foo\n')])
362
382
        self.id2 = tree_b.commit('commit 2')
363
383
 
364
384
    def test_merge_pull(self):
369
389
        tree_a = workingtree.WorkingTree.open('.')
370
390
        self.assertEqual([self.id2], tree_a.get_parent_ids())
371
391
 
 
392
    def test_merge_pull_preview(self):
 
393
        self.pullable_branch()
 
394
        (out, err) = self.run_bzr('merge --pull --preview -d a b')
 
395
        self.assertThat(out, matchers.DocTestMatches(
 
396
"""=== modified file 'file'
 
397
--- file\t...
 
398
+++ file\t...
 
399
@@ -1,1 +1,1 @@
 
400
-bar
 
401
+foo
 
402
 
 
403
""", doctest.ELLIPSIS | doctest.REPORT_UDIFF))
 
404
        tree_a = workingtree.WorkingTree.open('a')
 
405
        self.assertEqual([self.id1], tree_a.get_parent_ids())
 
406
 
372
407
    def test_merge_kind_change(self):
373
408
        tree_a = self.make_branch_and_tree('tree_a')
374
409
        self.build_tree_contents([('tree_a/file', 'content_1')])
408
443
        self.write_directive('directive', source.branch, 'target', 'rev2',
409
444
                             'rev1')
410
445
        out, err = self.run_bzr('merge -d target directive')
411
 
        self.failIfExists('target/a')
412
 
        self.failUnlessExists('target/b')
 
446
        self.assertPathDoesNotExist('target/a')
 
447
        self.assertPathExists('target/b')
413
448
        self.assertContainsRe(err, 'Performing cherrypick')
414
449
 
415
450
    def write_directive(self, filename, source, target, revision_id,
450
485
        branch_b.add('file2')
451
486
        branch_b.commit('added file2', rev_id='rev2b')
452
487
        branch_b.merge_from_branch(branch_a.branch)
453
 
        self.failUnlessExists('branch_b/file1')
 
488
        self.assertPathExists('branch_b/file1')
454
489
        branch_b.commit('merged branch_a', rev_id='rev3b')
455
490
 
456
491
        # It works if the revid has an interger revno
457
492
        self.run_bzr('merge -d target -r revid:rev2a branch_a')
458
 
        self.failUnlessExists('target/file1')
459
 
        self.failIfExists('target/file2')
 
493
        self.assertPathExists('target/file1')
 
494
        self.assertPathDoesNotExist('target/file2')
460
495
        target.revert()
461
496
 
462
497
        # It should work if the revid has no integer revno
463
498
        self.run_bzr('merge -d target -r revid:rev2a branch_b')
464
 
        self.failUnlessExists('target/file1')
465
 
        self.failIfExists('target/file2')
 
499
        self.assertPathExists('target/file1')
 
500
        self.assertPathDoesNotExist('target/file2')
466
501
 
467
502
    def assertDirectoryContent(self, directory, entries, message=''):
468
503
        """Assert whether entries (file or directories) exist in a directory.
516
551
 
517
552
    def test_merge_from_submit(self):
518
553
        tree_a = self.make_branch_and_tree('a')
 
554
        tree_a.commit('test')
519
555
        tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
520
556
        tree_c = tree_a.bzrdir.sprout('c').open_workingtree()
521
557
        out, err = self.run_bzr(['merge', '-d', 'c'])
526
562
 
527
563
    def test_remember_sets_submit(self):
528
564
        tree_a = self.make_branch_and_tree('a')
 
565
        tree_a.commit('rev1')
529
566
        tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
530
567
        self.assertIs(tree_b.branch.get_submit_branch(), None)
531
568
 
538
575
        self.assertEqual(tree_b.branch.get_submit_branch(),
539
576
                         tree_a.bzrdir.root_transport.base)
540
577
 
 
578
    def test_no_remember_dont_set_submit(self):
 
579
        tree_a = self.make_branch_and_tree('a')
 
580
        self.build_tree_contents([('a/file', "a\n")])
 
581
        tree_a.add('file')
 
582
        tree_a.commit('rev1')
 
583
        tree_b = tree_a.bzrdir.sprout('b').open_workingtree()
 
584
        self.assertIs(tree_b.branch.get_submit_branch(), None)
 
585
 
 
586
        # Remember should not happen if using default from parent
 
587
        out, err = self.run_bzr(['merge', '-d', 'b', '--no-remember'])
 
588
        self.assertEquals(None, tree_b.branch.get_submit_branch())
 
589
 
 
590
        # Remember should not happen if user supplies location but ask for not
 
591
        # remembering it
 
592
        out, err = self.run_bzr(['merge', '-d', 'b', '--no-remember', 'a'])
 
593
        self.assertEqual(None, tree_b.branch.get_submit_branch())
 
594
 
541
595
    def test_weave_cherrypick(self):
542
596
        this_tree = self.make_branch_and_tree('this')
543
597
        self.build_tree_contents([('this/file', "a\n")])
603
657
        other.add('other_file')
604
658
        other.commit('rev1b')
605
659
        self.run_bzr('merge -d this other -r0..')
606
 
        self.failUnlessExists('this/other_file')
 
660
        self.assertPathExists('this/other_file')
607
661
 
608
662
    def test_merge_interactive_unlocks_branch(self):
609
663
        this = self.make_branch_and_tree('this')
610
 
        other = self.make_branch_and_tree('other')
611
 
        other.commit('empty commit')
 
664
        this.commit('empty commit')
 
665
        other = this.bzrdir.sprout('other').open_workingtree()
 
666
        other.commit('empty commit 2')
612
667
        self.run_bzr('merge -i -d this other')
613
668
        this.lock_write()
614
669
        this.unlock()
615
670
 
616
 
    def test_merge_reversed_revision_range(self):
617
 
        tree = self.make_branch_and_tree(".")
 
671
    def test_merge_fetches_tags(self):
 
672
        """Tags are updated by merge, and revisions named in those tags are
 
673
        fetched.
 
674
        """
 
675
        # Make a source, sprout a target off it
 
676
        builder = self.make_branch_builder('source')
 
677
        builder.build_commit(message="Rev 1", rev_id='rev-1')
 
678
        source = builder.get_branch()
 
679
        target_bzrdir = source.bzrdir.sprout('target')
 
680
        # Add a non-ancestry tag to source
 
681
        builder.build_commit(message="Rev 2a", rev_id='rev-2a')
 
682
        source.tags.set_tag('tag-a', 'rev-2a')
 
683
        source.set_last_revision_info(1, 'rev-1')
 
684
        source.get_config().set_user_option('branch.fetch_tags', 'True')
 
685
        builder.build_commit(message="Rev 2b", rev_id='rev-2b')
 
686
        # Merge from source
 
687
        self.run_bzr('merge -d target source')
 
688
        target = target_bzrdir.open_branch()
 
689
        # The tag is present, and so is its revision.
 
690
        self.assertEqual('rev-2a', target.tags.lookup_tag('tag-a'))
 
691
        target.repository.get_revision('rev-2a')
 
692
 
 
693
 
 
694
class TestMergeRevisionRange(tests.TestCaseWithTransport):
 
695
 
 
696
    scenarios = (('whole-tree', dict(context='.')),
 
697
                 ('file-only', dict(context='a')))
 
698
 
 
699
    def setUp(self):
 
700
        super(TestMergeRevisionRange, self).setUp()
 
701
        self.tree = self.make_branch_and_tree(".")
 
702
        self.tree.commit('initial commit')
618
703
        for f in ("a", "b"):
619
704
            self.build_tree([f])
620
 
            tree.add(f)
621
 
            tree.commit("added "+f)
622
 
        for context in (".", "", "a"):
623
 
            self.run_bzr("merge -r 1..0 " + context)
624
 
            self.failIfExists("a")
625
 
            tree.revert()
626
 
            self.failUnlessExists("a")
627
 
 
 
705
            self.tree.add(f)
 
706
            self.tree.commit("added " + f)
 
707
 
 
708
    def test_merge_reversed_revision_range(self):
 
709
        self.run_bzr("merge -r 2..1 " + self.context)
 
710
        self.assertPathDoesNotExist("a")
 
711
        self.assertPathExists("b")
 
712
 
 
713
 
 
714
class TestMergeScript(script.TestCaseWithTransportAndScript):
 
715
    def test_merge_empty_branch(self):
 
716
        source = self.make_branch_and_tree('source')
 
717
        self.build_tree(['source/a'])
 
718
        source.add('a')
 
719
        source.commit('Added a', rev_id='rev1')
 
720
        target = self.make_branch_and_tree('target')
 
721
        self.run_script("""\
 
722
$ bzr merge -d target source
 
723
2>bzr: ERROR: Merging into empty branches not currently supported, https://bugs.launchpad.net/bzr/+bug/308562
 
724
""")
628
725
 
629
726
class TestMergeForce(tests.TestCaseWithTransport):
630
727