1
# Copyright (C) 2005-2011, 2016 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., 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
"""test suite for weave algorithm"""
24
from pprint import pformat
29
from ..osutils import sha_string
30
from ..sixish import (
33
from . import TestCase, TestCaseInTempDir
34
from ..weave import Weave, WeaveFormatError
35
from ..weavefile import write_weave, read_weave
38
# texts for use in testing
39
TEXT_0 = ["Hello world"]
40
TEXT_1 = ["Hello world",
44
class TestBase(TestCase):
46
def check_read_write(self, k):
47
"""Check the weave k can be written & re-read."""
48
from tempfile import TemporaryFile
57
self.log('serialized weave:')
61
self.log('parents: %s' % (k._parents == k2._parents))
62
self.log(' %r' % k._parents)
63
self.log(' %r' % k2._parents)
65
self.fail('read/write check failed')
68
class WeaveContains(TestBase):
69
"""Weave __contains__ operator"""
72
k = Weave(get_scope=lambda:None)
73
self.assertFalse('foo' in k)
74
k.add_lines('foo', [], TEXT_1)
75
self.assertTrue('foo' in k)
84
class AnnotateOne(TestBase):
88
k.add_lines('text0', [], TEXT_0)
89
self.assertEqual(k.annotate('text0'),
90
[('text0', TEXT_0[0])])
93
class InvalidAdd(TestBase):
94
"""Try to use invalid version number during add."""
99
self.assertRaises(errors.RevisionNotPresent,
106
class RepeatedAdd(TestBase):
107
"""Add the same version twice; harmless."""
109
def test_duplicate_add(self):
111
idx = k.add_lines('text0', [], TEXT_0)
112
idx2 = k.add_lines('text0', [], TEXT_0)
113
self.assertEqual(idx, idx2)
116
class InvalidRepeatedAdd(TestBase):
120
k.add_lines('basis', [], TEXT_0)
121
idx = k.add_lines('text0', [], TEXT_0)
122
self.assertRaises(errors.RevisionAlreadyPresent,
126
['not the same text'])
127
self.assertRaises(errors.RevisionAlreadyPresent,
130
['basis'], # not the right parents
134
class InsertLines(TestBase):
135
"""Store a revision that adds one line to the original.
137
Look at the annotations to make sure that the first line is matched
138
and not stored repeatedly."""
142
k.add_lines('text0', [], ['line 1'])
143
k.add_lines('text1', ['text0'], ['line 1', 'line 2'])
145
self.assertEqual(k.annotate('text0'),
146
[('text0', 'line 1')])
148
self.assertEqual(k.get_lines(1),
152
self.assertEqual(k.annotate('text1'),
153
[('text0', 'line 1'),
154
('text1', 'line 2')])
156
k.add_lines('text2', ['text0'], ['line 1', 'diverged line'])
158
self.assertEqual(k.annotate('text2'),
159
[('text0', 'line 1'),
160
('text2', 'diverged line')])
162
text3 = ['line 1', 'middle line', 'line 2']
167
# self.log("changes to text3: " + pformat(list(k._delta(set([0, 1]), text3))))
169
self.log("k._weave=" + pformat(k._weave))
171
self.assertEqual(k.annotate('text3'),
172
[('text0', 'line 1'),
173
('text3', 'middle line'),
174
('text1', 'line 2')])
176
# now multiple insertions at different places
178
['text0', 'text1', 'text3'],
179
['line 1', 'aaa', 'middle line', 'bbb', 'line 2', 'ccc'])
181
self.assertEqual(k.annotate('text4'),
182
[('text0', 'line 1'),
184
('text3', 'middle line'),
190
class DeleteLines(TestBase):
191
"""Deletion of lines from existing text.
193
Try various texts all based on a common ancestor."""
197
base_text = ['one', 'two', 'three', 'four']
199
k.add_lines('text0', [], base_text)
201
texts = [['one', 'two', 'three'],
202
['two', 'three', 'four'],
204
['one', 'two', 'three', 'four'],
209
ver = k.add_lines('text%d' % i,
213
self.log('final weave:')
214
self.log('k._weave=' + pformat(k._weave))
216
for i in range(len(texts)):
217
self.assertEqual(k.get_lines(i+1),
221
class SuicideDelete(TestBase):
222
"""Invalid weave which tries to add and delete simultaneously."""
228
k._weave = [('{', 0),
235
################################### SKIPPED
236
# Weave.get doesn't trap this anymore
239
self.assertRaises(WeaveFormatError,
244
class CannedDelete(TestBase):
245
"""Unpack canned weave with deleted lines."""
252
k._weave = [('{', 0),
255
'line to be deleted',
260
k._sha1s = [sha_string('first lineline to be deletedlast line')
261
, sha_string('first linelast line')]
263
self.assertEqual(k.get_lines(0),
265
'line to be deleted',
269
self.assertEqual(k.get_lines(1),
275
class CannedReplacement(TestBase):
276
"""Unpack canned weave with deleted lines."""
280
k._parents = [frozenset(),
283
k._weave = [('{', 0),
286
'line to be deleted',
294
k._sha1s = [sha_string('first lineline to be deletedlast line')
295
, sha_string('first linereplacement linelast line')]
297
self.assertEqual(k.get_lines(0),
299
'line to be deleted',
303
self.assertEqual(k.get_lines(1),
310
class BadWeave(TestBase):
311
"""Test that we trap an insert which should not occur."""
315
k._parents = [frozenset(),
317
k._weave = ['bad line',
321
' added in version 1',
330
################################### SKIPPED
331
# Weave.get doesn't trap this anymore
335
self.assertRaises(WeaveFormatError,
340
class BadInsert(TestBase):
341
"""Test that we trap an insert which should not occur."""
345
k._parents = [frozenset(),
350
k._weave = [('{', 0),
353
' added in version 1',
361
# this is not currently enforced by get
362
return ##########################################
364
self.assertRaises(WeaveFormatError,
368
self.assertRaises(WeaveFormatError,
373
class InsertNested(TestBase):
374
"""Insertion with nested instructions."""
378
k._parents = [frozenset(),
383
k._weave = [('{', 0),
386
' added in version 1',
395
k._sha1s = [sha_string('foo {}')
396
, sha_string('foo { added in version 1 also from v1}')
397
, sha_string('foo { added in v2}')
398
, sha_string('foo { added in version 1 added in v2 also from v1}')
401
self.assertEqual(k.get_lines(0),
405
self.assertEqual(k.get_lines(1),
407
' added in version 1',
411
self.assertEqual(k.get_lines(2),
416
self.assertEqual(k.get_lines(3),
418
' added in version 1',
424
class DeleteLines2(TestBase):
425
"""Test recording revisions that delete lines.
427
This relies on the weave having a way to represent lines knocked
428
out by a later revision."""
432
k.add_lines('text0', [], ["line the first",
437
self.assertEqual(len(k.get_lines(0)), 4)
439
k.add_lines('text1', ['text0'], ["line the first",
442
self.assertEqual(k.get_lines(1),
446
self.assertEqual(k.annotate('text1'),
447
[('text0', "line the first"),
451
class IncludeVersions(TestBase):
452
"""Check texts that are stored across multiple revisions.
454
Here we manually create a weave with particular encoding and make
455
sure it unpacks properly.
457
Text 0 includes nothing; text 1 includes text 0 and adds some
464
k._parents = [frozenset(), frozenset([0])]
465
k._weave = [('{', 0),
472
k._sha1s = [sha_string('first line')
473
, sha_string('first linesecond line')]
475
self.assertEqual(k.get_lines(1),
479
self.assertEqual(k.get_lines(0),
483
class DivergedIncludes(TestBase):
484
"""Weave with two diverged texts based on version 0.
487
# FIXME make the weave, dont poke at it.
490
k._names = ['0', '1', '2']
491
k._name_map = {'0':0, '1':1, '2':2}
492
k._parents = [frozenset(),
496
k._weave = [('{', 0),
503
"alternative second line",
507
k._sha1s = [sha_string('first line')
508
, sha_string('first linesecond line')
509
, sha_string('first linealternative second line')]
511
self.assertEqual(k.get_lines(0),
514
self.assertEqual(k.get_lines(1),
518
self.assertEqual(k.get_lines('2'),
520
"alternative second line"])
522
self.assertEqual(list(k.get_ancestry(['2'])),
526
class ReplaceLine(TestBase):
530
text0 = ['cheddar', 'stilton', 'gruyere']
531
text1 = ['cheddar', 'blue vein', 'neufchatel', 'chevre']
533
k.add_lines('text0', [], text0)
534
k.add_lines('text1', ['text0'], text1)
536
self.log('k._weave=' + pformat(k._weave))
538
self.assertEqual(k.get_lines(0), text0)
539
self.assertEqual(k.get_lines(1), text1)
542
class Merge(TestBase):
543
"""Storage of versions that merge diverged parents"""
549
['header', '', 'line from 1'],
550
['header', '', 'line from 2', 'more from 2'],
551
['header', '', 'line from 1', 'fixup line', 'line from 2'],
554
k.add_lines('text0', [], texts[0])
555
k.add_lines('text1', ['text0'], texts[1])
556
k.add_lines('text2', ['text0'], texts[2])
557
k.add_lines('merge', ['text0', 'text1', 'text2'], texts[3])
559
for i, t in enumerate(texts):
560
self.assertEqual(k.get_lines(i), t)
562
self.assertEqual(k.annotate('merge'),
563
[('text0', 'header'),
565
('text1', 'line from 1'),
566
('merge', 'fixup line'),
567
('text2', 'line from 2'),
570
self.assertEqual(list(k.get_ancestry(['merge'])),
571
['text0', 'text1', 'text2', 'merge'])
573
self.log('k._weave=' + pformat(k._weave))
575
self.check_read_write(k)
578
class Conflicts(TestBase):
579
"""Test detection of conflicting regions during a merge.
581
A base version is inserted, then two descendents try to
582
insert different lines in the same place. These should be
583
reported as a possible conflict and forwarded to the user."""
588
k.add_lines([], ['aaa', 'bbb'])
589
k.add_lines([0], ['aaa', '111', 'bbb'])
590
k.add_lines([1], ['aaa', '222', 'bbb'])
592
merged = k.merge([1, 2])
594
self.assertEqual([[['aaa']],
599
class NonConflict(TestBase):
600
"""Two descendants insert compatible changes.
602
No conflict should be reported."""
607
k.add_lines([], ['aaa', 'bbb'])
608
k.add_lines([0], ['111', 'aaa', 'ccc', 'bbb'])
609
k.add_lines([1], ['aaa', 'ccc', 'bbb', '222'])
612
class Khayyam(TestBase):
613
"""Test changes to multi-line texts, and read/write"""
615
def test_multi_line_merge(self):
617
"""A Book of Verses underneath the Bough,
618
A Jug of Wine, a Loaf of Bread, -- and Thou
619
Beside me singing in the Wilderness --
620
Oh, Wilderness were Paradise enow!""",
622
"""A Book of Verses underneath the Bough,
623
A Jug of Wine, a Loaf of Bread, -- and Thou
624
Beside me singing in the Wilderness --
625
Oh, Wilderness were Paradise now!""",
627
"""A Book of poems underneath the tree,
628
A Jug of Wine, a Loaf of Bread,
630
Beside me singing in the Wilderness --
631
Oh, Wilderness were Paradise now!
635
"""A Book of Verses underneath the Bough,
636
A Jug of Wine, a Loaf of Bread,
638
Beside me singing in the Wilderness --
639
Oh, Wilderness were Paradise now!""",
641
texts = [[l.strip() for l in t.split('\n')] for t in rawtexts]
647
ver = k.add_lines('text%d' % i,
649
parents.add('text%d' % i)
652
self.log("k._weave=" + pformat(k._weave))
654
for i, t in enumerate(texts):
655
self.assertEqual(k.get_lines(i), t)
657
self.check_read_write(k)
660
class JoinWeavesTests(TestBase):
663
super(JoinWeavesTests, self).setUp()
664
self.weave1 = Weave()
665
self.lines1 = ['hello\n']
666
self.lines3 = ['hello\n', 'cruel\n', 'world\n']
667
self.weave1.add_lines('v1', [], self.lines1)
668
self.weave1.add_lines('v2', ['v1'], ['hello\n', 'world\n'])
669
self.weave1.add_lines('v3', ['v2'], self.lines3)
671
def test_written_detection(self):
672
# Test detection of weave file corruption.
674
# Make sure that we can detect if a weave file has
675
# been corrupted. This doesn't test all forms of corruption,
676
# but it at least helps verify the data you get, is what you want.
679
w.add_lines('v1', [], ['hello\n'])
680
w.add_lines('v2', ['v1'], ['hello\n', 'there\n'])
685
# Because we are corrupting, we need to make sure we have the exact text
686
self.assertEqual('# bzr weave file v5\n'
687
'i\n1 f572d396fae9206628714fb2ce00f72e94f2258f\nn v1\n\n'
688
'i 0\n1 90f265c6e75f1c8f9ab76dcf85528352c5f215ef\nn v2\n\n'
689
'w\n{ 0\n. hello\n}\n{ 1\n. there\n}\nW\n',
692
# Change a single letter
693
tmpf = BytesIO(b'# bzr weave file v5\n'
694
b'i\n1 f572d396fae9206628714fb2ce00f72e94f2258f\nn v1\n\n'
695
b'i 0\n1 90f265c6e75f1c8f9ab76dcf85528352c5f215ef\nn v2\n\n'
696
b'w\n{ 0\n. hello\n}\n{ 1\n. There\n}\nW\n')
700
self.assertEqual('hello\n', w.get_text('v1'))
701
self.assertRaises(errors.WeaveInvalidChecksum, w.get_text, 'v2')
702
self.assertRaises(errors.WeaveInvalidChecksum, w.get_lines, 'v2')
703
self.assertRaises(errors.WeaveInvalidChecksum, w.check)
705
# Change the sha checksum
706
tmpf = BytesIO(b'# bzr weave file v5\n'
707
b'i\n1 f572d396fae9206628714fb2ce00f72e94f2258f\nn v1\n\n'
708
b'i 0\n1 f0f265c6e75f1c8f9ab76dcf85528352c5f215ef\nn v2\n\n'
709
b'w\n{ 0\n. hello\n}\n{ 1\n. there\n}\nW\n')
713
self.assertEqual('hello\n', w.get_text('v1'))
714
self.assertRaises(errors.WeaveInvalidChecksum, w.get_text, 'v2')
715
self.assertRaises(errors.WeaveInvalidChecksum, w.get_lines, 'v2')
716
self.assertRaises(errors.WeaveInvalidChecksum, w.check)
719
class TestWeave(TestCase):
721
def test_allow_reserved_false(self):
722
w = Weave('name', allow_reserved=False)
723
# Add lines is checked at the WeaveFile level, not at the Weave level
724
w.add_lines('name:', [], TEXT_1)
725
# But get_lines is checked at this level
726
self.assertRaises(errors.ReservedId, w.get_lines, 'name:')
728
def test_allow_reserved_true(self):
729
w = Weave('name', allow_reserved=True)
730
w.add_lines('name:', [], TEXT_1)
731
self.assertEqual(TEXT_1, w.get_lines('name:'))
734
class InstrumentedWeave(Weave):
735
"""Keep track of how many times functions are called."""
737
def __init__(self, weave_name=None):
738
self._extract_count = 0
739
Weave.__init__(self, weave_name=weave_name)
741
def _extract(self, versions):
742
self._extract_count += 1
743
return Weave._extract(self, versions)
746
class TestNeedsReweave(TestCase):
747
"""Internal corner cases for when reweave is needed."""
749
def test_compatible_parents(self):
751
my_parents = {1, 2, 3}
753
self.assertTrue(w1._compatible_parents(my_parents, {3}))
755
self.assertTrue(w1._compatible_parents(my_parents, set(my_parents)))
756
# same empty corner case
757
self.assertTrue(w1._compatible_parents(set(), set()))
758
# other cannot contain stuff my_parents does not
759
self.assertFalse(w1._compatible_parents(set(), {1}))
760
self.assertFalse(w1._compatible_parents(my_parents, {1, 2, 3, 4}))
761
self.assertFalse(w1._compatible_parents(my_parents, {4}))
764
class TestWeaveFile(TestCaseInTempDir):
766
def test_empty_file(self):
767
f = open('empty.weave', 'wb+')
769
self.assertRaises(errors.WeaveFormatError,