487
340
writer = pack.ContainerWriter(write_data)
489
342
access.set_writer(writer, index, (transport, packname))
490
memos = access.add_raw_records([('key', 10)], '1234567890')
343
memos = access.add_raw_records([10], '1234567890')
492
345
self.assertEqual(['1234567890'], list(access.get_raw_records(memos)))
494
def test_missing_index_raises_retry(self):
495
memos = self.make_pack_file()
496
transport = self.get_transport()
497
reload_called, reload_func = self.make_reload_func()
498
# Note that the index key has changed from 'foo' to 'bar'
499
access = pack_repo._DirectPackAccess({'bar':(transport, 'packname')},
500
reload_func=reload_func)
501
e = self.assertListRaises(errors.RetryWithNewPacks,
502
access.get_raw_records, memos)
503
# Because a key was passed in which does not match our index list, we
504
# assume that the listing was already reloaded
505
self.assertTrue(e.reload_occurred)
506
self.assertIsInstance(e.exc_info, tuple)
507
self.assertIs(e.exc_info[0], KeyError)
508
self.assertIsInstance(e.exc_info[1], KeyError)
510
def test_missing_index_raises_key_error_with_no_reload(self):
511
memos = self.make_pack_file()
512
transport = self.get_transport()
513
# Note that the index key has changed from 'foo' to 'bar'
514
access = pack_repo._DirectPackAccess({'bar':(transport, 'packname')})
515
e = self.assertListRaises(KeyError, access.get_raw_records, memos)
517
def test_missing_file_raises_retry(self):
518
memos = self.make_pack_file()
519
transport = self.get_transport()
520
reload_called, reload_func = self.make_reload_func()
521
# Note that the 'filename' has been changed to 'different-packname'
522
access = pack_repo._DirectPackAccess(
523
{'foo':(transport, 'different-packname')},
524
reload_func=reload_func)
525
e = self.assertListRaises(errors.RetryWithNewPacks,
526
access.get_raw_records, memos)
527
# The file has gone missing, so we assume we need to reload
528
self.assertFalse(e.reload_occurred)
529
self.assertIsInstance(e.exc_info, tuple)
530
self.assertIs(e.exc_info[0], errors.NoSuchFile)
531
self.assertIsInstance(e.exc_info[1], errors.NoSuchFile)
532
self.assertEqual('different-packname', e.exc_info[1].path)
534
def test_missing_file_raises_no_such_file_with_no_reload(self):
535
memos = self.make_pack_file()
536
transport = self.get_transport()
537
# Note that the 'filename' has been changed to 'different-packname'
538
access = pack_repo._DirectPackAccess(
539
{'foo': (transport, 'different-packname')})
540
e = self.assertListRaises(errors.NoSuchFile,
541
access.get_raw_records, memos)
543
def test_failing_readv_raises_retry(self):
544
memos = self.make_pack_file()
545
transport = self.get_transport()
546
failing_transport = MockReadvFailingTransport(
547
[transport.get_bytes('packname')])
548
reload_called, reload_func = self.make_reload_func()
549
access = pack_repo._DirectPackAccess(
550
{'foo': (failing_transport, 'packname')},
551
reload_func=reload_func)
552
# Asking for a single record will not trigger the Mock failure
553
self.assertEqual(['1234567890'],
554
list(access.get_raw_records(memos[:1])))
555
self.assertEqual(['12345'],
556
list(access.get_raw_records(memos[1:2])))
557
# A multiple offset readv() will fail mid-way through
558
e = self.assertListRaises(errors.RetryWithNewPacks,
559
access.get_raw_records, memos)
560
# The file has gone missing, so we assume we need to reload
561
self.assertFalse(e.reload_occurred)
562
self.assertIsInstance(e.exc_info, tuple)
563
self.assertIs(e.exc_info[0], errors.NoSuchFile)
564
self.assertIsInstance(e.exc_info[1], errors.NoSuchFile)
565
self.assertEqual('packname', e.exc_info[1].path)
567
def test_failing_readv_raises_no_such_file_with_no_reload(self):
568
memos = self.make_pack_file()
569
transport = self.get_transport()
570
failing_transport = MockReadvFailingTransport(
571
[transport.get_bytes('packname')])
572
reload_called, reload_func = self.make_reload_func()
573
access = pack_repo._DirectPackAccess(
574
{'foo':(failing_transport, 'packname')})
575
# Asking for a single record will not trigger the Mock failure
576
self.assertEqual(['1234567890'],
577
list(access.get_raw_records(memos[:1])))
578
self.assertEqual(['12345'],
579
list(access.get_raw_records(memos[1:2])))
580
# A multiple offset readv() will fail mid-way through
581
e = self.assertListRaises(errors.NoSuchFile,
582
access.get_raw_records, memos)
584
def test_reload_or_raise_no_reload(self):
585
access = pack_repo._DirectPackAccess({}, reload_func=None)
586
retry_exc = self.make_retry_exception()
587
# Without a reload_func, we will just re-raise the original exception
588
self.assertRaises(_TestException, access.reload_or_raise, retry_exc)
590
def test_reload_or_raise_reload_changed(self):
591
reload_called, reload_func = self.make_reload_func(return_val=True)
592
access = pack_repo._DirectPackAccess({}, reload_func=reload_func)
593
retry_exc = self.make_retry_exception()
594
access.reload_or_raise(retry_exc)
595
self.assertEqual([1], reload_called)
596
retry_exc.reload_occurred=True
597
access.reload_or_raise(retry_exc)
598
self.assertEqual([2], reload_called)
600
def test_reload_or_raise_reload_no_change(self):
601
reload_called, reload_func = self.make_reload_func(return_val=False)
602
access = pack_repo._DirectPackAccess({}, reload_func=reload_func)
603
retry_exc = self.make_retry_exception()
604
# If reload_occurred is False, then we consider it an error to have
605
# reload_func() return False (no changes).
606
self.assertRaises(_TestException, access.reload_or_raise, retry_exc)
607
self.assertEqual([1], reload_called)
608
retry_exc.reload_occurred=True
609
# If reload_occurred is True, then we assume nothing changed because
610
# it had changed earlier, but didn't change again
611
access.reload_or_raise(retry_exc)
612
self.assertEqual([2], reload_called)
614
def test_annotate_retries(self):
615
vf, reload_counter = self.make_vf_for_retrying()
616
# It is a little bit bogus to annotate the Revision VF, but it works,
617
# as we have ancestry stored there
619
reload_lines = vf.annotate(key)
620
self.assertEqual([1, 1, 0], reload_counter)
621
plain_lines = vf.annotate(key)
622
self.assertEqual([1, 1, 0], reload_counter) # No extra reloading
623
if reload_lines != plain_lines:
624
self.fail('Annotation was not identical with reloading.')
625
# Now delete the packs-in-use, which should trigger another reload, but
626
# this time we just raise an exception because we can't recover
627
for trans, name in vf._access._indices.itervalues():
629
self.assertRaises(errors.NoSuchFile, vf.annotate, key)
630
self.assertEqual([2, 1, 1], reload_counter)
632
def test__get_record_map_retries(self):
633
vf, reload_counter = self.make_vf_for_retrying()
634
keys = [('rev-1',), ('rev-2',), ('rev-3',)]
635
records = vf._get_record_map(keys)
636
self.assertEqual(keys, sorted(records.keys()))
637
self.assertEqual([1, 1, 0], reload_counter)
638
# Now delete the packs-in-use, which should trigger another reload, but
639
# this time we just raise an exception because we can't recover
640
for trans, name in vf._access._indices.itervalues():
642
self.assertRaises(errors.NoSuchFile, vf._get_record_map, keys)
643
self.assertEqual([2, 1, 1], reload_counter)
645
def test_get_record_stream_retries(self):
646
vf, reload_counter = self.make_vf_for_retrying()
647
keys = [('rev-1',), ('rev-2',), ('rev-3',)]
648
record_stream = vf.get_record_stream(keys, 'topological', False)
649
record = record_stream.next()
650
self.assertEqual(('rev-1',), record.key)
651
self.assertEqual([0, 0, 0], reload_counter)
652
record = record_stream.next()
653
self.assertEqual(('rev-2',), record.key)
654
self.assertEqual([1, 1, 0], reload_counter)
655
record = record_stream.next()
656
self.assertEqual(('rev-3',), record.key)
657
self.assertEqual([1, 1, 0], reload_counter)
658
# Now delete all pack files, and see that we raise the right error
659
for trans, name in vf._access._indices.itervalues():
661
self.assertListRaises(errors.NoSuchFile,
662
vf.get_record_stream, keys, 'topological', False)
664
def test_iter_lines_added_or_present_in_keys_retries(self):
665
vf, reload_counter = self.make_vf_for_retrying()
666
keys = [('rev-1',), ('rev-2',), ('rev-3',)]
667
# Unfortunately, iter_lines_added_or_present_in_keys iterates the
668
# result in random order (determined by the iteration order from a
669
# set()), so we don't have any solid way to trigger whether data is
670
# read before or after. However we tried to delete the middle node to
671
# exercise the code well.
672
# What we care about is that all lines are always yielded, but not
675
reload_lines = sorted(vf.iter_lines_added_or_present_in_keys(keys))
676
self.assertEqual([1, 1, 0], reload_counter)
677
# Now do it again, to make sure the result is equivalent
678
plain_lines = sorted(vf.iter_lines_added_or_present_in_keys(keys))
679
self.assertEqual([1, 1, 0], reload_counter) # No extra reloading
680
self.assertEqual(plain_lines, reload_lines)
681
self.assertEqual(21, len(plain_lines))
682
# Now delete all pack files, and see that we raise the right error
683
for trans, name in vf._access._indices.itervalues():
685
self.assertListRaises(errors.NoSuchFile,
686
vf.iter_lines_added_or_present_in_keys, keys)
687
self.assertEqual([2, 1, 1], reload_counter)
689
def test_get_record_stream_yields_disk_sorted_order(self):
690
# if we get 'unordered' pick a semi-optimal order for reading. The
691
# order should be grouped by pack file, and then by position in file
692
repo = self.make_repository('test', format='pack-0.92')
694
self.addCleanup(repo.unlock)
695
repo.start_write_group()
697
vf.add_lines(('f-id', 'rev-5'), [('f-id', 'rev-4')], ['lines\n'])
698
vf.add_lines(('f-id', 'rev-1'), [], ['lines\n'])
699
vf.add_lines(('f-id', 'rev-2'), [('f-id', 'rev-1')], ['lines\n'])
700
repo.commit_write_group()
701
# We inserted them as rev-5, rev-1, rev-2, we should get them back in
703
stream = vf.get_record_stream([('f-id', 'rev-1'), ('f-id', 'rev-5'),
704
('f-id', 'rev-2')], 'unordered', False)
705
keys = [r.key for r in stream]
706
self.assertEqual([('f-id', 'rev-5'), ('f-id', 'rev-1'),
707
('f-id', 'rev-2')], keys)
708
repo.start_write_group()
709
vf.add_lines(('f-id', 'rev-4'), [('f-id', 'rev-3')], ['lines\n'])
710
vf.add_lines(('f-id', 'rev-3'), [('f-id', 'rev-2')], ['lines\n'])
711
vf.add_lines(('f-id', 'rev-6'), [('f-id', 'rev-5')], ['lines\n'])
712
repo.commit_write_group()
713
# Request in random order, to make sure the output order isn't based on
715
request_keys = set(('f-id', 'rev-%d' % i) for i in range(1, 7))
716
stream = vf.get_record_stream(request_keys, 'unordered', False)
717
keys = [r.key for r in stream]
718
# We want to get the keys back in disk order, but it doesn't matter
719
# which pack we read from first. So this can come back in 2 orders
720
alt1 = [('f-id', 'rev-%d' % i) for i in [4, 3, 6, 5, 1, 2]]
721
alt2 = [('f-id', 'rev-%d' % i) for i in [5, 1, 2, 4, 3, 6]]
722
if keys != alt1 and keys != alt2:
723
self.fail('Returned key order did not match either expected order.'
724
' expected %s or %s, not %s'
725
% (alt1, alt2, keys))
728
348
class LowLevelKnitDataTests(TestCase):
877
454
# Change 2 bytes in the middle to \xff
878
455
gz_txt = gz_txt[:10] + '\xff\xff' + gz_txt[12:]
879
456
transport = MockTransport([gz_txt])
880
access = _KnitKeyAccess(transport, ConstantMapper('filename'))
881
knit = KnitVersionedFiles(None, access)
882
records = [(('rev-id-1',), (('rev-id-1',), 0, len(gz_txt)))]
883
self.assertRaises(errors.KnitCorrupt, list,
884
knit._read_records_iter(records))
885
# read_records_iter_raw will barf on bad gz data
886
self.assertRaises(errors.KnitCorrupt, list,
887
knit._read_records_iter_raw(records))
457
access = _KnitAccess(transport, 'filename', None, None, False, False)
458
data = _KnitData(access=access)
459
records = [('rev-id-1', (None, 0, len(gz_txt)))]
461
self.assertRaises(errors.KnitCorrupt, data.read_records, records)
463
# read_records_iter_raw will notice if we request the wrong version.
464
self.assertRaises(errors.KnitCorrupt, list,
465
data.read_records_iter_raw(records))
890
468
class LowLevelKnitIndexTests(TestCase):
892
def get_knit_index(self, transport, name, mode):
893
mapper = ConstantMapper(name)
470
def get_knit_index(self, *args, **kwargs):
471
orig = knit._load_data
473
knit._load_data = orig
474
self.addCleanup(reset)
894
475
from bzrlib._knit_load_data_py import _load_data_py
895
self.overrideAttr(knit, '_load_data', _load_data_py)
896
allow_writes = lambda: 'w' in mode
897
return _KndxIndex(transport, mapper, lambda:None, allow_writes, lambda:True)
476
knit._load_data = _load_data_py
477
return _KnitIndex(get_scope=lambda:None, *args, **kwargs)
479
def test_no_such_file(self):
480
transport = MockTransport()
482
self.assertRaises(NoSuchFile, self.get_knit_index,
483
transport, "filename", "r")
484
self.assertRaises(NoSuchFile, self.get_knit_index,
485
transport, "filename", "w", create=False)
899
487
def test_create_file(self):
900
488
transport = MockTransport()
901
index = self.get_knit_index(transport, "filename", "w")
903
call = transport.calls.pop(0)
904
# call[1][1] is a StringIO - we can't test it by simple equality.
905
self.assertEqual('put_file_non_atomic', call[0])
906
self.assertEqual('filename.kndx', call[1][0])
907
# With no history, _KndxIndex writes a new index:
908
self.assertEqual(_KndxIndex.HEADER,
909
call[1][1].getvalue())
910
self.assertEqual({'create_parent_dir': True}, call[2])
490
index = self.get_knit_index(transport, "filename", "w",
491
file_mode="wb", create=True)
493
("put_bytes_non_atomic",
494
("filename", index.HEADER), {"mode": "wb"}),
495
transport.calls.pop(0))
497
def test_delay_create_file(self):
498
transport = MockTransport()
500
index = self.get_knit_index(transport, "filename", "w",
501
create=True, file_mode="wb", create_parent_dir=True,
502
delay_create=True, dir_mode=0777)
503
self.assertEqual([], transport.calls)
505
index.add_versions([])
506
name, (filename, f), kwargs = transport.calls.pop(0)
507
self.assertEqual("put_file_non_atomic", name)
509
{"dir_mode": 0777, "create_parent_dir": True, "mode": "wb"},
511
self.assertEqual("filename", filename)
512
self.assertEqual(index.HEADER, f.read())
514
index.add_versions([])
515
self.assertEqual(("append_bytes", ("filename", ""), {}),
516
transport.calls.pop(0))
912
518
def test_read_utf8_version_id(self):
913
519
unicode_revision_id = u"version-\N{CYRILLIC CAPITAL LETTER A}"
914
520
utf8_revision_id = unicode_revision_id.encode('utf-8')
915
521
transport = MockTransport([
917
523
'%s option 0 1 :' % (utf8_revision_id,)
919
525
index = self.get_knit_index(transport, "filename", "r")
920
# _KndxIndex is a private class, and deals in utf8 revision_ids, not
526
# _KnitIndex is a private class, and deals in utf8 revision_ids, not
921
527
# Unicode revision_ids.
922
self.assertEqual({(utf8_revision_id,):()},
923
index.get_parent_map(index.keys()))
924
self.assertFalse((unicode_revision_id,) in index.keys())
528
self.assertTrue(index.has_version(utf8_revision_id))
529
self.assertFalse(index.has_version(unicode_revision_id))
926
531
def test_read_utf8_parents(self):
927
532
unicode_revision_id = u"version-\N{CYRILLIC CAPITAL LETTER A}"
928
533
utf8_revision_id = unicode_revision_id.encode('utf-8')
929
534
transport = MockTransport([
931
536
"version option 0 1 .%s :" % (utf8_revision_id,)
933
538
index = self.get_knit_index(transport, "filename", "r")
934
self.assertEqual({("version",):((utf8_revision_id,),)},
935
index.get_parent_map(index.keys()))
539
self.assertEqual((utf8_revision_id,),
540
index.get_parents_with_ghosts("version"))
937
542
def test_read_ignore_corrupted_lines(self):
938
543
transport = MockTransport([
941
546
"corrupted options 0 1 .b .c ",
942
547
"version options 0 1 :"
944
549
index = self.get_knit_index(transport, "filename", "r")
945
self.assertEqual(1, len(index.keys()))
946
self.assertEqual(set([("version",)]), index.keys())
550
self.assertEqual(1, index.num_versions())
551
self.assertTrue(index.has_version("version"))
948
553
def test_read_corrupted_header(self):
949
554
transport = MockTransport(['not a bzr knit index header\n'])
950
index = self.get_knit_index(transport, "filename", "r")
951
self.assertRaises(KnitHeaderError, index.keys)
555
self.assertRaises(KnitHeaderError,
556
self.get_knit_index, transport, "filename", "r")
953
558
def test_read_duplicate_entries(self):
954
559
transport = MockTransport([
956
561
"parent options 0 1 :",
957
562
"version options1 0 1 0 :",
958
563
"version options2 1 2 .other :",
959
564
"version options3 3 4 0 .other :"
961
566
index = self.get_knit_index(transport, "filename", "r")
962
self.assertEqual(2, len(index.keys()))
567
self.assertEqual(2, index.num_versions())
963
568
# check that the index used is the first one written. (Specific
964
569
# to KnitIndex style indices.
965
self.assertEqual("1", index._dictionary_compress([("version",)]))
966
self.assertEqual((("version",), 3, 4), index.get_position(("version",)))
967
self.assertEqual(["options3"], index.get_options(("version",)))
968
self.assertEqual({("version",):(("parent",), ("other",))},
969
index.get_parent_map([("version",)]))
570
self.assertEqual("1", index._version_list_to_index(["version"]))
571
self.assertEqual((None, 3, 4), index.get_position("version"))
572
self.assertEqual(["options3"], index.get_options("version"))
573
self.assertEqual(("parent", "other"),
574
index.get_parents_with_ghosts("version"))
971
576
def test_read_compressed_parents(self):
972
577
transport = MockTransport([
974
579
"a option 0 1 :",
975
580
"b option 0 1 0 :",
976
581
"c option 0 1 1 0 :",
978
583
index = self.get_knit_index(transport, "filename", "r")
979
self.assertEqual({("b",):(("a",),), ("c",):(("b",), ("a",))},
980
index.get_parent_map([("b",), ("c",)]))
584
self.assertEqual({"b":("a",), "c":("b", "a")},
585
index.get_parent_map(["b", "c"]))
982
587
def test_write_utf8_version_id(self):
983
588
unicode_revision_id = u"version-\N{CYRILLIC CAPITAL LETTER A}"
984
589
utf8_revision_id = unicode_revision_id.encode('utf-8')
985
590
transport = MockTransport([
988
593
index = self.get_knit_index(transport, "filename", "r")
990
((utf8_revision_id,), ["option"], ((utf8_revision_id,), 0, 1), [])])
991
call = transport.calls.pop(0)
992
# call[1][1] is a StringIO - we can't test it by simple equality.
993
self.assertEqual('put_file_non_atomic', call[0])
994
self.assertEqual('filename.kndx', call[1][0])
995
# With no history, _KndxIndex writes a new index:
996
self.assertEqual(_KndxIndex.HEADER +
997
"\n%s option 0 1 :" % (utf8_revision_id,),
998
call[1][1].getvalue())
999
self.assertEqual({'create_parent_dir': True}, call[2])
594
index.add_version(utf8_revision_id, ["option"], (None, 0, 1), [])
595
self.assertEqual(("append_bytes", ("filename",
596
"\n%s option 0 1 :" % (utf8_revision_id,)),
598
transport.calls.pop(0))
1001
600
def test_write_utf8_parents(self):
1002
601
unicode_revision_id = u"version-\N{CYRILLIC CAPITAL LETTER A}"
1003
602
utf8_revision_id = unicode_revision_id.encode('utf-8')
1004
603
transport = MockTransport([
1007
index = self.get_knit_index(transport, "filename", "r")
1009
(("version",), ["option"], (("version",), 0, 1), [(utf8_revision_id,)])])
1010
call = transport.calls.pop(0)
1011
# call[1][1] is a StringIO - we can't test it by simple equality.
1012
self.assertEqual('put_file_non_atomic', call[0])
1013
self.assertEqual('filename.kndx', call[1][0])
1014
# With no history, _KndxIndex writes a new index:
1015
self.assertEqual(_KndxIndex.HEADER +
1016
"\nversion option 0 1 .%s :" % (utf8_revision_id,),
1017
call[1][1].getvalue())
1018
self.assertEqual({'create_parent_dir': True}, call[2])
1020
def test_keys(self):
1021
transport = MockTransport([
1024
index = self.get_knit_index(transport, "filename", "r")
1026
self.assertEqual(set(), index.keys())
1028
index.add_records([(("a",), ["option"], (("a",), 0, 1), [])])
1029
self.assertEqual(set([("a",)]), index.keys())
1031
index.add_records([(("a",), ["option"], (("a",), 0, 1), [])])
1032
self.assertEqual(set([("a",)]), index.keys())
1034
index.add_records([(("b",), ["option"], (("b",), 0, 1), [])])
1035
self.assertEqual(set([("a",), ("b",)]), index.keys())
1037
def add_a_b(self, index, random_id=None):
1039
if random_id is not None:
1040
kwargs["random_id"] = random_id
1042
(("a",), ["option"], (("a",), 0, 1), [("b",)]),
1043
(("a",), ["opt"], (("a",), 1, 2), [("c",)]),
1044
(("b",), ["option"], (("b",), 2, 3), [("a",)])
1047
def assertIndexIsAB(self, index):
1052
index.get_parent_map(index.keys()))
1053
self.assertEqual((("a",), 1, 2), index.get_position(("a",)))
1054
self.assertEqual((("b",), 2, 3), index.get_position(("b",)))
1055
self.assertEqual(["opt"], index.get_options(("a",)))
606
index = self.get_knit_index(transport, "filename", "r")
607
index.add_version("version", ["option"], (None, 0, 1), [utf8_revision_id])
608
self.assertEqual(("append_bytes", ("filename",
609
"\nversion option 0 1 .%s :" % (utf8_revision_id,)),
611
transport.calls.pop(0))
613
def test_get_ancestry(self):
614
transport = MockTransport([
617
"b option 0 1 0 .e :",
618
"c option 0 1 1 0 :",
619
"d option 0 1 2 .f :"
621
index = self.get_knit_index(transport, "filename", "r")
623
self.assertEqual([], index.get_ancestry([]))
624
self.assertEqual(["a"], index.get_ancestry(["a"]))
625
self.assertEqual(["a", "b"], index.get_ancestry(["b"]))
626
self.assertEqual(["a", "b", "c"], index.get_ancestry(["c"]))
627
self.assertEqual(["a", "b", "c", "d"], index.get_ancestry(["d"]))
628
self.assertEqual(["a", "b"], index.get_ancestry(["a", "b"]))
629
self.assertEqual(["a", "b", "c"], index.get_ancestry(["a", "c"]))
631
self.assertRaises(RevisionNotPresent, index.get_ancestry, ["e"])
633
def test_get_ancestry_with_ghosts(self):
634
transport = MockTransport([
637
"b option 0 1 0 .e :",
638
"c option 0 1 0 .f .g :",
639
"d option 0 1 2 .h .j .k :"
641
index = self.get_knit_index(transport, "filename", "r")
643
self.assertEqual([], index.get_ancestry_with_ghosts([]))
644
self.assertEqual(["a"], index.get_ancestry_with_ghosts(["a"]))
645
self.assertEqual(["a", "e", "b"],
646
index.get_ancestry_with_ghosts(["b"]))
647
self.assertEqual(["a", "g", "f", "c"],
648
index.get_ancestry_with_ghosts(["c"]))
649
self.assertEqual(["a", "g", "f", "c", "k", "j", "h", "d"],
650
index.get_ancestry_with_ghosts(["d"]))
651
self.assertEqual(["a", "e", "b"],
652
index.get_ancestry_with_ghosts(["a", "b"]))
653
self.assertEqual(["a", "g", "f", "c"],
654
index.get_ancestry_with_ghosts(["a", "c"]))
656
["a", "g", "f", "c", "e", "b", "k", "j", "h", "d"],
657
index.get_ancestry_with_ghosts(["b", "d"]))
659
self.assertRaises(RevisionNotPresent,
660
index.get_ancestry_with_ghosts, ["e"])
662
def test_num_versions(self):
663
transport = MockTransport([
666
index = self.get_knit_index(transport, "filename", "r")
668
self.assertEqual(0, index.num_versions())
669
self.assertEqual(0, len(index))
671
index.add_version("a", ["option"], (None, 0, 1), [])
672
self.assertEqual(1, index.num_versions())
673
self.assertEqual(1, len(index))
675
index.add_version("a", ["option2"], (None, 1, 2), [])
676
self.assertEqual(1, index.num_versions())
677
self.assertEqual(1, len(index))
679
index.add_version("b", ["option"], (None, 0, 1), [])
680
self.assertEqual(2, index.num_versions())
681
self.assertEqual(2, len(index))
683
def test_get_versions(self):
684
transport = MockTransport([
687
index = self.get_knit_index(transport, "filename", "r")
689
self.assertEqual([], index.get_versions())
691
index.add_version("a", ["option"], (None, 0, 1), [])
692
self.assertEqual(["a"], index.get_versions())
694
index.add_version("a", ["option"], (None, 0, 1), [])
695
self.assertEqual(["a"], index.get_versions())
697
index.add_version("b", ["option"], (None, 0, 1), [])
698
self.assertEqual(["a", "b"], index.get_versions())
700
def test_add_version(self):
701
transport = MockTransport([
704
index = self.get_knit_index(transport, "filename", "r")
706
index.add_version("a", ["option"], (None, 0, 1), ["b"])
707
self.assertEqual(("append_bytes",
708
("filename", "\na option 0 1 .b :"),
709
{}), transport.calls.pop(0))
710
self.assertTrue(index.has_version("a"))
711
self.assertEqual(1, index.num_versions())
712
self.assertEqual((None, 0, 1), index.get_position("a"))
713
self.assertEqual(["option"], index.get_options("a"))
714
self.assertEqual(("b",), index.get_parents_with_ghosts("a"))
716
index.add_version("a", ["opt"], (None, 1, 2), ["c"])
717
self.assertEqual(("append_bytes",
718
("filename", "\na opt 1 2 .c :"),
719
{}), transport.calls.pop(0))
720
self.assertTrue(index.has_version("a"))
721
self.assertEqual(1, index.num_versions())
722
self.assertEqual((None, 1, 2), index.get_position("a"))
723
self.assertEqual(["opt"], index.get_options("a"))
724
self.assertEqual(("c",), index.get_parents_with_ghosts("a"))
726
index.add_version("b", ["option"], (None, 2, 3), ["a"])
727
self.assertEqual(("append_bytes",
728
("filename", "\nb option 2 3 0 :"),
729
{}), transport.calls.pop(0))
730
self.assertTrue(index.has_version("b"))
731
self.assertEqual(2, index.num_versions())
732
self.assertEqual((None, 2, 3), index.get_position("b"))
733
self.assertEqual(["option"], index.get_options("b"))
734
self.assertEqual(("a",), index.get_parents_with_ghosts("b"))
1057
736
def test_add_versions(self):
1058
737
transport = MockTransport([
1061
740
index = self.get_knit_index(transport, "filename", "r")
1064
call = transport.calls.pop(0)
1065
# call[1][1] is a StringIO - we can't test it by simple equality.
1066
self.assertEqual('put_file_non_atomic', call[0])
1067
self.assertEqual('filename.kndx', call[1][0])
1068
# With no history, _KndxIndex writes a new index:
743
("a", ["option"], (None, 0, 1), ["b"]),
744
("a", ["opt"], (None, 1, 2), ["c"]),
745
("b", ["option"], (None, 2, 3), ["a"])
747
self.assertEqual(("append_bytes", ("filename",
1071
748
"\na option 0 1 .b :"
1072
749
"\na opt 1 2 .c :"
1073
"\nb option 2 3 0 :",
1074
call[1][1].getvalue())
1075
self.assertEqual({'create_parent_dir': True}, call[2])
1076
self.assertIndexIsAB(index)
751
), {}), transport.calls.pop(0))
752
self.assertTrue(index.has_version("a"))
753
self.assertTrue(index.has_version("b"))
754
self.assertEqual(2, index.num_versions())
755
self.assertEqual((None, 1, 2), index.get_position("a"))
756
self.assertEqual((None, 2, 3), index.get_position("b"))
757
self.assertEqual(["opt"], index.get_options("a"))
758
self.assertEqual(["option"], index.get_options("b"))
759
self.assertEqual(("c",), index.get_parents_with_ghosts("a"))
760
self.assertEqual(("a",), index.get_parents_with_ghosts("b"))
1078
762
def test_add_versions_random_id_is_accepted(self):
1079
763
transport = MockTransport([
1082
766
index = self.get_knit_index(transport, "filename", "r")
1083
self.add_a_b(index, random_id=True)
769
("a", ["option"], (None, 0, 1), ["b"]),
770
("a", ["opt"], (None, 1, 2), ["c"]),
771
("b", ["option"], (None, 2, 3), ["a"])
1085
774
def test_delay_create_and_add_versions(self):
1086
775
transport = MockTransport()
1088
index = self.get_knit_index(transport, "filename", "w")
777
index = self.get_knit_index(transport, "filename", "w",
778
create=True, file_mode="wb", create_parent_dir=True,
779
delay_create=True, dir_mode=0777)
1090
780
self.assertEqual([], transport.calls)
1093
#[ {"dir_mode": 0777, "create_parent_dir": True, "mode": "wb"},
1095
# Two calls: one during which we load the existing index (and when its
1096
# missing create it), then a second where we write the contents out.
1097
self.assertEqual(2, len(transport.calls))
1098
call = transport.calls.pop(0)
1099
self.assertEqual('put_file_non_atomic', call[0])
1100
self.assertEqual('filename.kndx', call[1][0])
1101
# With no history, _KndxIndex writes a new index:
1102
self.assertEqual(_KndxIndex.HEADER, call[1][1].getvalue())
1103
self.assertEqual({'create_parent_dir': True}, call[2])
1104
call = transport.calls.pop(0)
1105
# call[1][1] is a StringIO - we can't test it by simple equality.
1106
self.assertEqual('put_file_non_atomic', call[0])
1107
self.assertEqual('filename.kndx', call[1][0])
1108
# With no history, _KndxIndex writes a new index:
783
("a", ["option"], (None, 0, 1), ["b"]),
784
("a", ["opt"], (None, 1, 2), ["c"]),
785
("b", ["option"], (None, 2, 3), ["a"])
787
name, (filename, f), kwargs = transport.calls.pop(0)
788
self.assertEqual("put_file_non_atomic", name)
790
{"dir_mode": 0777, "create_parent_dir": True, "mode": "wb"},
792
self.assertEqual("filename", filename)
1111
795
"\na option 0 1 .b :"
1112
796
"\na opt 1 2 .c :"
1113
797
"\nb option 2 3 0 :",
1114
call[1][1].getvalue())
1115
self.assertEqual({'create_parent_dir': True}, call[2])
1117
def assertTotalBuildSize(self, size, keys, positions):
1118
self.assertEqual(size,
1119
knit._get_total_build_size(None, keys, positions))
1121
def test__get_total_build_size(self):
1123
('a',): (('fulltext', False), (('a',), 0, 100), None),
1124
('b',): (('line-delta', False), (('b',), 100, 21), ('a',)),
1125
('c',): (('line-delta', False), (('c',), 121, 35), ('b',)),
1126
('d',): (('line-delta', False), (('d',), 156, 12), ('b',)),
1128
self.assertTotalBuildSize(100, [('a',)], positions)
1129
self.assertTotalBuildSize(121, [('b',)], positions)
1130
# c needs both a & b
1131
self.assertTotalBuildSize(156, [('c',)], positions)
1132
# we shouldn't count 'b' twice
1133
self.assertTotalBuildSize(156, [('b',), ('c',)], positions)
1134
self.assertTotalBuildSize(133, [('d',)], positions)
1135
self.assertTotalBuildSize(168, [('c',), ('d',)], positions)
800
def test_has_version(self):
801
transport = MockTransport([
805
index = self.get_knit_index(transport, "filename", "r")
807
self.assertTrue(index.has_version("a"))
808
self.assertFalse(index.has_version("b"))
1137
810
def test_get_position(self):
1138
811
transport = MockTransport([
1140
813
"a option 0 1 :",
1141
814
"b option 1 2 :"
1143
816
index = self.get_knit_index(transport, "filename", "r")
1145
self.assertEqual((("a",), 0, 1), index.get_position(("a",)))
1146
self.assertEqual((("b",), 1, 2), index.get_position(("b",)))
818
self.assertEqual((None, 0, 1), index.get_position("a"))
819
self.assertEqual((None, 1, 2), index.get_position("b"))
1148
821
def test_get_method(self):
1149
822
transport = MockTransport([
1151
824
"a fulltext,unknown 0 1 :",
1152
825
"b unknown,line-delta 1 2 :",
1261
969
def test_invalid_size(self):
1262
970
transport = MockTransport([
1264
972
"a option 1 1v :",
1266
index = self.get_knit_index(transport, 'filename', 'r')
1268
self.assertRaises(errors.KnitCorrupt, index.keys)
975
self.assertRaises(errors.KnitCorrupt,
976
self.get_knit_index, transport, 'filename', 'r')
1269
977
except TypeError, e:
1270
978
if (str(e) == ('exceptions must be strings, classes, or instances,'
1271
' not exceptions.ValueError')):
979
' not exceptions.ValueError')
980
and sys.version_info[0:2] >= (2,5)):
1272
981
self.knownFailure('Pyrex <0.9.5 fails with TypeError when'
1273
982
' raising new style exceptions with python'
1278
def test_scan_unvalidated_index_not_implemented(self):
1279
transport = MockTransport()
1280
index = self.get_knit_index(transport, 'filename', 'r')
1282
NotImplementedError, index.scan_unvalidated_index,
1283
'dummy graph_index')
1285
NotImplementedError, index.get_missing_compression_parents)
1287
987
def test_short_line(self):
1288
988
transport = MockTransport([
1290
990
"a option 0 10 :",
1291
991
"b option 10 10 0", # This line isn't terminated, ignored
1293
993
index = self.get_knit_index(transport, "filename", "r")
1294
self.assertEqual(set([('a',)]), index.keys())
994
self.assertEqual(['a'], index.get_versions())
1296
996
def test_skip_incomplete_record(self):
1297
997
# A line with bogus data should just be skipped
1298
998
transport = MockTransport([
1300
1000
"a option 0 10 :",
1301
1001
"b option 10 10 0", # This line isn't terminated, ignored
1302
1002
"c option 20 10 0 :", # Properly terminated, and starts with '\n'
1304
1004
index = self.get_knit_index(transport, "filename", "r")
1305
self.assertEqual(set([('a',), ('c',)]), index.keys())
1005
self.assertEqual(['a', 'c'], index.get_versions())
1307
1007
def test_trailing_characters(self):
1308
1008
# A line with bogus data should just be skipped
1309
1009
transport = MockTransport([
1311
1011
"a option 0 10 :",
1312
1012
"b option 10 10 0 :a", # This line has extra trailing characters
1313
1013
"c option 20 10 0 :", # Properly terminated, and starts with '\n'
1315
1015
index = self.get_knit_index(transport, "filename", "r")
1316
self.assertEqual(set([('a',), ('c',)]), index.keys())
1016
self.assertEqual(['a', 'c'], index.get_versions())
1319
1019
class LowLevelKnitIndexTests_c(LowLevelKnitIndexTests):
1321
_test_needs_features = [compiled_knit_feature]
1323
def get_knit_index(self, transport, name, mode):
1324
mapper = ConstantMapper(name)
1325
from bzrlib._knit_load_data_pyx import _load_data_c
1326
self.overrideAttr(knit, '_load_data', _load_data_c)
1327
allow_writes = lambda: mode == 'w'
1328
return _KndxIndex(transport, mapper, lambda:None,
1329
allow_writes, lambda:True)
1332
class Test_KnitAnnotator(TestCaseWithMemoryTransport):
1334
def make_annotator(self):
1335
factory = knit.make_pack_factory(True, True, 1)
1336
vf = factory(self.get_transport())
1337
return knit._KnitAnnotator(vf)
1339
def test__expand_fulltext(self):
1340
ann = self.make_annotator()
1341
rev_key = ('rev-id',)
1342
ann._num_compression_children[rev_key] = 1
1343
res = ann._expand_record(rev_key, (('parent-id',),), None,
1344
['line1\n', 'line2\n'], ('fulltext', True))
1345
# The content object and text lines should be cached appropriately
1346
self.assertEqual(['line1\n', 'line2'], res)
1347
content_obj = ann._content_objects[rev_key]
1348
self.assertEqual(['line1\n', 'line2\n'], content_obj._lines)
1349
self.assertEqual(res, content_obj.text())
1350
self.assertEqual(res, ann._text_cache[rev_key])
1352
def test__expand_delta_comp_parent_not_available(self):
1353
# Parent isn't available yet, so we return nothing, but queue up this
1354
# node for later processing
1355
ann = self.make_annotator()
1356
rev_key = ('rev-id',)
1357
parent_key = ('parent-id',)
1358
record = ['0,1,1\n', 'new-line\n']
1359
details = ('line-delta', False)
1360
res = ann._expand_record(rev_key, (parent_key,), parent_key,
1362
self.assertEqual(None, res)
1363
self.assertTrue(parent_key in ann._pending_deltas)
1364
pending = ann._pending_deltas[parent_key]
1365
self.assertEqual(1, len(pending))
1366
self.assertEqual((rev_key, (parent_key,), record, details), pending[0])
1368
def test__expand_record_tracks_num_children(self):
1369
ann = self.make_annotator()
1370
rev_key = ('rev-id',)
1371
rev2_key = ('rev2-id',)
1372
parent_key = ('parent-id',)
1373
record = ['0,1,1\n', 'new-line\n']
1374
details = ('line-delta', False)
1375
ann._num_compression_children[parent_key] = 2
1376
ann._expand_record(parent_key, (), None, ['line1\n', 'line2\n'],
1377
('fulltext', False))
1378
res = ann._expand_record(rev_key, (parent_key,), parent_key,
1380
self.assertEqual({parent_key: 1}, ann._num_compression_children)
1381
# Expanding the second child should remove the content object, and the
1382
# num_compression_children entry
1383
res = ann._expand_record(rev2_key, (parent_key,), parent_key,
1385
self.assertFalse(parent_key in ann._content_objects)
1386
self.assertEqual({}, ann._num_compression_children)
1387
# We should not cache the content_objects for rev2 and rev, because
1388
# they do not have compression children of their own.
1389
self.assertEqual({}, ann._content_objects)
1391
def test__expand_delta_records_blocks(self):
1392
ann = self.make_annotator()
1393
rev_key = ('rev-id',)
1394
parent_key = ('parent-id',)
1395
record = ['0,1,1\n', 'new-line\n']
1396
details = ('line-delta', True)
1397
ann._num_compression_children[parent_key] = 2
1398
ann._expand_record(parent_key, (), None,
1399
['line1\n', 'line2\n', 'line3\n'],
1400
('fulltext', False))
1401
ann._expand_record(rev_key, (parent_key,), parent_key, record, details)
1402
self.assertEqual({(rev_key, parent_key): [(1, 1, 1), (3, 3, 0)]},
1403
ann._matching_blocks)
1404
rev2_key = ('rev2-id',)
1405
record = ['0,1,1\n', 'new-line\n']
1406
details = ('line-delta', False)
1407
ann._expand_record(rev2_key, (parent_key,), parent_key, record, details)
1408
self.assertEqual([(1, 1, 2), (3, 3, 0)],
1409
ann._matching_blocks[(rev2_key, parent_key)])
1411
def test__get_parent_ann_uses_matching_blocks(self):
1412
ann = self.make_annotator()
1413
rev_key = ('rev-id',)
1414
parent_key = ('parent-id',)
1415
parent_ann = [(parent_key,)]*3
1416
block_key = (rev_key, parent_key)
1417
ann._annotations_cache[parent_key] = parent_ann
1418
ann._matching_blocks[block_key] = [(0, 1, 1), (3, 3, 0)]
1419
# We should not try to access any parent_lines content, because we know
1420
# we already have the matching blocks
1421
par_ann, blocks = ann._get_parent_annotations_and_matches(rev_key,
1422
['1\n', '2\n', '3\n'], parent_key)
1423
self.assertEqual(parent_ann, par_ann)
1424
self.assertEqual([(0, 1, 1), (3, 3, 0)], blocks)
1425
self.assertEqual({}, ann._matching_blocks)
1427
def test__process_pending(self):
1428
ann = self.make_annotator()
1429
rev_key = ('rev-id',)
1432
record = ['0,1,1\n', 'new-line\n']
1433
details = ('line-delta', False)
1434
p1_record = ['line1\n', 'line2\n']
1435
ann._num_compression_children[p1_key] = 1
1436
res = ann._expand_record(rev_key, (p1_key,p2_key), p1_key,
1438
self.assertEqual(None, res)
1439
# self.assertTrue(p1_key in ann._pending_deltas)
1440
self.assertEqual({}, ann._pending_annotation)
1441
# Now insert p1, and we should be able to expand the delta
1442
res = ann._expand_record(p1_key, (), None, p1_record,
1443
('fulltext', False))
1444
self.assertEqual(p1_record, res)
1445
ann._annotations_cache[p1_key] = [(p1_key,)]*2
1446
res = ann._process_pending(p1_key)
1447
self.assertEqual([], res)
1448
self.assertFalse(p1_key in ann._pending_deltas)
1449
self.assertTrue(p2_key in ann._pending_annotation)
1450
self.assertEqual({p2_key: [(rev_key, (p1_key, p2_key))]},
1451
ann._pending_annotation)
1452
# Now fill in parent 2, and pending annotation should be satisfied
1453
res = ann._expand_record(p2_key, (), None, [], ('fulltext', False))
1454
ann._annotations_cache[p2_key] = []
1455
res = ann._process_pending(p2_key)
1456
self.assertEqual([rev_key], res)
1457
self.assertEqual({}, ann._pending_annotation)
1458
self.assertEqual({}, ann._pending_deltas)
1460
def test_record_delta_removes_basis(self):
1461
ann = self.make_annotator()
1462
ann._expand_record(('parent-id',), (), None,
1463
['line1\n', 'line2\n'], ('fulltext', False))
1464
ann._num_compression_children['parent-id'] = 2
1466
def test_annotate_special_text(self):
1467
ann = self.make_annotator()
1469
rev1_key = ('rev-1',)
1470
rev2_key = ('rev-2',)
1471
rev3_key = ('rev-3',)
1472
spec_key = ('special:',)
1473
vf.add_lines(rev1_key, [], ['initial content\n'])
1474
vf.add_lines(rev2_key, [rev1_key], ['initial content\n',
1477
vf.add_lines(rev3_key, [rev1_key], ['initial content\n',
1480
spec_text = ('initial content\n'
1484
ann.add_special_text(spec_key, [rev2_key, rev3_key], spec_text)
1485
anns, lines = ann.annotate(spec_key)
1486
self.assertEqual([(rev1_key,),
1487
(rev2_key, rev3_key),
1491
self.assertEqualDiff(spec_text, ''.join(lines))
1021
_test_needs_features = [CompiledKnitFeature]
1023
def get_knit_index(self, *args, **kwargs):
1024
orig = knit._load_data
1026
knit._load_data = orig
1027
self.addCleanup(reset)
1028
from bzrlib._knit_load_data_c import _load_data_c
1029
knit._load_data = _load_data_c
1030
return _KnitIndex(get_scope=lambda:None, *args, **kwargs)
1494
1033
class KnitTests(TestCaseWithTransport):
1495
1034
"""Class containing knit test helper routines."""
1497
def make_test_knit(self, annotate=False, name='test'):
1498
mapper = ConstantMapper(name)
1499
return make_file_factory(annotate, mapper)(self.get_transport())
1502
class TestBadShaError(KnitTests):
1503
"""Tests for handling of sha errors."""
1505
def test_sha_exception_has_text(self):
1506
# having the failed text included in the error allows for recovery.
1507
source = self.make_test_knit()
1508
target = self.make_test_knit(name="target")
1509
if not source._max_delta_chain:
1510
raise TestNotApplicable(
1511
"cannot get delta-caused sha failures without deltas.")
1514
broken = ('broken',)
1515
source.add_lines(basis, (), ['foo\n'])
1516
source.add_lines(broken, (basis,), ['foo\n', 'bar\n'])
1517
# Seed target with a bad basis text
1518
target.add_lines(basis, (), ['gam\n'])
1519
target.insert_record_stream(
1520
source.get_record_stream([broken], 'unordered', False))
1521
err = self.assertRaises(errors.KnitCorrupt,
1522
target.get_record_stream([broken], 'unordered', True
1523
).next().get_bytes_as, 'chunked')
1524
self.assertEqual(['gam\n', 'bar\n'], err.content)
1525
# Test for formatting with live data
1526
self.assertStartsWith(str(err), "Knit ")
1036
def make_test_knit(self, annotate=False, delay_create=False, index=None,
1037
name='test', delta=True, access_mode='w'):
1039
factory = KnitPlainFactory()
1043
index = _KnitIndex(get_transport('.'), name + INDEX_SUFFIX,
1044
access_mode, create=True, file_mode=None,
1045
create_parent_dir=False, delay_create=delay_create,
1046
dir_mode=None, get_scope=lambda:None)
1047
access = _KnitAccess(get_transport('.'), name + DATA_SUFFIX, None,
1048
None, delay_create, False)
1049
return KnitVersionedFile(name, get_transport('.'), factory=factory,
1050
create=True, delay_create=delay_create, index=index,
1051
access_method=access, delta=delta)
1053
def assertRecordContentEqual(self, knit, version_id, candidate_content):
1054
"""Assert that some raw record content matches the raw record content
1055
for a particular version_id in the given knit.
1057
index_memo = knit._index.get_position(version_id)
1058
record = (version_id, index_memo)
1059
[(_, expected_content)] = list(knit._data.read_records_iter_raw([record]))
1060
self.assertEqual(expected_content, candidate_content)
1063
class BasicKnitTests(KnitTests):
1065
def add_stock_one_and_one_a(self, k):
1066
k.add_lines('text-1', [], split_lines(TEXT_1))
1067
k.add_lines('text-1a', ['text-1'], split_lines(TEXT_1A))
1069
def test_knit_constructor(self):
1070
"""Construct empty k"""
1071
self.make_test_knit()
1073
def test_make_explicit_index(self):
1074
"""We can supply an index to use."""
1075
knit = KnitVersionedFile('test', get_transport('.'),
1076
index='strangelove', access_method="a")
1077
self.assertEqual(knit._index, 'strangelove')
1079
def test_knit_add(self):
1080
"""Store one text in knit and retrieve"""
1081
k = self.make_test_knit()
1082
k.add_lines('text-1', [], split_lines(TEXT_1))
1083
self.assertTrue(k.has_version('text-1'))
1084
self.assertEqualDiff(''.join(k.get_lines('text-1')), TEXT_1)
1086
def test_newline_empty_lines(self):
1087
# ensure that ["\n"] round trips ok.
1088
knit = self.make_test_knit()
1089
knit.add_lines('a', [], ["\n"])
1090
knit.add_lines_with_ghosts('b', [], ["\n"])
1091
self.assertEqual(["\n"], knit.get_lines('a'))
1092
self.assertEqual(["\n"], knit.get_lines('b'))
1093
self.assertEqual(['fulltext'], knit._index.get_options('a'))
1094
self.assertEqual(['fulltext'], knit._index.get_options('b'))
1095
knit.add_lines('c', ['a'], ["\n"])
1096
knit.add_lines_with_ghosts('d', ['b'], ["\n"])
1097
self.assertEqual(["\n"], knit.get_lines('c'))
1098
self.assertEqual(["\n"], knit.get_lines('d'))
1099
self.assertEqual(['line-delta'], knit._index.get_options('c'))
1100
self.assertEqual(['line-delta'], knit._index.get_options('d'))
1102
def test_empty_lines(self):
1103
# bizarrely, [] is not listed as having no-eol.
1104
knit = self.make_test_knit()
1105
knit.add_lines('a', [], [])
1106
knit.add_lines_with_ghosts('b', [], [])
1107
self.assertEqual([], knit.get_lines('a'))
1108
self.assertEqual([], knit.get_lines('b'))
1109
self.assertEqual(['fulltext'], knit._index.get_options('a'))
1110
self.assertEqual(['fulltext'], knit._index.get_options('b'))
1111
knit.add_lines('c', ['a'], [])
1112
knit.add_lines_with_ghosts('d', ['b'], [])
1113
self.assertEqual([], knit.get_lines('c'))
1114
self.assertEqual([], knit.get_lines('d'))
1115
self.assertEqual(['line-delta'], knit._index.get_options('c'))
1116
self.assertEqual(['line-delta'], knit._index.get_options('d'))
1118
def test_knit_reload(self):
1119
# test that the content in a reloaded knit is correct
1120
k = self.make_test_knit()
1121
k.add_lines('text-1', [], split_lines(TEXT_1))
1123
k2 = make_file_knit('test', get_transport('.'), access_mode='r',
1124
factory=KnitPlainFactory(), create=True)
1125
self.assertTrue(k2.has_version('text-1'))
1126
self.assertEqualDiff(''.join(k2.get_lines('text-1')), TEXT_1)
1128
def test_knit_several(self):
1129
"""Store several texts in a knit"""
1130
k = self.make_test_knit()
1131
k.add_lines('text-1', [], split_lines(TEXT_1))
1132
k.add_lines('text-2', [], split_lines(TEXT_2))
1133
self.assertEqualDiff(''.join(k.get_lines('text-1')), TEXT_1)
1134
self.assertEqualDiff(''.join(k.get_lines('text-2')), TEXT_2)
1136
def test_repeated_add(self):
1137
"""Knit traps attempt to replace existing version"""
1138
k = self.make_test_knit()
1139
k.add_lines('text-1', [], split_lines(TEXT_1))
1140
self.assertRaises(RevisionAlreadyPresent,
1142
'text-1', [], split_lines(TEXT_1))
1144
def test_empty(self):
1145
k = self.make_test_knit(True)
1146
k.add_lines('text-1', [], [])
1147
self.assertEquals(k.get_lines('text-1'), [])
1149
def test_incomplete(self):
1150
"""Test if texts without a ending line-end can be inserted and
1152
k = make_file_knit('test', get_transport('.'), delta=False, create=True)
1153
k.add_lines('text-1', [], ['a\n', 'b' ])
1154
k.add_lines('text-2', ['text-1'], ['a\rb\n', 'b\n'])
1155
# reopening ensures maximum room for confusion
1156
k = make_file_knit('test', get_transport('.'), delta=False, create=True)
1157
self.assertEquals(k.get_lines('text-1'), ['a\n', 'b' ])
1158
self.assertEquals(k.get_lines('text-2'), ['a\rb\n', 'b\n'])
1160
def test_delta(self):
1161
"""Expression of knit delta as lines"""
1162
k = self.make_test_knit()
1163
td = list(line_delta(TEXT_1.splitlines(True),
1164
TEXT_1A.splitlines(True)))
1165
self.assertEqualDiff(''.join(td), delta_1_1a)
1166
out = apply_line_delta(TEXT_1.splitlines(True), td)
1167
self.assertEqualDiff(''.join(out), TEXT_1A)
1169
def test_add_with_parents(self):
1170
"""Store in knit with parents"""
1171
k = self.make_test_knit()
1172
self.add_stock_one_and_one_a(k)
1173
self.assertEqual({'text-1':(), 'text-1a':('text-1',)},
1174
k.get_parent_map(['text-1', 'text-1a']))
1176
def test_ancestry(self):
1177
"""Store in knit with parents"""
1178
k = self.make_test_knit()
1179
self.add_stock_one_and_one_a(k)
1180
self.assertEquals(set(k.get_ancestry(['text-1a'])), set(['text-1a', 'text-1']))
1182
def test_add_delta(self):
1183
"""Store in knit with parents"""
1184
k = self.make_test_knit(annotate=False)
1185
self.add_stock_one_and_one_a(k)
1186
self.assertEqualDiff(''.join(k.get_lines('text-1a')), TEXT_1A)
1188
def test_add_delta_knit_graph_index(self):
1189
"""Does adding work with a KnitGraphIndex."""
1190
index = InMemoryGraphIndex(2)
1191
knit_index = KnitGraphIndex(index, add_callback=index.add_nodes,
1193
k = self.make_test_knit(annotate=True, index=knit_index)
1194
self.add_stock_one_and_one_a(k)
1195
self.assertEqualDiff(''.join(k.get_lines('text-1a')), TEXT_1A)
1196
# check the index had the right data added.
1197
self.assertEqual(set([
1198
(index, ('text-1', ), ' 0 127', ((), ())),
1199
(index, ('text-1a', ), ' 127 140', ((('text-1', ),), (('text-1', ),))),
1200
]), set(index.iter_all_entries()))
1201
# we should not have a .kndx file
1202
self.assertFalse(get_transport('.').has('test.kndx'))
1204
def test_annotate(self):
1206
k = self.make_test_knit(annotate=True, name='knit')
1207
self.insert_and_test_small_annotate(k)
1209
def insert_and_test_small_annotate(self, k):
1210
"""test annotation with k works correctly."""
1211
k.add_lines('text-1', [], ['a\n', 'b\n'])
1212
k.add_lines('text-2', ['text-1'], ['a\n', 'c\n'])
1214
origins = k.annotate('text-2')
1215
self.assertEquals(origins[0], ('text-1', 'a\n'))
1216
self.assertEquals(origins[1], ('text-2', 'c\n'))
1218
def test_annotate_fulltext(self):
1220
k = self.make_test_knit(annotate=True, name='knit', delta=False)
1221
self.insert_and_test_small_annotate(k)
1223
def test_annotate_merge_1(self):
1224
k = self.make_test_knit(True)
1225
k.add_lines('text-a1', [], ['a\n', 'b\n'])
1226
k.add_lines('text-a2', [], ['d\n', 'c\n'])
1227
k.add_lines('text-am', ['text-a1', 'text-a2'], ['d\n', 'b\n'])
1228
origins = k.annotate('text-am')
1229
self.assertEquals(origins[0], ('text-a2', 'd\n'))
1230
self.assertEquals(origins[1], ('text-a1', 'b\n'))
1232
def test_annotate_merge_2(self):
1233
k = self.make_test_knit(True)
1234
k.add_lines('text-a1', [], ['a\n', 'b\n', 'c\n'])
1235
k.add_lines('text-a2', [], ['x\n', 'y\n', 'z\n'])
1236
k.add_lines('text-am', ['text-a1', 'text-a2'], ['a\n', 'y\n', 'c\n'])
1237
origins = k.annotate('text-am')
1238
self.assertEquals(origins[0], ('text-a1', 'a\n'))
1239
self.assertEquals(origins[1], ('text-a2', 'y\n'))
1240
self.assertEquals(origins[2], ('text-a1', 'c\n'))
1242
def test_annotate_merge_9(self):
1243
k = self.make_test_knit(True)
1244
k.add_lines('text-a1', [], ['a\n', 'b\n', 'c\n'])
1245
k.add_lines('text-a2', [], ['x\n', 'y\n', 'z\n'])
1246
k.add_lines('text-am', ['text-a1', 'text-a2'], ['k\n', 'y\n', 'c\n'])
1247
origins = k.annotate('text-am')
1248
self.assertEquals(origins[0], ('text-am', 'k\n'))
1249
self.assertEquals(origins[1], ('text-a2', 'y\n'))
1250
self.assertEquals(origins[2], ('text-a1', 'c\n'))
1252
def test_annotate_merge_3(self):
1253
k = self.make_test_knit(True)
1254
k.add_lines('text-a1', [], ['a\n', 'b\n', 'c\n'])
1255
k.add_lines('text-a2', [] ,['x\n', 'y\n', 'z\n'])
1256
k.add_lines('text-am', ['text-a1', 'text-a2'], ['k\n', 'y\n', 'z\n'])
1257
origins = k.annotate('text-am')
1258
self.assertEquals(origins[0], ('text-am', 'k\n'))
1259
self.assertEquals(origins[1], ('text-a2', 'y\n'))
1260
self.assertEquals(origins[2], ('text-a2', 'z\n'))
1262
def test_annotate_merge_4(self):
1263
k = self.make_test_knit(True)
1264
k.add_lines('text-a1', [], ['a\n', 'b\n', 'c\n'])
1265
k.add_lines('text-a2', [], ['x\n', 'y\n', 'z\n'])
1266
k.add_lines('text-a3', ['text-a1'], ['a\n', 'b\n', 'p\n'])
1267
k.add_lines('text-am', ['text-a2', 'text-a3'], ['a\n', 'b\n', 'z\n'])
1268
origins = k.annotate('text-am')
1269
self.assertEquals(origins[0], ('text-a1', 'a\n'))
1270
self.assertEquals(origins[1], ('text-a1', 'b\n'))
1271
self.assertEquals(origins[2], ('text-a2', 'z\n'))
1273
def test_annotate_merge_5(self):
1274
k = self.make_test_knit(True)
1275
k.add_lines('text-a1', [], ['a\n', 'b\n', 'c\n'])
1276
k.add_lines('text-a2', [], ['d\n', 'e\n', 'f\n'])
1277
k.add_lines('text-a3', [], ['x\n', 'y\n', 'z\n'])
1278
k.add_lines('text-am',
1279
['text-a1', 'text-a2', 'text-a3'],
1280
['a\n', 'e\n', 'z\n'])
1281
origins = k.annotate('text-am')
1282
self.assertEquals(origins[0], ('text-a1', 'a\n'))
1283
self.assertEquals(origins[1], ('text-a2', 'e\n'))
1284
self.assertEquals(origins[2], ('text-a3', 'z\n'))
1286
def test_annotate_file_cherry_pick(self):
1287
k = self.make_test_knit(True)
1288
k.add_lines('text-1', [], ['a\n', 'b\n', 'c\n'])
1289
k.add_lines('text-2', ['text-1'], ['d\n', 'e\n', 'f\n'])
1290
k.add_lines('text-3', ['text-2', 'text-1'], ['a\n', 'b\n', 'c\n'])
1291
origins = k.annotate('text-3')
1292
self.assertEquals(origins[0], ('text-1', 'a\n'))
1293
self.assertEquals(origins[1], ('text-1', 'b\n'))
1294
self.assertEquals(origins[2], ('text-1', 'c\n'))
1296
def _test_join_with_factories(self, k1_factory, k2_factory):
1297
k1 = make_file_knit('test1', get_transport('.'), factory=k1_factory, create=True)
1298
k1.add_lines('text-a', [], ['a1\n', 'a2\n', 'a3\n'])
1299
k1.add_lines('text-b', ['text-a'], ['a1\n', 'b2\n', 'a3\n'])
1300
k1.add_lines('text-c', [], ['c1\n', 'c2\n', 'c3\n'])
1301
k1.add_lines('text-d', ['text-c'], ['c1\n', 'd2\n', 'd3\n'])
1302
k1.add_lines('text-m', ['text-b', 'text-d'], ['a1\n', 'b2\n', 'd3\n'])
1303
k2 = make_file_knit('test2', get_transport('.'), factory=k2_factory, create=True)
1304
count = k2.join(k1, version_ids=['text-m'])
1305
self.assertEquals(count, 5)
1306
self.assertTrue(k2.has_version('text-a'))
1307
self.assertTrue(k2.has_version('text-c'))
1308
origins = k2.annotate('text-m')
1309
self.assertEquals(origins[0], ('text-a', 'a1\n'))
1310
self.assertEquals(origins[1], ('text-b', 'b2\n'))
1311
self.assertEquals(origins[2], ('text-d', 'd3\n'))
1313
def test_knit_join_plain_to_plain(self):
1314
"""Test joining a plain knit with a plain knit."""
1315
self._test_join_with_factories(KnitPlainFactory(), KnitPlainFactory())
1317
def test_knit_join_anno_to_anno(self):
1318
"""Test joining an annotated knit with an annotated knit."""
1319
self._test_join_with_factories(None, None)
1321
def test_knit_join_anno_to_plain(self):
1322
"""Test joining an annotated knit with a plain knit."""
1323
self._test_join_with_factories(None, KnitPlainFactory())
1325
def test_knit_join_plain_to_anno(self):
1326
"""Test joining a plain knit with an annotated knit."""
1327
self._test_join_with_factories(KnitPlainFactory(), None)
1329
def test_reannotate(self):
1330
k1 = make_file_knit('knit1', get_transport('.'),
1331
factory=KnitAnnotateFactory(), create=True)
1333
k1.add_lines('text-a', [], ['a\n', 'b\n'])
1335
k1.add_lines('text-b', ['text-a'], ['a\n', 'c\n'])
1337
k2 = make_file_knit('test2', get_transport('.'),
1338
factory=KnitAnnotateFactory(), create=True)
1339
k2.join(k1, version_ids=['text-b'])
1342
k1.add_lines('text-X', ['text-b'], ['a\n', 'b\n'])
1344
k2.add_lines('text-c', ['text-b'], ['z\n', 'c\n'])
1346
k2.add_lines('text-Y', ['text-b'], ['b\n', 'c\n'])
1348
# test-c will have index 3
1349
k1.join(k2, version_ids=['text-c'])
1351
lines = k1.get_lines('text-c')
1352
self.assertEquals(lines, ['z\n', 'c\n'])
1354
origins = k1.annotate('text-c')
1355
self.assertEquals(origins[0], ('text-c', 'z\n'))
1356
self.assertEquals(origins[1], ('text-b', 'c\n'))
1358
def test_get_line_delta_texts(self):
1359
"""Make sure we can call get_texts on text with reused line deltas"""
1360
k1 = make_file_knit('test1', get_transport('.'),
1361
factory=KnitPlainFactory(), create=True)
1366
parents = ['%d' % (t-1)]
1367
k1.add_lines('%d' % t, parents, ['hello\n'] * t)
1368
k1.get_texts(('%d' % t) for t in range(3))
1370
def test_iter_lines_reads_in_order(self):
1371
instrumented_t = get_transport('trace+memory:///')
1372
k1 = make_file_knit('id', instrumented_t, create=True, delta=True)
1373
self.assertEqual([('get', 'id.kndx',)], instrumented_t._activity)
1374
# add texts with no required ordering
1375
k1.add_lines('base', [], ['text\n'])
1376
k1.add_lines('base2', [], ['text2\n'])
1377
# clear the logged activity, but preserve the list instance in case of
1378
# clones pointing at it.
1379
del instrumented_t._activity[:]
1380
# request a last-first iteration
1381
results = list(k1.iter_lines_added_or_present_in_versions(
1384
[('readv', 'id.knit', [(0, 87), (87, 89)], False, None)],
1385
instrumented_t._activity)
1386
self.assertEqual([('text\n', 'base'), ('text2\n', 'base2')], results)
1388
def test_knit_format(self):
1389
# this tests that a new knit index file has the expected content
1390
# and that is writes the data we expect as records are added.
1391
knit = self.make_test_knit(True)
1392
# Now knit files are not created until we first add data to them
1393
self.assertFileEqual("# bzr knit index 8\n", 'test.kndx')
1394
knit.add_lines_with_ghosts('revid', ['a_ghost'], ['a\n'])
1395
self.assertFileEqual(
1396
"# bzr knit index 8\n"
1398
"revid fulltext 0 84 .a_ghost :",
1400
knit.add_lines_with_ghosts('revid2', ['revid'], ['a\n'])
1401
self.assertFileEqual(
1402
"# bzr knit index 8\n"
1403
"\nrevid fulltext 0 84 .a_ghost :"
1404
"\nrevid2 line-delta 84 82 0 :",
1406
# we should be able to load this file again
1407
knit = make_file_knit('test', get_transport('.'), access_mode='r')
1408
self.assertEqual(['revid', 'revid2'], knit.versions())
1409
# write a short write to the file and ensure that its ignored
1410
indexfile = file('test.kndx', 'ab')
1411
indexfile.write('\nrevid3 line-delta 166 82 1 2 3 4 5 .phwoar:demo ')
1413
# we should be able to load this file again
1414
knit = make_file_knit('test', get_transport('.'), access_mode='w')
1415
self.assertEqual(['revid', 'revid2'], knit.versions())
1416
# and add a revision with the same id the failed write had
1417
knit.add_lines('revid3', ['revid2'], ['a\n'])
1418
# and when reading it revid3 should now appear.
1419
knit = make_file_knit('test', get_transport('.'), access_mode='r')
1420
self.assertEqual(['revid', 'revid2', 'revid3'], knit.versions())
1421
self.assertEqual({'revid3':('revid2',)}, knit.get_parent_map(['revid3']))
1423
def test_delay_create(self):
1424
"""Test that passing delay_create=True creates files late"""
1425
knit = self.make_test_knit(annotate=True, delay_create=True)
1426
self.failIfExists('test.knit')
1427
self.failIfExists('test.kndx')
1428
knit.add_lines_with_ghosts('revid', ['a_ghost'], ['a\n'])
1429
self.failUnlessExists('test.knit')
1430
self.assertFileEqual(
1431
"# bzr knit index 8\n"
1433
"revid fulltext 0 84 .a_ghost :",
1436
def test_create_parent_dir(self):
1437
"""create_parent_dir can create knits in nonexistant dirs"""
1438
# Has no effect if we don't set 'delay_create'
1439
trans = get_transport('.')
1440
self.assertRaises(NoSuchFile, make_file_knit, 'dir/test',
1441
trans, access_mode='w', factory=None,
1442
create=True, create_parent_dir=True)
1443
# Nothing should have changed yet
1444
knit = make_file_knit('dir/test', trans, access_mode='w',
1445
factory=None, create=True,
1446
create_parent_dir=True,
1448
self.failIfExists('dir/test.knit')
1449
self.failIfExists('dir/test.kndx')
1450
self.failIfExists('dir')
1451
knit.add_lines('revid', [], ['a\n'])
1452
self.failUnlessExists('dir')
1453
self.failUnlessExists('dir/test.knit')
1454
self.assertFileEqual(
1455
"# bzr knit index 8\n"
1457
"revid fulltext 0 84 :",
1460
def test_create_mode_700(self):
1461
trans = get_transport('.')
1462
if not trans._can_roundtrip_unix_modebits():
1463
# Can't roundtrip, so no need to run this test
1465
knit = make_file_knit('dir/test', trans, access_mode='w', factory=None,
1466
create=True, create_parent_dir=True, delay_create=True,
1467
file_mode=0600, dir_mode=0700)
1468
knit.add_lines('revid', [], ['a\n'])
1469
self.assertTransportMode(trans, 'dir', 0700)
1470
self.assertTransportMode(trans, 'dir/test.knit', 0600)
1471
self.assertTransportMode(trans, 'dir/test.kndx', 0600)
1473
def test_create_mode_770(self):
1474
trans = get_transport('.')
1475
if not trans._can_roundtrip_unix_modebits():
1476
# Can't roundtrip, so no need to run this test
1478
knit = make_file_knit('dir/test', trans, access_mode='w', factory=None,
1479
create=True, create_parent_dir=True, delay_create=True,
1480
file_mode=0660, dir_mode=0770)
1481
knit.add_lines('revid', [], ['a\n'])
1482
self.assertTransportMode(trans, 'dir', 0770)
1483
self.assertTransportMode(trans, 'dir/test.knit', 0660)
1484
self.assertTransportMode(trans, 'dir/test.kndx', 0660)
1486
def test_create_mode_777(self):
1487
trans = get_transport('.')
1488
if not trans._can_roundtrip_unix_modebits():
1489
# Can't roundtrip, so no need to run this test
1491
knit = make_file_knit('dir/test', trans, access_mode='w', factory=None,
1492
create=True, create_parent_dir=True, delay_create=True,
1493
file_mode=0666, dir_mode=0777)
1494
knit.add_lines('revid', [], ['a\n'])
1495
self.assertTransportMode(trans, 'dir', 0777)
1496
self.assertTransportMode(trans, 'dir/test.knit', 0666)
1497
self.assertTransportMode(trans, 'dir/test.kndx', 0666)
1499
def test_plan_merge(self):
1500
my_knit = self.make_test_knit(annotate=True)
1501
my_knit.add_lines('text1', [], split_lines(TEXT_1))
1502
my_knit.add_lines('text1a', ['text1'], split_lines(TEXT_1A))
1503
my_knit.add_lines('text1b', ['text1'], split_lines(TEXT_1B))
1504
plan = list(my_knit.plan_merge('text1a', 'text1b'))
1505
for plan_line, expected_line in zip(plan, AB_MERGE):
1506
self.assertEqual(plan_line, expected_line)
1508
def test_get_stream_empty(self):
1509
"""Get a data stream for an empty knit file."""
1510
k1 = self.make_test_knit()
1511
format, data_list, reader_callable = k1.get_data_stream([])
1512
self.assertEqual('knit-plain', format)
1513
self.assertEqual([], data_list)
1514
content = reader_callable(None)
1515
self.assertEqual('', content)
1516
self.assertIsInstance(content, str)
1518
def test_get_stream_one_version(self):
1519
"""Get a data stream for a single record out of a knit containing just
1522
k1 = self.make_test_knit()
1524
('text-a', [], TEXT_1),
1526
expected_data_list = [
1527
# version, options, length, parents
1528
('text-a', ['fulltext'], 122, ()),
1530
for version_id, parents, lines in test_data:
1531
k1.add_lines(version_id, parents, split_lines(lines))
1533
format, data_list, reader_callable = k1.get_data_stream(['text-a'])
1534
self.assertEqual('knit-plain', format)
1535
self.assertEqual(expected_data_list, data_list)
1536
# There's only one record in the knit, so the content should be the
1537
# entire knit data file's contents.
1538
self.assertEqual(k1.transport.get_bytes(k1._data._access._filename),
1539
reader_callable(None))
1541
def test_get_stream_get_one_version_of_many(self):
1542
"""Get a data stream for just one version out of a knit containing many
1545
k1 = self.make_test_knit()
1546
# Insert the same data as test_knit_join, as they seem to cover a range
1547
# of cases (no parents, one parent, multiple parents).
1549
('text-a', [], TEXT_1),
1550
('text-b', ['text-a'], TEXT_1),
1551
('text-c', [], TEXT_1),
1552
('text-d', ['text-c'], TEXT_1),
1553
('text-m', ['text-b', 'text-d'], TEXT_1),
1555
expected_data_list = [
1556
# version, options, length, parents
1557
('text-m', ['line-delta'], 84, ('text-b', 'text-d')),
1559
for version_id, parents, lines in test_data:
1560
k1.add_lines(version_id, parents, split_lines(lines))
1562
format, data_list, reader_callable = k1.get_data_stream(['text-m'])
1563
self.assertEqual('knit-plain', format)
1564
self.assertEqual(expected_data_list, data_list)
1565
self.assertRecordContentEqual(k1, 'text-m', reader_callable(None))
1567
def test_get_data_stream_unordered_index(self):
1568
"""Get a data stream when the knit index reports versions out of order.
1570
https://bugs.launchpad.net/bzr/+bug/164637
1572
k1 = self.make_test_knit()
1574
('text-a', [], TEXT_1),
1575
('text-b', ['text-a'], TEXT_1),
1576
('text-c', [], TEXT_1),
1577
('text-d', ['text-c'], TEXT_1),
1578
('text-m', ['text-b', 'text-d'], TEXT_1),
1580
for version_id, parents, lines in test_data:
1581
k1.add_lines(version_id, parents, split_lines(lines))
1582
# monkey-patch versions method to return out of order, as if coming
1583
# from multiple independently indexed packs
1584
original_versions = k1.versions
1585
k1.versions = lambda: reversed(original_versions())
1586
expected_data_list = [
1587
('text-a', ['fulltext'], 122, ()),
1588
('text-b', ['line-delta'], 84, ('text-a',))]
1589
# now check the fulltext is first and the delta second
1590
format, data_list, _ = k1.get_data_stream(['text-a', 'text-b'])
1591
self.assertEqual('knit-plain', format)
1592
self.assertEqual(expected_data_list, data_list)
1593
# and that's true if we ask for them in the opposite order too
1594
format, data_list, _ = k1.get_data_stream(['text-b', 'text-a'])
1595
self.assertEqual(expected_data_list, data_list)
1596
# also try requesting more versions
1597
format, data_list, _ = k1.get_data_stream([
1598
'text-m', 'text-b', 'text-a'])
1600
('text-a', ['fulltext'], 122, ()),
1601
('text-b', ['line-delta'], 84, ('text-a',)),
1602
('text-m', ['line-delta'], 84, ('text-b', 'text-d')),
1605
def test_get_stream_ghost_parent(self):
1606
"""Get a data stream for a version with a ghost parent."""
1607
k1 = self.make_test_knit()
1609
k1.add_lines('text-a', [], split_lines(TEXT_1))
1610
k1.add_lines_with_ghosts('text-b', ['text-a', 'text-ghost'],
1611
split_lines(TEXT_1))
1613
expected_data_list = [
1614
# version, options, length, parents
1615
('text-b', ['line-delta'], 84, ('text-a', 'text-ghost')),
1618
format, data_list, reader_callable = k1.get_data_stream(['text-b'])
1619
self.assertEqual('knit-plain', format)
1620
self.assertEqual(expected_data_list, data_list)
1621
self.assertRecordContentEqual(k1, 'text-b', reader_callable(None))
1623
def test_get_stream_get_multiple_records(self):
1624
"""Get a stream for multiple records of a knit."""
1625
k1 = self.make_test_knit()
1626
# Insert the same data as test_knit_join, as they seem to cover a range
1627
# of cases (no parents, one parent, multiple parents).
1629
('text-a', [], TEXT_1),
1630
('text-b', ['text-a'], TEXT_1),
1631
('text-c', [], TEXT_1),
1632
('text-d', ['text-c'], TEXT_1),
1633
('text-m', ['text-b', 'text-d'], TEXT_1),
1635
for version_id, parents, lines in test_data:
1636
k1.add_lines(version_id, parents, split_lines(lines))
1638
# This test is actually a bit strict as the order in which they're
1639
# returned is not defined. This matches the current (deterministic)
1641
expected_data_list = [
1642
# version, options, length, parents
1643
('text-d', ['line-delta'], 84, ('text-c',)),
1644
('text-b', ['line-delta'], 84, ('text-a',)),
1646
# Note that even though we request the revision IDs in a particular
1647
# order, the data stream may return them in any order it likes. In this
1648
# case, they'll be in the order they were inserted into the knit.
1649
format, data_list, reader_callable = k1.get_data_stream(
1650
['text-d', 'text-b'])
1651
self.assertEqual('knit-plain', format)
1652
self.assertEqual(expected_data_list, data_list)
1653
# must match order they're returned
1654
self.assertRecordContentEqual(k1, 'text-d', reader_callable(84))
1655
self.assertRecordContentEqual(k1, 'text-b', reader_callable(84))
1656
self.assertEqual('', reader_callable(None),
1657
"There should be no more bytes left to read.")
1659
def test_get_stream_all(self):
1660
"""Get a data stream for all the records in a knit.
1662
This exercises fulltext records, line-delta records, records with
1663
various numbers of parents, and reading multiple records out of the
1664
callable. These cases ought to all be exercised individually by the
1665
other test_get_stream_* tests; this test is basically just paranoia.
1667
k1 = self.make_test_knit()
1668
# Insert the same data as test_knit_join, as they seem to cover a range
1669
# of cases (no parents, one parent, multiple parents).
1671
('text-a', [], TEXT_1),
1672
('text-b', ['text-a'], TEXT_1),
1673
('text-c', [], TEXT_1),
1674
('text-d', ['text-c'], TEXT_1),
1675
('text-m', ['text-b', 'text-d'], TEXT_1),
1677
for version_id, parents, lines in test_data:
1678
k1.add_lines(version_id, parents, split_lines(lines))
1680
# This test is actually a bit strict as the order in which they're
1681
# returned is not defined. This matches the current (deterministic)
1683
expected_data_list = [
1684
# version, options, length, parents
1685
('text-a', ['fulltext'], 122, ()),
1686
('text-b', ['line-delta'], 84, ('text-a',)),
1687
('text-m', ['line-delta'], 84, ('text-b', 'text-d')),
1688
('text-c', ['fulltext'], 121, ()),
1689
('text-d', ['line-delta'], 84, ('text-c',)),
1691
format, data_list, reader_callable = k1.get_data_stream(
1692
['text-a', 'text-b', 'text-c', 'text-d', 'text-m'])
1693
self.assertEqual('knit-plain', format)
1694
self.assertEqual(expected_data_list, data_list)
1695
for version_id, options, length, parents in expected_data_list:
1696
bytes = reader_callable(length)
1697
self.assertRecordContentEqual(k1, version_id, bytes)
1699
def assertKnitFilesEqual(self, knit1, knit2):
1700
"""Assert that the contents of the index and data files of two knits are
1704
knit1.transport.get_bytes(knit1._data._access._filename),
1705
knit2.transport.get_bytes(knit2._data._access._filename))
1707
knit1.transport.get_bytes(knit1._index._filename),
1708
knit2.transport.get_bytes(knit2._index._filename))
1710
def assertKnitValuesEqual(self, left, right):
1711
"""Assert that the texts, annotations and graph of left and right are
1714
self.assertEqual(set(left.versions()), set(right.versions()))
1715
for version in left.versions():
1716
self.assertEqual(left.get_parents_with_ghosts(version),
1717
right.get_parents_with_ghosts(version))
1718
self.assertEqual(left.get_lines(version),
1719
right.get_lines(version))
1720
self.assertEqual(left.annotate(version),
1721
right.annotate(version))
1723
def test_insert_data_stream_empty(self):
1724
"""Inserting a data stream with no records should not put any data into
1727
k1 = self.make_test_knit()
1728
k1.insert_data_stream(
1729
(k1.get_format_signature(), [], lambda ignored: ''))
1730
self.assertEqual('', k1.transport.get_bytes(k1._data._access._filename),
1731
"The .knit should be completely empty.")
1732
self.assertEqual(k1._index.HEADER,
1733
k1.transport.get_bytes(k1._index._filename),
1734
"The .kndx should have nothing apart from the header.")
1736
def test_insert_data_stream_one_record(self):
1737
"""Inserting a data stream with one record from a knit with one record
1738
results in byte-identical files.
1740
source = self.make_test_knit(name='source')
1741
source.add_lines('text-a', [], split_lines(TEXT_1))
1742
data_stream = source.get_data_stream(['text-a'])
1743
target = self.make_test_knit(name='target')
1744
target.insert_data_stream(data_stream)
1745
self.assertKnitFilesEqual(source, target)
1747
def test_insert_data_stream_annotated_unannotated(self):
1748
"""Inserting an annotated datastream to an unannotated knit works."""
1749
# case one - full texts.
1750
source = self.make_test_knit(name='source', annotate=True)
1751
target = self.make_test_knit(name='target', annotate=False)
1752
source.add_lines('text-a', [], split_lines(TEXT_1))
1753
target.insert_data_stream(source.get_data_stream(['text-a']))
1754
self.assertKnitValuesEqual(source, target)
1755
# case two - deltas.
1756
source.add_lines('text-b', ['text-a'], split_lines(TEXT_2))
1757
target.insert_data_stream(source.get_data_stream(['text-b']))
1758
self.assertKnitValuesEqual(source, target)
1760
def test_insert_data_stream_unannotated_annotated(self):
1761
"""Inserting an unannotated datastream to an annotated knit works."""
1762
# case one - full texts.
1763
source = self.make_test_knit(name='source', annotate=False)
1764
target = self.make_test_knit(name='target', annotate=True)
1765
source.add_lines('text-a', [], split_lines(TEXT_1))
1766
target.insert_data_stream(source.get_data_stream(['text-a']))
1767
self.assertKnitValuesEqual(source, target)
1768
# case two - deltas.
1769
source.add_lines('text-b', ['text-a'], split_lines(TEXT_2))
1770
target.insert_data_stream(source.get_data_stream(['text-b']))
1771
self.assertKnitValuesEqual(source, target)
1773
def test_insert_data_stream_records_already_present(self):
1774
"""Insert a data stream where some records are alreday present in the
1775
target, and some not. Only the new records are inserted.
1777
source = self.make_test_knit(name='source')
1778
target = self.make_test_knit(name='target')
1779
# Insert 'text-a' into both source and target
1780
source.add_lines('text-a', [], split_lines(TEXT_1))
1781
target.insert_data_stream(source.get_data_stream(['text-a']))
1782
# Insert 'text-b' into just the source.
1783
source.add_lines('text-b', ['text-a'], split_lines(TEXT_1))
1784
# Get a data stream of both text-a and text-b, and insert it.
1785
data_stream = source.get_data_stream(['text-a', 'text-b'])
1786
target.insert_data_stream(data_stream)
1787
# The source and target will now be identical. This means the text-a
1788
# record was not added a second time.
1789
self.assertKnitFilesEqual(source, target)
1791
def test_insert_data_stream_multiple_records(self):
1792
"""Inserting a data stream of all records from a knit with multiple
1793
records results in byte-identical files.
1795
source = self.make_test_knit(name='source')
1796
source.add_lines('text-a', [], split_lines(TEXT_1))
1797
source.add_lines('text-b', ['text-a'], split_lines(TEXT_1))
1798
source.add_lines('text-c', [], split_lines(TEXT_1))
1799
data_stream = source.get_data_stream(['text-a', 'text-b', 'text-c'])
1801
target = self.make_test_knit(name='target')
1802
target.insert_data_stream(data_stream)
1804
self.assertKnitFilesEqual(source, target)
1806
def test_insert_data_stream_ghost_parent(self):
1807
"""Insert a data stream with a record that has a ghost parent."""
1808
# Make a knit with a record, text-a, that has a ghost parent.
1809
source = self.make_test_knit(name='source')
1810
source.add_lines_with_ghosts('text-a', ['text-ghost'],
1811
split_lines(TEXT_1))
1812
data_stream = source.get_data_stream(['text-a'])
1814
target = self.make_test_knit(name='target')
1815
target.insert_data_stream(data_stream)
1817
self.assertKnitFilesEqual(source, target)
1819
# The target knit object is in a consistent state, i.e. the record we
1820
# just added is immediately visible.
1821
self.assertTrue(target.has_version('text-a'))
1822
self.assertFalse(target.has_version('text-ghost'))
1823
self.assertEqual({'text-a':('text-ghost',)},
1824
target.get_parent_map(['text-a', 'text-ghost']))
1825
self.assertEqual(split_lines(TEXT_1), target.get_lines('text-a'))
1827
def test_insert_data_stream_inconsistent_version_lines(self):
1828
"""Inserting a data stream which has different content for a version_id
1829
than already exists in the knit will raise KnitCorrupt.
1831
source = self.make_test_knit(name='source')
1832
target = self.make_test_knit(name='target')
1833
# Insert a different 'text-a' into both source and target
1834
source.add_lines('text-a', [], split_lines(TEXT_1))
1835
target.add_lines('text-a', [], split_lines(TEXT_2))
1836
# Insert a data stream with conflicting content into the target
1837
data_stream = source.get_data_stream(['text-a'])
1839
errors.KnitCorrupt, target.insert_data_stream, data_stream)
1841
def test_insert_data_stream_inconsistent_version_parents(self):
1842
"""Inserting a data stream which has different parents for a version_id
1843
than already exists in the knit will raise KnitCorrupt.
1845
source = self.make_test_knit(name='source')
1846
target = self.make_test_knit(name='target')
1847
# Insert a different 'text-a' into both source and target. They differ
1848
# only by the parents list, the content is the same.
1849
source.add_lines_with_ghosts('text-a', [], split_lines(TEXT_1))
1850
target.add_lines_with_ghosts('text-a', ['a-ghost'], split_lines(TEXT_1))
1851
# Insert a data stream with conflicting content into the target
1852
data_stream = source.get_data_stream(['text-a'])
1854
errors.KnitCorrupt, target.insert_data_stream, data_stream)
1856
def test_insert_data_stream_unknown_format(self):
1857
"""A data stream in a different format to the target knit cannot be
1860
It will raise KnitDataStreamUnknown because the fallback code will fail
1861
to make a knit. In future we may need KnitDataStreamIncompatible again,
1862
for more exotic cases.
1864
data_stream = ('fake-format-signature', [], lambda _: '')
1865
target = self.make_test_knit(name='target')
1867
errors.KnitDataStreamUnknown,
1868
target.insert_data_stream, data_stream)
1870
def test_insert_data_stream_bug_208418(self):
1871
"""You can insert a stream with an incompatible format, even when:
1872
* the stream has a line-delta record,
1873
* whose parent is in the target, also stored as a line-delta
1875
See <https://launchpad.net/bugs/208418>.
1877
base_lines = split_lines(TEXT_1)
1879
target = self.make_test_knit(name='target', annotate=True)
1880
target.add_lines('version-1', [], base_lines)
1881
target.add_lines('version-2', ['version-1'], base_lines + ['a\n'])
1882
# The second record should be a delta.
1883
self.assertEqual('line-delta', target._index.get_method('version-2'))
1885
# Make a source, with a different format, but the same data
1886
source = self.make_test_knit(name='source', annotate=False)
1887
source.add_lines('version-1', [], base_lines)
1888
source.add_lines('version-2', ['version-1'], base_lines + ['a\n'])
1889
# Now add another record, which should be stored as a delta against
1891
source.add_lines('version-3', ['version-2'], base_lines + ['b\n'])
1892
self.assertEqual('line-delta', source._index.get_method('version-3'))
1894
# Make a stream of the new version
1895
data_stream = source.get_data_stream(['version-3'])
1896
# And insert into the target
1897
target.insert_data_stream(data_stream)
1898
# No errors should have been raised.
1901
# * test that a stream of "already present version, then new version"
1902
# inserts correctly.
1905
def assertMadeStreamKnit(self, source_knit, versions, target_knit):
1906
"""Assert that a knit made from a stream is as expected."""
1907
a_stream = source_knit.get_data_stream(versions)
1908
expected_data = a_stream[2](None)
1909
a_stream = source_knit.get_data_stream(versions)
1910
a_knit = target_knit._knit_from_datastream(a_stream)
1911
self.assertEqual(source_knit.factory.__class__,
1912
a_knit.factory.__class__)
1913
self.assertIsInstance(a_knit._data._access, _StreamAccess)
1914
self.assertIsInstance(a_knit._index, _StreamIndex)
1915
self.assertEqual(a_knit._index.data_list, a_stream[1])
1916
self.assertEqual(a_knit._data._access.data, expected_data)
1917
self.assertEqual(a_knit.filename, target_knit.filename)
1918
self.assertEqual(a_knit.transport, target_knit.transport)
1919
self.assertEqual(a_knit._index, a_knit._data._access.stream_index)
1920
self.assertEqual(target_knit, a_knit._data._access.backing_knit)
1921
self.assertIsInstance(a_knit._data._access.orig_factory,
1922
source_knit.factory.__class__)
1924
def test__knit_from_data_stream_empty(self):
1925
"""Create a knit object from a datastream."""
1926
annotated = self.make_test_knit(name='source', annotate=True)
1927
plain = self.make_test_knit(name='target', annotate=False)
1928
# case 1: annotated source
1929
self.assertMadeStreamKnit(annotated, [], annotated)
1930
self.assertMadeStreamKnit(annotated, [], plain)
1931
# case 2: plain source
1932
self.assertMadeStreamKnit(plain, [], annotated)
1933
self.assertMadeStreamKnit(plain, [], plain)
1935
def test__knit_from_data_stream_unknown_format(self):
1936
annotated = self.make_test_knit(name='source', annotate=True)
1937
self.assertRaises(errors.KnitDataStreamUnknown,
1938
annotated._knit_from_datastream, ("unknown", None, None))
1950
Banana cup cake recipe
1956
- self-raising flour
1960
Banana cup cake recipe
1962
- bananas (do not use plantains!!!)
1969
Banana cup cake recipe
1972
- self-raising flour
1985
AB_MERGE_TEXT="""unchanged|Banana cup cake recipe
1990
new-b|- bananas (do not use plantains!!!)
1991
unchanged|- broken tea cups
1992
new-a|- self-raising flour
1995
AB_MERGE=[tuple(l.split('|')) for l in AB_MERGE_TEXT.splitlines(True)]
1998
def line_delta(from_lines, to_lines):
1999
"""Generate line-based delta from one text to another"""
2000
s = difflib.SequenceMatcher(None, from_lines, to_lines)
2001
for op in s.get_opcodes():
2002
if op[0] == 'equal':
2004
yield '%d,%d,%d\n' % (op[1], op[2], op[4]-op[3])
2005
for i in range(op[3], op[4]):
2009
def apply_line_delta(basis_lines, delta_lines):
2010
"""Apply a line-based perfect diff
2012
basis_lines -- text to apply the patch to
2013
delta_lines -- diff instructions and content
2015
out = basis_lines[:]
2018
while i < len(delta_lines):
2020
a, b, c = map(long, l.split(','))
2022
out[offset+a:offset+b] = delta_lines[i:i+c]
2024
offset = offset + (b - a) + c
2028
class TestWeaveToKnit(KnitTests):
2030
def test_weave_to_knit_matches(self):
2031
# check that the WeaveToKnit is_compatible function
2032
# registers True for a Weave to a Knit.
2033
w = Weave(get_scope=lambda:None)
2034
k = self.make_test_knit()
2035
self.failUnless(WeaveToKnit.is_compatible(w, k))
2036
self.failIf(WeaveToKnit.is_compatible(k, w))
2037
self.failIf(WeaveToKnit.is_compatible(w, w))
2038
self.failIf(WeaveToKnit.is_compatible(k, k))
1529
2041
class TestKnitIndex(KnitTests):
1657
2160
add_callback = self.catch_add
1659
2162
add_callback = None
1660
return _KnitGraphIndex(combined_index, lambda:True, deltas=deltas,
2163
return KnitGraphIndex(combined_index, deltas=deltas,
1661
2164
add_callback=add_callback)
1663
def test_keys(self):
1664
index = self.two_graph_index()
1665
self.assertEqual(set([('tail',), ('tip',), ('parent',), ('separate',)]),
2166
def test_get_ancestry(self):
2167
# get_ancestry is defined as eliding ghosts, not erroring.
2168
index = self.two_graph_index()
2169
self.assertEqual([], index.get_ancestry([]))
2170
self.assertEqual(['separate'], index.get_ancestry(['separate']))
2171
self.assertEqual(['tail'], index.get_ancestry(['tail']))
2172
self.assertEqual(['tail', 'parent'], index.get_ancestry(['parent']))
2173
self.assertEqual(['tail', 'parent', 'tip'], index.get_ancestry(['tip']))
2174
self.assertTrue(index.get_ancestry(['tip', 'separate']) in
2175
(['tail', 'parent', 'tip', 'separate'],
2176
['separate', 'tail', 'parent', 'tip'],
2178
# and without topo_sort
2179
self.assertEqual(set(['separate']),
2180
set(index.get_ancestry(['separate'], topo_sorted=False)))
2181
self.assertEqual(set(['tail']),
2182
set(index.get_ancestry(['tail'], topo_sorted=False)))
2183
self.assertEqual(set(['tail', 'parent']),
2184
set(index.get_ancestry(['parent'], topo_sorted=False)))
2185
self.assertEqual(set(['tail', 'parent', 'tip']),
2186
set(index.get_ancestry(['tip'], topo_sorted=False)))
2187
self.assertEqual(set(['separate', 'tail', 'parent', 'tip']),
2188
set(index.get_ancestry(['tip', 'separate'])))
2189
# asking for a ghost makes it go boom.
2190
self.assertRaises(errors.RevisionNotPresent, index.get_ancestry, ['ghost'])
2192
def test_get_ancestry_with_ghosts(self):
2193
index = self.two_graph_index()
2194
self.assertEqual([], index.get_ancestry_with_ghosts([]))
2195
self.assertEqual(['separate'], index.get_ancestry_with_ghosts(['separate']))
2196
self.assertEqual(['tail'], index.get_ancestry_with_ghosts(['tail']))
2197
self.assertTrue(index.get_ancestry_with_ghosts(['parent']) in
2198
(['tail', 'ghost', 'parent'],
2199
['ghost', 'tail', 'parent'],
2201
self.assertTrue(index.get_ancestry_with_ghosts(['tip']) in
2202
(['tail', 'ghost', 'parent', 'tip'],
2203
['ghost', 'tail', 'parent', 'tip'],
2205
self.assertTrue(index.get_ancestry_with_ghosts(['tip', 'separate']) in
2206
(['tail', 'ghost', 'parent', 'tip', 'separate'],
2207
['ghost', 'tail', 'parent', 'tip', 'separate'],
2208
['separate', 'tail', 'ghost', 'parent', 'tip'],
2209
['separate', 'ghost', 'tail', 'parent', 'tip'],
2211
# asking for a ghost makes it go boom.
2212
self.assertRaises(errors.RevisionNotPresent, index.get_ancestry_with_ghosts, ['ghost'])
2214
def test_num_versions(self):
2215
index = self.two_graph_index()
2216
self.assertEqual(4, index.num_versions())
2218
def test_get_versions(self):
2219
index = self.two_graph_index()
2220
self.assertEqual(set(['tail', 'tip', 'parent', 'separate']),
2221
set(index.get_versions()))
2223
def test_has_version(self):
2224
index = self.two_graph_index()
2225
self.assertTrue(index.has_version('tail'))
2226
self.assertFalse(index.has_version('ghost'))
1668
2228
def test_get_position(self):
1669
2229
index = self.two_graph_index()
1670
self.assertEqual((index._graph_index._indices[0], 0, 100), index.get_position(('tip',)))
1671
self.assertEqual((index._graph_index._indices[1], 100, 78), index.get_position(('parent',)))
2230
self.assertEqual((index._graph_index._indices[0], 0, 100), index.get_position('tip'))
2231
self.assertEqual((index._graph_index._indices[1], 100, 78), index.get_position('parent'))
1673
2233
def test_get_method_deltas(self):
1674
2234
index = self.two_graph_index(deltas=True)
1675
self.assertEqual('fulltext', index.get_method(('tip',)))
1676
self.assertEqual('line-delta', index.get_method(('parent',)))
2235
self.assertEqual('fulltext', index.get_method('tip'))
2236
self.assertEqual('line-delta', index.get_method('parent'))
1678
2238
def test_get_method_no_deltas(self):
1679
2239
# check that the parent-history lookup is ignored with deltas=False.
1680
2240
index = self.two_graph_index(deltas=False)
1681
self.assertEqual('fulltext', index.get_method(('tip',)))
1682
self.assertEqual('fulltext', index.get_method(('parent',)))
2241
self.assertEqual('fulltext', index.get_method('tip'))
2242
self.assertEqual('fulltext', index.get_method('parent'))
1684
2244
def test_get_options_deltas(self):
1685
2245
index = self.two_graph_index(deltas=True)
1686
self.assertEqual(['fulltext', 'no-eol'], index.get_options(('tip',)))
1687
self.assertEqual(['line-delta'], index.get_options(('parent',)))
2246
self.assertEqual(['fulltext', 'no-eol'], index.get_options('tip'))
2247
self.assertEqual(['line-delta'], index.get_options('parent'))
1689
2249
def test_get_options_no_deltas(self):
1690
2250
# check that the parent-history lookup is ignored with deltas=False.
1691
2251
index = self.two_graph_index(deltas=False)
1692
self.assertEqual(['fulltext', 'no-eol'], index.get_options(('tip',)))
1693
self.assertEqual(['fulltext'], index.get_options(('parent',)))
1695
def test_get_parent_map(self):
1696
index = self.two_graph_index()
1697
self.assertEqual({('parent',):(('tail',), ('ghost',))},
1698
index.get_parent_map([('parent',), ('ghost',)]))
2252
self.assertEqual(['fulltext', 'no-eol'], index.get_options('tip'))
2253
self.assertEqual(['fulltext'], index.get_options('parent'))
2255
def test_get_parents_with_ghosts(self):
2256
index = self.two_graph_index()
2257
self.assertEqual(('tail', 'ghost'), index.get_parents_with_ghosts('parent'))
2258
# and errors on ghosts.
2259
self.assertRaises(errors.RevisionNotPresent,
2260
index.get_parents_with_ghosts, 'ghost')
2262
def test_check_versions_present(self):
2263
# ghosts should not be considered present
2264
index = self.two_graph_index()
2265
self.assertRaises(RevisionNotPresent, index.check_versions_present,
2267
self.assertRaises(RevisionNotPresent, index.check_versions_present,
2269
index.check_versions_present(['tail', 'separate'])
1700
2271
def catch_add(self, entries):
1701
2272
self.caught_entries.append(entries)
1703
2274
def test_add_no_callback_errors(self):
1704
2275
index = self.two_graph_index()
1705
self.assertRaises(errors.ReadOnlyError, index.add_records,
1706
[(('new',), 'fulltext,no-eol', (None, 50, 60), ['separate'])])
2276
self.assertRaises(errors.ReadOnlyError, index.add_version,
2277
'new', 'fulltext,no-eol', (None, 50, 60), ['separate'])
1708
2279
def test_add_version_smoke(self):
1709
2280
index = self.two_graph_index(catch_adds=True)
1710
index.add_records([(('new',), 'fulltext,no-eol', (None, 50, 60),
2281
index.add_version('new', 'fulltext,no-eol', (None, 50, 60), ['separate'])
1712
2282
self.assertEqual([[(('new', ), 'N50 60', ((('separate',),),))]],
1713
2283
self.caught_entries)
1715
2285
def test_add_version_delta_not_delta_index(self):
1716
2286
index = self.two_graph_index(catch_adds=True)
1717
self.assertRaises(errors.KnitCorrupt, index.add_records,
1718
[(('new',), 'no-eol,line-delta', (None, 0, 100), [('parent',)])])
2287
self.assertRaises(errors.KnitCorrupt, index.add_version,
2288
'new', 'no-eol,line-delta', (None, 0, 100), ['parent'])
1719
2289
self.assertEqual([], self.caught_entries)
1721
2291
def test_add_version_same_dup(self):
1722
2292
index = self.two_graph_index(catch_adds=True)
1723
2293
# options can be spelt two different ways
1724
index.add_records([(('tip',), 'fulltext,no-eol', (None, 0, 100), [('parent',)])])
1725
index.add_records([(('tip',), 'no-eol,fulltext', (None, 0, 100), [('parent',)])])
1726
# position/length are ignored (because each pack could have fulltext or
1727
# delta, and be at a different position.
1728
index.add_records([(('tip',), 'fulltext,no-eol', (None, 50, 100),
1730
index.add_records([(('tip',), 'fulltext,no-eol', (None, 0, 1000),
1732
# but neither should have added data:
1733
self.assertEqual([[], [], [], []], self.caught_entries)
2294
index.add_version('tip', 'fulltext,no-eol', (None, 0, 100), ['parent'])
2295
index.add_version('tip', 'no-eol,fulltext', (None, 0, 100), ['parent'])
2296
# but neither should have added data.
2297
self.assertEqual([[], []], self.caught_entries)
1735
2299
def test_add_version_different_dup(self):
1736
2300
index = self.two_graph_index(deltas=True, catch_adds=True)
1737
2301
# change options
1738
self.assertRaises(errors.KnitCorrupt, index.add_records,
1739
[(('tip',), 'line-delta', (None, 0, 100), [('parent',)])])
1740
self.assertRaises(errors.KnitCorrupt, index.add_records,
1741
[(('tip',), 'fulltext', (None, 0, 100), [('parent',)])])
2302
self.assertRaises(errors.KnitCorrupt, index.add_version,
2303
'tip', 'no-eol,line-delta', (None, 0, 100), ['parent'])
2304
self.assertRaises(errors.KnitCorrupt, index.add_version,
2305
'tip', 'line-delta,no-eol', (None, 0, 100), ['parent'])
2306
self.assertRaises(errors.KnitCorrupt, index.add_version,
2307
'tip', 'fulltext', (None, 0, 100), ['parent'])
2309
self.assertRaises(errors.KnitCorrupt, index.add_version,
2310
'tip', 'fulltext,no-eol', (None, 50, 100), ['parent'])
2311
self.assertRaises(errors.KnitCorrupt, index.add_version,
2312
'tip', 'fulltext,no-eol', (None, 0, 1000), ['parent'])
1743
self.assertRaises(errors.KnitCorrupt, index.add_records,
1744
[(('tip',), 'fulltext,no-eol', (None, 0, 100), [])])
2314
self.assertRaises(errors.KnitCorrupt, index.add_version,
2315
'tip', 'fulltext,no-eol', (None, 0, 100), [])
1745
2316
self.assertEqual([], self.caught_entries)
1747
2318
def test_add_versions_nodeltas(self):
1748
2319
index = self.two_graph_index(catch_adds=True)
1750
(('new',), 'fulltext,no-eol', (None, 50, 60), [('separate',)]),
1751
(('new2',), 'fulltext', (None, 0, 6), [('new',)]),
2320
index.add_versions([
2321
('new', 'fulltext,no-eol', (None, 50, 60), ['separate']),
2322
('new2', 'fulltext', (None, 0, 6), ['new']),
1753
2324
self.assertEqual([(('new', ), 'N50 60', ((('separate',),),)),
1754
2325
(('new2', ), ' 0 6', ((('new',),),))],
2048
2551
def test_add_versions_delta_not_delta_index(self):
2049
2552
index = self.two_graph_index(catch_adds=True)
2050
self.assertRaises(errors.KnitCorrupt, index.add_records,
2051
[(('new',), 'no-eol,line-delta', (None, 0, 100), [('parent',)])])
2553
self.assertRaises(errors.KnitCorrupt, index.add_versions,
2554
[('new', 'no-eol,line-delta', (None, 0, 100), ['parent'])])
2052
2555
self.assertEqual([], self.caught_entries)
2054
2557
def test_add_versions_parents_not_parents_index(self):
2055
2558
index = self.two_graph_index(catch_adds=True)
2056
self.assertRaises(errors.KnitCorrupt, index.add_records,
2057
[(('new',), 'no-eol,fulltext', (None, 0, 100), [('parent',)])])
2559
self.assertRaises(errors.KnitCorrupt, index.add_versions,
2560
[('new', 'no-eol,fulltext', (None, 0, 100), ['parent'])])
2058
2561
self.assertEqual([], self.caught_entries)
2060
2563
def test_add_versions_random_id_accepted(self):
2061
2564
index = self.two_graph_index(catch_adds=True)
2062
index.add_records([], random_id=True)
2565
index.add_versions([], random_id=True)
2064
2567
def test_add_versions_same_dup(self):
2065
2568
index = self.two_graph_index(catch_adds=True)
2066
2569
# options can be spelt two different ways
2067
index.add_records([(('tip',), 'fulltext,no-eol', (None, 0, 100), [])])
2068
index.add_records([(('tip',), 'no-eol,fulltext', (None, 0, 100), [])])
2069
# position/length are ignored (because each pack could have fulltext or
2070
# delta, and be at a different position.
2071
index.add_records([(('tip',), 'fulltext,no-eol', (None, 50, 100), [])])
2072
index.add_records([(('tip',), 'fulltext,no-eol', (None, 0, 1000), [])])
2570
index.add_versions([('tip', 'fulltext,no-eol', (None, 0, 100), [])])
2571
index.add_versions([('tip', 'no-eol,fulltext', (None, 0, 100), [])])
2073
2572
# but neither should have added data.
2074
self.assertEqual([[], [], [], []], self.caught_entries)
2573
self.assertEqual([[], []], self.caught_entries)
2076
2575
def test_add_versions_different_dup(self):
2077
2576
index = self.two_graph_index(catch_adds=True)
2078
2577
# change options
2079
self.assertRaises(errors.KnitCorrupt, index.add_records,
2080
[(('tip',), 'no-eol,line-delta', (None, 0, 100), [])])
2081
self.assertRaises(errors.KnitCorrupt, index.add_records,
2082
[(('tip',), 'line-delta,no-eol', (None, 0, 100), [])])
2083
self.assertRaises(errors.KnitCorrupt, index.add_records,
2084
[(('tip',), 'fulltext', (None, 0, 100), [])])
2578
self.assertRaises(errors.KnitCorrupt, index.add_versions,
2579
[('tip', 'no-eol,line-delta', (None, 0, 100), [])])
2580
self.assertRaises(errors.KnitCorrupt, index.add_versions,
2581
[('tip', 'line-delta,no-eol', (None, 0, 100), [])])
2582
self.assertRaises(errors.KnitCorrupt, index.add_versions,
2583
[('tip', 'fulltext', (None, 0, 100), [])])
2585
self.assertRaises(errors.KnitCorrupt, index.add_versions,
2586
[('tip', 'fulltext,no-eol', (None, 50, 100), [])])
2587
self.assertRaises(errors.KnitCorrupt, index.add_versions,
2588
[('tip', 'fulltext,no-eol', (None, 0, 1000), [])])
2086
self.assertRaises(errors.KnitCorrupt, index.add_records,
2087
[(('tip',), 'fulltext,no-eol', (None, 0, 100), [('parent',)])])
2590
self.assertRaises(errors.KnitCorrupt, index.add_versions,
2591
[('tip', 'fulltext,no-eol', (None, 0, 100), ['parent'])])
2088
2592
# change options in the second record
2089
self.assertRaises(errors.KnitCorrupt, index.add_records,
2090
[(('tip',), 'fulltext,no-eol', (None, 0, 100), []),
2091
(('tip',), 'no-eol,line-delta', (None, 0, 100), [])])
2593
self.assertRaises(errors.KnitCorrupt, index.add_versions,
2594
[('tip', 'fulltext,no-eol', (None, 0, 100), []),
2595
('tip', 'no-eol,line-delta', (None, 0, 100), [])])
2092
2596
self.assertEqual([], self.caught_entries)
2095
class TestKnitVersionedFiles(KnitTests):
2097
def assertGroupKeysForIo(self, exp_groups, keys, non_local_keys,
2098
positions, _min_buffer_size=None):
2099
kvf = self.make_test_knit()
2100
if _min_buffer_size is None:
2101
_min_buffer_size = knit._STREAM_MIN_BUFFER_SIZE
2102
self.assertEqual(exp_groups, kvf._group_keys_for_io(keys,
2103
non_local_keys, positions,
2104
_min_buffer_size=_min_buffer_size))
2106
def assertSplitByPrefix(self, expected_map, expected_prefix_order,
2108
split, prefix_order = KnitVersionedFiles._split_by_prefix(keys)
2109
self.assertEqual(expected_map, split)
2110
self.assertEqual(expected_prefix_order, prefix_order)
2112
def test__group_keys_for_io(self):
2113
ft_detail = ('fulltext', False)
2114
ld_detail = ('line-delta', False)
2122
f_a: (ft_detail, (f_a, 0, 100), None),
2123
f_b: (ld_detail, (f_b, 100, 21), f_a),
2124
f_c: (ld_detail, (f_c, 180, 15), f_b),
2125
g_a: (ft_detail, (g_a, 121, 35), None),
2126
g_b: (ld_detail, (g_b, 156, 12), g_a),
2127
g_c: (ld_detail, (g_c, 195, 13), g_a),
2129
self.assertGroupKeysForIo([([f_a], set())],
2130
[f_a], [], positions)
2131
self.assertGroupKeysForIo([([f_a], set([f_a]))],
2132
[f_a], [f_a], positions)
2133
self.assertGroupKeysForIo([([f_a, f_b], set([]))],
2134
[f_a, f_b], [], positions)
2135
self.assertGroupKeysForIo([([f_a, f_b], set([f_b]))],
2136
[f_a, f_b], [f_b], positions)
2137
self.assertGroupKeysForIo([([f_a, f_b, g_a, g_b], set())],
2138
[f_a, g_a, f_b, g_b], [], positions)
2139
self.assertGroupKeysForIo([([f_a, f_b, g_a, g_b], set())],
2140
[f_a, g_a, f_b, g_b], [], positions,
2141
_min_buffer_size=150)
2142
self.assertGroupKeysForIo([([f_a, f_b], set()), ([g_a, g_b], set())],
2143
[f_a, g_a, f_b, g_b], [], positions,
2144
_min_buffer_size=100)
2145
self.assertGroupKeysForIo([([f_c], set()), ([g_b], set())],
2146
[f_c, g_b], [], positions,
2147
_min_buffer_size=125)
2148
self.assertGroupKeysForIo([([g_b, f_c], set())],
2149
[g_b, f_c], [], positions,
2150
_min_buffer_size=125)
2152
def test__split_by_prefix(self):
2153
self.assertSplitByPrefix({'f': [('f', 'a'), ('f', 'b')],
2154
'g': [('g', 'b'), ('g', 'a')],
2156
[('f', 'a'), ('g', 'b'),
2157
('g', 'a'), ('f', 'b')])
2159
self.assertSplitByPrefix({'f': [('f', 'a'), ('f', 'b')],
2160
'g': [('g', 'b'), ('g', 'a')],
2162
[('f', 'a'), ('f', 'b'),
2163
('g', 'b'), ('g', 'a')])
2165
self.assertSplitByPrefix({'f': [('f', 'a'), ('f', 'b')],
2166
'g': [('g', 'b'), ('g', 'a')],
2168
[('f', 'a'), ('f', 'b'),
2169
('g', 'b'), ('g', 'a')])
2171
self.assertSplitByPrefix({'f': [('f', 'a'), ('f', 'b')],
2172
'g': [('g', 'b'), ('g', 'a')],
2173
'': [('a',), ('b',)]
2175
[('f', 'a'), ('g', 'b'),
2177
('g', 'a'), ('f', 'b')])
2180
class TestStacking(KnitTests):
2182
def get_basis_and_test_knit(self):
2183
basis = self.make_test_knit(name='basis')
2184
basis = RecordingVersionedFilesDecorator(basis)
2185
test = self.make_test_knit(name='test')
2186
test.add_fallback_versioned_files(basis)
2189
def test_add_fallback_versioned_files(self):
2190
basis = self.make_test_knit(name='basis')
2191
test = self.make_test_knit(name='test')
2192
# It must not error; other tests test that the fallback is referred to
2193
# when accessing data.
2194
test.add_fallback_versioned_files(basis)
2196
def test_add_lines(self):
2197
# lines added to the test are not added to the basis
2198
basis, test = self.get_basis_and_test_knit()
2200
key_basis = ('bar',)
2201
key_cross_border = ('quux',)
2202
key_delta = ('zaphod',)
2203
test.add_lines(key, (), ['foo\n'])
2204
self.assertEqual({}, basis.get_parent_map([key]))
2205
# lines added to the test that reference across the stack do a
2207
basis.add_lines(key_basis, (), ['foo\n'])
2209
test.add_lines(key_cross_border, (key_basis,), ['foo\n'])
2210
self.assertEqual('fulltext', test._index.get_method(key_cross_border))
2211
# we don't even need to look at the basis to see that this should be
2212
# stored as a fulltext
2213
self.assertEqual([], basis.calls)
2214
# Subsequent adds do delta.
2216
test.add_lines(key_delta, (key_cross_border,), ['foo\n'])
2217
self.assertEqual('line-delta', test._index.get_method(key_delta))
2218
self.assertEqual([], basis.calls)
2220
def test_annotate(self):
2221
# annotations from the test knit are answered without asking the basis
2222
basis, test = self.get_basis_and_test_knit()
2224
key_basis = ('bar',)
2225
key_missing = ('missing',)
2226
test.add_lines(key, (), ['foo\n'])
2227
details = test.annotate(key)
2228
self.assertEqual([(key, 'foo\n')], details)
2229
self.assertEqual([], basis.calls)
2230
# But texts that are not in the test knit are looked for in the basis
2232
basis.add_lines(key_basis, (), ['foo\n', 'bar\n'])
2234
details = test.annotate(key_basis)
2235
self.assertEqual([(key_basis, 'foo\n'), (key_basis, 'bar\n')], details)
2236
# Not optimised to date:
2237
# self.assertEqual([("annotate", key_basis)], basis.calls)
2238
self.assertEqual([('get_parent_map', set([key_basis])),
2239
('get_parent_map', set([key_basis])),
2240
('get_record_stream', [key_basis], 'topological', True)],
2243
def test_check(self):
2244
# At the moment checking a stacked knit does implicitly check the
2246
basis, test = self.get_basis_and_test_knit()
2249
def test_get_parent_map(self):
2250
# parents in the test knit are answered without asking the basis
2251
basis, test = self.get_basis_and_test_knit()
2253
key_basis = ('bar',)
2254
key_missing = ('missing',)
2255
test.add_lines(key, (), [])
2256
parent_map = test.get_parent_map([key])
2257
self.assertEqual({key: ()}, parent_map)
2258
self.assertEqual([], basis.calls)
2259
# But parents that are not in the test knit are looked for in the basis
2260
basis.add_lines(key_basis, (), [])
2262
parent_map = test.get_parent_map([key, key_basis, key_missing])
2263
self.assertEqual({key: (),
2264
key_basis: ()}, parent_map)
2265
self.assertEqual([("get_parent_map", set([key_basis, key_missing]))],
2268
def test_get_record_stream_unordered_fulltexts(self):
2269
# records from the test knit are answered without asking the basis:
2270
basis, test = self.get_basis_and_test_knit()
2272
key_basis = ('bar',)
2273
key_missing = ('missing',)
2274
test.add_lines(key, (), ['foo\n'])
2275
records = list(test.get_record_stream([key], 'unordered', True))
2276
self.assertEqual(1, len(records))
2277
self.assertEqual([], basis.calls)
2278
# Missing (from test knit) objects are retrieved from the basis:
2279
basis.add_lines(key_basis, (), ['foo\n', 'bar\n'])
2281
records = list(test.get_record_stream([key_basis, key_missing],
2283
self.assertEqual(2, len(records))
2284
calls = list(basis.calls)
2285
for record in records:
2286
self.assertSubset([record.key], (key_basis, key_missing))
2287
if record.key == key_missing:
2288
self.assertIsInstance(record, AbsentContentFactory)
2290
reference = list(basis.get_record_stream([key_basis],
2291
'unordered', True))[0]
2292
self.assertEqual(reference.key, record.key)
2293
self.assertEqual(reference.sha1, record.sha1)
2294
self.assertEqual(reference.storage_kind, record.storage_kind)
2295
self.assertEqual(reference.get_bytes_as(reference.storage_kind),
2296
record.get_bytes_as(record.storage_kind))
2297
self.assertEqual(reference.get_bytes_as('fulltext'),
2298
record.get_bytes_as('fulltext'))
2299
# It's not strictly minimal, but it seems reasonable for now for it to
2300
# ask which fallbacks have which parents.
2302
("get_parent_map", set([key_basis, key_missing])),
2303
("get_record_stream", [key_basis], 'unordered', True)],
2306
def test_get_record_stream_ordered_fulltexts(self):
2307
# ordering is preserved down into the fallback store.
2308
basis, test = self.get_basis_and_test_knit()
2310
key_basis = ('bar',)
2311
key_basis_2 = ('quux',)
2312
key_missing = ('missing',)
2313
test.add_lines(key, (key_basis,), ['foo\n'])
2314
# Missing (from test knit) objects are retrieved from the basis:
2315
basis.add_lines(key_basis, (key_basis_2,), ['foo\n', 'bar\n'])
2316
basis.add_lines(key_basis_2, (), ['quux\n'])
2318
# ask for in non-topological order
2319
records = list(test.get_record_stream(
2320
[key, key_basis, key_missing, key_basis_2], 'topological', True))
2321
self.assertEqual(4, len(records))
2323
for record in records:
2324
self.assertSubset([record.key],
2325
(key_basis, key_missing, key_basis_2, key))
2326
if record.key == key_missing:
2327
self.assertIsInstance(record, AbsentContentFactory)
2329
results.append((record.key, record.sha1, record.storage_kind,
2330
record.get_bytes_as('fulltext')))
2331
calls = list(basis.calls)
2332
order = [record[0] for record in results]
2333
self.assertEqual([key_basis_2, key_basis, key], order)
2334
for result in results:
2335
if result[0] == key:
2339
record = source.get_record_stream([result[0]], 'unordered',
2341
self.assertEqual(record.key, result[0])
2342
self.assertEqual(record.sha1, result[1])
2343
# We used to check that the storage kind matched, but actually it
2344
# depends on whether it was sourced from the basis, or in a single
2345
# group, because asking for full texts returns proxy objects to a
2346
# _ContentMapGenerator object; so checking the kind is unneeded.
2347
self.assertEqual(record.get_bytes_as('fulltext'), result[3])
2348
# It's not strictly minimal, but it seems reasonable for now for it to
2349
# ask which fallbacks have which parents.
2351
("get_parent_map", set([key_basis, key_basis_2, key_missing])),
2352
# topological is requested from the fallback, because that is what
2353
# was requested at the top level.
2354
("get_record_stream", [key_basis_2, key_basis], 'topological', True)],
2357
def test_get_record_stream_unordered_deltas(self):
2358
# records from the test knit are answered without asking the basis:
2359
basis, test = self.get_basis_and_test_knit()
2361
key_basis = ('bar',)
2362
key_missing = ('missing',)
2363
test.add_lines(key, (), ['foo\n'])
2364
records = list(test.get_record_stream([key], 'unordered', False))
2365
self.assertEqual(1, len(records))
2366
self.assertEqual([], basis.calls)
2367
# Missing (from test knit) objects are retrieved from the basis:
2368
basis.add_lines(key_basis, (), ['foo\n', 'bar\n'])
2370
records = list(test.get_record_stream([key_basis, key_missing],
2371
'unordered', False))
2372
self.assertEqual(2, len(records))
2373
calls = list(basis.calls)
2374
for record in records:
2375
self.assertSubset([record.key], (key_basis, key_missing))
2376
if record.key == key_missing:
2377
self.assertIsInstance(record, AbsentContentFactory)
2379
reference = list(basis.get_record_stream([key_basis],
2380
'unordered', False))[0]
2381
self.assertEqual(reference.key, record.key)
2382
self.assertEqual(reference.sha1, record.sha1)
2383
self.assertEqual(reference.storage_kind, record.storage_kind)
2384
self.assertEqual(reference.get_bytes_as(reference.storage_kind),
2385
record.get_bytes_as(record.storage_kind))
2386
# It's not strictly minimal, but it seems reasonable for now for it to
2387
# ask which fallbacks have which parents.
2389
("get_parent_map", set([key_basis, key_missing])),
2390
("get_record_stream", [key_basis], 'unordered', False)],
2393
def test_get_record_stream_ordered_deltas(self):
2394
# ordering is preserved down into the fallback store.
2395
basis, test = self.get_basis_and_test_knit()
2397
key_basis = ('bar',)
2398
key_basis_2 = ('quux',)
2399
key_missing = ('missing',)
2400
test.add_lines(key, (key_basis,), ['foo\n'])
2401
# Missing (from test knit) objects are retrieved from the basis:
2402
basis.add_lines(key_basis, (key_basis_2,), ['foo\n', 'bar\n'])
2403
basis.add_lines(key_basis_2, (), ['quux\n'])
2405
# ask for in non-topological order
2406
records = list(test.get_record_stream(
2407
[key, key_basis, key_missing, key_basis_2], 'topological', False))
2408
self.assertEqual(4, len(records))
2410
for record in records:
2411
self.assertSubset([record.key],
2412
(key_basis, key_missing, key_basis_2, key))
2413
if record.key == key_missing:
2414
self.assertIsInstance(record, AbsentContentFactory)
2416
results.append((record.key, record.sha1, record.storage_kind,
2417
record.get_bytes_as(record.storage_kind)))
2418
calls = list(basis.calls)
2419
order = [record[0] for record in results]
2420
self.assertEqual([key_basis_2, key_basis, key], order)
2421
for result in results:
2422
if result[0] == key:
2426
record = source.get_record_stream([result[0]], 'unordered',
2428
self.assertEqual(record.key, result[0])
2429
self.assertEqual(record.sha1, result[1])
2430
self.assertEqual(record.storage_kind, result[2])
2431
self.assertEqual(record.get_bytes_as(record.storage_kind), result[3])
2432
# It's not strictly minimal, but it seems reasonable for now for it to
2433
# ask which fallbacks have which parents.
2435
("get_parent_map", set([key_basis, key_basis_2, key_missing])),
2436
("get_record_stream", [key_basis_2, key_basis], 'topological', False)],
2439
def test_get_sha1s(self):
2440
# sha1's in the test knit are answered without asking the basis
2441
basis, test = self.get_basis_and_test_knit()
2443
key_basis = ('bar',)
2444
key_missing = ('missing',)
2445
test.add_lines(key, (), ['foo\n'])
2446
key_sha1sum = osutils.sha_string('foo\n')
2447
sha1s = test.get_sha1s([key])
2448
self.assertEqual({key: key_sha1sum}, sha1s)
2449
self.assertEqual([], basis.calls)
2450
# But texts that are not in the test knit are looked for in the basis
2451
# directly (rather than via text reconstruction) so that remote servers
2452
# etc don't have to answer with full content.
2453
basis.add_lines(key_basis, (), ['foo\n', 'bar\n'])
2454
basis_sha1sum = osutils.sha_string('foo\nbar\n')
2456
sha1s = test.get_sha1s([key, key_missing, key_basis])
2457
self.assertEqual({key: key_sha1sum,
2458
key_basis: basis_sha1sum}, sha1s)
2459
self.assertEqual([("get_sha1s", set([key_basis, key_missing]))],
2462
def test_insert_record_stream(self):
2463
# records are inserted as normal; insert_record_stream builds on
2464
# add_lines, so a smoke test should be all that's needed:
2466
key_basis = ('bar',)
2467
key_delta = ('zaphod',)
2468
basis, test = self.get_basis_and_test_knit()
2469
source = self.make_test_knit(name='source')
2470
basis.add_lines(key_basis, (), ['foo\n'])
2472
source.add_lines(key_basis, (), ['foo\n'])
2473
source.add_lines(key_delta, (key_basis,), ['bar\n'])
2474
stream = source.get_record_stream([key_delta], 'unordered', False)
2475
test.insert_record_stream(stream)
2476
# XXX: this does somewhat too many calls in making sure of whether it
2477
# has to recreate the full text.
2478
self.assertEqual([("get_parent_map", set([key_basis])),
2479
('get_parent_map', set([key_basis])),
2480
('get_record_stream', [key_basis], 'unordered', True)],
2482
self.assertEqual({key_delta:(key_basis,)},
2483
test.get_parent_map([key_delta]))
2484
self.assertEqual('bar\n', test.get_record_stream([key_delta],
2485
'unordered', True).next().get_bytes_as('fulltext'))
2487
def test_iter_lines_added_or_present_in_keys(self):
2488
# Lines from the basis are returned, and lines for a given key are only
2492
# all sources are asked for keys:
2493
basis, test = self.get_basis_and_test_knit()
2494
basis.add_lines(key1, (), ["foo"])
2496
lines = list(test.iter_lines_added_or_present_in_keys([key1]))
2497
self.assertEqual([("foo\n", key1)], lines)
2498
self.assertEqual([("iter_lines_added_or_present_in_keys", set([key1]))],
2500
# keys in both are not duplicated:
2501
test.add_lines(key2, (), ["bar\n"])
2502
basis.add_lines(key2, (), ["bar\n"])
2504
lines = list(test.iter_lines_added_or_present_in_keys([key2]))
2505
self.assertEqual([("bar\n", key2)], lines)
2506
self.assertEqual([], basis.calls)
2508
def test_keys(self):
2511
# all sources are asked for keys:
2512
basis, test = self.get_basis_and_test_knit()
2514
self.assertEqual(set(), set(keys))
2515
self.assertEqual([("keys",)], basis.calls)
2516
# keys from a basis are returned:
2517
basis.add_lines(key1, (), [])
2520
self.assertEqual(set([key1]), set(keys))
2521
self.assertEqual([("keys",)], basis.calls)
2522
# keys in both are not duplicated:
2523
test.add_lines(key2, (), [])
2524
basis.add_lines(key2, (), [])
2527
self.assertEqual(2, len(keys))
2528
self.assertEqual(set([key1, key2]), set(keys))
2529
self.assertEqual([("keys",)], basis.calls)
2531
def test_add_mpdiffs(self):
2532
# records are inserted as normal; add_mpdiff builds on
2533
# add_lines, so a smoke test should be all that's needed:
2535
key_basis = ('bar',)
2536
key_delta = ('zaphod',)
2537
basis, test = self.get_basis_and_test_knit()
2538
source = self.make_test_knit(name='source')
2539
basis.add_lines(key_basis, (), ['foo\n'])
2541
source.add_lines(key_basis, (), ['foo\n'])
2542
source.add_lines(key_delta, (key_basis,), ['bar\n'])
2543
diffs = source.make_mpdiffs([key_delta])
2544
test.add_mpdiffs([(key_delta, (key_basis,),
2545
source.get_sha1s([key_delta])[key_delta], diffs[0])])
2546
self.assertEqual([("get_parent_map", set([key_basis])),
2547
('get_record_stream', [key_basis], 'unordered', True),],
2549
self.assertEqual({key_delta:(key_basis,)},
2550
test.get_parent_map([key_delta]))
2551
self.assertEqual('bar\n', test.get_record_stream([key_delta],
2552
'unordered', True).next().get_bytes_as('fulltext'))
2554
def test_make_mpdiffs(self):
2555
# Generating an mpdiff across a stacking boundary should detect parent
2559
key_right = ('zaphod',)
2560
basis, test = self.get_basis_and_test_knit()
2561
basis.add_lines(key_left, (), ['bar\n'])
2562
basis.add_lines(key_right, (), ['zaphod\n'])
2564
test.add_lines(key, (key_left, key_right),
2565
['bar\n', 'foo\n', 'zaphod\n'])
2566
diffs = test.make_mpdiffs([key])
2568
multiparent.MultiParent([multiparent.ParentText(0, 0, 0, 1),
2569
multiparent.NewText(['foo\n']),
2570
multiparent.ParentText(1, 0, 2, 1)])],
2572
self.assertEqual(3, len(basis.calls))
2574
("get_parent_map", set([key_left, key_right])),
2575
("get_parent_map", set([key_left, key_right])),
2578
last_call = basis.calls[-1]
2579
self.assertEqual('get_record_stream', last_call[0])
2580
self.assertEqual(set([key_left, key_right]), set(last_call[1]))
2581
self.assertEqual('topological', last_call[2])
2582
self.assertEqual(True, last_call[3])
2585
class TestNetworkBehaviour(KnitTests):
2586
"""Tests for getting data out of/into knits over the network."""
2588
def test_include_delta_closure_generates_a_knit_delta_closure(self):
2589
vf = self.make_test_knit(name='test')
2590
# put in three texts, giving ft, delta, delta
2591
vf.add_lines(('base',), (), ['base\n', 'content\n'])
2592
vf.add_lines(('d1',), (('base',),), ['d1\n'])
2593
vf.add_lines(('d2',), (('d1',),), ['d2\n'])
2594
# But heuristics could interfere, so check what happened:
2595
self.assertEqual(['knit-ft-gz', 'knit-delta-gz', 'knit-delta-gz'],
2596
[record.storage_kind for record in
2597
vf.get_record_stream([('base',), ('d1',), ('d2',)],
2598
'topological', False)])
2599
# generate a stream of just the deltas include_delta_closure=True,
2600
# serialise to the network, and check that we get a delta closure on the wire.
2601
stream = vf.get_record_stream([('d1',), ('d2',)], 'topological', True)
2602
netb = [record.get_bytes_as(record.storage_kind) for record in stream]
2603
# The first bytes should be a memo from _ContentMapGenerator, and the
2604
# second bytes should be empty (because its a API proxy not something
2605
# for wire serialisation.
2606
self.assertEqual('', netb[1])
2608
kind, line_end = network_bytes_to_kind_and_offset(bytes)
2609
self.assertEqual('knit-delta-closure', kind)
2612
class TestContentMapGenerator(KnitTests):
2613
"""Tests for ContentMapGenerator"""
2615
def test_get_record_stream_gives_records(self):
2616
vf = self.make_test_knit(name='test')
2617
# put in three texts, giving ft, delta, delta
2618
vf.add_lines(('base',), (), ['base\n', 'content\n'])
2619
vf.add_lines(('d1',), (('base',),), ['d1\n'])
2620
vf.add_lines(('d2',), (('d1',),), ['d2\n'])
2621
keys = [('d1',), ('d2',)]
2622
generator = _VFContentMapGenerator(vf, keys,
2623
global_map=vf.get_parent_map(keys))
2624
for record in generator.get_record_stream():
2625
if record.key == ('d1',):
2626
self.assertEqual('d1\n', record.get_bytes_as('fulltext'))
2628
self.assertEqual('d2\n', record.get_bytes_as('fulltext'))
2630
def test_get_record_stream_kinds_are_raw(self):
2631
vf = self.make_test_knit(name='test')
2632
# put in three texts, giving ft, delta, delta
2633
vf.add_lines(('base',), (), ['base\n', 'content\n'])
2634
vf.add_lines(('d1',), (('base',),), ['d1\n'])
2635
vf.add_lines(('d2',), (('d1',),), ['d2\n'])
2636
keys = [('base',), ('d1',), ('d2',)]
2637
generator = _VFContentMapGenerator(vf, keys,
2638
global_map=vf.get_parent_map(keys))
2639
kinds = {('base',): 'knit-delta-closure',
2640
('d1',): 'knit-delta-closure-ref',
2641
('d2',): 'knit-delta-closure-ref',
2643
for record in generator.get_record_stream():
2644
self.assertEqual(kinds[record.key], record.storage_kind)
2598
class TestPackKnits(KnitTests):
2599
"""Tests that use a _PackAccess and KnitGraphIndex."""
2601
def test_get_data_stream_packs_ignores_pack_overhead(self):
2602
# Packs have an encoding overhead that should not be included in the
2603
# 'size' field of a data stream, because it is not returned by the
2604
# raw_reading functions - it is why index_memo's are opaque, and
2605
# get_data_stream was abusing this.
2606
packname = 'test.pack'
2607
transport = self.get_transport()
2608
def write_data(bytes):
2609
transport.append_bytes(packname, bytes)
2610
writer = pack.ContainerWriter(write_data)
2612
index = InMemoryGraphIndex(2)
2613
knit_index = KnitGraphIndex(index, add_callback=index.add_nodes,
2615
indices = {index:(transport, packname)}
2616
access = _PackAccess(indices, writer=(writer, index))
2617
k = KnitVersionedFile('test', get_transport('.'),
2618
delta=True, create=True, index=knit_index, access_method=access)
2619
# insert something into the knit
2620
k.add_lines('text-1', [], ["foo\n"])
2621
# get a data stream for it
2622
stream = k.get_data_stream(['text-1'])
2623
# if the stream has been incorrectly assembled, we will get a short read
2624
# reading from the stream (as streams have no trailer)
2625
expected_length = stream[1][0][2]
2626
# we use -1 to do the read, so that if a trailer is added this test
2627
# will fail and we'll adjust it to handle that case correctly, rather
2628
# than allowing an over-read that is bogus.
2629
self.assertEqual(expected_length, len(stream[2](-1)))
2632
class Test_StreamIndex(KnitTests):
2634
def get_index(self, knit, stream):
2635
"""Get a _StreamIndex from knit and stream."""
2636
return knit._knit_from_datastream(stream)._index
2638
def assertIndexVersions(self, knit, versions):
2639
"""Check that the _StreamIndex versions are those of the stream."""
2640
index = self.get_index(knit, knit.get_data_stream(versions))
2641
self.assertEqual(set(index.get_versions()), set(versions))
2642
# check we didn't get duplicates
2643
self.assertEqual(len(index.get_versions()), len(versions))
2645
def assertIndexAncestry(self, knit, ancestry_versions, versions, result):
2646
"""Check the result of a get_ancestry call on knit."""
2647
index = self.get_index(knit, knit.get_data_stream(versions))
2650
set(index.get_ancestry(ancestry_versions, False)))
2652
def assertGetMethod(self, knit, versions, version, result):
2653
index = self.get_index(knit, knit.get_data_stream(versions))
2654
self.assertEqual(result, index.get_method(version))
2656
def assertGetOptions(self, knit, version, options):
2657
index = self.get_index(knit, knit.get_data_stream(version))
2658
self.assertEqual(options, index.get_options(version))
2660
def assertGetPosition(self, knit, versions, version, result):
2661
index = self.get_index(knit, knit.get_data_stream(versions))
2662
if result[1] is None:
2663
result = (result[0], index, result[2], result[3])
2664
self.assertEqual(result, index.get_position(version))
2666
def assertGetParentsWithGhosts(self, knit, versions, version, parents):
2667
index = self.get_index(knit, knit.get_data_stream(versions))
2668
self.assertEqual(parents, index.get_parents_with_ghosts(version))
2670
def make_knit_with_4_versions_2_dags(self):
2671
knit = self.make_test_knit()
2672
knit.add_lines('a', [], ["foo"])
2673
knit.add_lines('b', [], [])
2674
knit.add_lines('c', ['b', 'a'], [])
2675
knit.add_lines_with_ghosts('d', ['e', 'f'], [])
2678
def test_versions(self):
2679
"""The versions of a StreamIndex are those of the datastream."""
2680
knit = self.make_knit_with_4_versions_2_dags()
2681
# ask for most permutations, which catches bugs like falling back to the
2682
# target knit, or showing ghosts, etc.
2683
self.assertIndexVersions(knit, [])
2684
self.assertIndexVersions(knit, ['a'])
2685
self.assertIndexVersions(knit, ['b'])
2686
self.assertIndexVersions(knit, ['c'])
2687
self.assertIndexVersions(knit, ['d'])
2688
self.assertIndexVersions(knit, ['a', 'b'])
2689
self.assertIndexVersions(knit, ['b', 'c'])
2690
self.assertIndexVersions(knit, ['a', 'c'])
2691
self.assertIndexVersions(knit, ['a', 'b', 'c'])
2692
self.assertIndexVersions(knit, ['a', 'b', 'c', 'd'])
2694
def test_construct(self):
2695
"""Constructing a StreamIndex generates index data."""
2696
data_list = [('text-a', ['fulltext'], 127, []),
2697
('text-b', ['option'], 128, ['text-c'])]
2698
index = _StreamIndex(data_list, None)
2699
self.assertEqual({'text-a':(['fulltext'], (0, 127), []),
2700
'text-b':(['option'], (127, 127 + 128), ['text-c'])},
2703
def test_get_ancestry(self):
2704
knit = self.make_knit_with_4_versions_2_dags()
2705
self.assertIndexAncestry(knit, ['a'], ['a'], ['a'])
2706
self.assertIndexAncestry(knit, ['b'], ['b'], ['b'])
2707
self.assertIndexAncestry(knit, ['c'], ['c'], ['c'])
2708
self.assertIndexAncestry(knit, ['c'], ['a', 'b', 'c'],
2709
set(['a', 'b', 'c']))
2710
self.assertIndexAncestry(knit, ['c', 'd'], ['a', 'b', 'c', 'd'],
2711
set(['a', 'b', 'c', 'd']))
2713
def test_get_method(self):
2714
knit = self.make_knit_with_4_versions_2_dags()
2715
self.assertGetMethod(knit, ['a'], 'a', 'fulltext')
2716
self.assertGetMethod(knit, ['c'], 'c', 'line-delta')
2717
# get_method on a basis that is not in the datastream (but in the
2718
# backing knit) returns 'fulltext', because thats what we'll create as
2720
self.assertGetMethod(knit, ['c'], 'b', 'fulltext')
2722
def test_get_options(self):
2723
knit = self.make_knit_with_4_versions_2_dags()
2724
self.assertGetOptions(knit, 'a', ['no-eol', 'fulltext'])
2725
self.assertGetOptions(knit, 'c', ['line-delta'])
2727
def test_get_parents_with_ghosts(self):
2728
knit = self.make_knit_with_4_versions_2_dags()
2729
self.assertGetParentsWithGhosts(knit, ['a'], 'a', ())
2730
self.assertGetParentsWithGhosts(knit, ['c'], 'c', ('b', 'a'))
2731
self.assertGetParentsWithGhosts(knit, ['d'], 'd', ('e', 'f'))
2733
def test_get_position(self):
2734
knit = self.make_knit_with_4_versions_2_dags()
2735
# get_position returns (thunk_flag, index(can be None), start, end) for
2736
# _StreamAccess to use.
2737
self.assertGetPosition(knit, ['a'], 'a', (False, None, 0, 78))
2738
self.assertGetPosition(knit, ['a', 'c'], 'c', (False, None, 78, 156))
2739
# get_position on a text that is not in the datastream (but in the
2740
# backing knit) returns (True, 'versionid', None, None) - and then the
2741
# access object can construct the relevant data as needed.
2742
self.assertGetPosition(knit, ['a', 'c'], 'b', (True, 'b', None, None))
2745
class Test_StreamAccess(KnitTests):
2747
def get_index_access(self, knit, stream):
2748
"""Get a _StreamAccess from knit and stream."""
2749
knit = knit._knit_from_datastream(stream)
2750
return knit._index, knit._data._access
2752
def assertGetRawRecords(self, knit, versions):
2753
index, access = self.get_index_access(knit,
2754
knit.get_data_stream(versions))
2755
# check that every version asked for can be obtained from the resulting
2759
for version in versions:
2760
memos.append(knit._index.get_position(version))
2762
for version, data in zip(
2763
versions, knit._data._access.get_raw_records(memos)):
2764
original[version] = data
2766
for version in versions:
2767
memos.append(index.get_position(version))
2769
for version, data in zip(versions, access.get_raw_records(memos)):
2770
streamed[version] = data
2771
self.assertEqual(original, streamed)
2773
for version in versions:
2774
data = list(access.get_raw_records(
2775
[index.get_position(version)]))[0]
2776
self.assertEqual(original[version], data)
2778
def make_knit_with_two_versions(self):
2779
knit = self.make_test_knit()
2780
knit.add_lines('a', [], ["foo"])
2781
knit.add_lines('b', [], ["bar"])
2784
def test_get_raw_records(self):
2785
knit = self.make_knit_with_two_versions()
2786
self.assertGetRawRecords(knit, ['a', 'b'])
2787
self.assertGetRawRecords(knit, ['a'])
2788
self.assertGetRawRecords(knit, ['b'])
2790
def test_get_raw_record_from_backing_knit(self):
2791
# the thunk layer should create an artificial A on-demand when needed.
2792
source_knit = self.make_test_knit(name='plain', annotate=False)
2793
target_knit = self.make_test_knit(name='annotated', annotate=True)
2794
source_knit.add_lines("A", [], ["Foo\n"])
2795
# Give the target A, so we can try to thunk across to it.
2796
target_knit.join(source_knit)
2797
index, access = self.get_index_access(target_knit,
2798
source_knit.get_data_stream([]))
2799
raw_data = list(access.get_raw_records([(True, "A", None, None)]))[0]
2800
df = GzipFile(mode='rb', fileobj=StringIO(raw_data))
2802
'version A 1 5d36b88bb697a2d778f024048bafabd443d74503\n'
2806
def test_asking_for_thunk_stream_is_not_plain_errors(self):
2807
knit = self.make_test_knit(name='annotated', annotate=True)
2808
knit.add_lines("A", [], ["Foo\n"])
2809
index, access = self.get_index_access(knit,
2810
knit.get_data_stream([]))
2811
self.assertRaises(errors.KnitCorrupt,
2812
list, access.get_raw_records([(True, "A", None, None)]))
2815
class TestFormatSignatures(KnitTests):
2817
def test_knit_format_signatures(self):
2818
"""Different formats of knit have different signature strings."""
2819
knit = self.make_test_knit(name='a', annotate=True)
2820
self.assertEqual('knit-annotated', knit.get_format_signature())
2821
knit = self.make_test_knit(name='p', annotate=False)
2822
self.assertEqual('knit-plain', knit.get_format_signature())