3
# Copyright (C) 2005 by Canonical Ltd
1
# Copyright (C) 2005-2009, 2011 Canonical Ltd
5
3
# This program is free software; you can redistribute it and/or modify
6
4
# it under the terms of the GNU General Public License as published by
7
5
# the Free Software Foundation; either version 2 of the License, or
8
6
# (at your option) any later version.
10
8
# This program is distributed in the hope that it will be useful,
11
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
11
# GNU General Public License for more details.
15
13
# You should have received a copy of the GNU General Public License
16
14
# along with this program; if not, write to the Free Software
17
# 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
18
# TODO: tests regarding version names
19
# TODO: rbc 20050108 test that join does not leave an inconsistent weave
22
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
26
31
from bzrlib.weave import Weave, WeaveFormatError
27
32
from bzrlib.weavefile import write_weave, read_weave
28
from pprint import pformat
35
from sets import Set, ImmutableSet
37
frozenset = ImmutableSet
42
35
# texts for use in testing
61
54
self.log('serialized weave:')
62
55
self.log(tf.read())
58
self.log('parents: %s' % (k._parents == k2._parents))
59
self.log(' %r' % k._parents)
60
self.log(' %r' % k2._parents)
63
62
self.fail('read/write check failed')
65
class WeaveContains(TestBase):
66
"""Weave __contains__ operator"""
69
k = Weave(get_scope=lambda:None)
70
self.assertFalse('foo' in k)
71
k.add_lines('foo', [], TEXT_1)
72
self.assertTrue('foo' in k)
68
75
class Easy(TestBase):
73
class StoreText(TestBase):
74
"""Store and retrieve a simple text."""
77
idx = k.add([], TEXT_0)
78
self.assertEqual(k.get(idx), TEXT_0)
79
self.assertEqual(idx, 0)
83
81
class AnnotateOne(TestBase):
87
self.assertEqual(k.annotate(0),
91
class StoreTwo(TestBase):
95
idx = k.add([], TEXT_0)
96
self.assertEqual(idx, 0)
98
idx = k.add([], TEXT_1)
99
self.assertEqual(idx, 1)
101
self.assertEqual(k.get(0), TEXT_0)
102
self.assertEqual(k.get(1), TEXT_1)
104
k.dump(self.TEST_LOG)
85
k.add_lines('text0', [], TEXT_0)
86
self.assertEqual(k.annotate('text0'),
87
[('text0', TEXT_0[0])])
108
90
class InvalidAdd(TestBase):
109
91
"""Try to use invalid version number during add."""
110
93
def runTest(self):
113
self.assertRaises(IndexError,
96
self.assertRaises(errors.RevisionNotPresent,
103
class RepeatedAdd(TestBase):
104
"""Add the same version twice; harmless."""
106
def test_duplicate_add(self):
108
idx = k.add_lines('text0', [], TEXT_0)
109
idx2 = k.add_lines('text0', [], TEXT_0)
110
self.assertEqual(idx, idx2)
113
class InvalidRepeatedAdd(TestBase):
117
k.add_lines('basis', [], TEXT_0)
118
idx = k.add_lines('text0', [], TEXT_0)
119
self.assertRaises(errors.RevisionAlreadyPresent,
123
['not the same text'])
124
self.assertRaises(errors.RevisionAlreadyPresent,
127
['basis'], # not the right parents
119
131
class InsertLines(TestBase):
120
132
"""Store a revision that adds one line to the original.
124
136
def runTest(self):
127
k.add([], ['line 1'])
128
k.add([0], ['line 1', 'line 2'])
130
self.assertEqual(k.annotate(0),
133
self.assertEqual(k.get(1),
139
k.add_lines('text0', [], ['line 1'])
140
k.add_lines('text1', ['text0'], ['line 1', 'line 2'])
142
self.assertEqual(k.annotate('text0'),
143
[('text0', 'line 1')])
145
self.assertEqual(k.get_lines(1),
137
self.assertEqual(k.annotate(1),
141
k.add([0], ['line 1', 'diverged line'])
143
self.assertEqual(k.annotate(2),
145
(2, 'diverged line')])
149
self.assertEqual(k.annotate('text1'),
150
[('text0', 'line 1'),
151
('text1', 'line 2')])
153
k.add_lines('text2', ['text0'], ['line 1', 'diverged line'])
155
self.assertEqual(k.annotate('text2'),
156
[('text0', 'line 1'),
157
('text2', 'diverged line')])
147
159
text3 = ['line 1', 'middle line', 'line 2']
151
164
# self.log("changes to text3: " + pformat(list(k._delta(set([0, 1]), text3))))
153
166
self.log("k._weave=" + pformat(k._weave))
155
self.assertEqual(k.annotate(3),
168
self.assertEqual(k.annotate('text3'),
169
[('text0', 'line 1'),
170
('text3', 'middle line'),
171
('text1', 'line 2')])
160
173
# now multiple insertions at different places
175
['text0', 'text1', 'text3'],
162
176
['line 1', 'aaa', 'middle line', 'bbb', 'line 2', 'ccc'])
164
self.assertEqual(k.annotate(4),
178
self.assertEqual(k.annotate('text4'),
179
[('text0', 'line 1'),
181
('text3', 'middle line'),
174
187
class DeleteLines(TestBase):
579
k.add([], ['aaa', 'bbb'])
580
k.add([0], ['111', 'aaa', 'ccc', 'bbb'])
581
k.add([1], ['aaa', 'ccc', 'bbb', '222'])
587
class AutoMerge(TestBase):
591
texts = [['header', 'aaa', 'bbb'],
592
['header', 'aaa', 'line from 1', 'bbb'],
593
['header', 'aaa', 'bbb', 'line from 2', 'more from 2'],
600
self.log('k._weave=' + pformat(k._weave))
602
m = list(k.mash_iter([0, 1, 2]))
608
'line from 2', 'more from 2'])
604
k.add_lines([], ['aaa', 'bbb'])
605
k.add_lines([0], ['111', 'aaa', 'ccc', 'bbb'])
606
k.add_lines([1], ['aaa', 'ccc', 'bbb', '222'])
612
609
class Khayyam(TestBase):
613
610
"""Test changes to multi-line texts, and read/write"""
612
def test_multi_line_merge(self):
616
614
"""A Book of Verses underneath the Bough,
617
615
A Jug of Wine, a Loaf of Bread, -- and Thou
618
616
Beside me singing in the Wilderness --
619
617
Oh, Wilderness were Paradise enow!""",
621
619
"""A Book of Verses underneath the Bough,
622
620
A Jug of Wine, a Loaf of Bread, -- and Thou
623
621
Beside me singing in the Wilderness --
645
ver = k.add(list(parents), t)
644
ver = k.add_lines('text%d' % i,
646
parents.add('text%d' % i)
648
649
self.log("k._weave=" + pformat(k._weave))
650
651
for i, t in enumerate(texts):
651
self.assertEqual(k.get(i), t)
652
self.assertEqual(k.get_lines(i), t)
653
654
self.check_read_write(k)
657
class MergeCases(TestBase):
658
def doMerge(self, base, a, b, mp):
657
class JoinWeavesTests(TestBase):
660
super(JoinWeavesTests, self).setUp()
661
self.weave1 = Weave()
662
self.lines1 = ['hello\n']
663
self.lines3 = ['hello\n', 'cruel\n', 'world\n']
664
self.weave1.add_lines('v1', [], self.lines1)
665
self.weave1.add_lines('v2', ['v1'], ['hello\n', 'world\n'])
666
self.weave1.add_lines('v3', ['v2'], self.lines3)
668
def test_written_detection(self):
669
# Test detection of weave file corruption.
671
# Make sure that we can detect if a weave file has
672
# been corrupted. This doesn't test all forms of corruption,
673
# but it at least helps verify the data you get, is what you want.
659
674
from cStringIO import StringIO
660
from textwrap import dedent
666
w.add([], map(addcrlf, base))
667
w.add([0], map(addcrlf, a))
668
w.add([0], map(addcrlf, b))
677
w.add_lines('v1', [], ['hello\n'])
678
w.add_lines('v2', ['v1'], ['hello\n', 'there\n'])
670
self.log('weave is:')
671
680
tmpf = StringIO()
672
681
write_weave(w, tmpf)
673
self.log(tmpf.getvalue())
675
self.log('merge plan:')
676
p = list(w.plan_merge(1, 2))
677
for state, line in p:
679
self.log('%12s | %s' % (state, line[:-1]))
683
mt.writelines(w.weave_merge(p))
685
self.log(mt.getvalue())
687
mp = map(addcrlf, mp)
688
self.assertEqual(mt.readlines(), mp)
691
def testOneInsert(self):
697
def testSeparateInserts(self):
698
self.doMerge(['aaa', 'bbb', 'ccc'],
699
['aaa', 'xxx', 'bbb', 'ccc'],
700
['aaa', 'bbb', 'yyy', 'ccc'],
701
['aaa', 'xxx', 'bbb', 'yyy', 'ccc'])
703
def testSameInsert(self):
704
self.doMerge(['aaa', 'bbb', 'ccc'],
705
['aaa', 'xxx', 'bbb', 'ccc'],
706
['aaa', 'xxx', 'bbb', 'yyy', 'ccc'],
707
['aaa', 'xxx', 'bbb', 'yyy', 'ccc'])
709
def testOverlappedInsert(self):
710
self.doMerge(['aaa', 'bbb'],
711
['aaa', 'xxx', 'yyy', 'bbb'],
712
['aaa', 'xxx', 'bbb'],
713
['aaa', '<<<<', 'xxx', 'yyy', '====', 'xxx', '>>>>', 'bbb'])
715
# really it ought to reduce this to
716
# ['aaa', 'xxx', 'yyy', 'bbb']
719
def testClashReplace(self):
720
self.doMerge(['aaa'],
723
['<<<<', 'xxx', '====', 'yyy', 'zzz', '>>>>'])
725
def testNonClashInsert(self):
726
self.doMerge(['aaa'],
729
['<<<<', 'xxx', 'aaa', '====', 'yyy', 'zzz', '>>>>'])
731
self.doMerge(['aaa'],
737
def testDeleteAndModify(self):
738
"""Clashing delete and modification.
740
If one side modifies a region and the other deletes it then
741
there should be a conflict with one side blank.
744
#######################################
745
# skippd, not working yet
748
self.doMerge(['aaa', 'bbb', 'ccc'],
749
['aaa', 'ddd', 'ccc'],
751
['<<<<', 'aaa', '====', '>>>>', 'ccc'])
757
from unittest import TestSuite, TestLoader
762
suite.addTest(tl.loadTestsFromModule(testweave))
764
return int(not testsweet.run_suite(suite)) # for shell 0=true
767
if __name__ == '__main__':
769
sys.exit(testweave())
683
# Because we are corrupting, we need to make sure we have the exact text
684
self.assertEquals('# bzr weave file v5\n'
685
'i\n1 f572d396fae9206628714fb2ce00f72e94f2258f\nn v1\n\n'
686
'i 0\n1 90f265c6e75f1c8f9ab76dcf85528352c5f215ef\nn v2\n\n'
687
'w\n{ 0\n. hello\n}\n{ 1\n. there\n}\nW\n',
690
# Change a single letter
691
tmpf = StringIO('# bzr weave file v5\n'
692
'i\n1 f572d396fae9206628714fb2ce00f72e94f2258f\nn v1\n\n'
693
'i 0\n1 90f265c6e75f1c8f9ab76dcf85528352c5f215ef\nn v2\n\n'
694
'w\n{ 0\n. hello\n}\n{ 1\n. There\n}\nW\n')
698
self.assertEqual('hello\n', w.get_text('v1'))
699
self.assertRaises(errors.WeaveInvalidChecksum, w.get_text, 'v2')
700
self.assertRaises(errors.WeaveInvalidChecksum, w.get_lines, 'v2')
701
self.assertRaises(errors.WeaveInvalidChecksum, w.check)
703
# Change the sha checksum
704
tmpf = StringIO('# bzr weave file v5\n'
705
'i\n1 f572d396fae9206628714fb2ce00f72e94f2258f\nn v1\n\n'
706
'i 0\n1 f0f265c6e75f1c8f9ab76dcf85528352c5f215ef\nn v2\n\n'
707
'w\n{ 0\n. hello\n}\n{ 1\n. there\n}\nW\n')
711
self.assertEqual('hello\n', w.get_text('v1'))
712
self.assertRaises(errors.WeaveInvalidChecksum, w.get_text, 'v2')
713
self.assertRaises(errors.WeaveInvalidChecksum, w.get_lines, 'v2')
714
self.assertRaises(errors.WeaveInvalidChecksum, w.check)
717
class TestWeave(TestCase):
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:')
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:'))
732
class InstrumentedWeave(Weave):
733
"""Keep track of how many times functions are called."""
735
def __init__(self, weave_name=None):
736
self._extract_count = 0
737
Weave.__init__(self, weave_name=weave_name)
739
def _extract(self, versions):
740
self._extract_count += 1
741
return Weave._extract(self, versions)
744
class TestNeedsReweave(TestCase):
745
"""Internal corner cases for when reweave is needed."""
747
def test_compatible_parents(self):
749
my_parents = set([1, 2, 3])
751
self.assertTrue(w1._compatible_parents(my_parents, set([3])))
753
self.assertTrue(w1._compatible_parents(my_parents, set(my_parents)))
754
# same empty corner case
755
self.assertTrue(w1._compatible_parents(set(), set()))
756
# other cannot contain stuff my_parents does not
757
self.assertFalse(w1._compatible_parents(set(), set([1])))
758
self.assertFalse(w1._compatible_parents(my_parents, set([1, 2, 3, 4])))
759
self.assertFalse(w1._compatible_parents(my_parents, set([4])))
762
class TestWeaveFile(TestCaseInTempDir):
764
def test_empty_file(self):
765
f = open('empty.weave', 'wb+')
767
self.assertRaises(errors.WeaveFormatError,