~bzr-pqm/bzr/bzr.dev

5590.1.1 by John Arbash Meinel
Stop using tuned_gzip, it seems to give incorrect results on python 2.7
1
# Copyright (C) 2006-2011 Canonical Ltd
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
16
17
"""Tests for Knit data structure"""
18
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
19
from cStringIO import StringIO
5590.1.1 by John Arbash Meinel
Stop using tuned_gzip, it seems to give incorrect results on python 2.7
20
import gzip
2484.1.17 by John Arbash Meinel
Workaround for Pyrex <0.9.5 and python >=2.5 incompatibilities.
21
import sys
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
22
2196.2.5 by John Arbash Meinel
Add an exception class when the knit index storage method is unknown, and properly test for it
23
from bzrlib import (
24
    errors,
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
25
    knit,
3350.8.12 by Robert Collins
Stacked make_mpdiffs.
26
    multiparent,
3734.2.4 by Vincent Ladeuil
Fix python2.6 deprecation warnings related to hashlib.
27
    osutils,
2592.3.66 by Robert Collins
Allow adaption of KnitData to pack files.
28
    pack,
4913.2.24 by John Arbash Meinel
Track down a few more import typos.
29
    tests,
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
30
    transport,
2196.2.5 by John Arbash Meinel
Add an exception class when the knit index storage method is unknown, and properly test for it
31
    )
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
32
from bzrlib.errors import (
33
    KnitHeaderError,
34
    NoSuchFile,
35
    )
2592.3.1 by Robert Collins
Allow giving KnitVersionedFile an index object to use rather than implicitly creating one.
36
from bzrlib.index import *
1684.3.3 by Robert Collins
Add a special cased weaves to knit converter.
37
from bzrlib.knit import (
2794.1.2 by Robert Collins
Nuke versioned file add/get delta support, allowing easy simplification of unannotated Content, reducing memory copies and friction during commit on unannotated texts.
38
    AnnotatedKnitContent,
2151.1.1 by John Arbash Meinel
(Dmitry Vasiliev) Tune KnitContent and add tests
39
    KnitContent,
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
40
    KnitVersionedFiles,
2794.1.2 by Robert Collins
Nuke versioned file add/get delta support, allowing easy simplification of unannotated Content, reducing memory copies and friction during commit on unannotated texts.
41
    PlainKnitContent,
4005.3.6 by Robert Collins
Support delta_closure=True with NetworkRecordStream to transmit deltas over the wire when full text extraction is required on the far end.
42
    _VFContentMapGenerator,
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
43
    _KndxIndex,
44
    _KnitGraphIndex,
45
    _KnitKeyAccess,
46
    make_file_factory,
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
47
    )
5279.1.1 by Andrew Bennetts
lazy_import most things in merge.py; add a few representative modules to the import tariff tests; tweak a couple of other modules so that patiencediff is not necessarily imported; remove a bunch of unused imports from test_knit.py.
48
from bzrlib.patiencediff import PatienceSequenceMatcher
5757.8.11 by Jelmer Vernooij
Merge knitpackrepo-6.
49
from bzrlib.repofmt import (
50
    knitpack_repo,
51
    pack_repo,
52
    )
2592.3.66 by Robert Collins
Allow adaption of KnitData to pack files.
53
from bzrlib.tests import (
54
    TestCase,
55
    TestCaseWithMemoryTransport,
56
    TestCaseWithTransport,
3787.1.1 by Robert Collins
Embed the failed text in sha1 knit errors.
57
    TestNotApplicable,
2592.3.66 by Robert Collins
Allow adaption of KnitData to pack files.
58
    )
3350.8.2 by Robert Collins
stacked get_parent_map.
59
from bzrlib.versionedfile import (
3350.8.6 by Robert Collins
get_record_stream stacking for delta access.
60
    AbsentContentFactory,
3350.8.2 by Robert Collins
stacked get_parent_map.
61
    ConstantMapper,
4005.3.6 by Robert Collins
Support delta_closure=True with NetworkRecordStream to transmit deltas over the wire when full text extraction is required on the far end.
62
    network_bytes_to_kind_and_offset,
3350.8.2 by Robert Collins
stacked get_parent_map.
63
    RecordingVersionedFilesDecorator,
64
    )
5967.12.1 by Martin Pool
Move all test features into bzrlib.tests.features
65
from bzrlib.tests import (
66
    features,
67
    )
68
69
70
compiled_knit_feature = features.ModuleAvailableFeature(
71
    'bzrlib._knit_load_data_pyx')
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
72
73
2794.1.2 by Robert Collins
Nuke versioned file add/get delta support, allowing easy simplification of unannotated Content, reducing memory copies and friction during commit on unannotated texts.
74
class KnitContentTestsMixin(object):
2151.1.1 by John Arbash Meinel
(Dmitry Vasiliev) Tune KnitContent and add tests
75
76
    def test_constructor(self):
2794.1.2 by Robert Collins
Nuke versioned file add/get delta support, allowing easy simplification of unannotated Content, reducing memory copies and friction during commit on unannotated texts.
77
        content = self._make_content([])
2151.1.1 by John Arbash Meinel
(Dmitry Vasiliev) Tune KnitContent and add tests
78
79
    def test_text(self):
2794.1.2 by Robert Collins
Nuke versioned file add/get delta support, allowing easy simplification of unannotated Content, reducing memory copies and friction during commit on unannotated texts.
80
        content = self._make_content([])
2151.1.1 by John Arbash Meinel
(Dmitry Vasiliev) Tune KnitContent and add tests
81
        self.assertEqual(content.text(), [])
82
2794.1.2 by Robert Collins
Nuke versioned file add/get delta support, allowing easy simplification of unannotated Content, reducing memory copies and friction during commit on unannotated texts.
83
        content = self._make_content([("origin1", "text1"), ("origin2", "text2")])
2151.1.1 by John Arbash Meinel
(Dmitry Vasiliev) Tune KnitContent and add tests
84
        self.assertEqual(content.text(), ["text1", "text2"])
85
2794.1.2 by Robert Collins
Nuke versioned file add/get delta support, allowing easy simplification of unannotated Content, reducing memory copies and friction during commit on unannotated texts.
86
    def test_copy(self):
87
        content = self._make_content([("origin1", "text1"), ("origin2", "text2")])
88
        copy = content.copy()
89
        self.assertIsInstance(copy, content.__class__)
90
        self.assertEqual(copy.annotate(), content.annotate())
91
92
    def assertDerivedBlocksEqual(self, source, target, noeol=False):
93
        """Assert that the derived matching blocks match real output"""
94
        source_lines = source.splitlines(True)
95
        target_lines = target.splitlines(True)
96
        def nl(line):
97
            if noeol and not line.endswith('\n'):
98
                return line + '\n'
99
            else:
100
                return line
101
        source_content = self._make_content([(None, nl(l)) for l in source_lines])
102
        target_content = self._make_content([(None, nl(l)) for l in target_lines])
103
        line_delta = source_content.line_delta(target_content)
104
        delta_blocks = list(KnitContent.get_line_delta_blocks(line_delta,
105
            source_lines, target_lines))
5279.1.1 by Andrew Bennetts
lazy_import most things in merge.py; add a few representative modules to the import tariff tests; tweak a couple of other modules so that patiencediff is not necessarily imported; remove a bunch of unused imports from test_knit.py.
106
        matcher = PatienceSequenceMatcher(None, source_lines, target_lines)
107
        matcher_blocks = list(matcher.get_matching_blocks())
2794.1.2 by Robert Collins
Nuke versioned file add/get delta support, allowing easy simplification of unannotated Content, reducing memory copies and friction during commit on unannotated texts.
108
        self.assertEqual(matcher_blocks, delta_blocks)
109
110
    def test_get_line_delta_blocks(self):
111
        self.assertDerivedBlocksEqual('a\nb\nc\n', 'q\nc\n')
112
        self.assertDerivedBlocksEqual(TEXT_1, TEXT_1)
113
        self.assertDerivedBlocksEqual(TEXT_1, TEXT_1A)
114
        self.assertDerivedBlocksEqual(TEXT_1, TEXT_1B)
115
        self.assertDerivedBlocksEqual(TEXT_1B, TEXT_1A)
116
        self.assertDerivedBlocksEqual(TEXT_1A, TEXT_1B)
117
        self.assertDerivedBlocksEqual(TEXT_1A, '')
118
        self.assertDerivedBlocksEqual('', TEXT_1A)
119
        self.assertDerivedBlocksEqual('', '')
120
        self.assertDerivedBlocksEqual('a\nb\nc', 'a\nb\nc\nd')
121
122
    def test_get_line_delta_blocks_noeol(self):
123
        """Handle historical knit deltas safely
124
125
        Some existing knit deltas don't consider the last line to differ
126
        when the only difference whether it has a final newline.
127
128
        New knit deltas appear to always consider the last line to differ
129
        in this case.
130
        """
131
        self.assertDerivedBlocksEqual('a\nb\nc', 'a\nb\nc\nd\n', noeol=True)
132
        self.assertDerivedBlocksEqual('a\nb\nc\nd\n', 'a\nb\nc', noeol=True)
133
        self.assertDerivedBlocksEqual('a\nb\nc\n', 'a\nb\nc', noeol=True)
134
        self.assertDerivedBlocksEqual('a\nb\nc', 'a\nb\nc\n', noeol=True)
135
136
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
137
TEXT_1 = """\
138
Banana cup cakes:
139
140
- bananas
141
- eggs
142
- broken tea cups
143
"""
144
145
TEXT_1A = """\
146
Banana cup cake recipe
147
(serves 6)
148
149
- bananas
150
- eggs
151
- broken tea cups
152
- self-raising flour
153
"""
154
155
TEXT_1B = """\
156
Banana cup cake recipe
157
158
- bananas (do not use plantains!!!)
159
- broken tea cups
160
- flour
161
"""
162
163
delta_1_1a = """\
164
0,1,2
165
Banana cup cake recipe
166
(serves 6)
167
5,5,1
168
- self-raising flour
169
"""
170
171
TEXT_2 = """\
172
Boeuf bourguignon
173
174
- beef
175
- red wine
176
- small onions
177
- carrot
178
- mushrooms
179
"""
180
181
2794.1.2 by Robert Collins
Nuke versioned file add/get delta support, allowing easy simplification of unannotated Content, reducing memory copies and friction during commit on unannotated texts.
182
class TestPlainKnitContent(TestCase, KnitContentTestsMixin):
183
184
    def _make_content(self, lines):
185
        annotated_content = AnnotatedKnitContent(lines)
186
        return PlainKnitContent(annotated_content.text(), 'bogus')
187
188
    def test_annotate(self):
189
        content = self._make_content([])
190
        self.assertEqual(content.annotate(), [])
191
192
        content = self._make_content([("origin1", "text1"), ("origin2", "text2")])
193
        self.assertEqual(content.annotate(),
194
            [("bogus", "text1"), ("bogus", "text2")])
195
196
    def test_line_delta(self):
197
        content1 = self._make_content([("", "a"), ("", "b")])
198
        content2 = self._make_content([("", "a"), ("", "a"), ("", "c")])
199
        self.assertEqual(content1.line_delta(content2),
200
            [(1, 2, 2, ["a", "c"])])
201
202
    def test_line_delta_iter(self):
203
        content1 = self._make_content([("", "a"), ("", "b")])
204
        content2 = self._make_content([("", "a"), ("", "a"), ("", "c")])
205
        it = content1.line_delta_iter(content2)
206
        self.assertEqual(it.next(), (1, 2, 2, ["a", "c"]))
207
        self.assertRaises(StopIteration, it.next)
208
209
210
class TestAnnotatedKnitContent(TestCase, KnitContentTestsMixin):
211
212
    def _make_content(self, lines):
213
        return AnnotatedKnitContent(lines)
214
215
    def test_annotate(self):
216
        content = self._make_content([])
217
        self.assertEqual(content.annotate(), [])
218
219
        content = self._make_content([("origin1", "text1"), ("origin2", "text2")])
2151.1.1 by John Arbash Meinel
(Dmitry Vasiliev) Tune KnitContent and add tests
220
        self.assertEqual(content.annotate(),
221
            [("origin1", "text1"), ("origin2", "text2")])
222
223
    def test_line_delta(self):
2794.1.2 by Robert Collins
Nuke versioned file add/get delta support, allowing easy simplification of unannotated Content, reducing memory copies and friction during commit on unannotated texts.
224
        content1 = self._make_content([("", "a"), ("", "b")])
225
        content2 = self._make_content([("", "a"), ("", "a"), ("", "c")])
2151.1.1 by John Arbash Meinel
(Dmitry Vasiliev) Tune KnitContent and add tests
226
        self.assertEqual(content1.line_delta(content2),
227
            [(1, 2, 2, [("", "a"), ("", "c")])])
228
229
    def test_line_delta_iter(self):
2794.1.2 by Robert Collins
Nuke versioned file add/get delta support, allowing easy simplification of unannotated Content, reducing memory copies and friction during commit on unannotated texts.
230
        content1 = self._make_content([("", "a"), ("", "b")])
231
        content2 = self._make_content([("", "a"), ("", "a"), ("", "c")])
2151.1.1 by John Arbash Meinel
(Dmitry Vasiliev) Tune KnitContent and add tests
232
        it = content1.line_delta_iter(content2)
233
        self.assertEqual(it.next(), (1, 2, 2, [("", "a"), ("", "c")]))
234
        self.assertRaises(StopIteration, it.next)
235
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
236
237
class MockTransport(object):
238
239
    def __init__(self, file_lines=None):
240
        self.file_lines = file_lines
241
        self.calls = []
2196.2.3 by John Arbash Meinel
Update tests and code to pass after merging bzr.dev
242
        # We have no base directory for the MockTransport
243
        self.base = ''
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
244
245
    def get(self, filename):
246
        if self.file_lines is None:
247
            raise NoSuchFile(filename)
248
        else:
249
            return StringIO("\n".join(self.file_lines))
250
2329.1.1 by John Arbash Meinel
Update _KnitData parser to raise more helpful errors when it detects corruption.
251
    def readv(self, relpath, offsets):
252
        fp = self.get(relpath)
253
        for offset, size in offsets:
254
            fp.seek(offset)
255
            yield offset, fp.read(size)
256
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
257
    def __getattr__(self, name):
258
        def queue_call(*args, **kwargs):
259
            self.calls.append((name, args, kwargs))
260
        return queue_call
261
262
3789.2.2 by John Arbash Meinel
Test that a readv() failing after yielding data will still raise Retry
263
class MockReadvFailingTransport(MockTransport):
264
    """Fail in the middle of a readv() result.
265
3789.2.3 by John Arbash Meinel
Change the mocking a bit, so we can be sure it is failing at the right time.
266
    This Transport will successfully yield the first two requested hunks, but
267
    raise NoSuchFile for the rest.
3789.2.2 by John Arbash Meinel
Test that a readv() failing after yielding data will still raise Retry
268
    """
269
270
    def readv(self, relpath, offsets):
3789.2.3 by John Arbash Meinel
Change the mocking a bit, so we can be sure it is failing at the right time.
271
        count = 0
3789.2.2 by John Arbash Meinel
Test that a readv() failing after yielding data will still raise Retry
272
        for result in MockTransport.readv(self, relpath, offsets):
3789.2.3 by John Arbash Meinel
Change the mocking a bit, so we can be sure it is failing at the right time.
273
            count += 1
274
            # we use 2 because the first offset is the pack header, the second
275
            # is the first actual content requset
276
            if count > 2:
3789.2.2 by John Arbash Meinel
Test that a readv() failing after yielding data will still raise Retry
277
                raise errors.NoSuchFile(relpath)
278
            yield result
279
280
2592.3.66 by Robert Collins
Allow adaption of KnitData to pack files.
281
class KnitRecordAccessTestsMixin(object):
282
    """Tests for getting and putting knit records."""
283
284
    def test_add_raw_records(self):
285
        """Add_raw_records adds records retrievable later."""
286
        access = self.get_access()
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
287
        memos = access.add_raw_records([('key', 10)], '1234567890')
2592.3.66 by Robert Collins
Allow adaption of KnitData to pack files.
288
        self.assertEqual(['1234567890'], list(access.get_raw_records(memos)))
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
289
2592.3.67 by Robert Collins
More tests for bzrlib.knit._PackAccess.
290
    def test_add_several_raw_records(self):
291
        """add_raw_records with many records and read some back."""
292
        access = self.get_access()
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
293
        memos = access.add_raw_records([('key', 10), ('key2', 2), ('key3', 5)],
294
            '12345678901234567')
2592.3.67 by Robert Collins
More tests for bzrlib.knit._PackAccess.
295
        self.assertEqual(['1234567890', '12', '34567'],
296
            list(access.get_raw_records(memos)))
297
        self.assertEqual(['1234567890'],
298
            list(access.get_raw_records(memos[0:1])))
299
        self.assertEqual(['12'],
300
            list(access.get_raw_records(memos[1:2])))
301
        self.assertEqual(['34567'],
302
            list(access.get_raw_records(memos[2:3])))
303
        self.assertEqual(['1234567890', '34567'],
304
            list(access.get_raw_records(memos[0:1] + memos[2:3])))
305
2592.3.66 by Robert Collins
Allow adaption of KnitData to pack files.
306
307
class TestKnitKnitAccess(TestCaseWithMemoryTransport, KnitRecordAccessTestsMixin):
308
    """Tests for the .kndx implementation."""
309
310
    def get_access(self):
311
        """Get a .knit style access instance."""
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
312
        mapper = ConstantMapper("foo")
313
        access = _KnitKeyAccess(self.get_transport(), mapper)
2592.3.66 by Robert Collins
Allow adaption of KnitData to pack files.
314
        return access
3789.2.6 by John Arbash Meinel
Make _DirectPackAccess.reload_or_raise maintain the logic.
315
316
317
class _TestException(Exception):
318
    """Just an exception for local tests to use."""
319
2592.3.66 by Robert Collins
Allow adaption of KnitData to pack files.
320
321
class TestPackKnitAccess(TestCaseWithMemoryTransport, KnitRecordAccessTestsMixin):
322
    """Tests for the pack based access."""
323
324
    def get_access(self):
2592.3.67 by Robert Collins
More tests for bzrlib.knit._PackAccess.
325
        return self._get_access()[0]
326
327
    def _get_access(self, packname='packfile', index='FOO'):
2592.3.66 by Robert Collins
Allow adaption of KnitData to pack files.
328
        transport = self.get_transport()
329
        def write_data(bytes):
2592.3.67 by Robert Collins
More tests for bzrlib.knit._PackAccess.
330
            transport.append_bytes(packname, bytes)
2592.3.66 by Robert Collins
Allow adaption of KnitData to pack files.
331
        writer = pack.ContainerWriter(write_data)
332
        writer.begin()
5757.5.1 by Jelmer Vernooij
Move _DirectPackAccess to bzrlib.repofmt.pack_repo.
333
        access = pack_repo._DirectPackAccess({})
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
334
        access.set_writer(writer, index, (transport, packname))
2592.3.67 by Robert Collins
More tests for bzrlib.knit._PackAccess.
335
        return access, writer
2592.3.66 by Robert Collins
Allow adaption of KnitData to pack files.
336
3789.2.5 by John Arbash Meinel
Change _DirectPackAccess to only raise Retry when _reload_func is defined.
337
    def make_pack_file(self):
338
        """Create a pack file with 2 records."""
339
        access, writer = self._get_access(packname='packname', index='foo')
340
        memos = []
341
        memos.extend(access.add_raw_records([('key1', 10)], '1234567890'))
342
        memos.extend(access.add_raw_records([('key2', 5)], '12345'))
343
        writer.end()
344
        return memos
345
5050.64.1 by Andrew Bennetts
Add reload-and-retry logic to RepositoryPackCollection.pack when a concurrent pack happens.
346
    def test_pack_collection_pack_retries(self):
347
        """An explicit pack of a pack collection succeeds even when a
348
        concurrent pack happens.
349
        """
350
        builder = self.make_branch_builder('.')
351
        builder.start_series()
352
        builder.build_snapshot('rev-1', None, [
353
            ('add', ('', 'root-id', 'directory', None)),
354
            ('add', ('file', 'file-id', 'file', 'content\nrev 1\n')),
355
            ])
356
        builder.build_snapshot('rev-2', ['rev-1'], [
357
            ('modify', ('file-id', 'content\nrev 2\n')),
358
            ])
359
        builder.build_snapshot('rev-3', ['rev-2'], [
360
            ('modify', ('file-id', 'content\nrev 3\n')),
361
            ])
362
        self.addCleanup(builder.finish_series)
363
        b = builder.get_branch()
364
        self.addCleanup(b.lock_write().unlock)
365
        repo = b.repository
366
        collection = repo._pack_collection
367
        # Concurrently repack the repo.
368
        reopened_repo = repo.bzrdir.open_repository()
369
        reopened_repo.pack()
370
        # Pack the new pack.
371
        collection.pack()
372
3789.2.11 by John Arbash Meinel
KnitVersionedFile.get_record_stream now retries *and* fails correctly.
373
    def make_vf_for_retrying(self):
3789.2.10 by John Arbash Meinel
The first function for KnitVersionedFiles can now retry on request.
374
        """Create 3 packs and a reload function.
375
376
        Originally, 2 pack files will have the data, but one will be missing.
377
        And then the third will be used in place of the first two if reload()
378
        is called.
379
380
        :return: (versioned_file, reload_counter)
381
            versioned_file  a KnitVersionedFiles using the packs for access
382
        """
4617.7.1 by Robert Collins
Lock the format knit retry tests depend on - knits aren't used for 2a formats.
383
        builder = self.make_branch_builder('.', format="1.9")
4454.3.59 by John Arbash Meinel
Track down why the annotate retry code was failing.
384
        builder.start_series()
385
        builder.build_snapshot('rev-1', None, [
386
            ('add', ('', 'root-id', 'directory', None)),
387
            ('add', ('file', 'file-id', 'file', 'content\nrev 1\n')),
388
            ])
389
        builder.build_snapshot('rev-2', ['rev-1'], [
390
            ('modify', ('file-id', 'content\nrev 2\n')),
391
            ])
392
        builder.build_snapshot('rev-3', ['rev-2'], [
393
            ('modify', ('file-id', 'content\nrev 3\n')),
394
            ])
395
        builder.finish_series()
396
        b = builder.get_branch()
397
        b.lock_write()
398
        self.addCleanup(b.unlock)
4145.1.6 by Robert Collins
More test fallout, but all caught now.
399
        # Pack these three revisions into another pack file, but don't remove
400
        # the originals
4454.3.59 by John Arbash Meinel
Track down why the annotate retry code was failing.
401
        repo = b.repository
4145.1.6 by Robert Collins
More test fallout, but all caught now.
402
        collection = repo._pack_collection
403
        collection.ensure_loaded()
404
        orig_packs = collection.packs
5757.7.3 by Jelmer Vernooij
Move more knitpack-specific functionality out of Packer.
405
        packer = knitpack_repo.KnitPacker(collection, orig_packs, '.testpack')
4145.1.6 by Robert Collins
More test fallout, but all caught now.
406
        new_pack = packer.pack()
407
        # forget about the new pack
408
        collection.reset()
409
        repo.refresh_data()
4454.3.59 by John Arbash Meinel
Track down why the annotate retry code was failing.
410
        vf = repo.revisions
3789.2.10 by John Arbash Meinel
The first function for KnitVersionedFiles can now retry on request.
411
        # Set up a reload() function that switches to using the new pack file
412
        new_index = new_pack.revision_index
413
        access_tuple = new_pack.access_tuple()
414
        reload_counter = [0, 0, 0]
415
        def reload():
416
            reload_counter[0] += 1
417
            if reload_counter[1] > 0:
418
                # We already reloaded, nothing more to do
419
                reload_counter[2] += 1
420
                return False
421
            reload_counter[1] += 1
422
            vf._index._graph_index._indices[:] = [new_index]
423
            vf._access._indices.clear()
424
            vf._access._indices[new_index] = access_tuple
425
            return True
3789.2.11 by John Arbash Meinel
KnitVersionedFile.get_record_stream now retries *and* fails correctly.
426
        # Delete one of the pack files so the data will need to be reloaded. We
3789.2.12 by John Arbash Meinel
iter_lines_added_or_present now retries.
427
        # will delete the file with 'rev-2' in it
3789.2.10 by John Arbash Meinel
The first function for KnitVersionedFiles can now retry on request.
428
        trans, name = orig_packs[1].access_tuple()
429
        trans.delete(name)
430
        # We don't have the index trigger reloading because we want to test
431
        # that we reload when the .pack disappears
432
        vf._access._reload_func = reload
433
        return vf, reload_counter
434
3789.2.6 by John Arbash Meinel
Make _DirectPackAccess.reload_or_raise maintain the logic.
435
    def make_reload_func(self, return_val=True):
3789.2.5 by John Arbash Meinel
Change _DirectPackAccess to only raise Retry when _reload_func is defined.
436
        reload_called = [0]
437
        def reload():
438
            reload_called[0] += 1
3789.2.6 by John Arbash Meinel
Make _DirectPackAccess.reload_or_raise maintain the logic.
439
            return return_val
3789.2.5 by John Arbash Meinel
Change _DirectPackAccess to only raise Retry when _reload_func is defined.
440
        return reload_called, reload
441
3789.2.6 by John Arbash Meinel
Make _DirectPackAccess.reload_or_raise maintain the logic.
442
    def make_retry_exception(self):
443
        # We raise a real exception so that sys.exc_info() is properly
444
        # populated
445
        try:
446
            raise _TestException('foobar')
447
        except _TestException, e:
3789.2.29 by John Arbash Meinel
RetryWithNewPacks requires another argument.
448
            retry_exc = errors.RetryWithNewPacks(None, reload_occurred=False,
3789.2.6 by John Arbash Meinel
Make _DirectPackAccess.reload_or_raise maintain the logic.
449
                                                 exc_info=sys.exc_info())
5340.15.2 by John Arbash Meinel
supercede 2.4-613247-cleanup-tests
450
        # GZ 2010-08-10: Cycle with exc_info affects 3 tests
3789.2.6 by John Arbash Meinel
Make _DirectPackAccess.reload_or_raise maintain the logic.
451
        return retry_exc
452
2592.3.67 by Robert Collins
More tests for bzrlib.knit._PackAccess.
453
    def test_read_from_several_packs(self):
454
        access, writer = self._get_access()
455
        memos = []
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
456
        memos.extend(access.add_raw_records([('key', 10)], '1234567890'))
2592.3.67 by Robert Collins
More tests for bzrlib.knit._PackAccess.
457
        writer.end()
458
        access, writer = self._get_access('pack2', 'FOOBAR')
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
459
        memos.extend(access.add_raw_records([('key', 5)], '12345'))
2592.3.67 by Robert Collins
More tests for bzrlib.knit._PackAccess.
460
        writer.end()
461
        access, writer = self._get_access('pack3', 'BAZ')
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
462
        memos.extend(access.add_raw_records([('key', 5)], 'alpha'))
2592.3.67 by Robert Collins
More tests for bzrlib.knit._PackAccess.
463
        writer.end()
464
        transport = self.get_transport()
5757.5.1 by Jelmer Vernooij
Move _DirectPackAccess to bzrlib.repofmt.pack_repo.
465
        access = pack_repo._DirectPackAccess({"FOO":(transport, 'packfile'),
2592.3.67 by Robert Collins
More tests for bzrlib.knit._PackAccess.
466
            "FOOBAR":(transport, 'pack2'),
467
            "BAZ":(transport, 'pack3')})
468
        self.assertEqual(['1234567890', '12345', 'alpha'],
469
            list(access.get_raw_records(memos)))
470
        self.assertEqual(['1234567890'],
471
            list(access.get_raw_records(memos[0:1])))
472
        self.assertEqual(['12345'],
473
            list(access.get_raw_records(memos[1:2])))
474
        self.assertEqual(['alpha'],
475
            list(access.get_raw_records(memos[2:3])))
476
        self.assertEqual(['1234567890', 'alpha'],
477
            list(access.get_raw_records(memos[0:1] + memos[2:3])))
2592.3.66 by Robert Collins
Allow adaption of KnitData to pack files.
478
2592.3.70 by Robert Collins
Allow setting a writer after creating a knit._PackAccess object.
479
    def test_set_writer(self):
480
        """The writer should be settable post construction."""
5757.5.1 by Jelmer Vernooij
Move _DirectPackAccess to bzrlib.repofmt.pack_repo.
481
        access = pack_repo._DirectPackAccess({})
2592.3.70 by Robert Collins
Allow setting a writer after creating a knit._PackAccess object.
482
        transport = self.get_transport()
483
        packname = 'packfile'
484
        index = 'foo'
485
        def write_data(bytes):
486
            transport.append_bytes(packname, bytes)
487
        writer = pack.ContainerWriter(write_data)
488
        writer.begin()
489
        access.set_writer(writer, index, (transport, packname))
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
490
        memos = access.add_raw_records([('key', 10)], '1234567890')
2592.3.70 by Robert Collins
Allow setting a writer after creating a knit._PackAccess object.
491
        writer.end()
492
        self.assertEqual(['1234567890'], list(access.get_raw_records(memos)))
493
3789.2.1 by John Arbash Meinel
_DirectPackAccess can now raise RetryWithNewPacks when we think something has happened.
494
    def test_missing_index_raises_retry(self):
3789.2.5 by John Arbash Meinel
Change _DirectPackAccess to only raise Retry when _reload_func is defined.
495
        memos = self.make_pack_file()
3789.2.1 by John Arbash Meinel
_DirectPackAccess can now raise RetryWithNewPacks when we think something has happened.
496
        transport = self.get_transport()
3789.2.5 by John Arbash Meinel
Change _DirectPackAccess to only raise Retry when _reload_func is defined.
497
        reload_called, reload_func = self.make_reload_func()
498
        # Note that the index key has changed from 'foo' to 'bar'
5757.5.1 by Jelmer Vernooij
Move _DirectPackAccess to bzrlib.repofmt.pack_repo.
499
        access = pack_repo._DirectPackAccess({'bar':(transport, 'packname')},
3789.2.5 by John Arbash Meinel
Change _DirectPackAccess to only raise Retry when _reload_func is defined.
500
                                   reload_func=reload_func)
3789.2.1 by John Arbash Meinel
_DirectPackAccess can now raise RetryWithNewPacks when we think something has happened.
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)
509
3789.2.5 by John Arbash Meinel
Change _DirectPackAccess to only raise Retry when _reload_func is defined.
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'
5757.5.1 by Jelmer Vernooij
Move _DirectPackAccess to bzrlib.repofmt.pack_repo.
514
        access = pack_repo._DirectPackAccess({'bar':(transport, 'packname')})
3789.2.5 by John Arbash Meinel
Change _DirectPackAccess to only raise Retry when _reload_func is defined.
515
        e = self.assertListRaises(KeyError, access.get_raw_records, memos)
516
3789.2.1 by John Arbash Meinel
_DirectPackAccess can now raise RetryWithNewPacks when we think something has happened.
517
    def test_missing_file_raises_retry(self):
3789.2.5 by John Arbash Meinel
Change _DirectPackAccess to only raise Retry when _reload_func is defined.
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'
5757.5.1 by Jelmer Vernooij
Move _DirectPackAccess to bzrlib.repofmt.pack_repo.
522
        access = pack_repo._DirectPackAccess(
5757.8.1 by Jelmer Vernooij
Avoid bzrlib.knit imports when using groupcompress repositories.
523
            {'foo':(transport, 'different-packname')},
524
            reload_func=reload_func)
3789.2.5 by John Arbash Meinel
Change _DirectPackAccess to only raise Retry when _reload_func is defined.
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)
533
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'
5757.5.1 by Jelmer Vernooij
Move _DirectPackAccess to bzrlib.repofmt.pack_repo.
538
        access = pack_repo._DirectPackAccess(
5757.5.2 by Jelmer Vernooij
merge bzr.dev.
539
            {'foo': (transport, 'different-packname')})
3789.2.5 by John Arbash Meinel
Change _DirectPackAccess to only raise Retry when _reload_func is defined.
540
        e = self.assertListRaises(errors.NoSuchFile,
3789.2.1 by John Arbash Meinel
_DirectPackAccess can now raise RetryWithNewPacks when we think something has happened.
541
                                  access.get_raw_records, memos)
542
3789.2.2 by John Arbash Meinel
Test that a readv() failing after yielding data will still raise Retry
543
    def test_failing_readv_raises_retry(self):
3789.2.5 by John Arbash Meinel
Change _DirectPackAccess to only raise Retry when _reload_func is defined.
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()
5757.5.1 by Jelmer Vernooij
Move _DirectPackAccess to bzrlib.repofmt.pack_repo.
549
        access = pack_repo._DirectPackAccess(
5757.5.2 by Jelmer Vernooij
merge bzr.dev.
550
            {'foo': (failing_transport, 'packname')},
5757.8.1 by Jelmer Vernooij
Avoid bzrlib.knit imports when using groupcompress repositories.
551
            reload_func=reload_func)
3789.2.5 by John Arbash Meinel
Change _DirectPackAccess to only raise Retry when _reload_func is defined.
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)
566
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()
5757.5.1 by Jelmer Vernooij
Move _DirectPackAccess to bzrlib.repofmt.pack_repo.
573
        access = pack_repo._DirectPackAccess(
5757.8.1 by Jelmer Vernooij
Avoid bzrlib.knit imports when using groupcompress repositories.
574
            {'foo':(failing_transport, 'packname')})
3789.2.3 by John Arbash Meinel
Change the mocking a bit, so we can be sure it is failing at the right time.
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
3789.2.5 by John Arbash Meinel
Change _DirectPackAccess to only raise Retry when _reload_func is defined.
581
        e = self.assertListRaises(errors.NoSuchFile,
3789.2.2 by John Arbash Meinel
Test that a readv() failing after yielding data will still raise Retry
582
                                  access.get_raw_records, memos)
583
3789.2.6 by John Arbash Meinel
Make _DirectPackAccess.reload_or_raise maintain the logic.
584
    def test_reload_or_raise_no_reload(self):
5757.5.1 by Jelmer Vernooij
Move _DirectPackAccess to bzrlib.repofmt.pack_repo.
585
        access = pack_repo._DirectPackAccess({}, reload_func=None)
3789.2.6 by John Arbash Meinel
Make _DirectPackAccess.reload_or_raise maintain the logic.
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)
589
590
    def test_reload_or_raise_reload_changed(self):
591
        reload_called, reload_func = self.make_reload_func(return_val=True)
5757.5.1 by Jelmer Vernooij
Move _DirectPackAccess to bzrlib.repofmt.pack_repo.
592
        access = pack_repo._DirectPackAccess({}, reload_func=reload_func)
3789.2.6 by John Arbash Meinel
Make _DirectPackAccess.reload_or_raise maintain the logic.
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)
599
600
    def test_reload_or_raise_reload_no_change(self):
601
        reload_called, reload_func = self.make_reload_func(return_val=False)
5757.5.1 by Jelmer Vernooij
Move _DirectPackAccess to bzrlib.repofmt.pack_repo.
602
        access = pack_repo._DirectPackAccess({}, reload_func=reload_func)
3789.2.6 by John Arbash Meinel
Make _DirectPackAccess.reload_or_raise maintain the logic.
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)
613
3789.2.13 by John Arbash Meinel
KnitVersionedFile.annotate() now retries when appropriate.
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
618
        key = ('rev-3',)
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():
628
            trans.delete(name)
629
        self.assertRaises(errors.NoSuchFile, vf.annotate, key)
630
        self.assertEqual([2, 1, 1], reload_counter)
631
3789.2.10 by John Arbash Meinel
The first function for KnitVersionedFiles can now retry on request.
632
    def test__get_record_map_retries(self):
3789.2.11 by John Arbash Meinel
KnitVersionedFile.get_record_stream now retries *and* fails correctly.
633
        vf, reload_counter = self.make_vf_for_retrying()
3789.2.12 by John Arbash Meinel
iter_lines_added_or_present now retries.
634
        keys = [('rev-1',), ('rev-2',), ('rev-3',)]
3789.2.10 by John Arbash Meinel
The first function for KnitVersionedFiles can now retry on request.
635
        records = vf._get_record_map(keys)
636
        self.assertEqual(keys, sorted(records.keys()))
3789.2.11 by John Arbash Meinel
KnitVersionedFile.get_record_stream now retries *and* fails correctly.
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():
641
            trans.delete(name)
642
        self.assertRaises(errors.NoSuchFile, vf._get_record_map, keys)
643
        self.assertEqual([2, 1, 1], reload_counter)
644
645
    def test_get_record_stream_retries(self):
646
        vf, reload_counter = self.make_vf_for_retrying()
3789.2.12 by John Arbash Meinel
iter_lines_added_or_present now retries.
647
        keys = [('rev-1',), ('rev-2',), ('rev-3',)]
3789.2.11 by John Arbash Meinel
KnitVersionedFile.get_record_stream now retries *and* fails correctly.
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)
3789.2.12 by John Arbash Meinel
iter_lines_added_or_present now retries.
655
        record = record_stream.next()
656
        self.assertEqual(('rev-3',), record.key)
657
        self.assertEqual([1, 1, 0], reload_counter)
3789.2.11 by John Arbash Meinel
KnitVersionedFile.get_record_stream now retries *and* fails correctly.
658
        # Now delete all pack files, and see that we raise the right error
659
        for trans, name in vf._access._indices.itervalues():
660
            trans.delete(name)
661
        self.assertListRaises(errors.NoSuchFile,
662
            vf.get_record_stream, keys, 'topological', False)
663
3789.2.12 by John Arbash Meinel
iter_lines_added_or_present now retries.
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
673
        # duplicated
674
        count = 0
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():
684
            trans.delete(name)
685
        self.assertListRaises(errors.NoSuchFile,
686
            vf.iter_lines_added_or_present_in_keys, keys)
687
        self.assertEqual([2, 1, 1], reload_counter)
688
3878.1.1 by John Arbash Meinel
KVF.get_record_stream('unordered') now returns the records based on I/O ordering.
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')
693
        repo.lock_write()
694
        self.addCleanup(repo.unlock)
695
        repo.start_write_group()
696
        vf = repo.texts
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
702
        # the same order
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
714
        # the request
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))
726
2592.3.66 by Robert Collins
Allow adaption of KnitData to pack files.
727
2329.1.1 by John Arbash Meinel
Update _KnitData parser to raise more helpful errors when it detects corruption.
728
class LowLevelKnitDataTests(TestCase):
729
730
    def create_gz_content(self, text):
731
        sio = StringIO()
5590.1.1 by John Arbash Meinel
Stop using tuned_gzip, it seems to give incorrect results on python 2.7
732
        gz_file = gzip.GzipFile(mode='wb', fileobj=sio)
2329.1.1 by John Arbash Meinel
Update _KnitData parser to raise more helpful errors when it detects corruption.
733
        gz_file.write(text)
734
        gz_file.close()
735
        return sio.getvalue()
736
3789.2.4 by John Arbash Meinel
Add a multiple-record test, though it isn't quite what we want for the readv tests.
737
    def make_multiple_records(self):
738
        """Create the content for multiple records."""
5849.1.1 by Jelmer Vernooij
Use osutils.sha_string() when possible.
739
        sha1sum = osutils.sha_string('foo\nbar\n')
3789.2.4 by John Arbash Meinel
Add a multiple-record test, though it isn't quite what we want for the readv tests.
740
        total_txt = []
741
        gz_txt = self.create_gz_content('version rev-id-1 2 %s\n'
742
                                        'foo\n'
743
                                        'bar\n'
744
                                        'end rev-id-1\n'
745
                                        % (sha1sum,))
746
        record_1 = (0, len(gz_txt), sha1sum)
747
        total_txt.append(gz_txt)
5849.1.1 by Jelmer Vernooij
Use osutils.sha_string() when possible.
748
        sha1sum = osutils.sha_string('baz\n')
3789.2.4 by John Arbash Meinel
Add a multiple-record test, though it isn't quite what we want for the readv tests.
749
        gz_txt = self.create_gz_content('version rev-id-2 1 %s\n'
750
                                        'baz\n'
751
                                        'end rev-id-2\n'
752
                                        % (sha1sum,))
753
        record_2 = (record_1[1], len(gz_txt), sha1sum)
754
        total_txt.append(gz_txt)
755
        return total_txt, record_1, record_2
756
2329.1.1 by John Arbash Meinel
Update _KnitData parser to raise more helpful errors when it detects corruption.
757
    def test_valid_knit_data(self):
5849.1.1 by Jelmer Vernooij
Use osutils.sha_string() when possible.
758
        sha1sum = osutils.sha_string('foo\nbar\n')
2329.1.1 by John Arbash Meinel
Update _KnitData parser to raise more helpful errors when it detects corruption.
759
        gz_txt = self.create_gz_content('version rev-id-1 2 %s\n'
760
                                        'foo\n'
761
                                        'bar\n'
762
                                        'end rev-id-1\n'
763
                                        % (sha1sum,))
764
        transport = MockTransport([gz_txt])
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
765
        access = _KnitKeyAccess(transport, ConstantMapper('filename'))
766
        knit = KnitVersionedFiles(None, access)
767
        records = [(('rev-id-1',), (('rev-id-1',), 0, len(gz_txt)))]
768
769
        contents = list(knit._read_records_iter(records))
770
        self.assertEqual([(('rev-id-1',), ['foo\n', 'bar\n'],
771
            '4e48e2c9a3d2ca8a708cb0cc545700544efb5021')], contents)
772
773
        raw_contents = list(knit._read_records_iter_raw(records))
774
        self.assertEqual([(('rev-id-1',), gz_txt, sha1sum)], raw_contents)
2329.1.1 by John Arbash Meinel
Update _KnitData parser to raise more helpful errors when it detects corruption.
775
3789.2.4 by John Arbash Meinel
Add a multiple-record test, though it isn't quite what we want for the readv tests.
776
    def test_multiple_records_valid(self):
777
        total_txt, record_1, record_2 = self.make_multiple_records()
778
        transport = MockTransport([''.join(total_txt)])
779
        access = _KnitKeyAccess(transport, ConstantMapper('filename'))
780
        knit = KnitVersionedFiles(None, access)
781
        records = [(('rev-id-1',), (('rev-id-1',), record_1[0], record_1[1])),
782
                   (('rev-id-2',), (('rev-id-2',), record_2[0], record_2[1]))]
783
784
        contents = list(knit._read_records_iter(records))
785
        self.assertEqual([(('rev-id-1',), ['foo\n', 'bar\n'], record_1[2]),
786
                          (('rev-id-2',), ['baz\n'], record_2[2])],
787
                         contents)
788
789
        raw_contents = list(knit._read_records_iter_raw(records))
790
        self.assertEqual([(('rev-id-1',), total_txt[0], record_1[2]),
791
                          (('rev-id-2',), total_txt[1], record_2[2])],
792
                         raw_contents)
793
2329.1.1 by John Arbash Meinel
Update _KnitData parser to raise more helpful errors when it detects corruption.
794
    def test_not_enough_lines(self):
5849.1.1 by Jelmer Vernooij
Use osutils.sha_string() when possible.
795
        sha1sum = osutils.sha_string('foo\n')
2329.1.1 by John Arbash Meinel
Update _KnitData parser to raise more helpful errors when it detects corruption.
796
        # record says 2 lines data says 1
797
        gz_txt = self.create_gz_content('version rev-id-1 2 %s\n'
798
                                        'foo\n'
799
                                        'end rev-id-1\n'
800
                                        % (sha1sum,))
801
        transport = MockTransport([gz_txt])
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
802
        access = _KnitKeyAccess(transport, ConstantMapper('filename'))
803
        knit = KnitVersionedFiles(None, access)
804
        records = [(('rev-id-1',), (('rev-id-1',), 0, len(gz_txt)))]
805
        self.assertRaises(errors.KnitCorrupt, list,
806
            knit._read_records_iter(records))
2329.1.1 by John Arbash Meinel
Update _KnitData parser to raise more helpful errors when it detects corruption.
807
808
        # read_records_iter_raw won't detect that sort of mismatch/corruption
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
809
        raw_contents = list(knit._read_records_iter_raw(records))
810
        self.assertEqual([(('rev-id-1',),  gz_txt, sha1sum)], raw_contents)
2329.1.1 by John Arbash Meinel
Update _KnitData parser to raise more helpful errors when it detects corruption.
811
812
    def test_too_many_lines(self):
5849.1.1 by Jelmer Vernooij
Use osutils.sha_string() when possible.
813
        sha1sum = osutils.sha_string('foo\nbar\n')
2329.1.1 by John Arbash Meinel
Update _KnitData parser to raise more helpful errors when it detects corruption.
814
        # record says 1 lines data says 2
815
        gz_txt = self.create_gz_content('version rev-id-1 1 %s\n'
816
                                        'foo\n'
817
                                        'bar\n'
818
                                        'end rev-id-1\n'
819
                                        % (sha1sum,))
820
        transport = MockTransport([gz_txt])
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
821
        access = _KnitKeyAccess(transport, ConstantMapper('filename'))
822
        knit = KnitVersionedFiles(None, access)
823
        records = [(('rev-id-1',), (('rev-id-1',), 0, len(gz_txt)))]
824
        self.assertRaises(errors.KnitCorrupt, list,
825
            knit._read_records_iter(records))
2329.1.1 by John Arbash Meinel
Update _KnitData parser to raise more helpful errors when it detects corruption.
826
827
        # read_records_iter_raw won't detect that sort of mismatch/corruption
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
828
        raw_contents = list(knit._read_records_iter_raw(records))
829
        self.assertEqual([(('rev-id-1',), gz_txt, sha1sum)], raw_contents)
2329.1.1 by John Arbash Meinel
Update _KnitData parser to raise more helpful errors when it detects corruption.
830
831
    def test_mismatched_version_id(self):
5849.1.1 by Jelmer Vernooij
Use osutils.sha_string() when possible.
832
        sha1sum = osutils.sha_string('foo\nbar\n')
2329.1.1 by John Arbash Meinel
Update _KnitData parser to raise more helpful errors when it detects corruption.
833
        gz_txt = self.create_gz_content('version rev-id-1 2 %s\n'
834
                                        'foo\n'
835
                                        'bar\n'
836
                                        'end rev-id-1\n'
837
                                        % (sha1sum,))
838
        transport = MockTransport([gz_txt])
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
839
        access = _KnitKeyAccess(transport, ConstantMapper('filename'))
840
        knit = KnitVersionedFiles(None, access)
2329.1.1 by John Arbash Meinel
Update _KnitData parser to raise more helpful errors when it detects corruption.
841
        # We are asking for rev-id-2, but the data is rev-id-1
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
842
        records = [(('rev-id-2',), (('rev-id-2',), 0, len(gz_txt)))]
843
        self.assertRaises(errors.KnitCorrupt, list,
844
            knit._read_records_iter(records))
2329.1.1 by John Arbash Meinel
Update _KnitData parser to raise more helpful errors when it detects corruption.
845
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
846
        # read_records_iter_raw detects mismatches in the header
2329.1.1 by John Arbash Meinel
Update _KnitData parser to raise more helpful errors when it detects corruption.
847
        self.assertRaises(errors.KnitCorrupt, list,
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
848
            knit._read_records_iter_raw(records))
2329.1.1 by John Arbash Meinel
Update _KnitData parser to raise more helpful errors when it detects corruption.
849
850
    def test_uncompressed_data(self):
5849.1.1 by Jelmer Vernooij
Use osutils.sha_string() when possible.
851
        sha1sum = osutils.sha_string('foo\nbar\n')
2329.1.1 by John Arbash Meinel
Update _KnitData parser to raise more helpful errors when it detects corruption.
852
        txt = ('version rev-id-1 2 %s\n'
853
               'foo\n'
854
               'bar\n'
855
               'end rev-id-1\n'
856
               % (sha1sum,))
857
        transport = MockTransport([txt])
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
858
        access = _KnitKeyAccess(transport, ConstantMapper('filename'))
859
        knit = KnitVersionedFiles(None, access)
860
        records = [(('rev-id-1',), (('rev-id-1',), 0, len(txt)))]
2329.1.1 by John Arbash Meinel
Update _KnitData parser to raise more helpful errors when it detects corruption.
861
862
        # We don't have valid gzip data ==> corrupt
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
863
        self.assertRaises(errors.KnitCorrupt, list,
864
            knit._read_records_iter(records))
2329.1.1 by John Arbash Meinel
Update _KnitData parser to raise more helpful errors when it detects corruption.
865
866
        # read_records_iter_raw will notice the bad data
867
        self.assertRaises(errors.KnitCorrupt, list,
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
868
            knit._read_records_iter_raw(records))
2329.1.1 by John Arbash Meinel
Update _KnitData parser to raise more helpful errors when it detects corruption.
869
870
    def test_corrupted_data(self):
5849.1.1 by Jelmer Vernooij
Use osutils.sha_string() when possible.
871
        sha1sum = osutils.sha_string('foo\nbar\n')
2329.1.1 by John Arbash Meinel
Update _KnitData parser to raise more helpful errors when it detects corruption.
872
        gz_txt = self.create_gz_content('version rev-id-1 2 %s\n'
873
                                        'foo\n'
874
                                        'bar\n'
875
                                        'end rev-id-1\n'
876
                                        % (sha1sum,))
877
        # Change 2 bytes in the middle to \xff
878
        gz_txt = gz_txt[:10] + '\xff\xff' + gz_txt[12:]
879
        transport = MockTransport([gz_txt])
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
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))
2329.1.1 by John Arbash Meinel
Update _KnitData parser to raise more helpful errors when it detects corruption.
888
889
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
890
class LowLevelKnitIndexTests(TestCase):
891
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
892
    def get_knit_index(self, transport, name, mode):
893
        mapper = ConstantMapper(name)
2484.1.12 by John Arbash Meinel
Switch the layout to use a matching _knit_load_data_py.py and _knit_load_data_c.pyx
894
        from bzrlib._knit_load_data_py import _load_data_py
4985.1.5 by Vincent Ladeuil
Deploying the new overrideAttr facility further reduces the complexity
895
        self.overrideAttr(knit, '_load_data', _load_data_py)
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
896
        allow_writes = lambda: 'w' in mode
897
        return _KndxIndex(transport, mapper, lambda:None, allow_writes, lambda:True)
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
898
899
    def test_create_file(self):
900
        transport = MockTransport()
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
901
        index = self.get_knit_index(transport, "filename", "w")
902
        index.keys()
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])
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
911
912
    def test_read_utf8_version_id(self):
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
913
        unicode_revision_id = u"version-\N{CYRILLIC CAPITAL LETTER A}"
914
        utf8_revision_id = unicode_revision_id.encode('utf-8')
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
915
        transport = MockTransport([
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
916
            _KndxIndex.HEADER,
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
917
            '%s option 0 1 :' % (utf8_revision_id,)
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
918
            ])
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
919
        index = self.get_knit_index(transport, "filename", "r")
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
920
        # _KndxIndex is a private class, and deals in utf8 revision_ids, not
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
921
        # Unicode revision_ids.
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
922
        self.assertEqual({(utf8_revision_id,):()},
923
            index.get_parent_map(index.keys()))
924
        self.assertFalse((unicode_revision_id,) in index.keys())
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
925
926
    def test_read_utf8_parents(self):
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
927
        unicode_revision_id = u"version-\N{CYRILLIC CAPITAL LETTER A}"
928
        utf8_revision_id = unicode_revision_id.encode('utf-8')
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
929
        transport = MockTransport([
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
930
            _KndxIndex.HEADER,
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
931
            "version option 0 1 .%s :" % (utf8_revision_id,)
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
932
            ])
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
933
        index = self.get_knit_index(transport, "filename", "r")
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
934
        self.assertEqual({("version",):((utf8_revision_id,),)},
935
            index.get_parent_map(index.keys()))
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
936
937
    def test_read_ignore_corrupted_lines(self):
938
        transport = MockTransport([
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
939
            _KndxIndex.HEADER,
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
940
            "corrupted",
941
            "corrupted options 0 1 .b .c ",
942
            "version options 0 1 :"
943
            ])
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
944
        index = self.get_knit_index(transport, "filename", "r")
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
945
        self.assertEqual(1, len(index.keys()))
946
        self.assertEqual(set([("version",)]), index.keys())
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
947
948
    def test_read_corrupted_header(self):
2196.2.3 by John Arbash Meinel
Update tests and code to pass after merging bzr.dev
949
        transport = MockTransport(['not a bzr knit index header\n'])
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
950
        index = self.get_knit_index(transport, "filename", "r")
951
        self.assertRaises(KnitHeaderError, index.keys)
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
952
953
    def test_read_duplicate_entries(self):
954
        transport = MockTransport([
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
955
            _KndxIndex.HEADER,
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
956
            "parent options 0 1 :",
957
            "version options1 0 1 0 :",
958
            "version options2 1 2 .other :",
959
            "version options3 3 4 0 .other :"
960
            ])
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
961
        index = self.get_knit_index(transport, "filename", "r")
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
962
        self.assertEqual(2, len(index.keys()))
2592.3.8 by Robert Collins
Remove unneeded pulib method lookup on private class _KnitIndex.
963
        # check that the index used is the first one written. (Specific
964
        # to KnitIndex style indices.
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
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",)]))
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
970
971
    def test_read_compressed_parents(self):
972
        transport = MockTransport([
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
973
            _KndxIndex.HEADER,
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
974
            "a option 0 1 :",
975
            "b option 0 1 0 :",
976
            "c option 0 1 1 0 :",
977
            ])
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
978
        index = self.get_knit_index(transport, "filename", "r")
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
979
        self.assertEqual({("b",):(("a",),), ("c",):(("b",), ("a",))},
980
            index.get_parent_map([("b",), ("c",)]))
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
981
982
    def test_write_utf8_version_id(self):
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
983
        unicode_revision_id = u"version-\N{CYRILLIC CAPITAL LETTER A}"
984
        utf8_revision_id = unicode_revision_id.encode('utf-8')
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
985
        transport = MockTransport([
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
986
            _KndxIndex.HEADER
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
987
            ])
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
988
        index = self.get_knit_index(transport, "filename", "r")
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
989
        index.add_records([
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])
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
1000
1001
    def test_write_utf8_parents(self):
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
1002
        unicode_revision_id = u"version-\N{CYRILLIC CAPITAL LETTER A}"
1003
        utf8_revision_id = unicode_revision_id.encode('utf-8')
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
1004
        transport = MockTransport([
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1005
            _KndxIndex.HEADER
1006
            ])
1007
        index = self.get_knit_index(transport, "filename", "r")
1008
        index.add_records([
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])
1019
1020
    def test_keys(self):
1021
        transport = MockTransport([
1022
            _KndxIndex.HEADER
1023
            ])
1024
        index = self.get_knit_index(transport, "filename", "r")
1025
1026
        self.assertEqual(set(), index.keys())
1027
1028
        index.add_records([(("a",), ["option"], (("a",), 0, 1), [])])
1029
        self.assertEqual(set([("a",)]), index.keys())
1030
1031
        index.add_records([(("a",), ["option"], (("a",), 0, 1), [])])
1032
        self.assertEqual(set([("a",)]), index.keys())
1033
1034
        index.add_records([(("b",), ["option"], (("b",), 0, 1), [])])
1035
        self.assertEqual(set([("a",), ("b",)]), index.keys())
1036
1037
    def add_a_b(self, index, random_id=None):
1038
        kwargs = {}
1039
        if random_id is not None:
1040
            kwargs["random_id"] = random_id
1041
        index.add_records([
1042
            (("a",), ["option"], (("a",), 0, 1), [("b",)]),
1043
            (("a",), ["opt"], (("a",), 1, 2), [("c",)]),
1044
            (("b",), ["option"], (("b",), 2, 3), [("a",)])
1045
            ], **kwargs)
1046
1047
    def assertIndexIsAB(self, index):
1048
        self.assertEqual({
1049
            ('a',): (('c',),),
1050
            ('b',): (('a',),),
1051
            },
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",)))
1056
1057
    def test_add_versions(self):
1058
        transport = MockTransport([
1059
            _KndxIndex.HEADER
1060
            ])
1061
        index = self.get_knit_index(transport, "filename", "r")
1062
1063
        self.add_a_b(index)
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:
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
1069
        self.assertEqual(
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1070
            _KndxIndex.HEADER +
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
1071
            "\na option 0 1 .b :"
1072
            "\na opt 1 2 .c :"
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1073
            "\nb option 2 3 0 :",
1074
            call[1][1].getvalue())
1075
        self.assertEqual({'create_parent_dir': True}, call[2])
1076
        self.assertIndexIsAB(index)
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
1077
2841.2.1 by Robert Collins
* Commit no longer checks for new text keys during insertion when the
1078
    def test_add_versions_random_id_is_accepted(self):
1079
        transport = MockTransport([
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1080
            _KndxIndex.HEADER
2841.2.1 by Robert Collins
* Commit no longer checks for new text keys during insertion when the
1081
            ])
1082
        index = self.get_knit_index(transport, "filename", "r")
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1083
        self.add_a_b(index, random_id=True)
2841.2.1 by Robert Collins
* Commit no longer checks for new text keys during insertion when the
1084
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
1085
    def test_delay_create_and_add_versions(self):
1086
        transport = MockTransport()
1087
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1088
        index = self.get_knit_index(transport, "filename", "w")
1089
        # dir_mode=0777)
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
1090
        self.assertEqual([], transport.calls)
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1091
        self.add_a_b(index)
1092
        #self.assertEqual(
1093
        #[    {"dir_mode": 0777, "create_parent_dir": True, "mode": "wb"},
1094
        #    kwargs)
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:
1109
        self.assertEqual(
1110
            _KndxIndex.HEADER +
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
1111
            "\na option 0 1 .b :"
1112
            "\na opt 1 2 .c :"
1113
            "\nb option 2 3 0 :",
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1114
            call[1][1].getvalue())
1115
        self.assertEqual({'create_parent_dir': True}, call[2])
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
1116
4039.3.5 by John Arbash Meinel
Add direct tests for _get_total_build_size.
1117
    def assertTotalBuildSize(self, size, keys, positions):
1118
        self.assertEqual(size,
1119
                         knit._get_total_build_size(None, keys, positions))
1120
1121
    def test__get_total_build_size(self):
1122
        positions = {
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',)),
1127
            }
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)
1136
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
1137
    def test_get_position(self):
1138
        transport = MockTransport([
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1139
            _KndxIndex.HEADER,
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
1140
            "a option 0 1 :",
1141
            "b option 1 2 :"
1142
            ])
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
1143
        index = self.get_knit_index(transport, "filename", "r")
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
1144
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1145
        self.assertEqual((("a",), 0, 1), index.get_position(("a",)))
1146
        self.assertEqual((("b",), 1, 2), index.get_position(("b",)))
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
1147
1148
    def test_get_method(self):
1149
        transport = MockTransport([
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1150
            _KndxIndex.HEADER,
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
1151
            "a fulltext,unknown 0 1 :",
1152
            "b unknown,line-delta 1 2 :",
1153
            "c bad 3 4 :"
1154
            ])
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
1155
        index = self.get_knit_index(transport, "filename", "r")
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
1156
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
1157
        self.assertEqual("fulltext", index.get_method("a"))
1158
        self.assertEqual("line-delta", index.get_method("b"))
1159
        self.assertRaises(errors.KnitIndexUnknownMethod, index.get_method, "c")
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
1160
1161
    def test_get_options(self):
1162
        transport = MockTransport([
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1163
            _KndxIndex.HEADER,
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
1164
            "a opt1 0 1 :",
1165
            "b opt2,opt3 1 2 :"
1166
            ])
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
1167
        index = self.get_knit_index(transport, "filename", "r")
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
1168
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
1169
        self.assertEqual(["opt1"], index.get_options("a"))
1170
        self.assertEqual(["opt2", "opt3"], index.get_options("b"))
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
1171
3287.5.6 by Robert Collins
Remove _KnitIndex.get_parents.
1172
    def test_get_parent_map(self):
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
1173
        transport = MockTransport([
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1174
            _KndxIndex.HEADER,
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
1175
            "a option 0 1 :",
1176
            "b option 1 2 0 .c :",
1177
            "c option 1 2 1 0 .e :"
1178
            ])
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
1179
        index = self.get_knit_index(transport, "filename", "r")
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
1180
3287.5.6 by Robert Collins
Remove _KnitIndex.get_parents.
1181
        self.assertEqual({
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1182
            ("a",):(),
1183
            ("b",):(("a",), ("c",)),
1184
            ("c",):(("b",), ("a",), ("e",)),
1185
            }, index.get_parent_map(index.keys()))
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
1186
2484.1.13 by John Arbash Meinel
Add a test that KnitCorrupt is raised when parent strings are invalid.
1187
    def test_impossible_parent(self):
1188
        """Test we get KnitCorrupt if the parent couldn't possibly exist."""
1189
        transport = MockTransport([
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1190
            _KndxIndex.HEADER,
2484.1.13 by John Arbash Meinel
Add a test that KnitCorrupt is raised when parent strings are invalid.
1191
            "a option 0 1 :",
1192
            "b option 0 1 4 :"  # We don't have a 4th record
1193
            ])
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1194
        index = self.get_knit_index(transport, 'filename', 'r')
2484.1.17 by John Arbash Meinel
Workaround for Pyrex <0.9.5 and python >=2.5 incompatibilities.
1195
        try:
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1196
            self.assertRaises(errors.KnitCorrupt, index.keys)
2484.1.17 by John Arbash Meinel
Workaround for Pyrex <0.9.5 and python >=2.5 incompatibilities.
1197
        except TypeError, e:
1198
            if (str(e) == ('exceptions must be strings, classes, or instances,'
5848.2.1 by John Arbash Meinel
Break compatibility with python <2.6.
1199
                           ' not exceptions.IndexError')):
2484.1.17 by John Arbash Meinel
Workaround for Pyrex <0.9.5 and python >=2.5 incompatibilities.
1200
                self.knownFailure('Pyrex <0.9.5 fails with TypeError when'
1201
                                  ' raising new style exceptions with python'
1202
                                  ' >=2.5')
2484.1.19 by John Arbash Meinel
Don't suppress the TypeError if it doesn't match our requirements.
1203
            else:
1204
                raise
2484.1.13 by John Arbash Meinel
Add a test that KnitCorrupt is raised when parent strings are invalid.
1205
1206
    def test_corrupted_parent(self):
1207
        transport = MockTransport([
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1208
            _KndxIndex.HEADER,
2484.1.13 by John Arbash Meinel
Add a test that KnitCorrupt is raised when parent strings are invalid.
1209
            "a option 0 1 :",
1210
            "b option 0 1 :",
1211
            "c option 0 1 1v :", # Can't have a parent of '1v'
1212
            ])
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1213
        index = self.get_knit_index(transport, 'filename', 'r')
2484.1.17 by John Arbash Meinel
Workaround for Pyrex <0.9.5 and python >=2.5 incompatibilities.
1214
        try:
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1215
            self.assertRaises(errors.KnitCorrupt, index.keys)
2484.1.17 by John Arbash Meinel
Workaround for Pyrex <0.9.5 and python >=2.5 incompatibilities.
1216
        except TypeError, e:
1217
            if (str(e) == ('exceptions must be strings, classes, or instances,'
5848.2.1 by John Arbash Meinel
Break compatibility with python <2.6.
1218
                           ' not exceptions.ValueError')):
2484.1.17 by John Arbash Meinel
Workaround for Pyrex <0.9.5 and python >=2.5 incompatibilities.
1219
                self.knownFailure('Pyrex <0.9.5 fails with TypeError when'
1220
                                  ' raising new style exceptions with python'
1221
                                  ' >=2.5')
2484.1.19 by John Arbash Meinel
Don't suppress the TypeError if it doesn't match our requirements.
1222
            else:
1223
                raise
2484.1.13 by John Arbash Meinel
Add a test that KnitCorrupt is raised when parent strings are invalid.
1224
1225
    def test_corrupted_parent_in_list(self):
1226
        transport = MockTransport([
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1227
            _KndxIndex.HEADER,
2484.1.13 by John Arbash Meinel
Add a test that KnitCorrupt is raised when parent strings are invalid.
1228
            "a option 0 1 :",
1229
            "b option 0 1 :",
2484.1.17 by John Arbash Meinel
Workaround for Pyrex <0.9.5 and python >=2.5 incompatibilities.
1230
            "c option 0 1 1 v :", # Can't have a parent of 'v'
2484.1.13 by John Arbash Meinel
Add a test that KnitCorrupt is raised when parent strings are invalid.
1231
            ])
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1232
        index = self.get_knit_index(transport, 'filename', 'r')
2484.1.17 by John Arbash Meinel
Workaround for Pyrex <0.9.5 and python >=2.5 incompatibilities.
1233
        try:
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1234
            self.assertRaises(errors.KnitCorrupt, index.keys)
2484.1.17 by John Arbash Meinel
Workaround for Pyrex <0.9.5 and python >=2.5 incompatibilities.
1235
        except TypeError, e:
1236
            if (str(e) == ('exceptions must be strings, classes, or instances,'
5848.2.1 by John Arbash Meinel
Break compatibility with python <2.6.
1237
                           ' not exceptions.ValueError')):
2484.1.17 by John Arbash Meinel
Workaround for Pyrex <0.9.5 and python >=2.5 incompatibilities.
1238
                self.knownFailure('Pyrex <0.9.5 fails with TypeError when'
1239
                                  ' raising new style exceptions with python'
1240
                                  ' >=2.5')
2484.1.19 by John Arbash Meinel
Don't suppress the TypeError if it doesn't match our requirements.
1241
            else:
1242
                raise
2484.1.13 by John Arbash Meinel
Add a test that KnitCorrupt is raised when parent strings are invalid.
1243
2484.1.18 by John Arbash Meinel
Test that we properly verify the size and position strings.
1244
    def test_invalid_position(self):
1245
        transport = MockTransport([
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1246
            _KndxIndex.HEADER,
2484.1.18 by John Arbash Meinel
Test that we properly verify the size and position strings.
1247
            "a option 1v 1 :",
1248
            ])
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1249
        index = self.get_knit_index(transport, 'filename', 'r')
2484.1.18 by John Arbash Meinel
Test that we properly verify the size and position strings.
1250
        try:
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1251
            self.assertRaises(errors.KnitCorrupt, index.keys)
2484.1.18 by John Arbash Meinel
Test that we properly verify the size and position strings.
1252
        except TypeError, e:
1253
            if (str(e) == ('exceptions must be strings, classes, or instances,'
5848.2.1 by John Arbash Meinel
Break compatibility with python <2.6.
1254
                           ' not exceptions.ValueError')):
2484.1.18 by John Arbash Meinel
Test that we properly verify the size and position strings.
1255
                self.knownFailure('Pyrex <0.9.5 fails with TypeError when'
1256
                                  ' raising new style exceptions with python'
1257
                                  ' >=2.5')
2484.1.19 by John Arbash Meinel
Don't suppress the TypeError if it doesn't match our requirements.
1258
            else:
1259
                raise
2484.1.18 by John Arbash Meinel
Test that we properly verify the size and position strings.
1260
1261
    def test_invalid_size(self):
1262
        transport = MockTransport([
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1263
            _KndxIndex.HEADER,
2484.1.18 by John Arbash Meinel
Test that we properly verify the size and position strings.
1264
            "a option 1 1v :",
1265
            ])
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1266
        index = self.get_knit_index(transport, 'filename', 'r')
2484.1.18 by John Arbash Meinel
Test that we properly verify the size and position strings.
1267
        try:
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1268
            self.assertRaises(errors.KnitCorrupt, index.keys)
2484.1.18 by John Arbash Meinel
Test that we properly verify the size and position strings.
1269
        except TypeError, e:
1270
            if (str(e) == ('exceptions must be strings, classes, or instances,'
5848.2.1 by John Arbash Meinel
Break compatibility with python <2.6.
1271
                           ' not exceptions.ValueError')):
2484.1.18 by John Arbash Meinel
Test that we properly verify the size and position strings.
1272
                self.knownFailure('Pyrex <0.9.5 fails with TypeError when'
1273
                                  ' raising new style exceptions with python'
1274
                                  ' >=2.5')
2484.1.19 by John Arbash Meinel
Don't suppress the TypeError if it doesn't match our requirements.
1275
            else:
1276
                raise
2484.1.18 by John Arbash Meinel
Test that we properly verify the size and position strings.
1277
4011.5.7 by Andrew Bennetts
Remove leading underscore from _scan_unvalidate_index, explicitly NotImplementedError it for _KndxIndex.
1278
    def test_scan_unvalidated_index_not_implemented(self):
1279
        transport = MockTransport()
1280
        index = self.get_knit_index(transport, 'filename', 'r')
1281
        self.assertRaises(
1282
            NotImplementedError, index.scan_unvalidated_index,
1283
            'dummy graph_index')
4011.5.11 by Robert Collins
Polish the KnitVersionedFiles.scan_unvalidated_index api.
1284
        self.assertRaises(
1285
            NotImplementedError, index.get_missing_compression_parents)
4011.5.7 by Andrew Bennetts
Remove leading underscore from _scan_unvalidate_index, explicitly NotImplementedError it for _KndxIndex.
1286
2484.1.24 by John Arbash Meinel
Add direct tests of how we handle incomplete/'broken' lines
1287
    def test_short_line(self):
1288
        transport = MockTransport([
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1289
            _KndxIndex.HEADER,
2484.1.24 by John Arbash Meinel
Add direct tests of how we handle incomplete/'broken' lines
1290
            "a option 0 10  :",
1291
            "b option 10 10 0", # This line isn't terminated, ignored
1292
            ])
1293
        index = self.get_knit_index(transport, "filename", "r")
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1294
        self.assertEqual(set([('a',)]), index.keys())
2484.1.24 by John Arbash Meinel
Add direct tests of how we handle incomplete/'broken' lines
1295
1296
    def test_skip_incomplete_record(self):
1297
        # A line with bogus data should just be skipped
1298
        transport = MockTransport([
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1299
            _KndxIndex.HEADER,
2484.1.24 by John Arbash Meinel
Add direct tests of how we handle incomplete/'broken' lines
1300
            "a option 0 10  :",
1301
            "b option 10 10 0", # This line isn't terminated, ignored
1302
            "c option 20 10 0 :", # Properly terminated, and starts with '\n'
1303
            ])
1304
        index = self.get_knit_index(transport, "filename", "r")
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1305
        self.assertEqual(set([('a',), ('c',)]), index.keys())
2484.1.24 by John Arbash Meinel
Add direct tests of how we handle incomplete/'broken' lines
1306
1307
    def test_trailing_characters(self):
1308
        # A line with bogus data should just be skipped
1309
        transport = MockTransport([
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1310
            _KndxIndex.HEADER,
2484.1.24 by John Arbash Meinel
Add direct tests of how we handle incomplete/'broken' lines
1311
            "a option 0 10  :",
1312
            "b option 10 10 0 :a", # This line has extra trailing characters
1313
            "c option 20 10 0 :", # Properly terminated, and starts with '\n'
1314
            ])
1315
        index = self.get_knit_index(transport, "filename", "r")
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1316
        self.assertEqual(set([('a',), ('c',)]), index.keys())
2484.1.24 by John Arbash Meinel
Add direct tests of how we handle incomplete/'broken' lines
1317
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
1318
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
1319
class LowLevelKnitIndexTests_c(LowLevelKnitIndexTests):
1320
4913.2.20 by John Arbash Meinel
Change all of the compiled_foo to compiled_foo_feature
1321
    _test_needs_features = [compiled_knit_feature]
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
1322
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1323
    def get_knit_index(self, transport, name, mode):
1324
        mapper = ConstantMapper(name)
4573.1.1 by Andrew Bennetts
Fix imports for _knit_load_data_pyx, which was recently renamed.
1325
        from bzrlib._knit_load_data_pyx import _load_data_c
4985.1.5 by Vincent Ladeuil
Deploying the new overrideAttr facility further reduces the complexity
1326
        self.overrideAttr(knit, '_load_data', _load_data_c)
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1327
        allow_writes = lambda: mode == 'w'
4985.2.1 by Vincent Ladeuil
Deploy addAttrCleanup on the whole test suite.
1328
        return _KndxIndex(transport, mapper, lambda:None,
1329
                          allow_writes, lambda:True)
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
1330
1331
4454.3.28 by John Arbash Meinel
Continue breaking things to build it up cleanly.
1332
class Test_KnitAnnotator(TestCaseWithMemoryTransport):
1333
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)
1338
1339
    def test__expand_fulltext(self):
1340
        ann = self.make_annotator()
1341
        rev_key = ('rev-id',)
4454.3.36 by John Arbash Meinel
Only cache the content objects that we will reuse.
1342
        ann._num_compression_children[rev_key] = 1
4454.3.28 by John Arbash Meinel
Continue breaking things to build it up cleanly.
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])
1351
4454.3.30 by John Arbash Meinel
add a bit more work to be able to process 'pending_annotations'.
1352
    def test__expand_delta_comp_parent_not_available(self):
4454.3.28 by John Arbash Meinel
Continue breaking things to build it up cleanly.
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,
1361
                                 record, details)
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])
1367
4454.3.33 by John Arbash Meinel
Change the _expand_record code to pop out old content objects.
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,
1379
                                 record, details)
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,
1384
                                 record, details)
1385
        self.assertFalse(parent_key in ann._content_objects)
1386
        self.assertEqual({}, ann._num_compression_children)
4454.3.36 by John Arbash Meinel
Only cache the content objects that we will reuse.
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)
4454.3.33 by John Arbash Meinel
Change the _expand_record code to pop out old content objects.
1390
4454.3.37 by John Arbash Meinel
Add tests tha left-matching-blocks gets populated.
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)
4454.3.38 by John Arbash Meinel
Start using left-matching-blocks during the actual annotation.
1402
        self.assertEqual({(rev_key, parent_key): [(1, 1, 1), (3, 3, 0)]},
1403
                         ann._matching_blocks)
4454.3.37 by John Arbash Meinel
Add tests tha left-matching-blocks gets populated.
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)],
4454.3.38 by John Arbash Meinel
Start using left-matching-blocks during the actual annotation.
1409
                         ann._matching_blocks[(rev2_key, parent_key)])
1410
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)
4454.3.37 by John Arbash Meinel
Add tests tha left-matching-blocks gets populated.
1426
4454.3.31 by John Arbash Meinel
Change the processing lines to now handle fallbacks properly.
1427
    def test__process_pending(self):
4454.3.30 by John Arbash Meinel
add a bit more work to be able to process 'pending_annotations'.
1428
        ann = self.make_annotator()
1429
        rev_key = ('rev-id',)
1430
        p1_key = ('p1-id',)
1431
        p2_key = ('p2-id',)
1432
        record = ['0,1,1\n', 'new-line\n']
1433
        details = ('line-delta', False)
1434
        p1_record = ['line1\n', 'line2\n']
4454.3.33 by John Arbash Meinel
Change the _expand_record code to pop out old content objects.
1435
        ann._num_compression_children[p1_key] = 1
4454.3.30 by John Arbash Meinel
add a bit more work to be able to process 'pending_annotations'.
1436
        res = ann._expand_record(rev_key, (p1_key,p2_key), p1_key,
1437
                                 record, details)
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)
4454.3.31 by John Arbash Meinel
Change the processing lines to now handle fallbacks properly.
1447
        self.assertEqual([], res)
4454.3.30 by John Arbash Meinel
add a bit more work to be able to process 'pending_annotations'.
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))
4454.3.31 by John Arbash Meinel
Change the processing lines to now handle fallbacks properly.
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)
4454.3.30 by John Arbash Meinel
add a bit more work to be able to process 'pending_annotations'.
1459
4454.3.28 by John Arbash Meinel
Continue breaking things to build it up cleanly.
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
1465
4454.3.64 by John Arbash Meinel
Ensure that _KnitAnnotator also supports add_special_text.
1466
    def test_annotate_special_text(self):
1467
        ann = self.make_annotator()
1468
        vf = ann._vf
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',
1475
                                            'common content\n',
1476
                                            'content in 2\n'])
1477
        vf.add_lines(rev3_key, [rev1_key], ['initial content\n',
1478
                                            'common content\n',
1479
                                            'content in 3\n'])
1480
        spec_text = ('initial content\n'
1481
                     'common content\n'
1482
                     'content in 2\n'
1483
                     'content in 3\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),
1488
                          (rev2_key,),
1489
                          (rev3_key,),
1490
                         ], anns)
1491
        self.assertEqualDiff(spec_text, ''.join(lines))
1492
4454.3.28 by John Arbash Meinel
Continue breaking things to build it up cleanly.
1493
1684.3.3 by Robert Collins
Add a special cased weaves to knit converter.
1494
class KnitTests(TestCaseWithTransport):
1495
    """Class containing knit test helper routines."""
1563.2.16 by Robert Collins
Change WeaveStore into VersionedFileStore and make its versoined file class parameterisable.
1496
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1497
    def make_test_knit(self, annotate=False, name='test'):
1498
        mapper = ConstantMapper(name)
1499
        return make_file_factory(annotate, mapper)(self.get_transport())
1863.1.1 by John Arbash Meinel
Allow Versioned files to do caching if explicitly asked, and implement for Knit
1500
1501
3787.1.1 by Robert Collins
Embed the failed text in sha1 knit errors.
1502
class TestBadShaError(KnitTests):
1503
    """Tests for handling of sha errors."""
1504
4005.3.6 by Robert Collins
Support delta_closure=True with NetworkRecordStream to transmit deltas over the wire when full text extraction is required on the far end.
1505
    def test_sha_exception_has_text(self):
3787.1.1 by Robert Collins
Embed the failed text in sha1 knit errors.
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.")
1512
        # create a basis
1513
        basis = ('basis',)
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,
4005.3.6 by Robert Collins
Support delta_closure=True with NetworkRecordStream to transmit deltas over the wire when full text extraction is required on the far end.
1522
            target.get_record_stream([broken], 'unordered', True
1523
            ).next().get_bytes_as, 'chunked')
3787.1.1 by Robert Collins
Embed the failed text in sha1 knit errors.
1524
        self.assertEqual(['gam\n', 'bar\n'], err.content)
3787.1.2 by Robert Collins
Ensure SHA1KnitCorrupt formats ok.
1525
        # Test for formatting with live data
1526
        self.assertStartsWith(str(err), "Knit ")
3787.1.1 by Robert Collins
Embed the failed text in sha1 knit errors.
1527
1528
2102.2.1 by John Arbash Meinel
Fix bug #64789 _KnitIndex.add_versions() should dict compress new revisions
1529
class TestKnitIndex(KnitTests):
1530
1531
    def test_add_versions_dictionary_compresses(self):
1532
        """Adding versions to the index should update the lookup dict"""
1533
        knit = self.make_test_knit()
1534
        idx = knit._index
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1535
        idx.add_records([(('a-1',), ['fulltext'], (('a-1',), 0, 0), [])])
2102.2.1 by John Arbash Meinel
Fix bug #64789 _KnitIndex.add_versions() should dict compress new revisions
1536
        self.check_file_contents('test.kndx',
1537
            '# bzr knit index 8\n'
1538
            '\n'
1539
            'a-1 fulltext 0 0  :'
1540
            )
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1541
        idx.add_records([
1542
            (('a-2',), ['fulltext'], (('a-2',), 0, 0), [('a-1',)]),
1543
            (('a-3',), ['fulltext'], (('a-3',), 0, 0), [('a-2',)]),
1544
            ])
2102.2.1 by John Arbash Meinel
Fix bug #64789 _KnitIndex.add_versions() should dict compress new revisions
1545
        self.check_file_contents('test.kndx',
1546
            '# bzr knit index 8\n'
1547
            '\n'
1548
            'a-1 fulltext 0 0  :\n'
1549
            'a-2 fulltext 0 0 0 :\n'
1550
            'a-3 fulltext 0 0 1 :'
1551
            )
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1552
        self.assertEqual(set([('a-3',), ('a-1',), ('a-2',)]), idx.keys())
1553
        self.assertEqual({
1554
            ('a-1',): ((('a-1',), 0, 0), None, (), ('fulltext', False)),
1555
            ('a-2',): ((('a-2',), 0, 0), None, (('a-1',),), ('fulltext', False)),
1556
            ('a-3',): ((('a-3',), 0, 0), None, (('a-2',),), ('fulltext', False)),
1557
            }, idx.get_build_details(idx.keys()))
1558
        self.assertEqual({('a-1',):(),
1559
            ('a-2',):(('a-1',),),
1560
            ('a-3',):(('a-2',),),},
1561
            idx.get_parent_map(idx.keys()))
2102.2.1 by John Arbash Meinel
Fix bug #64789 _KnitIndex.add_versions() should dict compress new revisions
1562
1563
    def test_add_versions_fails_clean(self):
1564
        """If add_versions fails in the middle, it restores a pristine state.
1565
1566
        Any modifications that are made to the index are reset if all versions
1567
        cannot be added.
1568
        """
1569
        # This cheats a little bit by passing in a generator which will
1570
        # raise an exception before the processing finishes
1571
        # Other possibilities would be to have an version with the wrong number
1572
        # of entries, or to make the backing transport unable to write any
1573
        # files.
1574
1575
        knit = self.make_test_knit()
1576
        idx = knit._index
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1577
        idx.add_records([(('a-1',), ['fulltext'], (('a-1',), 0, 0), [])])
2102.2.1 by John Arbash Meinel
Fix bug #64789 _KnitIndex.add_versions() should dict compress new revisions
1578
1579
        class StopEarly(Exception):
1580
            pass
1581
1582
        def generate_failure():
1583
            """Add some entries and then raise an exception"""
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1584
            yield (('a-2',), ['fulltext'], (None, 0, 0), ('a-1',))
1585
            yield (('a-3',), ['fulltext'], (None, 0, 0), ('a-2',))
2102.2.1 by John Arbash Meinel
Fix bug #64789 _KnitIndex.add_versions() should dict compress new revisions
1586
            raise StopEarly()
1587
1588
        # Assert the pre-condition
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1589
        def assertA1Only():
1590
            self.assertEqual(set([('a-1',)]), set(idx.keys()))
1591
            self.assertEqual(
1592
                {('a-1',): ((('a-1',), 0, 0), None, (), ('fulltext', False))},
1593
                idx.get_build_details([('a-1',)]))
1594
            self.assertEqual({('a-1',):()}, idx.get_parent_map(idx.keys()))
1595
1596
        assertA1Only()
1597
        self.assertRaises(StopEarly, idx.add_records, generate_failure())
2102.2.1 by John Arbash Meinel
Fix bug #64789 _KnitIndex.add_versions() should dict compress new revisions
1598
        # And it shouldn't be modified
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1599
        assertA1Only()
2171.1.1 by John Arbash Meinel
Knit index files should ignore empty indexes rather than consider them corrupt.
1600
1601
    def test_knit_index_ignores_empty_files(self):
1602
        # There was a race condition in older bzr, where a ^C at the right time
1603
        # could leave an empty .kndx file, which bzr would later claim was a
1604
        # corrupted file since the header was not present. In reality, the file
1605
        # just wasn't created, so it should be ignored.
6083.1.1 by Jelmer Vernooij
Use get_transport_from_{url,path} in more places.
1606
        t = transport.get_transport_from_path('.')
2171.1.1 by John Arbash Meinel
Knit index files should ignore empty indexes rather than consider them corrupt.
1607
        t.put_bytes('test.kndx', '')
1608
1609
        knit = self.make_test_knit()
1610
1611
    def test_knit_index_checks_header(self):
6083.1.1 by Jelmer Vernooij
Use get_transport_from_{url,path} in more places.
1612
        t = transport.get_transport_from_path('.')
2171.1.1 by John Arbash Meinel
Knit index files should ignore empty indexes rather than consider them corrupt.
1613
        t.put_bytes('test.kndx', '# not really a knit header\n\n')
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1614
        k = self.make_test_knit()
1615
        self.assertRaises(KnitHeaderError, k.keys)
2592.3.2 by Robert Collins
Implement a get_graph for a new KnitGraphIndex that will implement a KnitIndex on top of the GraphIndex API.
1616
1617
1618
class TestGraphIndexKnit(KnitTests):
1619
    """Tests for knits using a GraphIndex rather than a KnitIndex."""
1620
1621
    def make_g_index(self, name, ref_lists=0, nodes=[]):
1622
        builder = GraphIndexBuilder(ref_lists)
1623
        for node, references, value in nodes:
1624
            builder.add_node(node, references, value)
1625
        stream = builder.finish()
1626
        trans = self.get_transport()
2890.2.1 by Robert Collins
* ``bzrlib.index.GraphIndex`` now requires a size parameter to the
1627
        size = trans.put_file(name, stream)
1628
        return GraphIndex(trans, name, size)
2592.3.2 by Robert Collins
Implement a get_graph for a new KnitGraphIndex that will implement a KnitIndex on top of the GraphIndex API.
1629
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
1630
    def two_graph_index(self, deltas=False, catch_adds=False):
2592.3.13 by Robert Collins
Implement KnitGraphIndex.get_method.
1631
        """Build a two-graph index.
1632
1633
        :param deltas: If true, use underlying indices with two node-ref
1634
            lists and 'parent' set to a delta-compressed against tail.
1635
        """
2592.3.2 by Robert Collins
Implement a get_graph for a new KnitGraphIndex that will implement a KnitIndex on top of the GraphIndex API.
1636
        # build a complex graph across several indices.
2592.3.13 by Robert Collins
Implement KnitGraphIndex.get_method.
1637
        if deltas:
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
1638
            # delta compression inn the index
2592.3.13 by Robert Collins
Implement KnitGraphIndex.get_method.
1639
            index1 = self.make_g_index('1', 2, [
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
1640
                (('tip', ), 'N0 100', ([('parent', )], [], )),
1641
                (('tail', ), '', ([], []))])
2592.3.13 by Robert Collins
Implement KnitGraphIndex.get_method.
1642
            index2 = self.make_g_index('2', 2, [
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
1643
                (('parent', ), ' 100 78', ([('tail', ), ('ghost', )], [('tail', )])),
1644
                (('separate', ), '', ([], []))])
2592.3.13 by Robert Collins
Implement KnitGraphIndex.get_method.
1645
        else:
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
1646
            # just blob location and graph in the index.
2592.3.13 by Robert Collins
Implement KnitGraphIndex.get_method.
1647
            index1 = self.make_g_index('1', 1, [
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
1648
                (('tip', ), 'N0 100', ([('parent', )], )),
1649
                (('tail', ), '', ([], ))])
2592.3.13 by Robert Collins
Implement KnitGraphIndex.get_method.
1650
            index2 = self.make_g_index('2', 1, [
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
1651
                (('parent', ), ' 100 78', ([('tail', ), ('ghost', )], )),
1652
                (('separate', ), '', ([], ))])
2592.3.2 by Robert Collins
Implement a get_graph for a new KnitGraphIndex that will implement a KnitIndex on top of the GraphIndex API.
1653
        combined_index = CombinedGraphIndex([index1, index2])
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
1654
        if catch_adds:
1655
            self.combined_index = combined_index
1656
            self.caught_entries = []
1657
            add_callback = self.catch_add
1658
        else:
1659
            add_callback = None
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1660
        return _KnitGraphIndex(combined_index, lambda:True, deltas=deltas,
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
1661
            add_callback=add_callback)
2592.3.4 by Robert Collins
Implement get_ancestry/get_ancestry_with_ghosts for KnitGraphIndex.
1662
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1663
    def test_keys(self):
1664
        index = self.two_graph_index()
1665
        self.assertEqual(set([('tail',), ('tip',), ('parent',), ('separate',)]),
1666
            set(index.keys()))
2592.3.9 by Robert Collins
Implement KnitGraphIndex.has_version.
1667
2592.3.10 by Robert Collins
Implement KnitGraphIndex.get_position.
1668
    def test_get_position(self):
1669
        index = self.two_graph_index()
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
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',)))
2592.3.10 by Robert Collins
Implement KnitGraphIndex.get_position.
1672
2592.3.13 by Robert Collins
Implement KnitGraphIndex.get_method.
1673
    def test_get_method_deltas(self):
1674
        index = self.two_graph_index(deltas=True)
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1675
        self.assertEqual('fulltext', index.get_method(('tip',)))
1676
        self.assertEqual('line-delta', index.get_method(('parent',)))
2592.3.11 by Robert Collins
Implement KnitGraphIndex.get_method.
1677
2592.3.13 by Robert Collins
Implement KnitGraphIndex.get_method.
1678
    def test_get_method_no_deltas(self):
1679
        # check that the parent-history lookup is ignored with deltas=False.
1680
        index = self.two_graph_index(deltas=False)
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1681
        self.assertEqual('fulltext', index.get_method(('tip',)))
1682
        self.assertEqual('fulltext', index.get_method(('parent',)))
2592.3.13 by Robert Collins
Implement KnitGraphIndex.get_method.
1683
2592.3.14 by Robert Collins
Implement KnitGraphIndex.get_options.
1684
    def test_get_options_deltas(self):
1685
        index = self.two_graph_index(deltas=True)
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1686
        self.assertEqual(['fulltext', 'no-eol'], index.get_options(('tip',)))
1687
        self.assertEqual(['line-delta'], index.get_options(('parent',)))
2592.3.14 by Robert Collins
Implement KnitGraphIndex.get_options.
1688
1689
    def test_get_options_no_deltas(self):
1690
        # check that the parent-history lookup is ignored with deltas=False.
1691
        index = self.two_graph_index(deltas=False)
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1692
        self.assertEqual(['fulltext', 'no-eol'], index.get_options(('tip',)))
1693
        self.assertEqual(['fulltext'], index.get_options(('parent',)))
1694
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',)]))
2592.3.14 by Robert Collins
Implement KnitGraphIndex.get_options.
1699
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
1700
    def catch_add(self, entries):
1701
        self.caught_entries.append(entries)
1702
1703
    def test_add_no_callback_errors(self):
1704
        index = self.two_graph_index()
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1705
        self.assertRaises(errors.ReadOnlyError, index.add_records,
1706
            [(('new',), 'fulltext,no-eol', (None, 50, 60), ['separate'])])
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
1707
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
1708
    def test_add_version_smoke(self):
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
1709
        index = self.two_graph_index(catch_adds=True)
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1710
        index.add_records([(('new',), 'fulltext,no-eol', (None, 50, 60),
1711
            [('separate',)])])
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
1712
        self.assertEqual([[(('new', ), 'N50 60', ((('separate',),),))]],
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
1713
            self.caught_entries)
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
1714
1715
    def test_add_version_delta_not_delta_index(self):
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
1716
        index = self.two_graph_index(catch_adds=True)
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1717
        self.assertRaises(errors.KnitCorrupt, index.add_records,
1718
            [(('new',), 'no-eol,line-delta', (None, 0, 100), [('parent',)])])
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
1719
        self.assertEqual([], self.caught_entries)
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
1720
1721
    def test_add_version_same_dup(self):
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
1722
        index = self.two_graph_index(catch_adds=True)
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
1723
        # options can be spelt two different ways
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
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),
1729
            [('parent',)])])
1730
        index.add_records([(('tip',), 'fulltext,no-eol', (None, 0, 1000),
1731
            [('parent',)])])
1732
        # but neither should have added data:
1733
        self.assertEqual([[], [], [], []], self.caught_entries)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1734
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
1735
    def test_add_version_different_dup(self):
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
1736
        index = self.two_graph_index(deltas=True, catch_adds=True)
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
1737
        # change options
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1738
        self.assertRaises(errors.KnitCorrupt, index.add_records,
3946.2.2 by Jelmer Vernooij
Remove matching test, fix handling of parentless indexes.
1739
            [(('tip',), 'line-delta', (None, 0, 100), [('parent',)])])
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1740
        self.assertRaises(errors.KnitCorrupt, index.add_records,
1741
            [(('tip',), 'fulltext', (None, 0, 100), [('parent',)])])
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
1742
        # parents
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1743
        self.assertRaises(errors.KnitCorrupt, index.add_records,
1744
            [(('tip',), 'fulltext,no-eol', (None, 0, 100), [])])
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
1745
        self.assertEqual([], self.caught_entries)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1746
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
1747
    def test_add_versions_nodeltas(self):
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
1748
        index = self.two_graph_index(catch_adds=True)
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1749
        index.add_records([
1750
                (('new',), 'fulltext,no-eol', (None, 50, 60), [('separate',)]),
1751
                (('new2',), 'fulltext', (None, 0, 6), [('new',)]),
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
1752
                ])
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
1753
        self.assertEqual([(('new', ), 'N50 60', ((('separate',),),)),
1754
            (('new2', ), ' 0 6', ((('new',),),))],
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
1755
            sorted(self.caught_entries[0]))
1756
        self.assertEqual(1, len(self.caught_entries))
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
1757
1758
    def test_add_versions_deltas(self):
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
1759
        index = self.two_graph_index(deltas=True, catch_adds=True)
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1760
        index.add_records([
1761
                (('new',), 'fulltext,no-eol', (None, 50, 60), [('separate',)]),
1762
                (('new2',), 'line-delta', (None, 0, 6), [('new',)]),
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
1763
                ])
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
1764
        self.assertEqual([(('new', ), 'N50 60', ((('separate',),), ())),
1765
            (('new2', ), ' 0 6', ((('new',),), (('new',),), ))],
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
1766
            sorted(self.caught_entries[0]))
1767
        self.assertEqual(1, len(self.caught_entries))
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
1768
1769
    def test_add_versions_delta_not_delta_index(self):
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
1770
        index = self.two_graph_index(catch_adds=True)
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1771
        self.assertRaises(errors.KnitCorrupt, index.add_records,
1772
            [(('new',), 'no-eol,line-delta', (None, 0, 100), [('parent',)])])
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
1773
        self.assertEqual([], self.caught_entries)
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
1774
2841.2.1 by Robert Collins
* Commit no longer checks for new text keys during insertion when the
1775
    def test_add_versions_random_id_accepted(self):
1776
        index = self.two_graph_index(catch_adds=True)
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1777
        index.add_records([], random_id=True)
2841.2.1 by Robert Collins
* Commit no longer checks for new text keys during insertion when the
1778
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
1779
    def test_add_versions_same_dup(self):
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
1780
        index = self.two_graph_index(catch_adds=True)
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
1781
        # options can be spelt two different ways
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1782
        index.add_records([(('tip',), 'fulltext,no-eol', (None, 0, 100),
1783
            [('parent',)])])
1784
        index.add_records([(('tip',), 'no-eol,fulltext', (None, 0, 100),
1785
            [('parent',)])])
1786
        # position/length are ignored (because each pack could have fulltext or
1787
        # delta, and be at a different position.
1788
        index.add_records([(('tip',), 'fulltext,no-eol', (None, 50, 100),
1789
            [('parent',)])])
1790
        index.add_records([(('tip',), 'fulltext,no-eol', (None, 0, 1000),
1791
            [('parent',)])])
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
1792
        # but neither should have added data.
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1793
        self.assertEqual([[], [], [], []], self.caught_entries)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1794
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
1795
    def test_add_versions_different_dup(self):
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
1796
        index = self.two_graph_index(deltas=True, catch_adds=True)
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
1797
        # change options
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1798
        self.assertRaises(errors.KnitCorrupt, index.add_records,
3946.2.2 by Jelmer Vernooij
Remove matching test, fix handling of parentless indexes.
1799
            [(('tip',), 'line-delta', (None, 0, 100), [('parent',)])])
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1800
        self.assertRaises(errors.KnitCorrupt, index.add_records,
1801
            [(('tip',), 'fulltext', (None, 0, 100), [('parent',)])])
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
1802
        # parents
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1803
        self.assertRaises(errors.KnitCorrupt, index.add_records,
1804
            [(('tip',), 'fulltext,no-eol', (None, 0, 100), [])])
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
1805
        # change options in the second record
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1806
        self.assertRaises(errors.KnitCorrupt, index.add_records,
1807
            [(('tip',), 'fulltext,no-eol', (None, 0, 100), [('parent',)]),
3946.2.2 by Jelmer Vernooij
Remove matching test, fix handling of parentless indexes.
1808
             (('tip',), 'line-delta', (None, 0, 100), [('parent',)])])
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
1809
        self.assertEqual([], self.caught_entries)
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
1810
4011.5.2 by Andrew Bennetts
Add more tests, improve existing tests, add GraphIndex._external_references()
1811
    def make_g_index_missing_compression_parent(self):
1812
        graph_index = self.make_g_index('missing_comp', 2,
1813
            [(('tip', ), ' 100 78',
1814
              ([('missing-parent', ), ('ghost', )], [('missing-parent', )]))])
1815
        return graph_index
4032.1.2 by John Arbash Meinel
Track down a few more files that have trailing whitespace.
1816
4257.4.14 by Andrew Bennetts
Add a unit test for _KnitGraphIndex.get_missing_parents, fix bug that it reveals.
1817
    def make_g_index_missing_parent(self):
1818
        graph_index = self.make_g_index('missing_parent', 2,
1819
            [(('parent', ), ' 100 78', ([], [])),
1820
             (('tip', ), ' 100 78',
1821
              ([('parent', ), ('missing-parent', )], [('parent', )])),
1822
              ])
1823
        return graph_index
1824
4011.5.2 by Andrew Bennetts
Add more tests, improve existing tests, add GraphIndex._external_references()
1825
    def make_g_index_no_external_refs(self):
1826
        graph_index = self.make_g_index('no_external_refs', 2,
1827
            [(('rev', ), ' 100 78',
1828
              ([('parent', ), ('ghost', )], []))])
1829
        return graph_index
1830
4011.5.1 by Andrew Bennetts
Start to add _add_unvalidated_index/get_missing_compression_parents methods to _KnitGraphIndex.
1831
    def test_add_good_unvalidated_index(self):
4011.5.2 by Andrew Bennetts
Add more tests, improve existing tests, add GraphIndex._external_references()
1832
        unvalidated = self.make_g_index_no_external_refs()
1833
        combined = CombinedGraphIndex([unvalidated])
4011.5.11 by Robert Collins
Polish the KnitVersionedFiles.scan_unvalidated_index api.
1834
        index = _KnitGraphIndex(combined, lambda: True, deltas=True)
4011.5.7 by Andrew Bennetts
Remove leading underscore from _scan_unvalidate_index, explicitly NotImplementedError it for _KndxIndex.
1835
        index.scan_unvalidated_index(unvalidated)
4011.5.1 by Andrew Bennetts
Start to add _add_unvalidated_index/get_missing_compression_parents methods to _KnitGraphIndex.
1836
        self.assertEqual(frozenset(), index.get_missing_compression_parents())
1837
4257.4.14 by Andrew Bennetts
Add a unit test for _KnitGraphIndex.get_missing_parents, fix bug that it reveals.
1838
    def test_add_missing_compression_parent_unvalidated_index(self):
4011.5.2 by Andrew Bennetts
Add more tests, improve existing tests, add GraphIndex._external_references()
1839
        unvalidated = self.make_g_index_missing_compression_parent()
1840
        combined = CombinedGraphIndex([unvalidated])
4011.5.11 by Robert Collins
Polish the KnitVersionedFiles.scan_unvalidated_index api.
1841
        index = _KnitGraphIndex(combined, lambda: True, deltas=True)
4011.5.7 by Andrew Bennetts
Remove leading underscore from _scan_unvalidate_index, explicitly NotImplementedError it for _KndxIndex.
1842
        index.scan_unvalidated_index(unvalidated)
4011.5.6 by Andrew Bennetts
Make sure it's not possible to commit a pack write group when any versioned file has missing compression parents.
1843
        # This also checks that its only the compression parent that is
1844
        # examined, otherwise 'ghost' would also be reported as a missing
1845
        # parent.
4011.5.1 by Andrew Bennetts
Start to add _add_unvalidated_index/get_missing_compression_parents methods to _KnitGraphIndex.
1846
        self.assertEqual(
4011.5.2 by Andrew Bennetts
Add more tests, improve existing tests, add GraphIndex._external_references()
1847
            frozenset([('missing-parent',)]),
1848
            index.get_missing_compression_parents())
4011.5.1 by Andrew Bennetts
Start to add _add_unvalidated_index/get_missing_compression_parents methods to _KnitGraphIndex.
1849
4257.4.14 by Andrew Bennetts
Add a unit test for _KnitGraphIndex.get_missing_parents, fix bug that it reveals.
1850
    def test_add_missing_noncompression_parent_unvalidated_index(self):
1851
        unvalidated = self.make_g_index_missing_parent()
1852
        combined = CombinedGraphIndex([unvalidated])
1853
        index = _KnitGraphIndex(combined, lambda: True, deltas=True,
1854
            track_external_parent_refs=True)
1855
        index.scan_unvalidated_index(unvalidated)
1856
        self.assertEqual(
1857
            frozenset([('missing-parent',)]), index.get_missing_parents())
1858
4257.4.15 by Andrew Bennetts
Add another test for _KnitGraphIndex.get_missing_parents().
1859
    def test_track_external_parent_refs(self):
1860
        g_index = self.make_g_index('empty', 2, [])
1861
        combined = CombinedGraphIndex([g_index])
1862
        index = _KnitGraphIndex(combined, lambda: True, deltas=True,
1863
            add_callback=self.catch_add, track_external_parent_refs=True)
1864
        self.caught_entries = []
1865
        index.add_records([
1866
            (('new-key',), 'fulltext,no-eol', (None, 50, 60),
1867
             [('parent-1',), ('parent-2',)])])
1868
        self.assertEqual(
1869
            frozenset([('parent-1',), ('parent-2',)]),
1870
            index.get_missing_parents())
1871
4011.5.1 by Andrew Bennetts
Start to add _add_unvalidated_index/get_missing_compression_parents methods to _KnitGraphIndex.
1872
    def test_add_unvalidated_index_with_present_external_references(self):
1873
        index = self.two_graph_index(deltas=True)
4011.5.10 by Andrew Bennetts
Replace XXX with better comment.
1874
        # Ugly hack to get at one of the underlying GraphIndex objects that
1875
        # two_graph_index built.
1876
        unvalidated = index._graph_index._indices[1]
1877
        # 'parent' is an external ref of _indices[1] (unvalidated), but is
1878
        # present in _indices[0].
4011.5.7 by Andrew Bennetts
Remove leading underscore from _scan_unvalidate_index, explicitly NotImplementedError it for _KndxIndex.
1879
        index.scan_unvalidated_index(unvalidated)
4011.5.1 by Andrew Bennetts
Start to add _add_unvalidated_index/get_missing_compression_parents methods to _KnitGraphIndex.
1880
        self.assertEqual(frozenset(), index.get_missing_compression_parents())
1881
4011.5.2 by Andrew Bennetts
Add more tests, improve existing tests, add GraphIndex._external_references()
1882
    def make_new_missing_parent_g_index(self, name):
1883
        missing_parent = name + '-missing-parent'
1884
        graph_index = self.make_g_index(name, 2,
1885
            [((name + 'tip', ), ' 100 78',
1886
              ([(missing_parent, ), ('ghost', )], [(missing_parent, )]))])
1887
        return graph_index
1888
1889
    def test_add_mulitiple_unvalidated_indices_with_missing_parents(self):
1890
        g_index_1 = self.make_new_missing_parent_g_index('one')
1891
        g_index_2 = self.make_new_missing_parent_g_index('two')
1892
        combined = CombinedGraphIndex([g_index_1, g_index_2])
4011.5.11 by Robert Collins
Polish the KnitVersionedFiles.scan_unvalidated_index api.
1893
        index = _KnitGraphIndex(combined, lambda: True, deltas=True)
4011.5.7 by Andrew Bennetts
Remove leading underscore from _scan_unvalidate_index, explicitly NotImplementedError it for _KndxIndex.
1894
        index.scan_unvalidated_index(g_index_1)
1895
        index.scan_unvalidated_index(g_index_2)
4011.5.2 by Andrew Bennetts
Add more tests, improve existing tests, add GraphIndex._external_references()
1896
        self.assertEqual(
1897
            frozenset([('one-missing-parent',), ('two-missing-parent',)]),
1898
            index.get_missing_compression_parents())
1899
1900
    def test_add_mulitiple_unvalidated_indices_with_mutual_dependencies(self):
1901
        graph_index_a = self.make_g_index('one', 2,
1902
            [(('parent-one', ), ' 100 78', ([('non-compression-parent',)], [])),
1903
             (('child-of-two', ), ' 100 78',
1904
              ([('parent-two',)], [('parent-two',)]))])
1905
        graph_index_b = self.make_g_index('two', 2,
1906
            [(('parent-two', ), ' 100 78', ([('non-compression-parent',)], [])),
1907
             (('child-of-one', ), ' 100 78',
1908
              ([('parent-one',)], [('parent-one',)]))])
1909
        combined = CombinedGraphIndex([graph_index_a, graph_index_b])
4011.5.11 by Robert Collins
Polish the KnitVersionedFiles.scan_unvalidated_index api.
1910
        index = _KnitGraphIndex(combined, lambda: True, deltas=True)
4011.5.7 by Andrew Bennetts
Remove leading underscore from _scan_unvalidate_index, explicitly NotImplementedError it for _KndxIndex.
1911
        index.scan_unvalidated_index(graph_index_a)
1912
        index.scan_unvalidated_index(graph_index_b)
4011.5.2 by Andrew Bennetts
Add more tests, improve existing tests, add GraphIndex._external_references()
1913
        self.assertEqual(
1914
            frozenset([]), index.get_missing_compression_parents())
4032.1.2 by John Arbash Meinel
Track down a few more files that have trailing whitespace.
1915
4011.5.2 by Andrew Bennetts
Add more tests, improve existing tests, add GraphIndex._external_references()
1916
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
1917
class TestNoParentsGraphIndexKnit(KnitTests):
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1918
    """Tests for knits using _KnitGraphIndex with no parents."""
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
1919
1920
    def make_g_index(self, name, ref_lists=0, nodes=[]):
1921
        builder = GraphIndexBuilder(ref_lists)
1922
        for node, references in nodes:
1923
            builder.add_node(node, references)
1924
        stream = builder.finish()
1925
        trans = self.get_transport()
2890.2.1 by Robert Collins
* ``bzrlib.index.GraphIndex`` now requires a size parameter to the
1926
        size = trans.put_file(name, stream)
1927
        return GraphIndex(trans, name, size)
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
1928
4011.5.11 by Robert Collins
Polish the KnitVersionedFiles.scan_unvalidated_index api.
1929
    def test_add_good_unvalidated_index(self):
1930
        unvalidated = self.make_g_index('unvalidated')
1931
        combined = CombinedGraphIndex([unvalidated])
1932
        index = _KnitGraphIndex(combined, lambda: True, parents=False)
1933
        index.scan_unvalidated_index(unvalidated)
1934
        self.assertEqual(frozenset(),
1935
            index.get_missing_compression_parents())
1936
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
1937
    def test_parents_deltas_incompatible(self):
1938
        index = CombinedGraphIndex([])
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1939
        self.assertRaises(errors.KnitError, _KnitGraphIndex, lambda:True,
1940
            index, deltas=True, parents=False)
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
1941
1942
    def two_graph_index(self, catch_adds=False):
1943
        """Build a two-graph index.
1944
1945
        :param deltas: If true, use underlying indices with two node-ref
1946
            lists and 'parent' set to a delta-compressed against tail.
1947
        """
1948
        # put several versions in the index.
1949
        index1 = self.make_g_index('1', 0, [
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
1950
            (('tip', ), 'N0 100'),
1951
            (('tail', ), '')])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
1952
        index2 = self.make_g_index('2', 0, [
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
1953
            (('parent', ), ' 100 78'),
1954
            (('separate', ), '')])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
1955
        combined_index = CombinedGraphIndex([index1, index2])
1956
        if catch_adds:
1957
            self.combined_index = combined_index
1958
            self.caught_entries = []
1959
            add_callback = self.catch_add
1960
        else:
1961
            add_callback = None
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1962
        return _KnitGraphIndex(combined_index, lambda:True, parents=False,
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
1963
            add_callback=add_callback)
1964
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1965
    def test_keys(self):
1966
        index = self.two_graph_index()
1967
        self.assertEqual(set([('tail',), ('tip',), ('parent',), ('separate',)]),
1968
            set(index.keys()))
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
1969
1970
    def test_get_position(self):
1971
        index = self.two_graph_index()
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1972
        self.assertEqual((index._graph_index._indices[0], 0, 100),
1973
            index.get_position(('tip',)))
1974
        self.assertEqual((index._graph_index._indices[1], 100, 78),
1975
            index.get_position(('parent',)))
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
1976
1977
    def test_get_method(self):
1978
        index = self.two_graph_index()
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1979
        self.assertEqual('fulltext', index.get_method(('tip',)))
1980
        self.assertEqual(['fulltext'], index.get_options(('parent',)))
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
1981
1982
    def test_get_options(self):
1983
        index = self.two_graph_index()
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1984
        self.assertEqual(['fulltext', 'no-eol'], index.get_options(('tip',)))
1985
        self.assertEqual(['fulltext'], index.get_options(('parent',)))
1986
1987
    def test_get_parent_map(self):
1988
        index = self.two_graph_index()
1989
        self.assertEqual({('parent',):None},
1990
            index.get_parent_map([('parent',), ('ghost',)]))
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
1991
1992
    def catch_add(self, entries):
1993
        self.caught_entries.append(entries)
1994
1995
    def test_add_no_callback_errors(self):
1996
        index = self.two_graph_index()
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
1997
        self.assertRaises(errors.ReadOnlyError, index.add_records,
1998
            [(('new',), 'fulltext,no-eol', (None, 50, 60), [('separate',)])])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
1999
2000
    def test_add_version_smoke(self):
2001
        index = self.two_graph_index(catch_adds=True)
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
2002
        index.add_records([(('new',), 'fulltext,no-eol', (None, 50, 60), [])])
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2003
        self.assertEqual([[(('new', ), 'N50 60')]],
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2004
            self.caught_entries)
2005
2006
    def test_add_version_delta_not_delta_index(self):
2007
        index = self.two_graph_index(catch_adds=True)
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
2008
        self.assertRaises(errors.KnitCorrupt, index.add_records,
2009
            [(('new',), 'no-eol,line-delta', (None, 0, 100), [])])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2010
        self.assertEqual([], self.caught_entries)
2011
2012
    def test_add_version_same_dup(self):
2013
        index = self.two_graph_index(catch_adds=True)
2014
        # options can be spelt two different ways
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
2015
        index.add_records([(('tip',), 'fulltext,no-eol', (None, 0, 100), [])])
2016
        index.add_records([(('tip',), 'no-eol,fulltext', (None, 0, 100), [])])
2017
        # position/length are ignored (because each pack could have fulltext or
2018
        # delta, and be at a different position.
2019
        index.add_records([(('tip',), 'fulltext,no-eol', (None, 50, 100), [])])
2020
        index.add_records([(('tip',), 'fulltext,no-eol', (None, 0, 1000), [])])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2021
        # but neither should have added data.
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
2022
        self.assertEqual([[], [], [], []], self.caught_entries)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
2023
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2024
    def test_add_version_different_dup(self):
2025
        index = self.two_graph_index(catch_adds=True)
2026
        # change options
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
2027
        self.assertRaises(errors.KnitCorrupt, index.add_records,
2028
            [(('tip',), 'no-eol,line-delta', (None, 0, 100), [])])
2029
        self.assertRaises(errors.KnitCorrupt, index.add_records,
2030
            [(('tip',), 'line-delta,no-eol', (None, 0, 100), [])])
2031
        self.assertRaises(errors.KnitCorrupt, index.add_records,
2032
            [(('tip',), 'fulltext', (None, 0, 100), [])])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2033
        # parents
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
2034
        self.assertRaises(errors.KnitCorrupt, index.add_records,
2035
            [(('tip',), 'fulltext,no-eol', (None, 0, 100), [('parent',)])])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2036
        self.assertEqual([], self.caught_entries)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
2037
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2038
    def test_add_versions(self):
2039
        index = self.two_graph_index(catch_adds=True)
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
2040
        index.add_records([
2041
                (('new',), 'fulltext,no-eol', (None, 50, 60), []),
2042
                (('new2',), 'fulltext', (None, 0, 6), []),
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2043
                ])
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2044
        self.assertEqual([(('new', ), 'N50 60'), (('new2', ), ' 0 6')],
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2045
            sorted(self.caught_entries[0]))
2046
        self.assertEqual(1, len(self.caught_entries))
2047
2048
    def test_add_versions_delta_not_delta_index(self):
2049
        index = self.two_graph_index(catch_adds=True)
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
2050
        self.assertRaises(errors.KnitCorrupt, index.add_records,
2051
            [(('new',), 'no-eol,line-delta', (None, 0, 100), [('parent',)])])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2052
        self.assertEqual([], self.caught_entries)
2053
2054
    def test_add_versions_parents_not_parents_index(self):
2055
        index = self.two_graph_index(catch_adds=True)
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
2056
        self.assertRaises(errors.KnitCorrupt, index.add_records,
2057
            [(('new',), 'no-eol,fulltext', (None, 0, 100), [('parent',)])])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2058
        self.assertEqual([], self.caught_entries)
2059
2841.2.1 by Robert Collins
* Commit no longer checks for new text keys during insertion when the
2060
    def test_add_versions_random_id_accepted(self):
2061
        index = self.two_graph_index(catch_adds=True)
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
2062
        index.add_records([], random_id=True)
2841.2.1 by Robert Collins
* Commit no longer checks for new text keys during insertion when the
2063
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2064
    def test_add_versions_same_dup(self):
2065
        index = self.two_graph_index(catch_adds=True)
2066
        # options can be spelt two different ways
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
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), [])])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2073
        # but neither should have added data.
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
2074
        self.assertEqual([[], [], [], []], self.caught_entries)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
2075
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2076
    def test_add_versions_different_dup(self):
2077
        index = self.two_graph_index(catch_adds=True)
2078
        # change options
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
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), [])])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2085
        # parents
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
2086
        self.assertRaises(errors.KnitCorrupt, index.add_records,
2087
            [(('tip',), 'fulltext,no-eol', (None, 0, 100), [('parent',)])])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2088
        # change options in the second record
3350.6.4 by Robert Collins
First cut at pluralised VersionedFiles. Some rather massive API incompatabilities, primarily because of the difficulty of coherence among competing stores.
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), [])])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2092
        self.assertEqual([], self.caught_entries)
3350.8.1 by Robert Collins
KnitVersionedFiles.add_fallback_versioned_files exists.
2093
2094
4039.3.6 by John Arbash Meinel
Turn _split_by_prefix into a classmethod, and add direct tests.
2095
class TestKnitVersionedFiles(KnitTests):
2096
4039.3.7 by John Arbash Meinel
Some direct tests for _group_keys_for_io
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))
2105
4039.3.6 by John Arbash Meinel
Turn _split_by_prefix into a classmethod, and add direct tests.
2106
    def assertSplitByPrefix(self, expected_map, expected_prefix_order,
2107
                            keys):
2108
        split, prefix_order = KnitVersionedFiles._split_by_prefix(keys)
2109
        self.assertEqual(expected_map, split)
2110
        self.assertEqual(expected_prefix_order, prefix_order)
2111
4039.3.7 by John Arbash Meinel
Some direct tests for _group_keys_for_io
2112
    def test__group_keys_for_io(self):
2113
        ft_detail = ('fulltext', False)
2114
        ld_detail = ('line-delta', False)
2115
        f_a = ('f', 'a')
2116
        f_b = ('f', 'b')
2117
        f_c = ('f', 'c')
2118
        g_a = ('g', 'a')
2119
        g_b = ('g', 'b')
2120
        g_c = ('g', 'c')
2121
        positions = {
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),
2128
            }
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)
2151
4039.3.6 by John Arbash Meinel
Turn _split_by_prefix into a classmethod, and add direct tests.
2152
    def test__split_by_prefix(self):
2153
        self.assertSplitByPrefix({'f': [('f', 'a'), ('f', 'b')],
2154
                                  'g': [('g', 'b'), ('g', 'a')],
2155
                                 }, ['f', 'g'],
2156
                                 [('f', 'a'), ('g', 'b'),
2157
                                  ('g', 'a'), ('f', 'b')])
2158
2159
        self.assertSplitByPrefix({'f': [('f', 'a'), ('f', 'b')],
2160
                                  'g': [('g', 'b'), ('g', 'a')],
2161
                                 }, ['f', 'g'],
2162
                                 [('f', 'a'), ('f', 'b'),
2163
                                  ('g', 'b'), ('g', 'a')])
2164
2165
        self.assertSplitByPrefix({'f': [('f', 'a'), ('f', 'b')],
2166
                                  'g': [('g', 'b'), ('g', 'a')],
2167
                                 }, ['f', 'g'],
2168
                                 [('f', 'a'), ('f', 'b'),
2169
                                  ('g', 'b'), ('g', 'a')])
2170
2171
        self.assertSplitByPrefix({'f': [('f', 'a'), ('f', 'b')],
2172
                                  'g': [('g', 'b'), ('g', 'a')],
2173
                                  '': [('a',), ('b',)]
2174
                                 }, ['f', 'g', ''],
2175
                                 [('f', 'a'), ('g', 'b'),
2176
                                  ('a',), ('b',),
2177
                                  ('g', 'a'), ('f', 'b')])
2178
2179
3350.8.1 by Robert Collins
KnitVersionedFiles.add_fallback_versioned_files exists.
2180
class TestStacking(KnitTests):
2181
2182
    def get_basis_and_test_knit(self):
2183
        basis = self.make_test_knit(name='basis')
3350.8.2 by Robert Collins
stacked get_parent_map.
2184
        basis = RecordingVersionedFilesDecorator(basis)
3350.8.1 by Robert Collins
KnitVersionedFiles.add_fallback_versioned_files exists.
2185
        test = self.make_test_knit(name='test')
2186
        test.add_fallback_versioned_files(basis)
2187
        return basis, test
2188
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)
2195
2196
    def test_add_lines(self):
3350.8.9 by Robert Collins
define behaviour for add_lines with stacked storage.
2197
        # lines added to the test are not added to the basis
2198
        basis, test = self.get_basis_and_test_knit()
2199
        key = ('foo',)
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
2206
        # fulltext.
2207
        basis.add_lines(key_basis, (), ['foo\n'])
2208
        basis.calls = []
2209
        test.add_lines(key_cross_border, (key_basis,), ['foo\n'])
2210
        self.assertEqual('fulltext', test._index.get_method(key_cross_border))
3830.3.10 by Martin Pool
Update more stacking effort tests
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)
3350.8.9 by Robert Collins
define behaviour for add_lines with stacked storage.
2214
        # Subsequent adds do delta.
3350.8.14 by Robert Collins
Review feedback.
2215
        basis.calls = []
3350.8.9 by Robert Collins
define behaviour for add_lines with stacked storage.
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)
3350.8.1 by Robert Collins
KnitVersionedFiles.add_fallback_versioned_files exists.
2219
2220
    def test_annotate(self):
3350.8.8 by Robert Collins
Stacking and knits don't play nice for annotation yet.
2221
        # annotations from the test knit are answered without asking the basis
2222
        basis, test = self.get_basis_and_test_knit()
2223
        key = ('foo',)
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
2231
        # directly.
2232
        basis.add_lines(key_basis, (), ['foo\n', 'bar\n'])
2233
        basis.calls = []
2234
        details = test.annotate(key_basis)
2235
        self.assertEqual([(key_basis, 'foo\n'), (key_basis, 'bar\n')], details)
3350.9.1 by Robert Collins
Redo annotate more simply, using just the public interfaces for VersionedFiles.
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])),
4537.3.5 by John Arbash Meinel
Fix 3 tests that assumed it would use 'unordered' in the fallback,
2240
            ('get_record_stream', [key_basis], 'topological', True)],
3350.9.1 by Robert Collins
Redo annotate more simply, using just the public interfaces for VersionedFiles.
2241
            basis.calls)
3350.8.1 by Robert Collins
KnitVersionedFiles.add_fallback_versioned_files exists.
2242
2243
    def test_check(self):
3517.4.19 by Martin Pool
Update test for knit.check() to expect it to recurse into fallback vfs
2244
        # At the moment checking a stacked knit does implicitly check the
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
2245
        # fallback files.
3350.8.1 by Robert Collins
KnitVersionedFiles.add_fallback_versioned_files exists.
2246
        basis, test = self.get_basis_and_test_knit()
2247
        test.check()
2248
2249
    def test_get_parent_map(self):
3350.8.2 by Robert Collins
stacked get_parent_map.
2250
        # parents in the test knit are answered without asking the basis
2251
        basis, test = self.get_basis_and_test_knit()
2252
        key = ('foo',)
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, (), [])
2261
        basis.calls = []
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]))],
2266
            basis.calls)
3350.8.1 by Robert Collins
KnitVersionedFiles.add_fallback_versioned_files exists.
2267
3350.8.7 by Robert Collins
get_record_stream for fulltexts working (but note extreme memory use!).
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()
2271
        key = ('foo',)
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'])
2280
        basis.calls = []
2281
        records = list(test.get_record_stream([key_basis, key_missing],
2282
            'unordered', True))
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)
2289
            else:
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'))
3350.8.14 by Robert Collins
Review feedback.
2299
        # It's not strictly minimal, but it seems reasonable for now for it to
3350.8.7 by Robert Collins
get_record_stream for fulltexts working (but note extreme memory use!).
2300
        # ask which fallbacks have which parents.
2301
        self.assertEqual([
2302
            ("get_parent_map", set([key_basis, key_missing])),
2303
            ("get_record_stream", [key_basis], 'unordered', True)],
2304
            calls)
2305
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()
2309
        key = ('foo',)
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'])
2317
        basis.calls = []
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))
2322
        results = []
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)
2328
            else:
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:
2336
                source = test
2337
            else:
2338
                source = basis
2339
            record = source.get_record_stream([result[0]], 'unordered',
2340
                True).next()
2341
            self.assertEqual(record.key, result[0])
2342
            self.assertEqual(record.sha1, result[1])
4005.3.6 by Robert Collins
Support delta_closure=True with NetworkRecordStream to transmit deltas over the wire when full text extraction is required on the far end.
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.
3350.8.7 by Robert Collins
get_record_stream for fulltexts working (but note extreme memory use!).
2347
            self.assertEqual(record.get_bytes_as('fulltext'), result[3])
3350.8.14 by Robert Collins
Review feedback.
2348
        # It's not strictly minimal, but it seems reasonable for now for it to
3350.8.7 by Robert Collins
get_record_stream for fulltexts working (but note extreme memory use!).
2349
        # ask which fallbacks have which parents.
2350
        self.assertEqual([
2351
            ("get_parent_map", set([key_basis, key_basis_2, key_missing])),
4537.3.5 by John Arbash Meinel
Fix 3 tests that assumed it would use 'unordered' in the fallback,
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)],
3350.8.7 by Robert Collins
get_record_stream for fulltexts working (but note extreme memory use!).
2355
            calls)
2356
3350.8.6 by Robert Collins
get_record_stream stacking for delta access.
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()
2360
        key = ('foo',)
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'])
2369
        basis.calls = []
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)
2378
            else:
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))
3350.8.14 by Robert Collins
Review feedback.
2386
        # It's not strictly minimal, but it seems reasonable for now for it to
3350.8.6 by Robert Collins
get_record_stream stacking for delta access.
2387
        # ask which fallbacks have which parents.
2388
        self.assertEqual([
2389
            ("get_parent_map", set([key_basis, key_missing])),
2390
            ("get_record_stream", [key_basis], 'unordered', False)],
2391
            calls)
2392
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()
2396
        key = ('foo',)
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'])
2404
        basis.calls = []
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))
2409
        results = []
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)
2415
            else:
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:
2423
                source = test
2424
            else:
2425
                source = basis
2426
            record = source.get_record_stream([result[0]], 'unordered',
2427
                False).next()
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])
3350.8.14 by Robert Collins
Review feedback.
2432
        # It's not strictly minimal, but it seems reasonable for now for it to
3350.8.6 by Robert Collins
get_record_stream stacking for delta access.
2433
        # ask which fallbacks have which parents.
2434
        self.assertEqual([
2435
            ("get_parent_map", set([key_basis, key_basis_2, key_missing])),
2436
            ("get_record_stream", [key_basis_2, key_basis], 'topological', False)],
2437
            calls)
3350.8.1 by Robert Collins
KnitVersionedFiles.add_fallback_versioned_files exists.
2438
2439
    def test_get_sha1s(self):
3350.8.3 by Robert Collins
VF.get_sha1s needed changing to be stackable.
2440
        # sha1's in the test knit are answered without asking the basis
2441
        basis, test = self.get_basis_and_test_knit()
2442
        key = ('foo',)
2443
        key_basis = ('bar',)
2444
        key_missing = ('missing',)
2445
        test.add_lines(key, (), ['foo\n'])
5849.1.1 by Jelmer Vernooij
Use osutils.sha_string() when possible.
2446
        key_sha1sum = osutils.sha_string('foo\n')
3350.8.3 by Robert Collins
VF.get_sha1s needed changing to be stackable.
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'])
5849.1.1 by Jelmer Vernooij
Use osutils.sha_string() when possible.
2454
        basis_sha1sum = osutils.sha_string('foo\nbar\n')
3350.8.3 by Robert Collins
VF.get_sha1s needed changing to be stackable.
2455
        basis.calls = []
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]))],
2460
            basis.calls)
3350.8.1 by Robert Collins
KnitVersionedFiles.add_fallback_versioned_files exists.
2461
2462
    def test_insert_record_stream(self):
3350.8.10 by Robert Collins
Stacked insert_record_stream.
2463
        # records are inserted as normal; insert_record_stream builds on
3350.8.14 by Robert Collins
Review feedback.
2464
        # add_lines, so a smoke test should be all that's needed:
3350.8.10 by Robert Collins
Stacked insert_record_stream.
2465
        key = ('foo',)
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'])
2471
        basis.calls = []
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)
3830.3.9 by Martin Pool
Simplify kvf insert_record_stream; add has_key shorthand methods; update stacking effort tests
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])),
3830.3.10 by Martin Pool
Update more stacking effort tests
2480
             ('get_record_stream', [key_basis], 'unordered', True)],
3350.8.10 by Robert Collins
Stacked insert_record_stream.
2481
            basis.calls)
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'))
3350.8.1 by Robert Collins
KnitVersionedFiles.add_fallback_versioned_files exists.
2486
2487
    def test_iter_lines_added_or_present_in_keys(self):
3350.8.5 by Robert Collins
Iter_lines_added_or_present_in_keys stacks.
2488
        # Lines from the basis are returned, and lines for a given key are only
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
2489
        # returned once.
3350.8.5 by Robert Collins
Iter_lines_added_or_present_in_keys stacks.
2490
        key1 = ('foo1',)
2491
        key2 = ('foo2',)
2492
        # all sources are asked for keys:
2493
        basis, test = self.get_basis_and_test_knit()
2494
        basis.add_lines(key1, (), ["foo"])
2495
        basis.calls = []
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]))],
2499
            basis.calls)
2500
        # keys in both are not duplicated:
2501
        test.add_lines(key2, (), ["bar\n"])
2502
        basis.add_lines(key2, (), ["bar\n"])
2503
        basis.calls = []
2504
        lines = list(test.iter_lines_added_or_present_in_keys([key2]))
2505
        self.assertEqual([("bar\n", key2)], lines)
2506
        self.assertEqual([], basis.calls)
3350.8.1 by Robert Collins
KnitVersionedFiles.add_fallback_versioned_files exists.
2507
2508
    def test_keys(self):
3350.8.4 by Robert Collins
Vf.keys() stacking support.
2509
        key1 = ('foo1',)
2510
        key2 = ('foo2',)
2511
        # all sources are asked for keys:
2512
        basis, test = self.get_basis_and_test_knit()
2513
        keys = test.keys()
2514
        self.assertEqual(set(), set(keys))
2515
        self.assertEqual([("keys",)], basis.calls)
2516
        # keys from a basis are returned:
2517
        basis.add_lines(key1, (), [])
2518
        basis.calls = []
2519
        keys = test.keys()
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, (), [])
2525
        basis.calls = []
2526
        keys = test.keys()
2527
        self.assertEqual(2, len(keys))
2528
        self.assertEqual(set([key1, key2]), set(keys))
2529
        self.assertEqual([("keys",)], basis.calls)
3350.8.1 by Robert Collins
KnitVersionedFiles.add_fallback_versioned_files exists.
2530
2531
    def test_add_mpdiffs(self):
3350.8.11 by Robert Collins
Stacked add_mpdiffs.
2532
        # records are inserted as normal; add_mpdiff builds on
3350.8.14 by Robert Collins
Review feedback.
2533
        # add_lines, so a smoke test should be all that's needed:
3350.8.11 by Robert Collins
Stacked add_mpdiffs.
2534
        key = ('foo',)
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'])
2540
        basis.calls = []
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])),
3830.3.10 by Martin Pool
Update more stacking effort tests
2547
            ('get_record_stream', [key_basis], 'unordered', True),],
3350.8.11 by Robert Collins
Stacked add_mpdiffs.
2548
            basis.calls)
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'))
3350.8.1 by Robert Collins
KnitVersionedFiles.add_fallback_versioned_files exists.
2553
2554
    def test_make_mpdiffs(self):
3350.8.12 by Robert Collins
Stacked make_mpdiffs.
2555
        # Generating an mpdiff across a stacking boundary should detect parent
2556
        # texts regions.
2557
        key = ('foo',)
2558
        key_left = ('bar',)
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'])
2563
        basis.calls = []
2564
        test.add_lines(key, (key_left, key_right),
2565
            ['bar\n', 'foo\n', 'zaphod\n'])
2566
        diffs = test.make_mpdiffs([key])
2567
        self.assertEqual([
2568
            multiparent.MultiParent([multiparent.ParentText(0, 0, 0, 1),
2569
                multiparent.NewText(['foo\n']),
2570
                multiparent.ParentText(1, 0, 2, 1)])],
2571
            diffs)
3830.3.10 by Martin Pool
Update more stacking effort tests
2572
        self.assertEqual(3, len(basis.calls))
3350.8.12 by Robert Collins
Stacked make_mpdiffs.
2573
        self.assertEqual([
2574
            ("get_parent_map", set([key_left, key_right])),
2575
            ("get_parent_map", set([key_left, key_right])),
2576
            ],
3830.3.10 by Martin Pool
Update more stacking effort tests
2577
            basis.calls[:-1])
2578
        last_call = basis.calls[-1]
3350.8.14 by Robert Collins
Review feedback.
2579
        self.assertEqual('get_record_stream', last_call[0])
2580
        self.assertEqual(set([key_left, key_right]), set(last_call[1]))
4537.3.5 by John Arbash Meinel
Fix 3 tests that assumed it would use 'unordered' in the fallback,
2581
        self.assertEqual('topological', last_call[2])
3350.8.14 by Robert Collins
Review feedback.
2582
        self.assertEqual(True, last_call[3])
4005.3.6 by Robert Collins
Support delta_closure=True with NetworkRecordStream to transmit deltas over the wire when full text extraction is required on the far end.
2583
2584
2585
class TestNetworkBehaviour(KnitTests):
2586
    """Tests for getting data out of/into knits over the network."""
2587
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])
2607
        bytes = netb[0]
2608
        kind, line_end = network_bytes_to_kind_and_offset(bytes)
2609
        self.assertEqual('knit-delta-closure', kind)
2610
2611
2612
class TestContentMapGenerator(KnitTests):
2613
    """Tests for ContentMapGenerator"""
2614
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'))
2627
            else:
2628
                self.assertEqual('d2\n', record.get_bytes_as('fulltext'))
2629
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',
2642
            }
2643
        for record in generator.get_record_stream():
2644
            self.assertEqual(kinds[record.key], record.storage_kind)