124
273
return queue_call
276
class MockReadvFailingTransport(MockTransport):
277
"""Fail in the middle of a readv() result.
279
This Transport will successfully yield the first two requested hunks, but
280
raise NoSuchFile for the rest.
283
def readv(self, relpath, offsets):
285
for result in MockTransport.readv(self, relpath, offsets):
287
# we use 2 because the first offset is the pack header, the second
288
# is the first actual content requset
290
raise errors.NoSuchFile(relpath)
294
class KnitRecordAccessTestsMixin(object):
295
"""Tests for getting and putting knit records."""
297
def test_add_raw_records(self):
298
"""Add_raw_records adds records retrievable later."""
299
access = self.get_access()
300
memos = access.add_raw_records([('key', 10)], '1234567890')
301
self.assertEqual(['1234567890'], list(access.get_raw_records(memos)))
303
def test_add_several_raw_records(self):
304
"""add_raw_records with many records and read some back."""
305
access = self.get_access()
306
memos = access.add_raw_records([('key', 10), ('key2', 2), ('key3', 5)],
308
self.assertEqual(['1234567890', '12', '34567'],
309
list(access.get_raw_records(memos)))
310
self.assertEqual(['1234567890'],
311
list(access.get_raw_records(memos[0:1])))
312
self.assertEqual(['12'],
313
list(access.get_raw_records(memos[1:2])))
314
self.assertEqual(['34567'],
315
list(access.get_raw_records(memos[2:3])))
316
self.assertEqual(['1234567890', '34567'],
317
list(access.get_raw_records(memos[0:1] + memos[2:3])))
320
class TestKnitKnitAccess(TestCaseWithMemoryTransport, KnitRecordAccessTestsMixin):
321
"""Tests for the .kndx implementation."""
323
def get_access(self):
324
"""Get a .knit style access instance."""
325
mapper = ConstantMapper("foo")
326
access = _KnitKeyAccess(self.get_transport(), mapper)
330
class _TestException(Exception):
331
"""Just an exception for local tests to use."""
334
class TestPackKnitAccess(TestCaseWithMemoryTransport, KnitRecordAccessTestsMixin):
335
"""Tests for the pack based access."""
337
def get_access(self):
338
return self._get_access()[0]
340
def _get_access(self, packname='packfile', index='FOO'):
341
transport = self.get_transport()
342
def write_data(bytes):
343
transport.append_bytes(packname, bytes)
344
writer = pack.ContainerWriter(write_data)
346
access = _DirectPackAccess({})
347
access.set_writer(writer, index, (transport, packname))
348
return access, writer
350
def make_pack_file(self):
351
"""Create a pack file with 2 records."""
352
access, writer = self._get_access(packname='packname', index='foo')
354
memos.extend(access.add_raw_records([('key1', 10)], '1234567890'))
355
memos.extend(access.add_raw_records([('key2', 5)], '12345'))
359
def make_vf_for_retrying(self):
360
"""Create 3 packs and a reload function.
362
Originally, 2 pack files will have the data, but one will be missing.
363
And then the third will be used in place of the first two if reload()
366
:return: (versioned_file, reload_counter)
367
versioned_file a KnitVersionedFiles using the packs for access
369
tree = self.make_branch_and_memory_tree('tree')
371
self.addCleanup(tree.unlock)
372
tree.add([''], ['root-id'])
373
tree.commit('one', rev_id='rev-1')
374
tree.commit('two', rev_id='rev-2')
375
tree.commit('three', rev_id='rev-3')
376
# Pack these three revisions into another pack file, but don't remove
378
repo = tree.branch.repository
379
collection = repo._pack_collection
380
collection.ensure_loaded()
381
orig_packs = collection.packs
382
packer = pack_repo.Packer(collection, orig_packs, '.testpack')
383
new_pack = packer.pack()
384
# forget about the new pack
387
vf = tree.branch.repository.revisions
388
# Set up a reload() function that switches to using the new pack file
389
new_index = new_pack.revision_index
390
access_tuple = new_pack.access_tuple()
391
reload_counter = [0, 0, 0]
393
reload_counter[0] += 1
394
if reload_counter[1] > 0:
395
# We already reloaded, nothing more to do
396
reload_counter[2] += 1
398
reload_counter[1] += 1
399
vf._index._graph_index._indices[:] = [new_index]
400
vf._access._indices.clear()
401
vf._access._indices[new_index] = access_tuple
403
# Delete one of the pack files so the data will need to be reloaded. We
404
# will delete the file with 'rev-2' in it
405
trans, name = orig_packs[1].access_tuple()
407
# We don't have the index trigger reloading because we want to test
408
# that we reload when the .pack disappears
409
vf._access._reload_func = reload
410
return vf, reload_counter
412
def make_reload_func(self, return_val=True):
415
reload_called[0] += 1
417
return reload_called, reload
419
def make_retry_exception(self):
420
# We raise a real exception so that sys.exc_info() is properly
423
raise _TestException('foobar')
424
except _TestException, e:
425
retry_exc = errors.RetryWithNewPacks(None, reload_occurred=False,
426
exc_info=sys.exc_info())
429
def test_read_from_several_packs(self):
430
access, writer = self._get_access()
432
memos.extend(access.add_raw_records([('key', 10)], '1234567890'))
434
access, writer = self._get_access('pack2', 'FOOBAR')
435
memos.extend(access.add_raw_records([('key', 5)], '12345'))
437
access, writer = self._get_access('pack3', 'BAZ')
438
memos.extend(access.add_raw_records([('key', 5)], 'alpha'))
440
transport = self.get_transport()
441
access = _DirectPackAccess({"FOO":(transport, 'packfile'),
442
"FOOBAR":(transport, 'pack2'),
443
"BAZ":(transport, 'pack3')})
444
self.assertEqual(['1234567890', '12345', 'alpha'],
445
list(access.get_raw_records(memos)))
446
self.assertEqual(['1234567890'],
447
list(access.get_raw_records(memos[0:1])))
448
self.assertEqual(['12345'],
449
list(access.get_raw_records(memos[1:2])))
450
self.assertEqual(['alpha'],
451
list(access.get_raw_records(memos[2:3])))
452
self.assertEqual(['1234567890', 'alpha'],
453
list(access.get_raw_records(memos[0:1] + memos[2:3])))
455
def test_set_writer(self):
456
"""The writer should be settable post construction."""
457
access = _DirectPackAccess({})
458
transport = self.get_transport()
459
packname = 'packfile'
461
def write_data(bytes):
462
transport.append_bytes(packname, bytes)
463
writer = pack.ContainerWriter(write_data)
465
access.set_writer(writer, index, (transport, packname))
466
memos = access.add_raw_records([('key', 10)], '1234567890')
468
self.assertEqual(['1234567890'], list(access.get_raw_records(memos)))
470
def test_missing_index_raises_retry(self):
471
memos = self.make_pack_file()
472
transport = self.get_transport()
473
reload_called, reload_func = self.make_reload_func()
474
# Note that the index key has changed from 'foo' to 'bar'
475
access = _DirectPackAccess({'bar':(transport, 'packname')},
476
reload_func=reload_func)
477
e = self.assertListRaises(errors.RetryWithNewPacks,
478
access.get_raw_records, memos)
479
# Because a key was passed in which does not match our index list, we
480
# assume that the listing was already reloaded
481
self.assertTrue(e.reload_occurred)
482
self.assertIsInstance(e.exc_info, tuple)
483
self.assertIs(e.exc_info[0], KeyError)
484
self.assertIsInstance(e.exc_info[1], KeyError)
486
def test_missing_index_raises_key_error_with_no_reload(self):
487
memos = self.make_pack_file()
488
transport = self.get_transport()
489
# Note that the index key has changed from 'foo' to 'bar'
490
access = _DirectPackAccess({'bar':(transport, 'packname')})
491
e = self.assertListRaises(KeyError, access.get_raw_records, memos)
493
def test_missing_file_raises_retry(self):
494
memos = self.make_pack_file()
495
transport = self.get_transport()
496
reload_called, reload_func = self.make_reload_func()
497
# Note that the 'filename' has been changed to 'different-packname'
498
access = _DirectPackAccess({'foo':(transport, 'different-packname')},
499
reload_func=reload_func)
500
e = self.assertListRaises(errors.RetryWithNewPacks,
501
access.get_raw_records, memos)
502
# The file has gone missing, so we assume we need to reload
503
self.assertFalse(e.reload_occurred)
504
self.assertIsInstance(e.exc_info, tuple)
505
self.assertIs(e.exc_info[0], errors.NoSuchFile)
506
self.assertIsInstance(e.exc_info[1], errors.NoSuchFile)
507
self.assertEqual('different-packname', e.exc_info[1].path)
509
def test_missing_file_raises_no_such_file_with_no_reload(self):
510
memos = self.make_pack_file()
511
transport = self.get_transport()
512
# Note that the 'filename' has been changed to 'different-packname'
513
access = _DirectPackAccess({'foo':(transport, 'different-packname')})
514
e = self.assertListRaises(errors.NoSuchFile,
515
access.get_raw_records, memos)
517
def test_failing_readv_raises_retry(self):
518
memos = self.make_pack_file()
519
transport = self.get_transport()
520
failing_transport = MockReadvFailingTransport(
521
[transport.get_bytes('packname')])
522
reload_called, reload_func = self.make_reload_func()
523
access = _DirectPackAccess({'foo':(failing_transport, 'packname')},
524
reload_func=reload_func)
525
# Asking for a single record will not trigger the Mock failure
526
self.assertEqual(['1234567890'],
527
list(access.get_raw_records(memos[:1])))
528
self.assertEqual(['12345'],
529
list(access.get_raw_records(memos[1:2])))
530
# A multiple offset readv() will fail mid-way through
531
e = self.assertListRaises(errors.RetryWithNewPacks,
532
access.get_raw_records, memos)
533
# The file has gone missing, so we assume we need to reload
534
self.assertFalse(e.reload_occurred)
535
self.assertIsInstance(e.exc_info, tuple)
536
self.assertIs(e.exc_info[0], errors.NoSuchFile)
537
self.assertIsInstance(e.exc_info[1], errors.NoSuchFile)
538
self.assertEqual('packname', e.exc_info[1].path)
540
def test_failing_readv_raises_no_such_file_with_no_reload(self):
541
memos = self.make_pack_file()
542
transport = self.get_transport()
543
failing_transport = MockReadvFailingTransport(
544
[transport.get_bytes('packname')])
545
reload_called, reload_func = self.make_reload_func()
546
access = _DirectPackAccess({'foo':(failing_transport, 'packname')})
547
# Asking for a single record will not trigger the Mock failure
548
self.assertEqual(['1234567890'],
549
list(access.get_raw_records(memos[:1])))
550
self.assertEqual(['12345'],
551
list(access.get_raw_records(memos[1:2])))
552
# A multiple offset readv() will fail mid-way through
553
e = self.assertListRaises(errors.NoSuchFile,
554
access.get_raw_records, memos)
556
def test_reload_or_raise_no_reload(self):
557
access = _DirectPackAccess({}, reload_func=None)
558
retry_exc = self.make_retry_exception()
559
# Without a reload_func, we will just re-raise the original exception
560
self.assertRaises(_TestException, access.reload_or_raise, retry_exc)
562
def test_reload_or_raise_reload_changed(self):
563
reload_called, reload_func = self.make_reload_func(return_val=True)
564
access = _DirectPackAccess({}, reload_func=reload_func)
565
retry_exc = self.make_retry_exception()
566
access.reload_or_raise(retry_exc)
567
self.assertEqual([1], reload_called)
568
retry_exc.reload_occurred=True
569
access.reload_or_raise(retry_exc)
570
self.assertEqual([2], reload_called)
572
def test_reload_or_raise_reload_no_change(self):
573
reload_called, reload_func = self.make_reload_func(return_val=False)
574
access = _DirectPackAccess({}, reload_func=reload_func)
575
retry_exc = self.make_retry_exception()
576
# If reload_occurred is False, then we consider it an error to have
577
# reload_func() return False (no changes).
578
self.assertRaises(_TestException, access.reload_or_raise, retry_exc)
579
self.assertEqual([1], reload_called)
580
retry_exc.reload_occurred=True
581
# If reload_occurred is True, then we assume nothing changed because
582
# it had changed earlier, but didn't change again
583
access.reload_or_raise(retry_exc)
584
self.assertEqual([2], reload_called)
586
def test_annotate_retries(self):
587
vf, reload_counter = self.make_vf_for_retrying()
588
# It is a little bit bogus to annotate the Revision VF, but it works,
589
# as we have ancestry stored there
591
reload_lines = vf.annotate(key)
592
self.assertEqual([1, 1, 0], reload_counter)
593
plain_lines = vf.annotate(key)
594
self.assertEqual([1, 1, 0], reload_counter) # No extra reloading
595
if reload_lines != plain_lines:
596
self.fail('Annotation was not identical with reloading.')
597
# Now delete the packs-in-use, which should trigger another reload, but
598
# this time we just raise an exception because we can't recover
599
for trans, name in vf._access._indices.itervalues():
601
self.assertRaises(errors.NoSuchFile, vf.annotate, key)
602
self.assertEqual([2, 1, 1], reload_counter)
604
def test__get_record_map_retries(self):
605
vf, reload_counter = self.make_vf_for_retrying()
606
keys = [('rev-1',), ('rev-2',), ('rev-3',)]
607
records = vf._get_record_map(keys)
608
self.assertEqual(keys, sorted(records.keys()))
609
self.assertEqual([1, 1, 0], reload_counter)
610
# Now delete the packs-in-use, which should trigger another reload, but
611
# this time we just raise an exception because we can't recover
612
for trans, name in vf._access._indices.itervalues():
614
self.assertRaises(errors.NoSuchFile, vf._get_record_map, keys)
615
self.assertEqual([2, 1, 1], reload_counter)
617
def test_get_record_stream_retries(self):
618
vf, reload_counter = self.make_vf_for_retrying()
619
keys = [('rev-1',), ('rev-2',), ('rev-3',)]
620
record_stream = vf.get_record_stream(keys, 'topological', False)
621
record = record_stream.next()
622
self.assertEqual(('rev-1',), record.key)
623
self.assertEqual([0, 0, 0], reload_counter)
624
record = record_stream.next()
625
self.assertEqual(('rev-2',), record.key)
626
self.assertEqual([1, 1, 0], reload_counter)
627
record = record_stream.next()
628
self.assertEqual(('rev-3',), record.key)
629
self.assertEqual([1, 1, 0], reload_counter)
630
# Now delete all pack files, and see that we raise the right error
631
for trans, name in vf._access._indices.itervalues():
633
self.assertListRaises(errors.NoSuchFile,
634
vf.get_record_stream, keys, 'topological', False)
636
def test_iter_lines_added_or_present_in_keys_retries(self):
637
vf, reload_counter = self.make_vf_for_retrying()
638
keys = [('rev-1',), ('rev-2',), ('rev-3',)]
639
# Unfortunately, iter_lines_added_or_present_in_keys iterates the
640
# result in random order (determined by the iteration order from a
641
# set()), so we don't have any solid way to trigger whether data is
642
# read before or after. However we tried to delete the middle node to
643
# exercise the code well.
644
# What we care about is that all lines are always yielded, but not
647
reload_lines = sorted(vf.iter_lines_added_or_present_in_keys(keys))
648
self.assertEqual([1, 1, 0], reload_counter)
649
# Now do it again, to make sure the result is equivalent
650
plain_lines = sorted(vf.iter_lines_added_or_present_in_keys(keys))
651
self.assertEqual([1, 1, 0], reload_counter) # No extra reloading
652
self.assertEqual(plain_lines, reload_lines)
653
self.assertEqual(21, len(plain_lines))
654
# Now delete all pack files, and see that we raise the right error
655
for trans, name in vf._access._indices.itervalues():
657
self.assertListRaises(errors.NoSuchFile,
658
vf.iter_lines_added_or_present_in_keys, keys)
659
self.assertEqual([2, 1, 1], reload_counter)
661
def test_get_record_stream_yields_disk_sorted_order(self):
662
# if we get 'unordered' pick a semi-optimal order for reading. The
663
# order should be grouped by pack file, and then by position in file
664
repo = self.make_repository('test', format='pack-0.92')
666
self.addCleanup(repo.unlock)
667
repo.start_write_group()
669
vf.add_lines(('f-id', 'rev-5'), [('f-id', 'rev-4')], ['lines\n'])
670
vf.add_lines(('f-id', 'rev-1'), [], ['lines\n'])
671
vf.add_lines(('f-id', 'rev-2'), [('f-id', 'rev-1')], ['lines\n'])
672
repo.commit_write_group()
673
# We inserted them as rev-5, rev-1, rev-2, we should get them back in
675
stream = vf.get_record_stream([('f-id', 'rev-1'), ('f-id', 'rev-5'),
676
('f-id', 'rev-2')], 'unordered', False)
677
keys = [r.key for r in stream]
678
self.assertEqual([('f-id', 'rev-5'), ('f-id', 'rev-1'),
679
('f-id', 'rev-2')], keys)
680
repo.start_write_group()
681
vf.add_lines(('f-id', 'rev-4'), [('f-id', 'rev-3')], ['lines\n'])
682
vf.add_lines(('f-id', 'rev-3'), [('f-id', 'rev-2')], ['lines\n'])
683
vf.add_lines(('f-id', 'rev-6'), [('f-id', 'rev-5')], ['lines\n'])
684
repo.commit_write_group()
685
# Request in random order, to make sure the output order isn't based on
687
request_keys = set(('f-id', 'rev-%d' % i) for i in range(1, 7))
688
stream = vf.get_record_stream(request_keys, 'unordered', False)
689
keys = [r.key for r in stream]
690
# We want to get the keys back in disk order, but it doesn't matter
691
# which pack we read from first. So this can come back in 2 orders
692
alt1 = [('f-id', 'rev-%d' % i) for i in [4, 3, 6, 5, 1, 2]]
693
alt2 = [('f-id', 'rev-%d' % i) for i in [5, 1, 2, 4, 3, 6]]
694
if keys != alt1 and keys != alt2:
695
self.fail('Returned key order did not match either expected order.'
696
' expected %s or %s, not %s'
697
% (alt1, alt2, keys))
127
700
class LowLevelKnitDataTests(TestCase):
129
702
def create_gz_content(self, text):
228
849
# Change 2 bytes in the middle to \xff
229
850
gz_txt = gz_txt[:10] + '\xff\xff' + gz_txt[12:]
230
851
transport = MockTransport([gz_txt])
231
data = _KnitData(transport, 'filename', mode='r')
232
records = [('rev-id-1', 0, len(gz_txt))]
234
self.assertRaises(errors.KnitCorrupt, data.read_records, records)
236
# read_records_iter_raw will notice if we request the wrong version.
237
self.assertRaises(errors.KnitCorrupt, list,
238
data.read_records_iter_raw(records))
852
access = _KnitKeyAccess(transport, ConstantMapper('filename'))
853
knit = KnitVersionedFiles(None, access)
854
records = [(('rev-id-1',), (('rev-id-1',), 0, len(gz_txt)))]
855
self.assertRaises(errors.KnitCorrupt, list,
856
knit._read_records_iter(records))
857
# read_records_iter_raw will barf on bad gz data
858
self.assertRaises(errors.KnitCorrupt, list,
859
knit._read_records_iter_raw(records))
241
862
class LowLevelKnitIndexTests(TestCase):
243
def test_no_such_file(self):
244
transport = MockTransport()
246
self.assertRaises(NoSuchFile, _KnitIndex, transport, "filename", "r")
247
self.assertRaises(NoSuchFile, _KnitIndex, transport,
248
"filename", "w", create=False)
864
def get_knit_index(self, transport, name, mode):
865
mapper = ConstantMapper(name)
866
orig = knit._load_data
868
knit._load_data = orig
869
self.addCleanup(reset)
870
from bzrlib._knit_load_data_py import _load_data_py
871
knit._load_data = _load_data_py
872
allow_writes = lambda: 'w' in mode
873
return _KndxIndex(transport, mapper, lambda:None, allow_writes, lambda:True)
250
875
def test_create_file(self):
251
876
transport = MockTransport()
253
index = _KnitIndex(transport, "filename", "w",
254
file_mode="wb", create=True)
256
("put_bytes_non_atomic",
257
("filename", index.HEADER), {"mode": "wb"}),
258
transport.calls.pop(0))
260
def test_delay_create_file(self):
261
transport = MockTransport()
263
index = _KnitIndex(transport, "filename", "w",
264
create=True, file_mode="wb", create_parent_dir=True,
265
delay_create=True, dir_mode=0777)
266
self.assertEqual([], transport.calls)
268
index.add_versions([])
269
name, (filename, f), kwargs = transport.calls.pop(0)
270
self.assertEqual("put_file_non_atomic", name)
272
{"dir_mode": 0777, "create_parent_dir": True, "mode": "wb"},
274
self.assertEqual("filename", filename)
275
self.assertEqual(index.HEADER, f.read())
277
index.add_versions([])
278
self.assertEqual(("append_bytes", ("filename", ""), {}),
279
transport.calls.pop(0))
877
index = self.get_knit_index(transport, "filename", "w")
879
call = transport.calls.pop(0)
880
# call[1][1] is a StringIO - we can't test it by simple equality.
881
self.assertEqual('put_file_non_atomic', call[0])
882
self.assertEqual('filename.kndx', call[1][0])
883
# With no history, _KndxIndex writes a new index:
884
self.assertEqual(_KndxIndex.HEADER,
885
call[1][1].getvalue())
886
self.assertEqual({'create_parent_dir': True}, call[2])
281
888
def test_read_utf8_version_id(self):
282
889
unicode_revision_id = u"version-\N{CYRILLIC CAPITAL LETTER A}"
283
890
utf8_revision_id = unicode_revision_id.encode('utf-8')
284
891
transport = MockTransport([
286
893
'%s option 0 1 :' % (utf8_revision_id,)
288
index = _KnitIndex(transport, "filename", "r")
289
# _KnitIndex is a private class, and deals in utf8 revision_ids, not
895
index = self.get_knit_index(transport, "filename", "r")
896
# _KndxIndex is a private class, and deals in utf8 revision_ids, not
290
897
# Unicode revision_ids.
291
self.assertTrue(index.has_version(utf8_revision_id))
292
self.assertFalse(index.has_version(unicode_revision_id))
898
self.assertEqual({(utf8_revision_id,):()},
899
index.get_parent_map(index.keys()))
900
self.assertFalse((unicode_revision_id,) in index.keys())
294
902
def test_read_utf8_parents(self):
295
903
unicode_revision_id = u"version-\N{CYRILLIC CAPITAL LETTER A}"
296
904
utf8_revision_id = unicode_revision_id.encode('utf-8')
297
905
transport = MockTransport([
299
907
"version option 0 1 .%s :" % (utf8_revision_id,)
301
index = _KnitIndex(transport, "filename", "r")
302
self.assertEqual([utf8_revision_id],
303
index.get_parents_with_ghosts("version"))
909
index = self.get_knit_index(transport, "filename", "r")
910
self.assertEqual({("version",):((utf8_revision_id,),)},
911
index.get_parent_map(index.keys()))
305
913
def test_read_ignore_corrupted_lines(self):
306
914
transport = MockTransport([
309
917
"corrupted options 0 1 .b .c ",
310
918
"version options 0 1 :"
312
index = _KnitIndex(transport, "filename", "r")
313
self.assertEqual(1, index.num_versions())
314
self.assertTrue(index.has_version("version"))
920
index = self.get_knit_index(transport, "filename", "r")
921
self.assertEqual(1, len(index.keys()))
922
self.assertEqual(set([("version",)]), index.keys())
316
924
def test_read_corrupted_header(self):
317
925
transport = MockTransport(['not a bzr knit index header\n'])
318
self.assertRaises(KnitHeaderError,
319
_KnitIndex, transport, "filename", "r")
926
index = self.get_knit_index(transport, "filename", "r")
927
self.assertRaises(KnitHeaderError, index.keys)
321
929
def test_read_duplicate_entries(self):
322
930
transport = MockTransport([
324
932
"parent options 0 1 :",
325
933
"version options1 0 1 0 :",
326
934
"version options2 1 2 .other :",
327
935
"version options3 3 4 0 .other :"
329
index = _KnitIndex(transport, "filename", "r")
330
self.assertEqual(2, index.num_versions())
331
self.assertEqual(1, index.lookup("version"))
332
self.assertEqual((3, 4), index.get_position("version"))
333
self.assertEqual(["options3"], index.get_options("version"))
334
self.assertEqual(["parent", "other"],
335
index.get_parents_with_ghosts("version"))
937
index = self.get_knit_index(transport, "filename", "r")
938
self.assertEqual(2, len(index.keys()))
939
# check that the index used is the first one written. (Specific
940
# to KnitIndex style indices.
941
self.assertEqual("1", index._dictionary_compress([("version",)]))
942
self.assertEqual((("version",), 3, 4), index.get_position(("version",)))
943
self.assertEqual(["options3"], index.get_options(("version",)))
944
self.assertEqual({("version",):(("parent",), ("other",))},
945
index.get_parent_map([("version",)]))
337
947
def test_read_compressed_parents(self):
338
948
transport = MockTransport([
340
950
"a option 0 1 :",
341
951
"b option 0 1 0 :",
342
952
"c option 0 1 1 0 :",
344
index = _KnitIndex(transport, "filename", "r")
345
self.assertEqual(["a"], index.get_parents("b"))
346
self.assertEqual(["b", "a"], index.get_parents("c"))
954
index = self.get_knit_index(transport, "filename", "r")
955
self.assertEqual({("b",):(("a",),), ("c",):(("b",), ("a",))},
956
index.get_parent_map([("b",), ("c",)]))
348
958
def test_write_utf8_version_id(self):
349
959
unicode_revision_id = u"version-\N{CYRILLIC CAPITAL LETTER A}"
350
960
utf8_revision_id = unicode_revision_id.encode('utf-8')
351
961
transport = MockTransport([
354
index = _KnitIndex(transport, "filename", "r")
355
index.add_version(utf8_revision_id, ["option"], 0, 1, [])
356
self.assertEqual(("append_bytes", ("filename",
357
"\n%s option 0 1 :" % (utf8_revision_id,)),
359
transport.calls.pop(0))
964
index = self.get_knit_index(transport, "filename", "r")
966
((utf8_revision_id,), ["option"], ((utf8_revision_id,), 0, 1), [])])
967
call = transport.calls.pop(0)
968
# call[1][1] is a StringIO - we can't test it by simple equality.
969
self.assertEqual('put_file_non_atomic', call[0])
970
self.assertEqual('filename.kndx', call[1][0])
971
# With no history, _KndxIndex writes a new index:
972
self.assertEqual(_KndxIndex.HEADER +
973
"\n%s option 0 1 :" % (utf8_revision_id,),
974
call[1][1].getvalue())
975
self.assertEqual({'create_parent_dir': True}, call[2])
361
977
def test_write_utf8_parents(self):
362
978
unicode_revision_id = u"version-\N{CYRILLIC CAPITAL LETTER A}"
363
979
utf8_revision_id = unicode_revision_id.encode('utf-8')
364
980
transport = MockTransport([
367
index = _KnitIndex(transport, "filename", "r")
368
index.add_version("version", ["option"], 0, 1, [utf8_revision_id])
369
self.assertEqual(("append_bytes", ("filename",
370
"\nversion option 0 1 .%s :" % (utf8_revision_id,)),
372
transport.calls.pop(0))
374
def test_get_graph(self):
375
transport = MockTransport()
376
index = _KnitIndex(transport, "filename", "w", create=True)
377
self.assertEqual([], index.get_graph())
379
index.add_version("a", ["option"], 0, 1, ["b"])
380
self.assertEqual([("a", ["b"])], index.get_graph())
382
index.add_version("c", ["option"], 0, 1, ["d"])
383
self.assertEqual([("a", ["b"]), ("c", ["d"])],
384
sorted(index.get_graph()))
386
def test_get_ancestry(self):
387
transport = MockTransport([
390
"b option 0 1 0 .e :",
391
"c option 0 1 1 0 :",
392
"d option 0 1 2 .f :"
394
index = _KnitIndex(transport, "filename", "r")
396
self.assertEqual([], index.get_ancestry([]))
397
self.assertEqual(["a"], index.get_ancestry(["a"]))
398
self.assertEqual(["a", "b"], index.get_ancestry(["b"]))
399
self.assertEqual(["a", "b", "c"], index.get_ancestry(["c"]))
400
self.assertEqual(["a", "b", "c", "d"], index.get_ancestry(["d"]))
401
self.assertEqual(["a", "b"], index.get_ancestry(["a", "b"]))
402
self.assertEqual(["a", "b", "c"], index.get_ancestry(["a", "c"]))
404
self.assertRaises(RevisionNotPresent, index.get_ancestry, ["e"])
406
def test_get_ancestry_with_ghosts(self):
407
transport = MockTransport([
410
"b option 0 1 0 .e :",
411
"c option 0 1 0 .f .g :",
412
"d option 0 1 2 .h .j .k :"
414
index = _KnitIndex(transport, "filename", "r")
416
self.assertEqual([], index.get_ancestry_with_ghosts([]))
417
self.assertEqual(["a"], index.get_ancestry_with_ghosts(["a"]))
418
self.assertEqual(["a", "e", "b"],
419
index.get_ancestry_with_ghosts(["b"]))
420
self.assertEqual(["a", "g", "f", "c"],
421
index.get_ancestry_with_ghosts(["c"]))
422
self.assertEqual(["a", "g", "f", "c", "k", "j", "h", "d"],
423
index.get_ancestry_with_ghosts(["d"]))
424
self.assertEqual(["a", "e", "b"],
425
index.get_ancestry_with_ghosts(["a", "b"]))
426
self.assertEqual(["a", "g", "f", "c"],
427
index.get_ancestry_with_ghosts(["a", "c"]))
983
index = self.get_knit_index(transport, "filename", "r")
985
(("version",), ["option"], (("version",), 0, 1), [(utf8_revision_id,)])])
986
call = transport.calls.pop(0)
987
# call[1][1] is a StringIO - we can't test it by simple equality.
988
self.assertEqual('put_file_non_atomic', call[0])
989
self.assertEqual('filename.kndx', call[1][0])
990
# With no history, _KndxIndex writes a new index:
991
self.assertEqual(_KndxIndex.HEADER +
992
"\nversion option 0 1 .%s :" % (utf8_revision_id,),
993
call[1][1].getvalue())
994
self.assertEqual({'create_parent_dir': True}, call[2])
997
transport = MockTransport([
1000
index = self.get_knit_index(transport, "filename", "r")
1002
self.assertEqual(set(), index.keys())
1004
index.add_records([(("a",), ["option"], (("a",), 0, 1), [])])
1005
self.assertEqual(set([("a",)]), index.keys())
1007
index.add_records([(("a",), ["option"], (("a",), 0, 1), [])])
1008
self.assertEqual(set([("a",)]), index.keys())
1010
index.add_records([(("b",), ["option"], (("b",), 0, 1), [])])
1011
self.assertEqual(set([("a",), ("b",)]), index.keys())
1013
def add_a_b(self, index, random_id=None):
1015
if random_id is not None:
1016
kwargs["random_id"] = random_id
1018
(("a",), ["option"], (("a",), 0, 1), [("b",)]),
1019
(("a",), ["opt"], (("a",), 1, 2), [("c",)]),
1020
(("b",), ["option"], (("b",), 2, 3), [("a",)])
1023
def assertIndexIsAB(self, index):
1028
index.get_parent_map(index.keys()))
1029
self.assertEqual((("a",), 1, 2), index.get_position(("a",)))
1030
self.assertEqual((("b",), 2, 3), index.get_position(("b",)))
1031
self.assertEqual(["opt"], index.get_options(("a",)))
1033
def test_add_versions(self):
1034
transport = MockTransport([
1037
index = self.get_knit_index(transport, "filename", "r")
1040
call = transport.calls.pop(0)
1041
# call[1][1] is a StringIO - we can't test it by simple equality.
1042
self.assertEqual('put_file_non_atomic', call[0])
1043
self.assertEqual('filename.kndx', call[1][0])
1044
# With no history, _KndxIndex writes a new index:
428
1045
self.assertEqual(
429
["a", "g", "f", "c", "e", "b", "k", "j", "h", "d"],
430
index.get_ancestry_with_ghosts(["b", "d"]))
432
self.assertRaises(RevisionNotPresent,
433
index.get_ancestry_with_ghosts, ["e"])
435
def test_num_versions(self):
436
transport = MockTransport([
439
index = _KnitIndex(transport, "filename", "r")
441
self.assertEqual(0, index.num_versions())
442
self.assertEqual(0, len(index))
444
index.add_version("a", ["option"], 0, 1, [])
445
self.assertEqual(1, index.num_versions())
446
self.assertEqual(1, len(index))
448
index.add_version("a", ["option2"], 1, 2, [])
449
self.assertEqual(1, index.num_versions())
450
self.assertEqual(1, len(index))
452
index.add_version("b", ["option"], 0, 1, [])
453
self.assertEqual(2, index.num_versions())
454
self.assertEqual(2, len(index))
456
def test_get_versions(self):
457
transport = MockTransport([
460
index = _KnitIndex(transport, "filename", "r")
462
self.assertEqual([], index.get_versions())
464
index.add_version("a", ["option"], 0, 1, [])
465
self.assertEqual(["a"], index.get_versions())
467
index.add_version("a", ["option"], 0, 1, [])
468
self.assertEqual(["a"], index.get_versions())
470
index.add_version("b", ["option"], 0, 1, [])
471
self.assertEqual(["a", "b"], index.get_versions())
473
def test_idx_to_name(self):
474
transport = MockTransport([
479
index = _KnitIndex(transport, "filename", "r")
481
self.assertEqual("a", index.idx_to_name(0))
482
self.assertEqual("b", index.idx_to_name(1))
483
self.assertEqual("b", index.idx_to_name(-1))
484
self.assertEqual("a", index.idx_to_name(-2))
486
def test_lookup(self):
487
transport = MockTransport([
492
index = _KnitIndex(transport, "filename", "r")
494
self.assertEqual(0, index.lookup("a"))
495
self.assertEqual(1, index.lookup("b"))
497
def test_add_version(self):
498
transport = MockTransport([
501
index = _KnitIndex(transport, "filename", "r")
503
index.add_version("a", ["option"], 0, 1, ["b"])
504
self.assertEqual(("append_bytes",
505
("filename", "\na option 0 1 .b :"),
506
{}), transport.calls.pop(0))
507
self.assertTrue(index.has_version("a"))
508
self.assertEqual(1, index.num_versions())
509
self.assertEqual((0, 1), index.get_position("a"))
510
self.assertEqual(["option"], index.get_options("a"))
511
self.assertEqual(["b"], index.get_parents_with_ghosts("a"))
513
index.add_version("a", ["opt"], 1, 2, ["c"])
514
self.assertEqual(("append_bytes",
515
("filename", "\na opt 1 2 .c :"),
516
{}), transport.calls.pop(0))
517
self.assertTrue(index.has_version("a"))
518
self.assertEqual(1, index.num_versions())
519
self.assertEqual((1, 2), index.get_position("a"))
520
self.assertEqual(["opt"], index.get_options("a"))
521
self.assertEqual(["c"], index.get_parents_with_ghosts("a"))
523
index.add_version("b", ["option"], 2, 3, ["a"])
524
self.assertEqual(("append_bytes",
525
("filename", "\nb option 2 3 0 :"),
526
{}), transport.calls.pop(0))
527
self.assertTrue(index.has_version("b"))
528
self.assertEqual(2, index.num_versions())
529
self.assertEqual((2, 3), index.get_position("b"))
530
self.assertEqual(["option"], index.get_options("b"))
531
self.assertEqual(["a"], index.get_parents_with_ghosts("b"))
533
def test_add_versions(self):
534
transport = MockTransport([
537
index = _KnitIndex(transport, "filename", "r")
540
("a", ["option"], 0, 1, ["b"]),
541
("a", ["opt"], 1, 2, ["c"]),
542
("b", ["option"], 2, 3, ["a"])
544
self.assertEqual(("append_bytes", ("filename",
545
1047
"\na option 0 1 .b :"
546
1048
"\na opt 1 2 .c :"
548
), {}), transport.calls.pop(0))
549
self.assertTrue(index.has_version("a"))
550
self.assertTrue(index.has_version("b"))
551
self.assertEqual(2, index.num_versions())
552
self.assertEqual((1, 2), index.get_position("a"))
553
self.assertEqual((2, 3), index.get_position("b"))
554
self.assertEqual(["opt"], index.get_options("a"))
555
self.assertEqual(["option"], index.get_options("b"))
556
self.assertEqual(["c"], index.get_parents_with_ghosts("a"))
557
self.assertEqual(["a"], index.get_parents_with_ghosts("b"))
1049
"\nb option 2 3 0 :",
1050
call[1][1].getvalue())
1051
self.assertEqual({'create_parent_dir': True}, call[2])
1052
self.assertIndexIsAB(index)
1054
def test_add_versions_random_id_is_accepted(self):
1055
transport = MockTransport([
1058
index = self.get_knit_index(transport, "filename", "r")
1059
self.add_a_b(index, random_id=True)
559
1061
def test_delay_create_and_add_versions(self):
560
1062
transport = MockTransport()
562
index = _KnitIndex(transport, "filename", "w",
563
create=True, file_mode="wb", create_parent_dir=True,
564
delay_create=True, dir_mode=0777)
1064
index = self.get_knit_index(transport, "filename", "w")
565
1066
self.assertEqual([], transport.calls)
568
("a", ["option"], 0, 1, ["b"]),
569
("a", ["opt"], 1, 2, ["c"]),
570
("b", ["option"], 2, 3, ["a"])
572
name, (filename, f), kwargs = transport.calls.pop(0)
573
self.assertEqual("put_file_non_atomic", name)
575
{"dir_mode": 0777, "create_parent_dir": True, "mode": "wb"},
577
self.assertEqual("filename", filename)
1069
#[ {"dir_mode": 0777, "create_parent_dir": True, "mode": "wb"},
1071
# Two calls: one during which we load the existing index (and when its
1072
# missing create it), then a second where we write the contents out.
1073
self.assertEqual(2, len(transport.calls))
1074
call = transport.calls.pop(0)
1075
self.assertEqual('put_file_non_atomic', call[0])
1076
self.assertEqual('filename.kndx', call[1][0])
1077
# With no history, _KndxIndex writes a new index:
1078
self.assertEqual(_KndxIndex.HEADER, call[1][1].getvalue())
1079
self.assertEqual({'create_parent_dir': True}, call[2])
1080
call = transport.calls.pop(0)
1081
# call[1][1] is a StringIO - we can't test it by simple equality.
1082
self.assertEqual('put_file_non_atomic', call[0])
1083
self.assertEqual('filename.kndx', call[1][0])
1084
# With no history, _KndxIndex writes a new index:
580
1087
"\na option 0 1 .b :"
581
1088
"\na opt 1 2 .c :"
582
1089
"\nb option 2 3 0 :",
585
def test_has_version(self):
586
transport = MockTransport([
590
index = _KnitIndex(transport, "filename", "r")
592
self.assertTrue(index.has_version("a"))
593
self.assertFalse(index.has_version("b"))
1090
call[1][1].getvalue())
1091
self.assertEqual({'create_parent_dir': True}, call[2])
1093
def assertTotalBuildSize(self, size, keys, positions):
1094
self.assertEqual(size,
1095
knit._get_total_build_size(None, keys, positions))
1097
def test__get_total_build_size(self):
1099
('a',): (('fulltext', False), (('a',), 0, 100), None),
1100
('b',): (('line-delta', False), (('b',), 100, 21), ('a',)),
1101
('c',): (('line-delta', False), (('c',), 121, 35), ('b',)),
1102
('d',): (('line-delta', False), (('d',), 156, 12), ('b',)),
1104
self.assertTotalBuildSize(100, [('a',)], positions)
1105
self.assertTotalBuildSize(121, [('b',)], positions)
1106
# c needs both a & b
1107
self.assertTotalBuildSize(156, [('c',)], positions)
1108
# we shouldn't count 'b' twice
1109
self.assertTotalBuildSize(156, [('b',), ('c',)], positions)
1110
self.assertTotalBuildSize(133, [('d',)], positions)
1111
self.assertTotalBuildSize(168, [('c',), ('d',)], positions)
595
1113
def test_get_position(self):
596
1114
transport = MockTransport([
598
1116
"a option 0 1 :",
599
1117
"b option 1 2 :"
601
index = _KnitIndex(transport, "filename", "r")
1119
index = self.get_knit_index(transport, "filename", "r")
603
self.assertEqual((0, 1), index.get_position("a"))
604
self.assertEqual((1, 2), index.get_position("b"))
1121
self.assertEqual((("a",), 0, 1), index.get_position(("a",)))
1122
self.assertEqual((("b",), 1, 2), index.get_position(("b",)))
606
1124
def test_get_method(self):
607
1125
transport = MockTransport([
609
1127
"a fulltext,unknown 0 1 :",
610
1128
"b unknown,line-delta 1 2 :",
613
index = _KnitIndex(transport, "filename", "r")
1131
index = self.get_knit_index(transport, "filename", "r")
615
1133
self.assertEqual("fulltext", index.get_method("a"))
616
1134
self.assertEqual("line-delta", index.get_method("b"))
619
1137
def test_get_options(self):
620
1138
transport = MockTransport([
623
1141
"b opt2,opt3 1 2 :"
625
index = _KnitIndex(transport, "filename", "r")
1143
index = self.get_knit_index(transport, "filename", "r")
627
1145
self.assertEqual(["opt1"], index.get_options("a"))
628
1146
self.assertEqual(["opt2", "opt3"], index.get_options("b"))
630
def test_get_parents(self):
631
transport = MockTransport([
634
"b option 1 2 0 .c :",
635
"c option 1 2 1 0 .e :"
637
index = _KnitIndex(transport, "filename", "r")
639
self.assertEqual([], index.get_parents("a"))
640
self.assertEqual(["a", "c"], index.get_parents("b"))
641
self.assertEqual(["b", "a"], index.get_parents("c"))
643
def test_get_parents_with_ghosts(self):
644
transport = MockTransport([
647
"b option 1 2 0 .c :",
648
"c option 1 2 1 0 .e :"
650
index = _KnitIndex(transport, "filename", "r")
652
self.assertEqual([], index.get_parents_with_ghosts("a"))
653
self.assertEqual(["a", "c"], index.get_parents_with_ghosts("b"))
654
self.assertEqual(["b", "a", "e"],
655
index.get_parents_with_ghosts("c"))
657
def test_check_versions_present(self):
658
transport = MockTransport([
663
index = _KnitIndex(transport, "filename", "r")
665
check = index.check_versions_present
671
self.assertRaises(RevisionNotPresent, check, ["c"])
672
self.assertRaises(RevisionNotPresent, check, ["a", "b", "c"])
1148
def test_get_parent_map(self):
1149
transport = MockTransport([
1152
"b option 1 2 0 .c :",
1153
"c option 1 2 1 0 .e :"
1155
index = self.get_knit_index(transport, "filename", "r")
1159
("b",):(("a",), ("c",)),
1160
("c",):(("b",), ("a",), ("e",)),
1161
}, index.get_parent_map(index.keys()))
1163
def test_impossible_parent(self):
1164
"""Test we get KnitCorrupt if the parent couldn't possibly exist."""
1165
transport = MockTransport([
1168
"b option 0 1 4 :" # We don't have a 4th record
1170
index = self.get_knit_index(transport, 'filename', 'r')
1172
self.assertRaises(errors.KnitCorrupt, index.keys)
1173
except TypeError, e:
1174
if (str(e) == ('exceptions must be strings, classes, or instances,'
1175
' not exceptions.IndexError')
1176
and sys.version_info[0:2] >= (2,5)):
1177
self.knownFailure('Pyrex <0.9.5 fails with TypeError when'
1178
' raising new style exceptions with python'
1183
def test_corrupted_parent(self):
1184
transport = MockTransport([
1188
"c option 0 1 1v :", # Can't have a parent of '1v'
1190
index = self.get_knit_index(transport, 'filename', 'r')
1192
self.assertRaises(errors.KnitCorrupt, index.keys)
1193
except TypeError, e:
1194
if (str(e) == ('exceptions must be strings, classes, or instances,'
1195
' not exceptions.ValueError')
1196
and sys.version_info[0:2] >= (2,5)):
1197
self.knownFailure('Pyrex <0.9.5 fails with TypeError when'
1198
' raising new style exceptions with python'
1203
def test_corrupted_parent_in_list(self):
1204
transport = MockTransport([
1208
"c option 0 1 1 v :", # Can't have a parent of 'v'
1210
index = self.get_knit_index(transport, 'filename', 'r')
1212
self.assertRaises(errors.KnitCorrupt, index.keys)
1213
except TypeError, e:
1214
if (str(e) == ('exceptions must be strings, classes, or instances,'
1215
' not exceptions.ValueError')
1216
and sys.version_info[0:2] >= (2,5)):
1217
self.knownFailure('Pyrex <0.9.5 fails with TypeError when'
1218
' raising new style exceptions with python'
1223
def test_invalid_position(self):
1224
transport = MockTransport([
1228
index = self.get_knit_index(transport, 'filename', 'r')
1230
self.assertRaises(errors.KnitCorrupt, index.keys)
1231
except TypeError, e:
1232
if (str(e) == ('exceptions must be strings, classes, or instances,'
1233
' not exceptions.ValueError')
1234
and sys.version_info[0:2] >= (2,5)):
1235
self.knownFailure('Pyrex <0.9.5 fails with TypeError when'
1236
' raising new style exceptions with python'
1241
def test_invalid_size(self):
1242
transport = MockTransport([
1246
index = self.get_knit_index(transport, 'filename', 'r')
1248
self.assertRaises(errors.KnitCorrupt, index.keys)
1249
except TypeError, e:
1250
if (str(e) == ('exceptions must be strings, classes, or instances,'
1251
' not exceptions.ValueError')
1252
and sys.version_info[0:2] >= (2,5)):
1253
self.knownFailure('Pyrex <0.9.5 fails with TypeError when'
1254
' raising new style exceptions with python'
1259
def test_scan_unvalidated_index_not_implemented(self):
1260
transport = MockTransport()
1261
index = self.get_knit_index(transport, 'filename', 'r')
1263
NotImplementedError, index.scan_unvalidated_index,
1264
'dummy graph_index')
1266
NotImplementedError, index.get_missing_compression_parents)
1268
def test_short_line(self):
1269
transport = MockTransport([
1272
"b option 10 10 0", # This line isn't terminated, ignored
1274
index = self.get_knit_index(transport, "filename", "r")
1275
self.assertEqual(set([('a',)]), index.keys())
1277
def test_skip_incomplete_record(self):
1278
# A line with bogus data should just be skipped
1279
transport = MockTransport([
1282
"b option 10 10 0", # This line isn't terminated, ignored
1283
"c option 20 10 0 :", # Properly terminated, and starts with '\n'
1285
index = self.get_knit_index(transport, "filename", "r")
1286
self.assertEqual(set([('a',), ('c',)]), index.keys())
1288
def test_trailing_characters(self):
1289
# A line with bogus data should just be skipped
1290
transport = MockTransport([
1293
"b option 10 10 0 :a", # This line has extra trailing characters
1294
"c option 20 10 0 :", # Properly terminated, and starts with '\n'
1296
index = self.get_knit_index(transport, "filename", "r")
1297
self.assertEqual(set([('a',), ('c',)]), index.keys())
1300
class LowLevelKnitIndexTests_c(LowLevelKnitIndexTests):
1302
_test_needs_features = [CompiledKnitFeature]
1304
def get_knit_index(self, transport, name, mode):
1305
mapper = ConstantMapper(name)
1306
orig = knit._load_data
1308
knit._load_data = orig
1309
self.addCleanup(reset)
1310
from bzrlib._knit_load_data_c import _load_data_c
1311
knit._load_data = _load_data_c
1312
allow_writes = lambda: mode == 'w'
1313
return _KndxIndex(transport, mapper, lambda:None, allow_writes, lambda:True)
675
1316
class KnitTests(TestCaseWithTransport):
676
1317
"""Class containing knit test helper routines."""
678
def make_test_knit(self, annotate=False, delay_create=False):
680
factory = KnitPlainFactory()
683
return KnitVersionedFile('test', get_transport('.'), access_mode='w',
684
factory=factory, create=True,
685
delay_create=delay_create)
688
class BasicKnitTests(KnitTests):
690
def add_stock_one_and_one_a(self, k):
691
k.add_lines('text-1', [], split_lines(TEXT_1))
692
k.add_lines('text-1a', ['text-1'], split_lines(TEXT_1A))
694
def test_knit_constructor(self):
695
"""Construct empty k"""
696
self.make_test_knit()
698
def test_knit_add(self):
699
"""Store one text in knit and retrieve"""
700
k = self.make_test_knit()
701
k.add_lines('text-1', [], split_lines(TEXT_1))
702
self.assertTrue(k.has_version('text-1'))
703
self.assertEqualDiff(''.join(k.get_lines('text-1')), TEXT_1)
705
def test_knit_reload(self):
706
# test that the content in a reloaded knit is correct
707
k = self.make_test_knit()
708
k.add_lines('text-1', [], split_lines(TEXT_1))
710
k2 = KnitVersionedFile('test', get_transport('.'), access_mode='r', factory=KnitPlainFactory(), create=True)
711
self.assertTrue(k2.has_version('text-1'))
712
self.assertEqualDiff(''.join(k2.get_lines('text-1')), TEXT_1)
714
def test_knit_several(self):
715
"""Store several texts in a knit"""
716
k = self.make_test_knit()
717
k.add_lines('text-1', [], split_lines(TEXT_1))
718
k.add_lines('text-2', [], split_lines(TEXT_2))
719
self.assertEqualDiff(''.join(k.get_lines('text-1')), TEXT_1)
720
self.assertEqualDiff(''.join(k.get_lines('text-2')), TEXT_2)
722
def test_repeated_add(self):
723
"""Knit traps attempt to replace existing version"""
724
k = self.make_test_knit()
725
k.add_lines('text-1', [], split_lines(TEXT_1))
726
self.assertRaises(RevisionAlreadyPresent,
728
'text-1', [], split_lines(TEXT_1))
730
def test_empty(self):
731
k = self.make_test_knit(True)
732
k.add_lines('text-1', [], [])
733
self.assertEquals(k.get_lines('text-1'), [])
735
def test_incomplete(self):
736
"""Test if texts without a ending line-end can be inserted and
738
k = KnitVersionedFile('test', get_transport('.'), delta=False, create=True)
739
k.add_lines('text-1', [], ['a\n', 'b' ])
740
k.add_lines('text-2', ['text-1'], ['a\rb\n', 'b\n'])
741
# reopening ensures maximum room for confusion
742
k = KnitVersionedFile('test', get_transport('.'), delta=False, create=True)
743
self.assertEquals(k.get_lines('text-1'), ['a\n', 'b' ])
744
self.assertEquals(k.get_lines('text-2'), ['a\rb\n', 'b\n'])
746
def test_delta(self):
747
"""Expression of knit delta as lines"""
748
k = self.make_test_knit()
749
td = list(line_delta(TEXT_1.splitlines(True),
750
TEXT_1A.splitlines(True)))
751
self.assertEqualDiff(''.join(td), delta_1_1a)
752
out = apply_line_delta(TEXT_1.splitlines(True), td)
753
self.assertEqualDiff(''.join(out), TEXT_1A)
755
def test_add_with_parents(self):
756
"""Store in knit with parents"""
757
k = self.make_test_knit()
758
self.add_stock_one_and_one_a(k)
759
self.assertEquals(k.get_parents('text-1'), [])
760
self.assertEquals(k.get_parents('text-1a'), ['text-1'])
762
def test_ancestry(self):
763
"""Store in knit with parents"""
764
k = self.make_test_knit()
765
self.add_stock_one_and_one_a(k)
766
self.assertEquals(set(k.get_ancestry(['text-1a'])), set(['text-1a', 'text-1']))
768
def test_add_delta(self):
769
"""Store in knit with parents"""
770
k = KnitVersionedFile('test', get_transport('.'), factory=KnitPlainFactory(),
771
delta=True, create=True)
772
self.add_stock_one_and_one_a(k)
774
self.assertEqualDiff(''.join(k.get_lines('text-1a')), TEXT_1A)
776
def test_annotate(self):
778
k = KnitVersionedFile('knit', get_transport('.'), factory=KnitAnnotateFactory(),
779
delta=True, create=True)
780
self.insert_and_test_small_annotate(k)
782
def insert_and_test_small_annotate(self, k):
783
"""test annotation with k works correctly."""
784
k.add_lines('text-1', [], ['a\n', 'b\n'])
785
k.add_lines('text-2', ['text-1'], ['a\n', 'c\n'])
787
origins = k.annotate('text-2')
788
self.assertEquals(origins[0], ('text-1', 'a\n'))
789
self.assertEquals(origins[1], ('text-2', 'c\n'))
791
def test_annotate_fulltext(self):
793
k = KnitVersionedFile('knit', get_transport('.'), factory=KnitAnnotateFactory(),
794
delta=False, create=True)
795
self.insert_and_test_small_annotate(k)
797
def test_annotate_merge_1(self):
798
k = self.make_test_knit(True)
799
k.add_lines('text-a1', [], ['a\n', 'b\n'])
800
k.add_lines('text-a2', [], ['d\n', 'c\n'])
801
k.add_lines('text-am', ['text-a1', 'text-a2'], ['d\n', 'b\n'])
802
origins = k.annotate('text-am')
803
self.assertEquals(origins[0], ('text-a2', 'd\n'))
804
self.assertEquals(origins[1], ('text-a1', 'b\n'))
806
def test_annotate_merge_2(self):
807
k = self.make_test_knit(True)
808
k.add_lines('text-a1', [], ['a\n', 'b\n', 'c\n'])
809
k.add_lines('text-a2', [], ['x\n', 'y\n', 'z\n'])
810
k.add_lines('text-am', ['text-a1', 'text-a2'], ['a\n', 'y\n', 'c\n'])
811
origins = k.annotate('text-am')
812
self.assertEquals(origins[0], ('text-a1', 'a\n'))
813
self.assertEquals(origins[1], ('text-a2', 'y\n'))
814
self.assertEquals(origins[2], ('text-a1', 'c\n'))
816
def test_annotate_merge_9(self):
817
k = self.make_test_knit(True)
818
k.add_lines('text-a1', [], ['a\n', 'b\n', 'c\n'])
819
k.add_lines('text-a2', [], ['x\n', 'y\n', 'z\n'])
820
k.add_lines('text-am', ['text-a1', 'text-a2'], ['k\n', 'y\n', 'c\n'])
821
origins = k.annotate('text-am')
822
self.assertEquals(origins[0], ('text-am', 'k\n'))
823
self.assertEquals(origins[1], ('text-a2', 'y\n'))
824
self.assertEquals(origins[2], ('text-a1', 'c\n'))
826
def test_annotate_merge_3(self):
827
k = self.make_test_knit(True)
828
k.add_lines('text-a1', [], ['a\n', 'b\n', 'c\n'])
829
k.add_lines('text-a2', [] ,['x\n', 'y\n', 'z\n'])
830
k.add_lines('text-am', ['text-a1', 'text-a2'], ['k\n', 'y\n', 'z\n'])
831
origins = k.annotate('text-am')
832
self.assertEquals(origins[0], ('text-am', 'k\n'))
833
self.assertEquals(origins[1], ('text-a2', 'y\n'))
834
self.assertEquals(origins[2], ('text-a2', 'z\n'))
836
def test_annotate_merge_4(self):
837
k = self.make_test_knit(True)
838
k.add_lines('text-a1', [], ['a\n', 'b\n', 'c\n'])
839
k.add_lines('text-a2', [], ['x\n', 'y\n', 'z\n'])
840
k.add_lines('text-a3', ['text-a1'], ['a\n', 'b\n', 'p\n'])
841
k.add_lines('text-am', ['text-a2', 'text-a3'], ['a\n', 'b\n', 'z\n'])
842
origins = k.annotate('text-am')
843
self.assertEquals(origins[0], ('text-a1', 'a\n'))
844
self.assertEquals(origins[1], ('text-a1', 'b\n'))
845
self.assertEquals(origins[2], ('text-a2', 'z\n'))
847
def test_annotate_merge_5(self):
848
k = self.make_test_knit(True)
849
k.add_lines('text-a1', [], ['a\n', 'b\n', 'c\n'])
850
k.add_lines('text-a2', [], ['d\n', 'e\n', 'f\n'])
851
k.add_lines('text-a3', [], ['x\n', 'y\n', 'z\n'])
852
k.add_lines('text-am',
853
['text-a1', 'text-a2', 'text-a3'],
854
['a\n', 'e\n', 'z\n'])
855
origins = k.annotate('text-am')
856
self.assertEquals(origins[0], ('text-a1', 'a\n'))
857
self.assertEquals(origins[1], ('text-a2', 'e\n'))
858
self.assertEquals(origins[2], ('text-a3', 'z\n'))
860
def test_annotate_file_cherry_pick(self):
861
k = self.make_test_knit(True)
862
k.add_lines('text-1', [], ['a\n', 'b\n', 'c\n'])
863
k.add_lines('text-2', ['text-1'], ['d\n', 'e\n', 'f\n'])
864
k.add_lines('text-3', ['text-2', 'text-1'], ['a\n', 'b\n', 'c\n'])
865
origins = k.annotate('text-3')
866
self.assertEquals(origins[0], ('text-1', 'a\n'))
867
self.assertEquals(origins[1], ('text-1', 'b\n'))
868
self.assertEquals(origins[2], ('text-1', 'c\n'))
870
def test_knit_join(self):
871
"""Store in knit with parents"""
872
k1 = KnitVersionedFile('test1', get_transport('.'), factory=KnitPlainFactory(), create=True)
873
k1.add_lines('text-a', [], split_lines(TEXT_1))
874
k1.add_lines('text-b', ['text-a'], split_lines(TEXT_1))
876
k1.add_lines('text-c', [], split_lines(TEXT_1))
877
k1.add_lines('text-d', ['text-c'], split_lines(TEXT_1))
879
k1.add_lines('text-m', ['text-b', 'text-d'], split_lines(TEXT_1))
881
k2 = KnitVersionedFile('test2', get_transport('.'), factory=KnitPlainFactory(), create=True)
882
count = k2.join(k1, version_ids=['text-m'])
883
self.assertEquals(count, 5)
884
self.assertTrue(k2.has_version('text-a'))
885
self.assertTrue(k2.has_version('text-c'))
887
def test_reannotate(self):
888
k1 = KnitVersionedFile('knit1', get_transport('.'),
889
factory=KnitAnnotateFactory(), create=True)
891
k1.add_lines('text-a', [], ['a\n', 'b\n'])
893
k1.add_lines('text-b', ['text-a'], ['a\n', 'c\n'])
895
k2 = KnitVersionedFile('test2', get_transport('.'),
896
factory=KnitAnnotateFactory(), create=True)
897
k2.join(k1, version_ids=['text-b'])
900
k1.add_lines('text-X', ['text-b'], ['a\n', 'b\n'])
902
k2.add_lines('text-c', ['text-b'], ['z\n', 'c\n'])
904
k2.add_lines('text-Y', ['text-b'], ['b\n', 'c\n'])
906
# test-c will have index 3
907
k1.join(k2, version_ids=['text-c'])
909
lines = k1.get_lines('text-c')
910
self.assertEquals(lines, ['z\n', 'c\n'])
912
origins = k1.annotate('text-c')
913
self.assertEquals(origins[0], ('text-c', 'z\n'))
914
self.assertEquals(origins[1], ('text-b', 'c\n'))
916
def test_get_line_delta_texts(self):
917
"""Make sure we can call get_texts on text with reused line deltas"""
918
k1 = KnitVersionedFile('test1', get_transport('.'),
919
factory=KnitPlainFactory(), create=True)
924
parents = ['%d' % (t-1)]
925
k1.add_lines('%d' % t, parents, ['hello\n'] * t)
926
k1.get_texts(('%d' % t) for t in range(3))
928
def test_iter_lines_reads_in_order(self):
929
t = MemoryTransport()
930
instrumented_t = TransportLogger(t)
931
k1 = KnitVersionedFile('id', instrumented_t, create=True, delta=True)
932
self.assertEqual([('id.kndx',)], instrumented_t._calls)
933
# add texts with no required ordering
934
k1.add_lines('base', [], ['text\n'])
935
k1.add_lines('base2', [], ['text2\n'])
937
instrumented_t._calls = []
938
# request a last-first iteration
939
results = list(k1.iter_lines_added_or_present_in_versions(['base2', 'base']))
940
self.assertEqual([('id.knit', [(0, 87), (87, 89)])], instrumented_t._calls)
941
self.assertEqual(['text\n', 'text2\n'], results)
943
def test_create_empty_annotated(self):
944
k1 = self.make_test_knit(True)
946
k1.add_lines('text-a', [], ['a\n', 'b\n'])
947
k2 = k1.create_empty('t', MemoryTransport())
948
self.assertTrue(isinstance(k2.factory, KnitAnnotateFactory))
949
self.assertEqual(k1.delta, k2.delta)
950
# the generic test checks for empty content and file class
952
def test_knit_format(self):
953
# this tests that a new knit index file has the expected content
954
# and that is writes the data we expect as records are added.
955
knit = self.make_test_knit(True)
956
# Now knit files are not created until we first add data to them
957
self.assertFileEqual("# bzr knit index 8\n", 'test.kndx')
958
knit.add_lines_with_ghosts('revid', ['a_ghost'], ['a\n'])
959
self.assertFileEqual(
960
"# bzr knit index 8\n"
962
"revid fulltext 0 84 .a_ghost :",
964
knit.add_lines_with_ghosts('revid2', ['revid'], ['a\n'])
965
self.assertFileEqual(
966
"# bzr knit index 8\n"
967
"\nrevid fulltext 0 84 .a_ghost :"
968
"\nrevid2 line-delta 84 82 0 :",
970
# we should be able to load this file again
971
knit = KnitVersionedFile('test', get_transport('.'), access_mode='r')
972
self.assertEqual(['revid', 'revid2'], knit.versions())
973
# write a short write to the file and ensure that its ignored
974
indexfile = file('test.kndx', 'at')
975
indexfile.write('\nrevid3 line-delta 166 82 1 2 3 4 5 .phwoar:demo ')
977
# we should be able to load this file again
978
knit = KnitVersionedFile('test', get_transport('.'), access_mode='w')
979
self.assertEqual(['revid', 'revid2'], knit.versions())
980
# and add a revision with the same id the failed write had
981
knit.add_lines('revid3', ['revid2'], ['a\n'])
982
# and when reading it revid3 should now appear.
983
knit = KnitVersionedFile('test', get_transport('.'), access_mode='r')
984
self.assertEqual(['revid', 'revid2', 'revid3'], knit.versions())
985
self.assertEqual(['revid2'], knit.get_parents('revid3'))
987
def test_delay_create(self):
988
"""Test that passing delay_create=True creates files late"""
989
knit = self.make_test_knit(annotate=True, delay_create=True)
990
self.failIfExists('test.knit')
991
self.failIfExists('test.kndx')
992
knit.add_lines_with_ghosts('revid', ['a_ghost'], ['a\n'])
993
self.failUnlessExists('test.knit')
994
self.assertFileEqual(
995
"# bzr knit index 8\n"
997
"revid fulltext 0 84 .a_ghost :",
1000
def test_create_parent_dir(self):
1001
"""create_parent_dir can create knits in nonexistant dirs"""
1002
# Has no effect if we don't set 'delay_create'
1003
trans = get_transport('.')
1004
self.assertRaises(NoSuchFile, KnitVersionedFile, 'dir/test',
1005
trans, access_mode='w', factory=None,
1006
create=True, create_parent_dir=True)
1007
# Nothing should have changed yet
1008
knit = KnitVersionedFile('dir/test', trans, access_mode='w',
1009
factory=None, create=True,
1010
create_parent_dir=True,
1012
self.failIfExists('dir/test.knit')
1013
self.failIfExists('dir/test.kndx')
1014
self.failIfExists('dir')
1015
knit.add_lines('revid', [], ['a\n'])
1016
self.failUnlessExists('dir')
1017
self.failUnlessExists('dir/test.knit')
1018
self.assertFileEqual(
1019
"# bzr knit index 8\n"
1021
"revid fulltext 0 84 :",
1024
def test_create_mode_700(self):
1025
trans = get_transport('.')
1026
if not trans._can_roundtrip_unix_modebits():
1027
# Can't roundtrip, so no need to run this test
1029
knit = KnitVersionedFile('dir/test', trans, access_mode='w',
1030
factory=None, create=True,
1031
create_parent_dir=True,
1035
knit.add_lines('revid', [], ['a\n'])
1036
self.assertTransportMode(trans, 'dir', 0700)
1037
self.assertTransportMode(trans, 'dir/test.knit', 0600)
1038
self.assertTransportMode(trans, 'dir/test.kndx', 0600)
1040
def test_create_mode_770(self):
1041
trans = get_transport('.')
1042
if not trans._can_roundtrip_unix_modebits():
1043
# Can't roundtrip, so no need to run this test
1045
knit = KnitVersionedFile('dir/test', trans, access_mode='w',
1046
factory=None, create=True,
1047
create_parent_dir=True,
1051
knit.add_lines('revid', [], ['a\n'])
1052
self.assertTransportMode(trans, 'dir', 0770)
1053
self.assertTransportMode(trans, 'dir/test.knit', 0660)
1054
self.assertTransportMode(trans, 'dir/test.kndx', 0660)
1056
def test_create_mode_777(self):
1057
trans = get_transport('.')
1058
if not trans._can_roundtrip_unix_modebits():
1059
# Can't roundtrip, so no need to run this test
1061
knit = KnitVersionedFile('dir/test', trans, access_mode='w',
1062
factory=None, create=True,
1063
create_parent_dir=True,
1067
knit.add_lines('revid', [], ['a\n'])
1068
self.assertTransportMode(trans, 'dir', 0777)
1069
self.assertTransportMode(trans, 'dir/test.knit', 0666)
1070
self.assertTransportMode(trans, 'dir/test.kndx', 0666)
1072
def test_plan_merge(self):
1073
my_knit = self.make_test_knit(annotate=True)
1074
my_knit.add_lines('text1', [], split_lines(TEXT_1))
1075
my_knit.add_lines('text1a', ['text1'], split_lines(TEXT_1A))
1076
my_knit.add_lines('text1b', ['text1'], split_lines(TEXT_1B))
1077
plan = list(my_knit.plan_merge('text1a', 'text1b'))
1078
for plan_line, expected_line in zip(plan, AB_MERGE):
1079
self.assertEqual(plan_line, expected_line)
1091
Banana cup cake recipe
1097
- self-raising flour
1101
Banana cup cake recipe
1103
- bananas (do not use plantains!!!)
1110
Banana cup cake recipe
1113
- self-raising flour
1126
AB_MERGE_TEXT="""unchanged|Banana cup cake recipe
1131
new-b|- bananas (do not use plantains!!!)
1132
unchanged|- broken tea cups
1133
new-a|- self-raising flour
1136
AB_MERGE=[tuple(l.split('|')) for l in AB_MERGE_TEXT.splitlines(True)]
1139
def line_delta(from_lines, to_lines):
1140
"""Generate line-based delta from one text to another"""
1141
s = difflib.SequenceMatcher(None, from_lines, to_lines)
1142
for op in s.get_opcodes():
1143
if op[0] == 'equal':
1145
yield '%d,%d,%d\n' % (op[1], op[2], op[4]-op[3])
1146
for i in range(op[3], op[4]):
1150
def apply_line_delta(basis_lines, delta_lines):
1151
"""Apply a line-based perfect diff
1153
basis_lines -- text to apply the patch to
1154
delta_lines -- diff instructions and content
1156
out = basis_lines[:]
1159
while i < len(delta_lines):
1161
a, b, c = map(long, l.split(','))
1163
out[offset+a:offset+b] = delta_lines[i:i+c]
1165
offset = offset + (b - a) + c
1169
class TestWeaveToKnit(KnitTests):
1171
def test_weave_to_knit_matches(self):
1172
# check that the WeaveToKnit is_compatible function
1173
# registers True for a Weave to a Knit.
1175
k = self.make_test_knit()
1176
self.failUnless(WeaveToKnit.is_compatible(w, k))
1177
self.failIf(WeaveToKnit.is_compatible(k, w))
1178
self.failIf(WeaveToKnit.is_compatible(w, w))
1179
self.failIf(WeaveToKnit.is_compatible(k, k))
1182
class TestKnitCaching(KnitTests):
1184
def create_knit(self, cache_add=False):
1185
k = self.make_test_knit(True)
1189
k.add_lines('text-1', [], split_lines(TEXT_1))
1190
k.add_lines('text-2', [], split_lines(TEXT_2))
1193
def test_no_caching(self):
1194
k = self.create_knit()
1195
# Nothing should be cached without setting 'enable_cache'
1196
self.assertEqual({}, k._data._cache)
1198
def test_cache_add_and_clear(self):
1199
k = self.create_knit(True)
1201
self.assertEqual(['text-1', 'text-2'], sorted(k._data._cache.keys()))
1204
self.assertEqual({}, k._data._cache)
1206
def test_cache_data_read_raw(self):
1207
k = self.create_knit()
1209
# Now cache and read
1212
def read_one_raw(version):
1213
pos_map = k._get_components_positions([version])
1214
method, pos, size, next = pos_map[version]
1215
lst = list(k._data.read_records_iter_raw([(version, pos, size)]))
1216
self.assertEqual(1, len(lst))
1219
val = read_one_raw('text-1')
1220
self.assertEqual({'text-1':val[1]}, k._data._cache)
1223
# After clear, new reads are not cached
1224
self.assertEqual({}, k._data._cache)
1226
val2 = read_one_raw('text-1')
1227
self.assertEqual(val, val2)
1228
self.assertEqual({}, k._data._cache)
1230
def test_cache_data_read(self):
1231
k = self.create_knit()
1233
def read_one(version):
1234
pos_map = k._get_components_positions([version])
1235
method, pos, size, next = pos_map[version]
1236
lst = list(k._data.read_records_iter([(version, pos, size)]))
1237
self.assertEqual(1, len(lst))
1240
# Now cache and read
1243
val = read_one('text-2')
1244
self.assertEqual(['text-2'], k._data._cache.keys())
1245
self.assertEqual('text-2', val[0])
1246
content, digest = k._data._parse_record('text-2',
1247
k._data._cache['text-2'])
1248
self.assertEqual(content, val[1])
1249
self.assertEqual(digest, val[2])
1252
self.assertEqual({}, k._data._cache)
1254
val2 = read_one('text-2')
1255
self.assertEqual(val, val2)
1256
self.assertEqual({}, k._data._cache)
1258
def test_cache_read(self):
1259
k = self.create_knit()
1262
text = k.get_text('text-1')
1263
self.assertEqual(TEXT_1, text)
1264
self.assertEqual(['text-1'], k._data._cache.keys())
1267
self.assertEqual({}, k._data._cache)
1269
text = k.get_text('text-1')
1270
self.assertEqual(TEXT_1, text)
1271
self.assertEqual({}, k._data._cache)
1319
def make_test_knit(self, annotate=False, name='test'):
1320
mapper = ConstantMapper(name)
1321
return make_file_factory(annotate, mapper)(self.get_transport())
1324
class TestBadShaError(KnitTests):
1325
"""Tests for handling of sha errors."""
1327
def test_sha_exception_has_text(self):
1328
# having the failed text included in the error allows for recovery.
1329
source = self.make_test_knit()
1330
target = self.make_test_knit(name="target")
1331
if not source._max_delta_chain:
1332
raise TestNotApplicable(
1333
"cannot get delta-caused sha failures without deltas.")
1336
broken = ('broken',)
1337
source.add_lines(basis, (), ['foo\n'])
1338
source.add_lines(broken, (basis,), ['foo\n', 'bar\n'])
1339
# Seed target with a bad basis text
1340
target.add_lines(basis, (), ['gam\n'])
1341
target.insert_record_stream(
1342
source.get_record_stream([broken], 'unordered', False))
1343
err = self.assertRaises(errors.KnitCorrupt,
1344
target.get_record_stream([broken], 'unordered', True
1345
).next().get_bytes_as, 'chunked')
1346
self.assertEqual(['gam\n', 'bar\n'], err.content)
1347
# Test for formatting with live data
1348
self.assertStartsWith(str(err), "Knit ")
1274
1351
class TestKnitIndex(KnitTests):
1347
1433
def test_knit_index_checks_header(self):
1348
1434
t = get_transport('.')
1349
1435
t.put_bytes('test.kndx', '# not really a knit header\n\n')
1351
self.assertRaises(KnitHeaderError, self.make_test_knit)
1436
k = self.make_test_knit()
1437
self.assertRaises(KnitHeaderError, k.keys)
1440
class TestGraphIndexKnit(KnitTests):
1441
"""Tests for knits using a GraphIndex rather than a KnitIndex."""
1443
def make_g_index(self, name, ref_lists=0, nodes=[]):
1444
builder = GraphIndexBuilder(ref_lists)
1445
for node, references, value in nodes:
1446
builder.add_node(node, references, value)
1447
stream = builder.finish()
1448
trans = self.get_transport()
1449
size = trans.put_file(name, stream)
1450
return GraphIndex(trans, name, size)
1452
def two_graph_index(self, deltas=False, catch_adds=False):
1453
"""Build a two-graph index.
1455
:param deltas: If true, use underlying indices with two node-ref
1456
lists and 'parent' set to a delta-compressed against tail.
1458
# build a complex graph across several indices.
1460
# delta compression inn the index
1461
index1 = self.make_g_index('1', 2, [
1462
(('tip', ), 'N0 100', ([('parent', )], [], )),
1463
(('tail', ), '', ([], []))])
1464
index2 = self.make_g_index('2', 2, [
1465
(('parent', ), ' 100 78', ([('tail', ), ('ghost', )], [('tail', )])),
1466
(('separate', ), '', ([], []))])
1468
# just blob location and graph in the index.
1469
index1 = self.make_g_index('1', 1, [
1470
(('tip', ), 'N0 100', ([('parent', )], )),
1471
(('tail', ), '', ([], ))])
1472
index2 = self.make_g_index('2', 1, [
1473
(('parent', ), ' 100 78', ([('tail', ), ('ghost', )], )),
1474
(('separate', ), '', ([], ))])
1475
combined_index = CombinedGraphIndex([index1, index2])
1477
self.combined_index = combined_index
1478
self.caught_entries = []
1479
add_callback = self.catch_add
1482
return _KnitGraphIndex(combined_index, lambda:True, deltas=deltas,
1483
add_callback=add_callback)
1485
def test_keys(self):
1486
index = self.two_graph_index()
1487
self.assertEqual(set([('tail',), ('tip',), ('parent',), ('separate',)]),
1490
def test_get_position(self):
1491
index = self.two_graph_index()
1492
self.assertEqual((index._graph_index._indices[0], 0, 100), index.get_position(('tip',)))
1493
self.assertEqual((index._graph_index._indices[1], 100, 78), index.get_position(('parent',)))
1495
def test_get_method_deltas(self):
1496
index = self.two_graph_index(deltas=True)
1497
self.assertEqual('fulltext', index.get_method(('tip',)))
1498
self.assertEqual('line-delta', index.get_method(('parent',)))
1500
def test_get_method_no_deltas(self):
1501
# check that the parent-history lookup is ignored with deltas=False.
1502
index = self.two_graph_index(deltas=False)
1503
self.assertEqual('fulltext', index.get_method(('tip',)))
1504
self.assertEqual('fulltext', index.get_method(('parent',)))
1506
def test_get_options_deltas(self):
1507
index = self.two_graph_index(deltas=True)
1508
self.assertEqual(['fulltext', 'no-eol'], index.get_options(('tip',)))
1509
self.assertEqual(['line-delta'], index.get_options(('parent',)))
1511
def test_get_options_no_deltas(self):
1512
# check that the parent-history lookup is ignored with deltas=False.
1513
index = self.two_graph_index(deltas=False)
1514
self.assertEqual(['fulltext', 'no-eol'], index.get_options(('tip',)))
1515
self.assertEqual(['fulltext'], index.get_options(('parent',)))
1517
def test_get_parent_map(self):
1518
index = self.two_graph_index()
1519
self.assertEqual({('parent',):(('tail',), ('ghost',))},
1520
index.get_parent_map([('parent',), ('ghost',)]))
1522
def catch_add(self, entries):
1523
self.caught_entries.append(entries)
1525
def test_add_no_callback_errors(self):
1526
index = self.two_graph_index()
1527
self.assertRaises(errors.ReadOnlyError, index.add_records,
1528
[(('new',), 'fulltext,no-eol', (None, 50, 60), ['separate'])])
1530
def test_add_version_smoke(self):
1531
index = self.two_graph_index(catch_adds=True)
1532
index.add_records([(('new',), 'fulltext,no-eol', (None, 50, 60),
1534
self.assertEqual([[(('new', ), 'N50 60', ((('separate',),),))]],
1535
self.caught_entries)
1537
def test_add_version_delta_not_delta_index(self):
1538
index = self.two_graph_index(catch_adds=True)
1539
self.assertRaises(errors.KnitCorrupt, index.add_records,
1540
[(('new',), 'no-eol,line-delta', (None, 0, 100), [('parent',)])])
1541
self.assertEqual([], self.caught_entries)
1543
def test_add_version_same_dup(self):
1544
index = self.two_graph_index(catch_adds=True)
1545
# options can be spelt two different ways
1546
index.add_records([(('tip',), 'fulltext,no-eol', (None, 0, 100), [('parent',)])])
1547
index.add_records([(('tip',), 'no-eol,fulltext', (None, 0, 100), [('parent',)])])
1548
# position/length are ignored (because each pack could have fulltext or
1549
# delta, and be at a different position.
1550
index.add_records([(('tip',), 'fulltext,no-eol', (None, 50, 100),
1552
index.add_records([(('tip',), 'fulltext,no-eol', (None, 0, 1000),
1554
# but neither should have added data:
1555
self.assertEqual([[], [], [], []], self.caught_entries)
1557
def test_add_version_different_dup(self):
1558
index = self.two_graph_index(deltas=True, catch_adds=True)
1560
self.assertRaises(errors.KnitCorrupt, index.add_records,
1561
[(('tip',), 'line-delta', (None, 0, 100), [('parent',)])])
1562
self.assertRaises(errors.KnitCorrupt, index.add_records,
1563
[(('tip',), 'fulltext', (None, 0, 100), [('parent',)])])
1565
self.assertRaises(errors.KnitCorrupt, index.add_records,
1566
[(('tip',), 'fulltext,no-eol', (None, 0, 100), [])])
1567
self.assertEqual([], self.caught_entries)
1569
def test_add_versions_nodeltas(self):
1570
index = self.two_graph_index(catch_adds=True)
1572
(('new',), 'fulltext,no-eol', (None, 50, 60), [('separate',)]),
1573
(('new2',), 'fulltext', (None, 0, 6), [('new',)]),
1575
self.assertEqual([(('new', ), 'N50 60', ((('separate',),),)),
1576
(('new2', ), ' 0 6', ((('new',),),))],
1577
sorted(self.caught_entries[0]))
1578
self.assertEqual(1, len(self.caught_entries))
1580
def test_add_versions_deltas(self):
1581
index = self.two_graph_index(deltas=True, catch_adds=True)
1583
(('new',), 'fulltext,no-eol', (None, 50, 60), [('separate',)]),
1584
(('new2',), 'line-delta', (None, 0, 6), [('new',)]),
1586
self.assertEqual([(('new', ), 'N50 60', ((('separate',),), ())),
1587
(('new2', ), ' 0 6', ((('new',),), (('new',),), ))],
1588
sorted(self.caught_entries[0]))
1589
self.assertEqual(1, len(self.caught_entries))
1591
def test_add_versions_delta_not_delta_index(self):
1592
index = self.two_graph_index(catch_adds=True)
1593
self.assertRaises(errors.KnitCorrupt, index.add_records,
1594
[(('new',), 'no-eol,line-delta', (None, 0, 100), [('parent',)])])
1595
self.assertEqual([], self.caught_entries)
1597
def test_add_versions_random_id_accepted(self):
1598
index = self.two_graph_index(catch_adds=True)
1599
index.add_records([], random_id=True)
1601
def test_add_versions_same_dup(self):
1602
index = self.two_graph_index(catch_adds=True)
1603
# options can be spelt two different ways
1604
index.add_records([(('tip',), 'fulltext,no-eol', (None, 0, 100),
1606
index.add_records([(('tip',), 'no-eol,fulltext', (None, 0, 100),
1608
# position/length are ignored (because each pack could have fulltext or
1609
# delta, and be at a different position.
1610
index.add_records([(('tip',), 'fulltext,no-eol', (None, 50, 100),
1612
index.add_records([(('tip',), 'fulltext,no-eol', (None, 0, 1000),
1614
# but neither should have added data.
1615
self.assertEqual([[], [], [], []], self.caught_entries)
1617
def test_add_versions_different_dup(self):
1618
index = self.two_graph_index(deltas=True, catch_adds=True)
1620
self.assertRaises(errors.KnitCorrupt, index.add_records,
1621
[(('tip',), 'line-delta', (None, 0, 100), [('parent',)])])
1622
self.assertRaises(errors.KnitCorrupt, index.add_records,
1623
[(('tip',), 'fulltext', (None, 0, 100), [('parent',)])])
1625
self.assertRaises(errors.KnitCorrupt, index.add_records,
1626
[(('tip',), 'fulltext,no-eol', (None, 0, 100), [])])
1627
# change options in the second record
1628
self.assertRaises(errors.KnitCorrupt, index.add_records,
1629
[(('tip',), 'fulltext,no-eol', (None, 0, 100), [('parent',)]),
1630
(('tip',), 'line-delta', (None, 0, 100), [('parent',)])])
1631
self.assertEqual([], self.caught_entries)
1633
def make_g_index_missing_compression_parent(self):
1634
graph_index = self.make_g_index('missing_comp', 2,
1635
[(('tip', ), ' 100 78',
1636
([('missing-parent', ), ('ghost', )], [('missing-parent', )]))])
1639
def make_g_index_missing_parent(self):
1640
graph_index = self.make_g_index('missing_parent', 2,
1641
[(('parent', ), ' 100 78', ([], [])),
1642
(('tip', ), ' 100 78',
1643
([('parent', ), ('missing-parent', )], [('parent', )])),
1647
def make_g_index_no_external_refs(self):
1648
graph_index = self.make_g_index('no_external_refs', 2,
1649
[(('rev', ), ' 100 78',
1650
([('parent', ), ('ghost', )], []))])
1653
def test_add_good_unvalidated_index(self):
1654
unvalidated = self.make_g_index_no_external_refs()
1655
combined = CombinedGraphIndex([unvalidated])
1656
index = _KnitGraphIndex(combined, lambda: True, deltas=True)
1657
index.scan_unvalidated_index(unvalidated)
1658
self.assertEqual(frozenset(), index.get_missing_compression_parents())
1660
def test_add_missing_compression_parent_unvalidated_index(self):
1661
unvalidated = self.make_g_index_missing_compression_parent()
1662
combined = CombinedGraphIndex([unvalidated])
1663
index = _KnitGraphIndex(combined, lambda: True, deltas=True)
1664
index.scan_unvalidated_index(unvalidated)
1665
# This also checks that its only the compression parent that is
1666
# examined, otherwise 'ghost' would also be reported as a missing
1669
frozenset([('missing-parent',)]),
1670
index.get_missing_compression_parents())
1672
def test_add_missing_noncompression_parent_unvalidated_index(self):
1673
unvalidated = self.make_g_index_missing_parent()
1674
combined = CombinedGraphIndex([unvalidated])
1675
index = _KnitGraphIndex(combined, lambda: True, deltas=True,
1676
track_external_parent_refs=True)
1677
index.scan_unvalidated_index(unvalidated)
1679
frozenset([('missing-parent',)]), index.get_missing_parents())
1681
def test_track_external_parent_refs(self):
1682
g_index = self.make_g_index('empty', 2, [])
1683
combined = CombinedGraphIndex([g_index])
1684
index = _KnitGraphIndex(combined, lambda: True, deltas=True,
1685
add_callback=self.catch_add, track_external_parent_refs=True)
1686
self.caught_entries = []
1688
(('new-key',), 'fulltext,no-eol', (None, 50, 60),
1689
[('parent-1',), ('parent-2',)])])
1691
frozenset([('parent-1',), ('parent-2',)]),
1692
index.get_missing_parents())
1694
def test_add_unvalidated_index_with_present_external_references(self):
1695
index = self.two_graph_index(deltas=True)
1696
# Ugly hack to get at one of the underlying GraphIndex objects that
1697
# two_graph_index built.
1698
unvalidated = index._graph_index._indices[1]
1699
# 'parent' is an external ref of _indices[1] (unvalidated), but is
1700
# present in _indices[0].
1701
index.scan_unvalidated_index(unvalidated)
1702
self.assertEqual(frozenset(), index.get_missing_compression_parents())
1704
def make_new_missing_parent_g_index(self, name):
1705
missing_parent = name + '-missing-parent'
1706
graph_index = self.make_g_index(name, 2,
1707
[((name + 'tip', ), ' 100 78',
1708
([(missing_parent, ), ('ghost', )], [(missing_parent, )]))])
1711
def test_add_mulitiple_unvalidated_indices_with_missing_parents(self):
1712
g_index_1 = self.make_new_missing_parent_g_index('one')
1713
g_index_2 = self.make_new_missing_parent_g_index('two')
1714
combined = CombinedGraphIndex([g_index_1, g_index_2])
1715
index = _KnitGraphIndex(combined, lambda: True, deltas=True)
1716
index.scan_unvalidated_index(g_index_1)
1717
index.scan_unvalidated_index(g_index_2)
1719
frozenset([('one-missing-parent',), ('two-missing-parent',)]),
1720
index.get_missing_compression_parents())
1722
def test_add_mulitiple_unvalidated_indices_with_mutual_dependencies(self):
1723
graph_index_a = self.make_g_index('one', 2,
1724
[(('parent-one', ), ' 100 78', ([('non-compression-parent',)], [])),
1725
(('child-of-two', ), ' 100 78',
1726
([('parent-two',)], [('parent-two',)]))])
1727
graph_index_b = self.make_g_index('two', 2,
1728
[(('parent-two', ), ' 100 78', ([('non-compression-parent',)], [])),
1729
(('child-of-one', ), ' 100 78',
1730
([('parent-one',)], [('parent-one',)]))])
1731
combined = CombinedGraphIndex([graph_index_a, graph_index_b])
1732
index = _KnitGraphIndex(combined, lambda: True, deltas=True)
1733
index.scan_unvalidated_index(graph_index_a)
1734
index.scan_unvalidated_index(graph_index_b)
1736
frozenset([]), index.get_missing_compression_parents())
1739
class TestNoParentsGraphIndexKnit(KnitTests):
1740
"""Tests for knits using _KnitGraphIndex with no parents."""
1742
def make_g_index(self, name, ref_lists=0, nodes=[]):
1743
builder = GraphIndexBuilder(ref_lists)
1744
for node, references in nodes:
1745
builder.add_node(node, references)
1746
stream = builder.finish()
1747
trans = self.get_transport()
1748
size = trans.put_file(name, stream)
1749
return GraphIndex(trans, name, size)
1751
def test_add_good_unvalidated_index(self):
1752
unvalidated = self.make_g_index('unvalidated')
1753
combined = CombinedGraphIndex([unvalidated])
1754
index = _KnitGraphIndex(combined, lambda: True, parents=False)
1755
index.scan_unvalidated_index(unvalidated)
1756
self.assertEqual(frozenset(),
1757
index.get_missing_compression_parents())
1759
def test_parents_deltas_incompatible(self):
1760
index = CombinedGraphIndex([])
1761
self.assertRaises(errors.KnitError, _KnitGraphIndex, lambda:True,
1762
index, deltas=True, parents=False)
1764
def two_graph_index(self, catch_adds=False):
1765
"""Build a two-graph index.
1767
:param deltas: If true, use underlying indices with two node-ref
1768
lists and 'parent' set to a delta-compressed against tail.
1770
# put several versions in the index.
1771
index1 = self.make_g_index('1', 0, [
1772
(('tip', ), 'N0 100'),
1774
index2 = self.make_g_index('2', 0, [
1775
(('parent', ), ' 100 78'),
1776
(('separate', ), '')])
1777
combined_index = CombinedGraphIndex([index1, index2])
1779
self.combined_index = combined_index
1780
self.caught_entries = []
1781
add_callback = self.catch_add
1784
return _KnitGraphIndex(combined_index, lambda:True, parents=False,
1785
add_callback=add_callback)
1787
def test_keys(self):
1788
index = self.two_graph_index()
1789
self.assertEqual(set([('tail',), ('tip',), ('parent',), ('separate',)]),
1792
def test_get_position(self):
1793
index = self.two_graph_index()
1794
self.assertEqual((index._graph_index._indices[0], 0, 100),
1795
index.get_position(('tip',)))
1796
self.assertEqual((index._graph_index._indices[1], 100, 78),
1797
index.get_position(('parent',)))
1799
def test_get_method(self):
1800
index = self.two_graph_index()
1801
self.assertEqual('fulltext', index.get_method(('tip',)))
1802
self.assertEqual(['fulltext'], index.get_options(('parent',)))
1804
def test_get_options(self):
1805
index = self.two_graph_index()
1806
self.assertEqual(['fulltext', 'no-eol'], index.get_options(('tip',)))
1807
self.assertEqual(['fulltext'], index.get_options(('parent',)))
1809
def test_get_parent_map(self):
1810
index = self.two_graph_index()
1811
self.assertEqual({('parent',):None},
1812
index.get_parent_map([('parent',), ('ghost',)]))
1814
def catch_add(self, entries):
1815
self.caught_entries.append(entries)
1817
def test_add_no_callback_errors(self):
1818
index = self.two_graph_index()
1819
self.assertRaises(errors.ReadOnlyError, index.add_records,
1820
[(('new',), 'fulltext,no-eol', (None, 50, 60), [('separate',)])])
1822
def test_add_version_smoke(self):
1823
index = self.two_graph_index(catch_adds=True)
1824
index.add_records([(('new',), 'fulltext,no-eol', (None, 50, 60), [])])
1825
self.assertEqual([[(('new', ), 'N50 60')]],
1826
self.caught_entries)
1828
def test_add_version_delta_not_delta_index(self):
1829
index = self.two_graph_index(catch_adds=True)
1830
self.assertRaises(errors.KnitCorrupt, index.add_records,
1831
[(('new',), 'no-eol,line-delta', (None, 0, 100), [])])
1832
self.assertEqual([], self.caught_entries)
1834
def test_add_version_same_dup(self):
1835
index = self.two_graph_index(catch_adds=True)
1836
# options can be spelt two different ways
1837
index.add_records([(('tip',), 'fulltext,no-eol', (None, 0, 100), [])])
1838
index.add_records([(('tip',), 'no-eol,fulltext', (None, 0, 100), [])])
1839
# position/length are ignored (because each pack could have fulltext or
1840
# delta, and be at a different position.
1841
index.add_records([(('tip',), 'fulltext,no-eol', (None, 50, 100), [])])
1842
index.add_records([(('tip',), 'fulltext,no-eol', (None, 0, 1000), [])])
1843
# but neither should have added data.
1844
self.assertEqual([[], [], [], []], self.caught_entries)
1846
def test_add_version_different_dup(self):
1847
index = self.two_graph_index(catch_adds=True)
1849
self.assertRaises(errors.KnitCorrupt, index.add_records,
1850
[(('tip',), 'no-eol,line-delta', (None, 0, 100), [])])
1851
self.assertRaises(errors.KnitCorrupt, index.add_records,
1852
[(('tip',), 'line-delta,no-eol', (None, 0, 100), [])])
1853
self.assertRaises(errors.KnitCorrupt, index.add_records,
1854
[(('tip',), 'fulltext', (None, 0, 100), [])])
1856
self.assertRaises(errors.KnitCorrupt, index.add_records,
1857
[(('tip',), 'fulltext,no-eol', (None, 0, 100), [('parent',)])])
1858
self.assertEqual([], self.caught_entries)
1860
def test_add_versions(self):
1861
index = self.two_graph_index(catch_adds=True)
1863
(('new',), 'fulltext,no-eol', (None, 50, 60), []),
1864
(('new2',), 'fulltext', (None, 0, 6), []),
1866
self.assertEqual([(('new', ), 'N50 60'), (('new2', ), ' 0 6')],
1867
sorted(self.caught_entries[0]))
1868
self.assertEqual(1, len(self.caught_entries))
1870
def test_add_versions_delta_not_delta_index(self):
1871
index = self.two_graph_index(catch_adds=True)
1872
self.assertRaises(errors.KnitCorrupt, index.add_records,
1873
[(('new',), 'no-eol,line-delta', (None, 0, 100), [('parent',)])])
1874
self.assertEqual([], self.caught_entries)
1876
def test_add_versions_parents_not_parents_index(self):
1877
index = self.two_graph_index(catch_adds=True)
1878
self.assertRaises(errors.KnitCorrupt, index.add_records,
1879
[(('new',), 'no-eol,fulltext', (None, 0, 100), [('parent',)])])
1880
self.assertEqual([], self.caught_entries)
1882
def test_add_versions_random_id_accepted(self):
1883
index = self.two_graph_index(catch_adds=True)
1884
index.add_records([], random_id=True)
1886
def test_add_versions_same_dup(self):
1887
index = self.two_graph_index(catch_adds=True)
1888
# options can be spelt two different ways
1889
index.add_records([(('tip',), 'fulltext,no-eol', (None, 0, 100), [])])
1890
index.add_records([(('tip',), 'no-eol,fulltext', (None, 0, 100), [])])
1891
# position/length are ignored (because each pack could have fulltext or
1892
# delta, and be at a different position.
1893
index.add_records([(('tip',), 'fulltext,no-eol', (None, 50, 100), [])])
1894
index.add_records([(('tip',), 'fulltext,no-eol', (None, 0, 1000), [])])
1895
# but neither should have added data.
1896
self.assertEqual([[], [], [], []], self.caught_entries)
1898
def test_add_versions_different_dup(self):
1899
index = self.two_graph_index(catch_adds=True)
1901
self.assertRaises(errors.KnitCorrupt, index.add_records,
1902
[(('tip',), 'no-eol,line-delta', (None, 0, 100), [])])
1903
self.assertRaises(errors.KnitCorrupt, index.add_records,
1904
[(('tip',), 'line-delta,no-eol', (None, 0, 100), [])])
1905
self.assertRaises(errors.KnitCorrupt, index.add_records,
1906
[(('tip',), 'fulltext', (None, 0, 100), [])])
1908
self.assertRaises(errors.KnitCorrupt, index.add_records,
1909
[(('tip',), 'fulltext,no-eol', (None, 0, 100), [('parent',)])])
1910
# change options in the second record
1911
self.assertRaises(errors.KnitCorrupt, index.add_records,
1912
[(('tip',), 'fulltext,no-eol', (None, 0, 100), []),
1913
(('tip',), 'no-eol,line-delta', (None, 0, 100), [])])
1914
self.assertEqual([], self.caught_entries)
1917
class TestKnitVersionedFiles(KnitTests):
1919
def assertGroupKeysForIo(self, exp_groups, keys, non_local_keys,
1920
positions, _min_buffer_size=None):
1921
kvf = self.make_test_knit()
1922
if _min_buffer_size is None:
1923
_min_buffer_size = knit._STREAM_MIN_BUFFER_SIZE
1924
self.assertEqual(exp_groups, kvf._group_keys_for_io(keys,
1925
non_local_keys, positions,
1926
_min_buffer_size=_min_buffer_size))
1928
def assertSplitByPrefix(self, expected_map, expected_prefix_order,
1930
split, prefix_order = KnitVersionedFiles._split_by_prefix(keys)
1931
self.assertEqual(expected_map, split)
1932
self.assertEqual(expected_prefix_order, prefix_order)
1934
def test__group_keys_for_io(self):
1935
ft_detail = ('fulltext', False)
1936
ld_detail = ('line-delta', False)
1944
f_a: (ft_detail, (f_a, 0, 100), None),
1945
f_b: (ld_detail, (f_b, 100, 21), f_a),
1946
f_c: (ld_detail, (f_c, 180, 15), f_b),
1947
g_a: (ft_detail, (g_a, 121, 35), None),
1948
g_b: (ld_detail, (g_b, 156, 12), g_a),
1949
g_c: (ld_detail, (g_c, 195, 13), g_a),
1951
self.assertGroupKeysForIo([([f_a], set())],
1952
[f_a], [], positions)
1953
self.assertGroupKeysForIo([([f_a], set([f_a]))],
1954
[f_a], [f_a], positions)
1955
self.assertGroupKeysForIo([([f_a, f_b], set([]))],
1956
[f_a, f_b], [], positions)
1957
self.assertGroupKeysForIo([([f_a, f_b], set([f_b]))],
1958
[f_a, f_b], [f_b], positions)
1959
self.assertGroupKeysForIo([([f_a, f_b, g_a, g_b], set())],
1960
[f_a, g_a, f_b, g_b], [], positions)
1961
self.assertGroupKeysForIo([([f_a, f_b, g_a, g_b], set())],
1962
[f_a, g_a, f_b, g_b], [], positions,
1963
_min_buffer_size=150)
1964
self.assertGroupKeysForIo([([f_a, f_b], set()), ([g_a, g_b], set())],
1965
[f_a, g_a, f_b, g_b], [], positions,
1966
_min_buffer_size=100)
1967
self.assertGroupKeysForIo([([f_c], set()), ([g_b], set())],
1968
[f_c, g_b], [], positions,
1969
_min_buffer_size=125)
1970
self.assertGroupKeysForIo([([g_b, f_c], set())],
1971
[g_b, f_c], [], positions,
1972
_min_buffer_size=125)
1974
def test__split_by_prefix(self):
1975
self.assertSplitByPrefix({'f': [('f', 'a'), ('f', 'b')],
1976
'g': [('g', 'b'), ('g', 'a')],
1978
[('f', 'a'), ('g', 'b'),
1979
('g', 'a'), ('f', 'b')])
1981
self.assertSplitByPrefix({'f': [('f', 'a'), ('f', 'b')],
1982
'g': [('g', 'b'), ('g', 'a')],
1984
[('f', 'a'), ('f', 'b'),
1985
('g', 'b'), ('g', 'a')])
1987
self.assertSplitByPrefix({'f': [('f', 'a'), ('f', 'b')],
1988
'g': [('g', 'b'), ('g', 'a')],
1990
[('f', 'a'), ('f', 'b'),
1991
('g', 'b'), ('g', 'a')])
1993
self.assertSplitByPrefix({'f': [('f', 'a'), ('f', 'b')],
1994
'g': [('g', 'b'), ('g', 'a')],
1995
'': [('a',), ('b',)]
1997
[('f', 'a'), ('g', 'b'),
1999
('g', 'a'), ('f', 'b')])
2002
class TestStacking(KnitTests):
2004
def get_basis_and_test_knit(self):
2005
basis = self.make_test_knit(name='basis')
2006
basis = RecordingVersionedFilesDecorator(basis)
2007
test = self.make_test_knit(name='test')
2008
test.add_fallback_versioned_files(basis)
2011
def test_add_fallback_versioned_files(self):
2012
basis = self.make_test_knit(name='basis')
2013
test = self.make_test_knit(name='test')
2014
# It must not error; other tests test that the fallback is referred to
2015
# when accessing data.
2016
test.add_fallback_versioned_files(basis)
2018
def test_add_lines(self):
2019
# lines added to the test are not added to the basis
2020
basis, test = self.get_basis_and_test_knit()
2022
key_basis = ('bar',)
2023
key_cross_border = ('quux',)
2024
key_delta = ('zaphod',)
2025
test.add_lines(key, (), ['foo\n'])
2026
self.assertEqual({}, basis.get_parent_map([key]))
2027
# lines added to the test that reference across the stack do a
2029
basis.add_lines(key_basis, (), ['foo\n'])
2031
test.add_lines(key_cross_border, (key_basis,), ['foo\n'])
2032
self.assertEqual('fulltext', test._index.get_method(key_cross_border))
2033
# we don't even need to look at the basis to see that this should be
2034
# stored as a fulltext
2035
self.assertEqual([], basis.calls)
2036
# Subsequent adds do delta.
2038
test.add_lines(key_delta, (key_cross_border,), ['foo\n'])
2039
self.assertEqual('line-delta', test._index.get_method(key_delta))
2040
self.assertEqual([], basis.calls)
2042
def test_annotate(self):
2043
# annotations from the test knit are answered without asking the basis
2044
basis, test = self.get_basis_and_test_knit()
2046
key_basis = ('bar',)
2047
key_missing = ('missing',)
2048
test.add_lines(key, (), ['foo\n'])
2049
details = test.annotate(key)
2050
self.assertEqual([(key, 'foo\n')], details)
2051
self.assertEqual([], basis.calls)
2052
# But texts that are not in the test knit are looked for in the basis
2054
basis.add_lines(key_basis, (), ['foo\n', 'bar\n'])
2056
details = test.annotate(key_basis)
2057
self.assertEqual([(key_basis, 'foo\n'), (key_basis, 'bar\n')], details)
2058
# Not optimised to date:
2059
# self.assertEqual([("annotate", key_basis)], basis.calls)
2060
self.assertEqual([('get_parent_map', set([key_basis])),
2061
('get_parent_map', set([key_basis])),
2062
('get_parent_map', set([key_basis])),
2063
('get_record_stream', [key_basis], 'unordered', True)],
2066
def test_check(self):
2067
# At the moment checking a stacked knit does implicitly check the
2069
basis, test = self.get_basis_and_test_knit()
2072
def test_get_parent_map(self):
2073
# parents in the test knit are answered without asking the basis
2074
basis, test = self.get_basis_and_test_knit()
2076
key_basis = ('bar',)
2077
key_missing = ('missing',)
2078
test.add_lines(key, (), [])
2079
parent_map = test.get_parent_map([key])
2080
self.assertEqual({key: ()}, parent_map)
2081
self.assertEqual([], basis.calls)
2082
# But parents that are not in the test knit are looked for in the basis
2083
basis.add_lines(key_basis, (), [])
2085
parent_map = test.get_parent_map([key, key_basis, key_missing])
2086
self.assertEqual({key: (),
2087
key_basis: ()}, parent_map)
2088
self.assertEqual([("get_parent_map", set([key_basis, key_missing]))],
2091
def test_get_record_stream_unordered_fulltexts(self):
2092
# records from the test knit are answered without asking the basis:
2093
basis, test = self.get_basis_and_test_knit()
2095
key_basis = ('bar',)
2096
key_missing = ('missing',)
2097
test.add_lines(key, (), ['foo\n'])
2098
records = list(test.get_record_stream([key], 'unordered', True))
2099
self.assertEqual(1, len(records))
2100
self.assertEqual([], basis.calls)
2101
# Missing (from test knit) objects are retrieved from the basis:
2102
basis.add_lines(key_basis, (), ['foo\n', 'bar\n'])
2104
records = list(test.get_record_stream([key_basis, key_missing],
2106
self.assertEqual(2, len(records))
2107
calls = list(basis.calls)
2108
for record in records:
2109
self.assertSubset([record.key], (key_basis, key_missing))
2110
if record.key == key_missing:
2111
self.assertIsInstance(record, AbsentContentFactory)
2113
reference = list(basis.get_record_stream([key_basis],
2114
'unordered', True))[0]
2115
self.assertEqual(reference.key, record.key)
2116
self.assertEqual(reference.sha1, record.sha1)
2117
self.assertEqual(reference.storage_kind, record.storage_kind)
2118
self.assertEqual(reference.get_bytes_as(reference.storage_kind),
2119
record.get_bytes_as(record.storage_kind))
2120
self.assertEqual(reference.get_bytes_as('fulltext'),
2121
record.get_bytes_as('fulltext'))
2122
# It's not strictly minimal, but it seems reasonable for now for it to
2123
# ask which fallbacks have which parents.
2125
("get_parent_map", set([key_basis, key_missing])),
2126
("get_record_stream", [key_basis], 'unordered', True)],
2129
def test_get_record_stream_ordered_fulltexts(self):
2130
# ordering is preserved down into the fallback store.
2131
basis, test = self.get_basis_and_test_knit()
2133
key_basis = ('bar',)
2134
key_basis_2 = ('quux',)
2135
key_missing = ('missing',)
2136
test.add_lines(key, (key_basis,), ['foo\n'])
2137
# Missing (from test knit) objects are retrieved from the basis:
2138
basis.add_lines(key_basis, (key_basis_2,), ['foo\n', 'bar\n'])
2139
basis.add_lines(key_basis_2, (), ['quux\n'])
2141
# ask for in non-topological order
2142
records = list(test.get_record_stream(
2143
[key, key_basis, key_missing, key_basis_2], 'topological', True))
2144
self.assertEqual(4, len(records))
2146
for record in records:
2147
self.assertSubset([record.key],
2148
(key_basis, key_missing, key_basis_2, key))
2149
if record.key == key_missing:
2150
self.assertIsInstance(record, AbsentContentFactory)
2152
results.append((record.key, record.sha1, record.storage_kind,
2153
record.get_bytes_as('fulltext')))
2154
calls = list(basis.calls)
2155
order = [record[0] for record in results]
2156
self.assertEqual([key_basis_2, key_basis, key], order)
2157
for result in results:
2158
if result[0] == key:
2162
record = source.get_record_stream([result[0]], 'unordered',
2164
self.assertEqual(record.key, result[0])
2165
self.assertEqual(record.sha1, result[1])
2166
# We used to check that the storage kind matched, but actually it
2167
# depends on whether it was sourced from the basis, or in a single
2168
# group, because asking for full texts returns proxy objects to a
2169
# _ContentMapGenerator object; so checking the kind is unneeded.
2170
self.assertEqual(record.get_bytes_as('fulltext'), result[3])
2171
# It's not strictly minimal, but it seems reasonable for now for it to
2172
# ask which fallbacks have which parents.
2174
("get_parent_map", set([key_basis, key_basis_2, key_missing])),
2175
# unordered is asked for by the underlying worker as it still
2176
# buffers everything while answering - which is a problem!
2177
("get_record_stream", [key_basis_2, key_basis], 'unordered', True)],
2180
def test_get_record_stream_unordered_deltas(self):
2181
# records from the test knit are answered without asking the basis:
2182
basis, test = self.get_basis_and_test_knit()
2184
key_basis = ('bar',)
2185
key_missing = ('missing',)
2186
test.add_lines(key, (), ['foo\n'])
2187
records = list(test.get_record_stream([key], 'unordered', False))
2188
self.assertEqual(1, len(records))
2189
self.assertEqual([], basis.calls)
2190
# Missing (from test knit) objects are retrieved from the basis:
2191
basis.add_lines(key_basis, (), ['foo\n', 'bar\n'])
2193
records = list(test.get_record_stream([key_basis, key_missing],
2194
'unordered', False))
2195
self.assertEqual(2, len(records))
2196
calls = list(basis.calls)
2197
for record in records:
2198
self.assertSubset([record.key], (key_basis, key_missing))
2199
if record.key == key_missing:
2200
self.assertIsInstance(record, AbsentContentFactory)
2202
reference = list(basis.get_record_stream([key_basis],
2203
'unordered', False))[0]
2204
self.assertEqual(reference.key, record.key)
2205
self.assertEqual(reference.sha1, record.sha1)
2206
self.assertEqual(reference.storage_kind, record.storage_kind)
2207
self.assertEqual(reference.get_bytes_as(reference.storage_kind),
2208
record.get_bytes_as(record.storage_kind))
2209
# It's not strictly minimal, but it seems reasonable for now for it to
2210
# ask which fallbacks have which parents.
2212
("get_parent_map", set([key_basis, key_missing])),
2213
("get_record_stream", [key_basis], 'unordered', False)],
2216
def test_get_record_stream_ordered_deltas(self):
2217
# ordering is preserved down into the fallback store.
2218
basis, test = self.get_basis_and_test_knit()
2220
key_basis = ('bar',)
2221
key_basis_2 = ('quux',)
2222
key_missing = ('missing',)
2223
test.add_lines(key, (key_basis,), ['foo\n'])
2224
# Missing (from test knit) objects are retrieved from the basis:
2225
basis.add_lines(key_basis, (key_basis_2,), ['foo\n', 'bar\n'])
2226
basis.add_lines(key_basis_2, (), ['quux\n'])
2228
# ask for in non-topological order
2229
records = list(test.get_record_stream(
2230
[key, key_basis, key_missing, key_basis_2], 'topological', False))
2231
self.assertEqual(4, len(records))
2233
for record in records:
2234
self.assertSubset([record.key],
2235
(key_basis, key_missing, key_basis_2, key))
2236
if record.key == key_missing:
2237
self.assertIsInstance(record, AbsentContentFactory)
2239
results.append((record.key, record.sha1, record.storage_kind,
2240
record.get_bytes_as(record.storage_kind)))
2241
calls = list(basis.calls)
2242
order = [record[0] for record in results]
2243
self.assertEqual([key_basis_2, key_basis, key], order)
2244
for result in results:
2245
if result[0] == key:
2249
record = source.get_record_stream([result[0]], 'unordered',
2251
self.assertEqual(record.key, result[0])
2252
self.assertEqual(record.sha1, result[1])
2253
self.assertEqual(record.storage_kind, result[2])
2254
self.assertEqual(record.get_bytes_as(record.storage_kind), result[3])
2255
# It's not strictly minimal, but it seems reasonable for now for it to
2256
# ask which fallbacks have which parents.
2258
("get_parent_map", set([key_basis, key_basis_2, key_missing])),
2259
("get_record_stream", [key_basis_2, key_basis], 'topological', False)],
2262
def test_get_sha1s(self):
2263
# sha1's in the test knit are answered without asking the basis
2264
basis, test = self.get_basis_and_test_knit()
2266
key_basis = ('bar',)
2267
key_missing = ('missing',)
2268
test.add_lines(key, (), ['foo\n'])
2269
key_sha1sum = osutils.sha('foo\n').hexdigest()
2270
sha1s = test.get_sha1s([key])
2271
self.assertEqual({key: key_sha1sum}, sha1s)
2272
self.assertEqual([], basis.calls)
2273
# But texts that are not in the test knit are looked for in the basis
2274
# directly (rather than via text reconstruction) so that remote servers
2275
# etc don't have to answer with full content.
2276
basis.add_lines(key_basis, (), ['foo\n', 'bar\n'])
2277
basis_sha1sum = osutils.sha('foo\nbar\n').hexdigest()
2279
sha1s = test.get_sha1s([key, key_missing, key_basis])
2280
self.assertEqual({key: key_sha1sum,
2281
key_basis: basis_sha1sum}, sha1s)
2282
self.assertEqual([("get_sha1s", set([key_basis, key_missing]))],
2285
def test_insert_record_stream(self):
2286
# records are inserted as normal; insert_record_stream builds on
2287
# add_lines, so a smoke test should be all that's needed:
2289
key_basis = ('bar',)
2290
key_delta = ('zaphod',)
2291
basis, test = self.get_basis_and_test_knit()
2292
source = self.make_test_knit(name='source')
2293
basis.add_lines(key_basis, (), ['foo\n'])
2295
source.add_lines(key_basis, (), ['foo\n'])
2296
source.add_lines(key_delta, (key_basis,), ['bar\n'])
2297
stream = source.get_record_stream([key_delta], 'unordered', False)
2298
test.insert_record_stream(stream)
2299
# XXX: this does somewhat too many calls in making sure of whether it
2300
# has to recreate the full text.
2301
self.assertEqual([("get_parent_map", set([key_basis])),
2302
('get_parent_map', set([key_basis])),
2303
('get_record_stream', [key_basis], 'unordered', True)],
2305
self.assertEqual({key_delta:(key_basis,)},
2306
test.get_parent_map([key_delta]))
2307
self.assertEqual('bar\n', test.get_record_stream([key_delta],
2308
'unordered', True).next().get_bytes_as('fulltext'))
2310
def test_iter_lines_added_or_present_in_keys(self):
2311
# Lines from the basis are returned, and lines for a given key are only
2315
# all sources are asked for keys:
2316
basis, test = self.get_basis_and_test_knit()
2317
basis.add_lines(key1, (), ["foo"])
2319
lines = list(test.iter_lines_added_or_present_in_keys([key1]))
2320
self.assertEqual([("foo\n", key1)], lines)
2321
self.assertEqual([("iter_lines_added_or_present_in_keys", set([key1]))],
2323
# keys in both are not duplicated:
2324
test.add_lines(key2, (), ["bar\n"])
2325
basis.add_lines(key2, (), ["bar\n"])
2327
lines = list(test.iter_lines_added_or_present_in_keys([key2]))
2328
self.assertEqual([("bar\n", key2)], lines)
2329
self.assertEqual([], basis.calls)
2331
def test_keys(self):
2334
# all sources are asked for keys:
2335
basis, test = self.get_basis_and_test_knit()
2337
self.assertEqual(set(), set(keys))
2338
self.assertEqual([("keys",)], basis.calls)
2339
# keys from a basis are returned:
2340
basis.add_lines(key1, (), [])
2343
self.assertEqual(set([key1]), set(keys))
2344
self.assertEqual([("keys",)], basis.calls)
2345
# keys in both are not duplicated:
2346
test.add_lines(key2, (), [])
2347
basis.add_lines(key2, (), [])
2350
self.assertEqual(2, len(keys))
2351
self.assertEqual(set([key1, key2]), set(keys))
2352
self.assertEqual([("keys",)], basis.calls)
2354
def test_add_mpdiffs(self):
2355
# records are inserted as normal; add_mpdiff builds on
2356
# add_lines, so a smoke test should be all that's needed:
2358
key_basis = ('bar',)
2359
key_delta = ('zaphod',)
2360
basis, test = self.get_basis_and_test_knit()
2361
source = self.make_test_knit(name='source')
2362
basis.add_lines(key_basis, (), ['foo\n'])
2364
source.add_lines(key_basis, (), ['foo\n'])
2365
source.add_lines(key_delta, (key_basis,), ['bar\n'])
2366
diffs = source.make_mpdiffs([key_delta])
2367
test.add_mpdiffs([(key_delta, (key_basis,),
2368
source.get_sha1s([key_delta])[key_delta], diffs[0])])
2369
self.assertEqual([("get_parent_map", set([key_basis])),
2370
('get_record_stream', [key_basis], 'unordered', True),],
2372
self.assertEqual({key_delta:(key_basis,)},
2373
test.get_parent_map([key_delta]))
2374
self.assertEqual('bar\n', test.get_record_stream([key_delta],
2375
'unordered', True).next().get_bytes_as('fulltext'))
2377
def test_make_mpdiffs(self):
2378
# Generating an mpdiff across a stacking boundary should detect parent
2382
key_right = ('zaphod',)
2383
basis, test = self.get_basis_and_test_knit()
2384
basis.add_lines(key_left, (), ['bar\n'])
2385
basis.add_lines(key_right, (), ['zaphod\n'])
2387
test.add_lines(key, (key_left, key_right),
2388
['bar\n', 'foo\n', 'zaphod\n'])
2389
diffs = test.make_mpdiffs([key])
2391
multiparent.MultiParent([multiparent.ParentText(0, 0, 0, 1),
2392
multiparent.NewText(['foo\n']),
2393
multiparent.ParentText(1, 0, 2, 1)])],
2395
self.assertEqual(3, len(basis.calls))
2397
("get_parent_map", set([key_left, key_right])),
2398
("get_parent_map", set([key_left, key_right])),
2401
last_call = basis.calls[-1]
2402
self.assertEqual('get_record_stream', last_call[0])
2403
self.assertEqual(set([key_left, key_right]), set(last_call[1]))
2404
self.assertEqual('unordered', last_call[2])
2405
self.assertEqual(True, last_call[3])
2408
class TestNetworkBehaviour(KnitTests):
2409
"""Tests for getting data out of/into knits over the network."""
2411
def test_include_delta_closure_generates_a_knit_delta_closure(self):
2412
vf = self.make_test_knit(name='test')
2413
# put in three texts, giving ft, delta, delta
2414
vf.add_lines(('base',), (), ['base\n', 'content\n'])
2415
vf.add_lines(('d1',), (('base',),), ['d1\n'])
2416
vf.add_lines(('d2',), (('d1',),), ['d2\n'])
2417
# But heuristics could interfere, so check what happened:
2418
self.assertEqual(['knit-ft-gz', 'knit-delta-gz', 'knit-delta-gz'],
2419
[record.storage_kind for record in
2420
vf.get_record_stream([('base',), ('d1',), ('d2',)],
2421
'topological', False)])
2422
# generate a stream of just the deltas include_delta_closure=True,
2423
# serialise to the network, and check that we get a delta closure on the wire.
2424
stream = vf.get_record_stream([('d1',), ('d2',)], 'topological', True)
2425
netb = [record.get_bytes_as(record.storage_kind) for record in stream]
2426
# The first bytes should be a memo from _ContentMapGenerator, and the
2427
# second bytes should be empty (because its a API proxy not something
2428
# for wire serialisation.
2429
self.assertEqual('', netb[1])
2431
kind, line_end = network_bytes_to_kind_and_offset(bytes)
2432
self.assertEqual('knit-delta-closure', kind)
2435
class TestContentMapGenerator(KnitTests):
2436
"""Tests for ContentMapGenerator"""
2438
def test_get_record_stream_gives_records(self):
2439
vf = self.make_test_knit(name='test')
2440
# put in three texts, giving ft, delta, delta
2441
vf.add_lines(('base',), (), ['base\n', 'content\n'])
2442
vf.add_lines(('d1',), (('base',),), ['d1\n'])
2443
vf.add_lines(('d2',), (('d1',),), ['d2\n'])
2444
keys = [('d1',), ('d2',)]
2445
generator = _VFContentMapGenerator(vf, keys,
2446
global_map=vf.get_parent_map(keys))
2447
for record in generator.get_record_stream():
2448
if record.key == ('d1',):
2449
self.assertEqual('d1\n', record.get_bytes_as('fulltext'))
2451
self.assertEqual('d2\n', record.get_bytes_as('fulltext'))
2453
def test_get_record_stream_kinds_are_raw(self):
2454
vf = self.make_test_knit(name='test')
2455
# put in three texts, giving ft, delta, delta
2456
vf.add_lines(('base',), (), ['base\n', 'content\n'])
2457
vf.add_lines(('d1',), (('base',),), ['d1\n'])
2458
vf.add_lines(('d2',), (('d1',),), ['d2\n'])
2459
keys = [('base',), ('d1',), ('d2',)]
2460
generator = _VFContentMapGenerator(vf, keys,
2461
global_map=vf.get_parent_map(keys))
2462
kinds = {('base',): 'knit-delta-closure',
2463
('d1',): 'knit-delta-closure-ref',
2464
('d2',): 'knit-delta-closure-ref',
2466
for record in generator.get_record_stream():
2467
self.assertEqual(kinds[record.key], record.storage_kind)