79
79
"""Store and retrieve a simple text."""
82
idx = k.add('text0', [], TEXT_0)
83
self.assertEqual(k.get(idx), TEXT_0)
82
idx = k.add_lines('text0', [], TEXT_0)
83
self.assertEqual(k.get_lines(idx), TEXT_0)
84
84
self.assertEqual(idx, 0)
87
87
class AnnotateOne(TestBase):
90
k.add('text0', [], TEXT_0)
91
self.assertEqual(k.annotate(0),
90
k.add_lines('text0', [], TEXT_0)
91
self.assertEqual(k.annotate('text0'),
92
[('text0', TEXT_0[0])])
95
95
class StoreTwo(TestBase):
99
idx = k.add('text0', [], TEXT_0)
99
idx = k.add_lines('text0', [], TEXT_0)
100
100
self.assertEqual(idx, 0)
102
idx = k.add('text1', [], TEXT_1)
102
idx = k.add_lines('text1', [], TEXT_1)
103
103
self.assertEqual(idx, 1)
105
self.assertEqual(k.get(0), TEXT_0)
106
self.assertEqual(k.get(1), TEXT_1)
109
class AddWithGivenSha(TestBase):
111
"""Add with caller-supplied SHA-1"""
115
k.add('text0', [], [t], sha1=sha_string(t))
105
self.assertEqual(k.get_lines(0), TEXT_0)
106
self.assertEqual(k.get_lines(1), TEXT_1)
118
109
class GetSha1(TestBase):
119
110
def test_get_sha1(self):
121
k.add('text0', [], 'text0')
112
k.add_lines('text0', [], 'text0')
122
113
self.assertEqual('34dc0e430c642a26c3dd1c2beb7a8b4f4445eb79',
123
114
k.get_sha1('text0'))
124
self.assertRaises(errors.WeaveRevisionNotPresent,
115
self.assertRaises(errors.RevisionNotPresent,
126
self.assertRaises(errors.WeaveRevisionNotPresent,
117
self.assertRaises(errors.RevisionNotPresent,
127
118
k.get_sha1, 'text1')
143
134
"""Add the same version twice; harmless."""
144
135
def runTest(self):
146
idx = k.add('text0', [], TEXT_0)
147
idx2 = k.add('text0', [], TEXT_0)
137
idx = k.add_lines('text0', [], TEXT_0)
138
idx2 = k.add_lines('text0', [], TEXT_0)
148
139
self.assertEqual(idx, idx2)
151
142
class InvalidRepeatedAdd(TestBase):
152
143
def runTest(self):
154
idx = k.add('text0', [], TEXT_0)
155
self.assertRaises(WeaveError,
145
k.add_lines('basis', [], TEXT_0)
146
idx = k.add_lines('text0', [], TEXT_0)
147
self.assertRaises(errors.RevisionAlreadyPresent,
159
151
['not the same text'])
160
self.assertRaises(WeaveError,
152
self.assertRaises(errors.RevisionAlreadyPresent,
163
[12], # not the right parents
155
['basis'], # not the right parents
172
164
def runTest(self):
175
k.add('text0', [], ['line 1'])
176
k.add('text1', [0], ['line 1', 'line 2'])
178
self.assertEqual(k.annotate(0),
181
self.assertEqual(k.get(1),
167
k.add_lines('text0', [], ['line 1'])
168
k.add_lines('text1', ['text0'], ['line 1', 'line 2'])
170
self.assertEqual(k.annotate('text0'),
171
[('text0', 'line 1')])
173
self.assertEqual(k.get_lines(1),
185
self.assertEqual(k.annotate(1),
189
k.add('text2', [0], ['line 1', 'diverged line'])
191
self.assertEqual(k.annotate(2),
193
(2, 'diverged line')])
177
self.assertEqual(k.annotate('text1'),
178
[('text0', 'line 1'),
179
('text1', 'line 2')])
181
k.add_lines('text2', ['text0'], ['line 1', 'diverged line'])
183
self.assertEqual(k.annotate('text2'),
184
[('text0', 'line 1'),
185
('text2', 'diverged line')])
195
187
text3 = ['line 1', 'middle line', 'line 2']
200
192
# self.log("changes to text3: " + pformat(list(k._delta(set([0, 1]), text3))))
202
194
self.log("k._weave=" + pformat(k._weave))
204
self.assertEqual(k.annotate(3),
196
self.assertEqual(k.annotate('text3'),
197
[('text0', 'line 1'),
198
('text3', 'middle line'),
199
('text1', 'line 2')])
209
201
# now multiple insertions at different places
203
['text0', 'text1', 'text3'],
212
204
['line 1', 'aaa', 'middle line', 'bbb', 'line 2', 'ccc'])
214
self.assertEqual(k.annotate(4),
206
self.assertEqual(k.annotate('text4'),
207
[('text0', 'line 1'),
209
('text3', 'middle line'),
223
215
class DeleteLines(TestBase):
230
222
base_text = ['one', 'two', 'three', 'four']
232
k.add('text0', [], base_text)
224
k.add_lines('text0', [], base_text)
234
226
texts = [['one', 'two', 'three'],
235
227
['two', 'three', 'four'],
431
423
, sha_string('foo { added in version 1 added in v2 also from v1}')
434
self.assertEqual(k.get(0),
426
self.assertEqual(k.get_lines(0),
438
self.assertEqual(k.get(1),
430
self.assertEqual(k.get_lines(1),
440
432
' added in version 1',
444
self.assertEqual(k.get(2),
436
self.assertEqual(k.get_lines(2),
449
self.assertEqual(k.get(3),
441
self.assertEqual(k.get_lines(3),
451
443
' added in version 1',
462
454
def runTest(self):
465
k.add('text0', [], ["line the first",
457
k.add_lines('text0', [], ["line the first",
470
self.assertEqual(len(k.get(0)), 4)
462
self.assertEqual(len(k.get_lines(0)), 4)
472
k.add('text1', [0], ["line the first",
464
k.add_lines('text1', ['text0'], ["line the first",
475
self.assertEqual(k.get(1),
467
self.assertEqual(k.get_lines(1),
476
468
["line the first",
479
self.assertEqual(k.annotate(1),
480
[(0, "line the first"),
471
self.assertEqual(k.annotate('text1'),
472
[('text0', "line the first"),
484
476
class IncludeVersions(TestBase):
538
533
, sha_string('first linesecond line')
539
534
, sha_string('first linealternative second line')]
541
self.assertEqual(k.get(0),
536
self.assertEqual(k.get_lines(0),
544
self.assertEqual(k.get(1),
539
self.assertEqual(k.get_lines(1),
548
self.assertEqual(k.get(2),
543
self.assertEqual(k.get_lines('2'),
550
545
"alternative second line"])
552
self.assertEqual(list(k.inclusions([2])),
547
self.assertEqual(list(k.get_ancestry(['2'])),
556
551
class ReplaceLine(TestBase):
560
555
text0 = ['cheddar', 'stilton', 'gruyere']
561
556
text1 = ['cheddar', 'blue vein', 'neufchatel', 'chevre']
563
k.add('text0', [], text0)
564
k.add('text1', [0], text1)
558
k.add_lines('text0', [], text0)
559
k.add_lines('text1', ['text0'], text1)
566
561
self.log('k._weave=' + pformat(k._weave))
568
self.assertEqual(k.get(0), text0)
569
self.assertEqual(k.get(1), text1)
563
self.assertEqual(k.get_lines(0), text0)
564
self.assertEqual(k.get_lines(1), text1)
572
567
class Merge(TestBase):
580
575
['header', '', 'line from 1', 'fixup line', 'line from 2'],
583
k.add('text0', [], texts[0])
584
k.add('text1', [0], texts[1])
585
k.add('text2', [0], texts[2])
586
k.add('merge', [0, 1, 2], texts[3])
578
k.add_lines('text0', [], texts[0])
579
k.add_lines('text1', ['text0'], texts[1])
580
k.add_lines('text2', ['text0'], texts[2])
581
k.add_lines('merge', ['text0', 'text1', 'text2'], texts[3])
588
583
for i, t in enumerate(texts):
589
self.assertEqual(k.get(i), t)
584
self.assertEqual(k.get_lines(i), t)
591
self.assertEqual(k.annotate(3),
586
self.assertEqual(k.annotate('merge'),
587
[('text0', 'header'),
589
('text1', 'line from 1'),
590
('merge', 'fixup line'),
591
('text2', 'line from 2'),
599
self.assertEqual(list(k.inclusions([3])),
594
self.assertEqual(list(k.get_ancestry(['merge'])),
595
['text0', 'text1', 'text2', 'merge'])
602
597
self.log('k._weave=' + pformat(k._weave))
617
k.add([], ['aaa', 'bbb'])
618
k.add([0], ['aaa', '111', 'bbb'])
619
k.add([1], ['aaa', '222', 'bbb'])
612
k.add_lines([], ['aaa', 'bbb'])
613
k.add_lines([0], ['aaa', '111', 'bbb'])
614
k.add_lines([1], ['aaa', '222', 'bbb'])
621
616
merged = k.merge([1, 2])
636
k.add([], ['aaa', 'bbb'])
637
k.add([0], ['111', 'aaa', 'ccc', 'bbb'])
638
k.add([1], ['aaa', 'ccc', 'bbb', '222'])
641
class AutoMerge(TestBase):
645
texts = [['header', 'aaa', 'bbb'],
646
['header', 'aaa', 'line from 1', 'bbb'],
647
['header', 'aaa', 'bbb', 'line from 2', 'more from 2'],
650
k.add('text0', [], texts[0])
651
k.add('text1', [0], texts[1])
652
k.add('text2', [0], texts[2])
654
self.log('k._weave=' + pformat(k._weave))
656
m = list(k.mash_iter([0, 1, 2]))
662
'line from 2', 'more from 2'])
631
k.add_lines([], ['aaa', 'bbb'])
632
k.add_lines([0], ['111', 'aaa', 'ccc', 'bbb'])
633
k.add_lines([1], ['aaa', 'ccc', 'bbb', '222'])
665
636
class Khayyam(TestBase):
666
637
"""Test changes to multi-line texts, and read/write"""
639
def test_multi_line_merge(self):
669
641
"""A Book of Verses underneath the Bough,
670
642
A Jug of Wine, a Loaf of Bread, -- and Thou
699
ver = k.add('text%d' % i,
671
ver = k.add_lines('text%d' % i,
700
672
list(parents), t)
673
parents.add('text%d' % i)
704
676
self.log("k._weave=" + pformat(k._weave))
706
678
for i, t in enumerate(texts):
707
self.assertEqual(k.get(i), t)
679
self.assertEqual(k.get_lines(i), t)
709
681
self.check_read_write(k)
721
w.add('text0', [], map(addcrlf, base))
722
w.add('text1', [0], map(addcrlf, a))
723
w.add('text2', [0], map(addcrlf, b))
693
w.add_lines('text0', [], map(addcrlf, base))
694
w.add_lines('text1', ['text0'], map(addcrlf, a))
695
w.add_lines('text2', ['text0'], map(addcrlf, b))
725
697
self.log('weave is:')
726
698
tmpf = StringIO()
815
787
self.weave1 = Weave()
816
788
self.lines1 = ['hello\n']
817
789
self.lines3 = ['hello\n', 'cruel\n', 'world\n']
818
self.weave1.add('v1', [], self.lines1)
819
self.weave1.add('v2', [0], ['hello\n', 'world\n'])
820
self.weave1.add('v3', [1], self.lines3)
790
self.weave1.add_lines('v1', [], self.lines1)
791
self.weave1.add_lines('v2', ['v1'], ['hello\n', 'world\n'])
792
self.weave1.add_lines('v3', ['v2'], self.lines3)
822
794
def test_join_empty(self):
823
795
"""Join two empty weaves."""
835
807
def test_join_unrelated(self):
836
808
"""Join two weaves with no history in common."""
838
wb.add('b1', [], ['line from b\n'])
810
wb.add_lines('b1', [], ['line from b\n'])
841
813
eq = self.assertEqual
843
eq(sorted(list(w1.iter_names())),
815
eq(sorted(w1.versions()),
844
816
['b1', 'v1', 'v2', 'v3'])
846
818
def test_join_related(self):
847
819
wa = self.weave1.copy()
848
820
wb = self.weave1.copy()
849
wa.add('a1', ['v3'], ['hello\n', 'sweet\n', 'world\n'])
850
wb.add('b1', ['v3'], ['hello\n', 'pale blue\n', 'world\n'])
821
wa.add_lines('a1', ['v3'], ['hello\n', 'sweet\n', 'world\n'])
822
wb.add_lines('b1', ['v3'], ['hello\n', 'pale blue\n', 'world\n'])
851
823
eq = self.assertEquals
857
829
['hello\n', 'pale blue\n', 'world\n'])
859
831
def test_join_parent_disagreement(self):
860
"""Cannot join weaves with different parents for a version."""
832
#join reconciles differening parents into a union.
863
wa.add('v1', [], ['hello\n'])
865
wb.add('v1', ['v0'], ['hello\n'])
866
self.assertRaises(WeaveError,
835
wa.add_lines('v1', [], ['hello\n'])
836
wb.add_lines('v0', [], [])
837
wb.add_lines('v1', ['v0'], ['hello\n'])
839
self.assertEqual(['v0'], wa.get_parents('v1'))
869
841
def test_join_text_disagreement(self):
870
842
"""Cannot join weaves with different texts for a version."""
873
wa.add('v1', [], ['hello\n'])
874
wb.add('v1', [], ['not\n', 'hello\n'])
845
wa.add_lines('v1', [], ['hello\n'])
846
wb.add_lines('v1', [], ['not\n', 'hello\n'])
875
847
self.assertRaises(WeaveError,
881
853
The source weave contains a different version at index 0."""
882
854
wa = self.weave1.copy()
884
wb.add('x1', [], ['line from x1\n'])
885
wb.add('v1', [], ['hello\n'])
886
wb.add('v2', ['v1'], ['hello\n', 'world\n'])
856
wb.add_lines('x1', [], ['line from x1\n'])
857
wb.add_lines('v1', [], ['hello\n'])
858
wb.add_lines('v2', ['v1'], ['hello\n', 'world\n'])
888
860
eq = self.assertEquals
889
eq(sorted(wa.iter_names()), ['v1', 'v2', 'v3', 'x1',])
861
eq(sorted(wa.versions()), ['v1', 'v2', 'v3', 'x1',])
890
862
eq(wa.get_text('x1'), 'line from x1\n')
893
class Corruption(TestCase):
895
def test_detection(self):
896
# Test weaves detect corruption.
898
# Weaves contain a checksum of their texts.
899
# When a text is extracted, this checksum should be
903
w.add('v1', [], ['hello\n'])
904
w.add('v2', ['v1'], ['hello\n', 'there\n'])
906
# We are going to invasively corrupt the text
907
# Make sure the internals of weave are the same
908
self.assertEqual([('{', 0)
916
self.assertEqual(['f572d396fae9206628714fb2ce00f72e94f2258f'
917
, '90f265c6e75f1c8f9ab76dcf85528352c5f215ef'
922
w._weave[4] = 'There\n'
924
self.assertEqual('hello\n', w.get_text('v1'))
925
self.assertRaises(errors.WeaveInvalidChecksum, w.get_text, 'v2')
926
self.assertRaises(errors.WeaveInvalidChecksum, w.get_lines, 'v2')
927
self.assertRaises(errors.WeaveInvalidChecksum, list, w.get_iter('v2'))
928
self.assertRaises(errors.WeaveInvalidChecksum, w.check)
931
w._weave[4] = 'there\n'
932
self.assertEqual('hello\nthere\n', w.get_text('v2'))
934
#Invalid checksum, first digit changed
935
w._sha1s[1] = 'f0f265c6e75f1c8f9ab76dcf85528352c5f215ef'
937
self.assertEqual('hello\n', w.get_text('v1'))
938
self.assertRaises(errors.WeaveInvalidChecksum, w.get_text, 'v2')
939
self.assertRaises(errors.WeaveInvalidChecksum, w.get_lines, 'v2')
940
self.assertRaises(errors.WeaveInvalidChecksum, list, w.get_iter('v2'))
941
self.assertRaises(errors.WeaveInvalidChecksum, w.check)
943
864
def test_written_detection(self):
944
865
# Test detection of weave file corruption.
949
870
from cStringIO import StringIO
952
w.add('v1', [], ['hello\n'])
953
w.add('v2', ['v1'], ['hello\n', 'there\n'])
873
w.add_lines('v1', [], ['hello\n'])
874
w.add_lines('v2', ['v1'], ['hello\n', 'there\n'])
955
876
tmpf = StringIO()
956
877
write_weave(w, tmpf)
973
894
self.assertEqual('hello\n', w.get_text('v1'))
974
895
self.assertRaises(errors.WeaveInvalidChecksum, w.get_text, 'v2')
975
896
self.assertRaises(errors.WeaveInvalidChecksum, w.get_lines, 'v2')
976
self.assertRaises(errors.WeaveInvalidChecksum, list, w.get_iter('v2'))
977
897
self.assertRaises(errors.WeaveInvalidChecksum, w.check)
979
899
# Change the sha checksum
987
907
self.assertEqual('hello\n', w.get_text('v1'))
988
908
self.assertRaises(errors.WeaveInvalidChecksum, w.get_text, 'v2')
989
909
self.assertRaises(errors.WeaveInvalidChecksum, w.get_lines, 'v2')
990
self.assertRaises(errors.WeaveInvalidChecksum, list, w.get_iter('v2'))
991
910
self.assertRaises(errors.WeaveInvalidChecksum, w.check)
1015
934
txt2 = ['a\n', 'c\n']
1016
935
txt3 = ['a\n', 'b\n', 'c\n']
1018
w1.add('txt0', [], txt0) # extract 1a
1019
w2.add('txt0', [], txt0) # extract 1b
1020
w1.add('txt1', [0], txt1)# extract 2a
1021
w2.add('txt2', [0], txt2)# extract 2b
937
w1.add_lines('txt0', [], txt0) # extract 1a
938
w2.add_lines('txt0', [], txt0) # extract 1b
939
w1.add_lines('txt1', ['txt0'], txt1)# extract 2a
940
w2.add_lines('txt2', ['txt0'], txt2)# extract 2b
1022
941
w1.join(w2) # extract 3a to add txt2
1023
942
w2.join(w1) # extract 3b to add txt1
1025
w1.add('txt3', [1, 2], txt3) # extract 4a
1026
w2.add('txt3', [1, 2], txt3) # extract 4b
944
w1.add_lines('txt3', ['txt1', 'txt2'], txt3) # extract 4a
945
w2.add_lines('txt3', ['txt2', 'txt1'], txt3) # extract 4b
1027
946
# These secretly have inverted parents
1029
948
# This should not have to do any extractions
1044
963
txt2 = ['a\n', 'c\n']
1045
964
txt3 = ['a\n', 'b\n', 'c\n']
1047
w1.add('txt0', [], txt0)
1048
w2.add('txt0', [], txt0)
1049
w1.add('txt1', [0], txt1)
1050
w2.add('txt1', [0,0], txt1)
966
w1.add_lines('txt0', [], txt0)
967
w2.add_lines('txt0', [], txt0)
968
w1.add_lines('txt1', ['txt0'], txt1)
969
w2.add_lines('txt1', ['txt0', 'txt0'], txt1)
1051
970
# Same text, effectively the same, because the
1052
971
# parent is only repeated
1053
972
w1.join(w2) # extract 3a to add txt2
1054
973
w2.join(w1) # extract 3b to add txt1
1057
class MismatchedTexts(TestCase):
1058
"""Test that merging two weaves with different texts fails."""
976
class TestNeedsRweave(TestCase):
977
"""Internal corner cases for when reweave is needed."""
1060
def test_reweave(self):
979
def test_compatible_parents(self):
1064
w1.add('txt0', [], ['a\n'])
1065
w2.add('txt0', [], ['a\n'])
1066
w1.add('txt1', [0], ['a\n', 'b\n'])
1067
w2.add('txt1', [0], ['a\n', 'c\n'])
1069
self.assertRaises(errors.WeaveTextDiffers, w1.reweave, w2)
981
my_parents = set([1, 2, 3])
983
self.assertTrue(w1._compatible_parents(my_parents, set([3])))
985
self.assertTrue(w1._compatible_parents(my_parents, set(my_parents)))
986
# same empty corner case
987
self.assertTrue(w1._compatible_parents(set(), set()))
988
# other cannot contain stuff my_parents does not
989
self.assertFalse(w1._compatible_parents(set(), set([1])))
990
self.assertFalse(w1._compatible_parents(my_parents, set([1, 2, 3, 4])))
991
self.assertFalse(w1._compatible_parents(my_parents, set([4])))