1
# Copyright (C) 2005 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
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
18
# TODO: tests regarding version names
19
# TODO: rbc 20050108 test that join does not leave an inconsistent weave
22
"""test suite for weave algorithm"""
24
from pprint import pformat
29
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
35
# texts for use in testing
36
TEXT_0 = ["Hello world"]
37
TEXT_1 = ["Hello world",
41
class TestBase(TestCase):
43
def check_read_write(self, k):
44
"""Check the weave k can be written & re-read."""
45
from tempfile import TemporaryFile
54
self.log('serialized weave:')
58
self.log('parents: %s' % (k._parents == k2._parents))
59
self.log(' %r' % k._parents)
60
self.log(' %r' % k2._parents)
62
self.fail('read/write check failed')
65
class WeaveContains(TestBase):
66
"""Weave __contains__ operator"""
69
self.assertFalse('foo' in k)
70
k.add_lines('foo', [], TEXT_1)
71
self.assertTrue('foo' in k)
79
class AnnotateOne(TestBase):
82
k.add_lines('text0', [], TEXT_0)
83
self.assertEqual(k.annotate('text0'),
84
[('text0', TEXT_0[0])])
87
class GetSha1(TestBase):
88
def test_get_sha1(self):
90
k.add_lines('text0', [], 'text0')
91
self.assertEqual('34dc0e430c642a26c3dd1c2beb7a8b4f4445eb79',
93
self.assertRaises(errors.RevisionNotPresent,
95
self.assertRaises(errors.RevisionNotPresent,
99
class InvalidAdd(TestBase):
100
"""Try to use invalid version number during add."""
104
self.assertRaises(errors.RevisionNotPresent,
111
class RepeatedAdd(TestBase):
112
"""Add the same version twice; harmless."""
114
def test_duplicate_add(self):
116
idx = k.add_lines('text0', [], TEXT_0)
117
idx2 = k.add_lines('text0', [], TEXT_0)
118
self.assertEqual(idx, idx2)
121
class InvalidRepeatedAdd(TestBase):
124
k.add_lines('basis', [], TEXT_0)
125
idx = k.add_lines('text0', [], TEXT_0)
126
self.assertRaises(errors.RevisionAlreadyPresent,
130
['not the same text'])
131
self.assertRaises(errors.RevisionAlreadyPresent,
134
['basis'], # not the right parents
138
class InsertLines(TestBase):
139
"""Store a revision that adds one line to the original.
141
Look at the annotations to make sure that the first line is matched
142
and not stored repeatedly."""
146
k.add_lines('text0', [], ['line 1'])
147
k.add_lines('text1', ['text0'], ['line 1', 'line 2'])
149
self.assertEqual(k.annotate('text0'),
150
[('text0', 'line 1')])
152
self.assertEqual(k.get_lines(1),
156
self.assertEqual(k.annotate('text1'),
157
[('text0', 'line 1'),
158
('text1', 'line 2')])
160
k.add_lines('text2', ['text0'], ['line 1', 'diverged line'])
162
self.assertEqual(k.annotate('text2'),
163
[('text0', 'line 1'),
164
('text2', 'diverged line')])
166
text3 = ['line 1', 'middle line', 'line 2']
171
# self.log("changes to text3: " + pformat(list(k._delta(set([0, 1]), text3))))
173
self.log("k._weave=" + pformat(k._weave))
175
self.assertEqual(k.annotate('text3'),
176
[('text0', 'line 1'),
177
('text3', 'middle line'),
178
('text1', 'line 2')])
180
# now multiple insertions at different places
182
['text0', 'text1', 'text3'],
183
['line 1', 'aaa', 'middle line', 'bbb', 'line 2', 'ccc'])
185
self.assertEqual(k.annotate('text4'),
186
[('text0', 'line 1'),
188
('text3', 'middle line'),
194
class DeleteLines(TestBase):
195
"""Deletion of lines from existing text.
197
Try various texts all based on a common ancestor."""
201
base_text = ['one', 'two', 'three', 'four']
203
k.add_lines('text0', [], base_text)
205
texts = [['one', 'two', 'three'],
206
['two', 'three', 'four'],
208
['one', 'two', 'three', 'four'],
213
ver = k.add_lines('text%d' % i,
217
self.log('final weave:')
218
self.log('k._weave=' + pformat(k._weave))
220
for i in range(len(texts)):
221
self.assertEqual(k.get_lines(i+1),
225
class SuicideDelete(TestBase):
226
"""Invalid weave which tries to add and delete simultaneously."""
232
k._weave = [('{', 0),
239
################################### SKIPPED
240
# Weave.get doesn't trap this anymore
243
self.assertRaises(WeaveFormatError,
248
class CannedDelete(TestBase):
249
"""Unpack canned weave with deleted lines."""
256
k._weave = [('{', 0),
259
'line to be deleted',
264
k._sha1s = [sha_string('first lineline to be deletedlast line')
265
, sha_string('first linelast line')]
267
self.assertEqual(k.get_lines(0),
269
'line to be deleted',
273
self.assertEqual(k.get_lines(1),
279
class CannedReplacement(TestBase):
280
"""Unpack canned weave with deleted lines."""
284
k._parents = [frozenset(),
287
k._weave = [('{', 0),
290
'line to be deleted',
298
k._sha1s = [sha_string('first lineline to be deletedlast line')
299
, sha_string('first linereplacement linelast line')]
301
self.assertEqual(k.get_lines(0),
303
'line to be deleted',
307
self.assertEqual(k.get_lines(1),
314
class BadWeave(TestBase):
315
"""Test that we trap an insert which should not occur."""
319
k._parents = [frozenset(),
321
k._weave = ['bad line',
325
' added in version 1',
334
################################### SKIPPED
335
# Weave.get doesn't trap this anymore
339
self.assertRaises(WeaveFormatError,
344
class BadInsert(TestBase):
345
"""Test that we trap an insert which should not occur."""
349
k._parents = [frozenset(),
354
k._weave = [('{', 0),
357
' added in version 1',
365
# this is not currently enforced by get
366
return ##########################################
368
self.assertRaises(WeaveFormatError,
372
self.assertRaises(WeaveFormatError,
377
class InsertNested(TestBase):
378
"""Insertion with nested instructions."""
382
k._parents = [frozenset(),
387
k._weave = [('{', 0),
390
' added in version 1',
399
k._sha1s = [sha_string('foo {}')
400
, sha_string('foo { added in version 1 also from v1}')
401
, sha_string('foo { added in v2}')
402
, sha_string('foo { added in version 1 added in v2 also from v1}')
405
self.assertEqual(k.get_lines(0),
409
self.assertEqual(k.get_lines(1),
411
' added in version 1',
415
self.assertEqual(k.get_lines(2),
420
self.assertEqual(k.get_lines(3),
422
' added in version 1',
428
class DeleteLines2(TestBase):
429
"""Test recording revisions that delete lines.
431
This relies on the weave having a way to represent lines knocked
432
out by a later revision."""
436
k.add_lines('text0', [], ["line the first",
441
self.assertEqual(len(k.get_lines(0)), 4)
443
k.add_lines('text1', ['text0'], ["line the first",
446
self.assertEqual(k.get_lines(1),
450
self.assertEqual(k.annotate('text1'),
451
[('text0', "line the first"),
455
class IncludeVersions(TestBase):
456
"""Check texts that are stored across multiple revisions.
458
Here we manually create a weave with particular encoding and make
459
sure it unpacks properly.
461
Text 0 includes nothing; text 1 includes text 0 and adds some
468
k._parents = [frozenset(), frozenset([0])]
469
k._weave = [('{', 0),
476
k._sha1s = [sha_string('first line')
477
, sha_string('first linesecond line')]
479
self.assertEqual(k.get_lines(1),
483
self.assertEqual(k.get_lines(0),
487
class DivergedIncludes(TestBase):
488
"""Weave with two diverged texts based on version 0.
491
# FIXME make the weave, dont poke at it.
494
k._names = ['0', '1', '2']
495
k._name_map = {'0':0, '1':1, '2':2}
496
k._parents = [frozenset(),
500
k._weave = [('{', 0),
507
"alternative second line",
511
k._sha1s = [sha_string('first line')
512
, sha_string('first linesecond line')
513
, sha_string('first linealternative second line')]
515
self.assertEqual(k.get_lines(0),
518
self.assertEqual(k.get_lines(1),
522
self.assertEqual(k.get_lines('2'),
524
"alternative second line"])
526
self.assertEqual(list(k.get_ancestry(['2'])),
530
class ReplaceLine(TestBase):
534
text0 = ['cheddar', 'stilton', 'gruyere']
535
text1 = ['cheddar', 'blue vein', 'neufchatel', 'chevre']
537
k.add_lines('text0', [], text0)
538
k.add_lines('text1', ['text0'], text1)
540
self.log('k._weave=' + pformat(k._weave))
542
self.assertEqual(k.get_lines(0), text0)
543
self.assertEqual(k.get_lines(1), text1)
546
class Merge(TestBase):
547
"""Storage of versions that merge diverged parents"""
552
['header', '', 'line from 1'],
553
['header', '', 'line from 2', 'more from 2'],
554
['header', '', 'line from 1', 'fixup line', 'line from 2'],
557
k.add_lines('text0', [], texts[0])
558
k.add_lines('text1', ['text0'], texts[1])
559
k.add_lines('text2', ['text0'], texts[2])
560
k.add_lines('merge', ['text0', 'text1', 'text2'], texts[3])
562
for i, t in enumerate(texts):
563
self.assertEqual(k.get_lines(i), t)
565
self.assertEqual(k.annotate('merge'),
566
[('text0', 'header'),
568
('text1', 'line from 1'),
569
('merge', 'fixup line'),
570
('text2', 'line from 2'),
573
self.assertEqual(list(k.get_ancestry(['merge'])),
574
['text0', 'text1', 'text2', 'merge'])
576
self.log('k._weave=' + pformat(k._weave))
578
self.check_read_write(k)
581
class Conflicts(TestBase):
582
"""Test detection of conflicting regions during a merge.
584
A base version is inserted, then two descendents try to
585
insert different lines in the same place. These should be
586
reported as a possible conflict and forwarded to the user."""
591
k.add_lines([], ['aaa', 'bbb'])
592
k.add_lines([0], ['aaa', '111', 'bbb'])
593
k.add_lines([1], ['aaa', '222', 'bbb'])
595
merged = k.merge([1, 2])
597
self.assertEquals([[['aaa']],
602
class NonConflict(TestBase):
603
"""Two descendants insert compatible changes.
605
No conflict should be reported."""
610
k.add_lines([], ['aaa', 'bbb'])
611
k.add_lines([0], ['111', 'aaa', 'ccc', 'bbb'])
612
k.add_lines([1], ['aaa', 'ccc', 'bbb', '222'])
615
class Khayyam(TestBase):
616
"""Test changes to multi-line texts, and read/write"""
618
def test_multi_line_merge(self):
620
"""A Book of Verses underneath the Bough,
621
A Jug of Wine, a Loaf of Bread, -- and Thou
622
Beside me singing in the Wilderness --
623
Oh, Wilderness were Paradise enow!""",
625
"""A Book of Verses underneath the Bough,
626
A Jug of Wine, a Loaf of Bread, -- and Thou
627
Beside me singing in the Wilderness --
628
Oh, Wilderness were Paradise now!""",
630
"""A Book of poems underneath the tree,
631
A Jug of Wine, a Loaf of Bread,
633
Beside me singing in the Wilderness --
634
Oh, Wilderness were Paradise now!
638
"""A Book of Verses underneath the Bough,
639
A Jug of Wine, a Loaf of Bread,
641
Beside me singing in the Wilderness --
642
Oh, Wilderness were Paradise now!""",
644
texts = [[l.strip() for l in t.split('\n')] for t in rawtexts]
650
ver = k.add_lines('text%d' % i,
652
parents.add('text%d' % i)
655
self.log("k._weave=" + pformat(k._weave))
657
for i, t in enumerate(texts):
658
self.assertEqual(k.get_lines(i), t)
660
self.check_read_write(k)
663
class JoinWeavesTests(TestBase):
665
super(JoinWeavesTests, self).setUp()
666
self.weave1 = Weave()
667
self.lines1 = ['hello\n']
668
self.lines3 = ['hello\n', 'cruel\n', 'world\n']
669
self.weave1.add_lines('v1', [], self.lines1)
670
self.weave1.add_lines('v2', ['v1'], ['hello\n', 'world\n'])
671
self.weave1.add_lines('v3', ['v2'], self.lines3)
673
def test_join_empty(self):
674
"""Join two empty weaves."""
675
eq = self.assertEqual
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)
686
def test_join_unrelated(self):
687
"""Join two weaves with no history in common."""
689
wb.add_lines('b1', [], ['line from b\n'])
692
eq = self.assertEqual
694
eq(sorted(w1.versions()),
695
['b1', 'v1', 'v2', 'v3'])
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
707
eq(wa.get_lines('b1'),
708
['hello\n', 'pale blue\n', 'world\n'])
710
def test_join_text_disagreement(self):
711
"""Cannot join weaves with different texts for a version."""
714
wa.add_lines('v1', [], ['hello\n'])
715
wb.add_lines('v1', [], ['not\n', 'hello\n'])
716
self.assertRaises(WeaveError,
719
def test_join_unordered(self):
720
"""Join weaves where indexes differ.
722
The source weave contains a different version at index 0."""
723
wa = self.weave1.copy()
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'])
729
eq = self.assertEquals
730
eq(sorted(wa.versions()), ['v1', 'v2', 'v3', 'x1',])
731
eq(wa.get_text('x1'), 'line from x1\n')
733
def test_written_detection(self):
734
# Test detection of weave file corruption.
736
# Make sure that we can detect if a weave file has
737
# been corrupted. This doesn't test all forms of corruption,
738
# but it at least helps verify the data you get, is what you want.
739
from cStringIO import StringIO
742
w.add_lines('v1', [], ['hello\n'])
743
w.add_lines('v2', ['v1'], ['hello\n', 'there\n'])
748
# Because we are corrupting, we need to make sure we have the exact text
749
self.assertEquals('# bzr weave file v5\n'
750
'i\n1 f572d396fae9206628714fb2ce00f72e94f2258f\nn v1\n\n'
751
'i 0\n1 90f265c6e75f1c8f9ab76dcf85528352c5f215ef\nn v2\n\n'
752
'w\n{ 0\n. hello\n}\n{ 1\n. there\n}\nW\n',
755
# Change a single letter
756
tmpf = StringIO('# bzr weave file v5\n'
757
'i\n1 f572d396fae9206628714fb2ce00f72e94f2258f\nn v1\n\n'
758
'i 0\n1 90f265c6e75f1c8f9ab76dcf85528352c5f215ef\nn v2\n\n'
759
'w\n{ 0\n. hello\n}\n{ 1\n. There\n}\nW\n')
763
self.assertEqual('hello\n', w.get_text('v1'))
764
self.assertRaises(errors.WeaveInvalidChecksum, w.get_text, 'v2')
765
self.assertRaises(errors.WeaveInvalidChecksum, w.get_lines, 'v2')
766
self.assertRaises(errors.WeaveInvalidChecksum, w.check)
768
# Change the sha checksum
769
tmpf = StringIO('# bzr weave file v5\n'
770
'i\n1 f572d396fae9206628714fb2ce00f72e94f2258f\nn v1\n\n'
771
'i 0\n1 f0f265c6e75f1c8f9ab76dcf85528352c5f215ef\nn v2\n\n'
772
'w\n{ 0\n. hello\n}\n{ 1\n. there\n}\nW\n')
776
self.assertEqual('hello\n', w.get_text('v1'))
777
self.assertRaises(errors.WeaveInvalidChecksum, w.get_text, 'v2')
778
self.assertRaises(errors.WeaveInvalidChecksum, w.get_lines, 'v2')
779
self.assertRaises(errors.WeaveInvalidChecksum, w.check)
782
class InstrumentedWeave(Weave):
783
"""Keep track of how many times functions are called."""
785
def __init__(self, weave_name=None):
786
self._extract_count = 0
787
Weave.__init__(self, weave_name=weave_name)
789
def _extract(self, versions):
790
self._extract_count += 1
791
return Weave._extract(self, versions)
794
class JoinOptimization(TestCase):
795
"""Test that Weave.join() doesn't extract all texts, only what must be done."""
798
w1 = InstrumentedWeave()
799
w2 = InstrumentedWeave()
802
txt1 = ['a\n', 'b\n']
803
txt2 = ['a\n', 'c\n']
804
txt3 = ['a\n', 'b\n', 'c\n']
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
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
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
821
self.assertEqual(4, w1._extract_count)
822
self.assertEqual(4, w2._extract_count)
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()
831
txt1 = ['a\n', 'b\n']
832
txt2 = ['a\n', 'c\n']
833
txt3 = ['a\n', 'b\n', 'c\n']
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
845
class TestNeedsReweave(TestCase):
846
"""Internal corner cases for when reweave is needed."""
848
def test_compatible_parents(self):
850
my_parents = set([1, 2, 3])
852
self.assertTrue(w1._compatible_parents(my_parents, set([3])))
854
self.assertTrue(w1._compatible_parents(my_parents, set(my_parents)))
855
# same empty corner case
856
self.assertTrue(w1._compatible_parents(set(), set()))
857
# other cannot contain stuff my_parents does not
858
self.assertFalse(w1._compatible_parents(set(), set([1])))
859
self.assertFalse(w1._compatible_parents(my_parents, set([1, 2, 3, 4])))
860
self.assertFalse(w1._compatible_parents(my_parents, set([4])))
863
class TestWeaveFile(TestCaseInTempDir):
865
def test_empty_file(self):
866
f = open('empty.weave', 'wb+')
868
self.assertRaises(errors.WeaveFormatError,