/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_weave.py

  • Committer: Martin Pool
  • Date: 2006-10-06 02:04:17 UTC
  • mfrom: (1908.10.1 bench_usecases.merge2)
  • mto: This revision was merged to the branch mainline in revision 2068.
  • Revision ID: mbp@sourcefrog.net-20061006020417-4949ca86f4417a4d
merge additional fix from cfbolz

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#! /usr/bin/python2.4
2
2
 
3
3
# Copyright (C) 2005 by Canonical Ltd
4
 
 
 
4
#
5
5
# This program is free software; you can redistribute it and/or modify
6
6
# it under the terms of the GNU General Public License as published by
7
7
# the Free Software Foundation; either version 2 of the License, or
8
8
# (at your option) any later version.
9
 
 
 
9
#
10
10
# This program is distributed in the hope that it will be useful,
11
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
13
# GNU General Public License for more details.
14
 
 
 
14
#
15
15
# You should have received a copy of the GNU General Public License
16
16
# along with this program; if not, write to the Free Software
17
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
25
 
26
26
from pprint import pformat
27
27
 
28
 
import bzrlib.errors as errors
 
28
from bzrlib import (
 
29
    errors,
 
30
    )
 
31
from bzrlib.osutils import sha_string
 
32
from bzrlib.tests import TestCase, TestCaseInTempDir
29
33
from bzrlib.weave import Weave, WeaveFormatError, WeaveError, reweave
30
34
from bzrlib.weavefile import write_weave, read_weave
31
 
from bzrlib.tests import TestCase
32
 
from bzrlib.osutils import sha_string
33
35
 
34
36
 
35
37
# texts for use in testing
682
684
        self.check_read_write(k)
683
685
 
684
686
 
685
 
class MergeCases(TestBase):
686
 
    def doMerge(self, base, a, b, mp):
687
 
        from cStringIO import StringIO
688
 
        from textwrap import dedent
689
 
 
690
 
        def addcrlf(x):
691
 
            return x + '\n'
692
 
        
693
 
        w = Weave()
694
 
        w.add_lines('text0', [], map(addcrlf, base))
695
 
        w.add_lines('text1', ['text0'], map(addcrlf, a))
696
 
        w.add_lines('text2', ['text0'], map(addcrlf, b))
697
 
 
698
 
        self.log('weave is:')
699
 
        tmpf = StringIO()
700
 
        write_weave(w, tmpf)
701
 
        self.log(tmpf.getvalue())
702
 
 
703
 
        self.log('merge plan:')
704
 
        p = list(w.plan_merge('text1', 'text2'))
705
 
        for state, line in p:
706
 
            if line:
707
 
                self.log('%12s | %s' % (state, line[:-1]))
708
 
 
709
 
        self.log('merge:')
710
 
        mt = StringIO()
711
 
        mt.writelines(w.weave_merge(p))
712
 
        mt.seek(0)
713
 
        self.log(mt.getvalue())
714
 
 
715
 
        mp = map(addcrlf, mp)
716
 
        self.assertEqual(mt.readlines(), mp)
717
 
        
718
 
        
719
 
    def testOneInsert(self):
720
 
        self.doMerge([],
721
 
                     ['aa'],
722
 
                     [],
723
 
                     ['aa'])
724
 
 
725
 
    def testSeparateInserts(self):
726
 
        self.doMerge(['aaa', 'bbb', 'ccc'],
727
 
                     ['aaa', 'xxx', 'bbb', 'ccc'],
728
 
                     ['aaa', 'bbb', 'yyy', 'ccc'],
729
 
                     ['aaa', 'xxx', 'bbb', 'yyy', 'ccc'])
730
 
 
731
 
    def testSameInsert(self):
732
 
        self.doMerge(['aaa', 'bbb', 'ccc'],
733
 
                     ['aaa', 'xxx', 'bbb', 'ccc'],
734
 
                     ['aaa', 'xxx', 'bbb', 'yyy', 'ccc'],
735
 
                     ['aaa', 'xxx', 'bbb', 'yyy', 'ccc'])
736
 
 
737
 
    def testOverlappedInsert(self):
738
 
        self.doMerge(['aaa', 'bbb'],
739
 
                     ['aaa', 'xxx', 'yyy', 'bbb'],
740
 
                     ['aaa', 'xxx', 'bbb'],
741
 
                     ['aaa', '<<<<<<< ', 'xxx', 'yyy', '=======', 'xxx', 
742
 
                      '>>>>>>> ', 'bbb'])
743
 
 
744
 
        # really it ought to reduce this to 
745
 
        # ['aaa', 'xxx', 'yyy', 'bbb']
746
 
 
747
 
 
748
 
    def testClashReplace(self):
749
 
        self.doMerge(['aaa'],
750
 
                     ['xxx'],
751
 
                     ['yyy', 'zzz'],
752
 
                     ['<<<<<<< ', 'xxx', '=======', 'yyy', 'zzz', 
753
 
                      '>>>>>>> '])
754
 
 
755
 
    def testNonClashInsert(self):
756
 
        self.doMerge(['aaa'],
757
 
                     ['xxx', 'aaa'],
758
 
                     ['yyy', 'zzz'],
759
 
                     ['<<<<<<< ', 'xxx', 'aaa', '=======', 'yyy', 'zzz', 
760
 
                      '>>>>>>> '])
761
 
 
762
 
        self.doMerge(['aaa'],
763
 
                     ['aaa'],
764
 
                     ['yyy', 'zzz'],
765
 
                     ['yyy', 'zzz'])
766
 
 
767
 
 
768
 
    def testDeleteAndModify(self):
769
 
        """Clashing delete and modification.
770
 
 
771
 
        If one side modifies a region and the other deletes it then
772
 
        there should be a conflict with one side blank.
773
 
        """
774
 
 
775
 
        #######################################
776
 
        # skippd, not working yet
777
 
        return
778
 
        
779
 
        self.doMerge(['aaa', 'bbb', 'ccc'],
780
 
                     ['aaa', 'ddd', 'ccc'],
781
 
                     ['aaa', 'ccc'],
782
 
                     ['<<<<<<<< ', 'aaa', '=======', '>>>>>>> ', 'ccc'])
783
 
 
784
 
    def _test_merge_from_strings(self, base, a, b, expected):
785
 
        w = Weave()
786
 
        w.add_lines('text0', [], base.splitlines(True))
787
 
        w.add_lines('text1', ['text0'], a.splitlines(True))
788
 
        w.add_lines('text2', ['text0'], b.splitlines(True))
789
 
        self.log('merge plan:')
790
 
        p = list(w.plan_merge('text1', 'text2'))
791
 
        for state, line in p:
792
 
            if line:
793
 
                self.log('%12s | %s' % (state, line[:-1]))
794
 
        self.log('merge result:')
795
 
        result_text = ''.join(w.weave_merge(p))
796
 
        self.log(result_text)
797
 
        self.assertEqualDiff(result_text, expected)
798
 
 
799
 
    def test_deletion_extended(self):
800
 
        """One side deletes, the other deletes more.
801
 
        """
802
 
        base = """\
803
 
            line 1
804
 
            line 2
805
 
            line 3
806
 
            """
807
 
        a = """\
808
 
            line 1
809
 
            line 2
810
 
            """
811
 
        b = """\
812
 
            line 1
813
 
            """
814
 
        result = """\
815
 
            line 1
816
 
            """
817
 
        self._test_merge_from_strings(base, a, b, result)
818
 
 
819
 
    def test_deletion_overlap(self):
820
 
        """Delete overlapping regions with no other conflict.
821
 
 
822
 
        Arguably it'd be better to treat these as agreement, rather than 
823
 
        conflict, but for now conflict is safer.
824
 
        """
825
 
        base = """\
826
 
            start context
827
 
            int a() {}
828
 
            int b() {}
829
 
            int c() {}
830
 
            end context
831
 
            """
832
 
        a = """\
833
 
            start context
834
 
            int a() {}
835
 
            end context
836
 
            """
837
 
        b = """\
838
 
            start context
839
 
            int c() {}
840
 
            end context
841
 
            """
842
 
        result = """\
843
 
            start context
844
 
<<<<<<< 
845
 
            int a() {}
846
 
=======
847
 
            int c() {}
848
 
>>>>>>> 
849
 
            end context
850
 
            """
851
 
        self._test_merge_from_strings(base, a, b, result)
852
 
 
853
 
    def test_agreement_deletion(self):
854
 
        """Agree to delete some lines, without conflicts."""
855
 
        base = """\
856
 
            start context
857
 
            base line 1
858
 
            base line 2
859
 
            end context
860
 
            """
861
 
        a = """\
862
 
            start context
863
 
            base line 1
864
 
            end context
865
 
            """
866
 
        b = """\
867
 
            start context
868
 
            base line 1
869
 
            end context
870
 
            """
871
 
        result = """\
872
 
            start context
873
 
            base line 1
874
 
            end context
875
 
            """
876
 
        self._test_merge_from_strings(base, a, b, result)
877
 
 
878
 
    def test_sync_on_deletion(self):
879
 
        """Specific case of merge where we can synchronize incorrectly.
880
 
        
881
 
        A previous version of the weave merge concluded that the two versions
882
 
        agreed on deleting line 2, and this could be a synchronization point.
883
 
        Line 1 was then considered in isolation, and thought to be deleted on 
884
 
        both sides.
885
 
 
886
 
        It's better to consider the whole thing as a disagreement region.
887
 
        """
888
 
        base = """\
889
 
            start context
890
 
            base line 1
891
 
            base line 2
892
 
            end context
893
 
            """
894
 
        a = """\
895
 
            start context
896
 
            base line 1
897
 
            a's replacement line 2
898
 
            end context
899
 
            """
900
 
        b = """\
901
 
            start context
902
 
            b replaces
903
 
            both lines
904
 
            end context
905
 
            """
906
 
        result = """\
907
 
            start context
908
 
<<<<<<< 
909
 
            base line 1
910
 
            a's replacement line 2
911
 
=======
912
 
            b replaces
913
 
            both lines
914
 
>>>>>>> 
915
 
            end context
916
 
            """
917
 
        self._test_merge_from_strings(base, a, b, result)
918
 
 
919
 
 
920
687
class JoinWeavesTests(TestBase):
921
688
    def setUp(self):
922
689
        super(JoinWeavesTests, self).setUp()
1125
892
        self.assertFalse(w1._compatible_parents(set(), set([1])))
1126
893
        self.assertFalse(w1._compatible_parents(my_parents, set([1, 2, 3, 4])))
1127
894
        self.assertFalse(w1._compatible_parents(my_parents, set([4])))
 
895
 
 
896
 
 
897
class TestWeaveFile(TestCaseInTempDir):
 
898
    
 
899
    def test_empty_file(self):
 
900
        f = open('empty.weave', 'wb+')
 
901
        try:
 
902
            self.assertRaises(errors.WeaveFormatError,
 
903
                              read_weave, f)
 
904
        finally:
 
905
            f.close()