3
# Copyright (C) 2005 by Canonical Ltd
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
# TODO: tests regarding version names
24
"""test suite for weave algorithm"""
26
from pprint import pformat
29
from bzrlib.weave import Weave, WeaveFormatError, WeaveError
30
from bzrlib.weavefile import write_weave, read_weave
31
from bzrlib.selftest import TestCase
32
from bzrlib.osutils import sha_string
38
from sets import Set, ImmutableSet
40
frozenset = ImmutableSet
45
# texts for use in testing
46
TEXT_0 = ["Hello world"]
47
TEXT_1 = ["Hello world",
52
class TestBase(TestCase):
53
def check_read_write(self, k):
54
"""Check the weave k can be written & re-read."""
55
from tempfile import TemporaryFile
64
self.log('serialized weave:')
68
self.log('parents: %s' % (k._parents == k2._parents))
69
self.log(' %r' % k._parents)
70
self.log(' %r' % k2._parents)
74
self.fail('read/write check failed')
84
class StoreText(TestBase):
85
"""Store and retrieve a simple text."""
88
idx = k.add('text0', [], TEXT_0)
89
self.assertEqual(k.get(idx), TEXT_0)
90
self.assertEqual(idx, 0)
94
class AnnotateOne(TestBase):
97
k.add('text0', [], TEXT_0)
98
self.assertEqual(k.annotate(0),
102
class StoreTwo(TestBase):
106
idx = k.add('text0', [], TEXT_0)
107
self.assertEqual(idx, 0)
109
idx = k.add('text1', [], TEXT_1)
110
self.assertEqual(idx, 1)
112
self.assertEqual(k.get(0), TEXT_0)
113
self.assertEqual(k.get(1), TEXT_1)
117
class AddWithGivenSha(TestBase):
119
"""Add with caller-supplied SHA-1"""
123
k.add('text0', [], [t], sha1=sha_string(t))
127
class InvalidAdd(TestBase):
128
"""Try to use invalid version number during add."""
132
self.assertRaises(IndexError,
139
class RepeatedAdd(TestBase):
140
"""Add the same version twice; harmless."""
143
idx = k.add('text0', [], TEXT_0)
144
idx2 = k.add('text0', [], TEXT_0)
145
self.assertEqual(idx, idx2)
149
class InvalidRepeatedAdd(TestBase):
152
idx = k.add('text0', [], TEXT_0)
153
self.assertRaises(WeaveError,
157
['not the same text'])
158
self.assertRaises(WeaveError,
161
[12], # not the right parents
166
class InsertLines(TestBase):
167
"""Store a revision that adds one line to the original.
169
Look at the annotations to make sure that the first line is matched
170
and not stored repeatedly."""
174
k.add('text0', [], ['line 1'])
175
k.add('text1', [0], ['line 1', 'line 2'])
177
self.assertEqual(k.annotate(0),
180
self.assertEqual(k.get(1),
184
self.assertEqual(k.annotate(1),
188
k.add('text2', [0], ['line 1', 'diverged line'])
190
self.assertEqual(k.annotate(2),
192
(2, 'diverged line')])
194
text3 = ['line 1', 'middle line', 'line 2']
199
# self.log("changes to text3: " + pformat(list(k._delta(set([0, 1]), text3))))
201
self.log("k._weave=" + pformat(k._weave))
203
self.assertEqual(k.annotate(3),
208
# now multiple insertions at different places
211
['line 1', 'aaa', 'middle line', 'bbb', 'line 2', 'ccc'])
213
self.assertEqual(k.annotate(4),
223
class DeleteLines(TestBase):
224
"""Deletion of lines from existing text.
226
Try various texts all based on a common ancestor."""
230
base_text = ['one', 'two', 'three', 'four']
232
k.add('text0', [], base_text)
234
texts = [['one', 'two', 'three'],
235
['two', 'three', 'four'],
237
['one', 'two', 'three', 'four'],
242
ver = k.add('text%d' % i,
246
self.log('final weave:')
247
self.log('k._weave=' + pformat(k._weave))
249
for i in range(len(texts)):
250
self.assertEqual(k.get(i+1),
256
class SuicideDelete(TestBase):
257
"""Invalid weave which tries to add and delete simultaneously."""
263
k._weave = [('{', 0),
270
################################### SKIPPED
271
# Weave.get doesn't trap this anymore
274
self.assertRaises(WeaveFormatError,
280
class CannedDelete(TestBase):
281
"""Unpack canned weave with deleted lines."""
288
k._weave = [('{', 0),
291
'line to be deleted',
297
self.assertEqual(k.get(0),
299
'line to be deleted',
303
self.assertEqual(k.get(1),
310
class CannedReplacement(TestBase):
311
"""Unpack canned weave with deleted lines."""
315
k._parents = [frozenset(),
318
k._weave = [('{', 0),
321
'line to be deleted',
330
self.assertEqual(k.get(0),
332
'line to be deleted',
336
self.assertEqual(k.get(1),
344
class BadWeave(TestBase):
345
"""Test that we trap an insert which should not occur."""
349
k._parents = [frozenset(),
351
k._weave = ['bad line',
355
' added in version 1',
364
################################### SKIPPED
365
# Weave.get doesn't trap this anymore
369
self.assertRaises(WeaveFormatError,
374
class BadInsert(TestBase):
375
"""Test that we trap an insert which should not occur."""
379
k._parents = [frozenset(),
384
k._weave = [('{', 0),
387
' added in version 1',
395
# this is not currently enforced by get
396
return ##########################################
398
self.assertRaises(WeaveFormatError,
402
self.assertRaises(WeaveFormatError,
407
class InsertNested(TestBase):
408
"""Insertion with nested instructions."""
412
k._parents = [frozenset(),
417
k._weave = [('{', 0),
420
' added in version 1',
429
self.assertEqual(k.get(0),
433
self.assertEqual(k.get(1),
435
' added in version 1',
439
self.assertEqual(k.get(2),
444
self.assertEqual(k.get(3),
446
' added in version 1',
453
class DeleteLines2(TestBase):
454
"""Test recording revisions that delete lines.
456
This relies on the weave having a way to represent lines knocked
457
out by a later revision."""
461
k.add('text0', [], ["line the first",
466
self.assertEqual(len(k.get(0)), 4)
468
k.add('text1', [0], ["line the first",
471
self.assertEqual(k.get(1),
475
self.assertEqual(k.annotate(1),
476
[(0, "line the first"),
481
class IncludeVersions(TestBase):
482
"""Check texts that are stored across multiple revisions.
484
Here we manually create a weave with particular encoding and make
485
sure it unpacks properly.
487
Text 0 includes nothing; text 1 includes text 0 and adds some
494
k._parents = [frozenset(), frozenset([0])]
495
k._weave = [('{', 0),
502
self.assertEqual(k.get(1),
506
self.assertEqual(k.get(0),
510
class DivergedIncludes(TestBase):
511
"""Weave with two diverged texts based on version 0.
516
k._parents = [frozenset(),
520
k._weave = [('{', 0),
527
"alternative second line",
531
self.assertEqual(k.get(0),
534
self.assertEqual(k.get(1),
538
self.assertEqual(k.get(2),
540
"alternative second line"])
542
self.assertEqual(list(k.inclusions([2])),
547
class ReplaceLine(TestBase):
551
text0 = ['cheddar', 'stilton', 'gruyere']
552
text1 = ['cheddar', 'blue vein', 'neufchatel', 'chevre']
554
k.add('text0', [], text0)
555
k.add('text1', [0], text1)
557
self.log('k._weave=' + pformat(k._weave))
559
self.assertEqual(k.get(0), text0)
560
self.assertEqual(k.get(1), text1)
564
class Merge(TestBase):
565
"""Storage of versions that merge diverged parents"""
570
['header', '', 'line from 1'],
571
['header', '', 'line from 2', 'more from 2'],
572
['header', '', 'line from 1', 'fixup line', 'line from 2'],
575
k.add('text0', [], texts[0])
576
k.add('text1', [0], texts[1])
577
k.add('text2', [0], texts[2])
578
k.add('merge', [0, 1, 2], texts[3])
580
for i, t in enumerate(texts):
581
self.assertEqual(k.get(i), t)
583
self.assertEqual(k.annotate(3),
591
self.assertEqual(list(k.inclusions([3])),
594
self.log('k._weave=' + pformat(k._weave))
596
self.check_read_write(k)
599
class Conflicts(TestBase):
600
"""Test detection of conflicting regions during a merge.
602
A base version is inserted, then two descendents try to
603
insert different lines in the same place. These should be
604
reported as a possible conflict and forwarded to the user."""
609
k.add([], ['aaa', 'bbb'])
610
k.add([0], ['aaa', '111', 'bbb'])
611
k.add([1], ['aaa', '222', 'bbb'])
613
merged = k.merge([1, 2])
615
self.assertEquals([[['aaa']],
621
class NonConflict(TestBase):
622
"""Two descendants insert compatible changes.
624
No conflict should be reported."""
629
k.add([], ['aaa', 'bbb'])
630
k.add([0], ['111', 'aaa', 'ccc', 'bbb'])
631
k.add([1], ['aaa', 'ccc', 'bbb', '222'])
637
class AutoMerge(TestBase):
641
texts = [['header', 'aaa', 'bbb'],
642
['header', 'aaa', 'line from 1', 'bbb'],
643
['header', 'aaa', 'bbb', 'line from 2', 'more from 2'],
646
k.add('text0', [], texts[0])
647
k.add('text1', [0], texts[1])
648
k.add('text2', [0], texts[2])
650
self.log('k._weave=' + pformat(k._weave))
652
m = list(k.mash_iter([0, 1, 2]))
658
'line from 2', 'more from 2'])
662
class Khayyam(TestBase):
663
"""Test changes to multi-line texts, and read/write"""
666
"""A Book of Verses underneath the Bough,
667
A Jug of Wine, a Loaf of Bread, -- and Thou
668
Beside me singing in the Wilderness --
669
Oh, Wilderness were Paradise enow!""",
671
"""A Book of Verses underneath the Bough,
672
A Jug of Wine, a Loaf of Bread, -- and Thou
673
Beside me singing in the Wilderness --
674
Oh, Wilderness were Paradise now!""",
676
"""A Book of poems underneath the tree,
677
A Jug of Wine, a Loaf of Bread,
679
Beside me singing in the Wilderness --
680
Oh, Wilderness were Paradise now!
684
"""A Book of Verses underneath the Bough,
685
A Jug of Wine, a Loaf of Bread,
687
Beside me singing in the Wilderness --
688
Oh, Wilderness were Paradise now!""",
690
texts = [[l.strip() for l in t.split('\n')] for t in rawtexts]
696
ver = k.add('text%d' % i,
701
self.log("k._weave=" + pformat(k._weave))
703
for i, t in enumerate(texts):
704
self.assertEqual(k.get(i), t)
706
self.check_read_write(k)
710
class MergeCases(TestBase):
711
def doMerge(self, base, a, b, mp):
712
from cStringIO import StringIO
713
from textwrap import dedent
719
w.add('text0', [], map(addcrlf, base))
720
w.add('text1', [0], map(addcrlf, a))
721
w.add('text2', [0], map(addcrlf, b))
723
self.log('weave is:')
726
self.log(tmpf.getvalue())
728
self.log('merge plan:')
729
p = list(w.plan_merge(1, 2))
730
for state, line in p:
732
self.log('%12s | %s' % (state, line[:-1]))
736
mt.writelines(w.weave_merge(p))
738
self.log(mt.getvalue())
740
mp = map(addcrlf, mp)
741
self.assertEqual(mt.readlines(), mp)
744
def testOneInsert(self):
750
def testSeparateInserts(self):
751
self.doMerge(['aaa', 'bbb', 'ccc'],
752
['aaa', 'xxx', 'bbb', 'ccc'],
753
['aaa', 'bbb', 'yyy', 'ccc'],
754
['aaa', 'xxx', 'bbb', 'yyy', 'ccc'])
756
def testSameInsert(self):
757
self.doMerge(['aaa', 'bbb', 'ccc'],
758
['aaa', 'xxx', 'bbb', 'ccc'],
759
['aaa', 'xxx', 'bbb', 'yyy', 'ccc'],
760
['aaa', 'xxx', 'bbb', 'yyy', 'ccc'])
762
def testOverlappedInsert(self):
763
self.doMerge(['aaa', 'bbb'],
764
['aaa', 'xxx', 'yyy', 'bbb'],
765
['aaa', 'xxx', 'bbb'],
766
['aaa', '<<<<', 'xxx', 'yyy', '====', 'xxx', '>>>>', 'bbb'])
768
# really it ought to reduce this to
769
# ['aaa', 'xxx', 'yyy', 'bbb']
772
def testClashReplace(self):
773
self.doMerge(['aaa'],
776
['<<<<', 'xxx', '====', 'yyy', 'zzz', '>>>>'])
778
def testNonClashInsert(self):
779
self.doMerge(['aaa'],
782
['<<<<', 'xxx', 'aaa', '====', 'yyy', 'zzz', '>>>>'])
784
self.doMerge(['aaa'],
790
def testDeleteAndModify(self):
791
"""Clashing delete and modification.
793
If one side modifies a region and the other deletes it then
794
there should be a conflict with one side blank.
797
#######################################
798
# skippd, not working yet
801
self.doMerge(['aaa', 'bbb', 'ccc'],
802
['aaa', 'ddd', 'ccc'],
804
['<<<<', 'aaa', '====', '>>>>', 'ccc'])
808
if __name__ == '__main__':
811
sys.exit(unittest.main())