~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_weave.py

  • Committer: Patch Queue Manager
  • Date: 2015-09-30 16:43:21 UTC
  • mfrom: (6603.2.2 fix-keep-dirty)
  • Revision ID: pqm@pqm.ubuntu.com-20150930164321-ct2v2qnmvimqt8qf
(vila) Avoid associating dirty patch headers with the previous file in the
 patch. (Colin Watson)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 Canonical Ltd
 
1
# Copyright (C) 2005-2009, 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
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
 
18
18
# TODO: tests regarding version names
19
 
# TODO: rbc 20050108 test that join does not leave an inconsistent weave 
 
19
# TODO: rbc 20050108 test that join does not leave an inconsistent weave
20
20
#       if it fails.
21
21
 
22
22
"""test suite for weave algorithm"""
28
28
    )
29
29
from bzrlib.osutils import sha_string
30
30
from bzrlib.tests import TestCase, TestCaseInTempDir
31
 
from bzrlib.weave import Weave, WeaveFormatError, WeaveError
 
31
from bzrlib.weave import Weave, WeaveFormatError
32
32
from bzrlib.weavefile import write_weave, read_weave
33
33
 
34
34
 
64
64
 
65
65
class WeaveContains(TestBase):
66
66
    """Weave __contains__ operator"""
 
67
 
67
68
    def runTest(self):
68
 
        k = Weave()
 
69
        k = Weave(get_scope=lambda:None)
69
70
        self.assertFalse('foo' in k)
70
71
        k.add_lines('foo', [], TEXT_1)
71
72
        self.assertTrue('foo' in k)
72
73
 
73
74
 
74
75
class Easy(TestBase):
 
76
 
75
77
    def runTest(self):
76
78
        k = Weave()
77
79
 
78
80
 
79
81
class AnnotateOne(TestBase):
 
82
 
80
83
    def runTest(self):
81
84
        k = Weave()
82
85
        k.add_lines('text0', [], TEXT_0)
84
87
                         [('text0', TEXT_0[0])])
85
88
 
86
89
 
87
 
class GetSha1(TestBase):
88
 
    def test_get_sha1(self):
89
 
        k = Weave()
90
 
        k.add_lines('text0', [], 'text0')
91
 
        self.assertEqual('34dc0e430c642a26c3dd1c2beb7a8b4f4445eb79',
92
 
                         k.get_sha1('text0'))
93
 
        self.assertRaises(errors.RevisionNotPresent,
94
 
                          k.get_sha1, 0)
95
 
        self.assertRaises(errors.RevisionNotPresent,
96
 
                          k.get_sha1, 'text1')
97
 
                        
98
 
 
99
90
class InvalidAdd(TestBase):
100
91
    """Try to use invalid version number during add."""
 
92
 
101
93
    def runTest(self):
102
94
        k = Weave()
103
95
 
119
111
 
120
112
 
121
113
class InvalidRepeatedAdd(TestBase):
 
114
 
122
115
    def runTest(self):
123
116
        k = Weave()
124
117
        k.add_lines('basis', [], TEXT_0)
133
126
                          'text0',
134
127
                          ['basis'],         # not the right parents
135
128
                          TEXT_0)
136
 
        
 
129
 
137
130
 
138
131
class InsertLines(TestBase):
139
132
    """Store a revision that adds one line to the original.
182
175
              ['text0', 'text1', 'text3'],
183
176
              ['line 1', 'aaa', 'middle line', 'bbb', 'line 2', 'ccc'])
184
177
 
185
 
        self.assertEqual(k.annotate('text4'), 
 
178
        self.assertEqual(k.annotate('text4'),
186
179
                         [('text0', 'line 1'),
187
180
                          ('text4', 'aaa'),
188
181
                          ('text3', 'middle line'),
201
194
        base_text = ['one', 'two', 'three', 'four']
202
195
 
203
196
        k.add_lines('text0', [], base_text)
204
 
        
 
197
 
205
198
        texts = [['one', 'two', 'three'],
206
199
                 ['two', 'three', 'four'],
207
200
                 ['one', 'four'],
238
231
                ]
239
232
        ################################### SKIPPED
240
233
        # Weave.get doesn't trap this anymore
241
 
        return 
 
234
        return
242
235
 
243
236
        self.assertRaises(WeaveFormatError,
244
237
                          k.get_lines,
245
 
                          0)        
 
238
                          0)
246
239
 
247
240
 
248
241
class CannedDelete(TestBase):
290
283
                'line to be deleted',
291
284
                (']', 1),
292
285
                ('{', 1),
293
 
                'replacement line',                
 
286
                'replacement line',
294
287
                ('}', 1),
295
288
                'last line',
296
289
                ('}', 0),
333
326
 
334
327
        ################################### SKIPPED
335
328
        # Weave.get doesn't trap this anymore
336
 
        return 
 
329
        return
337
330
 
338
331
 
339
332
        self.assertRaises(WeaveFormatError,
411
404
                          '  added in version 1',
412
405
                          '  also from v1',
413
406
                          '}'])
414
 
                       
 
407
 
415
408
        self.assertEqual(k.get_lines(2),
416
409
                         ['foo {',
417
410
                          '  added in v2',
423
416
                          '  added in v2',
424
417
                          '  also from v1',
425
418
                          '}'])
426
 
                         
 
419
 
427
420
 
428
421
class DeleteLines2(TestBase):
429
422
    """Test recording revisions that delete lines.
505
498
                ('}', 1),
506
499
                ('{', 2),
507
500
                "alternative second line",
508
 
                ('}', 2),                
 
501
                ('}', 2),
509
502
                ]
510
503
 
511
504
        k._sha1s = [sha_string('first line')
533
526
 
534
527
        text0 = ['cheddar', 'stilton', 'gruyere']
535
528
        text1 = ['cheddar', 'blue vein', 'neufchatel', 'chevre']
536
 
        
 
529
 
537
530
        k.add_lines('text0', [], text0)
538
531
        k.add_lines('text1', ['text0'], text1)
539
532
 
545
538
 
546
539
class Merge(TestBase):
547
540
    """Storage of versions that merge diverged parents"""
 
541
 
548
542
    def runTest(self):
549
543
        k = Weave()
550
544
 
621
615
            A Jug of Wine, a Loaf of Bread, -- and Thou
622
616
            Beside me singing in the Wilderness --
623
617
            Oh, Wilderness were Paradise enow!""",
624
 
            
 
618
 
625
619
            """A Book of Verses underneath the Bough,
626
620
            A Jug of Wine, a Loaf of Bread, -- and Thou
627
621
            Beside me singing in the Wilderness --
661
655
 
662
656
 
663
657
class JoinWeavesTests(TestBase):
 
658
 
664
659
    def setUp(self):
665
660
        super(JoinWeavesTests, self).setUp()
666
661
        self.weave1 = Weave()
669
664
        self.weave1.add_lines('v1', [], self.lines1)
670
665
        self.weave1.add_lines('v2', ['v1'], ['hello\n', 'world\n'])
671
666
        self.weave1.add_lines('v3', ['v2'], self.lines3)
672
 
        
673
 
    def test_join_empty(self):
674
 
        """Join two empty weaves."""
675
 
        eq = self.assertEqual
676
 
        w1 = Weave()
677
 
        w2 = Weave()
678
 
        w1.join(w2)
679
 
        eq(len(w1), 0)
680
 
        
681
 
    def test_join_empty_to_nonempty(self):
682
 
        """Join empty weave onto nonempty."""
683
 
        self.weave1.join(Weave())
684
 
        self.assertEqual(len(self.weave1), 3)
685
 
 
686
 
    def test_join_unrelated(self):
687
 
        """Join two weaves with no history in common."""
688
 
        wb = Weave()
689
 
        wb.add_lines('b1', [], ['line from b\n'])
690
 
        w1 = self.weave1
691
 
        w1.join(wb)
692
 
        eq = self.assertEqual
693
 
        eq(len(w1), 4)
694
 
        eq(sorted(w1.versions()),
695
 
           ['b1', 'v1', 'v2', 'v3'])
696
 
 
697
 
    def test_join_related(self):
698
 
        wa = self.weave1.copy()
699
 
        wb = self.weave1.copy()
700
 
        wa.add_lines('a1', ['v3'], ['hello\n', 'sweet\n', 'world\n'])
701
 
        wb.add_lines('b1', ['v3'], ['hello\n', 'pale blue\n', 'world\n'])
702
 
        eq = self.assertEquals
703
 
        eq(len(wa), 4)
704
 
        eq(len(wb), 4)
705
 
        wa.join(wb)
706
 
        eq(len(wa), 5)
707
 
        eq(wa.get_lines('b1'),
708
 
           ['hello\n', 'pale blue\n', 'world\n'])
709
 
 
710
 
    def test_join_text_disagreement(self):
711
 
        """Cannot join weaves with different texts for a version."""
712
 
        wa = Weave()
713
 
        wb = Weave()
714
 
        wa.add_lines('v1', [], ['hello\n'])
715
 
        wb.add_lines('v1', [], ['not\n', 'hello\n'])
716
 
        self.assertRaises(WeaveError,
717
 
                          wa.join, wb)
718
 
 
719
 
    def test_join_unordered(self):
720
 
        """Join weaves where indexes differ.
721
 
        
722
 
        The source weave contains a different version at index 0."""
723
 
        wa = self.weave1.copy()
724
 
        wb = Weave()
725
 
        wb.add_lines('x1', [], ['line from x1\n'])
726
 
        wb.add_lines('v1', [], ['hello\n'])
727
 
        wb.add_lines('v2', ['v1'], ['hello\n', 'world\n'])
728
 
        wa.join(wb)
729
 
        eq = self.assertEquals
730
 
        eq(sorted(wa.versions()), ['v1', 'v2', 'v3', 'x1',])
731
 
        eq(wa.get_text('x1'), 'line from x1\n')
732
667
 
733
668
    def test_written_detection(self):
734
669
        # Test detection of weave file corruption.
779
714
        self.assertRaises(errors.WeaveInvalidChecksum, w.check)
780
715
 
781
716
 
 
717
class TestWeave(TestCase):
 
718
 
 
719
    def test_allow_reserved_false(self):
 
720
        w = Weave('name', allow_reserved=False)
 
721
        # Add lines is checked at the WeaveFile level, not at the Weave level
 
722
        w.add_lines('name:', [], TEXT_1)
 
723
        # But get_lines is checked at this level
 
724
        self.assertRaises(errors.ReservedId, w.get_lines, 'name:')
 
725
 
 
726
    def test_allow_reserved_true(self):
 
727
        w = Weave('name', allow_reserved=True)
 
728
        w.add_lines('name:', [], TEXT_1)
 
729
        self.assertEqual(TEXT_1, w.get_lines('name:'))
 
730
 
 
731
 
782
732
class InstrumentedWeave(Weave):
783
733
    """Keep track of how many times functions are called."""
784
 
    
 
734
 
785
735
    def __init__(self, weave_name=None):
786
736
        self._extract_count = 0
787
737
        Weave.__init__(self, weave_name=weave_name)
791
741
        return Weave._extract(self, versions)
792
742
 
793
743
 
794
 
class JoinOptimization(TestCase):
795
 
    """Test that Weave.join() doesn't extract all texts, only what must be done."""
796
 
 
797
 
    def test_join(self):
798
 
        w1 = InstrumentedWeave()
799
 
        w2 = InstrumentedWeave()
800
 
 
801
 
        txt0 = ['a\n']
802
 
        txt1 = ['a\n', 'b\n']
803
 
        txt2 = ['a\n', 'c\n']
804
 
        txt3 = ['a\n', 'b\n', 'c\n']
805
 
 
806
 
        w1.add_lines('txt0', [], txt0) # extract 1a
807
 
        w2.add_lines('txt0', [], txt0) # extract 1b
808
 
        w1.add_lines('txt1', ['txt0'], txt1)# extract 2a
809
 
        w2.add_lines('txt2', ['txt0'], txt2)# extract 2b
810
 
        w1.join(w2) # extract 3a to add txt2 
811
 
        w2.join(w1) # extract 3b to add txt1 
812
 
 
813
 
        w1.add_lines('txt3', ['txt1', 'txt2'], txt3) # extract 4a 
814
 
        w2.add_lines('txt3', ['txt2', 'txt1'], txt3) # extract 4b
815
 
        # These secretly have inverted parents
816
 
 
817
 
        # This should not have to do any extractions
818
 
        w1.join(w2) # NO extract, texts already present with same parents
819
 
        w2.join(w1) # NO extract, texts already present with same parents
820
 
 
821
 
        self.assertEqual(4, w1._extract_count)
822
 
        self.assertEqual(4, w2._extract_count)
823
 
 
824
 
    def test_double_parent(self):
825
 
        # It should not be considered illegal to add
826
 
        # a revision with the same parent twice
827
 
        w1 = InstrumentedWeave()
828
 
        w2 = InstrumentedWeave()
829
 
 
830
 
        txt0 = ['a\n']
831
 
        txt1 = ['a\n', 'b\n']
832
 
        txt2 = ['a\n', 'c\n']
833
 
        txt3 = ['a\n', 'b\n', 'c\n']
834
 
 
835
 
        w1.add_lines('txt0', [], txt0)
836
 
        w2.add_lines('txt0', [], txt0)
837
 
        w1.add_lines('txt1', ['txt0'], txt1)
838
 
        w2.add_lines('txt1', ['txt0', 'txt0'], txt1)
839
 
        # Same text, effectively the same, because the
840
 
        # parent is only repeated
841
 
        w1.join(w2) # extract 3a to add txt2 
842
 
        w2.join(w1) # extract 3b to add txt1 
843
 
 
844
 
 
845
744
class TestNeedsReweave(TestCase):
846
745
    """Internal corner cases for when reweave is needed."""
847
746
 
861
760
 
862
761
 
863
762
class TestWeaveFile(TestCaseInTempDir):
864
 
    
 
763
 
865
764
    def test_empty_file(self):
866
765
        f = open('empty.weave', 'wb+')
867
766
        try: