1
# Copyright (C) 2005 Canonical Ltd
3
# Copyright (C) 2005 by Canonical Ltd
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.
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.
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
18
# TODO: tests regarding version names
19
# TODO: rbc 20050108 test that join does not leave an inconsistent weave
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
22
"""test suite for weave algorithm"""
26
from bzrlib.weave import Weave, WeaveFormatError
27
from bzrlib.weavefile import write_weave, read_weave
24
28
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
36
from sets import Set, ImmutableSet
38
frozenset = ImmutableSet
35
43
# texts for use in testing
74
class StoreText(TestBase):
75
"""Store and retrieve a simple text."""
78
idx = k.add([], TEXT_0)
79
self.assertEqual(k.get(idx), TEXT_0)
80
self.assertEqual(idx, 0)
79
84
class AnnotateOne(TestBase):
82
k.add_lines('text0', [], TEXT_0)
83
self.assertEqual(k.annotate('text0'),
84
[('text0', TEXT_0[0])])
88
self.assertEqual(k.annotate(0),
92
class StoreTwo(TestBase):
96
idx = k.add([], TEXT_0)
97
self.assertEqual(idx, 0)
99
idx = k.add([], TEXT_1)
100
self.assertEqual(idx, 1)
102
self.assertEqual(k.get(0), TEXT_0)
103
self.assertEqual(k.get(1), TEXT_1)
105
k.dump(self.TEST_LOG)
109
class DeltaAdd(TestBase):
110
"""Detection of changes prior to inserting new revision."""
113
k.add([], ['line 1'])
115
self.assertEqual(k._l,
121
changes = list(k._delta(set([0]),
125
self.log('raw changes: ' + pformat(changes))
127
# currently there are 3 lines in the weave, and we insert after them
128
self.assertEquals(changes,
129
[(3, 3, ['new line'])])
131
changes = k._delta(set([0]),
135
self.assertEquals(list(changes),
136
[(1, 1, ['top line'])])
138
self.check_read_write(k)
87
141
class InvalidAdd(TestBase):
89
143
def runTest(self):
92
self.assertRaises(errors.RevisionNotPresent,
146
self.assertRaises(ValueError,
99
class RepeatedAdd(TestBase):
100
"""Add the same version twice; harmless."""
102
def test_duplicate_add(self):
104
idx = k.add_lines('text0', [], TEXT_0)
105
idx2 = k.add_lines('text0', [], TEXT_0)
106
self.assertEqual(idx, idx2)
109
class InvalidRepeatedAdd(TestBase):
112
k.add_lines('basis', [], TEXT_0)
113
idx = k.add_lines('text0', [], TEXT_0)
114
self.assertRaises(errors.RevisionAlreadyPresent,
118
['not the same text'])
119
self.assertRaises(errors.RevisionAlreadyPresent,
122
['basis'], # not the right parents
126
152
class InsertLines(TestBase):
127
153
"""Store a revision that adds one line to the original.
131
157
def runTest(self):
134
k.add_lines('text0', [], ['line 1'])
135
k.add_lines('text1', ['text0'], ['line 1', 'line 2'])
137
self.assertEqual(k.annotate('text0'),
138
[('text0', 'line 1')])
140
self.assertEqual(k.get_lines(1),
160
k.add([], ['line 1'])
161
k.add([0], ['line 1', 'line 2'])
163
self.assertEqual(k.annotate(0),
166
self.assertEqual(k.get(1),
144
self.assertEqual(k.annotate('text1'),
145
[('text0', 'line 1'),
146
('text1', 'line 2')])
148
k.add_lines('text2', ['text0'], ['line 1', 'diverged line'])
150
self.assertEqual(k.annotate('text2'),
151
[('text0', 'line 1'),
152
('text2', 'diverged line')])
170
self.assertEqual(k.annotate(1),
174
k.add([0], ['line 1', 'diverged line'])
176
self.assertEqual(k.annotate(2),
178
(2, 'diverged line')])
154
180
text3 = ['line 1', 'middle line', 'line 2']
159
# self.log("changes to text3: " + pformat(list(k._delta(set([0, 1]), text3))))
161
self.log("k._weave=" + pformat(k._weave))
163
self.assertEqual(k.annotate('text3'),
164
[('text0', 'line 1'),
165
('text3', 'middle line'),
166
('text1', 'line 2')])
184
self.log("changes to text3: " + pformat(list(k._delta(set([0, 1]), text3))))
186
self.log("k._l=" + pformat(k._l))
188
self.assertEqual(k.annotate(3),
168
193
# now multiple insertions at different places
170
['text0', 'text1', 'text3'],
171
195
['line 1', 'aaa', 'middle line', 'bbb', 'line 2', 'ccc'])
173
self.assertEqual(k.annotate('text4'),
174
[('text0', 'line 1'),
176
('text3', 'middle line'),
197
self.assertEqual(k.annotate(4),
182
207
class DeleteLines(TestBase):
189
214
base_text = ['one', 'two', 'three', 'four']
191
k.add_lines('text0', [], base_text)
193
218
texts = [['one', 'two', 'three'],
194
219
['two', 'three', 'four'],
196
221
['one', 'two', 'three', 'four'],
201
ver = k.add_lines('text%d' % i,
205
227
self.log('final weave:')
206
self.log('k._weave=' + pformat(k._weave))
228
self.log('k._l=' + pformat(k._l))
208
230
for i in range(len(texts)):
209
self.assertEqual(k.get_lines(i+1),
231
self.assertEqual(k.get(i+1),
213
237
class SuicideDelete(TestBase):
252
k._sha1s = [sha_string('first lineline to be deletedlast line')
253
, sha_string('first linelast line')]
255
self.assertEqual(k.get_lines(0),
278
self.assertEqual(k.get(0),
257
280
'line to be deleted',
261
self.assertEqual(k.get_lines(1),
284
self.assertEqual(k.get(1),
267
291
class CannedReplacement(TestBase):
268
292
"""Unpack canned weave with deleted lines."""
269
293
def runTest(self):
272
k._parents = [frozenset(),
275
k._weave = [('{', 0),
278
302
'line to be deleted',
286
k._sha1s = [sha_string('first lineline to be deletedlast line')
287
, sha_string('first linereplacement linelast line')]
289
self.assertEqual(k.get_lines(0),
311
self.assertEqual(k.get(0),
291
313
'line to be deleted',
295
self.assertEqual(k.get_lines(1),
317
self.assertEqual(k.get(1),
297
319
'replacement line',
302
325
class BadWeave(TestBase):
303
326
"""Test that we trap an insert which should not occur."""
304
327
def runTest(self):
307
k._parents = [frozenset(),
309
k._weave = ['bad line',
421
439
def runTest(self):
424
k.add_lines('text0', [], ["line the first",
442
k.add([], ["line the first",
429
self.assertEqual(len(k.get_lines(0)), 4)
447
self.assertEqual(len(k.get(0)), 4)
431
k.add_lines('text1', ['text0'], ["line the first",
449
k.add([0], ["line the first",
434
self.assertEqual(k.get_lines(1),
452
self.assertEqual(k.get(1),
435
453
["line the first",
438
self.assertEqual(k.annotate('text1'),
439
[('text0', "line the first"),
456
self.assertEqual(k.annotate(1),
457
[(0, "line the first"),
443
462
class IncludeVersions(TestBase):
453
472
def runTest(self):
456
k._parents = [frozenset(), frozenset([0])]
457
k._weave = [('{', 0),
475
k._v = [frozenset(), frozenset([0])]
464
k._sha1s = [sha_string('first line')
465
, sha_string('first linesecond line')]
467
self.assertEqual(k.get_lines(1),
483
self.assertEqual(k.get(1),
471
self.assertEqual(k.get_lines(0),
487
self.assertEqual(k.get(0),
490
k.dump(self.TEST_LOG)
475
493
class DivergedIncludes(TestBase):
476
494
"""Weave with two diverged texts based on version 0.
478
496
def runTest(self):
479
# FIXME make the weave, dont poke at it.
482
k._names = ['0', '1', '2']
483
k._name_map = {'0':0, '1':1, '2':2}
484
k._parents = [frozenset(),
488
k._weave = [('{', 0),
542
555
['header', '', 'line from 1', 'fixup line', 'line from 2'],
545
k.add_lines('text0', [], texts[0])
546
k.add_lines('text1', ['text0'], texts[1])
547
k.add_lines('text2', ['text0'], texts[2])
548
k.add_lines('merge', ['text0', 'text1', 'text2'], texts[3])
561
k.add([0, 1, 2], texts[3])
550
563
for i, t in enumerate(texts):
551
self.assertEqual(k.get_lines(i), t)
564
self.assertEqual(k.get(i), t)
553
self.assertEqual(k.annotate('merge'),
554
[('text0', 'header'),
556
('text1', 'line from 1'),
557
('merge', 'fixup line'),
558
('text2', 'line from 2'),
566
self.assertEqual(k.annotate(3),
561
self.assertEqual(list(k.get_ancestry(['merge'])),
562
['text0', 'text1', 'text2', 'merge'])
574
self.assertEqual(k.inclusions([3]),
564
self.log('k._weave=' + pformat(k._weave))
577
self.log('k._l=' + pformat(k._l))
566
579
self.check_read_write(k)
598
k.add_lines([], ['aaa', 'bbb'])
599
k.add_lines([0], ['111', 'aaa', 'ccc', 'bbb'])
600
k.add_lines([1], ['aaa', 'ccc', 'bbb', '222'])
612
k.add([], ['aaa', 'bbb'])
613
k.add([0], ['111', 'aaa', 'ccc', 'bbb'])
614
k.add([1], ['aaa', 'ccc', 'bbb', '222'])
620
class AutoMerge(TestBase):
624
texts = [['header', 'aaa', 'bbb'],
625
['header', 'aaa', 'line from 1', 'bbb'],
626
['header', 'aaa', 'bbb', 'line from 2', 'more from 2'],
633
self.log('k._l=' + pformat(k._l))
635
m = list(k.mash_iter([0, 1, 2]))
641
'line from 2', 'more from 2'])
603
645
class Khayyam(TestBase):
604
646
"""Test changes to multi-line texts, and read/write"""
606
def test_multi_line_merge(self):
608
649
"""A Book of Verses underneath the Bough,
609
650
A Jug of Wine, a Loaf of Bread, -- and Thou
610
651
Beside me singing in the Wilderness --
611
652
Oh, Wilderness were Paradise enow!""",
613
654
"""A Book of Verses underneath the Bough,
614
655
A Jug of Wine, a Loaf of Bread, -- and Thou
615
656
Beside me singing in the Wilderness --
638
ver = k.add_lines('text%d' % i,
640
parents.add('text%d' % i)
678
ver = k.add(list(parents), t)
643
self.log("k._weave=" + pformat(k._weave))
681
self.log("k._l=" + pformat(k._l))
645
683
for i, t in enumerate(texts):
646
self.assertEqual(k.get_lines(i), t)
684
self.assertEqual(k.get(i), t)
648
686
self.check_read_write(k)
651
class JoinWeavesTests(TestBase):
653
super(JoinWeavesTests, self).setUp()
654
self.weave1 = Weave()
655
self.lines1 = ['hello\n']
656
self.lines3 = ['hello\n', 'cruel\n', 'world\n']
657
self.weave1.add_lines('v1', [], self.lines1)
658
self.weave1.add_lines('v2', ['v1'], ['hello\n', 'world\n'])
659
self.weave1.add_lines('v3', ['v2'], self.lines3)
661
def test_written_detection(self):
662
# Test detection of weave file corruption.
664
# Make sure that we can detect if a weave file has
665
# been corrupted. This doesn't test all forms of corruption,
666
# but it at least helps verify the data you get, is what you want.
667
from cStringIO import StringIO
670
w.add_lines('v1', [], ['hello\n'])
671
w.add_lines('v2', ['v1'], ['hello\n', 'there\n'])
676
# Because we are corrupting, we need to make sure we have the exact text
677
self.assertEquals('# bzr weave file v5\n'
678
'i\n1 f572d396fae9206628714fb2ce00f72e94f2258f\nn v1\n\n'
679
'i 0\n1 90f265c6e75f1c8f9ab76dcf85528352c5f215ef\nn v2\n\n'
680
'w\n{ 0\n. hello\n}\n{ 1\n. there\n}\nW\n',
683
# Change a single letter
684
tmpf = StringIO('# 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')
691
self.assertEqual('hello\n', w.get_text('v1'))
692
self.assertRaises(errors.WeaveInvalidChecksum, w.get_text, 'v2')
693
self.assertRaises(errors.WeaveInvalidChecksum, w.get_lines, 'v2')
694
self.assertRaises(errors.WeaveInvalidChecksum, w.check)
696
# Change the sha checksum
697
tmpf = StringIO('# bzr weave file v5\n'
698
'i\n1 f572d396fae9206628714fb2ce00f72e94f2258f\nn v1\n\n'
699
'i 0\n1 f0f265c6e75f1c8f9ab76dcf85528352c5f215ef\nn v2\n\n'
700
'w\n{ 0\n. hello\n}\n{ 1\n. there\n}\nW\n')
704
self.assertEqual('hello\n', w.get_text('v1'))
705
self.assertRaises(errors.WeaveInvalidChecksum, w.get_text, 'v2')
706
self.assertRaises(errors.WeaveInvalidChecksum, w.get_lines, 'v2')
707
self.assertRaises(errors.WeaveInvalidChecksum, w.check)
710
class TestWeave(TestCase):
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:')
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:'))
725
class InstrumentedWeave(Weave):
726
"""Keep track of how many times functions are called."""
728
def __init__(self, weave_name=None):
729
self._extract_count = 0
730
Weave.__init__(self, weave_name=weave_name)
732
def _extract(self, versions):
733
self._extract_count += 1
734
return Weave._extract(self, versions)
737
class TestNeedsReweave(TestCase):
738
"""Internal corner cases for when reweave is needed."""
740
def test_compatible_parents(self):
742
my_parents = set([1, 2, 3])
744
self.assertTrue(w1._compatible_parents(my_parents, set([3])))
746
self.assertTrue(w1._compatible_parents(my_parents, set(my_parents)))
747
# same empty corner case
748
self.assertTrue(w1._compatible_parents(set(), set()))
749
# other cannot contain stuff my_parents does not
750
self.assertFalse(w1._compatible_parents(set(), set([1])))
751
self.assertFalse(w1._compatible_parents(my_parents, set([1, 2, 3, 4])))
752
self.assertFalse(w1._compatible_parents(my_parents, set([4])))
755
class TestWeaveFile(TestCaseInTempDir):
757
def test_empty_file(self):
758
f = open('empty.weave', 'wb+')
760
self.assertRaises(errors.WeaveFormatError,
691
from unittest import TestSuite, TestLoader
696
suite.addTest(tl.loadTestsFromModule(testweave))
698
return int(not testsweet.run_suite(suite)) # for shell 0=true
701
if __name__ == '__main__':
703
sys.exit(testweave())