/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

Make pull update the progress bar more nicely

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 Canonical Ltd
2
 
#
 
1
#! /usr/bin/python2.4
 
2
 
 
3
# Copyright (C) 2005 by Canonical Ltd
 
4
 
3
5
# This program is free software; you can redistribute it and/or modify
4
6
# it under the terms of the GNU General Public License as published by
5
7
# the Free Software Foundation; either version 2 of the License, or
6
8
# (at your option) any later version.
7
 
#
 
9
 
8
10
# This program is distributed in the hope that it will be useful,
9
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
13
# GNU General Public License for more details.
12
 
#
 
14
 
13
15
# You should have received a copy of the GNU General Public License
14
16
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
18
 
17
19
 
18
20
# TODO: tests regarding version names
19
 
# TODO: rbc 20050108 test that join does not leave an inconsistent weave
 
21
# TODO: rbc 20050108 test that join does not leave an inconsistent weave 
20
22
#       if it fails.
21
23
 
22
24
"""test suite for weave algorithm"""
23
25
 
24
26
from pprint import pformat
25
27
 
26
 
from bzrlib import (
27
 
    errors,
28
 
    )
 
28
import bzrlib.errors as errors
 
29
from bzrlib.weave import Weave, WeaveFormatError, WeaveError, reweave
 
30
from bzrlib.weavefile import write_weave, read_weave
 
31
from bzrlib.tests import TestCase
29
32
from bzrlib.osutils import sha_string
30
 
from bzrlib.tests import TestCase, TestCaseInTempDir
31
 
from bzrlib.weave import Weave, WeaveFormatError, WeaveError
32
 
from bzrlib.weavefile import write_weave, read_weave
33
33
 
34
34
 
35
35
# texts for use in testing
39
39
 
40
40
 
41
41
class TestBase(TestCase):
42
 
 
43
42
    def check_read_write(self, k):
44
43
        """Check the weave k can be written & re-read."""
45
44
        from tempfile import TemporaryFile
65
64
class WeaveContains(TestBase):
66
65
    """Weave __contains__ operator"""
67
66
    def runTest(self):
68
 
        k = Weave(get_scope=lambda:None)
 
67
        k = Weave()
69
68
        self.assertFalse('foo' in k)
70
69
        k.add_lines('foo', [], TEXT_1)
71
70
        self.assertTrue('foo' in k)
76
75
        k = Weave()
77
76
 
78
77
 
 
78
class StoreText(TestBase):
 
79
    """Store and retrieve a simple text."""
 
80
    def runTest(self):
 
81
        k = Weave()
 
82
        idx = k.add_lines('text0', [], TEXT_0)
 
83
        self.assertEqual(k.get_lines(idx), TEXT_0)
 
84
        self.assertEqual(idx, 0)
 
85
 
 
86
 
79
87
class AnnotateOne(TestBase):
80
88
    def runTest(self):
81
89
        k = Weave()
84
92
                         [('text0', TEXT_0[0])])
85
93
 
86
94
 
 
95
class StoreTwo(TestBase):
 
96
    def runTest(self):
 
97
        k = Weave()
 
98
 
 
99
        idx = k.add_lines('text0', [], TEXT_0)
 
100
        self.assertEqual(idx, 0)
 
101
 
 
102
        idx = k.add_lines('text1', [], TEXT_1)
 
103
        self.assertEqual(idx, 1)
 
104
 
 
105
        self.assertEqual(k.get_lines(0), TEXT_0)
 
106
        self.assertEqual(k.get_lines(1), TEXT_1)
 
107
 
 
108
 
 
109
class GetSha1(TestBase):
 
110
    def test_get_sha1(self):
 
111
        k = Weave()
 
112
        k.add_lines('text0', [], 'text0')
 
113
        self.assertEqual('34dc0e430c642a26c3dd1c2beb7a8b4f4445eb79',
 
114
                         k.get_sha1('text0'))
 
115
        self.assertRaises(errors.RevisionNotPresent,
 
116
                          k.get_sha1, 0)
 
117
        self.assertRaises(errors.RevisionNotPresent,
 
118
                          k.get_sha1, 'text1')
 
119
                        
 
120
 
87
121
class InvalidAdd(TestBase):
88
122
    """Try to use invalid version number during add."""
89
123
    def runTest(self):
98
132
 
99
133
class RepeatedAdd(TestBase):
100
134
    """Add the same version twice; harmless."""
101
 
 
102
 
    def test_duplicate_add(self):
 
135
    def runTest(self):
103
136
        k = Weave()
104
137
        idx = k.add_lines('text0', [], TEXT_0)
105
138
        idx2 = k.add_lines('text0', [], TEXT_0)
121
154
                          'text0',
122
155
                          ['basis'],         # not the right parents
123
156
                          TEXT_0)
124
 
 
 
157
        
125
158
 
126
159
class InsertLines(TestBase):
127
160
    """Store a revision that adds one line to the original.
170
203
              ['text0', 'text1', 'text3'],
171
204
              ['line 1', 'aaa', 'middle line', 'bbb', 'line 2', 'ccc'])
172
205
 
173
 
        self.assertEqual(k.annotate('text4'),
 
206
        self.assertEqual(k.annotate('text4'), 
174
207
                         [('text0', 'line 1'),
175
208
                          ('text4', 'aaa'),
176
209
                          ('text3', 'middle line'),
189
222
        base_text = ['one', 'two', 'three', 'four']
190
223
 
191
224
        k.add_lines('text0', [], base_text)
192
 
 
 
225
        
193
226
        texts = [['one', 'two', 'three'],
194
227
                 ['two', 'three', 'four'],
195
228
                 ['one', 'four'],
226
259
                ]
227
260
        ################################### SKIPPED
228
261
        # Weave.get doesn't trap this anymore
229
 
        return
 
262
        return 
230
263
 
231
264
        self.assertRaises(WeaveFormatError,
232
265
                          k.get_lines,
233
 
                          0)
 
266
                          0)        
234
267
 
235
268
 
236
269
class CannedDelete(TestBase):
278
311
                'line to be deleted',
279
312
                (']', 1),
280
313
                ('{', 1),
281
 
                'replacement line',
 
314
                'replacement line',                
282
315
                ('}', 1),
283
316
                'last line',
284
317
                ('}', 0),
321
354
 
322
355
        ################################### SKIPPED
323
356
        # Weave.get doesn't trap this anymore
324
 
        return
 
357
        return 
325
358
 
326
359
 
327
360
        self.assertRaises(WeaveFormatError,
399
432
                          '  added in version 1',
400
433
                          '  also from v1',
401
434
                          '}'])
402
 
 
 
435
                       
403
436
        self.assertEqual(k.get_lines(2),
404
437
                         ['foo {',
405
438
                          '  added in v2',
411
444
                          '  added in v2',
412
445
                          '  also from v1',
413
446
                          '}'])
414
 
 
 
447
                         
415
448
 
416
449
class DeleteLines2(TestBase):
417
450
    """Test recording revisions that delete lines.
493
526
                ('}', 1),
494
527
                ('{', 2),
495
528
                "alternative second line",
496
 
                ('}', 2),
 
529
                ('}', 2),                
497
530
                ]
498
531
 
499
532
        k._sha1s = [sha_string('first line')
521
554
 
522
555
        text0 = ['cheddar', 'stilton', 'gruyere']
523
556
        text1 = ['cheddar', 'blue vein', 'neufchatel', 'chevre']
524
 
 
 
557
        
525
558
        k.add_lines('text0', [], text0)
526
559
        k.add_lines('text1', ['text0'], text1)
527
560
 
609
642
            A Jug of Wine, a Loaf of Bread, -- and Thou
610
643
            Beside me singing in the Wilderness --
611
644
            Oh, Wilderness were Paradise enow!""",
612
 
 
 
645
            
613
646
            """A Book of Verses underneath the Bough,
614
647
            A Jug of Wine, a Loaf of Bread, -- and Thou
615
648
            Beside me singing in the Wilderness --
648
681
        self.check_read_write(k)
649
682
 
650
683
 
 
684
class MergeCases(TestBase):
 
685
    def doMerge(self, base, a, b, mp):
 
686
        from cStringIO import StringIO
 
687
        from textwrap import dedent
 
688
 
 
689
        def addcrlf(x):
 
690
            return x + '\n'
 
691
        
 
692
        w = Weave()
 
693
        w.add_lines('text0', [], map(addcrlf, base))
 
694
        w.add_lines('text1', ['text0'], map(addcrlf, a))
 
695
        w.add_lines('text2', ['text0'], map(addcrlf, b))
 
696
 
 
697
        self.log('weave is:')
 
698
        tmpf = StringIO()
 
699
        write_weave(w, tmpf)
 
700
        self.log(tmpf.getvalue())
 
701
 
 
702
        self.log('merge plan:')
 
703
        p = list(w.plan_merge('text1', 'text2'))
 
704
        for state, line in p:
 
705
            if line:
 
706
                self.log('%12s | %s' % (state, line[:-1]))
 
707
 
 
708
        self.log('merge:')
 
709
        mt = StringIO()
 
710
        mt.writelines(w.weave_merge(p))
 
711
        mt.seek(0)
 
712
        self.log(mt.getvalue())
 
713
 
 
714
        mp = map(addcrlf, mp)
 
715
        self.assertEqual(mt.readlines(), mp)
 
716
        
 
717
        
 
718
    def testOneInsert(self):
 
719
        self.doMerge([],
 
720
                     ['aa'],
 
721
                     [],
 
722
                     ['aa'])
 
723
 
 
724
    def testSeparateInserts(self):
 
725
        self.doMerge(['aaa', 'bbb', 'ccc'],
 
726
                     ['aaa', 'xxx', 'bbb', 'ccc'],
 
727
                     ['aaa', 'bbb', 'yyy', 'ccc'],
 
728
                     ['aaa', 'xxx', 'bbb', 'yyy', 'ccc'])
 
729
 
 
730
    def testSameInsert(self):
 
731
        self.doMerge(['aaa', 'bbb', 'ccc'],
 
732
                     ['aaa', 'xxx', 'bbb', 'ccc'],
 
733
                     ['aaa', 'xxx', 'bbb', 'yyy', 'ccc'],
 
734
                     ['aaa', 'xxx', 'bbb', 'yyy', 'ccc'])
 
735
 
 
736
    def testOverlappedInsert(self):
 
737
        self.doMerge(['aaa', 'bbb'],
 
738
                     ['aaa', 'xxx', 'yyy', 'bbb'],
 
739
                     ['aaa', 'xxx', 'bbb'],
 
740
                     ['aaa', '<<<<<<< ', 'xxx', 'yyy', '=======', 'xxx', 
 
741
                      '>>>>>>> ', 'bbb'])
 
742
 
 
743
        # really it ought to reduce this to 
 
744
        # ['aaa', 'xxx', 'yyy', 'bbb']
 
745
 
 
746
 
 
747
    def testClashReplace(self):
 
748
        self.doMerge(['aaa'],
 
749
                     ['xxx'],
 
750
                     ['yyy', 'zzz'],
 
751
                     ['<<<<<<< ', 'xxx', '=======', 'yyy', 'zzz', 
 
752
                      '>>>>>>> '])
 
753
 
 
754
    def testNonClashInsert(self):
 
755
        self.doMerge(['aaa'],
 
756
                     ['xxx', 'aaa'],
 
757
                     ['yyy', 'zzz'],
 
758
                     ['<<<<<<< ', 'xxx', 'aaa', '=======', 'yyy', 'zzz', 
 
759
                      '>>>>>>> '])
 
760
 
 
761
        self.doMerge(['aaa'],
 
762
                     ['aaa'],
 
763
                     ['yyy', 'zzz'],
 
764
                     ['yyy', 'zzz'])
 
765
 
 
766
 
 
767
    def testDeleteAndModify(self):
 
768
        """Clashing delete and modification.
 
769
 
 
770
        If one side modifies a region and the other deletes it then
 
771
        there should be a conflict with one side blank.
 
772
        """
 
773
 
 
774
        #######################################
 
775
        # skippd, not working yet
 
776
        return
 
777
        
 
778
        self.doMerge(['aaa', 'bbb', 'ccc'],
 
779
                     ['aaa', 'ddd', 'ccc'],
 
780
                     ['aaa', 'ccc'],
 
781
                     ['<<<<<<<< ', 'aaa', '=======', '>>>>>>> ', 'ccc'])
 
782
 
 
783
 
651
784
class JoinWeavesTests(TestBase):
652
785
    def setUp(self):
653
786
        super(JoinWeavesTests, self).setUp()
657
790
        self.weave1.add_lines('v1', [], self.lines1)
658
791
        self.weave1.add_lines('v2', ['v1'], ['hello\n', 'world\n'])
659
792
        self.weave1.add_lines('v3', ['v2'], self.lines3)
 
793
        
 
794
    def test_join_empty(self):
 
795
        """Join two empty weaves."""
 
796
        eq = self.assertEqual
 
797
        w1 = Weave()
 
798
        w2 = Weave()
 
799
        w1.join(w2)
 
800
        eq(len(w1), 0)
 
801
        
 
802
    def test_join_empty_to_nonempty(self):
 
803
        """Join empty weave onto nonempty."""
 
804
        self.weave1.join(Weave())
 
805
        self.assertEqual(len(self.weave1), 3)
 
806
 
 
807
    def test_join_unrelated(self):
 
808
        """Join two weaves with no history in common."""
 
809
        wb = Weave()
 
810
        wb.add_lines('b1', [], ['line from b\n'])
 
811
        w1 = self.weave1
 
812
        w1.join(wb)
 
813
        eq = self.assertEqual
 
814
        eq(len(w1), 4)
 
815
        eq(sorted(w1.versions()),
 
816
           ['b1', 'v1', 'v2', 'v3'])
 
817
 
 
818
    def test_join_related(self):
 
819
        wa = self.weave1.copy()
 
820
        wb = self.weave1.copy()
 
821
        wa.add_lines('a1', ['v3'], ['hello\n', 'sweet\n', 'world\n'])
 
822
        wb.add_lines('b1', ['v3'], ['hello\n', 'pale blue\n', 'world\n'])
 
823
        eq = self.assertEquals
 
824
        eq(len(wa), 4)
 
825
        eq(len(wb), 4)
 
826
        wa.join(wb)
 
827
        eq(len(wa), 5)
 
828
        eq(wa.get_lines('b1'),
 
829
           ['hello\n', 'pale blue\n', 'world\n'])
 
830
 
 
831
    def test_join_parent_disagreement(self):
 
832
        #join reconciles differening parents into a union.
 
833
        wa = Weave()
 
834
        wb = Weave()
 
835
        wa.add_lines('v1', [], ['hello\n'])
 
836
        wb.add_lines('v0', [], [])
 
837
        wb.add_lines('v1', ['v0'], ['hello\n'])
 
838
        wa.join(wb)
 
839
        self.assertEqual(['v0'], wa.get_parents('v1'))
 
840
 
 
841
    def test_join_text_disagreement(self):
 
842
        """Cannot join weaves with different texts for a version."""
 
843
        wa = Weave()
 
844
        wb = Weave()
 
845
        wa.add_lines('v1', [], ['hello\n'])
 
846
        wb.add_lines('v1', [], ['not\n', 'hello\n'])
 
847
        self.assertRaises(WeaveError,
 
848
                          wa.join, wb)
 
849
 
 
850
    def test_join_unordered(self):
 
851
        """Join weaves where indexes differ.
 
852
        
 
853
        The source weave contains a different version at index 0."""
 
854
        wa = self.weave1.copy()
 
855
        wb = Weave()
 
856
        wb.add_lines('x1', [], ['line from x1\n'])
 
857
        wb.add_lines('v1', [], ['hello\n'])
 
858
        wb.add_lines('v2', ['v1'], ['hello\n', 'world\n'])
 
859
        wa.join(wb)
 
860
        eq = self.assertEquals
 
861
        eq(sorted(wa.versions()), ['v1', 'v2', 'v3', 'x1',])
 
862
        eq(wa.get_text('x1'), 'line from x1\n')
660
863
 
661
864
    def test_written_detection(self):
662
865
        # Test detection of weave file corruption.
707
910
        self.assertRaises(errors.WeaveInvalidChecksum, w.check)
708
911
 
709
912
 
710
 
class TestWeave(TestCase):
711
 
 
712
 
    def test_allow_reserved_false(self):
713
 
        w = Weave('name', allow_reserved=False)
714
 
        # Add lines is checked at the WeaveFile level, not at the Weave level
715
 
        w.add_lines('name:', [], TEXT_1)
716
 
        # But get_lines is checked at this level
717
 
        self.assertRaises(errors.ReservedId, w.get_lines, 'name:')
718
 
 
719
 
    def test_allow_reserved_true(self):
720
 
        w = Weave('name', allow_reserved=True)
721
 
        w.add_lines('name:', [], TEXT_1)
722
 
        self.assertEqual(TEXT_1, w.get_lines('name:'))
723
 
 
724
 
 
725
913
class InstrumentedWeave(Weave):
726
914
    """Keep track of how many times functions are called."""
727
 
 
 
915
    
728
916
    def __init__(self, weave_name=None):
729
917
        self._extract_count = 0
730
918
        Weave.__init__(self, weave_name=weave_name)
734
922
        return Weave._extract(self, versions)
735
923
 
736
924
 
737
 
class TestNeedsReweave(TestCase):
 
925
class JoinOptimization(TestCase):
 
926
    """Test that Weave.join() doesn't extract all texts, only what must be done."""
 
927
 
 
928
    def test_join(self):
 
929
        w1 = InstrumentedWeave()
 
930
        w2 = InstrumentedWeave()
 
931
 
 
932
        txt0 = ['a\n']
 
933
        txt1 = ['a\n', 'b\n']
 
934
        txt2 = ['a\n', 'c\n']
 
935
        txt3 = ['a\n', 'b\n', 'c\n']
 
936
 
 
937
        w1.add_lines('txt0', [], txt0) # extract 1a
 
938
        w2.add_lines('txt0', [], txt0) # extract 1b
 
939
        w1.add_lines('txt1', ['txt0'], txt1)# extract 2a
 
940
        w2.add_lines('txt2', ['txt0'], txt2)# extract 2b
 
941
        w1.join(w2) # extract 3a to add txt2 
 
942
        w2.join(w1) # extract 3b to add txt1 
 
943
 
 
944
        w1.add_lines('txt3', ['txt1', 'txt2'], txt3) # extract 4a 
 
945
        w2.add_lines('txt3', ['txt2', 'txt1'], txt3) # extract 4b
 
946
        # These secretly have inverted parents
 
947
 
 
948
        # This should not have to do any extractions
 
949
        w1.join(w2) # NO extract, texts already present with same parents
 
950
        w2.join(w1) # NO extract, texts already present with same parents
 
951
 
 
952
        self.assertEqual(4, w1._extract_count)
 
953
        self.assertEqual(4, w2._extract_count)
 
954
 
 
955
    def test_double_parent(self):
 
956
        # It should not be considered illegal to add
 
957
        # a revision with the same parent twice
 
958
        w1 = InstrumentedWeave()
 
959
        w2 = InstrumentedWeave()
 
960
 
 
961
        txt0 = ['a\n']
 
962
        txt1 = ['a\n', 'b\n']
 
963
        txt2 = ['a\n', 'c\n']
 
964
        txt3 = ['a\n', 'b\n', 'c\n']
 
965
 
 
966
        w1.add_lines('txt0', [], txt0)
 
967
        w2.add_lines('txt0', [], txt0)
 
968
        w1.add_lines('txt1', ['txt0'], txt1)
 
969
        w2.add_lines('txt1', ['txt0', 'txt0'], txt1)
 
970
        # Same text, effectively the same, because the
 
971
        # parent is only repeated
 
972
        w1.join(w2) # extract 3a to add txt2 
 
973
        w2.join(w1) # extract 3b to add txt1 
 
974
 
 
975
 
 
976
class TestNeedsRweave(TestCase):
738
977
    """Internal corner cases for when reweave is needed."""
739
978
 
740
979
    def test_compatible_parents(self):
750
989
        self.assertFalse(w1._compatible_parents(set(), set([1])))
751
990
        self.assertFalse(w1._compatible_parents(my_parents, set([1, 2, 3, 4])))
752
991
        self.assertFalse(w1._compatible_parents(my_parents, set([4])))
753
 
 
754
 
 
755
 
class TestWeaveFile(TestCaseInTempDir):
756
 
 
757
 
    def test_empty_file(self):
758
 
        f = open('empty.weave', 'wb+')
759
 
        try:
760
 
            self.assertRaises(errors.WeaveFormatError,
761
 
                              read_weave, f)
762
 
        finally:
763
 
            f.close()
 
992
        
 
993