34
30
RevisionNotPresent,
37
from bzrlib.index import *
38
33
from bzrlib.knit import (
44
37
KnitAnnotateFactory,
55
42
from bzrlib.osutils import split_lines
56
from bzrlib.tests import (
59
TestCaseWithMemoryTransport,
60
TestCaseWithTransport,
62
from bzrlib.transport import get_transport
43
from bzrlib.tests import TestCase, TestCaseWithTransport
44
from bzrlib.transport import TransportLogger, get_transport
63
45
from bzrlib.transport.memory import MemoryTransport
64
from bzrlib.tuned_gzip import GzipFile
65
from bzrlib.util import bencode
66
46
from bzrlib.weave import Weave
69
class _CompiledKnitFeature(Feature):
73
import bzrlib._knit_load_data_c
78
def feature_name(self):
79
return 'bzrlib._knit_load_data_c'
81
CompiledKnitFeature = _CompiledKnitFeature()
84
class KnitContentTestsMixin(object):
49
class KnitContentTests(TestCase):
86
51
def test_constructor(self):
87
content = self._make_content([])
52
content = KnitContent([])
89
54
def test_text(self):
90
content = self._make_content([])
55
content = KnitContent([])
91
56
self.assertEqual(content.text(), [])
93
content = self._make_content([("origin1", "text1"), ("origin2", "text2")])
58
content = KnitContent([("origin1", "text1"), ("origin2", "text2")])
94
59
self.assertEqual(content.text(), ["text1", "text2"])
61
def test_annotate(self):
62
content = KnitContent([])
63
self.assertEqual(content.annotate(), [])
65
content = KnitContent([("origin1", "text1"), ("origin2", "text2")])
66
self.assertEqual(content.annotate(),
67
[("origin1", "text1"), ("origin2", "text2")])
69
def test_annotate_iter(self):
70
content = KnitContent([])
71
it = content.annotate_iter()
72
self.assertRaises(StopIteration, it.next)
74
content = KnitContent([("origin1", "text1"), ("origin2", "text2")])
75
it = content.annotate_iter()
76
self.assertEqual(it.next(), ("origin1", "text1"))
77
self.assertEqual(it.next(), ("origin2", "text2"))
78
self.assertRaises(StopIteration, it.next)
96
80
def test_copy(self):
97
content = self._make_content([("origin1", "text1"), ("origin2", "text2")])
81
content = KnitContent([("origin1", "text1"), ("origin2", "text2")])
98
82
copy = content.copy()
99
self.assertIsInstance(copy, content.__class__)
100
self.assertEqual(copy.annotate(), content.annotate())
102
def assertDerivedBlocksEqual(self, source, target, noeol=False):
103
"""Assert that the derived matching blocks match real output"""
104
source_lines = source.splitlines(True)
105
target_lines = target.splitlines(True)
107
if noeol and not line.endswith('\n'):
111
source_content = self._make_content([(None, nl(l)) for l in source_lines])
112
target_content = self._make_content([(None, nl(l)) for l in target_lines])
113
line_delta = source_content.line_delta(target_content)
114
delta_blocks = list(KnitContent.get_line_delta_blocks(line_delta,
115
source_lines, target_lines))
116
matcher = KnitSequenceMatcher(None, source_lines, target_lines)
117
matcher_blocks = list(list(matcher.get_matching_blocks()))
118
self.assertEqual(matcher_blocks, delta_blocks)
120
def test_get_line_delta_blocks(self):
121
self.assertDerivedBlocksEqual('a\nb\nc\n', 'q\nc\n')
122
self.assertDerivedBlocksEqual(TEXT_1, TEXT_1)
123
self.assertDerivedBlocksEqual(TEXT_1, TEXT_1A)
124
self.assertDerivedBlocksEqual(TEXT_1, TEXT_1B)
125
self.assertDerivedBlocksEqual(TEXT_1B, TEXT_1A)
126
self.assertDerivedBlocksEqual(TEXT_1A, TEXT_1B)
127
self.assertDerivedBlocksEqual(TEXT_1A, '')
128
self.assertDerivedBlocksEqual('', TEXT_1A)
129
self.assertDerivedBlocksEqual('', '')
130
self.assertDerivedBlocksEqual('a\nb\nc', 'a\nb\nc\nd')
132
def test_get_line_delta_blocks_noeol(self):
133
"""Handle historical knit deltas safely
135
Some existing knit deltas don't consider the last line to differ
136
when the only difference whether it has a final newline.
138
New knit deltas appear to always consider the last line to differ
141
self.assertDerivedBlocksEqual('a\nb\nc', 'a\nb\nc\nd\n', noeol=True)
142
self.assertDerivedBlocksEqual('a\nb\nc\nd\n', 'a\nb\nc', noeol=True)
143
self.assertDerivedBlocksEqual('a\nb\nc\n', 'a\nb\nc', noeol=True)
144
self.assertDerivedBlocksEqual('a\nb\nc', 'a\nb\nc\n', noeol=True)
147
class TestPlainKnitContent(TestCase, KnitContentTestsMixin):
149
def _make_content(self, lines):
150
annotated_content = AnnotatedKnitContent(lines)
151
return PlainKnitContent(annotated_content.text(), 'bogus')
153
def test_annotate(self):
154
content = self._make_content([])
155
self.assertEqual(content.annotate(), [])
157
content = self._make_content([("origin1", "text1"), ("origin2", "text2")])
158
self.assertEqual(content.annotate(),
159
[("bogus", "text1"), ("bogus", "text2")])
161
def test_annotate_iter(self):
162
content = self._make_content([])
163
it = content.annotate_iter()
164
self.assertRaises(StopIteration, it.next)
166
content = self._make_content([("bogus", "text1"), ("bogus", "text2")])
167
it = content.annotate_iter()
168
self.assertEqual(it.next(), ("bogus", "text1"))
169
self.assertEqual(it.next(), ("bogus", "text2"))
170
self.assertRaises(StopIteration, it.next)
172
def test_line_delta(self):
173
content1 = self._make_content([("", "a"), ("", "b")])
174
content2 = self._make_content([("", "a"), ("", "a"), ("", "c")])
175
self.assertEqual(content1.line_delta(content2),
176
[(1, 2, 2, ["a", "c"])])
178
def test_line_delta_iter(self):
179
content1 = self._make_content([("", "a"), ("", "b")])
180
content2 = self._make_content([("", "a"), ("", "a"), ("", "c")])
181
it = content1.line_delta_iter(content2)
182
self.assertEqual(it.next(), (1, 2, 2, ["a", "c"]))
183
self.assertRaises(StopIteration, it.next)
186
class TestAnnotatedKnitContent(TestCase, KnitContentTestsMixin):
188
def _make_content(self, lines):
189
return AnnotatedKnitContent(lines)
191
def test_annotate(self):
192
content = self._make_content([])
193
self.assertEqual(content.annotate(), [])
195
content = self._make_content([("origin1", "text1"), ("origin2", "text2")])
196
self.assertEqual(content.annotate(),
83
self.assertIsInstance(copy, KnitContent)
84
self.assertEqual(copy.annotate(),
197
85
[("origin1", "text1"), ("origin2", "text2")])
199
def test_annotate_iter(self):
200
content = self._make_content([])
201
it = content.annotate_iter()
202
self.assertRaises(StopIteration, it.next)
204
content = self._make_content([("origin1", "text1"), ("origin2", "text2")])
205
it = content.annotate_iter()
206
self.assertEqual(it.next(), ("origin1", "text1"))
207
self.assertEqual(it.next(), ("origin2", "text2"))
208
self.assertRaises(StopIteration, it.next)
210
87
def test_line_delta(self):
211
content1 = self._make_content([("", "a"), ("", "b")])
212
content2 = self._make_content([("", "a"), ("", "a"), ("", "c")])
88
content1 = KnitContent([("", "a"), ("", "b")])
89
content2 = KnitContent([("", "a"), ("", "a"), ("", "c")])
213
90
self.assertEqual(content1.line_delta(content2),
214
91
[(1, 2, 2, [("", "a"), ("", "c")])])
216
93
def test_line_delta_iter(self):
217
content1 = self._make_content([("", "a"), ("", "b")])
218
content2 = self._make_content([("", "a"), ("", "a"), ("", "c")])
94
content1 = KnitContent([("", "a"), ("", "b")])
95
content2 = KnitContent([("", "a"), ("", "a"), ("", "c")])
219
96
it = content1.line_delta_iter(content2)
220
97
self.assertEqual(it.next(), (1, 2, 2, [("", "a"), ("", "c")]))
221
98
self.assertRaises(StopIteration, it.next)
1562
1078
for plan_line, expected_line in zip(plan, AB_MERGE):
1563
1079
self.assertEqual(plan_line, expected_line)
1565
def test_get_stream_empty(self):
1566
"""Get a data stream for an empty knit file."""
1567
k1 = self.make_test_knit()
1568
format, data_list, reader_callable = k1.get_data_stream([])
1569
self.assertEqual('knit-plain', format)
1570
self.assertEqual([], data_list)
1571
content = reader_callable(None)
1572
self.assertEqual('', content)
1573
self.assertIsInstance(content, str)
1575
def test_get_stream_one_version(self):
1576
"""Get a data stream for a single record out of a knit containing just
1579
k1 = self.make_test_knit()
1581
('text-a', [], TEXT_1),
1583
expected_data_list = [
1584
# version, options, length, parents
1585
('text-a', ['fulltext'], 122, ()),
1587
for version_id, parents, lines in test_data:
1588
k1.add_lines(version_id, parents, split_lines(lines))
1590
format, data_list, reader_callable = k1.get_data_stream(['text-a'])
1591
self.assertEqual('knit-plain', format)
1592
self.assertEqual(expected_data_list, data_list)
1593
# There's only one record in the knit, so the content should be the
1594
# entire knit data file's contents.
1595
self.assertEqual(k1.transport.get_bytes(k1._data._access._filename),
1596
reader_callable(None))
1598
def test_get_stream_get_one_version_of_many(self):
1599
"""Get a data stream for just one version out of a knit containing many
1602
k1 = self.make_test_knit()
1603
# Insert the same data as test_knit_join, as they seem to cover a range
1604
# of cases (no parents, one parent, multiple parents).
1606
('text-a', [], TEXT_1),
1607
('text-b', ['text-a'], TEXT_1),
1608
('text-c', [], TEXT_1),
1609
('text-d', ['text-c'], TEXT_1),
1610
('text-m', ['text-b', 'text-d'], TEXT_1),
1612
expected_data_list = [
1613
# version, options, length, parents
1614
('text-m', ['line-delta'], 84, ('text-b', 'text-d')),
1616
for version_id, parents, lines in test_data:
1617
k1.add_lines(version_id, parents, split_lines(lines))
1619
format, data_list, reader_callable = k1.get_data_stream(['text-m'])
1620
self.assertEqual('knit-plain', format)
1621
self.assertEqual(expected_data_list, data_list)
1622
self.assertRecordContentEqual(k1, 'text-m', reader_callable(None))
1624
def test_get_data_stream_unordered_index(self):
1625
"""Get a data stream when the knit index reports versions out of order.
1627
https://bugs.launchpad.net/bzr/+bug/164637
1629
k1 = self.make_test_knit()
1631
('text-a', [], TEXT_1),
1632
('text-b', ['text-a'], TEXT_1),
1633
('text-c', [], TEXT_1),
1634
('text-d', ['text-c'], TEXT_1),
1635
('text-m', ['text-b', 'text-d'], TEXT_1),
1637
for version_id, parents, lines in test_data:
1638
k1.add_lines(version_id, parents, split_lines(lines))
1639
# monkey-patch versions method to return out of order, as if coming
1640
# from multiple independently indexed packs
1641
original_versions = k1.versions
1642
k1.versions = lambda: reversed(original_versions())
1643
expected_data_list = [
1644
('text-a', ['fulltext'], 122, ()),
1645
('text-b', ['line-delta'], 84, ('text-a',))]
1646
# now check the fulltext is first and the delta second
1647
format, data_list, _ = k1.get_data_stream(['text-a', 'text-b'])
1648
self.assertEqual('knit-plain', format)
1649
self.assertEqual(expected_data_list, data_list)
1650
# and that's true if we ask for them in the opposite order too
1651
format, data_list, _ = k1.get_data_stream(['text-b', 'text-a'])
1652
self.assertEqual(expected_data_list, data_list)
1653
# also try requesting more versions
1654
format, data_list, _ = k1.get_data_stream([
1655
'text-m', 'text-b', 'text-a'])
1657
('text-a', ['fulltext'], 122, ()),
1658
('text-b', ['line-delta'], 84, ('text-a',)),
1659
('text-m', ['line-delta'], 84, ('text-b', 'text-d')),
1662
def test_get_stream_ghost_parent(self):
1663
"""Get a data stream for a version with a ghost parent."""
1664
k1 = self.make_test_knit()
1666
k1.add_lines('text-a', [], split_lines(TEXT_1))
1667
k1.add_lines_with_ghosts('text-b', ['text-a', 'text-ghost'],
1668
split_lines(TEXT_1))
1670
expected_data_list = [
1671
# version, options, length, parents
1672
('text-b', ['line-delta'], 84, ('text-a', 'text-ghost')),
1675
format, data_list, reader_callable = k1.get_data_stream(['text-b'])
1676
self.assertEqual('knit-plain', format)
1677
self.assertEqual(expected_data_list, data_list)
1678
self.assertRecordContentEqual(k1, 'text-b', reader_callable(None))
1680
def test_get_stream_get_multiple_records(self):
1681
"""Get a stream for multiple records of a knit."""
1682
k1 = self.make_test_knit()
1683
# Insert the same data as test_knit_join, as they seem to cover a range
1684
# of cases (no parents, one parent, multiple parents).
1686
('text-a', [], TEXT_1),
1687
('text-b', ['text-a'], TEXT_1),
1688
('text-c', [], TEXT_1),
1689
('text-d', ['text-c'], TEXT_1),
1690
('text-m', ['text-b', 'text-d'], TEXT_1),
1692
for version_id, parents, lines in test_data:
1693
k1.add_lines(version_id, parents, split_lines(lines))
1695
# This test is actually a bit strict as the order in which they're
1696
# returned is not defined. This matches the current (deterministic)
1698
expected_data_list = [
1699
# version, options, length, parents
1700
('text-d', ['line-delta'], 84, ('text-c',)),
1701
('text-b', ['line-delta'], 84, ('text-a',)),
1703
# Note that even though we request the revision IDs in a particular
1704
# order, the data stream may return them in any order it likes. In this
1705
# case, they'll be in the order they were inserted into the knit.
1706
format, data_list, reader_callable = k1.get_data_stream(
1707
['text-d', 'text-b'])
1708
self.assertEqual('knit-plain', format)
1709
self.assertEqual(expected_data_list, data_list)
1710
# must match order they're returned
1711
self.assertRecordContentEqual(k1, 'text-d', reader_callable(84))
1712
self.assertRecordContentEqual(k1, 'text-b', reader_callable(84))
1713
self.assertEqual('', reader_callable(None),
1714
"There should be no more bytes left to read.")
1716
def test_get_stream_all(self):
1717
"""Get a data stream for all the records in a knit.
1719
This exercises fulltext records, line-delta records, records with
1720
various numbers of parents, and reading multiple records out of the
1721
callable. These cases ought to all be exercised individually by the
1722
other test_get_stream_* tests; this test is basically just paranoia.
1724
k1 = self.make_test_knit()
1725
# Insert the same data as test_knit_join, as they seem to cover a range
1726
# of cases (no parents, one parent, multiple parents).
1728
('text-a', [], TEXT_1),
1729
('text-b', ['text-a'], TEXT_1),
1730
('text-c', [], TEXT_1),
1731
('text-d', ['text-c'], TEXT_1),
1732
('text-m', ['text-b', 'text-d'], TEXT_1),
1734
for version_id, parents, lines in test_data:
1735
k1.add_lines(version_id, parents, split_lines(lines))
1737
# This test is actually a bit strict as the order in which they're
1738
# returned is not defined. This matches the current (deterministic)
1740
expected_data_list = [
1741
# version, options, length, parents
1742
('text-a', ['fulltext'], 122, ()),
1743
('text-b', ['line-delta'], 84, ('text-a',)),
1744
('text-m', ['line-delta'], 84, ('text-b', 'text-d')),
1745
('text-c', ['fulltext'], 121, ()),
1746
('text-d', ['line-delta'], 84, ('text-c',)),
1748
format, data_list, reader_callable = k1.get_data_stream(
1749
['text-a', 'text-b', 'text-c', 'text-d', 'text-m'])
1750
self.assertEqual('knit-plain', format)
1751
self.assertEqual(expected_data_list, data_list)
1752
for version_id, options, length, parents in expected_data_list:
1753
bytes = reader_callable(length)
1754
self.assertRecordContentEqual(k1, version_id, bytes)
1756
def assertKnitFilesEqual(self, knit1, knit2):
1757
"""Assert that the contents of the index and data files of two knits are
1761
knit1.transport.get_bytes(knit1._data._access._filename),
1762
knit2.transport.get_bytes(knit2._data._access._filename))
1764
knit1.transport.get_bytes(knit1._index._filename),
1765
knit2.transport.get_bytes(knit2._index._filename))
1767
def assertKnitValuesEqual(self, left, right):
1768
"""Assert that the texts, annotations and graph of left and right are
1771
self.assertEqual(set(left.versions()), set(right.versions()))
1772
for version in left.versions():
1773
self.assertEqual(left.get_parents_with_ghosts(version),
1774
right.get_parents_with_ghosts(version))
1775
self.assertEqual(left.get_lines(version),
1776
right.get_lines(version))
1777
self.assertEqual(left.annotate(version),
1778
right.annotate(version))
1780
def test_insert_data_stream_empty(self):
1781
"""Inserting a data stream with no records should not put any data into
1784
k1 = self.make_test_knit()
1785
k1.insert_data_stream(
1786
(k1.get_format_signature(), [], lambda ignored: ''))
1787
self.assertEqual('', k1.transport.get_bytes(k1._data._access._filename),
1788
"The .knit should be completely empty.")
1789
self.assertEqual(k1._index.HEADER,
1790
k1.transport.get_bytes(k1._index._filename),
1791
"The .kndx should have nothing apart from the header.")
1793
def test_insert_data_stream_one_record(self):
1794
"""Inserting a data stream with one record from a knit with one record
1795
results in byte-identical files.
1797
source = self.make_test_knit(name='source')
1798
source.add_lines('text-a', [], split_lines(TEXT_1))
1799
data_stream = source.get_data_stream(['text-a'])
1800
target = self.make_test_knit(name='target')
1801
target.insert_data_stream(data_stream)
1802
self.assertKnitFilesEqual(source, target)
1804
def test_insert_data_stream_annotated_unannotated(self):
1805
"""Inserting an annotated datastream to an unannotated knit works."""
1806
# case one - full texts.
1807
source = self.make_test_knit(name='source', annotate=True)
1808
target = self.make_test_knit(name='target', annotate=False)
1809
source.add_lines('text-a', [], split_lines(TEXT_1))
1810
target.insert_data_stream(source.get_data_stream(['text-a']))
1811
self.assertKnitValuesEqual(source, target)
1812
# case two - deltas.
1813
source.add_lines('text-b', ['text-a'], split_lines(TEXT_2))
1814
target.insert_data_stream(source.get_data_stream(['text-b']))
1815
self.assertKnitValuesEqual(source, target)
1817
def test_insert_data_stream_unannotated_annotated(self):
1818
"""Inserting an unannotated datastream to an annotated knit works."""
1819
# case one - full texts.
1820
source = self.make_test_knit(name='source', annotate=False)
1821
target = self.make_test_knit(name='target', annotate=True)
1822
source.add_lines('text-a', [], split_lines(TEXT_1))
1823
target.insert_data_stream(source.get_data_stream(['text-a']))
1824
self.assertKnitValuesEqual(source, target)
1825
# case two - deltas.
1826
source.add_lines('text-b', ['text-a'], split_lines(TEXT_2))
1827
target.insert_data_stream(source.get_data_stream(['text-b']))
1828
self.assertKnitValuesEqual(source, target)
1830
def test_insert_data_stream_records_already_present(self):
1831
"""Insert a data stream where some records are alreday present in the
1832
target, and some not. Only the new records are inserted.
1834
source = self.make_test_knit(name='source')
1835
target = self.make_test_knit(name='target')
1836
# Insert 'text-a' into both source and target
1837
source.add_lines('text-a', [], split_lines(TEXT_1))
1838
target.insert_data_stream(source.get_data_stream(['text-a']))
1839
# Insert 'text-b' into just the source.
1840
source.add_lines('text-b', ['text-a'], split_lines(TEXT_1))
1841
# Get a data stream of both text-a and text-b, and insert it.
1842
data_stream = source.get_data_stream(['text-a', 'text-b'])
1843
target.insert_data_stream(data_stream)
1844
# The source and target will now be identical. This means the text-a
1845
# record was not added a second time.
1846
self.assertKnitFilesEqual(source, target)
1848
def test_insert_data_stream_multiple_records(self):
1849
"""Inserting a data stream of all records from a knit with multiple
1850
records results in byte-identical files.
1852
source = self.make_test_knit(name='source')
1853
source.add_lines('text-a', [], split_lines(TEXT_1))
1854
source.add_lines('text-b', ['text-a'], split_lines(TEXT_1))
1855
source.add_lines('text-c', [], split_lines(TEXT_1))
1856
data_stream = source.get_data_stream(['text-a', 'text-b', 'text-c'])
1858
target = self.make_test_knit(name='target')
1859
target.insert_data_stream(data_stream)
1861
self.assertKnitFilesEqual(source, target)
1863
def test_insert_data_stream_ghost_parent(self):
1864
"""Insert a data stream with a record that has a ghost parent."""
1865
# Make a knit with a record, text-a, that has a ghost parent.
1866
source = self.make_test_knit(name='source')
1867
source.add_lines_with_ghosts('text-a', ['text-ghost'],
1868
split_lines(TEXT_1))
1869
data_stream = source.get_data_stream(['text-a'])
1871
target = self.make_test_knit(name='target')
1872
target.insert_data_stream(data_stream)
1874
self.assertKnitFilesEqual(source, target)
1876
# The target knit object is in a consistent state, i.e. the record we
1877
# just added is immediately visible.
1878
self.assertTrue(target.has_version('text-a'))
1879
self.assertFalse(target.has_version('text-ghost'))
1880
self.assertEqual({'text-a':('text-ghost',)},
1881
target.get_parent_map(['text-a', 'text-ghost']))
1882
self.assertEqual(split_lines(TEXT_1), target.get_lines('text-a'))
1884
def test_insert_data_stream_inconsistent_version_lines(self):
1885
"""Inserting a data stream which has different content for a version_id
1886
than already exists in the knit will raise KnitCorrupt.
1888
source = self.make_test_knit(name='source')
1889
target = self.make_test_knit(name='target')
1890
# Insert a different 'text-a' into both source and target
1891
source.add_lines('text-a', [], split_lines(TEXT_1))
1892
target.add_lines('text-a', [], split_lines(TEXT_2))
1893
# Insert a data stream with conflicting content into the target
1894
data_stream = source.get_data_stream(['text-a'])
1896
errors.KnitCorrupt, target.insert_data_stream, data_stream)
1898
def test_insert_data_stream_inconsistent_version_parents(self):
1899
"""Inserting a data stream which has different parents for a version_id
1900
than already exists in the knit will raise KnitCorrupt.
1902
source = self.make_test_knit(name='source')
1903
target = self.make_test_knit(name='target')
1904
# Insert a different 'text-a' into both source and target. They differ
1905
# only by the parents list, the content is the same.
1906
source.add_lines_with_ghosts('text-a', [], split_lines(TEXT_1))
1907
target.add_lines_with_ghosts('text-a', ['a-ghost'], split_lines(TEXT_1))
1908
# Insert a data stream with conflicting content into the target
1909
data_stream = source.get_data_stream(['text-a'])
1911
errors.KnitCorrupt, target.insert_data_stream, data_stream)
1913
def test_insert_data_stream_unknown_format(self):
1914
"""A data stream in a different format to the target knit cannot be
1917
It will raise KnitDataStreamUnknown because the fallback code will fail
1918
to make a knit. In future we may need KnitDataStreamIncompatible again,
1919
for more exotic cases.
1921
data_stream = ('fake-format-signature', [], lambda _: '')
1922
target = self.make_test_knit(name='target')
1924
errors.KnitDataStreamUnknown,
1925
target.insert_data_stream, data_stream)
1927
def test_insert_data_stream_bug_208418(self):
1928
"""You can insert a stream with an incompatible format, even when:
1929
* the stream has a line-delta record,
1930
* whose parent is in the target, also stored as a line-delta
1932
See <https://launchpad.net/bugs/208418>.
1934
base_lines = split_lines(TEXT_1)
1936
target = self.make_test_knit(name='target', annotate=True)
1937
target.add_lines('version-1', [], base_lines)
1938
target.add_lines('version-2', ['version-1'], base_lines + ['a\n'])
1939
# The second record should be a delta.
1940
self.assertEqual('line-delta', target._index.get_method('version-2'))
1942
# Make a source, with a different format, but the same data
1943
source = self.make_test_knit(name='source', annotate=False)
1944
source.add_lines('version-1', [], base_lines)
1945
source.add_lines('version-2', ['version-1'], base_lines + ['a\n'])
1946
# Now add another record, which should be stored as a delta against
1948
source.add_lines('version-3', ['version-2'], base_lines + ['b\n'])
1949
self.assertEqual('line-delta', source._index.get_method('version-3'))
1951
# Make a stream of the new version
1952
data_stream = source.get_data_stream(['version-3'])
1953
# And insert into the target
1954
target.insert_data_stream(data_stream)
1955
# No errors should have been raised.
1958
# * test that a stream of "already present version, then new version"
1959
# inserts correctly.
1962
def assertMadeStreamKnit(self, source_knit, versions, target_knit):
1963
"""Assert that a knit made from a stream is as expected."""
1964
a_stream = source_knit.get_data_stream(versions)
1965
expected_data = a_stream[2](None)
1966
a_stream = source_knit.get_data_stream(versions)
1967
a_knit = target_knit._knit_from_datastream(a_stream)
1968
self.assertEqual(source_knit.factory.__class__,
1969
a_knit.factory.__class__)
1970
self.assertIsInstance(a_knit._data._access, _StreamAccess)
1971
self.assertIsInstance(a_knit._index, _StreamIndex)
1972
self.assertEqual(a_knit._index.data_list, a_stream[1])
1973
self.assertEqual(a_knit._data._access.data, expected_data)
1974
self.assertEqual(a_knit.filename, target_knit.filename)
1975
self.assertEqual(a_knit.transport, target_knit.transport)
1976
self.assertEqual(a_knit._index, a_knit._data._access.stream_index)
1977
self.assertEqual(target_knit, a_knit._data._access.backing_knit)
1978
self.assertIsInstance(a_knit._data._access.orig_factory,
1979
source_knit.factory.__class__)
1981
def test__knit_from_data_stream_empty(self):
1982
"""Create a knit object from a datastream."""
1983
annotated = self.make_test_knit(name='source', annotate=True)
1984
plain = self.make_test_knit(name='target', annotate=False)
1985
# case 1: annotated source
1986
self.assertMadeStreamKnit(annotated, [], annotated)
1987
self.assertMadeStreamKnit(annotated, [], plain)
1988
# case 2: plain source
1989
self.assertMadeStreamKnit(plain, [], annotated)
1990
self.assertMadeStreamKnit(plain, [], plain)
1992
def test__knit_from_data_stream_unknown_format(self):
1993
annotated = self.make_test_knit(name='source', annotate=True)
1994
self.assertRaises(errors.KnitDataStreamUnknown,
1995
annotated._knit_from_datastream, ("unknown", None, None))
1999
1083
Banana cup cakes:
2254
1349
t.put_bytes('test.kndx', '# not really a knit header\n\n')
2256
1351
self.assertRaises(KnitHeaderError, self.make_test_knit)
2259
class TestGraphIndexKnit(KnitTests):
2260
"""Tests for knits using a GraphIndex rather than a KnitIndex."""
2262
def make_g_index(self, name, ref_lists=0, nodes=[]):
2263
builder = GraphIndexBuilder(ref_lists)
2264
for node, references, value in nodes:
2265
builder.add_node(node, references, value)
2266
stream = builder.finish()
2267
trans = self.get_transport()
2268
size = trans.put_file(name, stream)
2269
return GraphIndex(trans, name, size)
2271
def two_graph_index(self, deltas=False, catch_adds=False):
2272
"""Build a two-graph index.
2274
:param deltas: If true, use underlying indices with two node-ref
2275
lists and 'parent' set to a delta-compressed against tail.
2277
# build a complex graph across several indices.
2279
# delta compression inn the index
2280
index1 = self.make_g_index('1', 2, [
2281
(('tip', ), 'N0 100', ([('parent', )], [], )),
2282
(('tail', ), '', ([], []))])
2283
index2 = self.make_g_index('2', 2, [
2284
(('parent', ), ' 100 78', ([('tail', ), ('ghost', )], [('tail', )])),
2285
(('separate', ), '', ([], []))])
2287
# just blob location and graph in the index.
2288
index1 = self.make_g_index('1', 1, [
2289
(('tip', ), 'N0 100', ([('parent', )], )),
2290
(('tail', ), '', ([], ))])
2291
index2 = self.make_g_index('2', 1, [
2292
(('parent', ), ' 100 78', ([('tail', ), ('ghost', )], )),
2293
(('separate', ), '', ([], ))])
2294
combined_index = CombinedGraphIndex([index1, index2])
2296
self.combined_index = combined_index
2297
self.caught_entries = []
2298
add_callback = self.catch_add
2301
return KnitGraphIndex(combined_index, deltas=deltas,
2302
add_callback=add_callback)
2304
def test_get_ancestry(self):
2305
# get_ancestry is defined as eliding ghosts, not erroring.
2306
index = self.two_graph_index()
2307
self.assertEqual([], index.get_ancestry([]))
2308
self.assertEqual(['separate'], index.get_ancestry(['separate']))
2309
self.assertEqual(['tail'], index.get_ancestry(['tail']))
2310
self.assertEqual(['tail', 'parent'], index.get_ancestry(['parent']))
2311
self.assertEqual(['tail', 'parent', 'tip'], index.get_ancestry(['tip']))
2312
self.assertTrue(index.get_ancestry(['tip', 'separate']) in
2313
(['tail', 'parent', 'tip', 'separate'],
2314
['separate', 'tail', 'parent', 'tip'],
2316
# and without topo_sort
2317
self.assertEqual(set(['separate']),
2318
set(index.get_ancestry(['separate'], topo_sorted=False)))
2319
self.assertEqual(set(['tail']),
2320
set(index.get_ancestry(['tail'], topo_sorted=False)))
2321
self.assertEqual(set(['tail', 'parent']),
2322
set(index.get_ancestry(['parent'], topo_sorted=False)))
2323
self.assertEqual(set(['tail', 'parent', 'tip']),
2324
set(index.get_ancestry(['tip'], topo_sorted=False)))
2325
self.assertEqual(set(['separate', 'tail', 'parent', 'tip']),
2326
set(index.get_ancestry(['tip', 'separate'])))
2327
# asking for a ghost makes it go boom.
2328
self.assertRaises(errors.RevisionNotPresent, index.get_ancestry, ['ghost'])
2330
def test_get_ancestry_with_ghosts(self):
2331
index = self.two_graph_index()
2332
self.assertEqual([], index.get_ancestry_with_ghosts([]))
2333
self.assertEqual(['separate'], index.get_ancestry_with_ghosts(['separate']))
2334
self.assertEqual(['tail'], index.get_ancestry_with_ghosts(['tail']))
2335
self.assertTrue(index.get_ancestry_with_ghosts(['parent']) in
2336
(['tail', 'ghost', 'parent'],
2337
['ghost', 'tail', 'parent'],
2339
self.assertTrue(index.get_ancestry_with_ghosts(['tip']) in
2340
(['tail', 'ghost', 'parent', 'tip'],
2341
['ghost', 'tail', 'parent', 'tip'],
2343
self.assertTrue(index.get_ancestry_with_ghosts(['tip', 'separate']) in
2344
(['tail', 'ghost', 'parent', 'tip', 'separate'],
2345
['ghost', 'tail', 'parent', 'tip', 'separate'],
2346
['separate', 'tail', 'ghost', 'parent', 'tip'],
2347
['separate', 'ghost', 'tail', 'parent', 'tip'],
2349
# asking for a ghost makes it go boom.
2350
self.assertRaises(errors.RevisionNotPresent, index.get_ancestry_with_ghosts, ['ghost'])
2352
def test_num_versions(self):
2353
index = self.two_graph_index()
2354
self.assertEqual(4, index.num_versions())
2356
def test_get_versions(self):
2357
index = self.two_graph_index()
2358
self.assertEqual(set(['tail', 'tip', 'parent', 'separate']),
2359
set(index.get_versions()))
2361
def test_has_version(self):
2362
index = self.two_graph_index()
2363
self.assertTrue(index.has_version('tail'))
2364
self.assertFalse(index.has_version('ghost'))
2366
def test_get_position(self):
2367
index = self.two_graph_index()
2368
self.assertEqual((index._graph_index._indices[0], 0, 100), index.get_position('tip'))
2369
self.assertEqual((index._graph_index._indices[1], 100, 78), index.get_position('parent'))
2371
def test_get_method_deltas(self):
2372
index = self.two_graph_index(deltas=True)
2373
self.assertEqual('fulltext', index.get_method('tip'))
2374
self.assertEqual('line-delta', index.get_method('parent'))
2376
def test_get_method_no_deltas(self):
2377
# check that the parent-history lookup is ignored with deltas=False.
2378
index = self.two_graph_index(deltas=False)
2379
self.assertEqual('fulltext', index.get_method('tip'))
2380
self.assertEqual('fulltext', index.get_method('parent'))
2382
def test_get_options_deltas(self):
2383
index = self.two_graph_index(deltas=True)
2384
self.assertEqual(['fulltext', 'no-eol'], index.get_options('tip'))
2385
self.assertEqual(['line-delta'], index.get_options('parent'))
2387
def test_get_options_no_deltas(self):
2388
# check that the parent-history lookup is ignored with deltas=False.
2389
index = self.two_graph_index(deltas=False)
2390
self.assertEqual(['fulltext', 'no-eol'], index.get_options('tip'))
2391
self.assertEqual(['fulltext'], index.get_options('parent'))
2393
def test_get_parents_with_ghosts(self):
2394
index = self.two_graph_index()
2395
self.assertEqual(('tail', 'ghost'), index.get_parents_with_ghosts('parent'))
2396
# and errors on ghosts.
2397
self.assertRaises(errors.RevisionNotPresent,
2398
index.get_parents_with_ghosts, 'ghost')
2400
def test_check_versions_present(self):
2401
# ghosts should not be considered present
2402
index = self.two_graph_index()
2403
self.assertRaises(RevisionNotPresent, index.check_versions_present,
2405
self.assertRaises(RevisionNotPresent, index.check_versions_present,
2407
index.check_versions_present(['tail', 'separate'])
2409
def catch_add(self, entries):
2410
self.caught_entries.append(entries)
2412
def test_add_no_callback_errors(self):
2413
index = self.two_graph_index()
2414
self.assertRaises(errors.ReadOnlyError, index.add_version,
2415
'new', 'fulltext,no-eol', (None, 50, 60), ['separate'])
2417
def test_add_version_smoke(self):
2418
index = self.two_graph_index(catch_adds=True)
2419
index.add_version('new', 'fulltext,no-eol', (None, 50, 60), ['separate'])
2420
self.assertEqual([[(('new', ), 'N50 60', ((('separate',),),))]],
2421
self.caught_entries)
2423
def test_add_version_delta_not_delta_index(self):
2424
index = self.two_graph_index(catch_adds=True)
2425
self.assertRaises(errors.KnitCorrupt, index.add_version,
2426
'new', 'no-eol,line-delta', (None, 0, 100), ['parent'])
2427
self.assertEqual([], self.caught_entries)
2429
def test_add_version_same_dup(self):
2430
index = self.two_graph_index(catch_adds=True)
2431
# options can be spelt two different ways
2432
index.add_version('tip', 'fulltext,no-eol', (None, 0, 100), ['parent'])
2433
index.add_version('tip', 'no-eol,fulltext', (None, 0, 100), ['parent'])
2434
# but neither should have added data.
2435
self.assertEqual([[], []], self.caught_entries)
2437
def test_add_version_different_dup(self):
2438
index = self.two_graph_index(deltas=True, catch_adds=True)
2440
self.assertRaises(errors.KnitCorrupt, index.add_version,
2441
'tip', 'no-eol,line-delta', (None, 0, 100), ['parent'])
2442
self.assertRaises(errors.KnitCorrupt, index.add_version,
2443
'tip', 'line-delta,no-eol', (None, 0, 100), ['parent'])
2444
self.assertRaises(errors.KnitCorrupt, index.add_version,
2445
'tip', 'fulltext', (None, 0, 100), ['parent'])
2447
self.assertRaises(errors.KnitCorrupt, index.add_version,
2448
'tip', 'fulltext,no-eol', (None, 50, 100), ['parent'])
2449
self.assertRaises(errors.KnitCorrupt, index.add_version,
2450
'tip', 'fulltext,no-eol', (None, 0, 1000), ['parent'])
2452
self.assertRaises(errors.KnitCorrupt, index.add_version,
2453
'tip', 'fulltext,no-eol', (None, 0, 100), [])
2454
self.assertEqual([], self.caught_entries)
2456
def test_add_versions_nodeltas(self):
2457
index = self.two_graph_index(catch_adds=True)
2458
index.add_versions([
2459
('new', 'fulltext,no-eol', (None, 50, 60), ['separate']),
2460
('new2', 'fulltext', (None, 0, 6), ['new']),
2462
self.assertEqual([(('new', ), 'N50 60', ((('separate',),),)),
2463
(('new2', ), ' 0 6', ((('new',),),))],
2464
sorted(self.caught_entries[0]))
2465
self.assertEqual(1, len(self.caught_entries))
2467
def test_add_versions_deltas(self):
2468
index = self.two_graph_index(deltas=True, catch_adds=True)
2469
index.add_versions([
2470
('new', 'fulltext,no-eol', (None, 50, 60), ['separate']),
2471
('new2', 'line-delta', (None, 0, 6), ['new']),
2473
self.assertEqual([(('new', ), 'N50 60', ((('separate',),), ())),
2474
(('new2', ), ' 0 6', ((('new',),), (('new',),), ))],
2475
sorted(self.caught_entries[0]))
2476
self.assertEqual(1, len(self.caught_entries))
2478
def test_add_versions_delta_not_delta_index(self):
2479
index = self.two_graph_index(catch_adds=True)
2480
self.assertRaises(errors.KnitCorrupt, index.add_versions,
2481
[('new', 'no-eol,line-delta', (None, 0, 100), ['parent'])])
2482
self.assertEqual([], self.caught_entries)
2484
def test_add_versions_random_id_accepted(self):
2485
index = self.two_graph_index(catch_adds=True)
2486
index.add_versions([], random_id=True)
2488
def test_add_versions_same_dup(self):
2489
index = self.two_graph_index(catch_adds=True)
2490
# options can be spelt two different ways
2491
index.add_versions([('tip', 'fulltext,no-eol', (None, 0, 100), ['parent'])])
2492
index.add_versions([('tip', 'no-eol,fulltext', (None, 0, 100), ['parent'])])
2493
# but neither should have added data.
2494
self.assertEqual([[], []], self.caught_entries)
2496
def test_add_versions_different_dup(self):
2497
index = self.two_graph_index(deltas=True, catch_adds=True)
2499
self.assertRaises(errors.KnitCorrupt, index.add_versions,
2500
[('tip', 'no-eol,line-delta', (None, 0, 100), ['parent'])])
2501
self.assertRaises(errors.KnitCorrupt, index.add_versions,
2502
[('tip', 'line-delta,no-eol', (None, 0, 100), ['parent'])])
2503
self.assertRaises(errors.KnitCorrupt, index.add_versions,
2504
[('tip', 'fulltext', (None, 0, 100), ['parent'])])
2506
self.assertRaises(errors.KnitCorrupt, index.add_versions,
2507
[('tip', 'fulltext,no-eol', (None, 50, 100), ['parent'])])
2508
self.assertRaises(errors.KnitCorrupt, index.add_versions,
2509
[('tip', 'fulltext,no-eol', (None, 0, 1000), ['parent'])])
2511
self.assertRaises(errors.KnitCorrupt, index.add_versions,
2512
[('tip', 'fulltext,no-eol', (None, 0, 100), [])])
2513
# change options in the second record
2514
self.assertRaises(errors.KnitCorrupt, index.add_versions,
2515
[('tip', 'fulltext,no-eol', (None, 0, 100), ['parent']),
2516
('tip', 'no-eol,line-delta', (None, 0, 100), ['parent'])])
2517
self.assertEqual([], self.caught_entries)
2519
def test_iter_parents(self):
2520
index1 = self.make_g_index('1', 1, [
2522
(('r0', ), 'N0 100', ([], )),
2524
(('r1', ), '', ([('r0', )], ))])
2525
index2 = self.make_g_index('2', 1, [
2527
(('r2', ), 'N0 100', ([('r1', ), ('r0', )], )),
2529
combined_index = CombinedGraphIndex([index1, index2])
2530
index = KnitGraphIndex(combined_index)
2532
# cases: each sample data individually:
2533
self.assertEqual(set([('r0', ())]),
2534
set(index.iter_parents(['r0'])))
2535
self.assertEqual(set([('r1', ('r0', ))]),
2536
set(index.iter_parents(['r1'])))
2537
self.assertEqual(set([('r2', ('r1', 'r0'))]),
2538
set(index.iter_parents(['r2'])))
2539
# no nodes returned for a missing node
2540
self.assertEqual(set(),
2541
set(index.iter_parents(['missing'])))
2542
# 1 node returned with missing nodes skipped
2543
self.assertEqual(set([('r1', ('r0', ))]),
2544
set(index.iter_parents(['ghost1', 'r1', 'ghost'])))
2546
self.assertEqual(set([('r0', ()), ('r1', ('r0', ))]),
2547
set(index.iter_parents(['r0', 'r1'])))
2548
# 2 nodes returned, missing skipped
2549
self.assertEqual(set([('r0', ()), ('r1', ('r0', ))]),
2550
set(index.iter_parents(['a', 'r0', 'b', 'r1', 'c'])))
2553
class TestNoParentsGraphIndexKnit(KnitTests):
2554
"""Tests for knits using KnitGraphIndex with no parents."""
2556
def make_g_index(self, name, ref_lists=0, nodes=[]):
2557
builder = GraphIndexBuilder(ref_lists)
2558
for node, references in nodes:
2559
builder.add_node(node, references)
2560
stream = builder.finish()
2561
trans = self.get_transport()
2562
size = trans.put_file(name, stream)
2563
return GraphIndex(trans, name, size)
2565
def test_parents_deltas_incompatible(self):
2566
index = CombinedGraphIndex([])
2567
self.assertRaises(errors.KnitError, KnitGraphIndex, index,
2568
deltas=True, parents=False)
2570
def two_graph_index(self, catch_adds=False):
2571
"""Build a two-graph index.
2573
:param deltas: If true, use underlying indices with two node-ref
2574
lists and 'parent' set to a delta-compressed against tail.
2576
# put several versions in the index.
2577
index1 = self.make_g_index('1', 0, [
2578
(('tip', ), 'N0 100'),
2580
index2 = self.make_g_index('2', 0, [
2581
(('parent', ), ' 100 78'),
2582
(('separate', ), '')])
2583
combined_index = CombinedGraphIndex([index1, index2])
2585
self.combined_index = combined_index
2586
self.caught_entries = []
2587
add_callback = self.catch_add
2590
return KnitGraphIndex(combined_index, parents=False,
2591
add_callback=add_callback)
2593
def test_get_ancestry(self):
2594
# with no parents, ancestry is always just the key.
2595
index = self.two_graph_index()
2596
self.assertEqual([], index.get_ancestry([]))
2597
self.assertEqual(['separate'], index.get_ancestry(['separate']))
2598
self.assertEqual(['tail'], index.get_ancestry(['tail']))
2599
self.assertEqual(['parent'], index.get_ancestry(['parent']))
2600
self.assertEqual(['tip'], index.get_ancestry(['tip']))
2601
self.assertTrue(index.get_ancestry(['tip', 'separate']) in
2602
(['tip', 'separate'],
2603
['separate', 'tip'],
2605
# asking for a ghost makes it go boom.
2606
self.assertRaises(errors.RevisionNotPresent, index.get_ancestry, ['ghost'])
2608
def test_get_ancestry_with_ghosts(self):
2609
index = self.two_graph_index()
2610
self.assertEqual([], index.get_ancestry_with_ghosts([]))
2611
self.assertEqual(['separate'], index.get_ancestry_with_ghosts(['separate']))
2612
self.assertEqual(['tail'], index.get_ancestry_with_ghosts(['tail']))
2613
self.assertEqual(['parent'], index.get_ancestry_with_ghosts(['parent']))
2614
self.assertEqual(['tip'], index.get_ancestry_with_ghosts(['tip']))
2615
self.assertTrue(index.get_ancestry_with_ghosts(['tip', 'separate']) in
2616
(['tip', 'separate'],
2617
['separate', 'tip'],
2619
# asking for a ghost makes it go boom.
2620
self.assertRaises(errors.RevisionNotPresent, index.get_ancestry_with_ghosts, ['ghost'])
2622
def test_num_versions(self):
2623
index = self.two_graph_index()
2624
self.assertEqual(4, index.num_versions())
2626
def test_get_versions(self):
2627
index = self.two_graph_index()
2628
self.assertEqual(set(['tail', 'tip', 'parent', 'separate']),
2629
set(index.get_versions()))
2631
def test_has_version(self):
2632
index = self.two_graph_index()
2633
self.assertTrue(index.has_version('tail'))
2634
self.assertFalse(index.has_version('ghost'))
2636
def test_get_position(self):
2637
index = self.two_graph_index()
2638
self.assertEqual((index._graph_index._indices[0], 0, 100), index.get_position('tip'))
2639
self.assertEqual((index._graph_index._indices[1], 100, 78), index.get_position('parent'))
2641
def test_get_method(self):
2642
index = self.two_graph_index()
2643
self.assertEqual('fulltext', index.get_method('tip'))
2644
self.assertEqual(['fulltext'], index.get_options('parent'))
2646
def test_get_options(self):
2647
index = self.two_graph_index()
2648
self.assertEqual(['fulltext', 'no-eol'], index.get_options('tip'))
2649
self.assertEqual(['fulltext'], index.get_options('parent'))
2651
def test_get_parents_with_ghosts(self):
2652
index = self.two_graph_index()
2653
self.assertEqual((), index.get_parents_with_ghosts('parent'))
2654
# and errors on ghosts.
2655
self.assertRaises(errors.RevisionNotPresent,
2656
index.get_parents_with_ghosts, 'ghost')
2658
def test_check_versions_present(self):
2659
index = self.two_graph_index()
2660
self.assertRaises(RevisionNotPresent, index.check_versions_present,
2662
self.assertRaises(RevisionNotPresent, index.check_versions_present,
2663
['tail', 'missing'])
2664
index.check_versions_present(['tail', 'separate'])
2666
def catch_add(self, entries):
2667
self.caught_entries.append(entries)
2669
def test_add_no_callback_errors(self):
2670
index = self.two_graph_index()
2671
self.assertRaises(errors.ReadOnlyError, index.add_version,
2672
'new', 'fulltext,no-eol', (None, 50, 60), ['separate'])
2674
def test_add_version_smoke(self):
2675
index = self.two_graph_index(catch_adds=True)
2676
index.add_version('new', 'fulltext,no-eol', (None, 50, 60), [])
2677
self.assertEqual([[(('new', ), 'N50 60')]],
2678
self.caught_entries)
2680
def test_add_version_delta_not_delta_index(self):
2681
index = self.two_graph_index(catch_adds=True)
2682
self.assertRaises(errors.KnitCorrupt, index.add_version,
2683
'new', 'no-eol,line-delta', (None, 0, 100), [])
2684
self.assertEqual([], self.caught_entries)
2686
def test_add_version_same_dup(self):
2687
index = self.two_graph_index(catch_adds=True)
2688
# options can be spelt two different ways
2689
index.add_version('tip', 'fulltext,no-eol', (None, 0, 100), [])
2690
index.add_version('tip', 'no-eol,fulltext', (None, 0, 100), [])
2691
# but neither should have added data.
2692
self.assertEqual([[], []], self.caught_entries)
2694
def test_add_version_different_dup(self):
2695
index = self.two_graph_index(catch_adds=True)
2697
self.assertRaises(errors.KnitCorrupt, index.add_version,
2698
'tip', 'no-eol,line-delta', (None, 0, 100), [])
2699
self.assertRaises(errors.KnitCorrupt, index.add_version,
2700
'tip', 'line-delta,no-eol', (None, 0, 100), [])
2701
self.assertRaises(errors.KnitCorrupt, index.add_version,
2702
'tip', 'fulltext', (None, 0, 100), [])
2704
self.assertRaises(errors.KnitCorrupt, index.add_version,
2705
'tip', 'fulltext,no-eol', (None, 50, 100), [])
2706
self.assertRaises(errors.KnitCorrupt, index.add_version,
2707
'tip', 'fulltext,no-eol', (None, 0, 1000), [])
2709
self.assertRaises(errors.KnitCorrupt, index.add_version,
2710
'tip', 'fulltext,no-eol', (None, 0, 100), ['parent'])
2711
self.assertEqual([], self.caught_entries)
2713
def test_add_versions(self):
2714
index = self.two_graph_index(catch_adds=True)
2715
index.add_versions([
2716
('new', 'fulltext,no-eol', (None, 50, 60), []),
2717
('new2', 'fulltext', (None, 0, 6), []),
2719
self.assertEqual([(('new', ), 'N50 60'), (('new2', ), ' 0 6')],
2720
sorted(self.caught_entries[0]))
2721
self.assertEqual(1, len(self.caught_entries))
2723
def test_add_versions_delta_not_delta_index(self):
2724
index = self.two_graph_index(catch_adds=True)
2725
self.assertRaises(errors.KnitCorrupt, index.add_versions,
2726
[('new', 'no-eol,line-delta', (None, 0, 100), ['parent'])])
2727
self.assertEqual([], self.caught_entries)
2729
def test_add_versions_parents_not_parents_index(self):
2730
index = self.two_graph_index(catch_adds=True)
2731
self.assertRaises(errors.KnitCorrupt, index.add_versions,
2732
[('new', 'no-eol,fulltext', (None, 0, 100), ['parent'])])
2733
self.assertEqual([], self.caught_entries)
2735
def test_add_versions_random_id_accepted(self):
2736
index = self.two_graph_index(catch_adds=True)
2737
index.add_versions([], random_id=True)
2739
def test_add_versions_same_dup(self):
2740
index = self.two_graph_index(catch_adds=True)
2741
# options can be spelt two different ways
2742
index.add_versions([('tip', 'fulltext,no-eol', (None, 0, 100), [])])
2743
index.add_versions([('tip', 'no-eol,fulltext', (None, 0, 100), [])])
2744
# but neither should have added data.
2745
self.assertEqual([[], []], self.caught_entries)
2747
def test_add_versions_different_dup(self):
2748
index = self.two_graph_index(catch_adds=True)
2750
self.assertRaises(errors.KnitCorrupt, index.add_versions,
2751
[('tip', 'no-eol,line-delta', (None, 0, 100), [])])
2752
self.assertRaises(errors.KnitCorrupt, index.add_versions,
2753
[('tip', 'line-delta,no-eol', (None, 0, 100), [])])
2754
self.assertRaises(errors.KnitCorrupt, index.add_versions,
2755
[('tip', 'fulltext', (None, 0, 100), [])])
2757
self.assertRaises(errors.KnitCorrupt, index.add_versions,
2758
[('tip', 'fulltext,no-eol', (None, 50, 100), [])])
2759
self.assertRaises(errors.KnitCorrupt, index.add_versions,
2760
[('tip', 'fulltext,no-eol', (None, 0, 1000), [])])
2762
self.assertRaises(errors.KnitCorrupt, index.add_versions,
2763
[('tip', 'fulltext,no-eol', (None, 0, 100), ['parent'])])
2764
# change options in the second record
2765
self.assertRaises(errors.KnitCorrupt, index.add_versions,
2766
[('tip', 'fulltext,no-eol', (None, 0, 100), []),
2767
('tip', 'no-eol,line-delta', (None, 0, 100), [])])
2768
self.assertEqual([], self.caught_entries)
2770
def test_iter_parents(self):
2771
index = self.two_graph_index()
2772
self.assertEqual(set([
2773
('tip', ()), ('tail', ()), ('parent', ()), ('separate', ())
2775
set(index.iter_parents(['tip', 'tail', 'ghost', 'parent', 'separate'])))
2776
self.assertEqual(set([('tip', ())]),
2777
set(index.iter_parents(['tip'])))
2778
self.assertEqual(set(),
2779
set(index.iter_parents([])))
2782
class TestPackKnits(KnitTests):
2783
"""Tests that use a _PackAccess and KnitGraphIndex."""
2785
def test_get_data_stream_packs_ignores_pack_overhead(self):
2786
# Packs have an encoding overhead that should not be included in the
2787
# 'size' field of a data stream, because it is not returned by the
2788
# raw_reading functions - it is why index_memo's are opaque, and
2789
# get_data_stream was abusing this.
2790
packname = 'test.pack'
2791
transport = self.get_transport()
2792
def write_data(bytes):
2793
transport.append_bytes(packname, bytes)
2794
writer = pack.ContainerWriter(write_data)
2796
index = InMemoryGraphIndex(2)
2797
knit_index = KnitGraphIndex(index, add_callback=index.add_nodes,
2799
indices = {index:(transport, packname)}
2800
access = _PackAccess(indices, writer=(writer, index))
2801
k = KnitVersionedFile('test', get_transport('.'),
2802
delta=True, create=True, index=knit_index, access_method=access)
2803
# insert something into the knit
2804
k.add_lines('text-1', [], ["foo\n"])
2805
# get a data stream for it
2806
stream = k.get_data_stream(['text-1'])
2807
# if the stream has been incorrectly assembled, we will get a short read
2808
# reading from the stream (as streams have no trailer)
2809
expected_length = stream[1][0][2]
2810
# we use -1 to do the read, so that if a trailer is added this test
2811
# will fail and we'll adjust it to handle that case correctly, rather
2812
# than allowing an over-read that is bogus.
2813
self.assertEqual(expected_length, len(stream[2](-1)))
2816
class Test_StreamIndex(KnitTests):
2818
def get_index(self, knit, stream):
2819
"""Get a _StreamIndex from knit and stream."""
2820
return knit._knit_from_datastream(stream)._index
2822
def assertIndexVersions(self, knit, versions):
2823
"""Check that the _StreamIndex versions are those of the stream."""
2824
index = self.get_index(knit, knit.get_data_stream(versions))
2825
self.assertEqual(set(index.get_versions()), set(versions))
2826
# check we didn't get duplicates
2827
self.assertEqual(len(index.get_versions()), len(versions))
2829
def assertIndexAncestry(self, knit, ancestry_versions, versions, result):
2830
"""Check the result of a get_ancestry call on knit."""
2831
index = self.get_index(knit, knit.get_data_stream(versions))
2834
set(index.get_ancestry(ancestry_versions, False)))
2836
def assertIterParents(self, knit, versions, parent_versions, result):
2837
"""Check the result of an iter_parents call on knit."""
2838
index = self.get_index(knit, knit.get_data_stream(versions))
2839
self.assertEqual(result, index.iter_parents(parent_versions))
2841
def assertGetMethod(self, knit, versions, version, result):
2842
index = self.get_index(knit, knit.get_data_stream(versions))
2843
self.assertEqual(result, index.get_method(version))
2845
def assertGetOptions(self, knit, version, options):
2846
index = self.get_index(knit, knit.get_data_stream(version))
2847
self.assertEqual(options, index.get_options(version))
2849
def assertGetPosition(self, knit, versions, version, result):
2850
index = self.get_index(knit, knit.get_data_stream(versions))
2851
if result[1] is None:
2852
result = (result[0], index, result[2], result[3])
2853
self.assertEqual(result, index.get_position(version))
2855
def assertGetParentsWithGhosts(self, knit, versions, version, parents):
2856
index = self.get_index(knit, knit.get_data_stream(versions))
2857
self.assertEqual(parents, index.get_parents_with_ghosts(version))
2859
def make_knit_with_4_versions_2_dags(self):
2860
knit = self.make_test_knit()
2861
knit.add_lines('a', [], ["foo"])
2862
knit.add_lines('b', [], [])
2863
knit.add_lines('c', ['b', 'a'], [])
2864
knit.add_lines_with_ghosts('d', ['e', 'f'], [])
2867
def test_versions(self):
2868
"""The versions of a StreamIndex are those of the datastream."""
2869
knit = self.make_knit_with_4_versions_2_dags()
2870
# ask for most permutations, which catches bugs like falling back to the
2871
# target knit, or showing ghosts, etc.
2872
self.assertIndexVersions(knit, [])
2873
self.assertIndexVersions(knit, ['a'])
2874
self.assertIndexVersions(knit, ['b'])
2875
self.assertIndexVersions(knit, ['c'])
2876
self.assertIndexVersions(knit, ['d'])
2877
self.assertIndexVersions(knit, ['a', 'b'])
2878
self.assertIndexVersions(knit, ['b', 'c'])
2879
self.assertIndexVersions(knit, ['a', 'c'])
2880
self.assertIndexVersions(knit, ['a', 'b', 'c'])
2881
self.assertIndexVersions(knit, ['a', 'b', 'c', 'd'])
2883
def test_construct(self):
2884
"""Constructing a StreamIndex generates index data."""
2885
data_list = [('text-a', ['fulltext'], 127, []),
2886
('text-b', ['option'], 128, ['text-c'])]
2887
index = _StreamIndex(data_list, None)
2888
self.assertEqual({'text-a':(['fulltext'], (0, 127), []),
2889
'text-b':(['option'], (127, 127 + 128), ['text-c'])},
2892
def test_get_ancestry(self):
2893
knit = self.make_knit_with_4_versions_2_dags()
2894
self.assertIndexAncestry(knit, ['a'], ['a'], ['a'])
2895
self.assertIndexAncestry(knit, ['b'], ['b'], ['b'])
2896
self.assertIndexAncestry(knit, ['c'], ['c'], ['c'])
2897
self.assertIndexAncestry(knit, ['c'], ['a', 'b', 'c'],
2898
set(['a', 'b', 'c']))
2899
self.assertIndexAncestry(knit, ['c', 'd'], ['a', 'b', 'c', 'd'],
2900
set(['a', 'b', 'c', 'd']))
2902
def test_get_method(self):
2903
knit = self.make_knit_with_4_versions_2_dags()
2904
self.assertGetMethod(knit, ['a'], 'a', 'fulltext')
2905
self.assertGetMethod(knit, ['c'], 'c', 'line-delta')
2906
# get_method on a basis that is not in the datastream (but in the
2907
# backing knit) returns 'fulltext', because thats what we'll create as
2909
self.assertGetMethod(knit, ['c'], 'b', 'fulltext')
2911
def test_iter_parents(self):
2912
knit = self.make_knit_with_4_versions_2_dags()
2913
self.assertIterParents(knit, ['a'], ['a'], [('a', ())])
2914
self.assertIterParents(knit, ['a', 'b'], ['a', 'b'],
2915
[('a', ()), ('b', ())])
2916
self.assertIterParents(knit, ['a', 'b', 'c'], ['a', 'b', 'c'],
2917
[('a', ()), ('b', ()), ('c', ('b', 'a'))])
2918
self.assertIterParents(knit, ['a', 'b', 'c', 'd'],
2919
['a', 'b', 'c', 'd'],
2920
[('a', ()), ('b', ()), ('c', ('b', 'a')), ('d', ('e', 'f'))])
2921
self.assertIterParents(knit, ['c'], ['a', 'b', 'c'],
2922
[('c', ('b', 'a'))])
2924
def test_get_options(self):
2925
knit = self.make_knit_with_4_versions_2_dags()
2926
self.assertGetOptions(knit, 'a', ['no-eol', 'fulltext'])
2927
self.assertGetOptions(knit, 'c', ['line-delta'])
2929
def test_get_parents_with_ghosts(self):
2930
knit = self.make_knit_with_4_versions_2_dags()
2931
self.assertGetParentsWithGhosts(knit, ['a'], 'a', ())
2932
self.assertGetParentsWithGhosts(knit, ['c'], 'c', ('b', 'a'))
2933
self.assertGetParentsWithGhosts(knit, ['d'], 'd', ('e', 'f'))
2935
def test_get_position(self):
2936
knit = self.make_knit_with_4_versions_2_dags()
2937
# get_position returns (thunk_flag, index(can be None), start, end) for
2938
# _StreamAccess to use.
2939
self.assertGetPosition(knit, ['a'], 'a', (False, None, 0, 78))
2940
self.assertGetPosition(knit, ['a', 'c'], 'c', (False, None, 78, 156))
2941
# get_position on a text that is not in the datastream (but in the
2942
# backing knit) returns (True, 'versionid', None, None) - and then the
2943
# access object can construct the relevant data as needed.
2944
self.assertGetPosition(knit, ['a', 'c'], 'b', (True, 'b', None, None))
2947
class Test_StreamAccess(KnitTests):
2949
def get_index_access(self, knit, stream):
2950
"""Get a _StreamAccess from knit and stream."""
2951
knit = knit._knit_from_datastream(stream)
2952
return knit._index, knit._data._access
2954
def assertGetRawRecords(self, knit, versions):
2955
index, access = self.get_index_access(knit,
2956
knit.get_data_stream(versions))
2957
# check that every version asked for can be obtained from the resulting
2961
for version in versions:
2962
memos.append(knit._index.get_position(version))
2964
for version, data in zip(
2965
versions, knit._data._access.get_raw_records(memos)):
2966
original[version] = data
2968
for version in versions:
2969
memos.append(index.get_position(version))
2971
for version, data in zip(versions, access.get_raw_records(memos)):
2972
streamed[version] = data
2973
self.assertEqual(original, streamed)
2975
for version in versions:
2976
data = list(access.get_raw_records(
2977
[index.get_position(version)]))[0]
2978
self.assertEqual(original[version], data)
2980
def make_knit_with_two_versions(self):
2981
knit = self.make_test_knit()
2982
knit.add_lines('a', [], ["foo"])
2983
knit.add_lines('b', [], ["bar"])
2986
def test_get_raw_records(self):
2987
knit = self.make_knit_with_two_versions()
2988
self.assertGetRawRecords(knit, ['a', 'b'])
2989
self.assertGetRawRecords(knit, ['a'])
2990
self.assertGetRawRecords(knit, ['b'])
2992
def test_get_raw_record_from_backing_knit(self):
2993
# the thunk layer should create an artificial A on-demand when needed.
2994
source_knit = self.make_test_knit(name='plain', annotate=False)
2995
target_knit = self.make_test_knit(name='annotated', annotate=True)
2996
source_knit.add_lines("A", [], ["Foo\n"])
2997
# Give the target A, so we can try to thunk across to it.
2998
target_knit.join(source_knit)
2999
index, access = self.get_index_access(target_knit,
3000
source_knit.get_data_stream([]))
3001
raw_data = list(access.get_raw_records([(True, "A", None, None)]))[0]
3002
df = GzipFile(mode='rb', fileobj=StringIO(raw_data))
3004
'version A 1 5d36b88bb697a2d778f024048bafabd443d74503\n'
3008
def test_asking_for_thunk_stream_is_not_plain_errors(self):
3009
knit = self.make_test_knit(name='annotated', annotate=True)
3010
knit.add_lines("A", [], ["Foo\n"])
3011
index, access = self.get_index_access(knit,
3012
knit.get_data_stream([]))
3013
self.assertRaises(errors.KnitCorrupt,
3014
list, access.get_raw_records([(True, "A", None, None)]))