17
17
"""Tests for Knit data structure"""
19
from cStringIO import StringIO
23
from bzrlib.errors import KnitError, RevisionAlreadyPresent
32
from bzrlib.errors import (
33
RevisionAlreadyPresent,
38
from bzrlib.index import *
24
39
from bzrlib.knit import (
29
from bzrlib.osutils import split_lines
30
from bzrlib.tests import TestCaseWithTransport
31
from bzrlib.transport import TransportLogger, get_transport
51
from bzrlib.repofmt import pack_repo
52
from bzrlib.tests import (
56
TestCaseWithMemoryTransport,
57
TestCaseWithTransport,
60
from bzrlib.transport import get_transport
32
61
from bzrlib.transport.memory import MemoryTransport
33
from bzrlib.weave import Weave
36
class KnitTests(TestCaseWithTransport):
37
"""Class containing knit test helper routines."""
39
def make_test_knit(self, annotate=False):
41
factory = KnitPlainFactory()
44
return KnitVersionedFile('test', get_transport('.'), access_mode='w', factory=factory, create=True)
47
class BasicKnitTests(KnitTests):
49
def add_stock_one_and_one_a(self, k):
50
k.add_lines('text-1', [], split_lines(TEXT_1))
51
k.add_lines('text-1a', ['text-1'], split_lines(TEXT_1A))
53
def test_knit_constructor(self):
54
"""Construct empty k"""
57
def test_knit_add(self):
58
"""Store one text in knit and retrieve"""
59
k = self.make_test_knit()
60
k.add_lines('text-1', [], split_lines(TEXT_1))
61
self.assertTrue(k.has_version('text-1'))
62
self.assertEqualDiff(''.join(k.get_lines('text-1')), TEXT_1)
64
def test_knit_reload(self):
65
# test that the content in a reloaded knit is correct
66
k = self.make_test_knit()
67
k.add_lines('text-1', [], split_lines(TEXT_1))
69
k2 = KnitVersionedFile('test', get_transport('.'), access_mode='r', factory=KnitPlainFactory(), create=True)
70
self.assertTrue(k2.has_version('text-1'))
71
self.assertEqualDiff(''.join(k2.get_lines('text-1')), TEXT_1)
73
def test_knit_several(self):
74
"""Store several texts in a knit"""
75
k = self.make_test_knit()
76
k.add_lines('text-1', [], split_lines(TEXT_1))
77
k.add_lines('text-2', [], split_lines(TEXT_2))
78
self.assertEqualDiff(''.join(k.get_lines('text-1')), TEXT_1)
79
self.assertEqualDiff(''.join(k.get_lines('text-2')), TEXT_2)
81
def test_repeated_add(self):
82
"""Knit traps attempt to replace existing version"""
83
k = self.make_test_knit()
84
k.add_lines('text-1', [], split_lines(TEXT_1))
85
self.assertRaises(RevisionAlreadyPresent,
87
'text-1', [], split_lines(TEXT_1))
90
k = self.make_test_knit(True)
91
k.add_lines('text-1', [], [])
92
self.assertEquals(k.get_lines('text-1'), [])
94
def test_incomplete(self):
95
"""Test if texts without a ending line-end can be inserted and
97
k = KnitVersionedFile('test', get_transport('.'), delta=False, create=True)
98
k.add_lines('text-1', [], ['a\n', 'b' ])
99
k.add_lines('text-2', ['text-1'], ['a\rb\n', 'b\n'])
100
# reopening ensures maximum room for confusion
101
k = KnitVersionedFile('test', get_transport('.'), delta=False, create=True)
102
self.assertEquals(k.get_lines('text-1'), ['a\n', 'b' ])
103
self.assertEquals(k.get_lines('text-2'), ['a\rb\n', 'b\n'])
105
def test_delta(self):
106
"""Expression of knit delta as lines"""
107
k = self.make_test_knit()
108
td = list(line_delta(TEXT_1.splitlines(True),
109
TEXT_1A.splitlines(True)))
110
self.assertEqualDiff(''.join(td), delta_1_1a)
111
out = apply_line_delta(TEXT_1.splitlines(True), td)
112
self.assertEqualDiff(''.join(out), TEXT_1A)
114
def test_add_with_parents(self):
115
"""Store in knit with parents"""
116
k = self.make_test_knit()
117
self.add_stock_one_and_one_a(k)
118
self.assertEquals(k.get_parents('text-1'), [])
119
self.assertEquals(k.get_parents('text-1a'), ['text-1'])
121
def test_ancestry(self):
122
"""Store in knit with parents"""
123
k = self.make_test_knit()
124
self.add_stock_one_and_one_a(k)
125
self.assertEquals(set(k.get_ancestry(['text-1a'])), set(['text-1a', 'text-1']))
127
def test_add_delta(self):
128
"""Store in knit with parents"""
129
k = KnitVersionedFile('test', get_transport('.'), factory=KnitPlainFactory(),
130
delta=True, create=True)
131
self.add_stock_one_and_one_a(k)
133
self.assertEqualDiff(''.join(k.get_lines('text-1a')), TEXT_1A)
135
def test_annotate(self):
137
k = KnitVersionedFile('knit', get_transport('.'), factory=KnitAnnotateFactory(),
138
delta=True, create=True)
139
self.insert_and_test_small_annotate(k)
141
def insert_and_test_small_annotate(self, k):
142
"""test annotation with k works correctly."""
143
k.add_lines('text-1', [], ['a\n', 'b\n'])
144
k.add_lines('text-2', ['text-1'], ['a\n', 'c\n'])
146
origins = k.annotate('text-2')
147
self.assertEquals(origins[0], ('text-1', 'a\n'))
148
self.assertEquals(origins[1], ('text-2', 'c\n'))
150
def test_annotate_fulltext(self):
152
k = KnitVersionedFile('knit', get_transport('.'), factory=KnitAnnotateFactory(),
153
delta=False, create=True)
154
self.insert_and_test_small_annotate(k)
156
def test_annotate_merge_1(self):
157
k = self.make_test_knit(True)
158
k.add_lines('text-a1', [], ['a\n', 'b\n'])
159
k.add_lines('text-a2', [], ['d\n', 'c\n'])
160
k.add_lines('text-am', ['text-a1', 'text-a2'], ['d\n', 'b\n'])
161
origins = k.annotate('text-am')
162
self.assertEquals(origins[0], ('text-a2', 'd\n'))
163
self.assertEquals(origins[1], ('text-a1', 'b\n'))
165
def test_annotate_merge_2(self):
166
k = self.make_test_knit(True)
167
k.add_lines('text-a1', [], ['a\n', 'b\n', 'c\n'])
168
k.add_lines('text-a2', [], ['x\n', 'y\n', 'z\n'])
169
k.add_lines('text-am', ['text-a1', 'text-a2'], ['a\n', 'y\n', 'c\n'])
170
origins = k.annotate('text-am')
171
self.assertEquals(origins[0], ('text-a1', 'a\n'))
172
self.assertEquals(origins[1], ('text-a2', 'y\n'))
173
self.assertEquals(origins[2], ('text-a1', 'c\n'))
175
def test_annotate_merge_9(self):
176
k = self.make_test_knit(True)
177
k.add_lines('text-a1', [], ['a\n', 'b\n', 'c\n'])
178
k.add_lines('text-a2', [], ['x\n', 'y\n', 'z\n'])
179
k.add_lines('text-am', ['text-a1', 'text-a2'], ['k\n', 'y\n', 'c\n'])
180
origins = k.annotate('text-am')
181
self.assertEquals(origins[0], ('text-am', 'k\n'))
182
self.assertEquals(origins[1], ('text-a2', 'y\n'))
183
self.assertEquals(origins[2], ('text-a1', 'c\n'))
185
def test_annotate_merge_3(self):
186
k = self.make_test_knit(True)
187
k.add_lines('text-a1', [], ['a\n', 'b\n', 'c\n'])
188
k.add_lines('text-a2', [] ,['x\n', 'y\n', 'z\n'])
189
k.add_lines('text-am', ['text-a1', 'text-a2'], ['k\n', 'y\n', 'z\n'])
190
origins = k.annotate('text-am')
191
self.assertEquals(origins[0], ('text-am', 'k\n'))
192
self.assertEquals(origins[1], ('text-a2', 'y\n'))
193
self.assertEquals(origins[2], ('text-a2', 'z\n'))
195
def test_annotate_merge_4(self):
196
k = self.make_test_knit(True)
197
k.add_lines('text-a1', [], ['a\n', 'b\n', 'c\n'])
198
k.add_lines('text-a2', [], ['x\n', 'y\n', 'z\n'])
199
k.add_lines('text-a3', ['text-a1'], ['a\n', 'b\n', 'p\n'])
200
k.add_lines('text-am', ['text-a2', 'text-a3'], ['a\n', 'b\n', 'z\n'])
201
origins = k.annotate('text-am')
202
self.assertEquals(origins[0], ('text-a1', 'a\n'))
203
self.assertEquals(origins[1], ('text-a1', 'b\n'))
204
self.assertEquals(origins[2], ('text-a2', 'z\n'))
206
def test_annotate_merge_5(self):
207
k = self.make_test_knit(True)
208
k.add_lines('text-a1', [], ['a\n', 'b\n', 'c\n'])
209
k.add_lines('text-a2', [], ['d\n', 'e\n', 'f\n'])
210
k.add_lines('text-a3', [], ['x\n', 'y\n', 'z\n'])
211
k.add_lines('text-am',
212
['text-a1', 'text-a2', 'text-a3'],
213
['a\n', 'e\n', 'z\n'])
214
origins = k.annotate('text-am')
215
self.assertEquals(origins[0], ('text-a1', 'a\n'))
216
self.assertEquals(origins[1], ('text-a2', 'e\n'))
217
self.assertEquals(origins[2], ('text-a3', 'z\n'))
219
def test_annotate_file_cherry_pick(self):
220
k = self.make_test_knit(True)
221
k.add_lines('text-1', [], ['a\n', 'b\n', 'c\n'])
222
k.add_lines('text-2', ['text-1'], ['d\n', 'e\n', 'f\n'])
223
k.add_lines('text-3', ['text-2', 'text-1'], ['a\n', 'b\n', 'c\n'])
224
origins = k.annotate('text-3')
225
self.assertEquals(origins[0], ('text-1', 'a\n'))
226
self.assertEquals(origins[1], ('text-1', 'b\n'))
227
self.assertEquals(origins[2], ('text-1', 'c\n'))
229
def test_knit_join(self):
230
"""Store in knit with parents"""
231
k1 = KnitVersionedFile('test1', get_transport('.'), factory=KnitPlainFactory(), create=True)
232
k1.add_lines('text-a', [], split_lines(TEXT_1))
233
k1.add_lines('text-b', ['text-a'], split_lines(TEXT_1))
235
k1.add_lines('text-c', [], split_lines(TEXT_1))
236
k1.add_lines('text-d', ['text-c'], split_lines(TEXT_1))
238
k1.add_lines('text-m', ['text-b', 'text-d'], split_lines(TEXT_1))
240
k2 = KnitVersionedFile('test2', get_transport('.'), factory=KnitPlainFactory(), create=True)
241
count = k2.join(k1, version_ids=['text-m'])
242
self.assertEquals(count, 5)
243
self.assertTrue(k2.has_version('text-a'))
244
self.assertTrue(k2.has_version('text-c'))
246
def test_reannotate(self):
247
k1 = KnitVersionedFile('knit1', get_transport('.'),
248
factory=KnitAnnotateFactory(), create=True)
250
k1.add_lines('text-a', [], ['a\n', 'b\n'])
252
k1.add_lines('text-b', ['text-a'], ['a\n', 'c\n'])
254
k2 = KnitVersionedFile('test2', get_transport('.'),
255
factory=KnitAnnotateFactory(), create=True)
256
k2.join(k1, version_ids=['text-b'])
259
k1.add_lines('text-X', ['text-b'], ['a\n', 'b\n'])
261
k2.add_lines('text-c', ['text-b'], ['z\n', 'c\n'])
263
k2.add_lines('text-Y', ['text-b'], ['b\n', 'c\n'])
265
# test-c will have index 3
266
k1.join(k2, version_ids=['text-c'])
268
lines = k1.get_lines('text-c')
269
self.assertEquals(lines, ['z\n', 'c\n'])
271
origins = k1.annotate('text-c')
272
self.assertEquals(origins[0], ('text-c', 'z\n'))
273
self.assertEquals(origins[1], ('text-b', 'c\n'))
275
def test_extraction_reads_components_once(self):
276
t = MemoryTransport()
277
instrumented_t = TransportLogger(t)
278
k1 = KnitVersionedFile('id', instrumented_t, create=True, delta=True)
279
# should read the index
280
self.assertEqual([('id.kndx',)], instrumented_t._calls)
281
instrumented_t._calls = []
283
k1.add_lines('base', [], ['text\n'])
284
# should not have read at all
285
self.assertEqual([], instrumented_t._calls)
288
k1.add_lines('sub', ['base'], ['text\n', 'text2\n'])
289
# should not have read at all
290
self.assertEqual([], instrumented_t._calls)
294
# should not have read at all
295
self.assertEqual([], instrumented_t._calls)
302
# should have read a component
303
# should not have read the first component only
304
self.assertEqual([('id.knit', [(0, 87)])], instrumented_t._calls)
305
instrumented_t._calls = []
308
# should not have read at all
309
self.assertEqual([], instrumented_t._calls)
310
# and now read the other component
312
# should have read the second component
313
self.assertEqual([('id.knit', [(87, 93)])], instrumented_t._calls)
314
instrumented_t._calls = []
319
k1.add_lines('sub2', ['base'], ['text\n', 'text3\n'])
320
# should read the first component only
321
self.assertEqual([('id.knit', [(0, 87)])], instrumented_t._calls)
323
def test_iter_lines_reads_in_order(self):
324
t = MemoryTransport()
325
instrumented_t = TransportLogger(t)
326
k1 = KnitVersionedFile('id', instrumented_t, create=True, delta=True)
327
self.assertEqual([('id.kndx',)], instrumented_t._calls)
328
# add texts with no required ordering
329
k1.add_lines('base', [], ['text\n'])
330
k1.add_lines('base2', [], ['text2\n'])
332
instrumented_t._calls = []
333
# request a last-first iteration
334
results = list(k1.iter_lines_added_or_present_in_versions(['base2', 'base']))
335
self.assertEqual([('id.knit', [(0, 87), (87, 89)])], instrumented_t._calls)
336
self.assertEqual(['text\n', 'text2\n'], results)
338
def test_create_empty_annotated(self):
339
k1 = self.make_test_knit(True)
341
k1.add_lines('text-a', [], ['a\n', 'b\n'])
342
k2 = k1.create_empty('t', MemoryTransport())
343
self.assertTrue(isinstance(k2.factory, KnitAnnotateFactory))
344
self.assertEqual(k1.delta, k2.delta)
345
# the generic test checks for empty content and file class
347
def test_knit_format(self):
348
# this tests that a new knit index file has the expected content
349
# and that is writes the data we expect as records are added.
350
knit = self.make_test_knit(True)
351
self.assertFileEqual("# bzr knit index 8\n", 'test.kndx')
352
knit.add_lines_with_ghosts('revid', ['a_ghost'], ['a\n'])
353
self.assertFileEqual(
354
"# bzr knit index 8\n"
356
"revid fulltext 0 84 .a_ghost :",
358
knit.add_lines_with_ghosts('revid2', ['revid'], ['a\n'])
359
self.assertFileEqual(
360
"# bzr knit index 8\n"
361
"\nrevid fulltext 0 84 .a_ghost :"
362
"\nrevid2 line-delta 84 82 0 :",
364
# we should be able to load this file again
365
knit = KnitVersionedFile('test', get_transport('.'), access_mode='r')
366
self.assertEqual(['revid', 'revid2'], knit.versions())
367
# write a short write to the file and ensure that its ignored
368
indexfile = file('test.kndx', 'at')
369
indexfile.write('\nrevid3 line-delta 166 82 1 2 3 4 5 .phwoar:demo ')
371
# we should be able to load this file again
372
knit = KnitVersionedFile('test', get_transport('.'), access_mode='w')
373
self.assertEqual(['revid', 'revid2'], knit.versions())
374
# and add a revision with the same id the failed write had
375
knit.add_lines('revid3', ['revid2'], ['a\n'])
376
# and when reading it revid3 should now appear.
377
knit = KnitVersionedFile('test', get_transport('.'), access_mode='r')
378
self.assertEqual(['revid', 'revid2', 'revid3'], knit.versions())
379
self.assertEqual(['revid2'], knit.get_parents('revid3'))
381
def test_plan_merge(self):
382
my_knit = self.make_test_knit(annotate=True)
383
my_knit.add_lines('text1', [], split_lines(TEXT_1))
384
my_knit.add_lines('text1a', ['text1'], split_lines(TEXT_1A))
385
my_knit.add_lines('text1b', ['text1'], split_lines(TEXT_1B))
386
plan = list(my_knit.plan_merge('text1a', 'text1b'))
387
for plan_line, expected_line in zip(plan, AB_MERGE):
388
self.assertEqual(plan_line, expected_line)
62
from bzrlib.tuned_gzip import GzipFile
63
from bzrlib.versionedfile import (
66
RecordingVersionedFilesDecorator,
70
class _CompiledKnitFeature(Feature):
74
import bzrlib._knit_load_data_c
79
def feature_name(self):
80
return 'bzrlib._knit_load_data_c'
82
CompiledKnitFeature = _CompiledKnitFeature()
85
class KnitContentTestsMixin(object):
87
def test_constructor(self):
88
content = self._make_content([])
91
content = self._make_content([])
92
self.assertEqual(content.text(), [])
94
content = self._make_content([("origin1", "text1"), ("origin2", "text2")])
95
self.assertEqual(content.text(), ["text1", "text2"])
98
content = self._make_content([("origin1", "text1"), ("origin2", "text2")])
100
self.assertIsInstance(copy, content.__class__)
101
self.assertEqual(copy.annotate(), content.annotate())
103
def assertDerivedBlocksEqual(self, source, target, noeol=False):
104
"""Assert that the derived matching blocks match real output"""
105
source_lines = source.splitlines(True)
106
target_lines = target.splitlines(True)
108
if noeol and not line.endswith('\n'):
112
source_content = self._make_content([(None, nl(l)) for l in source_lines])
113
target_content = self._make_content([(None, nl(l)) for l in target_lines])
114
line_delta = source_content.line_delta(target_content)
115
delta_blocks = list(KnitContent.get_line_delta_blocks(line_delta,
116
source_lines, target_lines))
117
matcher = KnitSequenceMatcher(None, source_lines, target_lines)
118
matcher_blocks = list(list(matcher.get_matching_blocks()))
119
self.assertEqual(matcher_blocks, delta_blocks)
121
def test_get_line_delta_blocks(self):
122
self.assertDerivedBlocksEqual('a\nb\nc\n', 'q\nc\n')
123
self.assertDerivedBlocksEqual(TEXT_1, TEXT_1)
124
self.assertDerivedBlocksEqual(TEXT_1, TEXT_1A)
125
self.assertDerivedBlocksEqual(TEXT_1, TEXT_1B)
126
self.assertDerivedBlocksEqual(TEXT_1B, TEXT_1A)
127
self.assertDerivedBlocksEqual(TEXT_1A, TEXT_1B)
128
self.assertDerivedBlocksEqual(TEXT_1A, '')
129
self.assertDerivedBlocksEqual('', TEXT_1A)
130
self.assertDerivedBlocksEqual('', '')
131
self.assertDerivedBlocksEqual('a\nb\nc', 'a\nb\nc\nd')
133
def test_get_line_delta_blocks_noeol(self):
134
"""Handle historical knit deltas safely
136
Some existing knit deltas don't consider the last line to differ
137
when the only difference whether it has a final newline.
139
New knit deltas appear to always consider the last line to differ
142
self.assertDerivedBlocksEqual('a\nb\nc', 'a\nb\nc\nd\n', noeol=True)
143
self.assertDerivedBlocksEqual('a\nb\nc\nd\n', 'a\nb\nc', noeol=True)
144
self.assertDerivedBlocksEqual('a\nb\nc\n', 'a\nb\nc', noeol=True)
145
self.assertDerivedBlocksEqual('a\nb\nc', 'a\nb\nc\n', noeol=True)
435
AB_MERGE_TEXT="""unchanged|Banana cup cake recipe
440
new-b|- bananas (do not use plantains!!!)
441
unchanged|- broken tea cups
442
new-a|- self-raising flour
445
AB_MERGE=[tuple(l.split('|')) for l in AB_MERGE_TEXT.splitlines(True)]
448
def line_delta(from_lines, to_lines):
449
"""Generate line-based delta from one text to another"""
450
s = difflib.SequenceMatcher(None, from_lines, to_lines)
451
for op in s.get_opcodes():
454
yield '%d,%d,%d\n' % (op[1], op[2], op[4]-op[3])
455
for i in range(op[3], op[4]):
459
def apply_line_delta(basis_lines, delta_lines):
460
"""Apply a line-based perfect diff
462
basis_lines -- text to apply the patch to
463
delta_lines -- diff instructions and content
193
class TestPlainKnitContent(TestCase, KnitContentTestsMixin):
195
def _make_content(self, lines):
196
annotated_content = AnnotatedKnitContent(lines)
197
return PlainKnitContent(annotated_content.text(), 'bogus')
199
def test_annotate(self):
200
content = self._make_content([])
201
self.assertEqual(content.annotate(), [])
203
content = self._make_content([("origin1", "text1"), ("origin2", "text2")])
204
self.assertEqual(content.annotate(),
205
[("bogus", "text1"), ("bogus", "text2")])
207
def test_line_delta(self):
208
content1 = self._make_content([("", "a"), ("", "b")])
209
content2 = self._make_content([("", "a"), ("", "a"), ("", "c")])
210
self.assertEqual(content1.line_delta(content2),
211
[(1, 2, 2, ["a", "c"])])
213
def test_line_delta_iter(self):
214
content1 = self._make_content([("", "a"), ("", "b")])
215
content2 = self._make_content([("", "a"), ("", "a"), ("", "c")])
216
it = content1.line_delta_iter(content2)
217
self.assertEqual(it.next(), (1, 2, 2, ["a", "c"]))
218
self.assertRaises(StopIteration, it.next)
221
class TestAnnotatedKnitContent(TestCase, KnitContentTestsMixin):
223
def _make_content(self, lines):
224
return AnnotatedKnitContent(lines)
226
def test_annotate(self):
227
content = self._make_content([])
228
self.assertEqual(content.annotate(), [])
230
content = self._make_content([("origin1", "text1"), ("origin2", "text2")])
231
self.assertEqual(content.annotate(),
232
[("origin1", "text1"), ("origin2", "text2")])
234
def test_line_delta(self):
235
content1 = self._make_content([("", "a"), ("", "b")])
236
content2 = self._make_content([("", "a"), ("", "a"), ("", "c")])
237
self.assertEqual(content1.line_delta(content2),
238
[(1, 2, 2, [("", "a"), ("", "c")])])
240
def test_line_delta_iter(self):
241
content1 = self._make_content([("", "a"), ("", "b")])
242
content2 = self._make_content([("", "a"), ("", "a"), ("", "c")])
243
it = content1.line_delta_iter(content2)
244
self.assertEqual(it.next(), (1, 2, 2, [("", "a"), ("", "c")]))
245
self.assertRaises(StopIteration, it.next)
248
class MockTransport(object):
250
def __init__(self, file_lines=None):
251
self.file_lines = file_lines
253
# We have no base directory for the MockTransport
256
def get(self, filename):
257
if self.file_lines is None:
258
raise NoSuchFile(filename)
260
return StringIO("\n".join(self.file_lines))
262
def readv(self, relpath, offsets):
263
fp = self.get(relpath)
264
for offset, size in offsets:
266
yield offset, fp.read(size)
268
def __getattr__(self, name):
269
def queue_call(*args, **kwargs):
270
self.calls.append((name, args, kwargs))
274
class MockReadvFailingTransport(MockTransport):
275
"""Fail in the middle of a readv() result.
277
This Transport will successfully yield the first two requested hunks, but
278
raise NoSuchFile for the rest.
468
while i < len(delta_lines):
470
a, b, c = map(long, l.split(','))
472
out[offset+a:offset+b] = delta_lines[i:i+c]
474
offset = offset + (b - a) + c
478
class TestWeaveToKnit(KnitTests):
480
def test_weave_to_knit_matches(self):
481
# check that the WeaveToKnit is_compatible function
482
# registers True for a Weave to a Knit.
281
def readv(self, relpath, offsets):
283
for result in MockTransport.readv(self, relpath, offsets):
285
# we use 2 because the first offset is the pack header, the second
286
# is the first actual content requset
288
raise errors.NoSuchFile(relpath)
292
class KnitRecordAccessTestsMixin(object):
293
"""Tests for getting and putting knit records."""
295
def test_add_raw_records(self):
296
"""Add_raw_records adds records retrievable later."""
297
access = self.get_access()
298
memos = access.add_raw_records([('key', 10)], '1234567890')
299
self.assertEqual(['1234567890'], list(access.get_raw_records(memos)))
301
def test_add_several_raw_records(self):
302
"""add_raw_records with many records and read some back."""
303
access = self.get_access()
304
memos = access.add_raw_records([('key', 10), ('key2', 2), ('key3', 5)],
306
self.assertEqual(['1234567890', '12', '34567'],
307
list(access.get_raw_records(memos)))
308
self.assertEqual(['1234567890'],
309
list(access.get_raw_records(memos[0:1])))
310
self.assertEqual(['12'],
311
list(access.get_raw_records(memos[1:2])))
312
self.assertEqual(['34567'],
313
list(access.get_raw_records(memos[2:3])))
314
self.assertEqual(['1234567890', '34567'],
315
list(access.get_raw_records(memos[0:1] + memos[2:3])))
318
class TestKnitKnitAccess(TestCaseWithMemoryTransport, KnitRecordAccessTestsMixin):
319
"""Tests for the .kndx implementation."""
321
def get_access(self):
322
"""Get a .knit style access instance."""
323
mapper = ConstantMapper("foo")
324
access = _KnitKeyAccess(self.get_transport(), mapper)
328
class _TestException(Exception):
329
"""Just an exception for local tests to use."""
332
class TestPackKnitAccess(TestCaseWithMemoryTransport, KnitRecordAccessTestsMixin):
333
"""Tests for the pack based access."""
335
def get_access(self):
336
return self._get_access()[0]
338
def _get_access(self, packname='packfile', index='FOO'):
339
transport = self.get_transport()
340
def write_data(bytes):
341
transport.append_bytes(packname, bytes)
342
writer = pack.ContainerWriter(write_data)
344
access = _DirectPackAccess({})
345
access.set_writer(writer, index, (transport, packname))
346
return access, writer
348
def make_pack_file(self):
349
"""Create a pack file with 2 records."""
350
access, writer = self._get_access(packname='packname', index='foo')
352
memos.extend(access.add_raw_records([('key1', 10)], '1234567890'))
353
memos.extend(access.add_raw_records([('key2', 5)], '12345'))
357
def make_vf_for_retrying(self):
358
"""Create 3 packs and a reload function.
360
Originally, 2 pack files will have the data, but one will be missing.
361
And then the third will be used in place of the first two if reload()
364
:return: (versioned_file, reload_counter)
365
versioned_file a KnitVersionedFiles using the packs for access
367
tree = self.make_branch_and_memory_tree('tree')
370
tree.add([''], ['root-id'])
371
tree.commit('one', rev_id='rev-1')
372
tree.commit('two', rev_id='rev-2')
373
tree.commit('three', rev_id='rev-3')
374
# Pack these two revisions into another pack file, but don't remove
376
repo = tree.branch.repository
377
collection = repo._pack_collection
378
collection.ensure_loaded()
379
orig_packs = collection.packs
380
packer = pack_repo.Packer(collection, orig_packs, '.testpack')
381
new_pack = packer.pack()
383
vf = tree.branch.repository.revisions
386
tree.branch.repository.lock_read()
387
self.addCleanup(tree.branch.repository.unlock)
389
# Set up a reload() function that switches to using the new pack file
390
new_index = new_pack.revision_index
391
access_tuple = new_pack.access_tuple()
392
reload_counter = [0, 0, 0]
394
reload_counter[0] += 1
395
if reload_counter[1] > 0:
396
# We already reloaded, nothing more to do
397
reload_counter[2] += 1
399
reload_counter[1] += 1
400
vf._index._graph_index._indices[:] = [new_index]
401
vf._access._indices.clear()
402
vf._access._indices[new_index] = access_tuple
404
# Delete one of the pack files so the data will need to be reloaded. We
405
# will delete the file with 'rev-2' in it
406
trans, name = orig_packs[1].access_tuple()
408
# We don't have the index trigger reloading because we want to test
409
# that we reload when the .pack disappears
410
vf._access._reload_func = reload
411
return vf, reload_counter
413
def make_reload_func(self, return_val=True):
416
reload_called[0] += 1
418
return reload_called, reload
420
def make_retry_exception(self):
421
# We raise a real exception so that sys.exc_info() is properly
424
raise _TestException('foobar')
425
except _TestException, e:
426
retry_exc = errors.RetryWithNewPacks(None, reload_occurred=False,
427
exc_info=sys.exc_info())
430
def test_read_from_several_packs(self):
431
access, writer = self._get_access()
433
memos.extend(access.add_raw_records([('key', 10)], '1234567890'))
435
access, writer = self._get_access('pack2', 'FOOBAR')
436
memos.extend(access.add_raw_records([('key', 5)], '12345'))
438
access, writer = self._get_access('pack3', 'BAZ')
439
memos.extend(access.add_raw_records([('key', 5)], 'alpha'))
441
transport = self.get_transport()
442
access = _DirectPackAccess({"FOO":(transport, 'packfile'),
443
"FOOBAR":(transport, 'pack2'),
444
"BAZ":(transport, 'pack3')})
445
self.assertEqual(['1234567890', '12345', 'alpha'],
446
list(access.get_raw_records(memos)))
447
self.assertEqual(['1234567890'],
448
list(access.get_raw_records(memos[0:1])))
449
self.assertEqual(['12345'],
450
list(access.get_raw_records(memos[1:2])))
451
self.assertEqual(['alpha'],
452
list(access.get_raw_records(memos[2:3])))
453
self.assertEqual(['1234567890', 'alpha'],
454
list(access.get_raw_records(memos[0:1] + memos[2:3])))
456
def test_set_writer(self):
457
"""The writer should be settable post construction."""
458
access = _DirectPackAccess({})
459
transport = self.get_transport()
460
packname = 'packfile'
462
def write_data(bytes):
463
transport.append_bytes(packname, bytes)
464
writer = pack.ContainerWriter(write_data)
466
access.set_writer(writer, index, (transport, packname))
467
memos = access.add_raw_records([('key', 10)], '1234567890')
469
self.assertEqual(['1234567890'], list(access.get_raw_records(memos)))
471
def test_missing_index_raises_retry(self):
472
memos = self.make_pack_file()
473
transport = self.get_transport()
474
reload_called, reload_func = self.make_reload_func()
475
# Note that the index key has changed from 'foo' to 'bar'
476
access = _DirectPackAccess({'bar':(transport, 'packname')},
477
reload_func=reload_func)
478
e = self.assertListRaises(errors.RetryWithNewPacks,
479
access.get_raw_records, memos)
480
# Because a key was passed in which does not match our index list, we
481
# assume that the listing was already reloaded
482
self.assertTrue(e.reload_occurred)
483
self.assertIsInstance(e.exc_info, tuple)
484
self.assertIs(e.exc_info[0], KeyError)
485
self.assertIsInstance(e.exc_info[1], KeyError)
487
def test_missing_index_raises_key_error_with_no_reload(self):
488
memos = self.make_pack_file()
489
transport = self.get_transport()
490
# Note that the index key has changed from 'foo' to 'bar'
491
access = _DirectPackAccess({'bar':(transport, 'packname')})
492
e = self.assertListRaises(KeyError, access.get_raw_records, memos)
494
def test_missing_file_raises_retry(self):
495
memos = self.make_pack_file()
496
transport = self.get_transport()
497
reload_called, reload_func = self.make_reload_func()
498
# Note that the 'filename' has been changed to 'different-packname'
499
access = _DirectPackAccess({'foo':(transport, 'different-packname')},
500
reload_func=reload_func)
501
e = self.assertListRaises(errors.RetryWithNewPacks,
502
access.get_raw_records, memos)
503
# The file has gone missing, so we assume we need to reload
504
self.assertFalse(e.reload_occurred)
505
self.assertIsInstance(e.exc_info, tuple)
506
self.assertIs(e.exc_info[0], errors.NoSuchFile)
507
self.assertIsInstance(e.exc_info[1], errors.NoSuchFile)
508
self.assertEqual('different-packname', e.exc_info[1].path)
510
def test_missing_file_raises_no_such_file_with_no_reload(self):
511
memos = self.make_pack_file()
512
transport = self.get_transport()
513
# Note that the 'filename' has been changed to 'different-packname'
514
access = _DirectPackAccess({'foo':(transport, 'different-packname')})
515
e = self.assertListRaises(errors.NoSuchFile,
516
access.get_raw_records, memos)
518
def test_failing_readv_raises_retry(self):
519
memos = self.make_pack_file()
520
transport = self.get_transport()
521
failing_transport = MockReadvFailingTransport(
522
[transport.get_bytes('packname')])
523
reload_called, reload_func = self.make_reload_func()
524
access = _DirectPackAccess({'foo':(failing_transport, 'packname')},
525
reload_func=reload_func)
526
# Asking for a single record will not trigger the Mock failure
527
self.assertEqual(['1234567890'],
528
list(access.get_raw_records(memos[:1])))
529
self.assertEqual(['12345'],
530
list(access.get_raw_records(memos[1:2])))
531
# A multiple offset readv() will fail mid-way through
532
e = self.assertListRaises(errors.RetryWithNewPacks,
533
access.get_raw_records, memos)
534
# The file has gone missing, so we assume we need to reload
535
self.assertFalse(e.reload_occurred)
536
self.assertIsInstance(e.exc_info, tuple)
537
self.assertIs(e.exc_info[0], errors.NoSuchFile)
538
self.assertIsInstance(e.exc_info[1], errors.NoSuchFile)
539
self.assertEqual('packname', e.exc_info[1].path)
541
def test_failing_readv_raises_no_such_file_with_no_reload(self):
542
memos = self.make_pack_file()
543
transport = self.get_transport()
544
failing_transport = MockReadvFailingTransport(
545
[transport.get_bytes('packname')])
546
reload_called, reload_func = self.make_reload_func()
547
access = _DirectPackAccess({'foo':(failing_transport, 'packname')})
548
# Asking for a single record will not trigger the Mock failure
549
self.assertEqual(['1234567890'],
550
list(access.get_raw_records(memos[:1])))
551
self.assertEqual(['12345'],
552
list(access.get_raw_records(memos[1:2])))
553
# A multiple offset readv() will fail mid-way through
554
e = self.assertListRaises(errors.NoSuchFile,
555
access.get_raw_records, memos)
557
def test_reload_or_raise_no_reload(self):
558
access = _DirectPackAccess({}, reload_func=None)
559
retry_exc = self.make_retry_exception()
560
# Without a reload_func, we will just re-raise the original exception
561
self.assertRaises(_TestException, access.reload_or_raise, retry_exc)
563
def test_reload_or_raise_reload_changed(self):
564
reload_called, reload_func = self.make_reload_func(return_val=True)
565
access = _DirectPackAccess({}, reload_func=reload_func)
566
retry_exc = self.make_retry_exception()
567
access.reload_or_raise(retry_exc)
568
self.assertEqual([1], reload_called)
569
retry_exc.reload_occurred=True
570
access.reload_or_raise(retry_exc)
571
self.assertEqual([2], reload_called)
573
def test_reload_or_raise_reload_no_change(self):
574
reload_called, reload_func = self.make_reload_func(return_val=False)
575
access = _DirectPackAccess({}, reload_func=reload_func)
576
retry_exc = self.make_retry_exception()
577
# If reload_occurred is False, then we consider it an error to have
578
# reload_func() return False (no changes).
579
self.assertRaises(_TestException, access.reload_or_raise, retry_exc)
580
self.assertEqual([1], reload_called)
581
retry_exc.reload_occurred=True
582
# If reload_occurred is True, then we assume nothing changed because
583
# it had changed earlier, but didn't change again
584
access.reload_or_raise(retry_exc)
585
self.assertEqual([2], reload_called)
587
def test_annotate_retries(self):
588
vf, reload_counter = self.make_vf_for_retrying()
589
# It is a little bit bogus to annotate the Revision VF, but it works,
590
# as we have ancestry stored there
592
reload_lines = vf.annotate(key)
593
self.assertEqual([1, 1, 0], reload_counter)
594
plain_lines = vf.annotate(key)
595
self.assertEqual([1, 1, 0], reload_counter) # No extra reloading
596
if reload_lines != plain_lines:
597
self.fail('Annotation was not identical with reloading.')
598
# Now delete the packs-in-use, which should trigger another reload, but
599
# this time we just raise an exception because we can't recover
600
for trans, name in vf._access._indices.itervalues():
602
self.assertRaises(errors.NoSuchFile, vf.annotate, key)
603
self.assertEqual([2, 1, 1], reload_counter)
605
def test__get_record_map_retries(self):
606
vf, reload_counter = self.make_vf_for_retrying()
607
keys = [('rev-1',), ('rev-2',), ('rev-3',)]
608
records = vf._get_record_map(keys)
609
self.assertEqual(keys, sorted(records.keys()))
610
self.assertEqual([1, 1, 0], reload_counter)
611
# Now delete the packs-in-use, which should trigger another reload, but
612
# this time we just raise an exception because we can't recover
613
for trans, name in vf._access._indices.itervalues():
615
self.assertRaises(errors.NoSuchFile, vf._get_record_map, keys)
616
self.assertEqual([2, 1, 1], reload_counter)
618
def test_get_record_stream_retries(self):
619
vf, reload_counter = self.make_vf_for_retrying()
620
keys = [('rev-1',), ('rev-2',), ('rev-3',)]
621
record_stream = vf.get_record_stream(keys, 'topological', False)
622
record = record_stream.next()
623
self.assertEqual(('rev-1',), record.key)
624
self.assertEqual([0, 0, 0], reload_counter)
625
record = record_stream.next()
626
self.assertEqual(('rev-2',), record.key)
627
self.assertEqual([1, 1, 0], reload_counter)
628
record = record_stream.next()
629
self.assertEqual(('rev-3',), record.key)
630
self.assertEqual([1, 1, 0], reload_counter)
631
# Now delete all pack files, and see that we raise the right error
632
for trans, name in vf._access._indices.itervalues():
634
self.assertListRaises(errors.NoSuchFile,
635
vf.get_record_stream, keys, 'topological', False)
637
def test_iter_lines_added_or_present_in_keys_retries(self):
638
vf, reload_counter = self.make_vf_for_retrying()
639
keys = [('rev-1',), ('rev-2',), ('rev-3',)]
640
# Unfortunately, iter_lines_added_or_present_in_keys iterates the
641
# result in random order (determined by the iteration order from a
642
# set()), so we don't have any solid way to trigger whether data is
643
# read before or after. However we tried to delete the middle node to
644
# exercise the code well.
645
# What we care about is that all lines are always yielded, but not
648
reload_lines = sorted(vf.iter_lines_added_or_present_in_keys(keys))
649
self.assertEqual([1, 1, 0], reload_counter)
650
# Now do it again, to make sure the result is equivalent
651
plain_lines = sorted(vf.iter_lines_added_or_present_in_keys(keys))
652
self.assertEqual([1, 1, 0], reload_counter) # No extra reloading
653
self.assertEqual(plain_lines, reload_lines)
654
self.assertEqual(21, len(plain_lines))
655
# Now delete all pack files, and see that we raise the right error
656
for trans, name in vf._access._indices.itervalues():
658
self.assertListRaises(errors.NoSuchFile,
659
vf.iter_lines_added_or_present_in_keys, keys)
660
self.assertEqual([2, 1, 1], reload_counter)
662
def test_get_record_stream_yields_disk_sorted_order(self):
663
# if we get 'unordered' pick a semi-optimal order for reading. The
664
# order should be grouped by pack file, and then by position in file
665
repo = self.make_repository('test', format='pack-0.92')
667
self.addCleanup(repo.unlock)
668
repo.start_write_group()
670
vf.add_lines(('f-id', 'rev-5'), [('f-id', 'rev-4')], ['lines\n'])
671
vf.add_lines(('f-id', 'rev-1'), [], ['lines\n'])
672
vf.add_lines(('f-id', 'rev-2'), [('f-id', 'rev-1')], ['lines\n'])
673
repo.commit_write_group()
674
# We inserted them as rev-5, rev-1, rev-2, we should get them back in
676
stream = vf.get_record_stream([('f-id', 'rev-1'), ('f-id', 'rev-5'),
677
('f-id', 'rev-2')], 'unordered', False)
678
keys = [r.key for r in stream]
679
self.assertEqual([('f-id', 'rev-5'), ('f-id', 'rev-1'),
680
('f-id', 'rev-2')], keys)
681
repo.start_write_group()
682
vf.add_lines(('f-id', 'rev-4'), [('f-id', 'rev-3')], ['lines\n'])
683
vf.add_lines(('f-id', 'rev-3'), [('f-id', 'rev-2')], ['lines\n'])
684
vf.add_lines(('f-id', 'rev-6'), [('f-id', 'rev-5')], ['lines\n'])
685
repo.commit_write_group()
686
# Request in random order, to make sure the output order isn't based on
688
request_keys = set(('f-id', 'rev-%d' % i) for i in range(1, 7))
689
stream = vf.get_record_stream(request_keys, 'unordered', False)
690
keys = [r.key for r in stream]
691
# We want to get the keys back in disk order, but it doesn't matter
692
# which pack we read from first. So this can come back in 2 orders
693
alt1 = [('f-id', 'rev-%d' % i) for i in [4, 3, 6, 5, 1, 2]]
694
alt2 = [('f-id', 'rev-%d' % i) for i in [5, 1, 2, 4, 3, 6]]
695
if keys != alt1 and keys != alt2:
696
self.fail('Returned key order did not match either expected order.'
697
' expected %s or %s, not %s'
698
% (alt1, alt2, keys))
701
class LowLevelKnitDataTests(TestCase):
703
def create_gz_content(self, text):
705
gz_file = gzip.GzipFile(mode='wb', fileobj=sio)
708
return sio.getvalue()
710
def make_multiple_records(self):
711
"""Create the content for multiple records."""
712
sha1sum = osutils.sha('foo\nbar\n').hexdigest()
714
gz_txt = self.create_gz_content('version rev-id-1 2 %s\n'
719
record_1 = (0, len(gz_txt), sha1sum)
720
total_txt.append(gz_txt)
721
sha1sum = osutils.sha('baz\n').hexdigest()
722
gz_txt = self.create_gz_content('version rev-id-2 1 %s\n'
726
record_2 = (record_1[1], len(gz_txt), sha1sum)
727
total_txt.append(gz_txt)
728
return total_txt, record_1, record_2
730
def test_valid_knit_data(self):
731
sha1sum = osutils.sha('foo\nbar\n').hexdigest()
732
gz_txt = self.create_gz_content('version rev-id-1 2 %s\n'
737
transport = MockTransport([gz_txt])
738
access = _KnitKeyAccess(transport, ConstantMapper('filename'))
739
knit = KnitVersionedFiles(None, access)
740
records = [(('rev-id-1',), (('rev-id-1',), 0, len(gz_txt)))]
742
contents = list(knit._read_records_iter(records))
743
self.assertEqual([(('rev-id-1',), ['foo\n', 'bar\n'],
744
'4e48e2c9a3d2ca8a708cb0cc545700544efb5021')], contents)
746
raw_contents = list(knit._read_records_iter_raw(records))
747
self.assertEqual([(('rev-id-1',), gz_txt, sha1sum)], raw_contents)
749
def test_multiple_records_valid(self):
750
total_txt, record_1, record_2 = self.make_multiple_records()
751
transport = MockTransport([''.join(total_txt)])
752
access = _KnitKeyAccess(transport, ConstantMapper('filename'))
753
knit = KnitVersionedFiles(None, access)
754
records = [(('rev-id-1',), (('rev-id-1',), record_1[0], record_1[1])),
755
(('rev-id-2',), (('rev-id-2',), record_2[0], record_2[1]))]
757
contents = list(knit._read_records_iter(records))
758
self.assertEqual([(('rev-id-1',), ['foo\n', 'bar\n'], record_1[2]),
759
(('rev-id-2',), ['baz\n'], record_2[2])],
762
raw_contents = list(knit._read_records_iter_raw(records))
763
self.assertEqual([(('rev-id-1',), total_txt[0], record_1[2]),
764
(('rev-id-2',), total_txt[1], record_2[2])],
767
def test_not_enough_lines(self):
768
sha1sum = osutils.sha('foo\n').hexdigest()
769
# record says 2 lines data says 1
770
gz_txt = self.create_gz_content('version rev-id-1 2 %s\n'
774
transport = MockTransport([gz_txt])
775
access = _KnitKeyAccess(transport, ConstantMapper('filename'))
776
knit = KnitVersionedFiles(None, access)
777
records = [(('rev-id-1',), (('rev-id-1',), 0, len(gz_txt)))]
778
self.assertRaises(errors.KnitCorrupt, list,
779
knit._read_records_iter(records))
781
# read_records_iter_raw won't detect that sort of mismatch/corruption
782
raw_contents = list(knit._read_records_iter_raw(records))
783
self.assertEqual([(('rev-id-1',), gz_txt, sha1sum)], raw_contents)
785
def test_too_many_lines(self):
786
sha1sum = osutils.sha('foo\nbar\n').hexdigest()
787
# record says 1 lines data says 2
788
gz_txt = self.create_gz_content('version rev-id-1 1 %s\n'
793
transport = MockTransport([gz_txt])
794
access = _KnitKeyAccess(transport, ConstantMapper('filename'))
795
knit = KnitVersionedFiles(None, access)
796
records = [(('rev-id-1',), (('rev-id-1',), 0, len(gz_txt)))]
797
self.assertRaises(errors.KnitCorrupt, list,
798
knit._read_records_iter(records))
800
# read_records_iter_raw won't detect that sort of mismatch/corruption
801
raw_contents = list(knit._read_records_iter_raw(records))
802
self.assertEqual([(('rev-id-1',), gz_txt, sha1sum)], raw_contents)
804
def test_mismatched_version_id(self):
805
sha1sum = osutils.sha('foo\nbar\n').hexdigest()
806
gz_txt = self.create_gz_content('version rev-id-1 2 %s\n'
811
transport = MockTransport([gz_txt])
812
access = _KnitKeyAccess(transport, ConstantMapper('filename'))
813
knit = KnitVersionedFiles(None, access)
814
# We are asking for rev-id-2, but the data is rev-id-1
815
records = [(('rev-id-2',), (('rev-id-2',), 0, len(gz_txt)))]
816
self.assertRaises(errors.KnitCorrupt, list,
817
knit._read_records_iter(records))
819
# read_records_iter_raw detects mismatches in the header
820
self.assertRaises(errors.KnitCorrupt, list,
821
knit._read_records_iter_raw(records))
823
def test_uncompressed_data(self):
824
sha1sum = osutils.sha('foo\nbar\n').hexdigest()
825
txt = ('version rev-id-1 2 %s\n'
830
transport = MockTransport([txt])
831
access = _KnitKeyAccess(transport, ConstantMapper('filename'))
832
knit = KnitVersionedFiles(None, access)
833
records = [(('rev-id-1',), (('rev-id-1',), 0, len(txt)))]
835
# We don't have valid gzip data ==> corrupt
836
self.assertRaises(errors.KnitCorrupt, list,
837
knit._read_records_iter(records))
839
# read_records_iter_raw will notice the bad data
840
self.assertRaises(errors.KnitCorrupt, list,
841
knit._read_records_iter_raw(records))
843
def test_corrupted_data(self):
844
sha1sum = osutils.sha('foo\nbar\n').hexdigest()
845
gz_txt = self.create_gz_content('version rev-id-1 2 %s\n'
850
# Change 2 bytes in the middle to \xff
851
gz_txt = gz_txt[:10] + '\xff\xff' + gz_txt[12:]
852
transport = MockTransport([gz_txt])
853
access = _KnitKeyAccess(transport, ConstantMapper('filename'))
854
knit = KnitVersionedFiles(None, access)
855
records = [(('rev-id-1',), (('rev-id-1',), 0, len(gz_txt)))]
856
self.assertRaises(errors.KnitCorrupt, list,
857
knit._read_records_iter(records))
858
# read_records_iter_raw will barf on bad gz data
859
self.assertRaises(errors.KnitCorrupt, list,
860
knit._read_records_iter_raw(records))
863
class LowLevelKnitIndexTests(TestCase):
865
def get_knit_index(self, transport, name, mode):
866
mapper = ConstantMapper(name)
867
orig = knit._load_data
869
knit._load_data = orig
870
self.addCleanup(reset)
871
from bzrlib._knit_load_data_py import _load_data_py
872
knit._load_data = _load_data_py
873
allow_writes = lambda: 'w' in mode
874
return _KndxIndex(transport, mapper, lambda:None, allow_writes, lambda:True)
876
def test_create_file(self):
877
transport = MockTransport()
878
index = self.get_knit_index(transport, "filename", "w")
880
call = transport.calls.pop(0)
881
# call[1][1] is a StringIO - we can't test it by simple equality.
882
self.assertEqual('put_file_non_atomic', call[0])
883
self.assertEqual('filename.kndx', call[1][0])
884
# With no history, _KndxIndex writes a new index:
885
self.assertEqual(_KndxIndex.HEADER,
886
call[1][1].getvalue())
887
self.assertEqual({'create_parent_dir': True}, call[2])
889
def test_read_utf8_version_id(self):
890
unicode_revision_id = u"version-\N{CYRILLIC CAPITAL LETTER A}"
891
utf8_revision_id = unicode_revision_id.encode('utf-8')
892
transport = MockTransport([
894
'%s option 0 1 :' % (utf8_revision_id,)
896
index = self.get_knit_index(transport, "filename", "r")
897
# _KndxIndex is a private class, and deals in utf8 revision_ids, not
898
# Unicode revision_ids.
899
self.assertEqual({(utf8_revision_id,):()},
900
index.get_parent_map(index.keys()))
901
self.assertFalse((unicode_revision_id,) in index.keys())
903
def test_read_utf8_parents(self):
904
unicode_revision_id = u"version-\N{CYRILLIC CAPITAL LETTER A}"
905
utf8_revision_id = unicode_revision_id.encode('utf-8')
906
transport = MockTransport([
908
"version option 0 1 .%s :" % (utf8_revision_id,)
910
index = self.get_knit_index(transport, "filename", "r")
911
self.assertEqual({("version",):((utf8_revision_id,),)},
912
index.get_parent_map(index.keys()))
914
def test_read_ignore_corrupted_lines(self):
915
transport = MockTransport([
918
"corrupted options 0 1 .b .c ",
919
"version options 0 1 :"
921
index = self.get_knit_index(transport, "filename", "r")
922
self.assertEqual(1, len(index.keys()))
923
self.assertEqual(set([("version",)]), index.keys())
925
def test_read_corrupted_header(self):
926
transport = MockTransport(['not a bzr knit index header\n'])
927
index = self.get_knit_index(transport, "filename", "r")
928
self.assertRaises(KnitHeaderError, index.keys)
930
def test_read_duplicate_entries(self):
931
transport = MockTransport([
933
"parent options 0 1 :",
934
"version options1 0 1 0 :",
935
"version options2 1 2 .other :",
936
"version options3 3 4 0 .other :"
938
index = self.get_knit_index(transport, "filename", "r")
939
self.assertEqual(2, len(index.keys()))
940
# check that the index used is the first one written. (Specific
941
# to KnitIndex style indices.
942
self.assertEqual("1", index._dictionary_compress([("version",)]))
943
self.assertEqual((("version",), 3, 4), index.get_position(("version",)))
944
self.assertEqual(["options3"], index.get_options(("version",)))
945
self.assertEqual({("version",):(("parent",), ("other",))},
946
index.get_parent_map([("version",)]))
948
def test_read_compressed_parents(self):
949
transport = MockTransport([
953
"c option 0 1 1 0 :",
955
index = self.get_knit_index(transport, "filename", "r")
956
self.assertEqual({("b",):(("a",),), ("c",):(("b",), ("a",))},
957
index.get_parent_map([("b",), ("c",)]))
959
def test_write_utf8_version_id(self):
960
unicode_revision_id = u"version-\N{CYRILLIC CAPITAL LETTER A}"
961
utf8_revision_id = unicode_revision_id.encode('utf-8')
962
transport = MockTransport([
965
index = self.get_knit_index(transport, "filename", "r")
967
((utf8_revision_id,), ["option"], ((utf8_revision_id,), 0, 1), [])])
968
call = transport.calls.pop(0)
969
# call[1][1] is a StringIO - we can't test it by simple equality.
970
self.assertEqual('put_file_non_atomic', call[0])
971
self.assertEqual('filename.kndx', call[1][0])
972
# With no history, _KndxIndex writes a new index:
973
self.assertEqual(_KndxIndex.HEADER +
974
"\n%s option 0 1 :" % (utf8_revision_id,),
975
call[1][1].getvalue())
976
self.assertEqual({'create_parent_dir': True}, call[2])
978
def test_write_utf8_parents(self):
979
unicode_revision_id = u"version-\N{CYRILLIC CAPITAL LETTER A}"
980
utf8_revision_id = unicode_revision_id.encode('utf-8')
981
transport = MockTransport([
984
index = self.get_knit_index(transport, "filename", "r")
986
(("version",), ["option"], (("version",), 0, 1), [(utf8_revision_id,)])])
987
call = transport.calls.pop(0)
988
# call[1][1] is a StringIO - we can't test it by simple equality.
989
self.assertEqual('put_file_non_atomic', call[0])
990
self.assertEqual('filename.kndx', call[1][0])
991
# With no history, _KndxIndex writes a new index:
992
self.assertEqual(_KndxIndex.HEADER +
993
"\nversion option 0 1 .%s :" % (utf8_revision_id,),
994
call[1][1].getvalue())
995
self.assertEqual({'create_parent_dir': True}, call[2])
998
transport = MockTransport([
1001
index = self.get_knit_index(transport, "filename", "r")
1003
self.assertEqual(set(), index.keys())
1005
index.add_records([(("a",), ["option"], (("a",), 0, 1), [])])
1006
self.assertEqual(set([("a",)]), index.keys())
1008
index.add_records([(("a",), ["option"], (("a",), 0, 1), [])])
1009
self.assertEqual(set([("a",)]), index.keys())
1011
index.add_records([(("b",), ["option"], (("b",), 0, 1), [])])
1012
self.assertEqual(set([("a",), ("b",)]), index.keys())
1014
def add_a_b(self, index, random_id=None):
1016
if random_id is not None:
1017
kwargs["random_id"] = random_id
1019
(("a",), ["option"], (("a",), 0, 1), [("b",)]),
1020
(("a",), ["opt"], (("a",), 1, 2), [("c",)]),
1021
(("b",), ["option"], (("b",), 2, 3), [("a",)])
1024
def assertIndexIsAB(self, index):
1029
index.get_parent_map(index.keys()))
1030
self.assertEqual((("a",), 1, 2), index.get_position(("a",)))
1031
self.assertEqual((("b",), 2, 3), index.get_position(("b",)))
1032
self.assertEqual(["opt"], index.get_options(("a",)))
1034
def test_add_versions(self):
1035
transport = MockTransport([
1038
index = self.get_knit_index(transport, "filename", "r")
1041
call = transport.calls.pop(0)
1042
# call[1][1] is a StringIO - we can't test it by simple equality.
1043
self.assertEqual('put_file_non_atomic', call[0])
1044
self.assertEqual('filename.kndx', call[1][0])
1045
# With no history, _KndxIndex writes a new index:
1048
"\na option 0 1 .b :"
1050
"\nb option 2 3 0 :",
1051
call[1][1].getvalue())
1052
self.assertEqual({'create_parent_dir': True}, call[2])
1053
self.assertIndexIsAB(index)
1055
def test_add_versions_random_id_is_accepted(self):
1056
transport = MockTransport([
1059
index = self.get_knit_index(transport, "filename", "r")
1060
self.add_a_b(index, random_id=True)
1062
def test_delay_create_and_add_versions(self):
1063
transport = MockTransport()
1065
index = self.get_knit_index(transport, "filename", "w")
1067
self.assertEqual([], transport.calls)
1070
#[ {"dir_mode": 0777, "create_parent_dir": True, "mode": "wb"},
1072
# Two calls: one during which we load the existing index (and when its
1073
# missing create it), then a second where we write the contents out.
1074
self.assertEqual(2, len(transport.calls))
1075
call = transport.calls.pop(0)
1076
self.assertEqual('put_file_non_atomic', call[0])
1077
self.assertEqual('filename.kndx', call[1][0])
1078
# With no history, _KndxIndex writes a new index:
1079
self.assertEqual(_KndxIndex.HEADER, call[1][1].getvalue())
1080
self.assertEqual({'create_parent_dir': True}, call[2])
1081
call = transport.calls.pop(0)
1082
# call[1][1] is a StringIO - we can't test it by simple equality.
1083
self.assertEqual('put_file_non_atomic', call[0])
1084
self.assertEqual('filename.kndx', call[1][0])
1085
# With no history, _KndxIndex writes a new index:
1088
"\na option 0 1 .b :"
1090
"\nb option 2 3 0 :",
1091
call[1][1].getvalue())
1092
self.assertEqual({'create_parent_dir': True}, call[2])
1094
def test_get_position(self):
1095
transport = MockTransport([
1100
index = self.get_knit_index(transport, "filename", "r")
1102
self.assertEqual((("a",), 0, 1), index.get_position(("a",)))
1103
self.assertEqual((("b",), 1, 2), index.get_position(("b",)))
1105
def test_get_method(self):
1106
transport = MockTransport([
1108
"a fulltext,unknown 0 1 :",
1109
"b unknown,line-delta 1 2 :",
1112
index = self.get_knit_index(transport, "filename", "r")
1114
self.assertEqual("fulltext", index.get_method("a"))
1115
self.assertEqual("line-delta", index.get_method("b"))
1116
self.assertRaises(errors.KnitIndexUnknownMethod, index.get_method, "c")
1118
def test_get_options(self):
1119
transport = MockTransport([
1124
index = self.get_knit_index(transport, "filename", "r")
1126
self.assertEqual(["opt1"], index.get_options("a"))
1127
self.assertEqual(["opt2", "opt3"], index.get_options("b"))
1129
def test_get_parent_map(self):
1130
transport = MockTransport([
1133
"b option 1 2 0 .c :",
1134
"c option 1 2 1 0 .e :"
1136
index = self.get_knit_index(transport, "filename", "r")
1140
("b",):(("a",), ("c",)),
1141
("c",):(("b",), ("a",), ("e",)),
1142
}, index.get_parent_map(index.keys()))
1144
def test_impossible_parent(self):
1145
"""Test we get KnitCorrupt if the parent couldn't possibly exist."""
1146
transport = MockTransport([
1149
"b option 0 1 4 :" # We don't have a 4th record
1151
index = self.get_knit_index(transport, 'filename', 'r')
1153
self.assertRaises(errors.KnitCorrupt, index.keys)
1154
except TypeError, e:
1155
if (str(e) == ('exceptions must be strings, classes, or instances,'
1156
' not exceptions.IndexError')
1157
and sys.version_info[0:2] >= (2,5)):
1158
self.knownFailure('Pyrex <0.9.5 fails with TypeError when'
1159
' raising new style exceptions with python'
1164
def test_corrupted_parent(self):
1165
transport = MockTransport([
1169
"c option 0 1 1v :", # Can't have a parent of '1v'
1171
index = self.get_knit_index(transport, 'filename', 'r')
1173
self.assertRaises(errors.KnitCorrupt, index.keys)
1174
except TypeError, e:
1175
if (str(e) == ('exceptions must be strings, classes, or instances,'
1176
' not exceptions.ValueError')
1177
and sys.version_info[0:2] >= (2,5)):
1178
self.knownFailure('Pyrex <0.9.5 fails with TypeError when'
1179
' raising new style exceptions with python'
1184
def test_corrupted_parent_in_list(self):
1185
transport = MockTransport([
1189
"c option 0 1 1 v :", # Can't have a parent of 'v'
1191
index = self.get_knit_index(transport, 'filename', 'r')
1193
self.assertRaises(errors.KnitCorrupt, index.keys)
1194
except TypeError, e:
1195
if (str(e) == ('exceptions must be strings, classes, or instances,'
1196
' not exceptions.ValueError')
1197
and sys.version_info[0:2] >= (2,5)):
1198
self.knownFailure('Pyrex <0.9.5 fails with TypeError when'
1199
' raising new style exceptions with python'
1204
def test_invalid_position(self):
1205
transport = MockTransport([
1209
index = self.get_knit_index(transport, 'filename', 'r')
1211
self.assertRaises(errors.KnitCorrupt, index.keys)
1212
except TypeError, e:
1213
if (str(e) == ('exceptions must be strings, classes, or instances,'
1214
' not exceptions.ValueError')
1215
and sys.version_info[0:2] >= (2,5)):
1216
self.knownFailure('Pyrex <0.9.5 fails with TypeError when'
1217
' raising new style exceptions with python'
1222
def test_invalid_size(self):
1223
transport = MockTransport([
1227
index = self.get_knit_index(transport, 'filename', 'r')
1229
self.assertRaises(errors.KnitCorrupt, index.keys)
1230
except TypeError, e:
1231
if (str(e) == ('exceptions must be strings, classes, or instances,'
1232
' not exceptions.ValueError')
1233
and sys.version_info[0:2] >= (2,5)):
1234
self.knownFailure('Pyrex <0.9.5 fails with TypeError when'
1235
' raising new style exceptions with python'
1240
def test_short_line(self):
1241
transport = MockTransport([
1244
"b option 10 10 0", # This line isn't terminated, ignored
1246
index = self.get_knit_index(transport, "filename", "r")
1247
self.assertEqual(set([('a',)]), index.keys())
1249
def test_skip_incomplete_record(self):
1250
# A line with bogus data should just be skipped
1251
transport = MockTransport([
1254
"b option 10 10 0", # This line isn't terminated, ignored
1255
"c option 20 10 0 :", # Properly terminated, and starts with '\n'
1257
index = self.get_knit_index(transport, "filename", "r")
1258
self.assertEqual(set([('a',), ('c',)]), index.keys())
1260
def test_trailing_characters(self):
1261
# A line with bogus data should just be skipped
1262
transport = MockTransport([
1265
"b option 10 10 0 :a", # This line has extra trailing characters
1266
"c option 20 10 0 :", # Properly terminated, and starts with '\n'
1268
index = self.get_knit_index(transport, "filename", "r")
1269
self.assertEqual(set([('a',), ('c',)]), index.keys())
1272
class LowLevelKnitIndexTests_c(LowLevelKnitIndexTests):
1274
_test_needs_features = [CompiledKnitFeature]
1276
def get_knit_index(self, transport, name, mode):
1277
mapper = ConstantMapper(name)
1278
orig = knit._load_data
1280
knit._load_data = orig
1281
self.addCleanup(reset)
1282
from bzrlib._knit_load_data_c import _load_data_c
1283
knit._load_data = _load_data_c
1284
allow_writes = lambda: mode == 'w'
1285
return _KndxIndex(transport, mapper, lambda:None, allow_writes, lambda:True)
1288
class KnitTests(TestCaseWithTransport):
1289
"""Class containing knit test helper routines."""
1291
def make_test_knit(self, annotate=False, name='test'):
1292
mapper = ConstantMapper(name)
1293
return make_file_factory(annotate, mapper)(self.get_transport())
1296
class TestBadShaError(KnitTests):
1297
"""Tests for handling of sha errors."""
1299
def test_exception_has_text(self):
1300
# having the failed text included in the error allows for recovery.
1301
source = self.make_test_knit()
1302
target = self.make_test_knit(name="target")
1303
if not source._max_delta_chain:
1304
raise TestNotApplicable(
1305
"cannot get delta-caused sha failures without deltas.")
1308
broken = ('broken',)
1309
source.add_lines(basis, (), ['foo\n'])
1310
source.add_lines(broken, (basis,), ['foo\n', 'bar\n'])
1311
# Seed target with a bad basis text
1312
target.add_lines(basis, (), ['gam\n'])
1313
target.insert_record_stream(
1314
source.get_record_stream([broken], 'unordered', False))
1315
err = self.assertRaises(errors.KnitCorrupt,
1316
target.get_record_stream([broken], 'unordered', True).next)
1317
self.assertEqual(['gam\n', 'bar\n'], err.content)
1318
# Test for formatting with live data
1319
self.assertStartsWith(str(err), "Knit ")
1322
class TestKnitIndex(KnitTests):
1324
def test_add_versions_dictionary_compresses(self):
1325
"""Adding versions to the index should update the lookup dict"""
1326
knit = self.make_test_knit()
1328
idx.add_records([(('a-1',), ['fulltext'], (('a-1',), 0, 0), [])])
1329
self.check_file_contents('test.kndx',
1330
'# bzr knit index 8\n'
1332
'a-1 fulltext 0 0 :'
1335
(('a-2',), ['fulltext'], (('a-2',), 0, 0), [('a-1',)]),
1336
(('a-3',), ['fulltext'], (('a-3',), 0, 0), [('a-2',)]),
1338
self.check_file_contents('test.kndx',
1339
'# bzr knit index 8\n'
1341
'a-1 fulltext 0 0 :\n'
1342
'a-2 fulltext 0 0 0 :\n'
1343
'a-3 fulltext 0 0 1 :'
1345
self.assertEqual(set([('a-3',), ('a-1',), ('a-2',)]), idx.keys())
1347
('a-1',): ((('a-1',), 0, 0), None, (), ('fulltext', False)),
1348
('a-2',): ((('a-2',), 0, 0), None, (('a-1',),), ('fulltext', False)),
1349
('a-3',): ((('a-3',), 0, 0), None, (('a-2',),), ('fulltext', False)),
1350
}, idx.get_build_details(idx.keys()))
1351
self.assertEqual({('a-1',):(),
1352
('a-2',):(('a-1',),),
1353
('a-3',):(('a-2',),),},
1354
idx.get_parent_map(idx.keys()))
1356
def test_add_versions_fails_clean(self):
1357
"""If add_versions fails in the middle, it restores a pristine state.
1359
Any modifications that are made to the index are reset if all versions
1362
# This cheats a little bit by passing in a generator which will
1363
# raise an exception before the processing finishes
1364
# Other possibilities would be to have an version with the wrong number
1365
# of entries, or to make the backing transport unable to write any
1368
knit = self.make_test_knit()
1370
idx.add_records([(('a-1',), ['fulltext'], (('a-1',), 0, 0), [])])
1372
class StopEarly(Exception):
1375
def generate_failure():
1376
"""Add some entries and then raise an exception"""
1377
yield (('a-2',), ['fulltext'], (None, 0, 0), ('a-1',))
1378
yield (('a-3',), ['fulltext'], (None, 0, 0), ('a-2',))
1381
# Assert the pre-condition
1383
self.assertEqual(set([('a-1',)]), set(idx.keys()))
1385
{('a-1',): ((('a-1',), 0, 0), None, (), ('fulltext', False))},
1386
idx.get_build_details([('a-1',)]))
1387
self.assertEqual({('a-1',):()}, idx.get_parent_map(idx.keys()))
1390
self.assertRaises(StopEarly, idx.add_records, generate_failure())
1391
# And it shouldn't be modified
1394
def test_knit_index_ignores_empty_files(self):
1395
# There was a race condition in older bzr, where a ^C at the right time
1396
# could leave an empty .kndx file, which bzr would later claim was a
1397
# corrupted file since the header was not present. In reality, the file
1398
# just wasn't created, so it should be ignored.
1399
t = get_transport('.')
1400
t.put_bytes('test.kndx', '')
1402
knit = self.make_test_knit()
1404
def test_knit_index_checks_header(self):
1405
t = get_transport('.')
1406
t.put_bytes('test.kndx', '# not really a knit header\n\n')
484
1407
k = self.make_test_knit()
485
self.failUnless(WeaveToKnit.is_compatible(w, k))
486
self.failIf(WeaveToKnit.is_compatible(k, w))
487
self.failIf(WeaveToKnit.is_compatible(w, w))
488
self.failIf(WeaveToKnit.is_compatible(k, k))
1408
self.assertRaises(KnitHeaderError, k.keys)
1411
class TestGraphIndexKnit(KnitTests):
1412
"""Tests for knits using a GraphIndex rather than a KnitIndex."""
1414
def make_g_index(self, name, ref_lists=0, nodes=[]):
1415
builder = GraphIndexBuilder(ref_lists)
1416
for node, references, value in nodes:
1417
builder.add_node(node, references, value)
1418
stream = builder.finish()
1419
trans = self.get_transport()
1420
size = trans.put_file(name, stream)
1421
return GraphIndex(trans, name, size)
1423
def two_graph_index(self, deltas=False, catch_adds=False):
1424
"""Build a two-graph index.
1426
:param deltas: If true, use underlying indices with two node-ref
1427
lists and 'parent' set to a delta-compressed against tail.
1429
# build a complex graph across several indices.
1431
# delta compression inn the index
1432
index1 = self.make_g_index('1', 2, [
1433
(('tip', ), 'N0 100', ([('parent', )], [], )),
1434
(('tail', ), '', ([], []))])
1435
index2 = self.make_g_index('2', 2, [
1436
(('parent', ), ' 100 78', ([('tail', ), ('ghost', )], [('tail', )])),
1437
(('separate', ), '', ([], []))])
1439
# just blob location and graph in the index.
1440
index1 = self.make_g_index('1', 1, [
1441
(('tip', ), 'N0 100', ([('parent', )], )),
1442
(('tail', ), '', ([], ))])
1443
index2 = self.make_g_index('2', 1, [
1444
(('parent', ), ' 100 78', ([('tail', ), ('ghost', )], )),
1445
(('separate', ), '', ([], ))])
1446
combined_index = CombinedGraphIndex([index1, index2])
1448
self.combined_index = combined_index
1449
self.caught_entries = []
1450
add_callback = self.catch_add
1453
return _KnitGraphIndex(combined_index, lambda:True, deltas=deltas,
1454
add_callback=add_callback)
1456
def test_keys(self):
1457
index = self.two_graph_index()
1458
self.assertEqual(set([('tail',), ('tip',), ('parent',), ('separate',)]),
1461
def test_get_position(self):
1462
index = self.two_graph_index()
1463
self.assertEqual((index._graph_index._indices[0], 0, 100), index.get_position(('tip',)))
1464
self.assertEqual((index._graph_index._indices[1], 100, 78), index.get_position(('parent',)))
1466
def test_get_method_deltas(self):
1467
index = self.two_graph_index(deltas=True)
1468
self.assertEqual('fulltext', index.get_method(('tip',)))
1469
self.assertEqual('line-delta', index.get_method(('parent',)))
1471
def test_get_method_no_deltas(self):
1472
# check that the parent-history lookup is ignored with deltas=False.
1473
index = self.two_graph_index(deltas=False)
1474
self.assertEqual('fulltext', index.get_method(('tip',)))
1475
self.assertEqual('fulltext', index.get_method(('parent',)))
1477
def test_get_options_deltas(self):
1478
index = self.two_graph_index(deltas=True)
1479
self.assertEqual(['fulltext', 'no-eol'], index.get_options(('tip',)))
1480
self.assertEqual(['line-delta'], index.get_options(('parent',)))
1482
def test_get_options_no_deltas(self):
1483
# check that the parent-history lookup is ignored with deltas=False.
1484
index = self.two_graph_index(deltas=False)
1485
self.assertEqual(['fulltext', 'no-eol'], index.get_options(('tip',)))
1486
self.assertEqual(['fulltext'], index.get_options(('parent',)))
1488
def test_get_parent_map(self):
1489
index = self.two_graph_index()
1490
self.assertEqual({('parent',):(('tail',), ('ghost',))},
1491
index.get_parent_map([('parent',), ('ghost',)]))
1493
def catch_add(self, entries):
1494
self.caught_entries.append(entries)
1496
def test_add_no_callback_errors(self):
1497
index = self.two_graph_index()
1498
self.assertRaises(errors.ReadOnlyError, index.add_records,
1499
[(('new',), 'fulltext,no-eol', (None, 50, 60), ['separate'])])
1501
def test_add_version_smoke(self):
1502
index = self.two_graph_index(catch_adds=True)
1503
index.add_records([(('new',), 'fulltext,no-eol', (None, 50, 60),
1505
self.assertEqual([[(('new', ), 'N50 60', ((('separate',),),))]],
1506
self.caught_entries)
1508
def test_add_version_delta_not_delta_index(self):
1509
index = self.two_graph_index(catch_adds=True)
1510
self.assertRaises(errors.KnitCorrupt, index.add_records,
1511
[(('new',), 'no-eol,line-delta', (None, 0, 100), [('parent',)])])
1512
self.assertEqual([], self.caught_entries)
1514
def test_add_version_same_dup(self):
1515
index = self.two_graph_index(catch_adds=True)
1516
# options can be spelt two different ways
1517
index.add_records([(('tip',), 'fulltext,no-eol', (None, 0, 100), [('parent',)])])
1518
index.add_records([(('tip',), 'no-eol,fulltext', (None, 0, 100), [('parent',)])])
1519
# position/length are ignored (because each pack could have fulltext or
1520
# delta, and be at a different position.
1521
index.add_records([(('tip',), 'fulltext,no-eol', (None, 50, 100),
1523
index.add_records([(('tip',), 'fulltext,no-eol', (None, 0, 1000),
1525
# but neither should have added data:
1526
self.assertEqual([[], [], [], []], self.caught_entries)
1528
def test_add_version_different_dup(self):
1529
index = self.two_graph_index(deltas=True, catch_adds=True)
1531
self.assertRaises(errors.KnitCorrupt, index.add_records,
1532
[(('tip',), 'no-eol,line-delta', (None, 0, 100), [('parent',)])])
1533
self.assertRaises(errors.KnitCorrupt, index.add_records,
1534
[(('tip',), 'line-delta,no-eol', (None, 0, 100), [('parent',)])])
1535
self.assertRaises(errors.KnitCorrupt, index.add_records,
1536
[(('tip',), 'fulltext', (None, 0, 100), [('parent',)])])
1538
self.assertRaises(errors.KnitCorrupt, index.add_records,
1539
[(('tip',), 'fulltext,no-eol', (None, 0, 100), [])])
1540
self.assertEqual([], self.caught_entries)
1542
def test_add_versions_nodeltas(self):
1543
index = self.two_graph_index(catch_adds=True)
1545
(('new',), 'fulltext,no-eol', (None, 50, 60), [('separate',)]),
1546
(('new2',), 'fulltext', (None, 0, 6), [('new',)]),
1548
self.assertEqual([(('new', ), 'N50 60', ((('separate',),),)),
1549
(('new2', ), ' 0 6', ((('new',),),))],
1550
sorted(self.caught_entries[0]))
1551
self.assertEqual(1, len(self.caught_entries))
1553
def test_add_versions_deltas(self):
1554
index = self.two_graph_index(deltas=True, catch_adds=True)
1556
(('new',), 'fulltext,no-eol', (None, 50, 60), [('separate',)]),
1557
(('new2',), 'line-delta', (None, 0, 6), [('new',)]),
1559
self.assertEqual([(('new', ), 'N50 60', ((('separate',),), ())),
1560
(('new2', ), ' 0 6', ((('new',),), (('new',),), ))],
1561
sorted(self.caught_entries[0]))
1562
self.assertEqual(1, len(self.caught_entries))
1564
def test_add_versions_delta_not_delta_index(self):
1565
index = self.two_graph_index(catch_adds=True)
1566
self.assertRaises(errors.KnitCorrupt, index.add_records,
1567
[(('new',), 'no-eol,line-delta', (None, 0, 100), [('parent',)])])
1568
self.assertEqual([], self.caught_entries)
1570
def test_add_versions_random_id_accepted(self):
1571
index = self.two_graph_index(catch_adds=True)
1572
index.add_records([], random_id=True)
1574
def test_add_versions_same_dup(self):
1575
index = self.two_graph_index(catch_adds=True)
1576
# options can be spelt two different ways
1577
index.add_records([(('tip',), 'fulltext,no-eol', (None, 0, 100),
1579
index.add_records([(('tip',), 'no-eol,fulltext', (None, 0, 100),
1581
# position/length are ignored (because each pack could have fulltext or
1582
# delta, and be at a different position.
1583
index.add_records([(('tip',), 'fulltext,no-eol', (None, 50, 100),
1585
index.add_records([(('tip',), 'fulltext,no-eol', (None, 0, 1000),
1587
# but neither should have added data.
1588
self.assertEqual([[], [], [], []], self.caught_entries)
1590
def test_add_versions_different_dup(self):
1591
index = self.two_graph_index(deltas=True, catch_adds=True)
1593
self.assertRaises(errors.KnitCorrupt, index.add_records,
1594
[(('tip',), 'no-eol,line-delta', (None, 0, 100), [('parent',)])])
1595
self.assertRaises(errors.KnitCorrupt, index.add_records,
1596
[(('tip',), 'line-delta,no-eol', (None, 0, 100), [('parent',)])])
1597
self.assertRaises(errors.KnitCorrupt, index.add_records,
1598
[(('tip',), 'fulltext', (None, 0, 100), [('parent',)])])
1600
self.assertRaises(errors.KnitCorrupt, index.add_records,
1601
[(('tip',), 'fulltext,no-eol', (None, 0, 100), [])])
1602
# change options in the second record
1603
self.assertRaises(errors.KnitCorrupt, index.add_records,
1604
[(('tip',), 'fulltext,no-eol', (None, 0, 100), [('parent',)]),
1605
(('tip',), 'no-eol,line-delta', (None, 0, 100), [('parent',)])])
1606
self.assertEqual([], self.caught_entries)
1609
class TestNoParentsGraphIndexKnit(KnitTests):
1610
"""Tests for knits using _KnitGraphIndex with no parents."""
1612
def make_g_index(self, name, ref_lists=0, nodes=[]):
1613
builder = GraphIndexBuilder(ref_lists)
1614
for node, references in nodes:
1615
builder.add_node(node, references)
1616
stream = builder.finish()
1617
trans = self.get_transport()
1618
size = trans.put_file(name, stream)
1619
return GraphIndex(trans, name, size)
1621
def test_parents_deltas_incompatible(self):
1622
index = CombinedGraphIndex([])
1623
self.assertRaises(errors.KnitError, _KnitGraphIndex, lambda:True,
1624
index, deltas=True, parents=False)
1626
def two_graph_index(self, catch_adds=False):
1627
"""Build a two-graph index.
1629
:param deltas: If true, use underlying indices with two node-ref
1630
lists and 'parent' set to a delta-compressed against tail.
1632
# put several versions in the index.
1633
index1 = self.make_g_index('1', 0, [
1634
(('tip', ), 'N0 100'),
1636
index2 = self.make_g_index('2', 0, [
1637
(('parent', ), ' 100 78'),
1638
(('separate', ), '')])
1639
combined_index = CombinedGraphIndex([index1, index2])
1641
self.combined_index = combined_index
1642
self.caught_entries = []
1643
add_callback = self.catch_add
1646
return _KnitGraphIndex(combined_index, lambda:True, parents=False,
1647
add_callback=add_callback)
1649
def test_keys(self):
1650
index = self.two_graph_index()
1651
self.assertEqual(set([('tail',), ('tip',), ('parent',), ('separate',)]),
1654
def test_get_position(self):
1655
index = self.two_graph_index()
1656
self.assertEqual((index._graph_index._indices[0], 0, 100),
1657
index.get_position(('tip',)))
1658
self.assertEqual((index._graph_index._indices[1], 100, 78),
1659
index.get_position(('parent',)))
1661
def test_get_method(self):
1662
index = self.two_graph_index()
1663
self.assertEqual('fulltext', index.get_method(('tip',)))
1664
self.assertEqual(['fulltext'], index.get_options(('parent',)))
1666
def test_get_options(self):
1667
index = self.two_graph_index()
1668
self.assertEqual(['fulltext', 'no-eol'], index.get_options(('tip',)))
1669
self.assertEqual(['fulltext'], index.get_options(('parent',)))
1671
def test_get_parent_map(self):
1672
index = self.two_graph_index()
1673
self.assertEqual({('parent',):None},
1674
index.get_parent_map([('parent',), ('ghost',)]))
1676
def catch_add(self, entries):
1677
self.caught_entries.append(entries)
1679
def test_add_no_callback_errors(self):
1680
index = self.two_graph_index()
1681
self.assertRaises(errors.ReadOnlyError, index.add_records,
1682
[(('new',), 'fulltext,no-eol', (None, 50, 60), [('separate',)])])
1684
def test_add_version_smoke(self):
1685
index = self.two_graph_index(catch_adds=True)
1686
index.add_records([(('new',), 'fulltext,no-eol', (None, 50, 60), [])])
1687
self.assertEqual([[(('new', ), 'N50 60')]],
1688
self.caught_entries)
1690
def test_add_version_delta_not_delta_index(self):
1691
index = self.two_graph_index(catch_adds=True)
1692
self.assertRaises(errors.KnitCorrupt, index.add_records,
1693
[(('new',), 'no-eol,line-delta', (None, 0, 100), [])])
1694
self.assertEqual([], self.caught_entries)
1696
def test_add_version_same_dup(self):
1697
index = self.two_graph_index(catch_adds=True)
1698
# options can be spelt two different ways
1699
index.add_records([(('tip',), 'fulltext,no-eol', (None, 0, 100), [])])
1700
index.add_records([(('tip',), 'no-eol,fulltext', (None, 0, 100), [])])
1701
# position/length are ignored (because each pack could have fulltext or
1702
# delta, and be at a different position.
1703
index.add_records([(('tip',), 'fulltext,no-eol', (None, 50, 100), [])])
1704
index.add_records([(('tip',), 'fulltext,no-eol', (None, 0, 1000), [])])
1705
# but neither should have added data.
1706
self.assertEqual([[], [], [], []], self.caught_entries)
1708
def test_add_version_different_dup(self):
1709
index = self.two_graph_index(catch_adds=True)
1711
self.assertRaises(errors.KnitCorrupt, index.add_records,
1712
[(('tip',), 'no-eol,line-delta', (None, 0, 100), [])])
1713
self.assertRaises(errors.KnitCorrupt, index.add_records,
1714
[(('tip',), 'line-delta,no-eol', (None, 0, 100), [])])
1715
self.assertRaises(errors.KnitCorrupt, index.add_records,
1716
[(('tip',), 'fulltext', (None, 0, 100), [])])
1718
self.assertRaises(errors.KnitCorrupt, index.add_records,
1719
[(('tip',), 'fulltext,no-eol', (None, 0, 100), [('parent',)])])
1720
self.assertEqual([], self.caught_entries)
1722
def test_add_versions(self):
1723
index = self.two_graph_index(catch_adds=True)
1725
(('new',), 'fulltext,no-eol', (None, 50, 60), []),
1726
(('new2',), 'fulltext', (None, 0, 6), []),
1728
self.assertEqual([(('new', ), 'N50 60'), (('new2', ), ' 0 6')],
1729
sorted(self.caught_entries[0]))
1730
self.assertEqual(1, len(self.caught_entries))
1732
def test_add_versions_delta_not_delta_index(self):
1733
index = self.two_graph_index(catch_adds=True)
1734
self.assertRaises(errors.KnitCorrupt, index.add_records,
1735
[(('new',), 'no-eol,line-delta', (None, 0, 100), [('parent',)])])
1736
self.assertEqual([], self.caught_entries)
1738
def test_add_versions_parents_not_parents_index(self):
1739
index = self.two_graph_index(catch_adds=True)
1740
self.assertRaises(errors.KnitCorrupt, index.add_records,
1741
[(('new',), 'no-eol,fulltext', (None, 0, 100), [('parent',)])])
1742
self.assertEqual([], self.caught_entries)
1744
def test_add_versions_random_id_accepted(self):
1745
index = self.two_graph_index(catch_adds=True)
1746
index.add_records([], random_id=True)
1748
def test_add_versions_same_dup(self):
1749
index = self.two_graph_index(catch_adds=True)
1750
# options can be spelt two different ways
1751
index.add_records([(('tip',), 'fulltext,no-eol', (None, 0, 100), [])])
1752
index.add_records([(('tip',), 'no-eol,fulltext', (None, 0, 100), [])])
1753
# position/length are ignored (because each pack could have fulltext or
1754
# delta, and be at a different position.
1755
index.add_records([(('tip',), 'fulltext,no-eol', (None, 50, 100), [])])
1756
index.add_records([(('tip',), 'fulltext,no-eol', (None, 0, 1000), [])])
1757
# but neither should have added data.
1758
self.assertEqual([[], [], [], []], self.caught_entries)
1760
def test_add_versions_different_dup(self):
1761
index = self.two_graph_index(catch_adds=True)
1763
self.assertRaises(errors.KnitCorrupt, index.add_records,
1764
[(('tip',), 'no-eol,line-delta', (None, 0, 100), [])])
1765
self.assertRaises(errors.KnitCorrupt, index.add_records,
1766
[(('tip',), 'line-delta,no-eol', (None, 0, 100), [])])
1767
self.assertRaises(errors.KnitCorrupt, index.add_records,
1768
[(('tip',), 'fulltext', (None, 0, 100), [])])
1770
self.assertRaises(errors.KnitCorrupt, index.add_records,
1771
[(('tip',), 'fulltext,no-eol', (None, 0, 100), [('parent',)])])
1772
# change options in the second record
1773
self.assertRaises(errors.KnitCorrupt, index.add_records,
1774
[(('tip',), 'fulltext,no-eol', (None, 0, 100), []),
1775
(('tip',), 'no-eol,line-delta', (None, 0, 100), [])])
1776
self.assertEqual([], self.caught_entries)
1779
class TestStacking(KnitTests):
1781
def get_basis_and_test_knit(self):
1782
basis = self.make_test_knit(name='basis')
1783
basis = RecordingVersionedFilesDecorator(basis)
1784
test = self.make_test_knit(name='test')
1785
test.add_fallback_versioned_files(basis)
1788
def test_add_fallback_versioned_files(self):
1789
basis = self.make_test_knit(name='basis')
1790
test = self.make_test_knit(name='test')
1791
# It must not error; other tests test that the fallback is referred to
1792
# when accessing data.
1793
test.add_fallback_versioned_files(basis)
1795
def test_add_lines(self):
1796
# lines added to the test are not added to the basis
1797
basis, test = self.get_basis_and_test_knit()
1799
key_basis = ('bar',)
1800
key_cross_border = ('quux',)
1801
key_delta = ('zaphod',)
1802
test.add_lines(key, (), ['foo\n'])
1803
self.assertEqual({}, basis.get_parent_map([key]))
1804
# lines added to the test that reference across the stack do a
1806
basis.add_lines(key_basis, (), ['foo\n'])
1808
test.add_lines(key_cross_border, (key_basis,), ['foo\n'])
1809
self.assertEqual('fulltext', test._index.get_method(key_cross_border))
1810
# we don't even need to look at the basis to see that this should be
1811
# stored as a fulltext
1812
self.assertEqual([], basis.calls)
1813
# Subsequent adds do delta.
1815
test.add_lines(key_delta, (key_cross_border,), ['foo\n'])
1816
self.assertEqual('line-delta', test._index.get_method(key_delta))
1817
self.assertEqual([], basis.calls)
1819
def test_annotate(self):
1820
# annotations from the test knit are answered without asking the basis
1821
basis, test = self.get_basis_and_test_knit()
1823
key_basis = ('bar',)
1824
key_missing = ('missing',)
1825
test.add_lines(key, (), ['foo\n'])
1826
details = test.annotate(key)
1827
self.assertEqual([(key, 'foo\n')], details)
1828
self.assertEqual([], basis.calls)
1829
# But texts that are not in the test knit are looked for in the basis
1831
basis.add_lines(key_basis, (), ['foo\n', 'bar\n'])
1833
details = test.annotate(key_basis)
1834
self.assertEqual([(key_basis, 'foo\n'), (key_basis, 'bar\n')], details)
1835
# Not optimised to date:
1836
# self.assertEqual([("annotate", key_basis)], basis.calls)
1837
self.assertEqual([('get_parent_map', set([key_basis])),
1838
('get_parent_map', set([key_basis])),
1839
('get_parent_map', set([key_basis])),
1840
('get_record_stream', [key_basis], 'unordered', True)],
1843
def test_check(self):
1844
# At the moment checking a stacked knit does implicitly check the
1846
basis, test = self.get_basis_and_test_knit()
1849
def test_get_parent_map(self):
1850
# parents in the test knit are answered without asking the basis
1851
basis, test = self.get_basis_and_test_knit()
1853
key_basis = ('bar',)
1854
key_missing = ('missing',)
1855
test.add_lines(key, (), [])
1856
parent_map = test.get_parent_map([key])
1857
self.assertEqual({key: ()}, parent_map)
1858
self.assertEqual([], basis.calls)
1859
# But parents that are not in the test knit are looked for in the basis
1860
basis.add_lines(key_basis, (), [])
1862
parent_map = test.get_parent_map([key, key_basis, key_missing])
1863
self.assertEqual({key: (),
1864
key_basis: ()}, parent_map)
1865
self.assertEqual([("get_parent_map", set([key_basis, key_missing]))],
1868
def test_get_record_stream_unordered_fulltexts(self):
1869
# records from the test knit are answered without asking the basis:
1870
basis, test = self.get_basis_and_test_knit()
1872
key_basis = ('bar',)
1873
key_missing = ('missing',)
1874
test.add_lines(key, (), ['foo\n'])
1875
records = list(test.get_record_stream([key], 'unordered', True))
1876
self.assertEqual(1, len(records))
1877
self.assertEqual([], basis.calls)
1878
# Missing (from test knit) objects are retrieved from the basis:
1879
basis.add_lines(key_basis, (), ['foo\n', 'bar\n'])
1881
records = list(test.get_record_stream([key_basis, key_missing],
1883
self.assertEqual(2, len(records))
1884
calls = list(basis.calls)
1885
for record in records:
1886
self.assertSubset([record.key], (key_basis, key_missing))
1887
if record.key == key_missing:
1888
self.assertIsInstance(record, AbsentContentFactory)
1890
reference = list(basis.get_record_stream([key_basis],
1891
'unordered', True))[0]
1892
self.assertEqual(reference.key, record.key)
1893
self.assertEqual(reference.sha1, record.sha1)
1894
self.assertEqual(reference.storage_kind, record.storage_kind)
1895
self.assertEqual(reference.get_bytes_as(reference.storage_kind),
1896
record.get_bytes_as(record.storage_kind))
1897
self.assertEqual(reference.get_bytes_as('fulltext'),
1898
record.get_bytes_as('fulltext'))
1899
# It's not strictly minimal, but it seems reasonable for now for it to
1900
# ask which fallbacks have which parents.
1902
("get_parent_map", set([key_basis, key_missing])),
1903
("get_record_stream", [key_basis], 'unordered', True)],
1906
def test_get_record_stream_ordered_fulltexts(self):
1907
# ordering is preserved down into the fallback store.
1908
basis, test = self.get_basis_and_test_knit()
1910
key_basis = ('bar',)
1911
key_basis_2 = ('quux',)
1912
key_missing = ('missing',)
1913
test.add_lines(key, (key_basis,), ['foo\n'])
1914
# Missing (from test knit) objects are retrieved from the basis:
1915
basis.add_lines(key_basis, (key_basis_2,), ['foo\n', 'bar\n'])
1916
basis.add_lines(key_basis_2, (), ['quux\n'])
1918
# ask for in non-topological order
1919
records = list(test.get_record_stream(
1920
[key, key_basis, key_missing, key_basis_2], 'topological', True))
1921
self.assertEqual(4, len(records))
1923
for record in records:
1924
self.assertSubset([record.key],
1925
(key_basis, key_missing, key_basis_2, key))
1926
if record.key == key_missing:
1927
self.assertIsInstance(record, AbsentContentFactory)
1929
results.append((record.key, record.sha1, record.storage_kind,
1930
record.get_bytes_as('fulltext')))
1931
calls = list(basis.calls)
1932
order = [record[0] for record in results]
1933
self.assertEqual([key_basis_2, key_basis, key], order)
1934
for result in results:
1935
if result[0] == key:
1939
record = source.get_record_stream([result[0]], 'unordered',
1941
self.assertEqual(record.key, result[0])
1942
self.assertEqual(record.sha1, result[1])
1943
self.assertEqual(record.storage_kind, result[2])
1944
self.assertEqual(record.get_bytes_as('fulltext'), result[3])
1945
# It's not strictly minimal, but it seems reasonable for now for it to
1946
# ask which fallbacks have which parents.
1948
("get_parent_map", set([key_basis, key_basis_2, key_missing])),
1949
# unordered is asked for by the underlying worker as it still
1950
# buffers everything while answering - which is a problem!
1951
("get_record_stream", [key_basis_2, key_basis], 'unordered', True)],
1954
def test_get_record_stream_unordered_deltas(self):
1955
# records from the test knit are answered without asking the basis:
1956
basis, test = self.get_basis_and_test_knit()
1958
key_basis = ('bar',)
1959
key_missing = ('missing',)
1960
test.add_lines(key, (), ['foo\n'])
1961
records = list(test.get_record_stream([key], 'unordered', False))
1962
self.assertEqual(1, len(records))
1963
self.assertEqual([], basis.calls)
1964
# Missing (from test knit) objects are retrieved from the basis:
1965
basis.add_lines(key_basis, (), ['foo\n', 'bar\n'])
1967
records = list(test.get_record_stream([key_basis, key_missing],
1968
'unordered', False))
1969
self.assertEqual(2, len(records))
1970
calls = list(basis.calls)
1971
for record in records:
1972
self.assertSubset([record.key], (key_basis, key_missing))
1973
if record.key == key_missing:
1974
self.assertIsInstance(record, AbsentContentFactory)
1976
reference = list(basis.get_record_stream([key_basis],
1977
'unordered', False))[0]
1978
self.assertEqual(reference.key, record.key)
1979
self.assertEqual(reference.sha1, record.sha1)
1980
self.assertEqual(reference.storage_kind, record.storage_kind)
1981
self.assertEqual(reference.get_bytes_as(reference.storage_kind),
1982
record.get_bytes_as(record.storage_kind))
1983
# It's not strictly minimal, but it seems reasonable for now for it to
1984
# ask which fallbacks have which parents.
1986
("get_parent_map", set([key_basis, key_missing])),
1987
("get_record_stream", [key_basis], 'unordered', False)],
1990
def test_get_record_stream_ordered_deltas(self):
1991
# ordering is preserved down into the fallback store.
1992
basis, test = self.get_basis_and_test_knit()
1994
key_basis = ('bar',)
1995
key_basis_2 = ('quux',)
1996
key_missing = ('missing',)
1997
test.add_lines(key, (key_basis,), ['foo\n'])
1998
# Missing (from test knit) objects are retrieved from the basis:
1999
basis.add_lines(key_basis, (key_basis_2,), ['foo\n', 'bar\n'])
2000
basis.add_lines(key_basis_2, (), ['quux\n'])
2002
# ask for in non-topological order
2003
records = list(test.get_record_stream(
2004
[key, key_basis, key_missing, key_basis_2], 'topological', False))
2005
self.assertEqual(4, len(records))
2007
for record in records:
2008
self.assertSubset([record.key],
2009
(key_basis, key_missing, key_basis_2, key))
2010
if record.key == key_missing:
2011
self.assertIsInstance(record, AbsentContentFactory)
2013
results.append((record.key, record.sha1, record.storage_kind,
2014
record.get_bytes_as(record.storage_kind)))
2015
calls = list(basis.calls)
2016
order = [record[0] for record in results]
2017
self.assertEqual([key_basis_2, key_basis, key], order)
2018
for result in results:
2019
if result[0] == key:
2023
record = source.get_record_stream([result[0]], 'unordered',
2025
self.assertEqual(record.key, result[0])
2026
self.assertEqual(record.sha1, result[1])
2027
self.assertEqual(record.storage_kind, result[2])
2028
self.assertEqual(record.get_bytes_as(record.storage_kind), result[3])
2029
# It's not strictly minimal, but it seems reasonable for now for it to
2030
# ask which fallbacks have which parents.
2032
("get_parent_map", set([key_basis, key_basis_2, key_missing])),
2033
("get_record_stream", [key_basis_2, key_basis], 'topological', False)],
2036
def test_get_sha1s(self):
2037
# sha1's in the test knit are answered without asking the basis
2038
basis, test = self.get_basis_and_test_knit()
2040
key_basis = ('bar',)
2041
key_missing = ('missing',)
2042
test.add_lines(key, (), ['foo\n'])
2043
key_sha1sum = osutils.sha('foo\n').hexdigest()
2044
sha1s = test.get_sha1s([key])
2045
self.assertEqual({key: key_sha1sum}, sha1s)
2046
self.assertEqual([], basis.calls)
2047
# But texts that are not in the test knit are looked for in the basis
2048
# directly (rather than via text reconstruction) so that remote servers
2049
# etc don't have to answer with full content.
2050
basis.add_lines(key_basis, (), ['foo\n', 'bar\n'])
2051
basis_sha1sum = osutils.sha('foo\nbar\n').hexdigest()
2053
sha1s = test.get_sha1s([key, key_missing, key_basis])
2054
self.assertEqual({key: key_sha1sum,
2055
key_basis: basis_sha1sum}, sha1s)
2056
self.assertEqual([("get_sha1s", set([key_basis, key_missing]))],
2059
def test_insert_record_stream(self):
2060
# records are inserted as normal; insert_record_stream builds on
2061
# add_lines, so a smoke test should be all that's needed:
2063
key_basis = ('bar',)
2064
key_delta = ('zaphod',)
2065
basis, test = self.get_basis_and_test_knit()
2066
source = self.make_test_knit(name='source')
2067
basis.add_lines(key_basis, (), ['foo\n'])
2069
source.add_lines(key_basis, (), ['foo\n'])
2070
source.add_lines(key_delta, (key_basis,), ['bar\n'])
2071
stream = source.get_record_stream([key_delta], 'unordered', False)
2072
test.insert_record_stream(stream)
2073
# XXX: this does somewhat too many calls in making sure of whether it
2074
# has to recreate the full text.
2075
self.assertEqual([("get_parent_map", set([key_basis])),
2076
('get_parent_map', set([key_basis])),
2077
('get_record_stream', [key_basis], 'unordered', True)],
2079
self.assertEqual({key_delta:(key_basis,)},
2080
test.get_parent_map([key_delta]))
2081
self.assertEqual('bar\n', test.get_record_stream([key_delta],
2082
'unordered', True).next().get_bytes_as('fulltext'))
2084
def test_iter_lines_added_or_present_in_keys(self):
2085
# Lines from the basis are returned, and lines for a given key are only
2089
# all sources are asked for keys:
2090
basis, test = self.get_basis_and_test_knit()
2091
basis.add_lines(key1, (), ["foo"])
2093
lines = list(test.iter_lines_added_or_present_in_keys([key1]))
2094
self.assertEqual([("foo\n", key1)], lines)
2095
self.assertEqual([("iter_lines_added_or_present_in_keys", set([key1]))],
2097
# keys in both are not duplicated:
2098
test.add_lines(key2, (), ["bar\n"])
2099
basis.add_lines(key2, (), ["bar\n"])
2101
lines = list(test.iter_lines_added_or_present_in_keys([key2]))
2102
self.assertEqual([("bar\n", key2)], lines)
2103
self.assertEqual([], basis.calls)
2105
def test_keys(self):
2108
# all sources are asked for keys:
2109
basis, test = self.get_basis_and_test_knit()
2111
self.assertEqual(set(), set(keys))
2112
self.assertEqual([("keys",)], basis.calls)
2113
# keys from a basis are returned:
2114
basis.add_lines(key1, (), [])
2117
self.assertEqual(set([key1]), set(keys))
2118
self.assertEqual([("keys",)], basis.calls)
2119
# keys in both are not duplicated:
2120
test.add_lines(key2, (), [])
2121
basis.add_lines(key2, (), [])
2124
self.assertEqual(2, len(keys))
2125
self.assertEqual(set([key1, key2]), set(keys))
2126
self.assertEqual([("keys",)], basis.calls)
2128
def test_add_mpdiffs(self):
2129
# records are inserted as normal; add_mpdiff builds on
2130
# add_lines, so a smoke test should be all that's needed:
2132
key_basis = ('bar',)
2133
key_delta = ('zaphod',)
2134
basis, test = self.get_basis_and_test_knit()
2135
source = self.make_test_knit(name='source')
2136
basis.add_lines(key_basis, (), ['foo\n'])
2138
source.add_lines(key_basis, (), ['foo\n'])
2139
source.add_lines(key_delta, (key_basis,), ['bar\n'])
2140
diffs = source.make_mpdiffs([key_delta])
2141
test.add_mpdiffs([(key_delta, (key_basis,),
2142
source.get_sha1s([key_delta])[key_delta], diffs[0])])
2143
self.assertEqual([("get_parent_map", set([key_basis])),
2144
('get_record_stream', [key_basis], 'unordered', True),],
2146
self.assertEqual({key_delta:(key_basis,)},
2147
test.get_parent_map([key_delta]))
2148
self.assertEqual('bar\n', test.get_record_stream([key_delta],
2149
'unordered', True).next().get_bytes_as('fulltext'))
2151
def test_make_mpdiffs(self):
2152
# Generating an mpdiff across a stacking boundary should detect parent
2156
key_right = ('zaphod',)
2157
basis, test = self.get_basis_and_test_knit()
2158
basis.add_lines(key_left, (), ['bar\n'])
2159
basis.add_lines(key_right, (), ['zaphod\n'])
2161
test.add_lines(key, (key_left, key_right),
2162
['bar\n', 'foo\n', 'zaphod\n'])
2163
diffs = test.make_mpdiffs([key])
2165
multiparent.MultiParent([multiparent.ParentText(0, 0, 0, 1),
2166
multiparent.NewText(['foo\n']),
2167
multiparent.ParentText(1, 0, 2, 1)])],
2169
self.assertEqual(3, len(basis.calls))
2171
("get_parent_map", set([key_left, key_right])),
2172
("get_parent_map", set([key_left, key_right])),
2175
last_call = basis.calls[-1]
2176
self.assertEqual('get_record_stream', last_call[0])
2177
self.assertEqual(set([key_left, key_right]), set(last_call[1]))
2178
self.assertEqual('unordered', last_call[2])
2179
self.assertEqual(True, last_call[3])