1
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Tests for Knit data structure"""
19
from cStringIO import StringIO
31
from bzrlib.errors import (
32
RevisionAlreadyPresent,
37
from bzrlib.index import *
38
from bzrlib.knit import (
58
from bzrlib.osutils import split_lines
59
from bzrlib.symbol_versioning import one_four
60
from bzrlib.tests import (
63
TestCaseWithMemoryTransport,
64
TestCaseWithTransport,
66
from bzrlib.transport import get_transport
67
from bzrlib.transport.memory import MemoryTransport
68
from bzrlib.tuned_gzip import GzipFile
69
from bzrlib.util import bencode
70
from bzrlib.weave import Weave
73
class _CompiledKnitFeature(Feature):
77
import bzrlib._knit_load_data_c
82
def feature_name(self):
83
return 'bzrlib._knit_load_data_c'
85
CompiledKnitFeature = _CompiledKnitFeature()
88
class KnitContentTestsMixin(object):
90
def test_constructor(self):
91
content = self._make_content([])
94
content = self._make_content([])
95
self.assertEqual(content.text(), [])
97
content = self._make_content([("origin1", "text1"), ("origin2", "text2")])
98
self.assertEqual(content.text(), ["text1", "text2"])
101
content = self._make_content([("origin1", "text1"), ("origin2", "text2")])
102
copy = content.copy()
103
self.assertIsInstance(copy, content.__class__)
104
self.assertEqual(copy.annotate(), content.annotate())
106
def assertDerivedBlocksEqual(self, source, target, noeol=False):
107
"""Assert that the derived matching blocks match real output"""
108
source_lines = source.splitlines(True)
109
target_lines = target.splitlines(True)
111
if noeol and not line.endswith('\n'):
115
source_content = self._make_content([(None, nl(l)) for l in source_lines])
116
target_content = self._make_content([(None, nl(l)) for l in target_lines])
117
line_delta = source_content.line_delta(target_content)
118
delta_blocks = list(KnitContent.get_line_delta_blocks(line_delta,
119
source_lines, target_lines))
120
matcher = KnitSequenceMatcher(None, source_lines, target_lines)
121
matcher_blocks = list(list(matcher.get_matching_blocks()))
122
self.assertEqual(matcher_blocks, delta_blocks)
124
def test_get_line_delta_blocks(self):
125
self.assertDerivedBlocksEqual('a\nb\nc\n', 'q\nc\n')
126
self.assertDerivedBlocksEqual(TEXT_1, TEXT_1)
127
self.assertDerivedBlocksEqual(TEXT_1, TEXT_1A)
128
self.assertDerivedBlocksEqual(TEXT_1, TEXT_1B)
129
self.assertDerivedBlocksEqual(TEXT_1B, TEXT_1A)
130
self.assertDerivedBlocksEqual(TEXT_1A, TEXT_1B)
131
self.assertDerivedBlocksEqual(TEXT_1A, '')
132
self.assertDerivedBlocksEqual('', TEXT_1A)
133
self.assertDerivedBlocksEqual('', '')
134
self.assertDerivedBlocksEqual('a\nb\nc', 'a\nb\nc\nd')
136
def test_get_line_delta_blocks_noeol(self):
137
"""Handle historical knit deltas safely
139
Some existing knit deltas don't consider the last line to differ
140
when the only difference whether it has a final newline.
142
New knit deltas appear to always consider the last line to differ
145
self.assertDerivedBlocksEqual('a\nb\nc', 'a\nb\nc\nd\n', noeol=True)
146
self.assertDerivedBlocksEqual('a\nb\nc\nd\n', 'a\nb\nc', noeol=True)
147
self.assertDerivedBlocksEqual('a\nb\nc\n', 'a\nb\nc', noeol=True)
148
self.assertDerivedBlocksEqual('a\nb\nc', 'a\nb\nc\n', noeol=True)
151
class TestPlainKnitContent(TestCase, KnitContentTestsMixin):
153
def _make_content(self, lines):
154
annotated_content = AnnotatedKnitContent(lines)
155
return PlainKnitContent(annotated_content.text(), 'bogus')
157
def test_annotate(self):
158
content = self._make_content([])
159
self.assertEqual(content.annotate(), [])
161
content = self._make_content([("origin1", "text1"), ("origin2", "text2")])
162
self.assertEqual(content.annotate(),
163
[("bogus", "text1"), ("bogus", "text2")])
165
def test_line_delta(self):
166
content1 = self._make_content([("", "a"), ("", "b")])
167
content2 = self._make_content([("", "a"), ("", "a"), ("", "c")])
168
self.assertEqual(content1.line_delta(content2),
169
[(1, 2, 2, ["a", "c"])])
171
def test_line_delta_iter(self):
172
content1 = self._make_content([("", "a"), ("", "b")])
173
content2 = self._make_content([("", "a"), ("", "a"), ("", "c")])
174
it = content1.line_delta_iter(content2)
175
self.assertEqual(it.next(), (1, 2, 2, ["a", "c"]))
176
self.assertRaises(StopIteration, it.next)
179
class TestAnnotatedKnitContent(TestCase, KnitContentTestsMixin):
181
def _make_content(self, lines):
182
return AnnotatedKnitContent(lines)
184
def test_annotate(self):
185
content = self._make_content([])
186
self.assertEqual(content.annotate(), [])
188
content = self._make_content([("origin1", "text1"), ("origin2", "text2")])
189
self.assertEqual(content.annotate(),
190
[("origin1", "text1"), ("origin2", "text2")])
192
def test_line_delta(self):
193
content1 = self._make_content([("", "a"), ("", "b")])
194
content2 = self._make_content([("", "a"), ("", "a"), ("", "c")])
195
self.assertEqual(content1.line_delta(content2),
196
[(1, 2, 2, [("", "a"), ("", "c")])])
198
def test_line_delta_iter(self):
199
content1 = self._make_content([("", "a"), ("", "b")])
200
content2 = self._make_content([("", "a"), ("", "a"), ("", "c")])
201
it = content1.line_delta_iter(content2)
202
self.assertEqual(it.next(), (1, 2, 2, [("", "a"), ("", "c")]))
203
self.assertRaises(StopIteration, it.next)
206
class MockTransport(object):
208
def __init__(self, file_lines=None):
209
self.file_lines = file_lines
211
# We have no base directory for the MockTransport
214
def get(self, filename):
215
if self.file_lines is None:
216
raise NoSuchFile(filename)
218
return StringIO("\n".join(self.file_lines))
220
def readv(self, relpath, offsets):
221
fp = self.get(relpath)
222
for offset, size in offsets:
224
yield offset, fp.read(size)
226
def __getattr__(self, name):
227
def queue_call(*args, **kwargs):
228
self.calls.append((name, args, kwargs))
232
class KnitRecordAccessTestsMixin(object):
233
"""Tests for getting and putting knit records."""
235
def assertAccessExists(self, access):
236
"""Ensure the data area for access has been initialised/exists."""
237
raise NotImplementedError(self.assertAccessExists)
239
def test_add_raw_records(self):
240
"""Add_raw_records adds records retrievable later."""
241
access = self.get_access()
242
memos = access.add_raw_records([10], '1234567890')
243
self.assertEqual(['1234567890'], list(access.get_raw_records(memos)))
245
def test_add_several_raw_records(self):
246
"""add_raw_records with many records and read some back."""
247
access = self.get_access()
248
memos = access.add_raw_records([10, 2, 5], '12345678901234567')
249
self.assertEqual(['1234567890', '12', '34567'],
250
list(access.get_raw_records(memos)))
251
self.assertEqual(['1234567890'],
252
list(access.get_raw_records(memos[0:1])))
253
self.assertEqual(['12'],
254
list(access.get_raw_records(memos[1:2])))
255
self.assertEqual(['34567'],
256
list(access.get_raw_records(memos[2:3])))
257
self.assertEqual(['1234567890', '34567'],
258
list(access.get_raw_records(memos[0:1] + memos[2:3])))
260
def test_create(self):
261
"""create() should make a file on disk."""
262
access = self.get_access()
264
self.assertAccessExists(access)
266
def test_open_file(self):
267
"""open_file never errors."""
268
access = self.get_access()
272
class TestKnitKnitAccess(TestCaseWithMemoryTransport, KnitRecordAccessTestsMixin):
273
"""Tests for the .kndx implementation."""
275
def assertAccessExists(self, access):
276
self.assertNotEqual(None, access.open_file())
278
def get_access(self):
279
"""Get a .knit style access instance."""
280
access = _KnitAccess(self.get_transport(), "foo.knit", None, None,
285
class TestPackKnitAccess(TestCaseWithMemoryTransport, KnitRecordAccessTestsMixin):
286
"""Tests for the pack based access."""
288
def assertAccessExists(self, access):
289
# as pack based access has no backing unless an index maps data, this
293
def get_access(self):
294
return self._get_access()[0]
296
def _get_access(self, packname='packfile', index='FOO'):
297
transport = self.get_transport()
298
def write_data(bytes):
299
transport.append_bytes(packname, bytes)
300
writer = pack.ContainerWriter(write_data)
302
indices = {index:(transport, packname)}
303
access = _PackAccess(indices, writer=(writer, index))
304
return access, writer
306
def test_read_from_several_packs(self):
307
access, writer = self._get_access()
309
memos.extend(access.add_raw_records([10], '1234567890'))
311
access, writer = self._get_access('pack2', 'FOOBAR')
312
memos.extend(access.add_raw_records([5], '12345'))
314
access, writer = self._get_access('pack3', 'BAZ')
315
memos.extend(access.add_raw_records([5], 'alpha'))
317
transport = self.get_transport()
318
access = _PackAccess({"FOO":(transport, 'packfile'),
319
"FOOBAR":(transport, 'pack2'),
320
"BAZ":(transport, 'pack3')})
321
self.assertEqual(['1234567890', '12345', 'alpha'],
322
list(access.get_raw_records(memos)))
323
self.assertEqual(['1234567890'],
324
list(access.get_raw_records(memos[0:1])))
325
self.assertEqual(['12345'],
326
list(access.get_raw_records(memos[1:2])))
327
self.assertEqual(['alpha'],
328
list(access.get_raw_records(memos[2:3])))
329
self.assertEqual(['1234567890', 'alpha'],
330
list(access.get_raw_records(memos[0:1] + memos[2:3])))
332
def test_set_writer(self):
333
"""The writer should be settable post construction."""
334
access = _PackAccess({})
335
transport = self.get_transport()
336
packname = 'packfile'
338
def write_data(bytes):
339
transport.append_bytes(packname, bytes)
340
writer = pack.ContainerWriter(write_data)
342
access.set_writer(writer, index, (transport, packname))
343
memos = access.add_raw_records([10], '1234567890')
345
self.assertEqual(['1234567890'], list(access.get_raw_records(memos)))
348
class LowLevelKnitDataTests(TestCase):
350
def create_gz_content(self, text):
352
gz_file = gzip.GzipFile(mode='wb', fileobj=sio)
355
return sio.getvalue()
357
def test_valid_knit_data(self):
358
sha1sum = sha.new('foo\nbar\n').hexdigest()
359
gz_txt = self.create_gz_content('version rev-id-1 2 %s\n'
364
transport = MockTransport([gz_txt])
365
access = _KnitAccess(transport, 'filename', None, None, False, False)
366
data = _KnitData(access=access)
367
records = [('rev-id-1', (None, 0, len(gz_txt)))]
369
contents = data.read_records(records)
370
self.assertEqual({'rev-id-1':(['foo\n', 'bar\n'], sha1sum)}, contents)
372
raw_contents = list(data.read_records_iter_raw(records))
373
self.assertEqual([('rev-id-1', gz_txt)], raw_contents)
375
def test_not_enough_lines(self):
376
sha1sum = sha.new('foo\n').hexdigest()
377
# record says 2 lines data says 1
378
gz_txt = self.create_gz_content('version rev-id-1 2 %s\n'
382
transport = MockTransport([gz_txt])
383
access = _KnitAccess(transport, 'filename', None, None, False, False)
384
data = _KnitData(access=access)
385
records = [('rev-id-1', (None, 0, len(gz_txt)))]
386
self.assertRaises(errors.KnitCorrupt, data.read_records, records)
388
# read_records_iter_raw won't detect that sort of mismatch/corruption
389
raw_contents = list(data.read_records_iter_raw(records))
390
self.assertEqual([('rev-id-1', gz_txt)], raw_contents)
392
def test_too_many_lines(self):
393
sha1sum = sha.new('foo\nbar\n').hexdigest()
394
# record says 1 lines data says 2
395
gz_txt = self.create_gz_content('version rev-id-1 1 %s\n'
400
transport = MockTransport([gz_txt])
401
access = _KnitAccess(transport, 'filename', None, None, False, False)
402
data = _KnitData(access=access)
403
records = [('rev-id-1', (None, 0, len(gz_txt)))]
404
self.assertRaises(errors.KnitCorrupt, data.read_records, records)
406
# read_records_iter_raw won't detect that sort of mismatch/corruption
407
raw_contents = list(data.read_records_iter_raw(records))
408
self.assertEqual([('rev-id-1', gz_txt)], raw_contents)
410
def test_mismatched_version_id(self):
411
sha1sum = sha.new('foo\nbar\n').hexdigest()
412
gz_txt = self.create_gz_content('version rev-id-1 2 %s\n'
417
transport = MockTransport([gz_txt])
418
access = _KnitAccess(transport, 'filename', None, None, False, False)
419
data = _KnitData(access=access)
420
# We are asking for rev-id-2, but the data is rev-id-1
421
records = [('rev-id-2', (None, 0, len(gz_txt)))]
422
self.assertRaises(errors.KnitCorrupt, data.read_records, records)
424
# read_records_iter_raw will notice if we request the wrong version.
425
self.assertRaises(errors.KnitCorrupt, list,
426
data.read_records_iter_raw(records))
428
def test_uncompressed_data(self):
429
sha1sum = sha.new('foo\nbar\n').hexdigest()
430
txt = ('version rev-id-1 2 %s\n'
435
transport = MockTransport([txt])
436
access = _KnitAccess(transport, 'filename', None, None, False, False)
437
data = _KnitData(access=access)
438
records = [('rev-id-1', (None, 0, len(txt)))]
440
# We don't have valid gzip data ==> corrupt
441
self.assertRaises(errors.KnitCorrupt, data.read_records, records)
443
# read_records_iter_raw will notice the bad data
444
self.assertRaises(errors.KnitCorrupt, list,
445
data.read_records_iter_raw(records))
447
def test_corrupted_data(self):
448
sha1sum = sha.new('foo\nbar\n').hexdigest()
449
gz_txt = self.create_gz_content('version rev-id-1 2 %s\n'
454
# Change 2 bytes in the middle to \xff
455
gz_txt = gz_txt[:10] + '\xff\xff' + gz_txt[12:]
456
transport = MockTransport([gz_txt])
457
access = _KnitAccess(transport, 'filename', None, None, False, False)
458
data = _KnitData(access=access)
459
records = [('rev-id-1', (None, 0, len(gz_txt)))]
461
self.assertRaises(errors.KnitCorrupt, data.read_records, records)
463
# read_records_iter_raw will notice if we request the wrong version.
464
self.assertRaises(errors.KnitCorrupt, list,
465
data.read_records_iter_raw(records))
468
class LowLevelKnitIndexTests(TestCase):
470
def get_knit_index(self, *args, **kwargs):
471
orig = knit._load_data
473
knit._load_data = orig
474
self.addCleanup(reset)
475
from bzrlib._knit_load_data_py import _load_data_py
476
knit._load_data = _load_data_py
477
return _KnitIndex(get_scope=lambda:None, *args, **kwargs)
479
def test_no_such_file(self):
480
transport = MockTransport()
482
self.assertRaises(NoSuchFile, self.get_knit_index,
483
transport, "filename", "r")
484
self.assertRaises(NoSuchFile, self.get_knit_index,
485
transport, "filename", "w", create=False)
487
def test_create_file(self):
488
transport = MockTransport()
490
index = self.get_knit_index(transport, "filename", "w",
491
file_mode="wb", create=True)
493
("put_bytes_non_atomic",
494
("filename", index.HEADER), {"mode": "wb"}),
495
transport.calls.pop(0))
497
def test_delay_create_file(self):
498
transport = MockTransport()
500
index = self.get_knit_index(transport, "filename", "w",
501
create=True, file_mode="wb", create_parent_dir=True,
502
delay_create=True, dir_mode=0777)
503
self.assertEqual([], transport.calls)
505
index.add_versions([])
506
name, (filename, f), kwargs = transport.calls.pop(0)
507
self.assertEqual("put_file_non_atomic", name)
509
{"dir_mode": 0777, "create_parent_dir": True, "mode": "wb"},
511
self.assertEqual("filename", filename)
512
self.assertEqual(index.HEADER, f.read())
514
index.add_versions([])
515
self.assertEqual(("append_bytes", ("filename", ""), {}),
516
transport.calls.pop(0))
518
def test_read_utf8_version_id(self):
519
unicode_revision_id = u"version-\N{CYRILLIC CAPITAL LETTER A}"
520
utf8_revision_id = unicode_revision_id.encode('utf-8')
521
transport = MockTransport([
523
'%s option 0 1 :' % (utf8_revision_id,)
525
index = self.get_knit_index(transport, "filename", "r")
526
# _KnitIndex is a private class, and deals in utf8 revision_ids, not
527
# Unicode revision_ids.
528
self.assertTrue(index.has_version(utf8_revision_id))
529
self.assertFalse(index.has_version(unicode_revision_id))
531
def test_read_utf8_parents(self):
532
unicode_revision_id = u"version-\N{CYRILLIC CAPITAL LETTER A}"
533
utf8_revision_id = unicode_revision_id.encode('utf-8')
534
transport = MockTransport([
536
"version option 0 1 .%s :" % (utf8_revision_id,)
538
index = self.get_knit_index(transport, "filename", "r")
539
self.assertEqual((utf8_revision_id,),
540
index.get_parents_with_ghosts("version"))
542
def test_read_ignore_corrupted_lines(self):
543
transport = MockTransport([
546
"corrupted options 0 1 .b .c ",
547
"version options 0 1 :"
549
index = self.get_knit_index(transport, "filename", "r")
550
self.assertEqual(1, index.num_versions())
551
self.assertTrue(index.has_version("version"))
553
def test_read_corrupted_header(self):
554
transport = MockTransport(['not a bzr knit index header\n'])
555
self.assertRaises(KnitHeaderError,
556
self.get_knit_index, transport, "filename", "r")
558
def test_read_duplicate_entries(self):
559
transport = MockTransport([
561
"parent options 0 1 :",
562
"version options1 0 1 0 :",
563
"version options2 1 2 .other :",
564
"version options3 3 4 0 .other :"
566
index = self.get_knit_index(transport, "filename", "r")
567
self.assertEqual(2, index.num_versions())
568
# check that the index used is the first one written. (Specific
569
# to KnitIndex style indices.
570
self.assertEqual("1", index._version_list_to_index(["version"]))
571
self.assertEqual((None, 3, 4), index.get_position("version"))
572
self.assertEqual(["options3"], index.get_options("version"))
573
self.assertEqual(("parent", "other"),
574
index.get_parents_with_ghosts("version"))
576
def test_read_compressed_parents(self):
577
transport = MockTransport([
581
"c option 0 1 1 0 :",
583
index = self.get_knit_index(transport, "filename", "r")
584
self.assertEqual({"b":("a",), "c":("b", "a")},
585
index.get_parent_map(["b", "c"]))
587
def test_write_utf8_version_id(self):
588
unicode_revision_id = u"version-\N{CYRILLIC CAPITAL LETTER A}"
589
utf8_revision_id = unicode_revision_id.encode('utf-8')
590
transport = MockTransport([
593
index = self.get_knit_index(transport, "filename", "r")
594
index.add_version(utf8_revision_id, ["option"], (None, 0, 1), [])
595
self.assertEqual(("append_bytes", ("filename",
596
"\n%s option 0 1 :" % (utf8_revision_id,)),
598
transport.calls.pop(0))
600
def test_write_utf8_parents(self):
601
unicode_revision_id = u"version-\N{CYRILLIC CAPITAL LETTER A}"
602
utf8_revision_id = unicode_revision_id.encode('utf-8')
603
transport = MockTransport([
606
index = self.get_knit_index(transport, "filename", "r")
607
index.add_version("version", ["option"], (None, 0, 1), [utf8_revision_id])
608
self.assertEqual(("append_bytes", ("filename",
609
"\nversion option 0 1 .%s :" % (utf8_revision_id,)),
611
transport.calls.pop(0))
613
def test_get_ancestry(self):
614
transport = MockTransport([
617
"b option 0 1 0 .e :",
618
"c option 0 1 1 0 :",
619
"d option 0 1 2 .f :"
621
index = self.get_knit_index(transport, "filename", "r")
623
self.assertEqual([], index.get_ancestry([]))
624
self.assertEqual(["a"], index.get_ancestry(["a"]))
625
self.assertEqual(["a", "b"], index.get_ancestry(["b"]))
626
self.assertEqual(["a", "b", "c"], index.get_ancestry(["c"]))
627
self.assertEqual(["a", "b", "c", "d"], index.get_ancestry(["d"]))
628
self.assertEqual(["a", "b"], index.get_ancestry(["a", "b"]))
629
self.assertEqual(["a", "b", "c"], index.get_ancestry(["a", "c"]))
631
self.assertRaises(RevisionNotPresent, index.get_ancestry, ["e"])
633
def test_get_ancestry_with_ghosts(self):
634
transport = MockTransport([
637
"b option 0 1 0 .e :",
638
"c option 0 1 0 .f .g :",
639
"d option 0 1 2 .h .j .k :"
641
index = self.get_knit_index(transport, "filename", "r")
643
self.assertEqual([], index.get_ancestry_with_ghosts([]))
644
self.assertEqual(["a"], index.get_ancestry_with_ghosts(["a"]))
645
self.assertEqual(["a", "e", "b"],
646
index.get_ancestry_with_ghosts(["b"]))
647
self.assertEqual(["a", "g", "f", "c"],
648
index.get_ancestry_with_ghosts(["c"]))
649
self.assertEqual(["a", "g", "f", "c", "k", "j", "h", "d"],
650
index.get_ancestry_with_ghosts(["d"]))
651
self.assertEqual(["a", "e", "b"],
652
index.get_ancestry_with_ghosts(["a", "b"]))
653
self.assertEqual(["a", "g", "f", "c"],
654
index.get_ancestry_with_ghosts(["a", "c"]))
656
["a", "g", "f", "c", "e", "b", "k", "j", "h", "d"],
657
index.get_ancestry_with_ghosts(["b", "d"]))
659
self.assertRaises(RevisionNotPresent,
660
index.get_ancestry_with_ghosts, ["e"])
662
def test_num_versions(self):
663
transport = MockTransport([
666
index = self.get_knit_index(transport, "filename", "r")
668
self.assertEqual(0, index.num_versions())
669
self.assertEqual(0, len(index))
671
index.add_version("a", ["option"], (None, 0, 1), [])
672
self.assertEqual(1, index.num_versions())
673
self.assertEqual(1, len(index))
675
index.add_version("a", ["option2"], (None, 1, 2), [])
676
self.assertEqual(1, index.num_versions())
677
self.assertEqual(1, len(index))
679
index.add_version("b", ["option"], (None, 0, 1), [])
680
self.assertEqual(2, index.num_versions())
681
self.assertEqual(2, len(index))
683
def test_get_versions(self):
684
transport = MockTransport([
687
index = self.get_knit_index(transport, "filename", "r")
689
self.assertEqual([], index.get_versions())
691
index.add_version("a", ["option"], (None, 0, 1), [])
692
self.assertEqual(["a"], index.get_versions())
694
index.add_version("a", ["option"], (None, 0, 1), [])
695
self.assertEqual(["a"], index.get_versions())
697
index.add_version("b", ["option"], (None, 0, 1), [])
698
self.assertEqual(["a", "b"], index.get_versions())
700
def test_add_version(self):
701
transport = MockTransport([
704
index = self.get_knit_index(transport, "filename", "r")
706
index.add_version("a", ["option"], (None, 0, 1), ["b"])
707
self.assertEqual(("append_bytes",
708
("filename", "\na option 0 1 .b :"),
709
{}), transport.calls.pop(0))
710
self.assertTrue(index.has_version("a"))
711
self.assertEqual(1, index.num_versions())
712
self.assertEqual((None, 0, 1), index.get_position("a"))
713
self.assertEqual(["option"], index.get_options("a"))
714
self.assertEqual(("b",), index.get_parents_with_ghosts("a"))
716
index.add_version("a", ["opt"], (None, 1, 2), ["c"])
717
self.assertEqual(("append_bytes",
718
("filename", "\na opt 1 2 .c :"),
719
{}), transport.calls.pop(0))
720
self.assertTrue(index.has_version("a"))
721
self.assertEqual(1, index.num_versions())
722
self.assertEqual((None, 1, 2), index.get_position("a"))
723
self.assertEqual(["opt"], index.get_options("a"))
724
self.assertEqual(("c",), index.get_parents_with_ghosts("a"))
726
index.add_version("b", ["option"], (None, 2, 3), ["a"])
727
self.assertEqual(("append_bytes",
728
("filename", "\nb option 2 3 0 :"),
729
{}), transport.calls.pop(0))
730
self.assertTrue(index.has_version("b"))
731
self.assertEqual(2, index.num_versions())
732
self.assertEqual((None, 2, 3), index.get_position("b"))
733
self.assertEqual(["option"], index.get_options("b"))
734
self.assertEqual(("a",), index.get_parents_with_ghosts("b"))
736
def test_add_versions(self):
737
transport = MockTransport([
740
index = self.get_knit_index(transport, "filename", "r")
743
("a", ["option"], (None, 0, 1), ["b"]),
744
("a", ["opt"], (None, 1, 2), ["c"]),
745
("b", ["option"], (None, 2, 3), ["a"])
747
self.assertEqual(("append_bytes", ("filename",
748
"\na option 0 1 .b :"
751
), {}), transport.calls.pop(0))
752
self.assertTrue(index.has_version("a"))
753
self.assertTrue(index.has_version("b"))
754
self.assertEqual(2, index.num_versions())
755
self.assertEqual((None, 1, 2), index.get_position("a"))
756
self.assertEqual((None, 2, 3), index.get_position("b"))
757
self.assertEqual(["opt"], index.get_options("a"))
758
self.assertEqual(["option"], index.get_options("b"))
759
self.assertEqual(("c",), index.get_parents_with_ghosts("a"))
760
self.assertEqual(("a",), index.get_parents_with_ghosts("b"))
762
def test_add_versions_random_id_is_accepted(self):
763
transport = MockTransport([
766
index = self.get_knit_index(transport, "filename", "r")
769
("a", ["option"], (None, 0, 1), ["b"]),
770
("a", ["opt"], (None, 1, 2), ["c"]),
771
("b", ["option"], (None, 2, 3), ["a"])
774
def test_delay_create_and_add_versions(self):
775
transport = MockTransport()
777
index = self.get_knit_index(transport, "filename", "w",
778
create=True, file_mode="wb", create_parent_dir=True,
779
delay_create=True, dir_mode=0777)
780
self.assertEqual([], transport.calls)
783
("a", ["option"], (None, 0, 1), ["b"]),
784
("a", ["opt"], (None, 1, 2), ["c"]),
785
("b", ["option"], (None, 2, 3), ["a"])
787
name, (filename, f), kwargs = transport.calls.pop(0)
788
self.assertEqual("put_file_non_atomic", name)
790
{"dir_mode": 0777, "create_parent_dir": True, "mode": "wb"},
792
self.assertEqual("filename", filename)
795
"\na option 0 1 .b :"
797
"\nb option 2 3 0 :",
800
def test_has_version(self):
801
transport = MockTransport([
805
index = self.get_knit_index(transport, "filename", "r")
807
self.assertTrue(index.has_version("a"))
808
self.assertFalse(index.has_version("b"))
810
def test_get_position(self):
811
transport = MockTransport([
816
index = self.get_knit_index(transport, "filename", "r")
818
self.assertEqual((None, 0, 1), index.get_position("a"))
819
self.assertEqual((None, 1, 2), index.get_position("b"))
821
def test_get_method(self):
822
transport = MockTransport([
824
"a fulltext,unknown 0 1 :",
825
"b unknown,line-delta 1 2 :",
828
index = self.get_knit_index(transport, "filename", "r")
830
self.assertEqual("fulltext", index.get_method("a"))
831
self.assertEqual("line-delta", index.get_method("b"))
832
self.assertRaises(errors.KnitIndexUnknownMethod, index.get_method, "c")
834
def test_get_options(self):
835
transport = MockTransport([
840
index = self.get_knit_index(transport, "filename", "r")
842
self.assertEqual(["opt1"], index.get_options("a"))
843
self.assertEqual(["opt2", "opt3"], index.get_options("b"))
845
def test_get_parent_map(self):
846
transport = MockTransport([
849
"b option 1 2 0 .c :",
850
"c option 1 2 1 0 .e :"
852
index = self.get_knit_index(transport, "filename", "r")
858
}, index.get_parent_map(["a", "b", "c"]))
860
def test_get_parents_with_ghosts(self):
861
transport = MockTransport([
864
"b option 1 2 0 .c :",
865
"c option 1 2 1 0 .e :"
867
index = self.get_knit_index(transport, "filename", "r")
869
self.assertEqual((), index.get_parents_with_ghosts("a"))
870
self.assertEqual(("a", "c"), index.get_parents_with_ghosts("b"))
871
self.assertEqual(("b", "a", "e"),
872
index.get_parents_with_ghosts("c"))
874
def test_check_versions_present(self):
875
transport = MockTransport([
880
index = self.get_knit_index(transport, "filename", "r")
882
check = index.check_versions_present
888
self.assertRaises(RevisionNotPresent, check, ["c"])
889
self.assertRaises(RevisionNotPresent, check, ["a", "b", "c"])
891
def test_impossible_parent(self):
892
"""Test we get KnitCorrupt if the parent couldn't possibly exist."""
893
transport = MockTransport([
896
"b option 0 1 4 :" # We don't have a 4th record
899
self.assertRaises(errors.KnitCorrupt,
900
self.get_knit_index, transport, 'filename', 'r')
902
if (str(e) == ('exceptions must be strings, classes, or instances,'
903
' not exceptions.IndexError')
904
and sys.version_info[0:2] >= (2,5)):
905
self.knownFailure('Pyrex <0.9.5 fails with TypeError when'
906
' raising new style exceptions with python'
911
def test_corrupted_parent(self):
912
transport = MockTransport([
916
"c option 0 1 1v :", # Can't have a parent of '1v'
919
self.assertRaises(errors.KnitCorrupt,
920
self.get_knit_index, transport, 'filename', 'r')
922
if (str(e) == ('exceptions must be strings, classes, or instances,'
923
' not exceptions.ValueError')
924
and sys.version_info[0:2] >= (2,5)):
925
self.knownFailure('Pyrex <0.9.5 fails with TypeError when'
926
' raising new style exceptions with python'
931
def test_corrupted_parent_in_list(self):
932
transport = MockTransport([
936
"c option 0 1 1 v :", # Can't have a parent of 'v'
939
self.assertRaises(errors.KnitCorrupt,
940
self.get_knit_index, transport, 'filename', 'r')
942
if (str(e) == ('exceptions must be strings, classes, or instances,'
943
' not exceptions.ValueError')
944
and sys.version_info[0:2] >= (2,5)):
945
self.knownFailure('Pyrex <0.9.5 fails with TypeError when'
946
' raising new style exceptions with python'
951
def test_invalid_position(self):
952
transport = MockTransport([
957
self.assertRaises(errors.KnitCorrupt,
958
self.get_knit_index, transport, 'filename', 'r')
960
if (str(e) == ('exceptions must be strings, classes, or instances,'
961
' not exceptions.ValueError')
962
and sys.version_info[0:2] >= (2,5)):
963
self.knownFailure('Pyrex <0.9.5 fails with TypeError when'
964
' raising new style exceptions with python'
969
def test_invalid_size(self):
970
transport = MockTransport([
975
self.assertRaises(errors.KnitCorrupt,
976
self.get_knit_index, transport, 'filename', 'r')
978
if (str(e) == ('exceptions must be strings, classes, or instances,'
979
' not exceptions.ValueError')
980
and sys.version_info[0:2] >= (2,5)):
981
self.knownFailure('Pyrex <0.9.5 fails with TypeError when'
982
' raising new style exceptions with python'
987
def test_short_line(self):
988
transport = MockTransport([
991
"b option 10 10 0", # This line isn't terminated, ignored
993
index = self.get_knit_index(transport, "filename", "r")
994
self.assertEqual(['a'], index.get_versions())
996
def test_skip_incomplete_record(self):
997
# A line with bogus data should just be skipped
998
transport = MockTransport([
1001
"b option 10 10 0", # This line isn't terminated, ignored
1002
"c option 20 10 0 :", # Properly terminated, and starts with '\n'
1004
index = self.get_knit_index(transport, "filename", "r")
1005
self.assertEqual(['a', 'c'], index.get_versions())
1007
def test_trailing_characters(self):
1008
# A line with bogus data should just be skipped
1009
transport = MockTransport([
1012
"b option 10 10 0 :a", # This line has extra trailing characters
1013
"c option 20 10 0 :", # Properly terminated, and starts with '\n'
1015
index = self.get_knit_index(transport, "filename", "r")
1016
self.assertEqual(['a', 'c'], index.get_versions())
1019
class LowLevelKnitIndexTests_c(LowLevelKnitIndexTests):
1021
_test_needs_features = [CompiledKnitFeature]
1023
def get_knit_index(self, *args, **kwargs):
1024
orig = knit._load_data
1026
knit._load_data = orig
1027
self.addCleanup(reset)
1028
from bzrlib._knit_load_data_c import _load_data_c
1029
knit._load_data = _load_data_c
1030
return _KnitIndex(get_scope=lambda:None, *args, **kwargs)
1033
class KnitTests(TestCaseWithTransport):
1034
"""Class containing knit test helper routines."""
1036
def make_test_knit(self, annotate=False, delay_create=False, index=None,
1037
name='test', delta=True, access_mode='w'):
1039
factory = KnitPlainFactory()
1043
index = _KnitIndex(get_transport('.'), name + INDEX_SUFFIX,
1044
access_mode, create=True, file_mode=None,
1045
create_parent_dir=False, delay_create=delay_create,
1046
dir_mode=None, get_scope=lambda:None)
1047
access = _KnitAccess(get_transport('.'), name + DATA_SUFFIX, None,
1048
None, delay_create, False)
1049
return KnitVersionedFile(name, get_transport('.'), factory=factory,
1050
create=True, delay_create=delay_create, index=index,
1051
access_method=access, delta=delta)
1053
def assertRecordContentEqual(self, knit, version_id, candidate_content):
1054
"""Assert that some raw record content matches the raw record content
1055
for a particular version_id in the given knit.
1057
index_memo = knit._index.get_position(version_id)
1058
record = (version_id, index_memo)
1059
[(_, expected_content)] = list(knit._data.read_records_iter_raw([record]))
1060
self.assertEqual(expected_content, candidate_content)
1063
class BasicKnitTests(KnitTests):
1065
def add_stock_one_and_one_a(self, k):
1066
k.add_lines('text-1', [], split_lines(TEXT_1))
1067
k.add_lines('text-1a', ['text-1'], split_lines(TEXT_1A))
1069
def test_knit_constructor(self):
1070
"""Construct empty k"""
1071
self.make_test_knit()
1073
def test_make_explicit_index(self):
1074
"""We can supply an index to use."""
1075
knit = KnitVersionedFile('test', get_transport('.'),
1076
index='strangelove', access_method="a")
1077
self.assertEqual(knit._index, 'strangelove')
1079
def test_knit_add(self):
1080
"""Store one text in knit and retrieve"""
1081
k = self.make_test_knit()
1082
k.add_lines('text-1', [], split_lines(TEXT_1))
1083
self.assertTrue(k.has_version('text-1'))
1084
self.assertEqualDiff(''.join(k.get_lines('text-1')), TEXT_1)
1086
def test_newline_empty_lines(self):
1087
# ensure that ["\n"] round trips ok.
1088
knit = self.make_test_knit()
1089
knit.add_lines('a', [], ["\n"])
1090
knit.add_lines_with_ghosts('b', [], ["\n"])
1091
self.assertEqual(["\n"], knit.get_lines('a'))
1092
self.assertEqual(["\n"], knit.get_lines('b'))
1093
self.assertEqual(['fulltext'], knit._index.get_options('a'))
1094
self.assertEqual(['fulltext'], knit._index.get_options('b'))
1095
knit.add_lines('c', ['a'], ["\n"])
1096
knit.add_lines_with_ghosts('d', ['b'], ["\n"])
1097
self.assertEqual(["\n"], knit.get_lines('c'))
1098
self.assertEqual(["\n"], knit.get_lines('d'))
1099
self.assertEqual(['line-delta'], knit._index.get_options('c'))
1100
self.assertEqual(['line-delta'], knit._index.get_options('d'))
1102
def test_empty_lines(self):
1103
# bizarrely, [] is not listed as having no-eol.
1104
knit = self.make_test_knit()
1105
knit.add_lines('a', [], [])
1106
knit.add_lines_with_ghosts('b', [], [])
1107
self.assertEqual([], knit.get_lines('a'))
1108
self.assertEqual([], knit.get_lines('b'))
1109
self.assertEqual(['fulltext'], knit._index.get_options('a'))
1110
self.assertEqual(['fulltext'], knit._index.get_options('b'))
1111
knit.add_lines('c', ['a'], [])
1112
knit.add_lines_with_ghosts('d', ['b'], [])
1113
self.assertEqual([], knit.get_lines('c'))
1114
self.assertEqual([], knit.get_lines('d'))
1115
self.assertEqual(['line-delta'], knit._index.get_options('c'))
1116
self.assertEqual(['line-delta'], knit._index.get_options('d'))
1118
def test_knit_reload(self):
1119
# test that the content in a reloaded knit is correct
1120
k = self.make_test_knit()
1121
k.add_lines('text-1', [], split_lines(TEXT_1))
1123
k2 = make_file_knit('test', get_transport('.'), access_mode='r',
1124
factory=KnitPlainFactory(), create=True)
1125
self.assertTrue(k2.has_version('text-1'))
1126
self.assertEqualDiff(''.join(k2.get_lines('text-1')), TEXT_1)
1128
def test_knit_several(self):
1129
"""Store several texts in a knit"""
1130
k = self.make_test_knit()
1131
k.add_lines('text-1', [], split_lines(TEXT_1))
1132
k.add_lines('text-2', [], split_lines(TEXT_2))
1133
self.assertEqualDiff(''.join(k.get_lines('text-1')), TEXT_1)
1134
self.assertEqualDiff(''.join(k.get_lines('text-2')), TEXT_2)
1136
def test_repeated_add(self):
1137
"""Knit traps attempt to replace existing version"""
1138
k = self.make_test_knit()
1139
k.add_lines('text-1', [], split_lines(TEXT_1))
1140
self.assertRaises(RevisionAlreadyPresent,
1142
'text-1', [], split_lines(TEXT_1))
1144
def test_empty(self):
1145
k = self.make_test_knit(True)
1146
k.add_lines('text-1', [], [])
1147
self.assertEquals(k.get_lines('text-1'), [])
1149
def test_incomplete(self):
1150
"""Test if texts without a ending line-end can be inserted and
1152
k = make_file_knit('test', get_transport('.'), delta=False, create=True)
1153
k.add_lines('text-1', [], ['a\n', 'b' ])
1154
k.add_lines('text-2', ['text-1'], ['a\rb\n', 'b\n'])
1155
# reopening ensures maximum room for confusion
1156
k = make_file_knit('test', get_transport('.'), delta=False, create=True)
1157
self.assertEquals(k.get_lines('text-1'), ['a\n', 'b' ])
1158
self.assertEquals(k.get_lines('text-2'), ['a\rb\n', 'b\n'])
1160
def test_delta(self):
1161
"""Expression of knit delta as lines"""
1162
k = self.make_test_knit()
1163
td = list(line_delta(TEXT_1.splitlines(True),
1164
TEXT_1A.splitlines(True)))
1165
self.assertEqualDiff(''.join(td), delta_1_1a)
1166
out = apply_line_delta(TEXT_1.splitlines(True), td)
1167
self.assertEqualDiff(''.join(out), TEXT_1A)
1169
def test_add_with_parents(self):
1170
"""Store in knit with parents"""
1171
k = self.make_test_knit()
1172
self.add_stock_one_and_one_a(k)
1173
self.assertEqual({'text-1':(), 'text-1a':('text-1',)},
1174
k.get_parent_map(['text-1', 'text-1a']))
1176
def test_ancestry(self):
1177
"""Store in knit with parents"""
1178
k = self.make_test_knit()
1179
self.add_stock_one_and_one_a(k)
1180
self.assertEquals(set(k.get_ancestry(['text-1a'])), set(['text-1a', 'text-1']))
1182
def test_add_delta(self):
1183
"""Store in knit with parents"""
1184
k = self.make_test_knit(annotate=False)
1185
self.add_stock_one_and_one_a(k)
1186
self.assertEqualDiff(''.join(k.get_lines('text-1a')), TEXT_1A)
1188
def test_add_delta_knit_graph_index(self):
1189
"""Does adding work with a KnitGraphIndex."""
1190
index = InMemoryGraphIndex(2)
1191
knit_index = KnitGraphIndex(index, add_callback=index.add_nodes,
1193
k = self.make_test_knit(annotate=True, index=knit_index)
1194
self.add_stock_one_and_one_a(k)
1195
self.assertEqualDiff(''.join(k.get_lines('text-1a')), TEXT_1A)
1196
# check the index had the right data added.
1197
self.assertEqual(set([
1198
(index, ('text-1', ), ' 0 127', ((), ())),
1199
(index, ('text-1a', ), ' 127 140', ((('text-1', ),), (('text-1', ),))),
1200
]), set(index.iter_all_entries()))
1201
# we should not have a .kndx file
1202
self.assertFalse(get_transport('.').has('test.kndx'))
1204
def test_annotate(self):
1206
k = self.make_test_knit(annotate=True, name='knit')
1207
self.insert_and_test_small_annotate(k)
1209
def insert_and_test_small_annotate(self, k):
1210
"""test annotation with k works correctly."""
1211
k.add_lines('text-1', [], ['a\n', 'b\n'])
1212
k.add_lines('text-2', ['text-1'], ['a\n', 'c\n'])
1214
origins = k.annotate('text-2')
1215
self.assertEquals(origins[0], ('text-1', 'a\n'))
1216
self.assertEquals(origins[1], ('text-2', 'c\n'))
1218
def test_annotate_fulltext(self):
1220
k = self.make_test_knit(annotate=True, name='knit', delta=False)
1221
self.insert_and_test_small_annotate(k)
1223
def test_annotate_merge_1(self):
1224
k = self.make_test_knit(True)
1225
k.add_lines('text-a1', [], ['a\n', 'b\n'])
1226
k.add_lines('text-a2', [], ['d\n', 'c\n'])
1227
k.add_lines('text-am', ['text-a1', 'text-a2'], ['d\n', 'b\n'])
1228
origins = k.annotate('text-am')
1229
self.assertEquals(origins[0], ('text-a2', 'd\n'))
1230
self.assertEquals(origins[1], ('text-a1', 'b\n'))
1232
def test_annotate_merge_2(self):
1233
k = self.make_test_knit(True)
1234
k.add_lines('text-a1', [], ['a\n', 'b\n', 'c\n'])
1235
k.add_lines('text-a2', [], ['x\n', 'y\n', 'z\n'])
1236
k.add_lines('text-am', ['text-a1', 'text-a2'], ['a\n', 'y\n', 'c\n'])
1237
origins = k.annotate('text-am')
1238
self.assertEquals(origins[0], ('text-a1', 'a\n'))
1239
self.assertEquals(origins[1], ('text-a2', 'y\n'))
1240
self.assertEquals(origins[2], ('text-a1', 'c\n'))
1242
def test_annotate_merge_9(self):
1243
k = self.make_test_knit(True)
1244
k.add_lines('text-a1', [], ['a\n', 'b\n', 'c\n'])
1245
k.add_lines('text-a2', [], ['x\n', 'y\n', 'z\n'])
1246
k.add_lines('text-am', ['text-a1', 'text-a2'], ['k\n', 'y\n', 'c\n'])
1247
origins = k.annotate('text-am')
1248
self.assertEquals(origins[0], ('text-am', 'k\n'))
1249
self.assertEquals(origins[1], ('text-a2', 'y\n'))
1250
self.assertEquals(origins[2], ('text-a1', 'c\n'))
1252
def test_annotate_merge_3(self):
1253
k = self.make_test_knit(True)
1254
k.add_lines('text-a1', [], ['a\n', 'b\n', 'c\n'])
1255
k.add_lines('text-a2', [] ,['x\n', 'y\n', 'z\n'])
1256
k.add_lines('text-am', ['text-a1', 'text-a2'], ['k\n', 'y\n', 'z\n'])
1257
origins = k.annotate('text-am')
1258
self.assertEquals(origins[0], ('text-am', 'k\n'))
1259
self.assertEquals(origins[1], ('text-a2', 'y\n'))
1260
self.assertEquals(origins[2], ('text-a2', 'z\n'))
1262
def test_annotate_merge_4(self):
1263
k = self.make_test_knit(True)
1264
k.add_lines('text-a1', [], ['a\n', 'b\n', 'c\n'])
1265
k.add_lines('text-a2', [], ['x\n', 'y\n', 'z\n'])
1266
k.add_lines('text-a3', ['text-a1'], ['a\n', 'b\n', 'p\n'])
1267
k.add_lines('text-am', ['text-a2', 'text-a3'], ['a\n', 'b\n', 'z\n'])
1268
origins = k.annotate('text-am')
1269
self.assertEquals(origins[0], ('text-a1', 'a\n'))
1270
self.assertEquals(origins[1], ('text-a1', 'b\n'))
1271
self.assertEquals(origins[2], ('text-a2', 'z\n'))
1273
def test_annotate_merge_5(self):
1274
k = self.make_test_knit(True)
1275
k.add_lines('text-a1', [], ['a\n', 'b\n', 'c\n'])
1276
k.add_lines('text-a2', [], ['d\n', 'e\n', 'f\n'])
1277
k.add_lines('text-a3', [], ['x\n', 'y\n', 'z\n'])
1278
k.add_lines('text-am',
1279
['text-a1', 'text-a2', 'text-a3'],
1280
['a\n', 'e\n', 'z\n'])
1281
origins = k.annotate('text-am')
1282
self.assertEquals(origins[0], ('text-a1', 'a\n'))
1283
self.assertEquals(origins[1], ('text-a2', 'e\n'))
1284
self.assertEquals(origins[2], ('text-a3', 'z\n'))
1286
def test_annotate_file_cherry_pick(self):
1287
k = self.make_test_knit(True)
1288
k.add_lines('text-1', [], ['a\n', 'b\n', 'c\n'])
1289
k.add_lines('text-2', ['text-1'], ['d\n', 'e\n', 'f\n'])
1290
k.add_lines('text-3', ['text-2', 'text-1'], ['a\n', 'b\n', 'c\n'])
1291
origins = k.annotate('text-3')
1292
self.assertEquals(origins[0], ('text-1', 'a\n'))
1293
self.assertEquals(origins[1], ('text-1', 'b\n'))
1294
self.assertEquals(origins[2], ('text-1', 'c\n'))
1296
def _test_join_with_factories(self, k1_factory, k2_factory):
1297
k1 = make_file_knit('test1', get_transport('.'), factory=k1_factory, create=True)
1298
k1.add_lines('text-a', [], ['a1\n', 'a2\n', 'a3\n'])
1299
k1.add_lines('text-b', ['text-a'], ['a1\n', 'b2\n', 'a3\n'])
1300
k1.add_lines('text-c', [], ['c1\n', 'c2\n', 'c3\n'])
1301
k1.add_lines('text-d', ['text-c'], ['c1\n', 'd2\n', 'd3\n'])
1302
k1.add_lines('text-m', ['text-b', 'text-d'], ['a1\n', 'b2\n', 'd3\n'])
1303
k2 = make_file_knit('test2', get_transport('.'), factory=k2_factory, create=True)
1304
count = k2.join(k1, version_ids=['text-m'])
1305
self.assertEquals(count, 5)
1306
self.assertTrue(k2.has_version('text-a'))
1307
self.assertTrue(k2.has_version('text-c'))
1308
origins = k2.annotate('text-m')
1309
self.assertEquals(origins[0], ('text-a', 'a1\n'))
1310
self.assertEquals(origins[1], ('text-b', 'b2\n'))
1311
self.assertEquals(origins[2], ('text-d', 'd3\n'))
1313
def test_knit_join_plain_to_plain(self):
1314
"""Test joining a plain knit with a plain knit."""
1315
self._test_join_with_factories(KnitPlainFactory(), KnitPlainFactory())
1317
def test_knit_join_anno_to_anno(self):
1318
"""Test joining an annotated knit with an annotated knit."""
1319
self._test_join_with_factories(None, None)
1321
def test_knit_join_anno_to_plain(self):
1322
"""Test joining an annotated knit with a plain knit."""
1323
self._test_join_with_factories(None, KnitPlainFactory())
1325
def test_knit_join_plain_to_anno(self):
1326
"""Test joining a plain knit with an annotated knit."""
1327
self._test_join_with_factories(KnitPlainFactory(), None)
1329
def test_reannotate(self):
1330
k1 = make_file_knit('knit1', get_transport('.'),
1331
factory=KnitAnnotateFactory(), create=True)
1333
k1.add_lines('text-a', [], ['a\n', 'b\n'])
1335
k1.add_lines('text-b', ['text-a'], ['a\n', 'c\n'])
1337
k2 = make_file_knit('test2', get_transport('.'),
1338
factory=KnitAnnotateFactory(), create=True)
1339
k2.join(k1, version_ids=['text-b'])
1342
k1.add_lines('text-X', ['text-b'], ['a\n', 'b\n'])
1344
k2.add_lines('text-c', ['text-b'], ['z\n', 'c\n'])
1346
k2.add_lines('text-Y', ['text-b'], ['b\n', 'c\n'])
1348
# test-c will have index 3
1349
k1.join(k2, version_ids=['text-c'])
1351
lines = k1.get_lines('text-c')
1352
self.assertEquals(lines, ['z\n', 'c\n'])
1354
origins = k1.annotate('text-c')
1355
self.assertEquals(origins[0], ('text-c', 'z\n'))
1356
self.assertEquals(origins[1], ('text-b', 'c\n'))
1358
def test_get_line_delta_texts(self):
1359
"""Make sure we can call get_texts on text with reused line deltas"""
1360
k1 = make_file_knit('test1', get_transport('.'),
1361
factory=KnitPlainFactory(), create=True)
1366
parents = ['%d' % (t-1)]
1367
k1.add_lines('%d' % t, parents, ['hello\n'] * t)
1368
k1.get_texts(('%d' % t) for t in range(3))
1370
def test_iter_lines_reads_in_order(self):
1371
instrumented_t = get_transport('trace+memory:///')
1372
k1 = make_file_knit('id', instrumented_t, create=True, delta=True)
1373
self.assertEqual([('get', 'id.kndx',)], instrumented_t._activity)
1374
# add texts with no required ordering
1375
k1.add_lines('base', [], ['text\n'])
1376
k1.add_lines('base2', [], ['text2\n'])
1377
# clear the logged activity, but preserve the list instance in case of
1378
# clones pointing at it.
1379
del instrumented_t._activity[:]
1380
# request a last-first iteration
1381
results = list(k1.iter_lines_added_or_present_in_versions(
1384
[('readv', 'id.knit', [(0, 87), (87, 89)], False, None)],
1385
instrumented_t._activity)
1386
self.assertEqual([('text\n', 'base'), ('text2\n', 'base2')], results)
1388
def test_knit_format(self):
1389
# this tests that a new knit index file has the expected content
1390
# and that is writes the data we expect as records are added.
1391
knit = self.make_test_knit(True)
1392
# Now knit files are not created until we first add data to them
1393
self.assertFileEqual("# bzr knit index 8\n", 'test.kndx')
1394
knit.add_lines_with_ghosts('revid', ['a_ghost'], ['a\n'])
1395
self.assertFileEqual(
1396
"# bzr knit index 8\n"
1398
"revid fulltext 0 84 .a_ghost :",
1400
knit.add_lines_with_ghosts('revid2', ['revid'], ['a\n'])
1401
self.assertFileEqual(
1402
"# bzr knit index 8\n"
1403
"\nrevid fulltext 0 84 .a_ghost :"
1404
"\nrevid2 line-delta 84 82 0 :",
1406
# we should be able to load this file again
1407
knit = make_file_knit('test', get_transport('.'), access_mode='r')
1408
self.assertEqual(['revid', 'revid2'], knit.versions())
1409
# write a short write to the file and ensure that its ignored
1410
indexfile = file('test.kndx', 'ab')
1411
indexfile.write('\nrevid3 line-delta 166 82 1 2 3 4 5 .phwoar:demo ')
1413
# we should be able to load this file again
1414
knit = make_file_knit('test', get_transport('.'), access_mode='w')
1415
self.assertEqual(['revid', 'revid2'], knit.versions())
1416
# and add a revision with the same id the failed write had
1417
knit.add_lines('revid3', ['revid2'], ['a\n'])
1418
# and when reading it revid3 should now appear.
1419
knit = make_file_knit('test', get_transport('.'), access_mode='r')
1420
self.assertEqual(['revid', 'revid2', 'revid3'], knit.versions())
1421
self.assertEqual({'revid3':('revid2',)}, knit.get_parent_map(['revid3']))
1423
def test_delay_create(self):
1424
"""Test that passing delay_create=True creates files late"""
1425
knit = self.make_test_knit(annotate=True, delay_create=True)
1426
self.failIfExists('test.knit')
1427
self.failIfExists('test.kndx')
1428
knit.add_lines_with_ghosts('revid', ['a_ghost'], ['a\n'])
1429
self.failUnlessExists('test.knit')
1430
self.assertFileEqual(
1431
"# bzr knit index 8\n"
1433
"revid fulltext 0 84 .a_ghost :",
1436
def test_create_parent_dir(self):
1437
"""create_parent_dir can create knits in nonexistant dirs"""
1438
# Has no effect if we don't set 'delay_create'
1439
trans = get_transport('.')
1440
self.assertRaises(NoSuchFile, make_file_knit, 'dir/test',
1441
trans, access_mode='w', factory=None,
1442
create=True, create_parent_dir=True)
1443
# Nothing should have changed yet
1444
knit = make_file_knit('dir/test', trans, access_mode='w',
1445
factory=None, create=True,
1446
create_parent_dir=True,
1448
self.failIfExists('dir/test.knit')
1449
self.failIfExists('dir/test.kndx')
1450
self.failIfExists('dir')
1451
knit.add_lines('revid', [], ['a\n'])
1452
self.failUnlessExists('dir')
1453
self.failUnlessExists('dir/test.knit')
1454
self.assertFileEqual(
1455
"# bzr knit index 8\n"
1457
"revid fulltext 0 84 :",
1460
def test_create_mode_700(self):
1461
trans = get_transport('.')
1462
if not trans._can_roundtrip_unix_modebits():
1463
# Can't roundtrip, so no need to run this test
1465
knit = make_file_knit('dir/test', trans, access_mode='w', factory=None,
1466
create=True, create_parent_dir=True, delay_create=True,
1467
file_mode=0600, dir_mode=0700)
1468
knit.add_lines('revid', [], ['a\n'])
1469
self.assertTransportMode(trans, 'dir', 0700)
1470
self.assertTransportMode(trans, 'dir/test.knit', 0600)
1471
self.assertTransportMode(trans, 'dir/test.kndx', 0600)
1473
def test_create_mode_770(self):
1474
trans = get_transport('.')
1475
if not trans._can_roundtrip_unix_modebits():
1476
# Can't roundtrip, so no need to run this test
1478
knit = make_file_knit('dir/test', trans, access_mode='w', factory=None,
1479
create=True, create_parent_dir=True, delay_create=True,
1480
file_mode=0660, dir_mode=0770)
1481
knit.add_lines('revid', [], ['a\n'])
1482
self.assertTransportMode(trans, 'dir', 0770)
1483
self.assertTransportMode(trans, 'dir/test.knit', 0660)
1484
self.assertTransportMode(trans, 'dir/test.kndx', 0660)
1486
def test_create_mode_777(self):
1487
trans = get_transport('.')
1488
if not trans._can_roundtrip_unix_modebits():
1489
# Can't roundtrip, so no need to run this test
1491
knit = make_file_knit('dir/test', trans, access_mode='w', factory=None,
1492
create=True, create_parent_dir=True, delay_create=True,
1493
file_mode=0666, dir_mode=0777)
1494
knit.add_lines('revid', [], ['a\n'])
1495
self.assertTransportMode(trans, 'dir', 0777)
1496
self.assertTransportMode(trans, 'dir/test.knit', 0666)
1497
self.assertTransportMode(trans, 'dir/test.kndx', 0666)
1499
def test_plan_merge(self):
1500
my_knit = self.make_test_knit(annotate=True)
1501
my_knit.add_lines('text1', [], split_lines(TEXT_1))
1502
my_knit.add_lines('text1a', ['text1'], split_lines(TEXT_1A))
1503
my_knit.add_lines('text1b', ['text1'], split_lines(TEXT_1B))
1504
plan = list(my_knit.plan_merge('text1a', 'text1b'))
1505
for plan_line, expected_line in zip(plan, AB_MERGE):
1506
self.assertEqual(plan_line, expected_line)
1508
def test_get_stream_empty(self):
1509
"""Get a data stream for an empty knit file."""
1510
k1 = self.make_test_knit()
1511
format, data_list, reader_callable = k1.get_data_stream([])
1512
self.assertEqual('knit-plain', format)
1513
self.assertEqual([], data_list)
1514
content = reader_callable(None)
1515
self.assertEqual('', content)
1516
self.assertIsInstance(content, str)
1518
def test_get_stream_one_version(self):
1519
"""Get a data stream for a single record out of a knit containing just
1522
k1 = self.make_test_knit()
1524
('text-a', [], TEXT_1),
1526
expected_data_list = [
1527
# version, options, length, parents
1528
('text-a', ['fulltext'], 122, ()),
1530
for version_id, parents, lines in test_data:
1531
k1.add_lines(version_id, parents, split_lines(lines))
1533
format, data_list, reader_callable = k1.get_data_stream(['text-a'])
1534
self.assertEqual('knit-plain', format)
1535
self.assertEqual(expected_data_list, data_list)
1536
# There's only one record in the knit, so the content should be the
1537
# entire knit data file's contents.
1538
self.assertEqual(k1.transport.get_bytes(k1._data._access._filename),
1539
reader_callable(None))
1541
def test_get_stream_get_one_version_of_many(self):
1542
"""Get a data stream for just one version out of a knit containing many
1545
k1 = self.make_test_knit()
1546
# Insert the same data as test_knit_join, as they seem to cover a range
1547
# of cases (no parents, one parent, multiple parents).
1549
('text-a', [], TEXT_1),
1550
('text-b', ['text-a'], TEXT_1),
1551
('text-c', [], TEXT_1),
1552
('text-d', ['text-c'], TEXT_1),
1553
('text-m', ['text-b', 'text-d'], TEXT_1),
1555
expected_data_list = [
1556
# version, options, length, parents
1557
('text-m', ['line-delta'], 84, ('text-b', 'text-d')),
1559
for version_id, parents, lines in test_data:
1560
k1.add_lines(version_id, parents, split_lines(lines))
1562
format, data_list, reader_callable = k1.get_data_stream(['text-m'])
1563
self.assertEqual('knit-plain', format)
1564
self.assertEqual(expected_data_list, data_list)
1565
self.assertRecordContentEqual(k1, 'text-m', reader_callable(None))
1567
def test_get_data_stream_unordered_index(self):
1568
"""Get a data stream when the knit index reports versions out of order.
1570
https://bugs.launchpad.net/bzr/+bug/164637
1572
k1 = self.make_test_knit()
1574
('text-a', [], TEXT_1),
1575
('text-b', ['text-a'], TEXT_1),
1576
('text-c', [], TEXT_1),
1577
('text-d', ['text-c'], TEXT_1),
1578
('text-m', ['text-b', 'text-d'], TEXT_1),
1580
for version_id, parents, lines in test_data:
1581
k1.add_lines(version_id, parents, split_lines(lines))
1582
# monkey-patch versions method to return out of order, as if coming
1583
# from multiple independently indexed packs
1584
original_versions = k1.versions
1585
k1.versions = lambda: reversed(original_versions())
1586
expected_data_list = [
1587
('text-a', ['fulltext'], 122, ()),
1588
('text-b', ['line-delta'], 84, ('text-a',))]
1589
# now check the fulltext is first and the delta second
1590
format, data_list, _ = k1.get_data_stream(['text-a', 'text-b'])
1591
self.assertEqual('knit-plain', format)
1592
self.assertEqual(expected_data_list, data_list)
1593
# and that's true if we ask for them in the opposite order too
1594
format, data_list, _ = k1.get_data_stream(['text-b', 'text-a'])
1595
self.assertEqual(expected_data_list, data_list)
1596
# also try requesting more versions
1597
format, data_list, _ = k1.get_data_stream([
1598
'text-m', 'text-b', 'text-a'])
1600
('text-a', ['fulltext'], 122, ()),
1601
('text-b', ['line-delta'], 84, ('text-a',)),
1602
('text-m', ['line-delta'], 84, ('text-b', 'text-d')),
1605
def test_get_stream_ghost_parent(self):
1606
"""Get a data stream for a version with a ghost parent."""
1607
k1 = self.make_test_knit()
1609
k1.add_lines('text-a', [], split_lines(TEXT_1))
1610
k1.add_lines_with_ghosts('text-b', ['text-a', 'text-ghost'],
1611
split_lines(TEXT_1))
1613
expected_data_list = [
1614
# version, options, length, parents
1615
('text-b', ['line-delta'], 84, ('text-a', 'text-ghost')),
1618
format, data_list, reader_callable = k1.get_data_stream(['text-b'])
1619
self.assertEqual('knit-plain', format)
1620
self.assertEqual(expected_data_list, data_list)
1621
self.assertRecordContentEqual(k1, 'text-b', reader_callable(None))
1623
def test_get_stream_get_multiple_records(self):
1624
"""Get a stream for multiple records of a knit."""
1625
k1 = self.make_test_knit()
1626
# Insert the same data as test_knit_join, as they seem to cover a range
1627
# of cases (no parents, one parent, multiple parents).
1629
('text-a', [], TEXT_1),
1630
('text-b', ['text-a'], TEXT_1),
1631
('text-c', [], TEXT_1),
1632
('text-d', ['text-c'], TEXT_1),
1633
('text-m', ['text-b', 'text-d'], TEXT_1),
1635
for version_id, parents, lines in test_data:
1636
k1.add_lines(version_id, parents, split_lines(lines))
1638
# This test is actually a bit strict as the order in which they're
1639
# returned is not defined. This matches the current (deterministic)
1641
expected_data_list = [
1642
# version, options, length, parents
1643
('text-d', ['line-delta'], 84, ('text-c',)),
1644
('text-b', ['line-delta'], 84, ('text-a',)),
1646
# Note that even though we request the revision IDs in a particular
1647
# order, the data stream may return them in any order it likes. In this
1648
# case, they'll be in the order they were inserted into the knit.
1649
format, data_list, reader_callable = k1.get_data_stream(
1650
['text-d', 'text-b'])
1651
self.assertEqual('knit-plain', format)
1652
self.assertEqual(expected_data_list, data_list)
1653
# must match order they're returned
1654
self.assertRecordContentEqual(k1, 'text-d', reader_callable(84))
1655
self.assertRecordContentEqual(k1, 'text-b', reader_callable(84))
1656
self.assertEqual('', reader_callable(None),
1657
"There should be no more bytes left to read.")
1659
def test_get_stream_all(self):
1660
"""Get a data stream for all the records in a knit.
1662
This exercises fulltext records, line-delta records, records with
1663
various numbers of parents, and reading multiple records out of the
1664
callable. These cases ought to all be exercised individually by the
1665
other test_get_stream_* tests; this test is basically just paranoia.
1667
k1 = self.make_test_knit()
1668
# Insert the same data as test_knit_join, as they seem to cover a range
1669
# of cases (no parents, one parent, multiple parents).
1671
('text-a', [], TEXT_1),
1672
('text-b', ['text-a'], TEXT_1),
1673
('text-c', [], TEXT_1),
1674
('text-d', ['text-c'], TEXT_1),
1675
('text-m', ['text-b', 'text-d'], TEXT_1),
1677
for version_id, parents, lines in test_data:
1678
k1.add_lines(version_id, parents, split_lines(lines))
1680
# This test is actually a bit strict as the order in which they're
1681
# returned is not defined. This matches the current (deterministic)
1683
expected_data_list = [
1684
# version, options, length, parents
1685
('text-a', ['fulltext'], 122, ()),
1686
('text-b', ['line-delta'], 84, ('text-a',)),
1687
('text-m', ['line-delta'], 84, ('text-b', 'text-d')),
1688
('text-c', ['fulltext'], 121, ()),
1689
('text-d', ['line-delta'], 84, ('text-c',)),
1691
format, data_list, reader_callable = k1.get_data_stream(
1692
['text-a', 'text-b', 'text-c', 'text-d', 'text-m'])
1693
self.assertEqual('knit-plain', format)
1694
self.assertEqual(expected_data_list, data_list)
1695
for version_id, options, length, parents in expected_data_list:
1696
bytes = reader_callable(length)
1697
self.assertRecordContentEqual(k1, version_id, bytes)
1699
def assertKnitFilesEqual(self, knit1, knit2):
1700
"""Assert that the contents of the index and data files of two knits are
1704
knit1.transport.get_bytes(knit1._data._access._filename),
1705
knit2.transport.get_bytes(knit2._data._access._filename))
1707
knit1.transport.get_bytes(knit1._index._filename),
1708
knit2.transport.get_bytes(knit2._index._filename))
1710
def assertKnitValuesEqual(self, left, right):
1711
"""Assert that the texts, annotations and graph of left and right are
1714
self.assertEqual(set(left.versions()), set(right.versions()))
1715
for version in left.versions():
1716
self.assertEqual(left.get_parents_with_ghosts(version),
1717
right.get_parents_with_ghosts(version))
1718
self.assertEqual(left.get_lines(version),
1719
right.get_lines(version))
1720
self.assertEqual(left.annotate(version),
1721
right.annotate(version))
1723
def test_insert_data_stream_empty(self):
1724
"""Inserting a data stream with no records should not put any data into
1727
k1 = self.make_test_knit()
1728
k1.insert_data_stream(
1729
(k1.get_format_signature(), [], lambda ignored: ''))
1730
self.assertEqual('', k1.transport.get_bytes(k1._data._access._filename),
1731
"The .knit should be completely empty.")
1732
self.assertEqual(k1._index.HEADER,
1733
k1.transport.get_bytes(k1._index._filename),
1734
"The .kndx should have nothing apart from the header.")
1736
def test_insert_data_stream_one_record(self):
1737
"""Inserting a data stream with one record from a knit with one record
1738
results in byte-identical files.
1740
source = self.make_test_knit(name='source')
1741
source.add_lines('text-a', [], split_lines(TEXT_1))
1742
data_stream = source.get_data_stream(['text-a'])
1743
target = self.make_test_knit(name='target')
1744
target.insert_data_stream(data_stream)
1745
self.assertKnitFilesEqual(source, target)
1747
def test_insert_data_stream_annotated_unannotated(self):
1748
"""Inserting an annotated datastream to an unannotated knit works."""
1749
# case one - full texts.
1750
source = self.make_test_knit(name='source', annotate=True)
1751
target = self.make_test_knit(name='target', annotate=False)
1752
source.add_lines('text-a', [], split_lines(TEXT_1))
1753
target.insert_data_stream(source.get_data_stream(['text-a']))
1754
self.assertKnitValuesEqual(source, target)
1755
# case two - deltas.
1756
source.add_lines('text-b', ['text-a'], split_lines(TEXT_2))
1757
target.insert_data_stream(source.get_data_stream(['text-b']))
1758
self.assertKnitValuesEqual(source, target)
1760
def test_insert_data_stream_unannotated_annotated(self):
1761
"""Inserting an unannotated datastream to an annotated knit works."""
1762
# case one - full texts.
1763
source = self.make_test_knit(name='source', annotate=False)
1764
target = self.make_test_knit(name='target', annotate=True)
1765
source.add_lines('text-a', [], split_lines(TEXT_1))
1766
target.insert_data_stream(source.get_data_stream(['text-a']))
1767
self.assertKnitValuesEqual(source, target)
1768
# case two - deltas.
1769
source.add_lines('text-b', ['text-a'], split_lines(TEXT_2))
1770
target.insert_data_stream(source.get_data_stream(['text-b']))
1771
self.assertKnitValuesEqual(source, target)
1773
def test_insert_data_stream_records_already_present(self):
1774
"""Insert a data stream where some records are alreday present in the
1775
target, and some not. Only the new records are inserted.
1777
source = self.make_test_knit(name='source')
1778
target = self.make_test_knit(name='target')
1779
# Insert 'text-a' into both source and target
1780
source.add_lines('text-a', [], split_lines(TEXT_1))
1781
target.insert_data_stream(source.get_data_stream(['text-a']))
1782
# Insert 'text-b' into just the source.
1783
source.add_lines('text-b', ['text-a'], split_lines(TEXT_1))
1784
# Get a data stream of both text-a and text-b, and insert it.
1785
data_stream = source.get_data_stream(['text-a', 'text-b'])
1786
target.insert_data_stream(data_stream)
1787
# The source and target will now be identical. This means the text-a
1788
# record was not added a second time.
1789
self.assertKnitFilesEqual(source, target)
1791
def test_insert_data_stream_multiple_records(self):
1792
"""Inserting a data stream of all records from a knit with multiple
1793
records results in byte-identical files.
1795
source = self.make_test_knit(name='source')
1796
source.add_lines('text-a', [], split_lines(TEXT_1))
1797
source.add_lines('text-b', ['text-a'], split_lines(TEXT_1))
1798
source.add_lines('text-c', [], split_lines(TEXT_1))
1799
data_stream = source.get_data_stream(['text-a', 'text-b', 'text-c'])
1801
target = self.make_test_knit(name='target')
1802
target.insert_data_stream(data_stream)
1804
self.assertKnitFilesEqual(source, target)
1806
def test_insert_data_stream_ghost_parent(self):
1807
"""Insert a data stream with a record that has a ghost parent."""
1808
# Make a knit with a record, text-a, that has a ghost parent.
1809
source = self.make_test_knit(name='source')
1810
source.add_lines_with_ghosts('text-a', ['text-ghost'],
1811
split_lines(TEXT_1))
1812
data_stream = source.get_data_stream(['text-a'])
1814
target = self.make_test_knit(name='target')
1815
target.insert_data_stream(data_stream)
1817
self.assertKnitFilesEqual(source, target)
1819
# The target knit object is in a consistent state, i.e. the record we
1820
# just added is immediately visible.
1821
self.assertTrue(target.has_version('text-a'))
1822
self.assertFalse(target.has_version('text-ghost'))
1823
self.assertEqual({'text-a':('text-ghost',)},
1824
target.get_parent_map(['text-a', 'text-ghost']))
1825
self.assertEqual(split_lines(TEXT_1), target.get_lines('text-a'))
1827
def test_insert_data_stream_inconsistent_version_lines(self):
1828
"""Inserting a data stream which has different content for a version_id
1829
than already exists in the knit will raise KnitCorrupt.
1831
source = self.make_test_knit(name='source')
1832
target = self.make_test_knit(name='target')
1833
# Insert a different 'text-a' into both source and target
1834
source.add_lines('text-a', [], split_lines(TEXT_1))
1835
target.add_lines('text-a', [], split_lines(TEXT_2))
1836
# Insert a data stream with conflicting content into the target
1837
data_stream = source.get_data_stream(['text-a'])
1839
errors.KnitCorrupt, target.insert_data_stream, data_stream)
1841
def test_insert_data_stream_inconsistent_version_parents(self):
1842
"""Inserting a data stream which has different parents for a version_id
1843
than already exists in the knit will raise KnitCorrupt.
1845
source = self.make_test_knit(name='source')
1846
target = self.make_test_knit(name='target')
1847
# Insert a different 'text-a' into both source and target. They differ
1848
# only by the parents list, the content is the same.
1849
source.add_lines_with_ghosts('text-a', [], split_lines(TEXT_1))
1850
target.add_lines_with_ghosts('text-a', ['a-ghost'], split_lines(TEXT_1))
1851
# Insert a data stream with conflicting content into the target
1852
data_stream = source.get_data_stream(['text-a'])
1854
errors.KnitCorrupt, target.insert_data_stream, data_stream)
1856
def test_insert_data_stream_unknown_format(self):
1857
"""A data stream in a different format to the target knit cannot be
1860
It will raise KnitDataStreamUnknown because the fallback code will fail
1861
to make a knit. In future we may need KnitDataStreamIncompatible again,
1862
for more exotic cases.
1864
data_stream = ('fake-format-signature', [], lambda _: '')
1865
target = self.make_test_knit(name='target')
1867
errors.KnitDataStreamUnknown,
1868
target.insert_data_stream, data_stream)
1870
# * test that a stream of "already present version, then new version"
1871
# inserts correctly.
1874
def assertMadeStreamKnit(self, source_knit, versions, target_knit):
1875
"""Assert that a knit made from a stream is as expected."""
1876
a_stream = source_knit.get_data_stream(versions)
1877
expected_data = a_stream[2](None)
1878
a_stream = source_knit.get_data_stream(versions)
1879
a_knit = target_knit._knit_from_datastream(a_stream)
1880
self.assertEqual(source_knit.factory.__class__,
1881
a_knit.factory.__class__)
1882
self.assertIsInstance(a_knit._data._access, _StreamAccess)
1883
self.assertIsInstance(a_knit._index, _StreamIndex)
1884
self.assertEqual(a_knit._index.data_list, a_stream[1])
1885
self.assertEqual(a_knit._data._access.data, expected_data)
1886
self.assertEqual(a_knit.filename, target_knit.filename)
1887
self.assertEqual(a_knit.transport, target_knit.transport)
1888
self.assertEqual(a_knit._index, a_knit._data._access.stream_index)
1889
self.assertEqual(target_knit, a_knit._data._access.backing_knit)
1890
self.assertIsInstance(a_knit._data._access.orig_factory,
1891
source_knit.factory.__class__)
1893
def test__knit_from_data_stream_empty(self):
1894
"""Create a knit object from a datastream."""
1895
annotated = self.make_test_knit(name='source', annotate=True)
1896
plain = self.make_test_knit(name='target', annotate=False)
1897
# case 1: annotated source
1898
self.assertMadeStreamKnit(annotated, [], annotated)
1899
self.assertMadeStreamKnit(annotated, [], plain)
1900
# case 2: plain source
1901
self.assertMadeStreamKnit(plain, [], annotated)
1902
self.assertMadeStreamKnit(plain, [], plain)
1904
def test__knit_from_data_stream_unknown_format(self):
1905
annotated = self.make_test_knit(name='source', annotate=True)
1906
self.assertRaises(errors.KnitDataStreamUnknown,
1907
annotated._knit_from_datastream, ("unknown", None, None))
1919
Banana cup cake recipe
1925
- self-raising flour
1929
Banana cup cake recipe
1931
- bananas (do not use plantains!!!)
1938
Banana cup cake recipe
1941
- self-raising flour
1954
AB_MERGE_TEXT="""unchanged|Banana cup cake recipe
1959
new-b|- bananas (do not use plantains!!!)
1960
unchanged|- broken tea cups
1961
new-a|- self-raising flour
1964
AB_MERGE=[tuple(l.split('|')) for l in AB_MERGE_TEXT.splitlines(True)]
1967
def line_delta(from_lines, to_lines):
1968
"""Generate line-based delta from one text to another"""
1969
s = difflib.SequenceMatcher(None, from_lines, to_lines)
1970
for op in s.get_opcodes():
1971
if op[0] == 'equal':
1973
yield '%d,%d,%d\n' % (op[1], op[2], op[4]-op[3])
1974
for i in range(op[3], op[4]):
1978
def apply_line_delta(basis_lines, delta_lines):
1979
"""Apply a line-based perfect diff
1981
basis_lines -- text to apply the patch to
1982
delta_lines -- diff instructions and content
1984
out = basis_lines[:]
1987
while i < len(delta_lines):
1989
a, b, c = map(long, l.split(','))
1991
out[offset+a:offset+b] = delta_lines[i:i+c]
1993
offset = offset + (b - a) + c
1997
class TestWeaveToKnit(KnitTests):
1999
def test_weave_to_knit_matches(self):
2000
# check that the WeaveToKnit is_compatible function
2001
# registers True for a Weave to a Knit.
2002
w = Weave(get_scope=lambda:None)
2003
k = self.make_test_knit()
2004
self.failUnless(WeaveToKnit.is_compatible(w, k))
2005
self.failIf(WeaveToKnit.is_compatible(k, w))
2006
self.failIf(WeaveToKnit.is_compatible(w, w))
2007
self.failIf(WeaveToKnit.is_compatible(k, k))
2010
class TestKnitIndex(KnitTests):
2012
def test_add_versions_dictionary_compresses(self):
2013
"""Adding versions to the index should update the lookup dict"""
2014
knit = self.make_test_knit()
2016
idx.add_version('a-1', ['fulltext'], (None, 0, 0), [])
2017
self.check_file_contents('test.kndx',
2018
'# bzr knit index 8\n'
2020
'a-1 fulltext 0 0 :'
2022
idx.add_versions([('a-2', ['fulltext'], (None, 0, 0), ['a-1']),
2023
('a-3', ['fulltext'], (None, 0, 0), ['a-2']),
2025
self.check_file_contents('test.kndx',
2026
'# bzr knit index 8\n'
2028
'a-1 fulltext 0 0 :\n'
2029
'a-2 fulltext 0 0 0 :\n'
2030
'a-3 fulltext 0 0 1 :'
2032
self.assertEqual(['a-1', 'a-2', 'a-3'], idx._history)
2033
self.assertEqual({'a-1':('a-1', ['fulltext'], 0, 0, (), 0),
2034
'a-2':('a-2', ['fulltext'], 0, 0, ('a-1',), 1),
2035
'a-3':('a-3', ['fulltext'], 0, 0, ('a-2',), 2),
2038
def test_add_versions_fails_clean(self):
2039
"""If add_versions fails in the middle, it restores a pristine state.
2041
Any modifications that are made to the index are reset if all versions
2044
# This cheats a little bit by passing in a generator which will
2045
# raise an exception before the processing finishes
2046
# Other possibilities would be to have an version with the wrong number
2047
# of entries, or to make the backing transport unable to write any
2050
knit = self.make_test_knit()
2052
idx.add_version('a-1', ['fulltext'], (None, 0, 0), [])
2054
class StopEarly(Exception):
2057
def generate_failure():
2058
"""Add some entries and then raise an exception"""
2059
yield ('a-2', ['fulltext'], (None, 0, 0), ('a-1',))
2060
yield ('a-3', ['fulltext'], (None, 0, 0), ('a-2',))
2063
# Assert the pre-condition
2064
self.assertEqual(['a-1'], idx._history)
2065
self.assertEqual({'a-1':('a-1', ['fulltext'], 0, 0, (), 0)}, idx._cache)
2067
self.assertRaises(StopEarly, idx.add_versions, generate_failure())
2069
# And it shouldn't be modified
2070
self.assertEqual(['a-1'], idx._history)
2071
self.assertEqual({'a-1':('a-1', ['fulltext'], 0, 0, (), 0)}, idx._cache)
2073
def test_knit_index_ignores_empty_files(self):
2074
# There was a race condition in older bzr, where a ^C at the right time
2075
# could leave an empty .kndx file, which bzr would later claim was a
2076
# corrupted file since the header was not present. In reality, the file
2077
# just wasn't created, so it should be ignored.
2078
t = get_transport('.')
2079
t.put_bytes('test.kndx', '')
2081
knit = self.make_test_knit()
2083
def test_knit_index_checks_header(self):
2084
t = get_transport('.')
2085
t.put_bytes('test.kndx', '# not really a knit header\n\n')
2087
self.assertRaises(KnitHeaderError, self.make_test_knit)
2090
class TestGraphIndexKnit(KnitTests):
2091
"""Tests for knits using a GraphIndex rather than a KnitIndex."""
2093
def make_g_index(self, name, ref_lists=0, nodes=[]):
2094
builder = GraphIndexBuilder(ref_lists)
2095
for node, references, value in nodes:
2096
builder.add_node(node, references, value)
2097
stream = builder.finish()
2098
trans = self.get_transport()
2099
size = trans.put_file(name, stream)
2100
return GraphIndex(trans, name, size)
2102
def two_graph_index(self, deltas=False, catch_adds=False):
2103
"""Build a two-graph index.
2105
:param deltas: If true, use underlying indices with two node-ref
2106
lists and 'parent' set to a delta-compressed against tail.
2108
# build a complex graph across several indices.
2110
# delta compression inn the index
2111
index1 = self.make_g_index('1', 2, [
2112
(('tip', ), 'N0 100', ([('parent', )], [], )),
2113
(('tail', ), '', ([], []))])
2114
index2 = self.make_g_index('2', 2, [
2115
(('parent', ), ' 100 78', ([('tail', ), ('ghost', )], [('tail', )])),
2116
(('separate', ), '', ([], []))])
2118
# just blob location and graph in the index.
2119
index1 = self.make_g_index('1', 1, [
2120
(('tip', ), 'N0 100', ([('parent', )], )),
2121
(('tail', ), '', ([], ))])
2122
index2 = self.make_g_index('2', 1, [
2123
(('parent', ), ' 100 78', ([('tail', ), ('ghost', )], )),
2124
(('separate', ), '', ([], ))])
2125
combined_index = CombinedGraphIndex([index1, index2])
2127
self.combined_index = combined_index
2128
self.caught_entries = []
2129
add_callback = self.catch_add
2132
return KnitGraphIndex(combined_index, deltas=deltas,
2133
add_callback=add_callback)
2135
def test_get_ancestry(self):
2136
# get_ancestry is defined as eliding ghosts, not erroring.
2137
index = self.two_graph_index()
2138
self.assertEqual([], index.get_ancestry([]))
2139
self.assertEqual(['separate'], index.get_ancestry(['separate']))
2140
self.assertEqual(['tail'], index.get_ancestry(['tail']))
2141
self.assertEqual(['tail', 'parent'], index.get_ancestry(['parent']))
2142
self.assertEqual(['tail', 'parent', 'tip'], index.get_ancestry(['tip']))
2143
self.assertTrue(index.get_ancestry(['tip', 'separate']) in
2144
(['tail', 'parent', 'tip', 'separate'],
2145
['separate', 'tail', 'parent', 'tip'],
2147
# and without topo_sort
2148
self.assertEqual(set(['separate']),
2149
set(index.get_ancestry(['separate'], topo_sorted=False)))
2150
self.assertEqual(set(['tail']),
2151
set(index.get_ancestry(['tail'], topo_sorted=False)))
2152
self.assertEqual(set(['tail', 'parent']),
2153
set(index.get_ancestry(['parent'], topo_sorted=False)))
2154
self.assertEqual(set(['tail', 'parent', 'tip']),
2155
set(index.get_ancestry(['tip'], topo_sorted=False)))
2156
self.assertEqual(set(['separate', 'tail', 'parent', 'tip']),
2157
set(index.get_ancestry(['tip', 'separate'])))
2158
# asking for a ghost makes it go boom.
2159
self.assertRaises(errors.RevisionNotPresent, index.get_ancestry, ['ghost'])
2161
def test_get_ancestry_with_ghosts(self):
2162
index = self.two_graph_index()
2163
self.assertEqual([], index.get_ancestry_with_ghosts([]))
2164
self.assertEqual(['separate'], index.get_ancestry_with_ghosts(['separate']))
2165
self.assertEqual(['tail'], index.get_ancestry_with_ghosts(['tail']))
2166
self.assertTrue(index.get_ancestry_with_ghosts(['parent']) in
2167
(['tail', 'ghost', 'parent'],
2168
['ghost', 'tail', 'parent'],
2170
self.assertTrue(index.get_ancestry_with_ghosts(['tip']) in
2171
(['tail', 'ghost', 'parent', 'tip'],
2172
['ghost', 'tail', 'parent', 'tip'],
2174
self.assertTrue(index.get_ancestry_with_ghosts(['tip', 'separate']) in
2175
(['tail', 'ghost', 'parent', 'tip', 'separate'],
2176
['ghost', 'tail', 'parent', 'tip', 'separate'],
2177
['separate', 'tail', 'ghost', 'parent', 'tip'],
2178
['separate', 'ghost', 'tail', 'parent', 'tip'],
2180
# asking for a ghost makes it go boom.
2181
self.assertRaises(errors.RevisionNotPresent, index.get_ancestry_with_ghosts, ['ghost'])
2183
def test_num_versions(self):
2184
index = self.two_graph_index()
2185
self.assertEqual(4, index.num_versions())
2187
def test_get_versions(self):
2188
index = self.two_graph_index()
2189
self.assertEqual(set(['tail', 'tip', 'parent', 'separate']),
2190
set(index.get_versions()))
2192
def test_has_version(self):
2193
index = self.two_graph_index()
2194
self.assertTrue(index.has_version('tail'))
2195
self.assertFalse(index.has_version('ghost'))
2197
def test_get_position(self):
2198
index = self.two_graph_index()
2199
self.assertEqual((index._graph_index._indices[0], 0, 100), index.get_position('tip'))
2200
self.assertEqual((index._graph_index._indices[1], 100, 78), index.get_position('parent'))
2202
def test_get_method_deltas(self):
2203
index = self.two_graph_index(deltas=True)
2204
self.assertEqual('fulltext', index.get_method('tip'))
2205
self.assertEqual('line-delta', index.get_method('parent'))
2207
def test_get_method_no_deltas(self):
2208
# check that the parent-history lookup is ignored with deltas=False.
2209
index = self.two_graph_index(deltas=False)
2210
self.assertEqual('fulltext', index.get_method('tip'))
2211
self.assertEqual('fulltext', index.get_method('parent'))
2213
def test_get_options_deltas(self):
2214
index = self.two_graph_index(deltas=True)
2215
self.assertEqual(['fulltext', 'no-eol'], index.get_options('tip'))
2216
self.assertEqual(['line-delta'], index.get_options('parent'))
2218
def test_get_options_no_deltas(self):
2219
# check that the parent-history lookup is ignored with deltas=False.
2220
index = self.two_graph_index(deltas=False)
2221
self.assertEqual(['fulltext', 'no-eol'], index.get_options('tip'))
2222
self.assertEqual(['fulltext'], index.get_options('parent'))
2224
def test_get_parents_with_ghosts(self):
2225
index = self.two_graph_index()
2226
self.assertEqual(('tail', 'ghost'), index.get_parents_with_ghosts('parent'))
2227
# and errors on ghosts.
2228
self.assertRaises(errors.RevisionNotPresent,
2229
index.get_parents_with_ghosts, 'ghost')
2231
def test_check_versions_present(self):
2232
# ghosts should not be considered present
2233
index = self.two_graph_index()
2234
self.assertRaises(RevisionNotPresent, index.check_versions_present,
2236
self.assertRaises(RevisionNotPresent, index.check_versions_present,
2238
index.check_versions_present(['tail', 'separate'])
2240
def catch_add(self, entries):
2241
self.caught_entries.append(entries)
2243
def test_add_no_callback_errors(self):
2244
index = self.two_graph_index()
2245
self.assertRaises(errors.ReadOnlyError, index.add_version,
2246
'new', 'fulltext,no-eol', (None, 50, 60), ['separate'])
2248
def test_add_version_smoke(self):
2249
index = self.two_graph_index(catch_adds=True)
2250
index.add_version('new', 'fulltext,no-eol', (None, 50, 60), ['separate'])
2251
self.assertEqual([[(('new', ), 'N50 60', ((('separate',),),))]],
2252
self.caught_entries)
2254
def test_add_version_delta_not_delta_index(self):
2255
index = self.two_graph_index(catch_adds=True)
2256
self.assertRaises(errors.KnitCorrupt, index.add_version,
2257
'new', 'no-eol,line-delta', (None, 0, 100), ['parent'])
2258
self.assertEqual([], self.caught_entries)
2260
def test_add_version_same_dup(self):
2261
index = self.two_graph_index(catch_adds=True)
2262
# options can be spelt two different ways
2263
index.add_version('tip', 'fulltext,no-eol', (None, 0, 100), ['parent'])
2264
index.add_version('tip', 'no-eol,fulltext', (None, 0, 100), ['parent'])
2265
# but neither should have added data.
2266
self.assertEqual([[], []], self.caught_entries)
2268
def test_add_version_different_dup(self):
2269
index = self.two_graph_index(deltas=True, catch_adds=True)
2271
self.assertRaises(errors.KnitCorrupt, index.add_version,
2272
'tip', 'no-eol,line-delta', (None, 0, 100), ['parent'])
2273
self.assertRaises(errors.KnitCorrupt, index.add_version,
2274
'tip', 'line-delta,no-eol', (None, 0, 100), ['parent'])
2275
self.assertRaises(errors.KnitCorrupt, index.add_version,
2276
'tip', 'fulltext', (None, 0, 100), ['parent'])
2278
self.assertRaises(errors.KnitCorrupt, index.add_version,
2279
'tip', 'fulltext,no-eol', (None, 50, 100), ['parent'])
2280
self.assertRaises(errors.KnitCorrupt, index.add_version,
2281
'tip', 'fulltext,no-eol', (None, 0, 1000), ['parent'])
2283
self.assertRaises(errors.KnitCorrupt, index.add_version,
2284
'tip', 'fulltext,no-eol', (None, 0, 100), [])
2285
self.assertEqual([], self.caught_entries)
2287
def test_add_versions_nodeltas(self):
2288
index = self.two_graph_index(catch_adds=True)
2289
index.add_versions([
2290
('new', 'fulltext,no-eol', (None, 50, 60), ['separate']),
2291
('new2', 'fulltext', (None, 0, 6), ['new']),
2293
self.assertEqual([(('new', ), 'N50 60', ((('separate',),),)),
2294
(('new2', ), ' 0 6', ((('new',),),))],
2295
sorted(self.caught_entries[0]))
2296
self.assertEqual(1, len(self.caught_entries))
2298
def test_add_versions_deltas(self):
2299
index = self.two_graph_index(deltas=True, catch_adds=True)
2300
index.add_versions([
2301
('new', 'fulltext,no-eol', (None, 50, 60), ['separate']),
2302
('new2', 'line-delta', (None, 0, 6), ['new']),
2304
self.assertEqual([(('new', ), 'N50 60', ((('separate',),), ())),
2305
(('new2', ), ' 0 6', ((('new',),), (('new',),), ))],
2306
sorted(self.caught_entries[0]))
2307
self.assertEqual(1, len(self.caught_entries))
2309
def test_add_versions_delta_not_delta_index(self):
2310
index = self.two_graph_index(catch_adds=True)
2311
self.assertRaises(errors.KnitCorrupt, index.add_versions,
2312
[('new', 'no-eol,line-delta', (None, 0, 100), ['parent'])])
2313
self.assertEqual([], self.caught_entries)
2315
def test_add_versions_random_id_accepted(self):
2316
index = self.two_graph_index(catch_adds=True)
2317
index.add_versions([], random_id=True)
2319
def test_add_versions_same_dup(self):
2320
index = self.two_graph_index(catch_adds=True)
2321
# options can be spelt two different ways
2322
index.add_versions([('tip', 'fulltext,no-eol', (None, 0, 100), ['parent'])])
2323
index.add_versions([('tip', 'no-eol,fulltext', (None, 0, 100), ['parent'])])
2324
# but neither should have added data.
2325
self.assertEqual([[], []], self.caught_entries)
2327
def test_add_versions_different_dup(self):
2328
index = self.two_graph_index(deltas=True, catch_adds=True)
2330
self.assertRaises(errors.KnitCorrupt, index.add_versions,
2331
[('tip', 'no-eol,line-delta', (None, 0, 100), ['parent'])])
2332
self.assertRaises(errors.KnitCorrupt, index.add_versions,
2333
[('tip', 'line-delta,no-eol', (None, 0, 100), ['parent'])])
2334
self.assertRaises(errors.KnitCorrupt, index.add_versions,
2335
[('tip', 'fulltext', (None, 0, 100), ['parent'])])
2337
self.assertRaises(errors.KnitCorrupt, index.add_versions,
2338
[('tip', 'fulltext,no-eol', (None, 50, 100), ['parent'])])
2339
self.assertRaises(errors.KnitCorrupt, index.add_versions,
2340
[('tip', 'fulltext,no-eol', (None, 0, 1000), ['parent'])])
2342
self.assertRaises(errors.KnitCorrupt, index.add_versions,
2343
[('tip', 'fulltext,no-eol', (None, 0, 100), [])])
2344
# change options in the second record
2345
self.assertRaises(errors.KnitCorrupt, index.add_versions,
2346
[('tip', 'fulltext,no-eol', (None, 0, 100), ['parent']),
2347
('tip', 'no-eol,line-delta', (None, 0, 100), ['parent'])])
2348
self.assertEqual([], self.caught_entries)
2350
class TestNoParentsGraphIndexKnit(KnitTests):
2351
"""Tests for knits using KnitGraphIndex with no parents."""
2353
def make_g_index(self, name, ref_lists=0, nodes=[]):
2354
builder = GraphIndexBuilder(ref_lists)
2355
for node, references in nodes:
2356
builder.add_node(node, references)
2357
stream = builder.finish()
2358
trans = self.get_transport()
2359
size = trans.put_file(name, stream)
2360
return GraphIndex(trans, name, size)
2362
def test_parents_deltas_incompatible(self):
2363
index = CombinedGraphIndex([])
2364
self.assertRaises(errors.KnitError, KnitGraphIndex, index,
2365
deltas=True, parents=False)
2367
def two_graph_index(self, catch_adds=False):
2368
"""Build a two-graph index.
2370
:param deltas: If true, use underlying indices with two node-ref
2371
lists and 'parent' set to a delta-compressed against tail.
2373
# put several versions in the index.
2374
index1 = self.make_g_index('1', 0, [
2375
(('tip', ), 'N0 100'),
2377
index2 = self.make_g_index('2', 0, [
2378
(('parent', ), ' 100 78'),
2379
(('separate', ), '')])
2380
combined_index = CombinedGraphIndex([index1, index2])
2382
self.combined_index = combined_index
2383
self.caught_entries = []
2384
add_callback = self.catch_add
2387
return KnitGraphIndex(combined_index, parents=False,
2388
add_callback=add_callback)
2390
def test_get_ancestry(self):
2391
# with no parents, ancestry is always just the key.
2392
index = self.two_graph_index()
2393
self.assertEqual([], index.get_ancestry([]))
2394
self.assertEqual(['separate'], index.get_ancestry(['separate']))
2395
self.assertEqual(['tail'], index.get_ancestry(['tail']))
2396
self.assertEqual(['parent'], index.get_ancestry(['parent']))
2397
self.assertEqual(['tip'], index.get_ancestry(['tip']))
2398
self.assertTrue(index.get_ancestry(['tip', 'separate']) in
2399
(['tip', 'separate'],
2400
['separate', 'tip'],
2402
# asking for a ghost makes it go boom.
2403
self.assertRaises(errors.RevisionNotPresent, index.get_ancestry, ['ghost'])
2405
def test_get_ancestry_with_ghosts(self):
2406
index = self.two_graph_index()
2407
self.assertEqual([], index.get_ancestry_with_ghosts([]))
2408
self.assertEqual(['separate'], index.get_ancestry_with_ghosts(['separate']))
2409
self.assertEqual(['tail'], index.get_ancestry_with_ghosts(['tail']))
2410
self.assertEqual(['parent'], index.get_ancestry_with_ghosts(['parent']))
2411
self.assertEqual(['tip'], index.get_ancestry_with_ghosts(['tip']))
2412
self.assertTrue(index.get_ancestry_with_ghosts(['tip', 'separate']) in
2413
(['tip', 'separate'],
2414
['separate', 'tip'],
2416
# asking for a ghost makes it go boom.
2417
self.assertRaises(errors.RevisionNotPresent, index.get_ancestry_with_ghosts, ['ghost'])
2419
def test_num_versions(self):
2420
index = self.two_graph_index()
2421
self.assertEqual(4, index.num_versions())
2423
def test_get_versions(self):
2424
index = self.two_graph_index()
2425
self.assertEqual(set(['tail', 'tip', 'parent', 'separate']),
2426
set(index.get_versions()))
2428
def test_has_version(self):
2429
index = self.two_graph_index()
2430
self.assertTrue(index.has_version('tail'))
2431
self.assertFalse(index.has_version('ghost'))
2433
def test_get_position(self):
2434
index = self.two_graph_index()
2435
self.assertEqual((index._graph_index._indices[0], 0, 100), index.get_position('tip'))
2436
self.assertEqual((index._graph_index._indices[1], 100, 78), index.get_position('parent'))
2438
def test_get_method(self):
2439
index = self.two_graph_index()
2440
self.assertEqual('fulltext', index.get_method('tip'))
2441
self.assertEqual(['fulltext'], index.get_options('parent'))
2443
def test_get_options(self):
2444
index = self.two_graph_index()
2445
self.assertEqual(['fulltext', 'no-eol'], index.get_options('tip'))
2446
self.assertEqual(['fulltext'], index.get_options('parent'))
2448
def test_get_parents_with_ghosts(self):
2449
index = self.two_graph_index()
2450
self.assertEqual((), index.get_parents_with_ghosts('parent'))
2451
# and errors on ghosts.
2452
self.assertRaises(errors.RevisionNotPresent,
2453
index.get_parents_with_ghosts, 'ghost')
2455
def test_check_versions_present(self):
2456
index = self.two_graph_index()
2457
self.assertRaises(RevisionNotPresent, index.check_versions_present,
2459
self.assertRaises(RevisionNotPresent, index.check_versions_present,
2460
['tail', 'missing'])
2461
index.check_versions_present(['tail', 'separate'])
2463
def catch_add(self, entries):
2464
self.caught_entries.append(entries)
2466
def test_add_no_callback_errors(self):
2467
index = self.two_graph_index()
2468
self.assertRaises(errors.ReadOnlyError, index.add_version,
2469
'new', 'fulltext,no-eol', (None, 50, 60), ['separate'])
2471
def test_add_version_smoke(self):
2472
index = self.two_graph_index(catch_adds=True)
2473
index.add_version('new', 'fulltext,no-eol', (None, 50, 60), [])
2474
self.assertEqual([[(('new', ), 'N50 60')]],
2475
self.caught_entries)
2477
def test_add_version_delta_not_delta_index(self):
2478
index = self.two_graph_index(catch_adds=True)
2479
self.assertRaises(errors.KnitCorrupt, index.add_version,
2480
'new', 'no-eol,line-delta', (None, 0, 100), [])
2481
self.assertEqual([], self.caught_entries)
2483
def test_add_version_same_dup(self):
2484
index = self.two_graph_index(catch_adds=True)
2485
# options can be spelt two different ways
2486
index.add_version('tip', 'fulltext,no-eol', (None, 0, 100), [])
2487
index.add_version('tip', 'no-eol,fulltext', (None, 0, 100), [])
2488
# but neither should have added data.
2489
self.assertEqual([[], []], self.caught_entries)
2491
def test_add_version_different_dup(self):
2492
index = self.two_graph_index(catch_adds=True)
2494
self.assertRaises(errors.KnitCorrupt, index.add_version,
2495
'tip', 'no-eol,line-delta', (None, 0, 100), [])
2496
self.assertRaises(errors.KnitCorrupt, index.add_version,
2497
'tip', 'line-delta,no-eol', (None, 0, 100), [])
2498
self.assertRaises(errors.KnitCorrupt, index.add_version,
2499
'tip', 'fulltext', (None, 0, 100), [])
2501
self.assertRaises(errors.KnitCorrupt, index.add_version,
2502
'tip', 'fulltext,no-eol', (None, 50, 100), [])
2503
self.assertRaises(errors.KnitCorrupt, index.add_version,
2504
'tip', 'fulltext,no-eol', (None, 0, 1000), [])
2506
self.assertRaises(errors.KnitCorrupt, index.add_version,
2507
'tip', 'fulltext,no-eol', (None, 0, 100), ['parent'])
2508
self.assertEqual([], self.caught_entries)
2510
def test_add_versions(self):
2511
index = self.two_graph_index(catch_adds=True)
2512
index.add_versions([
2513
('new', 'fulltext,no-eol', (None, 50, 60), []),
2514
('new2', 'fulltext', (None, 0, 6), []),
2516
self.assertEqual([(('new', ), 'N50 60'), (('new2', ), ' 0 6')],
2517
sorted(self.caught_entries[0]))
2518
self.assertEqual(1, len(self.caught_entries))
2520
def test_add_versions_delta_not_delta_index(self):
2521
index = self.two_graph_index(catch_adds=True)
2522
self.assertRaises(errors.KnitCorrupt, index.add_versions,
2523
[('new', 'no-eol,line-delta', (None, 0, 100), ['parent'])])
2524
self.assertEqual([], self.caught_entries)
2526
def test_add_versions_parents_not_parents_index(self):
2527
index = self.two_graph_index(catch_adds=True)
2528
self.assertRaises(errors.KnitCorrupt, index.add_versions,
2529
[('new', 'no-eol,fulltext', (None, 0, 100), ['parent'])])
2530
self.assertEqual([], self.caught_entries)
2532
def test_add_versions_random_id_accepted(self):
2533
index = self.two_graph_index(catch_adds=True)
2534
index.add_versions([], random_id=True)
2536
def test_add_versions_same_dup(self):
2537
index = self.two_graph_index(catch_adds=True)
2538
# options can be spelt two different ways
2539
index.add_versions([('tip', 'fulltext,no-eol', (None, 0, 100), [])])
2540
index.add_versions([('tip', 'no-eol,fulltext', (None, 0, 100), [])])
2541
# but neither should have added data.
2542
self.assertEqual([[], []], self.caught_entries)
2544
def test_add_versions_different_dup(self):
2545
index = self.two_graph_index(catch_adds=True)
2547
self.assertRaises(errors.KnitCorrupt, index.add_versions,
2548
[('tip', 'no-eol,line-delta', (None, 0, 100), [])])
2549
self.assertRaises(errors.KnitCorrupt, index.add_versions,
2550
[('tip', 'line-delta,no-eol', (None, 0, 100), [])])
2551
self.assertRaises(errors.KnitCorrupt, index.add_versions,
2552
[('tip', 'fulltext', (None, 0, 100), [])])
2554
self.assertRaises(errors.KnitCorrupt, index.add_versions,
2555
[('tip', 'fulltext,no-eol', (None, 50, 100), [])])
2556
self.assertRaises(errors.KnitCorrupt, index.add_versions,
2557
[('tip', 'fulltext,no-eol', (None, 0, 1000), [])])
2559
self.assertRaises(errors.KnitCorrupt, index.add_versions,
2560
[('tip', 'fulltext,no-eol', (None, 0, 100), ['parent'])])
2561
# change options in the second record
2562
self.assertRaises(errors.KnitCorrupt, index.add_versions,
2563
[('tip', 'fulltext,no-eol', (None, 0, 100), []),
2564
('tip', 'no-eol,line-delta', (None, 0, 100), [])])
2565
self.assertEqual([], self.caught_entries)
2567
class TestPackKnits(KnitTests):
2568
"""Tests that use a _PackAccess and KnitGraphIndex."""
2570
def test_get_data_stream_packs_ignores_pack_overhead(self):
2571
# Packs have an encoding overhead that should not be included in the
2572
# 'size' field of a data stream, because it is not returned by the
2573
# raw_reading functions - it is why index_memo's are opaque, and
2574
# get_data_stream was abusing this.
2575
packname = 'test.pack'
2576
transport = self.get_transport()
2577
def write_data(bytes):
2578
transport.append_bytes(packname, bytes)
2579
writer = pack.ContainerWriter(write_data)
2581
index = InMemoryGraphIndex(2)
2582
knit_index = KnitGraphIndex(index, add_callback=index.add_nodes,
2584
indices = {index:(transport, packname)}
2585
access = _PackAccess(indices, writer=(writer, index))
2586
k = KnitVersionedFile('test', get_transport('.'),
2587
delta=True, create=True, index=knit_index, access_method=access)
2588
# insert something into the knit
2589
k.add_lines('text-1', [], ["foo\n"])
2590
# get a data stream for it
2591
stream = k.get_data_stream(['text-1'])
2592
# if the stream has been incorrectly assembled, we will get a short read
2593
# reading from the stream (as streams have no trailer)
2594
expected_length = stream[1][0][2]
2595
# we use -1 to do the read, so that if a trailer is added this test
2596
# will fail and we'll adjust it to handle that case correctly, rather
2597
# than allowing an over-read that is bogus.
2598
self.assertEqual(expected_length, len(stream[2](-1)))
2601
class Test_StreamIndex(KnitTests):
2603
def get_index(self, knit, stream):
2604
"""Get a _StreamIndex from knit and stream."""
2605
return knit._knit_from_datastream(stream)._index
2607
def assertIndexVersions(self, knit, versions):
2608
"""Check that the _StreamIndex versions are those of the stream."""
2609
index = self.get_index(knit, knit.get_data_stream(versions))
2610
self.assertEqual(set(index.get_versions()), set(versions))
2611
# check we didn't get duplicates
2612
self.assertEqual(len(index.get_versions()), len(versions))
2614
def assertIndexAncestry(self, knit, ancestry_versions, versions, result):
2615
"""Check the result of a get_ancestry call on knit."""
2616
index = self.get_index(knit, knit.get_data_stream(versions))
2619
set(index.get_ancestry(ancestry_versions, False)))
2621
def assertGetMethod(self, knit, versions, version, result):
2622
index = self.get_index(knit, knit.get_data_stream(versions))
2623
self.assertEqual(result, index.get_method(version))
2625
def assertGetOptions(self, knit, version, options):
2626
index = self.get_index(knit, knit.get_data_stream(version))
2627
self.assertEqual(options, index.get_options(version))
2629
def assertGetPosition(self, knit, versions, version, result):
2630
index = self.get_index(knit, knit.get_data_stream(versions))
2631
if result[1] is None:
2632
result = (result[0], index, result[2], result[3])
2633
self.assertEqual(result, index.get_position(version))
2635
def assertGetParentsWithGhosts(self, knit, versions, version, parents):
2636
index = self.get_index(knit, knit.get_data_stream(versions))
2637
self.assertEqual(parents, index.get_parents_with_ghosts(version))
2639
def make_knit_with_4_versions_2_dags(self):
2640
knit = self.make_test_knit()
2641
knit.add_lines('a', [], ["foo"])
2642
knit.add_lines('b', [], [])
2643
knit.add_lines('c', ['b', 'a'], [])
2644
knit.add_lines_with_ghosts('d', ['e', 'f'], [])
2647
def test_versions(self):
2648
"""The versions of a StreamIndex are those of the datastream."""
2649
knit = self.make_knit_with_4_versions_2_dags()
2650
# ask for most permutations, which catches bugs like falling back to the
2651
# target knit, or showing ghosts, etc.
2652
self.assertIndexVersions(knit, [])
2653
self.assertIndexVersions(knit, ['a'])
2654
self.assertIndexVersions(knit, ['b'])
2655
self.assertIndexVersions(knit, ['c'])
2656
self.assertIndexVersions(knit, ['d'])
2657
self.assertIndexVersions(knit, ['a', 'b'])
2658
self.assertIndexVersions(knit, ['b', 'c'])
2659
self.assertIndexVersions(knit, ['a', 'c'])
2660
self.assertIndexVersions(knit, ['a', 'b', 'c'])
2661
self.assertIndexVersions(knit, ['a', 'b', 'c', 'd'])
2663
def test_construct(self):
2664
"""Constructing a StreamIndex generates index data."""
2665
data_list = [('text-a', ['fulltext'], 127, []),
2666
('text-b', ['option'], 128, ['text-c'])]
2667
index = _StreamIndex(data_list, None)
2668
self.assertEqual({'text-a':(['fulltext'], (0, 127), []),
2669
'text-b':(['option'], (127, 127 + 128), ['text-c'])},
2672
def test_get_ancestry(self):
2673
knit = self.make_knit_with_4_versions_2_dags()
2674
self.assertIndexAncestry(knit, ['a'], ['a'], ['a'])
2675
self.assertIndexAncestry(knit, ['b'], ['b'], ['b'])
2676
self.assertIndexAncestry(knit, ['c'], ['c'], ['c'])
2677
self.assertIndexAncestry(knit, ['c'], ['a', 'b', 'c'],
2678
set(['a', 'b', 'c']))
2679
self.assertIndexAncestry(knit, ['c', 'd'], ['a', 'b', 'c', 'd'],
2680
set(['a', 'b', 'c', 'd']))
2682
def test_get_method(self):
2683
knit = self.make_knit_with_4_versions_2_dags()
2684
self.assertGetMethod(knit, ['a'], 'a', 'fulltext')
2685
self.assertGetMethod(knit, ['c'], 'c', 'line-delta')
2686
# get_method on a basis that is not in the datastream (but in the
2687
# backing knit) returns 'fulltext', because thats what we'll create as
2689
self.assertGetMethod(knit, ['c'], 'b', 'fulltext')
2691
def test_get_options(self):
2692
knit = self.make_knit_with_4_versions_2_dags()
2693
self.assertGetOptions(knit, 'a', ['no-eol', 'fulltext'])
2694
self.assertGetOptions(knit, 'c', ['line-delta'])
2696
def test_get_parents_with_ghosts(self):
2697
knit = self.make_knit_with_4_versions_2_dags()
2698
self.assertGetParentsWithGhosts(knit, ['a'], 'a', ())
2699
self.assertGetParentsWithGhosts(knit, ['c'], 'c', ('b', 'a'))
2700
self.assertGetParentsWithGhosts(knit, ['d'], 'd', ('e', 'f'))
2702
def test_get_position(self):
2703
knit = self.make_knit_with_4_versions_2_dags()
2704
# get_position returns (thunk_flag, index(can be None), start, end) for
2705
# _StreamAccess to use.
2706
self.assertGetPosition(knit, ['a'], 'a', (False, None, 0, 78))
2707
self.assertGetPosition(knit, ['a', 'c'], 'c', (False, None, 78, 156))
2708
# get_position on a text that is not in the datastream (but in the
2709
# backing knit) returns (True, 'versionid', None, None) - and then the
2710
# access object can construct the relevant data as needed.
2711
self.assertGetPosition(knit, ['a', 'c'], 'b', (True, 'b', None, None))
2714
class Test_StreamAccess(KnitTests):
2716
def get_index_access(self, knit, stream):
2717
"""Get a _StreamAccess from knit and stream."""
2718
knit = knit._knit_from_datastream(stream)
2719
return knit._index, knit._data._access
2721
def assertGetRawRecords(self, knit, versions):
2722
index, access = self.get_index_access(knit,
2723
knit.get_data_stream(versions))
2724
# check that every version asked for can be obtained from the resulting
2728
for version in versions:
2729
memos.append(knit._index.get_position(version))
2731
for version, data in zip(
2732
versions, knit._data._access.get_raw_records(memos)):
2733
original[version] = data
2735
for version in versions:
2736
memos.append(index.get_position(version))
2738
for version, data in zip(versions, access.get_raw_records(memos)):
2739
streamed[version] = data
2740
self.assertEqual(original, streamed)
2742
for version in versions:
2743
data = list(access.get_raw_records(
2744
[index.get_position(version)]))[0]
2745
self.assertEqual(original[version], data)
2747
def make_knit_with_two_versions(self):
2748
knit = self.make_test_knit()
2749
knit.add_lines('a', [], ["foo"])
2750
knit.add_lines('b', [], ["bar"])
2753
def test_get_raw_records(self):
2754
knit = self.make_knit_with_two_versions()
2755
self.assertGetRawRecords(knit, ['a', 'b'])
2756
self.assertGetRawRecords(knit, ['a'])
2757
self.assertGetRawRecords(knit, ['b'])
2759
def test_get_raw_record_from_backing_knit(self):
2760
# the thunk layer should create an artificial A on-demand when needed.
2761
source_knit = self.make_test_knit(name='plain', annotate=False)
2762
target_knit = self.make_test_knit(name='annotated', annotate=True)
2763
source_knit.add_lines("A", [], ["Foo\n"])
2764
# Give the target A, so we can try to thunk across to it.
2765
target_knit.join(source_knit)
2766
index, access = self.get_index_access(target_knit,
2767
source_knit.get_data_stream([]))
2768
raw_data = list(access.get_raw_records([(True, "A", None, None)]))[0]
2769
df = GzipFile(mode='rb', fileobj=StringIO(raw_data))
2771
'version A 1 5d36b88bb697a2d778f024048bafabd443d74503\n'
2775
def test_asking_for_thunk_stream_is_not_plain_errors(self):
2776
knit = self.make_test_knit(name='annotated', annotate=True)
2777
knit.add_lines("A", [], ["Foo\n"])
2778
index, access = self.get_index_access(knit,
2779
knit.get_data_stream([]))
2780
self.assertRaises(errors.KnitCorrupt,
2781
list, access.get_raw_records([(True, "A", None, None)]))
2784
class TestFormatSignatures(KnitTests):
2786
def test_knit_format_signatures(self):
2787
"""Different formats of knit have different signature strings."""
2788
knit = self.make_test_knit(name='a', annotate=True)
2789
self.assertEqual('knit-annotated', knit.get_format_signature())
2790
knit = self.make_test_knit(name='p', annotate=False)
2791
self.assertEqual('knit-plain', knit.get_format_signature())