/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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
1
# Copyright (C) 2005, 2006, 2007 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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
"""Tests for Knit data structure"""
18
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
19
from cStringIO import StringIO
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
20
import difflib
2329.1.1 by John Arbash Meinel
Update _KnitData parser to raise more helpful errors when it detects corruption.
21
import gzip
22
import sha
2484.1.17 by John Arbash Meinel
Workaround for Pyrex <0.9.5 and python >=2.5 incompatibilities.
23
import sys
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
24
2196.2.5 by John Arbash Meinel
Add an exception class when the knit index storage method is unknown, and properly test for it
25
from bzrlib import (
26
    errors,
2484.1.5 by John Arbash Meinel
Simplistic implementations of custom parsers for options and parents
27
    generate_ids,
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
28
    knit,
2592.3.66 by Robert Collins
Allow adaption of KnitData to pack files.
29
    pack,
2196.2.5 by John Arbash Meinel
Add an exception class when the knit index storage method is unknown, and properly test for it
30
    )
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
31
from bzrlib.errors import (
32
    RevisionAlreadyPresent,
33
    KnitHeaderError,
34
    RevisionNotPresent,
35
    NoSuchFile,
36
    )
2592.3.1 by Robert Collins
Allow giving KnitVersionedFile an index object to use rather than implicitly creating one.
37
from bzrlib.index import *
1684.3.3 by Robert Collins
Add a special cased weaves to knit converter.
38
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.
39
    AnnotatedKnitContent,
2151.1.1 by John Arbash Meinel
(Dmitry Vasiliev) Tune KnitContent and add tests
40
    KnitContent,
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.
41
    KnitGraphIndex,
1684.3.3 by Robert Collins
Add a special cased weaves to knit converter.
42
    KnitVersionedFile,
43
    KnitPlainFactory,
44
    KnitAnnotateFactory,
2592.3.66 by Robert Collins
Allow adaption of KnitData to pack files.
45
    _KnitAccess,
2329.1.1 by John Arbash Meinel
Update _KnitData parser to raise more helpful errors when it detects corruption.
46
    _KnitData,
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
47
    _KnitIndex,
2592.3.66 by Robert Collins
Allow adaption of KnitData to pack files.
48
    _PackAccess,
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.
49
    PlainKnitContent,
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
50
    _StreamAccess,
51
    _StreamIndex,
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
52
    WeaveToKnit,
2520.4.41 by Aaron Bentley
Accelerate mpdiff generation
53
    KnitSequenceMatcher,
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
54
    )
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
55
from bzrlib.osutils import split_lines
2592.3.66 by Robert Collins
Allow adaption of KnitData to pack files.
56
from bzrlib.tests import (
57
    Feature,
58
    TestCase,
59
    TestCaseWithMemoryTransport,
60
    TestCaseWithTransport,
61
    )
2745.5.3 by Robert Collins
* Move transport logging into a new transport class
62
from bzrlib.transport import get_transport
1563.2.13 by Robert Collins
InterVersionedFile implemented.
63
from bzrlib.transport.memory import MemoryTransport
3052.2.3 by Robert Collins
Handle insert_data_stream of an unannotated stream into an annotated knit.
64
from bzrlib.tuned_gzip import GzipFile
2535.3.15 by Andrew Bennetts
Add KnitVersionedFile.get_stream_as_bytes, start smart implementation of RemoteRepository.get_data_stream.
65
from bzrlib.util import bencode
1684.3.3 by Robert Collins
Add a special cased weaves to knit converter.
66
from bzrlib.weave import Weave
67
68
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
69
class _CompiledKnitFeature(Feature):
70
71
    def _probe(self):
72
        try:
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
73
            import bzrlib._knit_load_data_c
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
74
        except ImportError:
75
            return False
76
        return True
77
78
    def feature_name(self):
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
79
        return 'bzrlib._knit_load_data_c'
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
80
81
CompiledKnitFeature = _CompiledKnitFeature()
82
83
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.
84
class KnitContentTestsMixin(object):
2151.1.1 by John Arbash Meinel
(Dmitry Vasiliev) Tune KnitContent and add tests
85
86
    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.
87
        content = self._make_content([])
2151.1.1 by John Arbash Meinel
(Dmitry Vasiliev) Tune KnitContent and add tests
88
89
    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.
90
        content = self._make_content([])
2151.1.1 by John Arbash Meinel
(Dmitry Vasiliev) Tune KnitContent and add tests
91
        self.assertEqual(content.text(), [])
92
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.
93
        content = self._make_content([("origin1", "text1"), ("origin2", "text2")])
2151.1.1 by John Arbash Meinel
(Dmitry Vasiliev) Tune KnitContent and add tests
94
        self.assertEqual(content.text(), ["text1", "text2"])
95
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.
96
    def test_copy(self):
97
        content = self._make_content([("origin1", "text1"), ("origin2", "text2")])
98
        copy = content.copy()
99
        self.assertIsInstance(copy, content.__class__)
100
        self.assertEqual(copy.annotate(), content.annotate())
101
102
    def assertDerivedBlocksEqual(self, source, target, noeol=False):
103
        """Assert that the derived matching blocks match real output"""
104
        source_lines = source.splitlines(True)
105
        target_lines = target.splitlines(True)
106
        def nl(line):
107
            if noeol and not line.endswith('\n'):
108
                return line + '\n'
109
            else:
110
                return line
111
        source_content = self._make_content([(None, nl(l)) for l in source_lines])
112
        target_content = self._make_content([(None, nl(l)) for l in target_lines])
113
        line_delta = source_content.line_delta(target_content)
114
        delta_blocks = list(KnitContent.get_line_delta_blocks(line_delta,
115
            source_lines, target_lines))
116
        matcher = KnitSequenceMatcher(None, source_lines, target_lines)
117
        matcher_blocks = list(list(matcher.get_matching_blocks()))
118
        self.assertEqual(matcher_blocks, delta_blocks)
119
120
    def test_get_line_delta_blocks(self):
121
        self.assertDerivedBlocksEqual('a\nb\nc\n', 'q\nc\n')
122
        self.assertDerivedBlocksEqual(TEXT_1, TEXT_1)
123
        self.assertDerivedBlocksEqual(TEXT_1, TEXT_1A)
124
        self.assertDerivedBlocksEqual(TEXT_1, TEXT_1B)
125
        self.assertDerivedBlocksEqual(TEXT_1B, TEXT_1A)
126
        self.assertDerivedBlocksEqual(TEXT_1A, TEXT_1B)
127
        self.assertDerivedBlocksEqual(TEXT_1A, '')
128
        self.assertDerivedBlocksEqual('', TEXT_1A)
129
        self.assertDerivedBlocksEqual('', '')
130
        self.assertDerivedBlocksEqual('a\nb\nc', 'a\nb\nc\nd')
131
132
    def test_get_line_delta_blocks_noeol(self):
133
        """Handle historical knit deltas safely
134
135
        Some existing knit deltas don't consider the last line to differ
136
        when the only difference whether it has a final newline.
137
138
        New knit deltas appear to always consider the last line to differ
139
        in this case.
140
        """
141
        self.assertDerivedBlocksEqual('a\nb\nc', 'a\nb\nc\nd\n', noeol=True)
142
        self.assertDerivedBlocksEqual('a\nb\nc\nd\n', 'a\nb\nc', noeol=True)
143
        self.assertDerivedBlocksEqual('a\nb\nc\n', 'a\nb\nc', noeol=True)
144
        self.assertDerivedBlocksEqual('a\nb\nc', 'a\nb\nc\n', noeol=True)
145
146
147
class TestPlainKnitContent(TestCase, KnitContentTestsMixin):
148
149
    def _make_content(self, lines):
150
        annotated_content = AnnotatedKnitContent(lines)
151
        return PlainKnitContent(annotated_content.text(), 'bogus')
152
153
    def test_annotate(self):
154
        content = self._make_content([])
155
        self.assertEqual(content.annotate(), [])
156
157
        content = self._make_content([("origin1", "text1"), ("origin2", "text2")])
158
        self.assertEqual(content.annotate(),
159
            [("bogus", "text1"), ("bogus", "text2")])
160
161
    def test_annotate_iter(self):
162
        content = self._make_content([])
163
        it = content.annotate_iter()
164
        self.assertRaises(StopIteration, it.next)
165
166
        content = self._make_content([("bogus", "text1"), ("bogus", "text2")])
167
        it = content.annotate_iter()
168
        self.assertEqual(it.next(), ("bogus", "text1"))
169
        self.assertEqual(it.next(), ("bogus", "text2"))
170
        self.assertRaises(StopIteration, it.next)
171
172
    def test_line_delta(self):
173
        content1 = self._make_content([("", "a"), ("", "b")])
174
        content2 = self._make_content([("", "a"), ("", "a"), ("", "c")])
175
        self.assertEqual(content1.line_delta(content2),
176
            [(1, 2, 2, ["a", "c"])])
177
178
    def test_line_delta_iter(self):
179
        content1 = self._make_content([("", "a"), ("", "b")])
180
        content2 = self._make_content([("", "a"), ("", "a"), ("", "c")])
181
        it = content1.line_delta_iter(content2)
182
        self.assertEqual(it.next(), (1, 2, 2, ["a", "c"]))
183
        self.assertRaises(StopIteration, it.next)
184
185
186
class TestAnnotatedKnitContent(TestCase, KnitContentTestsMixin):
187
188
    def _make_content(self, lines):
189
        return AnnotatedKnitContent(lines)
190
191
    def test_annotate(self):
192
        content = self._make_content([])
193
        self.assertEqual(content.annotate(), [])
194
195
        content = self._make_content([("origin1", "text1"), ("origin2", "text2")])
2151.1.1 by John Arbash Meinel
(Dmitry Vasiliev) Tune KnitContent and add tests
196
        self.assertEqual(content.annotate(),
197
            [("origin1", "text1"), ("origin2", "text2")])
198
199
    def test_annotate_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.
200
        content = self._make_content([])
2151.1.1 by John Arbash Meinel
(Dmitry Vasiliev) Tune KnitContent and add tests
201
        it = content.annotate_iter()
202
        self.assertRaises(StopIteration, it.next)
203
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.
204
        content = self._make_content([("origin1", "text1"), ("origin2", "text2")])
2151.1.1 by John Arbash Meinel
(Dmitry Vasiliev) Tune KnitContent and add tests
205
        it = content.annotate_iter()
206
        self.assertEqual(it.next(), ("origin1", "text1"))
207
        self.assertEqual(it.next(), ("origin2", "text2"))
208
        self.assertRaises(StopIteration, it.next)
209
210
    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.
211
        content1 = self._make_content([("", "a"), ("", "b")])
212
        content2 = self._make_content([("", "a"), ("", "a"), ("", "c")])
2151.1.1 by John Arbash Meinel
(Dmitry Vasiliev) Tune KnitContent and add tests
213
        self.assertEqual(content1.line_delta(content2),
214
            [(1, 2, 2, [("", "a"), ("", "c")])])
215
216
    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.
217
        content1 = self._make_content([("", "a"), ("", "b")])
218
        content2 = self._make_content([("", "a"), ("", "a"), ("", "c")])
2151.1.1 by John Arbash Meinel
(Dmitry Vasiliev) Tune KnitContent and add tests
219
        it = content1.line_delta_iter(content2)
220
        self.assertEqual(it.next(), (1, 2, 2, [("", "a"), ("", "c")]))
221
        self.assertRaises(StopIteration, it.next)
222
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
223
224
class MockTransport(object):
225
226
    def __init__(self, file_lines=None):
227
        self.file_lines = file_lines
228
        self.calls = []
2196.2.3 by John Arbash Meinel
Update tests and code to pass after merging bzr.dev
229
        # We have no base directory for the MockTransport
230
        self.base = ''
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
231
232
    def get(self, filename):
233
        if self.file_lines is None:
234
            raise NoSuchFile(filename)
235
        else:
236
            return StringIO("\n".join(self.file_lines))
237
2329.1.1 by John Arbash Meinel
Update _KnitData parser to raise more helpful errors when it detects corruption.
238
    def readv(self, relpath, offsets):
239
        fp = self.get(relpath)
240
        for offset, size in offsets:
241
            fp.seek(offset)
242
            yield offset, fp.read(size)
243
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
244
    def __getattr__(self, name):
245
        def queue_call(*args, **kwargs):
246
            self.calls.append((name, args, kwargs))
247
        return queue_call
248
249
2592.3.66 by Robert Collins
Allow adaption of KnitData to pack files.
250
class KnitRecordAccessTestsMixin(object):
251
    """Tests for getting and putting knit records."""
252
253
    def assertAccessExists(self, access):
254
        """Ensure the data area for access has been initialised/exists."""
255
        raise NotImplementedError(self.assertAccessExists)
256
257
    def test_add_raw_records(self):
258
        """Add_raw_records adds records retrievable later."""
259
        access = self.get_access()
260
        memos = access.add_raw_records([10], '1234567890')
261
        self.assertEqual(['1234567890'], list(access.get_raw_records(memos)))
2592.3.67 by Robert Collins
More tests for bzrlib.knit._PackAccess.
262
 
263
    def test_add_several_raw_records(self):
264
        """add_raw_records with many records and read some back."""
265
        access = self.get_access()
266
        memos = access.add_raw_records([10, 2, 5], '12345678901234567')
267
        self.assertEqual(['1234567890', '12', '34567'],
268
            list(access.get_raw_records(memos)))
269
        self.assertEqual(['1234567890'],
270
            list(access.get_raw_records(memos[0:1])))
271
        self.assertEqual(['12'],
272
            list(access.get_raw_records(memos[1:2])))
273
        self.assertEqual(['34567'],
274
            list(access.get_raw_records(memos[2:3])))
275
        self.assertEqual(['1234567890', '34567'],
276
            list(access.get_raw_records(memos[0:1] + memos[2:3])))
277
2592.3.66 by Robert Collins
Allow adaption of KnitData to pack files.
278
    def test_create(self):
279
        """create() should make a file on disk."""
280
        access = self.get_access()
281
        access.create()
282
        self.assertAccessExists(access)
283
284
    def test_open_file(self):
285
        """open_file never errors."""
286
        access = self.get_access()
287
        access.open_file()
288
289
290
class TestKnitKnitAccess(TestCaseWithMemoryTransport, KnitRecordAccessTestsMixin):
291
    """Tests for the .kndx implementation."""
292
293
    def assertAccessExists(self, access):
294
        self.assertNotEqual(None, access.open_file())
295
296
    def get_access(self):
297
        """Get a .knit style access instance."""
298
        access = _KnitAccess(self.get_transport(), "foo.knit", None, None,
299
            False, False)
300
        return access
301
    
302
303
class TestPackKnitAccess(TestCaseWithMemoryTransport, KnitRecordAccessTestsMixin):
304
    """Tests for the pack based access."""
305
306
    def assertAccessExists(self, access):
307
        # as pack based access has no backing unless an index maps data, this
308
        # is a no-op.
309
        pass
310
311
    def get_access(self):
2592.3.67 by Robert Collins
More tests for bzrlib.knit._PackAccess.
312
        return self._get_access()[0]
313
314
    def _get_access(self, packname='packfile', index='FOO'):
2592.3.66 by Robert Collins
Allow adaption of KnitData to pack files.
315
        transport = self.get_transport()
316
        def write_data(bytes):
2592.3.67 by Robert Collins
More tests for bzrlib.knit._PackAccess.
317
            transport.append_bytes(packname, bytes)
2592.3.66 by Robert Collins
Allow adaption of KnitData to pack files.
318
        writer = pack.ContainerWriter(write_data)
319
        writer.begin()
2592.3.67 by Robert Collins
More tests for bzrlib.knit._PackAccess.
320
        indices = {index:(transport, packname)}
321
        access = _PackAccess(indices, writer=(writer, index))
322
        return access, writer
2592.3.66 by Robert Collins
Allow adaption of KnitData to pack files.
323
2592.3.67 by Robert Collins
More tests for bzrlib.knit._PackAccess.
324
    def test_read_from_several_packs(self):
325
        access, writer = self._get_access()
326
        memos = []
327
        memos.extend(access.add_raw_records([10], '1234567890'))
328
        writer.end()
329
        access, writer = self._get_access('pack2', 'FOOBAR')
330
        memos.extend(access.add_raw_records([5], '12345'))
331
        writer.end()
332
        access, writer = self._get_access('pack3', 'BAZ')
333
        memos.extend(access.add_raw_records([5], 'alpha'))
334
        writer.end()
335
        transport = self.get_transport()
336
        access = _PackAccess({"FOO":(transport, 'packfile'),
337
            "FOOBAR":(transport, 'pack2'),
338
            "BAZ":(transport, 'pack3')})
339
        self.assertEqual(['1234567890', '12345', 'alpha'],
340
            list(access.get_raw_records(memos)))
341
        self.assertEqual(['1234567890'],
342
            list(access.get_raw_records(memos[0:1])))
343
        self.assertEqual(['12345'],
344
            list(access.get_raw_records(memos[1:2])))
345
        self.assertEqual(['alpha'],
346
            list(access.get_raw_records(memos[2:3])))
347
        self.assertEqual(['1234567890', 'alpha'],
348
            list(access.get_raw_records(memos[0:1] + memos[2:3])))
2592.3.66 by Robert Collins
Allow adaption of KnitData to pack files.
349
2592.3.70 by Robert Collins
Allow setting a writer after creating a knit._PackAccess object.
350
    def test_set_writer(self):
351
        """The writer should be settable post construction."""
352
        access = _PackAccess({})
353
        transport = self.get_transport()
354
        packname = 'packfile'
355
        index = 'foo'
356
        def write_data(bytes):
357
            transport.append_bytes(packname, bytes)
358
        writer = pack.ContainerWriter(write_data)
359
        writer.begin()
360
        access.set_writer(writer, index, (transport, packname))
361
        memos = access.add_raw_records([10], '1234567890')
362
        writer.end()
363
        self.assertEqual(['1234567890'], list(access.get_raw_records(memos)))
364
2592.3.66 by Robert Collins
Allow adaption of KnitData to pack files.
365
2329.1.1 by John Arbash Meinel
Update _KnitData parser to raise more helpful errors when it detects corruption.
366
class LowLevelKnitDataTests(TestCase):
367
368
    def create_gz_content(self, text):
369
        sio = StringIO()
370
        gz_file = gzip.GzipFile(mode='wb', fileobj=sio)
371
        gz_file.write(text)
372
        gz_file.close()
373
        return sio.getvalue()
374
375
    def test_valid_knit_data(self):
376
        sha1sum = sha.new('foo\nbar\n').hexdigest()
377
        gz_txt = self.create_gz_content('version rev-id-1 2 %s\n'
378
                                        'foo\n'
379
                                        'bar\n'
380
                                        'end rev-id-1\n'
381
                                        % (sha1sum,))
382
        transport = MockTransport([gz_txt])
2592.3.66 by Robert Collins
Allow adaption of KnitData to pack files.
383
        access = _KnitAccess(transport, 'filename', None, None, False, False)
384
        data = _KnitData(access=access)
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
385
        records = [('rev-id-1', (None, 0, len(gz_txt)))]
2329.1.1 by John Arbash Meinel
Update _KnitData parser to raise more helpful errors when it detects corruption.
386
387
        contents = data.read_records(records)
388
        self.assertEqual({'rev-id-1':(['foo\n', 'bar\n'], sha1sum)}, contents)
389
390
        raw_contents = list(data.read_records_iter_raw(records))
391
        self.assertEqual([('rev-id-1', gz_txt)], raw_contents)
392
393
    def test_not_enough_lines(self):
394
        sha1sum = sha.new('foo\n').hexdigest()
395
        # record says 2 lines data says 1
396
        gz_txt = self.create_gz_content('version rev-id-1 2 %s\n'
397
                                        'foo\n'
398
                                        'end rev-id-1\n'
399
                                        % (sha1sum,))
400
        transport = MockTransport([gz_txt])
2592.3.66 by Robert Collins
Allow adaption of KnitData to pack files.
401
        access = _KnitAccess(transport, 'filename', None, None, False, False)
402
        data = _KnitData(access=access)
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
403
        records = [('rev-id-1', (None, 0, len(gz_txt)))]
2329.1.1 by John Arbash Meinel
Update _KnitData parser to raise more helpful errors when it detects corruption.
404
        self.assertRaises(errors.KnitCorrupt, data.read_records, records)
405
406
        # read_records_iter_raw won't detect that sort of mismatch/corruption
407
        raw_contents = list(data.read_records_iter_raw(records))
408
        self.assertEqual([('rev-id-1', gz_txt)], raw_contents)
409
410
    def test_too_many_lines(self):
411
        sha1sum = sha.new('foo\nbar\n').hexdigest()
412
        # record says 1 lines data says 2
413
        gz_txt = self.create_gz_content('version rev-id-1 1 %s\n'
414
                                        'foo\n'
415
                                        'bar\n'
416
                                        'end rev-id-1\n'
417
                                        % (sha1sum,))
418
        transport = MockTransport([gz_txt])
2592.3.66 by Robert Collins
Allow adaption of KnitData to pack files.
419
        access = _KnitAccess(transport, 'filename', None, None, False, False)
420
        data = _KnitData(access=access)
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
421
        records = [('rev-id-1', (None, 0, len(gz_txt)))]
2329.1.1 by John Arbash Meinel
Update _KnitData parser to raise more helpful errors when it detects corruption.
422
        self.assertRaises(errors.KnitCorrupt, data.read_records, records)
423
424
        # read_records_iter_raw won't detect that sort of mismatch/corruption
425
        raw_contents = list(data.read_records_iter_raw(records))
426
        self.assertEqual([('rev-id-1', gz_txt)], raw_contents)
427
428
    def test_mismatched_version_id(self):
429
        sha1sum = sha.new('foo\nbar\n').hexdigest()
430
        gz_txt = self.create_gz_content('version rev-id-1 2 %s\n'
431
                                        'foo\n'
432
                                        'bar\n'
433
                                        'end rev-id-1\n'
434
                                        % (sha1sum,))
435
        transport = MockTransport([gz_txt])
2592.3.66 by Robert Collins
Allow adaption of KnitData to pack files.
436
        access = _KnitAccess(transport, 'filename', None, None, False, False)
437
        data = _KnitData(access=access)
2329.1.1 by John Arbash Meinel
Update _KnitData parser to raise more helpful errors when it detects corruption.
438
        # We are asking for rev-id-2, but the data is rev-id-1
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
439
        records = [('rev-id-2', (None, 0, len(gz_txt)))]
2329.1.1 by John Arbash Meinel
Update _KnitData parser to raise more helpful errors when it detects corruption.
440
        self.assertRaises(errors.KnitCorrupt, data.read_records, records)
441
442
        # read_records_iter_raw will notice if we request the wrong version.
443
        self.assertRaises(errors.KnitCorrupt, list,
444
                          data.read_records_iter_raw(records))
445
446
    def test_uncompressed_data(self):
447
        sha1sum = sha.new('foo\nbar\n').hexdigest()
448
        txt = ('version rev-id-1 2 %s\n'
449
               'foo\n'
450
               'bar\n'
451
               'end rev-id-1\n'
452
               % (sha1sum,))
453
        transport = MockTransport([txt])
2592.3.66 by Robert Collins
Allow adaption of KnitData to pack files.
454
        access = _KnitAccess(transport, 'filename', None, None, False, False)
455
        data = _KnitData(access=access)
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
456
        records = [('rev-id-1', (None, 0, len(txt)))]
2329.1.1 by John Arbash Meinel
Update _KnitData parser to raise more helpful errors when it detects corruption.
457
458
        # We don't have valid gzip data ==> corrupt
459
        self.assertRaises(errors.KnitCorrupt, data.read_records, records)
460
461
        # read_records_iter_raw will notice the bad data
462
        self.assertRaises(errors.KnitCorrupt, list,
463
                          data.read_records_iter_raw(records))
464
465
    def test_corrupted_data(self):
466
        sha1sum = sha.new('foo\nbar\n').hexdigest()
467
        gz_txt = self.create_gz_content('version rev-id-1 2 %s\n'
468
                                        'foo\n'
469
                                        'bar\n'
470
                                        'end rev-id-1\n'
471
                                        % (sha1sum,))
472
        # Change 2 bytes in the middle to \xff
473
        gz_txt = gz_txt[:10] + '\xff\xff' + gz_txt[12:]
474
        transport = MockTransport([gz_txt])
2592.3.66 by Robert Collins
Allow adaption of KnitData to pack files.
475
        access = _KnitAccess(transport, 'filename', None, None, False, False)
476
        data = _KnitData(access=access)
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
477
        records = [('rev-id-1', (None, 0, len(gz_txt)))]
2329.1.1 by John Arbash Meinel
Update _KnitData parser to raise more helpful errors when it detects corruption.
478
479
        self.assertRaises(errors.KnitCorrupt, data.read_records, records)
480
481
        # read_records_iter_raw will notice if we request the wrong version.
482
        self.assertRaises(errors.KnitCorrupt, list,
483
                          data.read_records_iter_raw(records))
484
485
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
486
class LowLevelKnitIndexTests(TestCase):
487
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
488
    def get_knit_index(self, *args, **kwargs):
489
        orig = knit._load_data
490
        def reset():
491
            knit._load_data = orig
492
        self.addCleanup(reset)
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
493
        from bzrlib._knit_load_data_py import _load_data_py
494
        knit._load_data = _load_data_py
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
495
        return _KnitIndex(*args, **kwargs)
496
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
497
    def test_no_such_file(self):
498
        transport = MockTransport()
499
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
500
        self.assertRaises(NoSuchFile, self.get_knit_index,
501
                          transport, "filename", "r")
502
        self.assertRaises(NoSuchFile, self.get_knit_index,
503
                          transport, "filename", "w", create=False)
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
504
505
    def test_create_file(self):
506
        transport = MockTransport()
507
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
508
        index = self.get_knit_index(transport, "filename", "w",
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
509
            file_mode="wb", create=True)
510
        self.assertEqual(
511
                ("put_bytes_non_atomic",
512
                    ("filename", index.HEADER), {"mode": "wb"}),
513
                transport.calls.pop(0))
514
515
    def test_delay_create_file(self):
516
        transport = MockTransport()
517
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
518
        index = self.get_knit_index(transport, "filename", "w",
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
519
            create=True, file_mode="wb", create_parent_dir=True,
520
            delay_create=True, dir_mode=0777)
521
        self.assertEqual([], transport.calls)
522
523
        index.add_versions([])
524
        name, (filename, f), kwargs = transport.calls.pop(0)
525
        self.assertEqual("put_file_non_atomic", name)
526
        self.assertEqual(
527
            {"dir_mode": 0777, "create_parent_dir": True, "mode": "wb"},
528
            kwargs)
529
        self.assertEqual("filename", filename)
530
        self.assertEqual(index.HEADER, f.read())
531
532
        index.add_versions([])
533
        self.assertEqual(("append_bytes", ("filename", ""), {}),
534
            transport.calls.pop(0))
535
536
    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
537
        unicode_revision_id = u"version-\N{CYRILLIC CAPITAL LETTER A}"
538
        utf8_revision_id = unicode_revision_id.encode('utf-8')
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
539
        transport = MockTransport([
540
            _KnitIndex.HEADER,
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
541
            '%s option 0 1 :' % (utf8_revision_id,)
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
542
            ])
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
543
        index = self.get_knit_index(transport, "filename", "r")
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
544
        # _KnitIndex is a private class, and deals in utf8 revision_ids, not
545
        # Unicode revision_ids.
546
        self.assertTrue(index.has_version(utf8_revision_id))
547
        self.assertFalse(index.has_version(unicode_revision_id))
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
548
549
    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
550
        unicode_revision_id = u"version-\N{CYRILLIC CAPITAL LETTER A}"
551
        utf8_revision_id = unicode_revision_id.encode('utf-8')
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
552
        transport = MockTransport([
553
            _KnitIndex.HEADER,
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
554
            "version option 0 1 .%s :" % (utf8_revision_id,)
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
555
            ])
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
556
        index = self.get_knit_index(transport, "filename", "r")
3287.5.5 by Robert Collins
Refactor internals of knit implementations to implement get_parents_with_ghosts in terms of get_parent_map.
557
        self.assertEqual((utf8_revision_id,),
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
558
            index.get_parents_with_ghosts("version"))
559
560
    def test_read_ignore_corrupted_lines(self):
561
        transport = MockTransport([
562
            _KnitIndex.HEADER,
563
            "corrupted",
564
            "corrupted options 0 1 .b .c ",
565
            "version options 0 1 :"
566
            ])
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
567
        index = self.get_knit_index(transport, "filename", "r")
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
568
        self.assertEqual(1, index.num_versions())
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
569
        self.assertTrue(index.has_version("version"))
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
570
571
    def test_read_corrupted_header(self):
2196.2.3 by John Arbash Meinel
Update tests and code to pass after merging bzr.dev
572
        transport = MockTransport(['not a bzr knit index header\n'])
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
573
        self.assertRaises(KnitHeaderError,
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
574
            self.get_knit_index, transport, "filename", "r")
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
575
576
    def test_read_duplicate_entries(self):
577
        transport = MockTransport([
578
            _KnitIndex.HEADER,
579
            "parent options 0 1 :",
580
            "version options1 0 1 0 :",
581
            "version options2 1 2 .other :",
582
            "version options3 3 4 0 .other :"
583
            ])
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
584
        index = self.get_knit_index(transport, "filename", "r")
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
585
        self.assertEqual(2, index.num_versions())
2592.3.8 by Robert Collins
Remove unneeded pulib method lookup on private class _KnitIndex.
586
        # check that the index used is the first one written. (Specific
587
        # to KnitIndex style indices.
588
        self.assertEqual("1", index._version_list_to_index(["version"]))
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
589
        self.assertEqual((None, 3, 4), index.get_position("version"))
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
590
        self.assertEqual(["options3"], index.get_options("version"))
3287.5.5 by Robert Collins
Refactor internals of knit implementations to implement get_parents_with_ghosts in terms of get_parent_map.
591
        self.assertEqual(("parent", "other"),
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
592
            index.get_parents_with_ghosts("version"))
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
593
594
    def test_read_compressed_parents(self):
595
        transport = MockTransport([
596
            _KnitIndex.HEADER,
597
            "a option 0 1 :",
598
            "b option 0 1 0 :",
599
            "c option 0 1 1 0 :",
600
            ])
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
601
        index = self.get_knit_index(transport, "filename", "r")
3287.5.6 by Robert Collins
Remove _KnitIndex.get_parents.
602
        self.assertEqual({"b":("a",), "c":("b", "a")},
603
            index.get_parent_map(["b", "c"]))
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
604
605
    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
606
        unicode_revision_id = u"version-\N{CYRILLIC CAPITAL LETTER A}"
607
        utf8_revision_id = unicode_revision_id.encode('utf-8')
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
608
        transport = MockTransport([
609
            _KnitIndex.HEADER
610
            ])
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
611
        index = self.get_knit_index(transport, "filename", "r")
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
612
        index.add_version(utf8_revision_id, ["option"], (None, 0, 1), [])
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
613
        self.assertEqual(("append_bytes", ("filename",
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
614
            "\n%s option 0 1  :" % (utf8_revision_id,)),
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
615
            {}),
616
            transport.calls.pop(0))
617
618
    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
619
        unicode_revision_id = u"version-\N{CYRILLIC CAPITAL LETTER A}"
620
        utf8_revision_id = unicode_revision_id.encode('utf-8')
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
621
        transport = MockTransport([
622
            _KnitIndex.HEADER
623
            ])
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
624
        index = self.get_knit_index(transport, "filename", "r")
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
625
        index.add_version("version", ["option"], (None, 0, 1), [utf8_revision_id])
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
626
        self.assertEqual(("append_bytes", ("filename",
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
627
            "\nversion option 0 1 .%s :" % (utf8_revision_id,)),
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
628
            {}),
629
            transport.calls.pop(0))
630
631
    def test_get_ancestry(self):
632
        transport = MockTransport([
633
            _KnitIndex.HEADER,
634
            "a option 0 1 :",
635
            "b option 0 1 0 .e :",
636
            "c option 0 1 1 0 :",
637
            "d option 0 1 2 .f :"
638
            ])
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
639
        index = self.get_knit_index(transport, "filename", "r")
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
640
641
        self.assertEqual([], index.get_ancestry([]))
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
642
        self.assertEqual(["a"], index.get_ancestry(["a"]))
643
        self.assertEqual(["a", "b"], index.get_ancestry(["b"]))
644
        self.assertEqual(["a", "b", "c"], index.get_ancestry(["c"]))
645
        self.assertEqual(["a", "b", "c", "d"], index.get_ancestry(["d"]))
646
        self.assertEqual(["a", "b"], index.get_ancestry(["a", "b"]))
647
        self.assertEqual(["a", "b", "c"], index.get_ancestry(["a", "c"]))
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
648
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
649
        self.assertRaises(RevisionNotPresent, index.get_ancestry, ["e"])
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
650
651
    def test_get_ancestry_with_ghosts(self):
652
        transport = MockTransport([
653
            _KnitIndex.HEADER,
654
            "a option 0 1 :",
655
            "b option 0 1 0 .e :",
656
            "c option 0 1 0 .f .g :",
657
            "d option 0 1 2 .h .j .k :"
658
            ])
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
659
        index = self.get_knit_index(transport, "filename", "r")
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
660
661
        self.assertEqual([], index.get_ancestry_with_ghosts([]))
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
662
        self.assertEqual(["a"], index.get_ancestry_with_ghosts(["a"]))
663
        self.assertEqual(["a", "e", "b"],
664
            index.get_ancestry_with_ghosts(["b"]))
665
        self.assertEqual(["a", "g", "f", "c"],
666
            index.get_ancestry_with_ghosts(["c"]))
667
        self.assertEqual(["a", "g", "f", "c", "k", "j", "h", "d"],
668
            index.get_ancestry_with_ghosts(["d"]))
669
        self.assertEqual(["a", "e", "b"],
670
            index.get_ancestry_with_ghosts(["a", "b"]))
671
        self.assertEqual(["a", "g", "f", "c"],
672
            index.get_ancestry_with_ghosts(["a", "c"]))
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
673
        self.assertEqual(
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
674
            ["a", "g", "f", "c", "e", "b", "k", "j", "h", "d"],
675
            index.get_ancestry_with_ghosts(["b", "d"]))
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
676
677
        self.assertRaises(RevisionNotPresent,
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
678
            index.get_ancestry_with_ghosts, ["e"])
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
679
2592.3.43 by Robert Collins
A knit iter_parents API.
680
    def test_iter_parents(self):
681
        transport = MockTransport()
682
        index = self.get_knit_index(transport, "filename", "w", create=True)
683
        # no parents
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
684
        index.add_version('r0', ['option'], (None, 0, 1), [])
2592.3.43 by Robert Collins
A knit iter_parents API.
685
        # 1 parent
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
686
        index.add_version('r1', ['option'], (None, 0, 1), ['r0'])
2592.3.43 by Robert Collins
A knit iter_parents API.
687
        # 2 parents
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
688
        index.add_version('r2', ['option'], (None, 0, 1), ['r1', 'r0'])
2592.3.43 by Robert Collins
A knit iter_parents API.
689
        # XXX TODO a ghost
690
        # cases: each sample data individually:
691
        self.assertEqual(set([('r0', ())]),
692
            set(index.iter_parents(['r0'])))
693
        self.assertEqual(set([('r1', ('r0', ))]),
694
            set(index.iter_parents(['r1'])))
695
        self.assertEqual(set([('r2', ('r1', 'r0'))]),
696
            set(index.iter_parents(['r2'])))
697
        # no nodes returned for a missing node
698
        self.assertEqual(set(),
699
            set(index.iter_parents(['missing'])))
700
        # 1 node returned with missing nodes skipped
701
        self.assertEqual(set([('r1', ('r0', ))]),
702
            set(index.iter_parents(['ghost1', 'r1', 'ghost'])))
703
        # 2 nodes returned
704
        self.assertEqual(set([('r0', ()), ('r1', ('r0', ))]),
705
            set(index.iter_parents(['r0', 'r1'])))
706
        # 2 nodes returned, missing skipped
707
        self.assertEqual(set([('r0', ()), ('r1', ('r0', ))]),
708
            set(index.iter_parents(['a', 'r0', 'b', 'r1', 'c'])))
709
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
710
    def test_num_versions(self):
711
        transport = MockTransport([
712
            _KnitIndex.HEADER
713
            ])
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
714
        index = self.get_knit_index(transport, "filename", "r")
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
715
716
        self.assertEqual(0, index.num_versions())
717
        self.assertEqual(0, len(index))
718
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
719
        index.add_version("a", ["option"], (None, 0, 1), [])
720
        self.assertEqual(1, index.num_versions())
721
        self.assertEqual(1, len(index))
722
723
        index.add_version("a", ["option2"], (None, 1, 2), [])
724
        self.assertEqual(1, index.num_versions())
725
        self.assertEqual(1, len(index))
726
727
        index.add_version("b", ["option"], (None, 0, 1), [])
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
728
        self.assertEqual(2, index.num_versions())
729
        self.assertEqual(2, len(index))
730
731
    def test_get_versions(self):
732
        transport = MockTransport([
733
            _KnitIndex.HEADER
734
            ])
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
735
        index = self.get_knit_index(transport, "filename", "r")
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
736
737
        self.assertEqual([], index.get_versions())
738
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
739
        index.add_version("a", ["option"], (None, 0, 1), [])
740
        self.assertEqual(["a"], index.get_versions())
741
742
        index.add_version("a", ["option"], (None, 0, 1), [])
743
        self.assertEqual(["a"], index.get_versions())
744
745
        index.add_version("b", ["option"], (None, 0, 1), [])
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
746
        self.assertEqual(["a", "b"], index.get_versions())
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
747
748
    def test_add_version(self):
749
        transport = MockTransport([
750
            _KnitIndex.HEADER
751
            ])
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
752
        index = self.get_knit_index(transport, "filename", "r")
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
753
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
754
        index.add_version("a", ["option"], (None, 0, 1), ["b"])
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
755
        self.assertEqual(("append_bytes",
756
            ("filename", "\na option 0 1 .b :"),
757
            {}), transport.calls.pop(0))
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
758
        self.assertTrue(index.has_version("a"))
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
759
        self.assertEqual(1, index.num_versions())
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
760
        self.assertEqual((None, 0, 1), index.get_position("a"))
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
761
        self.assertEqual(["option"], index.get_options("a"))
3287.5.5 by Robert Collins
Refactor internals of knit implementations to implement get_parents_with_ghosts in terms of get_parent_map.
762
        self.assertEqual(("b",), index.get_parents_with_ghosts("a"))
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
763
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
764
        index.add_version("a", ["opt"], (None, 1, 2), ["c"])
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
765
        self.assertEqual(("append_bytes",
766
            ("filename", "\na opt 1 2 .c :"),
767
            {}), transport.calls.pop(0))
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
768
        self.assertTrue(index.has_version("a"))
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
769
        self.assertEqual(1, index.num_versions())
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
770
        self.assertEqual((None, 1, 2), index.get_position("a"))
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
771
        self.assertEqual(["opt"], index.get_options("a"))
3287.5.5 by Robert Collins
Refactor internals of knit implementations to implement get_parents_with_ghosts in terms of get_parent_map.
772
        self.assertEqual(("c",), index.get_parents_with_ghosts("a"))
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
773
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
774
        index.add_version("b", ["option"], (None, 2, 3), ["a"])
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
775
        self.assertEqual(("append_bytes",
776
            ("filename", "\nb option 2 3 0 :"),
777
            {}), transport.calls.pop(0))
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
778
        self.assertTrue(index.has_version("b"))
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
779
        self.assertEqual(2, index.num_versions())
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
780
        self.assertEqual((None, 2, 3), index.get_position("b"))
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
781
        self.assertEqual(["option"], index.get_options("b"))
3287.5.5 by Robert Collins
Refactor internals of knit implementations to implement get_parents_with_ghosts in terms of get_parent_map.
782
        self.assertEqual(("a",), index.get_parents_with_ghosts("b"))
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
783
784
    def test_add_versions(self):
785
        transport = MockTransport([
786
            _KnitIndex.HEADER
787
            ])
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
788
        index = self.get_knit_index(transport, "filename", "r")
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
789
790
        index.add_versions([
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
791
            ("a", ["option"], (None, 0, 1), ["b"]),
792
            ("a", ["opt"], (None, 1, 2), ["c"]),
793
            ("b", ["option"], (None, 2, 3), ["a"])
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
794
            ])
795
        self.assertEqual(("append_bytes", ("filename",
796
            "\na option 0 1 .b :"
797
            "\na opt 1 2 .c :"
798
            "\nb option 2 3 0 :"
799
            ), {}), transport.calls.pop(0))
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
800
        self.assertTrue(index.has_version("a"))
801
        self.assertTrue(index.has_version("b"))
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
802
        self.assertEqual(2, index.num_versions())
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
803
        self.assertEqual((None, 1, 2), index.get_position("a"))
804
        self.assertEqual((None, 2, 3), index.get_position("b"))
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
805
        self.assertEqual(["opt"], index.get_options("a"))
806
        self.assertEqual(["option"], index.get_options("b"))
3287.5.5 by Robert Collins
Refactor internals of knit implementations to implement get_parents_with_ghosts in terms of get_parent_map.
807
        self.assertEqual(("c",), index.get_parents_with_ghosts("a"))
808
        self.assertEqual(("a",), index.get_parents_with_ghosts("b"))
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
809
2841.2.1 by Robert Collins
* Commit no longer checks for new text keys during insertion when the
810
    def test_add_versions_random_id_is_accepted(self):
811
        transport = MockTransport([
812
            _KnitIndex.HEADER
813
            ])
814
        index = self.get_knit_index(transport, "filename", "r")
815
816
        index.add_versions([
817
            ("a", ["option"], (None, 0, 1), ["b"]),
818
            ("a", ["opt"], (None, 1, 2), ["c"]),
819
            ("b", ["option"], (None, 2, 3), ["a"])
820
            ], random_id=True)
821
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
822
    def test_delay_create_and_add_versions(self):
823
        transport = MockTransport()
824
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
825
        index = self.get_knit_index(transport, "filename", "w",
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
826
            create=True, file_mode="wb", create_parent_dir=True,
827
            delay_create=True, dir_mode=0777)
828
        self.assertEqual([], transport.calls)
829
830
        index.add_versions([
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
831
            ("a", ["option"], (None, 0, 1), ["b"]),
832
            ("a", ["opt"], (None, 1, 2), ["c"]),
833
            ("b", ["option"], (None, 2, 3), ["a"])
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
834
            ])
835
        name, (filename, f), kwargs = transport.calls.pop(0)
836
        self.assertEqual("put_file_non_atomic", name)
837
        self.assertEqual(
838
            {"dir_mode": 0777, "create_parent_dir": True, "mode": "wb"},
839
            kwargs)
840
        self.assertEqual("filename", filename)
841
        self.assertEqual(
842
            index.HEADER +
843
            "\na option 0 1 .b :"
844
            "\na opt 1 2 .c :"
845
            "\nb option 2 3 0 :",
846
            f.read())
847
848
    def test_has_version(self):
849
        transport = MockTransport([
850
            _KnitIndex.HEADER,
851
            "a option 0 1 :"
852
            ])
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
853
        index = self.get_knit_index(transport, "filename", "r")
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
854
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
855
        self.assertTrue(index.has_version("a"))
856
        self.assertFalse(index.has_version("b"))
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
857
858
    def test_get_position(self):
859
        transport = MockTransport([
860
            _KnitIndex.HEADER,
861
            "a option 0 1 :",
862
            "b option 1 2 :"
863
            ])
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
864
        index = self.get_knit_index(transport, "filename", "r")
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
865
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
866
        self.assertEqual((None, 0, 1), index.get_position("a"))
867
        self.assertEqual((None, 1, 2), index.get_position("b"))
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
868
869
    def test_get_method(self):
870
        transport = MockTransport([
871
            _KnitIndex.HEADER,
872
            "a fulltext,unknown 0 1 :",
873
            "b unknown,line-delta 1 2 :",
874
            "c bad 3 4 :"
875
            ])
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
876
        index = self.get_knit_index(transport, "filename", "r")
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
877
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
878
        self.assertEqual("fulltext", index.get_method("a"))
879
        self.assertEqual("line-delta", index.get_method("b"))
880
        self.assertRaises(errors.KnitIndexUnknownMethod, index.get_method, "c")
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
881
882
    def test_get_options(self):
883
        transport = MockTransport([
884
            _KnitIndex.HEADER,
885
            "a opt1 0 1 :",
886
            "b opt2,opt3 1 2 :"
887
            ])
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
888
        index = self.get_knit_index(transport, "filename", "r")
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
889
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
890
        self.assertEqual(["opt1"], index.get_options("a"))
891
        self.assertEqual(["opt2", "opt3"], index.get_options("b"))
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
892
3287.5.6 by Robert Collins
Remove _KnitIndex.get_parents.
893
    def test_get_parent_map(self):
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
894
        transport = MockTransport([
895
            _KnitIndex.HEADER,
896
            "a option 0 1 :",
897
            "b option 1 2 0 .c :",
898
            "c option 1 2 1 0 .e :"
899
            ])
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
900
        index = self.get_knit_index(transport, "filename", "r")
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
901
3287.5.6 by Robert Collins
Remove _KnitIndex.get_parents.
902
        self.assertEqual({
903
            "a":(),
904
            "b":("a", "c"),
905
            "c":("b", "a", "e"),
906
            }, index.get_parent_map(["a", "b", "c"]))
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
907
908
    def test_get_parents_with_ghosts(self):
909
        transport = MockTransport([
910
            _KnitIndex.HEADER,
911
            "a option 0 1 :",
912
            "b option 1 2 0 .c :",
913
            "c option 1 2 1 0 .e :"
914
            ])
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
915
        index = self.get_knit_index(transport, "filename", "r")
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
916
3287.5.5 by Robert Collins
Refactor internals of knit implementations to implement get_parents_with_ghosts in terms of get_parent_map.
917
        self.assertEqual((), index.get_parents_with_ghosts("a"))
918
        self.assertEqual(("a", "c"), index.get_parents_with_ghosts("b"))
919
        self.assertEqual(("b", "a", "e"),
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
920
            index.get_parents_with_ghosts("c"))
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
921
922
    def test_check_versions_present(self):
923
        transport = MockTransport([
924
            _KnitIndex.HEADER,
925
            "a option 0 1 :",
926
            "b option 0 1 :"
927
            ])
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
928
        index = self.get_knit_index(transport, "filename", "r")
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
929
930
        check = index.check_versions_present
931
932
        check([])
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
933
        check(["a"])
934
        check(["b"])
935
        check(["a", "b"])
936
        self.assertRaises(RevisionNotPresent, check, ["c"])
937
        self.assertRaises(RevisionNotPresent, check, ["a", "b", "c"])
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
938
2484.1.13 by John Arbash Meinel
Add a test that KnitCorrupt is raised when parent strings are invalid.
939
    def test_impossible_parent(self):
940
        """Test we get KnitCorrupt if the parent couldn't possibly exist."""
941
        transport = MockTransport([
942
            _KnitIndex.HEADER,
943
            "a option 0 1 :",
944
            "b option 0 1 4 :"  # We don't have a 4th record
945
            ])
2484.1.17 by John Arbash Meinel
Workaround for Pyrex <0.9.5 and python >=2.5 incompatibilities.
946
        try:
947
            self.assertRaises(errors.KnitCorrupt,
948
                              self.get_knit_index, transport, 'filename', 'r')
949
        except TypeError, e:
950
            if (str(e) == ('exceptions must be strings, classes, or instances,'
951
                           ' not exceptions.IndexError')
952
                and sys.version_info[0:2] >= (2,5)):
953
                self.knownFailure('Pyrex <0.9.5 fails with TypeError when'
954
                                  ' raising new style exceptions with python'
955
                                  ' >=2.5')
2484.1.19 by John Arbash Meinel
Don't suppress the TypeError if it doesn't match our requirements.
956
            else:
957
                raise
2484.1.13 by John Arbash Meinel
Add a test that KnitCorrupt is raised when parent strings are invalid.
958
959
    def test_corrupted_parent(self):
960
        transport = MockTransport([
961
            _KnitIndex.HEADER,
962
            "a option 0 1 :",
963
            "b option 0 1 :",
964
            "c option 0 1 1v :", # Can't have a parent of '1v'
965
            ])
2484.1.17 by John Arbash Meinel
Workaround for Pyrex <0.9.5 and python >=2.5 incompatibilities.
966
        try:
967
            self.assertRaises(errors.KnitCorrupt,
968
                              self.get_knit_index, transport, 'filename', 'r')
969
        except TypeError, e:
970
            if (str(e) == ('exceptions must be strings, classes, or instances,'
971
                           ' not exceptions.ValueError')
972
                and sys.version_info[0:2] >= (2,5)):
973
                self.knownFailure('Pyrex <0.9.5 fails with TypeError when'
974
                                  ' raising new style exceptions with python'
975
                                  ' >=2.5')
2484.1.19 by John Arbash Meinel
Don't suppress the TypeError if it doesn't match our requirements.
976
            else:
977
                raise
2484.1.13 by John Arbash Meinel
Add a test that KnitCorrupt is raised when parent strings are invalid.
978
979
    def test_corrupted_parent_in_list(self):
980
        transport = MockTransport([
981
            _KnitIndex.HEADER,
982
            "a option 0 1 :",
983
            "b option 0 1 :",
2484.1.17 by John Arbash Meinel
Workaround for Pyrex <0.9.5 and python >=2.5 incompatibilities.
984
            "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.
985
            ])
2484.1.17 by John Arbash Meinel
Workaround for Pyrex <0.9.5 and python >=2.5 incompatibilities.
986
        try:
987
            self.assertRaises(errors.KnitCorrupt,
988
                              self.get_knit_index, transport, 'filename', 'r')
989
        except TypeError, e:
990
            if (str(e) == ('exceptions must be strings, classes, or instances,'
991
                           ' not exceptions.ValueError')
992
                and sys.version_info[0:2] >= (2,5)):
993
                self.knownFailure('Pyrex <0.9.5 fails with TypeError when'
994
                                  ' raising new style exceptions with python'
995
                                  ' >=2.5')
2484.1.19 by John Arbash Meinel
Don't suppress the TypeError if it doesn't match our requirements.
996
            else:
997
                raise
2484.1.13 by John Arbash Meinel
Add a test that KnitCorrupt is raised when parent strings are invalid.
998
2484.1.18 by John Arbash Meinel
Test that we properly verify the size and position strings.
999
    def test_invalid_position(self):
1000
        transport = MockTransport([
1001
            _KnitIndex.HEADER,
1002
            "a option 1v 1 :",
1003
            ])
1004
        try:
1005
            self.assertRaises(errors.KnitCorrupt,
1006
                              self.get_knit_index, transport, 'filename', 'r')
1007
        except TypeError, e:
1008
            if (str(e) == ('exceptions must be strings, classes, or instances,'
1009
                           ' not exceptions.ValueError')
1010
                and sys.version_info[0:2] >= (2,5)):
1011
                self.knownFailure('Pyrex <0.9.5 fails with TypeError when'
1012
                                  ' raising new style exceptions with python'
1013
                                  ' >=2.5')
2484.1.19 by John Arbash Meinel
Don't suppress the TypeError if it doesn't match our requirements.
1014
            else:
1015
                raise
2484.1.18 by John Arbash Meinel
Test that we properly verify the size and position strings.
1016
1017
    def test_invalid_size(self):
1018
        transport = MockTransport([
1019
            _KnitIndex.HEADER,
1020
            "a option 1 1v :",
1021
            ])
1022
        try:
1023
            self.assertRaises(errors.KnitCorrupt,
1024
                              self.get_knit_index, transport, 'filename', 'r')
1025
        except TypeError, e:
1026
            if (str(e) == ('exceptions must be strings, classes, or instances,'
1027
                           ' not exceptions.ValueError')
1028
                and sys.version_info[0:2] >= (2,5)):
1029
                self.knownFailure('Pyrex <0.9.5 fails with TypeError when'
1030
                                  ' raising new style exceptions with python'
1031
                                  ' >=2.5')
2484.1.19 by John Arbash Meinel
Don't suppress the TypeError if it doesn't match our requirements.
1032
            else:
1033
                raise
2484.1.18 by John Arbash Meinel
Test that we properly verify the size and position strings.
1034
2484.1.24 by John Arbash Meinel
Add direct tests of how we handle incomplete/'broken' lines
1035
    def test_short_line(self):
1036
        transport = MockTransport([
1037
            _KnitIndex.HEADER,
1038
            "a option 0 10  :",
1039
            "b option 10 10 0", # This line isn't terminated, ignored
1040
            ])
1041
        index = self.get_knit_index(transport, "filename", "r")
1042
        self.assertEqual(['a'], index.get_versions())
1043
1044
    def test_skip_incomplete_record(self):
1045
        # A line with bogus data should just be skipped
1046
        transport = MockTransport([
1047
            _KnitIndex.HEADER,
1048
            "a option 0 10  :",
1049
            "b option 10 10 0", # This line isn't terminated, ignored
1050
            "c option 20 10 0 :", # Properly terminated, and starts with '\n'
1051
            ])
1052
        index = self.get_knit_index(transport, "filename", "r")
1053
        self.assertEqual(['a', 'c'], index.get_versions())
1054
1055
    def test_trailing_characters(self):
1056
        # A line with bogus data should just be skipped
1057
        transport = MockTransport([
1058
            _KnitIndex.HEADER,
1059
            "a option 0 10  :",
1060
            "b option 10 10 0 :a", # This line has extra trailing characters
1061
            "c option 20 10 0 :", # Properly terminated, and starts with '\n'
1062
            ])
1063
        index = self.get_knit_index(transport, "filename", "r")
1064
        self.assertEqual(['a', 'c'], index.get_versions())
1065
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
1066
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
1067
class LowLevelKnitIndexTests_c(LowLevelKnitIndexTests):
1068
1069
    _test_needs_features = [CompiledKnitFeature]
1070
1071
    def get_knit_index(self, *args, **kwargs):
1072
        orig = knit._load_data
1073
        def reset():
1074
            knit._load_data = orig
1075
        self.addCleanup(reset)
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
1076
        from bzrlib._knit_load_data_c import _load_data_c
1077
        knit._load_data = _load_data_c
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
1078
        return _KnitIndex(*args, **kwargs)
1079
1080
1081
1684.3.3 by Robert Collins
Add a special cased weaves to knit converter.
1082
class KnitTests(TestCaseWithTransport):
1083
    """Class containing knit test helper routines."""
1563.2.16 by Robert Collins
Change WeaveStore into VersionedFileStore and make its versoined file class parameterisable.
1084
2535.3.27 by Andrew Bennetts
Merge from bzr.dev.
1085
    def make_test_knit(self, annotate=False, delay_create=False, index=None,
1086
                       name='test'):
1563.2.16 by Robert Collins
Change WeaveStore into VersionedFileStore and make its versoined file class parameterisable.
1087
        if not annotate:
1088
            factory = KnitPlainFactory()
1089
        else:
1090
            factory = None
2535.3.4 by Andrew Bennetts
Simple implementation of Knit.insert_data_stream.
1091
        return KnitVersionedFile(name, get_transport('.'), access_mode='w',
1946.2.1 by John Arbash Meinel
2 changes to knits. Delay creating the .knit or .kndx file until we have actually tried to write data. Because of this, we must allow the Knit to create the prefix directories
1092
                                 factory=factory, create=True,
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.
1093
                                 delay_create=delay_create, index=index)
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1094
2535.3.53 by Andrew Bennetts
Remove get_stream_as_bytes from KnitVersionedFile's API, make it a function in knitrepo.py instead.
1095
    def assertRecordContentEqual(self, knit, version_id, candidate_content):
1096
        """Assert that some raw record content matches the raw record content
1097
        for a particular version_id in the given knit.
1098
        """
1099
        index_memo = knit._index.get_position(version_id)
1100
        record = (version_id, index_memo)
1101
        [(_, expected_content)] = list(knit._data.read_records_iter_raw([record]))
1102
        self.assertEqual(expected_content, candidate_content)
1103
1684.3.3 by Robert Collins
Add a special cased weaves to knit converter.
1104
1105
class BasicKnitTests(KnitTests):
1106
1107
    def add_stock_one_and_one_a(self, k):
1108
        k.add_lines('text-1', [], split_lines(TEXT_1))
1109
        k.add_lines('text-1a', ['text-1'], split_lines(TEXT_1A))
1110
1111
    def test_knit_constructor(self):
1112
        """Construct empty k"""
1113
        self.make_test_knit()
1114
2592.3.1 by Robert Collins
Allow giving KnitVersionedFile an index object to use rather than implicitly creating one.
1115
    def test_make_explicit_index(self):
1116
        """We can supply an index to use."""
1117
        knit = KnitVersionedFile('test', get_transport('.'),
1118
            index='strangelove')
1119
        self.assertEqual(knit._index, 'strangelove')
1120
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1121
    def test_knit_add(self):
1122
        """Store one text in knit and retrieve"""
1563.2.16 by Robert Collins
Change WeaveStore into VersionedFileStore and make its versoined file class parameterisable.
1123
        k = self.make_test_knit()
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1124
        k.add_lines('text-1', [], split_lines(TEXT_1))
1125
        self.assertTrue(k.has_version('text-1'))
1126
        self.assertEqualDiff(''.join(k.get_lines('text-1')), TEXT_1)
1127
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
1128
    def test_newline_empty_lines(self):
1129
        # ensure that ["\n"] round trips ok.
1130
        knit = self.make_test_knit()
1131
        knit.add_lines('a', [], ["\n"])
1132
        knit.add_lines_with_ghosts('b', [], ["\n"])
1133
        self.assertEqual(["\n"], knit.get_lines('a'))
1134
        self.assertEqual(["\n"], knit.get_lines('b'))
1135
        self.assertEqual(['fulltext'], knit._index.get_options('a'))
1136
        self.assertEqual(['fulltext'], knit._index.get_options('b'))
1137
        knit.add_lines('c', ['a'], ["\n"])
1138
        knit.add_lines_with_ghosts('d', ['b'], ["\n"])
1139
        self.assertEqual(["\n"], knit.get_lines('c'))
1140
        self.assertEqual(["\n"], knit.get_lines('d'))
1141
        self.assertEqual(['line-delta'], knit._index.get_options('c'))
1142
        self.assertEqual(['line-delta'], knit._index.get_options('d'))
1143
1144
    def test_empty_lines(self):
1145
        # bizarrely, [] is not listed as having no-eol. 
1146
        knit = self.make_test_knit()
1147
        knit.add_lines('a', [], [])
1148
        knit.add_lines_with_ghosts('b', [], [])
1149
        self.assertEqual([], knit.get_lines('a'))
1150
        self.assertEqual([], knit.get_lines('b'))
1151
        self.assertEqual(['fulltext'], knit._index.get_options('a'))
1152
        self.assertEqual(['fulltext'], knit._index.get_options('b'))
1153
        knit.add_lines('c', ['a'], [])
1154
        knit.add_lines_with_ghosts('d', ['b'], [])
1155
        self.assertEqual([], knit.get_lines('c'))
1156
        self.assertEqual([], knit.get_lines('d'))
1157
        self.assertEqual(['line-delta'], knit._index.get_options('c'))
1158
        self.assertEqual(['line-delta'], knit._index.get_options('d'))
1159
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1160
    def test_knit_reload(self):
1641.1.2 by Robert Collins
Change knit index files to be robust in the presence of partial writes.
1161
        # test that the content in a reloaded knit is correct
1563.2.16 by Robert Collins
Change WeaveStore into VersionedFileStore and make its versoined file class parameterisable.
1162
        k = self.make_test_knit()
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1163
        k.add_lines('text-1', [], split_lines(TEXT_1))
1164
        del k
1685.1.39 by John Arbash Meinel
Updating test_knit to not instantiate a LocalTransport directly.
1165
        k2 = KnitVersionedFile('test', get_transport('.'), access_mode='r', factory=KnitPlainFactory(), create=True)
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1166
        self.assertTrue(k2.has_version('text-1'))
1167
        self.assertEqualDiff(''.join(k2.get_lines('text-1')), TEXT_1)
1168
1169
    def test_knit_several(self):
1170
        """Store several texts in a knit"""
1563.2.16 by Robert Collins
Change WeaveStore into VersionedFileStore and make its versoined file class parameterisable.
1171
        k = self.make_test_knit()
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1172
        k.add_lines('text-1', [], split_lines(TEXT_1))
1173
        k.add_lines('text-2', [], split_lines(TEXT_2))
1174
        self.assertEqualDiff(''.join(k.get_lines('text-1')), TEXT_1)
1175
        self.assertEqualDiff(''.join(k.get_lines('text-2')), TEXT_2)
1176
        
1177
    def test_repeated_add(self):
1178
        """Knit traps attempt to replace existing version"""
1563.2.16 by Robert Collins
Change WeaveStore into VersionedFileStore and make its versoined file class parameterisable.
1179
        k = self.make_test_knit()
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1180
        k.add_lines('text-1', [], split_lines(TEXT_1))
1181
        self.assertRaises(RevisionAlreadyPresent, 
1182
                k.add_lines,
1183
                'text-1', [], split_lines(TEXT_1))
1184
1185
    def test_empty(self):
1563.2.16 by Robert Collins
Change WeaveStore into VersionedFileStore and make its versoined file class parameterisable.
1186
        k = self.make_test_knit(True)
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1187
        k.add_lines('text-1', [], [])
1188
        self.assertEquals(k.get_lines('text-1'), [])
1189
1190
    def test_incomplete(self):
1191
        """Test if texts without a ending line-end can be inserted and
1192
        extracted."""
1685.1.39 by John Arbash Meinel
Updating test_knit to not instantiate a LocalTransport directly.
1193
        k = KnitVersionedFile('test', get_transport('.'), delta=False, create=True)
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1194
        k.add_lines('text-1', [], ['a\n',    'b'  ])
1195
        k.add_lines('text-2', ['text-1'], ['a\rb\n', 'b\n'])
1666.1.6 by Robert Collins
Make knit the default format.
1196
        # reopening ensures maximum room for confusion
1685.1.39 by John Arbash Meinel
Updating test_knit to not instantiate a LocalTransport directly.
1197
        k = KnitVersionedFile('test', get_transport('.'), delta=False, create=True)
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1198
        self.assertEquals(k.get_lines('text-1'), ['a\n',    'b'  ])
1199
        self.assertEquals(k.get_lines('text-2'), ['a\rb\n', 'b\n'])
1200
1201
    def test_delta(self):
1202
        """Expression of knit delta as lines"""
1563.2.16 by Robert Collins
Change WeaveStore into VersionedFileStore and make its versoined file class parameterisable.
1203
        k = self.make_test_knit()
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1204
        td = list(line_delta(TEXT_1.splitlines(True),
1205
                             TEXT_1A.splitlines(True)))
1206
        self.assertEqualDiff(''.join(td), delta_1_1a)
1207
        out = apply_line_delta(TEXT_1.splitlines(True), td)
1208
        self.assertEqualDiff(''.join(out), TEXT_1A)
1209
1210
    def test_add_with_parents(self):
1211
        """Store in knit with parents"""
1563.2.16 by Robert Collins
Change WeaveStore into VersionedFileStore and make its versoined file class parameterisable.
1212
        k = self.make_test_knit()
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1213
        self.add_stock_one_and_one_a(k)
3287.5.2 by Robert Collins
Deprecate VersionedFile.get_parents, breaking pulling from a ghost containing knit or pack repository to weaves, which improves correctness and allows simplification of core code.
1214
        self.assertEqual({'text-1':(), 'text-1a':('text-1',)},
1215
            k.get_parent_map(['text-1', 'text-1a']))
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1216
1217
    def test_ancestry(self):
1218
        """Store in knit with parents"""
1563.2.16 by Robert Collins
Change WeaveStore into VersionedFileStore and make its versoined file class parameterisable.
1219
        k = self.make_test_knit()
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1220
        self.add_stock_one_and_one_a(k)
1221
        self.assertEquals(set(k.get_ancestry(['text-1a'])), set(['text-1a', 'text-1']))
1222
1223
    def test_add_delta(self):
1224
        """Store in knit with parents"""
1685.1.39 by John Arbash Meinel
Updating test_knit to not instantiate a LocalTransport directly.
1225
        k = KnitVersionedFile('test', get_transport('.'), factory=KnitPlainFactory(),
1563.2.25 by Robert Collins
Merge in upstream.
1226
            delta=True, create=True)
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1227
        self.add_stock_one_and_one_a(k)
1596.2.7 by Robert Collins
Remove the requirement for reannotation in knit joins.
1228
        k.clear_cache()
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1229
        self.assertEqualDiff(''.join(k.get_lines('text-1a')), TEXT_1A)
1230
2592.3.21 by Robert Collins
Test using a KnitGraphIndex for storage.
1231
    def test_add_delta_knit_graph_index(self):
1232
        """Does adding work with a KnitGraphIndex."""
1233
        index = InMemoryGraphIndex(2)
1234
        knit_index = KnitGraphIndex(index, add_callback=index.add_nodes,
1235
            deltas=True)
1236
        k = KnitVersionedFile('test', get_transport('.'),
1237
            delta=True, create=True, index=knit_index)
1238
        self.add_stock_one_and_one_a(k)
1239
        k.clear_cache()
1240
        self.assertEqualDiff(''.join(k.get_lines('text-1a')), TEXT_1A)
1241
        # check the index had the right data added.
1242
        self.assertEqual(set([
2624.2.14 by Robert Collins
Add source index to the index iteration API to allow mapping back to the origin of retrieved data.
1243
            (index, ('text-1', ), ' 0 127', ((), ())),
1244
            (index, ('text-1a', ), ' 127 140', ((('text-1', ),), (('text-1', ),))),
2592.3.21 by Robert Collins
Test using a KnitGraphIndex for storage.
1245
            ]), set(index.iter_all_entries()))
1246
        # we should not have a .kndx file
1247
        self.assertFalse(get_transport('.').has('test.kndx'))
1248
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1249
    def test_annotate(self):
1250
        """Annotations"""
1685.1.39 by John Arbash Meinel
Updating test_knit to not instantiate a LocalTransport directly.
1251
        k = KnitVersionedFile('knit', get_transport('.'), factory=KnitAnnotateFactory(),
1563.2.25 by Robert Collins
Merge in upstream.
1252
            delta=True, create=True)
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1253
        self.insert_and_test_small_annotate(k)
1254
1255
    def insert_and_test_small_annotate(self, k):
1256
        """test annotation with k works correctly."""
1257
        k.add_lines('text-1', [], ['a\n', 'b\n'])
1258
        k.add_lines('text-2', ['text-1'], ['a\n', 'c\n'])
1259
1260
        origins = k.annotate('text-2')
1261
        self.assertEquals(origins[0], ('text-1', 'a\n'))
1262
        self.assertEquals(origins[1], ('text-2', 'c\n'))
1263
1264
    def test_annotate_fulltext(self):
1265
        """Annotations"""
1685.1.39 by John Arbash Meinel
Updating test_knit to not instantiate a LocalTransport directly.
1266
        k = KnitVersionedFile('knit', get_transport('.'), factory=KnitAnnotateFactory(),
1563.2.25 by Robert Collins
Merge in upstream.
1267
            delta=False, create=True)
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1268
        self.insert_and_test_small_annotate(k)
1269
1270
    def test_annotate_merge_1(self):
1563.2.16 by Robert Collins
Change WeaveStore into VersionedFileStore and make its versoined file class parameterisable.
1271
        k = self.make_test_knit(True)
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1272
        k.add_lines('text-a1', [], ['a\n', 'b\n'])
1273
        k.add_lines('text-a2', [], ['d\n', 'c\n'])
1274
        k.add_lines('text-am', ['text-a1', 'text-a2'], ['d\n', 'b\n'])
1275
        origins = k.annotate('text-am')
1276
        self.assertEquals(origins[0], ('text-a2', 'd\n'))
1277
        self.assertEquals(origins[1], ('text-a1', 'b\n'))
1278
1279
    def test_annotate_merge_2(self):
1563.2.16 by Robert Collins
Change WeaveStore into VersionedFileStore and make its versoined file class parameterisable.
1280
        k = self.make_test_knit(True)
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1281
        k.add_lines('text-a1', [], ['a\n', 'b\n', 'c\n'])
1282
        k.add_lines('text-a2', [], ['x\n', 'y\n', 'z\n'])
1283
        k.add_lines('text-am', ['text-a1', 'text-a2'], ['a\n', 'y\n', 'c\n'])
1284
        origins = k.annotate('text-am')
1285
        self.assertEquals(origins[0], ('text-a1', 'a\n'))
1286
        self.assertEquals(origins[1], ('text-a2', 'y\n'))
1287
        self.assertEquals(origins[2], ('text-a1', 'c\n'))
1288
1289
    def test_annotate_merge_9(self):
1563.2.16 by Robert Collins
Change WeaveStore into VersionedFileStore and make its versoined file class parameterisable.
1290
        k = self.make_test_knit(True)
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1291
        k.add_lines('text-a1', [], ['a\n', 'b\n', 'c\n'])
1292
        k.add_lines('text-a2', [], ['x\n', 'y\n', 'z\n'])
1293
        k.add_lines('text-am', ['text-a1', 'text-a2'], ['k\n', 'y\n', 'c\n'])
1294
        origins = k.annotate('text-am')
1295
        self.assertEquals(origins[0], ('text-am', 'k\n'))
1296
        self.assertEquals(origins[1], ('text-a2', 'y\n'))
1297
        self.assertEquals(origins[2], ('text-a1', 'c\n'))
1298
1299
    def test_annotate_merge_3(self):
1563.2.16 by Robert Collins
Change WeaveStore into VersionedFileStore and make its versoined file class parameterisable.
1300
        k = self.make_test_knit(True)
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1301
        k.add_lines('text-a1', [], ['a\n', 'b\n', 'c\n'])
1302
        k.add_lines('text-a2', [] ,['x\n', 'y\n', 'z\n'])
1303
        k.add_lines('text-am', ['text-a1', 'text-a2'], ['k\n', 'y\n', 'z\n'])
1304
        origins = k.annotate('text-am')
1305
        self.assertEquals(origins[0], ('text-am', 'k\n'))
1306
        self.assertEquals(origins[1], ('text-a2', 'y\n'))
1307
        self.assertEquals(origins[2], ('text-a2', 'z\n'))
1308
1309
    def test_annotate_merge_4(self):
1563.2.16 by Robert Collins
Change WeaveStore into VersionedFileStore and make its versoined file class parameterisable.
1310
        k = self.make_test_knit(True)
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1311
        k.add_lines('text-a1', [], ['a\n', 'b\n', 'c\n'])
1312
        k.add_lines('text-a2', [], ['x\n', 'y\n', 'z\n'])
1313
        k.add_lines('text-a3', ['text-a1'], ['a\n', 'b\n', 'p\n'])
1314
        k.add_lines('text-am', ['text-a2', 'text-a3'], ['a\n', 'b\n', 'z\n'])
1315
        origins = k.annotate('text-am')
1316
        self.assertEquals(origins[0], ('text-a1', 'a\n'))
1317
        self.assertEquals(origins[1], ('text-a1', 'b\n'))
1318
        self.assertEquals(origins[2], ('text-a2', 'z\n'))
1319
1320
    def test_annotate_merge_5(self):
1563.2.16 by Robert Collins
Change WeaveStore into VersionedFileStore and make its versoined file class parameterisable.
1321
        k = self.make_test_knit(True)
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1322
        k.add_lines('text-a1', [], ['a\n', 'b\n', 'c\n'])
1323
        k.add_lines('text-a2', [], ['d\n', 'e\n', 'f\n'])
1324
        k.add_lines('text-a3', [], ['x\n', 'y\n', 'z\n'])
1325
        k.add_lines('text-am',
1326
                    ['text-a1', 'text-a2', 'text-a3'],
1327
                    ['a\n', 'e\n', 'z\n'])
1328
        origins = k.annotate('text-am')
1329
        self.assertEquals(origins[0], ('text-a1', 'a\n'))
1330
        self.assertEquals(origins[1], ('text-a2', 'e\n'))
1331
        self.assertEquals(origins[2], ('text-a3', 'z\n'))
1332
1333
    def test_annotate_file_cherry_pick(self):
1563.2.16 by Robert Collins
Change WeaveStore into VersionedFileStore and make its versoined file class parameterisable.
1334
        k = self.make_test_knit(True)
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1335
        k.add_lines('text-1', [], ['a\n', 'b\n', 'c\n'])
1336
        k.add_lines('text-2', ['text-1'], ['d\n', 'e\n', 'f\n'])
1337
        k.add_lines('text-3', ['text-2', 'text-1'], ['a\n', 'b\n', 'c\n'])
1338
        origins = k.annotate('text-3')
1339
        self.assertEquals(origins[0], ('text-1', 'a\n'))
1340
        self.assertEquals(origins[1], ('text-1', 'b\n'))
1341
        self.assertEquals(origins[2], ('text-1', 'c\n'))
1342
2851.4.6 by Ian Clatworthy
review tweaks
1343
    def _test_join_with_factories(self, k1_factory, k2_factory):
2851.4.1 by Ian Clatworthy
Support joining plain knits to annotated knits and vice versa
1344
        k1 = KnitVersionedFile('test1', get_transport('.'), factory=k1_factory, create=True)
1345
        k1.add_lines('text-a', [], ['a1\n', 'a2\n', 'a3\n'])
1346
        k1.add_lines('text-b', ['text-a'], ['a1\n', 'b2\n', 'a3\n'])
1347
        k1.add_lines('text-c', [], ['c1\n', 'c2\n', 'c3\n'])
1348
        k1.add_lines('text-d', ['text-c'], ['c1\n', 'd2\n', 'd3\n'])
1349
        k1.add_lines('text-m', ['text-b', 'text-d'], ['a1\n', 'b2\n', 'd3\n'])
1350
        k2 = KnitVersionedFile('test2', get_transport('.'), factory=k2_factory, create=True)
2851.4.6 by Ian Clatworthy
review tweaks
1351
        count = k2.join(k1, version_ids=['text-m'])
1352
        self.assertEquals(count, 5)
1353
        self.assertTrue(k2.has_version('text-a'))
1354
        self.assertTrue(k2.has_version('text-c'))
1355
        origins = k2.annotate('text-m')
1356
        self.assertEquals(origins[0], ('text-a', 'a1\n'))
1357
        self.assertEquals(origins[1], ('text-b', 'b2\n'))
1358
        self.assertEquals(origins[2], ('text-d', 'd3\n'))
2851.4.1 by Ian Clatworthy
Support joining plain knits to annotated knits and vice versa
1359
1360
    def test_knit_join_plain_to_plain(self):
1361
        """Test joining a plain knit with a plain knit."""
2851.4.6 by Ian Clatworthy
review tweaks
1362
        self._test_join_with_factories(KnitPlainFactory(), KnitPlainFactory())
2851.4.1 by Ian Clatworthy
Support joining plain knits to annotated knits and vice versa
1363
1364
    def test_knit_join_anno_to_anno(self):
1365
        """Test joining an annotated knit with an annotated knit."""
2851.4.6 by Ian Clatworthy
review tweaks
1366
        self._test_join_with_factories(None, None)
2851.4.1 by Ian Clatworthy
Support joining plain knits to annotated knits and vice versa
1367
1368
    def test_knit_join_anno_to_plain(self):
1369
        """Test joining an annotated knit with a plain knit."""
2851.4.6 by Ian Clatworthy
review tweaks
1370
        self._test_join_with_factories(None, KnitPlainFactory())
2851.4.1 by Ian Clatworthy
Support joining plain knits to annotated knits and vice versa
1371
1372
    def test_knit_join_plain_to_anno(self):
1373
        """Test joining a plain knit with an annotated knit."""
2851.4.6 by Ian Clatworthy
review tweaks
1374
        self._test_join_with_factories(KnitPlainFactory(), None)
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1375
1376
    def test_reannotate(self):
1685.1.39 by John Arbash Meinel
Updating test_knit to not instantiate a LocalTransport directly.
1377
        k1 = KnitVersionedFile('knit1', get_transport('.'),
1563.2.25 by Robert Collins
Merge in upstream.
1378
                               factory=KnitAnnotateFactory(), create=True)
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1379
        # 0
1380
        k1.add_lines('text-a', [], ['a\n', 'b\n'])
1381
        # 1
1382
        k1.add_lines('text-b', ['text-a'], ['a\n', 'c\n'])
1383
1685.1.39 by John Arbash Meinel
Updating test_knit to not instantiate a LocalTransport directly.
1384
        k2 = KnitVersionedFile('test2', get_transport('.'),
1563.2.25 by Robert Collins
Merge in upstream.
1385
                               factory=KnitAnnotateFactory(), create=True)
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1386
        k2.join(k1, version_ids=['text-b'])
1387
1388
        # 2
1389
        k1.add_lines('text-X', ['text-b'], ['a\n', 'b\n'])
1390
        # 2
1391
        k2.add_lines('text-c', ['text-b'], ['z\n', 'c\n'])
1392
        # 3
1393
        k2.add_lines('text-Y', ['text-b'], ['b\n', 'c\n'])
1394
1395
        # test-c will have index 3
1396
        k1.join(k2, version_ids=['text-c'])
1397
1398
        lines = k1.get_lines('text-c')
1399
        self.assertEquals(lines, ['z\n', 'c\n'])
1400
1401
        origins = k1.annotate('text-c')
1594.2.24 by Robert Collins
Make use of the transaction finalisation warning support to implement in-knit caching.
1402
        self.assertEquals(origins[0], ('text-c', 'z\n'))
1403
        self.assertEquals(origins[1], ('text-b', 'c\n'))
1404
1756.3.4 by Aaron Bentley
Fix bug getting texts when line deltas were reused
1405
    def test_get_line_delta_texts(self):
1406
        """Make sure we can call get_texts on text with reused line deltas"""
1407
        k1 = KnitVersionedFile('test1', get_transport('.'), 
1408
                               factory=KnitPlainFactory(), create=True)
1409
        for t in range(3):
1410
            if t == 0:
1411
                parents = []
1412
            else:
1413
                parents = ['%d' % (t-1)]
1414
            k1.add_lines('%d' % t, parents, ['hello\n'] * t)
1415
        k1.get_texts(('%d' % t) for t in range(3))
1594.3.1 by Robert Collins
Merge transaction finalisation and ensure iter_lines_added_or_present in knits does a old-to-new read in the knit.
1416
        
1417
    def test_iter_lines_reads_in_order(self):
2745.5.3 by Robert Collins
* Move transport logging into a new transport class
1418
        instrumented_t = get_transport('trace+memory:///')
1594.3.1 by Robert Collins
Merge transaction finalisation and ensure iter_lines_added_or_present in knits does a old-to-new read in the knit.
1419
        k1 = KnitVersionedFile('id', instrumented_t, create=True, delta=True)
2745.5.3 by Robert Collins
* Move transport logging into a new transport class
1420
        self.assertEqual([('get', 'id.kndx',)], instrumented_t._activity)
1594.3.1 by Robert Collins
Merge transaction finalisation and ensure iter_lines_added_or_present in knits does a old-to-new read in the knit.
1421
        # add texts with no required ordering
1422
        k1.add_lines('base', [], ['text\n'])
1423
        k1.add_lines('base2', [], ['text2\n'])
1424
        k1.clear_cache()
2745.5.4 by Robert Collins
Review feedback.
1425
        # clear the logged activity, but preserve the list instance in case of
1426
        # clones pointing at it.
2745.5.3 by Robert Collins
* Move transport logging into a new transport class
1427
        del instrumented_t._activity[:]
1594.3.1 by Robert Collins
Merge transaction finalisation and ensure iter_lines_added_or_present in knits does a old-to-new read in the knit.
1428
        # request a last-first iteration
2745.5.3 by Robert Collins
* Move transport logging into a new transport class
1429
        results = list(k1.iter_lines_added_or_present_in_versions(
1430
            ['base2', 'base']))
2745.5.6 by Robert Collins
Fix knit test fallout from final readv api change.
1431
        self.assertEqual(
1432
            [('readv', 'id.knit', [(0, 87), (87, 89)], False, None)],
2745.5.3 by Robert Collins
* Move transport logging into a new transport class
1433
            instrumented_t._activity)
2975.3.1 by Robert Collins
Change (without backwards compatibility) the
1434
        self.assertEqual([('text\n', 'base'), ('text2\n', 'base2')], results)
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1435
1641.1.2 by Robert Collins
Change knit index files to be robust in the presence of partial writes.
1436
    def test_knit_format(self):
1437
        # this tests that a new knit index file has the expected content
1438
        # and that is writes the data we expect as records are added.
1439
        knit = self.make_test_knit(True)
1946.2.1 by John Arbash Meinel
2 changes to knits. Delay creating the .knit or .kndx file until we have actually tried to write data. Because of this, we must allow the Knit to create the prefix directories
1440
        # Now knit files are not created until we first add data to them
1666.1.6 by Robert Collins
Make knit the default format.
1441
        self.assertFileEqual("# bzr knit index 8\n", 'test.kndx')
1641.1.2 by Robert Collins
Change knit index files to be robust in the presence of partial writes.
1442
        knit.add_lines_with_ghosts('revid', ['a_ghost'], ['a\n'])
1443
        self.assertFileEqual(
1666.1.6 by Robert Collins
Make knit the default format.
1444
            "# bzr knit index 8\n"
1641.1.2 by Robert Collins
Change knit index files to be robust in the presence of partial writes.
1445
            "\n"
1446
            "revid fulltext 0 84 .a_ghost :",
1447
            'test.kndx')
1448
        knit.add_lines_with_ghosts('revid2', ['revid'], ['a\n'])
1449
        self.assertFileEqual(
1666.1.6 by Robert Collins
Make knit the default format.
1450
            "# bzr knit index 8\n"
1641.1.2 by Robert Collins
Change knit index files to be robust in the presence of partial writes.
1451
            "\nrevid fulltext 0 84 .a_ghost :"
1452
            "\nrevid2 line-delta 84 82 0 :",
1453
            'test.kndx')
1454
        # we should be able to load this file again
1685.1.39 by John Arbash Meinel
Updating test_knit to not instantiate a LocalTransport directly.
1455
        knit = KnitVersionedFile('test', get_transport('.'), access_mode='r')
1641.1.2 by Robert Collins
Change knit index files to be robust in the presence of partial writes.
1456
        self.assertEqual(['revid', 'revid2'], knit.versions())
1457
        # write a short write to the file and ensure that its ignored
2484.1.23 by John Arbash Meinel
When we append a new line, don't use text mode
1458
        indexfile = file('test.kndx', 'ab')
1641.1.2 by Robert Collins
Change knit index files to be robust in the presence of partial writes.
1459
        indexfile.write('\nrevid3 line-delta 166 82 1 2 3 4 5 .phwoar:demo ')
1460
        indexfile.close()
1461
        # we should be able to load this file again
1685.1.39 by John Arbash Meinel
Updating test_knit to not instantiate a LocalTransport directly.
1462
        knit = KnitVersionedFile('test', get_transport('.'), access_mode='w')
1654.1.5 by Robert Collins
Merge partial index write support for knits, adding a test case per review comments.
1463
        self.assertEqual(['revid', 'revid2'], knit.versions())
1464
        # and add a revision with the same id the failed write had
1465
        knit.add_lines('revid3', ['revid2'], ['a\n'])
1466
        # and when reading it revid3 should now appear.
1685.1.39 by John Arbash Meinel
Updating test_knit to not instantiate a LocalTransport directly.
1467
        knit = KnitVersionedFile('test', get_transport('.'), access_mode='r')
1654.1.5 by Robert Collins
Merge partial index write support for knits, adding a test case per review comments.
1468
        self.assertEqual(['revid', 'revid2', 'revid3'], knit.versions())
3287.5.2 by Robert Collins
Deprecate VersionedFile.get_parents, breaking pulling from a ghost containing knit or pack repository to weaves, which improves correctness and allows simplification of core code.
1469
        self.assertEqual({'revid3':('revid2',)}, knit.get_parent_map(['revid3']))
1654.1.5 by Robert Collins
Merge partial index write support for knits, adding a test case per review comments.
1470
1946.2.1 by John Arbash Meinel
2 changes to knits. Delay creating the .knit or .kndx file until we have actually tried to write data. Because of this, we must allow the Knit to create the prefix directories
1471
    def test_delay_create(self):
1472
        """Test that passing delay_create=True creates files late"""
1473
        knit = self.make_test_knit(annotate=True, delay_create=True)
1474
        self.failIfExists('test.knit')
1475
        self.failIfExists('test.kndx')
1476
        knit.add_lines_with_ghosts('revid', ['a_ghost'], ['a\n'])
1477
        self.failUnlessExists('test.knit')
1478
        self.assertFileEqual(
1479
            "# bzr knit index 8\n"
1480
            "\n"
1481
            "revid fulltext 0 84 .a_ghost :",
1482
            'test.kndx')
1483
1946.2.2 by John Arbash Meinel
test delay_create does the right thing
1484
    def test_create_parent_dir(self):
1485
        """create_parent_dir can create knits in nonexistant dirs"""
1486
        # Has no effect if we don't set 'delay_create'
1487
        trans = get_transport('.')
1488
        self.assertRaises(NoSuchFile, KnitVersionedFile, 'dir/test',
1489
                          trans, access_mode='w', factory=None,
1490
                          create=True, create_parent_dir=True)
1491
        # Nothing should have changed yet
1492
        knit = KnitVersionedFile('dir/test', trans, access_mode='w',
1493
                                 factory=None, create=True,
1494
                                 create_parent_dir=True,
1495
                                 delay_create=True)
1496
        self.failIfExists('dir/test.knit')
1497
        self.failIfExists('dir/test.kndx')
1498
        self.failIfExists('dir')
1499
        knit.add_lines('revid', [], ['a\n'])
1500
        self.failUnlessExists('dir')
1501
        self.failUnlessExists('dir/test.knit')
1502
        self.assertFileEqual(
1503
            "# bzr knit index 8\n"
1504
            "\n"
1505
            "revid fulltext 0 84  :",
1506
            'dir/test.kndx')
1507
1946.2.13 by John Arbash Meinel
Test that passing modes does the right thing for knits.
1508
    def test_create_mode_700(self):
1509
        trans = get_transport('.')
1510
        if not trans._can_roundtrip_unix_modebits():
1511
            # Can't roundtrip, so no need to run this test
1512
            return
1513
        knit = KnitVersionedFile('dir/test', trans, access_mode='w',
1514
                                 factory=None, create=True,
1515
                                 create_parent_dir=True,
1516
                                 delay_create=True,
1517
                                 file_mode=0600,
1518
                                 dir_mode=0700)
1519
        knit.add_lines('revid', [], ['a\n'])
1520
        self.assertTransportMode(trans, 'dir', 0700)
1521
        self.assertTransportMode(trans, 'dir/test.knit', 0600)
1522
        self.assertTransportMode(trans, 'dir/test.kndx', 0600)
1523
1524
    def test_create_mode_770(self):
1525
        trans = get_transport('.')
1526
        if not trans._can_roundtrip_unix_modebits():
1527
            # Can't roundtrip, so no need to run this test
1528
            return
1529
        knit = KnitVersionedFile('dir/test', trans, access_mode='w',
1530
                                 factory=None, create=True,
1531
                                 create_parent_dir=True,
1532
                                 delay_create=True,
1533
                                 file_mode=0660,
1534
                                 dir_mode=0770)
1535
        knit.add_lines('revid', [], ['a\n'])
1536
        self.assertTransportMode(trans, 'dir', 0770)
1537
        self.assertTransportMode(trans, 'dir/test.knit', 0660)
1538
        self.assertTransportMode(trans, 'dir/test.kndx', 0660)
1539
1540
    def test_create_mode_777(self):
1541
        trans = get_transport('.')
1542
        if not trans._can_roundtrip_unix_modebits():
1543
            # Can't roundtrip, so no need to run this test
1544
            return
1545
        knit = KnitVersionedFile('dir/test', trans, access_mode='w',
1546
                                 factory=None, create=True,
1547
                                 create_parent_dir=True,
1548
                                 delay_create=True,
1549
                                 file_mode=0666,
1550
                                 dir_mode=0777)
1551
        knit.add_lines('revid', [], ['a\n'])
1552
        self.assertTransportMode(trans, 'dir', 0777)
1553
        self.assertTransportMode(trans, 'dir/test.knit', 0666)
1554
        self.assertTransportMode(trans, 'dir/test.kndx', 0666)
1555
1664.2.1 by Aaron Bentley
Start work on plan_merge test
1556
    def test_plan_merge(self):
1557
        my_knit = self.make_test_knit(annotate=True)
1558
        my_knit.add_lines('text1', [], split_lines(TEXT_1))
1559
        my_knit.add_lines('text1a', ['text1'], split_lines(TEXT_1A))
1560
        my_knit.add_lines('text1b', ['text1'], split_lines(TEXT_1B))
1664.2.3 by Aaron Bentley
Add failing test case
1561
        plan = list(my_knit.plan_merge('text1a', 'text1b'))
1664.2.6 by Aaron Bentley
Got plan-merge passing tests
1562
        for plan_line, expected_line in zip(plan, AB_MERGE):
1563
            self.assertEqual(plan_line, expected_line)
1641.1.2 by Robert Collins
Change knit index files to be robust in the presence of partial writes.
1564
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1565
    def test_get_stream_empty(self):
1566
        """Get a data stream for an empty knit file."""
1567
        k1 = self.make_test_knit()
1568
        format, data_list, reader_callable = k1.get_data_stream([])
2535.3.17 by Andrew Bennetts
[broken] Closer to a working Repository.fetch_revisions smart request.
1569
        self.assertEqual('knit-plain', format)
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1570
        self.assertEqual([], data_list)
1571
        content = reader_callable(None)
1572
        self.assertEqual('', content)
1573
        self.assertIsInstance(content, str)
1574
1575
    def test_get_stream_one_version(self):
1576
        """Get a data stream for a single record out of a knit containing just
1577
        one record.
1578
        """
1579
        k1 = self.make_test_knit()
1580
        test_data = [
1581
            ('text-a', [], TEXT_1),
1582
            ]
1583
        expected_data_list = [
1584
            # version, options, length, parents
3287.5.5 by Robert Collins
Refactor internals of knit implementations to implement get_parents_with_ghosts in terms of get_parent_map.
1585
            ('text-a', ['fulltext'], 122, ()),
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1586
           ]
1587
        for version_id, parents, lines in test_data:
1588
            k1.add_lines(version_id, parents, split_lines(lines))
1589
1590
        format, data_list, reader_callable = k1.get_data_stream(['text-a'])
2535.3.17 by Andrew Bennetts
[broken] Closer to a working Repository.fetch_revisions smart request.
1591
        self.assertEqual('knit-plain', format)
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1592
        self.assertEqual(expected_data_list, data_list)
1593
        # There's only one record in the knit, so the content should be the
1594
        # entire knit data file's contents.
2535.3.36 by Andrew Bennetts
Merge bzr.dev
1595
        self.assertEqual(k1.transport.get_bytes(k1._data._access._filename),
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1596
                         reader_callable(None))
1597
        
1598
    def test_get_stream_get_one_version_of_many(self):
1599
        """Get a data stream for just one version out of a knit containing many
1600
        versions.
1601
        """
1602
        k1 = self.make_test_knit()
1603
        # Insert the same data as test_knit_join, as they seem to cover a range
1604
        # of cases (no parents, one parent, multiple parents).
1605
        test_data = [
1606
            ('text-a', [], TEXT_1),
1607
            ('text-b', ['text-a'], TEXT_1),
1608
            ('text-c', [], TEXT_1),
1609
            ('text-d', ['text-c'], TEXT_1),
1610
            ('text-m', ['text-b', 'text-d'], TEXT_1),
1611
            ]
1612
        expected_data_list = [
1613
            # version, options, length, parents
3287.5.5 by Robert Collins
Refactor internals of knit implementations to implement get_parents_with_ghosts in terms of get_parent_map.
1614
            ('text-m', ['line-delta'], 84, ('text-b', 'text-d')),
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1615
            ]
1616
        for version_id, parents, lines in test_data:
1617
            k1.add_lines(version_id, parents, split_lines(lines))
1618
1619
        format, data_list, reader_callable = k1.get_data_stream(['text-m'])
2535.3.17 by Andrew Bennetts
[broken] Closer to a working Repository.fetch_revisions smart request.
1620
        self.assertEqual('knit-plain', format)
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1621
        self.assertEqual(expected_data_list, data_list)
1622
        self.assertRecordContentEqual(k1, 'text-m', reader_callable(None))
1623
        
3023.2.2 by Martin Pool
Fix KnitVersionedFile.get_data_stream to not assume .versions() is sorted. (lp:165106)
1624
    def test_get_data_stream_unordered_index(self):
1625
        """Get a data stream when the knit index reports versions out of order.
1626
1627
        https://bugs.launchpad.net/bzr/+bug/164637
1628
        """
1629
        k1 = self.make_test_knit()
1630
        test_data = [
1631
            ('text-a', [], TEXT_1),
1632
            ('text-b', ['text-a'], TEXT_1),
1633
            ('text-c', [], TEXT_1),
1634
            ('text-d', ['text-c'], TEXT_1),
1635
            ('text-m', ['text-b', 'text-d'], TEXT_1),
1636
            ]
1637
        for version_id, parents, lines in test_data:
1638
            k1.add_lines(version_id, parents, split_lines(lines))
1639
        # monkey-patch versions method to return out of order, as if coming
1640
        # from multiple independently indexed packs
1641
        original_versions = k1.versions
1642
        k1.versions = lambda: reversed(original_versions())
1643
        expected_data_list = [
3287.5.5 by Robert Collins
Refactor internals of knit implementations to implement get_parents_with_ghosts in terms of get_parent_map.
1644
            ('text-a', ['fulltext'], 122, ()),
1645
            ('text-b', ['line-delta'], 84, ('text-a',))]
3023.2.2 by Martin Pool
Fix KnitVersionedFile.get_data_stream to not assume .versions() is sorted. (lp:165106)
1646
        # now check the fulltext is first and the delta second
1647
        format, data_list, _ = k1.get_data_stream(['text-a', 'text-b'])
1648
        self.assertEqual('knit-plain', format)
1649
        self.assertEqual(expected_data_list, data_list)
1650
        # and that's true if we ask for them in the opposite order too
1651
        format, data_list, _ = k1.get_data_stream(['text-b', 'text-a'])
1652
        self.assertEqual(expected_data_list, data_list)
1653
        # also try requesting more versions
1654
        format, data_list, _ = k1.get_data_stream([
1655
            'text-m', 'text-b', 'text-a'])
1656
        self.assertEqual([
3287.5.5 by Robert Collins
Refactor internals of knit implementations to implement get_parents_with_ghosts in terms of get_parent_map.
1657
            ('text-a', ['fulltext'], 122, ()),
1658
            ('text-b', ['line-delta'], 84, ('text-a',)),
1659
            ('text-m', ['line-delta'], 84, ('text-b', 'text-d')),
3023.2.2 by Martin Pool
Fix KnitVersionedFile.get_data_stream to not assume .versions() is sorted. (lp:165106)
1660
            ], data_list)
1661
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1662
    def test_get_stream_ghost_parent(self):
1663
        """Get a data stream for a version with a ghost parent."""
1664
        k1 = self.make_test_knit()
1665
        # Test data
1666
        k1.add_lines('text-a', [], split_lines(TEXT_1))
1667
        k1.add_lines_with_ghosts('text-b', ['text-a', 'text-ghost'],
1668
                                 split_lines(TEXT_1))
1669
        # Expected data
1670
        expected_data_list = [
1671
            # version, options, length, parents
3287.5.5 by Robert Collins
Refactor internals of knit implementations to implement get_parents_with_ghosts in terms of get_parent_map.
1672
            ('text-b', ['line-delta'], 84, ('text-a', 'text-ghost')),
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1673
            ]
1674
        
1675
        format, data_list, reader_callable = k1.get_data_stream(['text-b'])
2535.3.17 by Andrew Bennetts
[broken] Closer to a working Repository.fetch_revisions smart request.
1676
        self.assertEqual('knit-plain', format)
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1677
        self.assertEqual(expected_data_list, data_list)
1678
        self.assertRecordContentEqual(k1, 'text-b', reader_callable(None))
1679
    
1680
    def test_get_stream_get_multiple_records(self):
1681
        """Get a stream for multiple records of a knit."""
1682
        k1 = self.make_test_knit()
1683
        # Insert the same data as test_knit_join, as they seem to cover a range
1684
        # of cases (no parents, one parent, multiple parents).
1685
        test_data = [
1686
            ('text-a', [], TEXT_1),
1687
            ('text-b', ['text-a'], TEXT_1),
1688
            ('text-c', [], TEXT_1),
1689
            ('text-d', ['text-c'], TEXT_1),
1690
            ('text-m', ['text-b', 'text-d'], TEXT_1),
1691
            ]
3023.2.3 by Martin Pool
Update tests for new ordering of results from get_data_stream - the order is not defined by the interface, but is stable
1692
        for version_id, parents, lines in test_data:
1693
            k1.add_lines(version_id, parents, split_lines(lines))
1694
1695
        # This test is actually a bit strict as the order in which they're
1696
        # returned is not defined.  This matches the current (deterministic)
1697
        # behaviour.
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1698
        expected_data_list = [
1699
            # version, options, length, parents
3287.5.5 by Robert Collins
Refactor internals of knit implementations to implement get_parents_with_ghosts in terms of get_parent_map.
1700
            ('text-d', ['line-delta'], 84, ('text-c',)),
1701
            ('text-b', ['line-delta'], 84, ('text-a',)),
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1702
            ]
1703
        # Note that even though we request the revision IDs in a particular
1704
        # order, the data stream may return them in any order it likes.  In this
1705
        # case, they'll be in the order they were inserted into the knit.
1706
        format, data_list, reader_callable = k1.get_data_stream(
1707
            ['text-d', 'text-b'])
2535.3.17 by Andrew Bennetts
[broken] Closer to a working Repository.fetch_revisions smart request.
1708
        self.assertEqual('knit-plain', format)
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1709
        self.assertEqual(expected_data_list, data_list)
3023.2.3 by Martin Pool
Update tests for new ordering of results from get_data_stream - the order is not defined by the interface, but is stable
1710
        # must match order they're returned
1711
        self.assertRecordContentEqual(k1, 'text-d', reader_callable(84))
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1712
        self.assertRecordContentEqual(k1, 'text-b', reader_callable(84))
1713
        self.assertEqual('', reader_callable(None),
1714
                         "There should be no more bytes left to read.")
1715
1716
    def test_get_stream_all(self):
1717
        """Get a data stream for all the records in a knit.
1718
1719
        This exercises fulltext records, line-delta records, records with
1720
        various numbers of parents, and reading multiple records out of the
1721
        callable.  These cases ought to all be exercised individually by the
1722
        other test_get_stream_* tests; this test is basically just paranoia.
1723
        """
1724
        k1 = self.make_test_knit()
1725
        # Insert the same data as test_knit_join, as they seem to cover a range
1726
        # of cases (no parents, one parent, multiple parents).
1727
        test_data = [
1728
            ('text-a', [], TEXT_1),
1729
            ('text-b', ['text-a'], TEXT_1),
1730
            ('text-c', [], TEXT_1),
1731
            ('text-d', ['text-c'], TEXT_1),
1732
            ('text-m', ['text-b', 'text-d'], TEXT_1),
1733
           ]
3023.2.3 by Martin Pool
Update tests for new ordering of results from get_data_stream - the order is not defined by the interface, but is stable
1734
        for version_id, parents, lines in test_data:
1735
            k1.add_lines(version_id, parents, split_lines(lines))
1736
1737
        # This test is actually a bit strict as the order in which they're
1738
        # returned is not defined.  This matches the current (deterministic)
1739
        # behaviour.
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1740
        expected_data_list = [
1741
            # version, options, length, parents
3287.5.5 by Robert Collins
Refactor internals of knit implementations to implement get_parents_with_ghosts in terms of get_parent_map.
1742
            ('text-a', ['fulltext'], 122, ()),
1743
            ('text-b', ['line-delta'], 84, ('text-a',)),
1744
            ('text-m', ['line-delta'], 84, ('text-b', 'text-d')),
1745
            ('text-c', ['fulltext'], 121, ()),
1746
            ('text-d', ['line-delta'], 84, ('text-c',)),
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1747
            ]
1748
        format, data_list, reader_callable = k1.get_data_stream(
1749
            ['text-a', 'text-b', 'text-c', 'text-d', 'text-m'])
2535.3.17 by Andrew Bennetts
[broken] Closer to a working Repository.fetch_revisions smart request.
1750
        self.assertEqual('knit-plain', format)
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1751
        self.assertEqual(expected_data_list, data_list)
1752
        for version_id, options, length, parents in expected_data_list:
1753
            bytes = reader_callable(length)
1754
            self.assertRecordContentEqual(k1, version_id, bytes)
1755
2535.3.4 by Andrew Bennetts
Simple implementation of Knit.insert_data_stream.
1756
    def assertKnitFilesEqual(self, knit1, knit2):
1757
        """Assert that the contents of the index and data files of two knits are
1758
        equal.
1759
        """
1760
        self.assertEqual(
2535.3.36 by Andrew Bennetts
Merge bzr.dev
1761
            knit1.transport.get_bytes(knit1._data._access._filename),
1762
            knit2.transport.get_bytes(knit2._data._access._filename))
2535.3.4 by Andrew Bennetts
Simple implementation of Knit.insert_data_stream.
1763
        self.assertEqual(
1764
            knit1.transport.get_bytes(knit1._index._filename),
1765
            knit2.transport.get_bytes(knit2._index._filename))
1766
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
1767
    def assertKnitValuesEqual(self, left, right):
3052.2.5 by Andrew Bennetts
Address the rest of the review comments from John and myself.
1768
        """Assert that the texts, annotations and graph of left and right are
1769
        the same.
1770
        """
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
1771
        self.assertEqual(set(left.versions()), set(right.versions()))
1772
        for version in left.versions():
1773
            self.assertEqual(left.get_parents_with_ghosts(version),
1774
                right.get_parents_with_ghosts(version))
1775
            self.assertEqual(left.get_lines(version),
1776
                right.get_lines(version))
1777
            self.assertEqual(left.annotate(version),
1778
                right.annotate(version))
1779
2535.3.4 by Andrew Bennetts
Simple implementation of Knit.insert_data_stream.
1780
    def test_insert_data_stream_empty(self):
1781
        """Inserting a data stream with no records should not put any data into
1782
        the knit.
1783
        """
1784
        k1 = self.make_test_knit()
1785
        k1.insert_data_stream(
1786
            (k1.get_format_signature(), [], lambda ignored: ''))
2535.3.36 by Andrew Bennetts
Merge bzr.dev
1787
        self.assertEqual('', k1.transport.get_bytes(k1._data._access._filename),
2535.3.4 by Andrew Bennetts
Simple implementation of Knit.insert_data_stream.
1788
                         "The .knit should be completely empty.")
1789
        self.assertEqual(k1._index.HEADER,
1790
                         k1.transport.get_bytes(k1._index._filename),
1791
                         "The .kndx should have nothing apart from the header.")
1792
1793
    def test_insert_data_stream_one_record(self):
1794
        """Inserting a data stream with one record from a knit with one record
1795
        results in byte-identical files.
1796
        """
1797
        source = self.make_test_knit(name='source')
1798
        source.add_lines('text-a', [], split_lines(TEXT_1))
1799
        data_stream = source.get_data_stream(['text-a'])
1800
        target = self.make_test_knit(name='target')
1801
        target.insert_data_stream(data_stream)
1802
        self.assertKnitFilesEqual(source, target)
1803
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
1804
    def test_insert_data_stream_annotated_unannotated(self):
1805
        """Inserting an annotated datastream to an unannotated knit works."""
1806
        # case one - full texts.
1807
        source = self.make_test_knit(name='source', annotate=True)
1808
        target = self.make_test_knit(name='target', annotate=False)
1809
        source.add_lines('text-a', [], split_lines(TEXT_1))
1810
        target.insert_data_stream(source.get_data_stream(['text-a']))
1811
        self.assertKnitValuesEqual(source, target)
1812
        # case two - deltas.
1813
        source.add_lines('text-b', ['text-a'], split_lines(TEXT_2))
1814
        target.insert_data_stream(source.get_data_stream(['text-b']))
1815
        self.assertKnitValuesEqual(source, target)
1816
3052.2.3 by Robert Collins
Handle insert_data_stream of an unannotated stream into an annotated knit.
1817
    def test_insert_data_stream_unannotated_annotated(self):
1818
        """Inserting an unannotated datastream to an annotated knit works."""
1819
        # case one - full texts.
1820
        source = self.make_test_knit(name='source', annotate=False)
1821
        target = self.make_test_knit(name='target', annotate=True)
1822
        source.add_lines('text-a', [], split_lines(TEXT_1))
1823
        target.insert_data_stream(source.get_data_stream(['text-a']))
1824
        self.assertKnitValuesEqual(source, target)
1825
        # case two - deltas.
1826
        source.add_lines('text-b', ['text-a'], split_lines(TEXT_2))
1827
        target.insert_data_stream(source.get_data_stream(['text-b']))
1828
        self.assertKnitValuesEqual(source, target)
1829
2535.3.4 by Andrew Bennetts
Simple implementation of Knit.insert_data_stream.
1830
    def test_insert_data_stream_records_already_present(self):
1831
        """Insert a data stream where some records are alreday present in the
1832
        target, and some not.  Only the new records are inserted.
1833
        """
1834
        source = self.make_test_knit(name='source')
1835
        target = self.make_test_knit(name='target')
1836
        # Insert 'text-a' into both source and target
1837
        source.add_lines('text-a', [], split_lines(TEXT_1))
1838
        target.insert_data_stream(source.get_data_stream(['text-a']))
1839
        # Insert 'text-b' into just the source.
1840
        source.add_lines('text-b', ['text-a'], split_lines(TEXT_1))
1841
        # Get a data stream of both text-a and text-b, and insert it.
1842
        data_stream = source.get_data_stream(['text-a', 'text-b'])
1843
        target.insert_data_stream(data_stream)
1844
        # The source and target will now be identical.  This means the text-a
1845
        # record was not added a second time.
1846
        self.assertKnitFilesEqual(source, target)
1847
1848
    def test_insert_data_stream_multiple_records(self):
1849
        """Inserting a data stream of all records from a knit with multiple
1850
        records results in byte-identical files.
1851
        """
1852
        source = self.make_test_knit(name='source')
1853
        source.add_lines('text-a', [], split_lines(TEXT_1))
1854
        source.add_lines('text-b', ['text-a'], split_lines(TEXT_1))
1855
        source.add_lines('text-c', [], split_lines(TEXT_1))
1856
        data_stream = source.get_data_stream(['text-a', 'text-b', 'text-c'])
1857
        
1858
        target = self.make_test_knit(name='target')
1859
        target.insert_data_stream(data_stream)
1860
        
1861
        self.assertKnitFilesEqual(source, target)
1862
1863
    def test_insert_data_stream_ghost_parent(self):
1864
        """Insert a data stream with a record that has a ghost parent."""
1865
        # Make a knit with a record, text-a, that has a ghost parent.
1866
        source = self.make_test_knit(name='source')
1867
        source.add_lines_with_ghosts('text-a', ['text-ghost'],
1868
                                     split_lines(TEXT_1))
1869
        data_stream = source.get_data_stream(['text-a'])
1870
1871
        target = self.make_test_knit(name='target')
1872
        target.insert_data_stream(data_stream)
1873
1874
        self.assertKnitFilesEqual(source, target)
1875
1876
        # The target knit object is in a consistent state, i.e. the record we
1877
        # just added is immediately visible.
1878
        self.assertTrue(target.has_version('text-a'))
3287.6.5 by Robert Collins
Deprecate VersionedFile.has_ghost.
1879
        self.assertFalse(target.has_version('text-ghost'))
1880
        self.assertEqual({'text-a':('text-ghost',)},
1881
            target.get_parent_map(['text-a', 'text-ghost']))
2535.3.4 by Andrew Bennetts
Simple implementation of Knit.insert_data_stream.
1882
        self.assertEqual(split_lines(TEXT_1), target.get_lines('text-a'))
1883
1884
    def test_insert_data_stream_inconsistent_version_lines(self):
1885
        """Inserting a data stream which has different content for a version_id
1886
        than already exists in the knit will raise KnitCorrupt.
1887
        """
1888
        source = self.make_test_knit(name='source')
1889
        target = self.make_test_knit(name='target')
1890
        # Insert a different 'text-a' into both source and target
1891
        source.add_lines('text-a', [], split_lines(TEXT_1))
1892
        target.add_lines('text-a', [], split_lines(TEXT_2))
1893
        # Insert a data stream with conflicting content into the target
1894
        data_stream = source.get_data_stream(['text-a'])
1895
        self.assertRaises(
1896
            errors.KnitCorrupt, target.insert_data_stream, data_stream)
1897
1898
    def test_insert_data_stream_inconsistent_version_parents(self):
1899
        """Inserting a data stream which has different parents for a version_id
1900
        than already exists in the knit will raise KnitCorrupt.
1901
        """
1902
        source = self.make_test_knit(name='source')
1903
        target = self.make_test_knit(name='target')
1904
        # Insert a different 'text-a' into both source and target.  They differ
1905
        # only by the parents list, the content is the same.
1906
        source.add_lines_with_ghosts('text-a', [], split_lines(TEXT_1))
1907
        target.add_lines_with_ghosts('text-a', ['a-ghost'], split_lines(TEXT_1))
1908
        # Insert a data stream with conflicting content into the target
1909
        data_stream = source.get_data_stream(['text-a'])
1910
        self.assertRaises(
1911
            errors.KnitCorrupt, target.insert_data_stream, data_stream)
1912
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
1913
    def test_insert_data_stream_unknown_format(self):
2535.3.4 by Andrew Bennetts
Simple implementation of Knit.insert_data_stream.
1914
        """A data stream in a different format to the target knit cannot be
1915
        inserted.
1916
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
1917
        It will raise KnitDataStreamUnknown because the fallback code will fail
1918
        to make a knit. In future we may need KnitDataStreamIncompatible again,
1919
        for more exotic cases.
2535.3.4 by Andrew Bennetts
Simple implementation of Knit.insert_data_stream.
1920
        """
1921
        data_stream = ('fake-format-signature', [], lambda _: '')
1922
        target = self.make_test_knit(name='target')
1923
        self.assertRaises(
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
1924
            errors.KnitDataStreamUnknown,
2535.3.4 by Andrew Bennetts
Simple implementation of Knit.insert_data_stream.
1925
            target.insert_data_stream, data_stream)
1926
3287.7.4 by Andrew Bennetts
Failing test for bug 208418.
1927
    def test_insert_data_stream_bug_208418(self):
1928
        """You can insert a stream with an incompatible format, even when:
1929
          * the stream has a line-delta record,
1930
          * whose parent is in the target, also stored as a line-delta
1931
1932
        See <https://launchpad.net/bugs/208418>.
1933
        """
1934
        base_lines = split_lines(TEXT_1)
1935
        # Make the target
1936
        target = self.make_test_knit(name='target', annotate=True)
1937
        target.add_lines('version-1', [], base_lines)
1938
        target.add_lines('version-2', ['version-1'], base_lines + ['a\n'])
1939
        # The second record should be a delta.
1940
        self.assertEqual('line-delta', target._index.get_method('version-2'))
1941
        
1942
        # Make a source, with a different format, but the same data
1943
        source = self.make_test_knit(name='source', annotate=False)
1944
        source.add_lines('version-1', [], base_lines)
1945
        source.add_lines('version-2', ['version-1'], base_lines + ['a\n'])
1946
        # Now add another record, which should be stored as a delta against
1947
        # version-2.
1948
        source.add_lines('version-3', ['version-2'], base_lines + ['b\n'])
1949
        self.assertEqual('line-delta', source._index.get_method('version-3'))
1950
1951
        # Make a stream of the new version
1952
        data_stream = source.get_data_stream(['version-3'])
1953
        # And insert into the target
1954
        target.insert_data_stream(data_stream)
1955
        # No errors should have been raised.
1956
1957
2535.3.5 by Andrew Bennetts
Batch writes as much as possible in insert_data_stream.
1958
    #  * test that a stream of "already present version, then new version"
1959
    #    inserts correctly.
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1960
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
1961
1962
    def assertMadeStreamKnit(self, source_knit, versions, target_knit):
1963
        """Assert that a knit made from a stream is as expected."""
1964
        a_stream = source_knit.get_data_stream(versions)
1965
        expected_data = a_stream[2](None)
1966
        a_stream = source_knit.get_data_stream(versions)
1967
        a_knit = target_knit._knit_from_datastream(a_stream)
1968
        self.assertEqual(source_knit.factory.__class__,
1969
            a_knit.factory.__class__)
1970
        self.assertIsInstance(a_knit._data._access, _StreamAccess)
1971
        self.assertIsInstance(a_knit._index, _StreamIndex)
1972
        self.assertEqual(a_knit._index.data_list, a_stream[1])
1973
        self.assertEqual(a_knit._data._access.data, expected_data)
1974
        self.assertEqual(a_knit.filename, target_knit.filename)
1975
        self.assertEqual(a_knit.transport, target_knit.transport)
3052.2.3 by Robert Collins
Handle insert_data_stream of an unannotated stream into an annotated knit.
1976
        self.assertEqual(a_knit._index, a_knit._data._access.stream_index)
1977
        self.assertEqual(target_knit, a_knit._data._access.backing_knit)
1978
        self.assertIsInstance(a_knit._data._access.orig_factory,
1979
            source_knit.factory.__class__)
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
1980
1981
    def test__knit_from_data_stream_empty(self):
1982
        """Create a knit object from a datastream."""
1983
        annotated = self.make_test_knit(name='source', annotate=True)
1984
        plain = self.make_test_knit(name='target', annotate=False)
1985
        # case 1: annotated source
1986
        self.assertMadeStreamKnit(annotated, [], annotated)
1987
        self.assertMadeStreamKnit(annotated, [], plain)
1988
        # case 2: plain source
1989
        self.assertMadeStreamKnit(plain, [], annotated)
1990
        self.assertMadeStreamKnit(plain, [], plain)
1991
1992
    def test__knit_from_data_stream_unknown_format(self):
1993
        annotated = self.make_test_knit(name='source', annotate=True)
1994
        self.assertRaises(errors.KnitDataStreamUnknown,
1995
            annotated._knit_from_datastream, ("unknown", None, None))
1996
1997
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1998
TEXT_1 = """\
1999
Banana cup cakes:
2000
2001
- bananas
2002
- eggs
2003
- broken tea cups
2004
"""
2005
2006
TEXT_1A = """\
2007
Banana cup cake recipe
2008
(serves 6)
2009
2010
- bananas
2011
- eggs
2012
- broken tea cups
2013
- self-raising flour
2014
"""
2015
1664.2.1 by Aaron Bentley
Start work on plan_merge test
2016
TEXT_1B = """\
2017
Banana cup cake recipe
2018
2019
- bananas (do not use plantains!!!)
2020
- broken tea cups
2021
- flour
2022
"""
2023
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
2024
delta_1_1a = """\
2025
0,1,2
2026
Banana cup cake recipe
2027
(serves 6)
2028
5,5,1
2029
- self-raising flour
2030
"""
2031
2032
TEXT_2 = """\
2033
Boeuf bourguignon
2034
2035
- beef
2036
- red wine
2037
- small onions
2038
- carrot
2039
- mushrooms
2040
"""
2041
1664.2.3 by Aaron Bentley
Add failing test case
2042
AB_MERGE_TEXT="""unchanged|Banana cup cake recipe
2043
new-a|(serves 6)
2044
unchanged|
2045
killed-b|- bananas
2046
killed-b|- eggs
2047
new-b|- bananas (do not use plantains!!!)
2048
unchanged|- broken tea cups
2049
new-a|- self-raising flour
1664.2.6 by Aaron Bentley
Got plan-merge passing tests
2050
new-b|- flour
2051
"""
1664.2.3 by Aaron Bentley
Add failing test case
2052
AB_MERGE=[tuple(l.split('|')) for l in AB_MERGE_TEXT.splitlines(True)]
2053
2054
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
2055
def line_delta(from_lines, to_lines):
2056
    """Generate line-based delta from one text to another"""
2057
    s = difflib.SequenceMatcher(None, from_lines, to_lines)
2058
    for op in s.get_opcodes():
2059
        if op[0] == 'equal':
2060
            continue
2061
        yield '%d,%d,%d\n' % (op[1], op[2], op[4]-op[3])
2062
        for i in range(op[3], op[4]):
2063
            yield to_lines[i]
2064
2065
2066
def apply_line_delta(basis_lines, delta_lines):
2067
    """Apply a line-based perfect diff
2068
    
2069
    basis_lines -- text to apply the patch to
2070
    delta_lines -- diff instructions and content
2071
    """
2072
    out = basis_lines[:]
2073
    i = 0
2074
    offset = 0
2075
    while i < len(delta_lines):
2076
        l = delta_lines[i]
2077
        a, b, c = map(long, l.split(','))
2078
        i = i + 1
2079
        out[offset+a:offset+b] = delta_lines[i:i+c]
2080
        i = i + c
2081
        offset = offset + (b - a) + c
2082
    return out
1684.3.3 by Robert Collins
Add a special cased weaves to knit converter.
2083
2084
2085
class TestWeaveToKnit(KnitTests):
2086
2087
    def test_weave_to_knit_matches(self):
2088
        # check that the WeaveToKnit is_compatible function
2089
        # registers True for a Weave to a Knit.
2090
        w = Weave()
2091
        k = self.make_test_knit()
2092
        self.failUnless(WeaveToKnit.is_compatible(w, k))
2093
        self.failIf(WeaveToKnit.is_compatible(k, w))
2094
        self.failIf(WeaveToKnit.is_compatible(w, w))
2095
        self.failIf(WeaveToKnit.is_compatible(k, k))
1863.1.1 by John Arbash Meinel
Allow Versioned files to do caching if explicitly asked, and implement for Knit
2096
2097
2098
class TestKnitCaching(KnitTests):
2099
    
2850.1.1 by Robert Collins
* ``KnitVersionedFile.add*`` will no longer cache added records even when
2100
    def create_knit(self):
1863.1.1 by John Arbash Meinel
Allow Versioned files to do caching if explicitly asked, and implement for Knit
2101
        k = self.make_test_knit(True)
2102
        k.add_lines('text-1', [], split_lines(TEXT_1))
2103
        k.add_lines('text-2', [], split_lines(TEXT_2))
2104
        return k
2105
2106
    def test_no_caching(self):
2107
        k = self.create_knit()
2108
        # Nothing should be cached without setting 'enable_cache'
2109
        self.assertEqual({}, k._data._cache)
2110
2111
    def test_cache_data_read_raw(self):
2112
        k = self.create_knit()
2113
2114
        # Now cache and read
2115
        k.enable_cache()
2116
2117
        def read_one_raw(version):
2118
            pos_map = k._get_components_positions([version])
3224.1.13 by John Arbash Meinel
Revert the _get_component_positions api
2119
            method, index_memo, next = pos_map[version]
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
2120
            lst = list(k._data.read_records_iter_raw([(version, index_memo)]))
1863.1.1 by John Arbash Meinel
Allow Versioned files to do caching if explicitly asked, and implement for Knit
2121
            self.assertEqual(1, len(lst))
2122
            return lst[0]
2123
2124
        val = read_one_raw('text-1')
1863.1.8 by John Arbash Meinel
Removing disk-backed-cache
2125
        self.assertEqual({'text-1':val[1]}, k._data._cache)
1863.1.1 by John Arbash Meinel
Allow Versioned files to do caching if explicitly asked, and implement for Knit
2126
2127
        k.clear_cache()
2128
        # After clear, new reads are not cached
2129
        self.assertEqual({}, k._data._cache)
2130
2131
        val2 = read_one_raw('text-1')
2132
        self.assertEqual(val, val2)
2133
        self.assertEqual({}, k._data._cache)
2134
2135
    def test_cache_data_read(self):
2136
        k = self.create_knit()
2137
2138
        def read_one(version):
2139
            pos_map = k._get_components_positions([version])
3224.1.13 by John Arbash Meinel
Revert the _get_component_positions api
2140
            method, index_memo, next = pos_map[version]
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
2141
            lst = list(k._data.read_records_iter([(version, index_memo)]))
1863.1.1 by John Arbash Meinel
Allow Versioned files to do caching if explicitly asked, and implement for Knit
2142
            self.assertEqual(1, len(lst))
2143
            return lst[0]
2144
2145
        # Now cache and read
2146
        k.enable_cache()
2147
2148
        val = read_one('text-2')
2149
        self.assertEqual(['text-2'], k._data._cache.keys())
2150
        self.assertEqual('text-2', val[0])
2151
        content, digest = k._data._parse_record('text-2',
2152
                                                k._data._cache['text-2'])
2153
        self.assertEqual(content, val[1])
2154
        self.assertEqual(digest, val[2])
2155
2156
        k.clear_cache()
2157
        self.assertEqual({}, k._data._cache)
2158
2159
        val2 = read_one('text-2')
2160
        self.assertEqual(val, val2)
2161
        self.assertEqual({}, k._data._cache)
2162
2163
    def test_cache_read(self):
2164
        k = self.create_knit()
2165
        k.enable_cache()
2166
2167
        text = k.get_text('text-1')
2168
        self.assertEqual(TEXT_1, text)
2169
        self.assertEqual(['text-1'], k._data._cache.keys())
2170
2171
        k.clear_cache()
2172
        self.assertEqual({}, k._data._cache)
2173
2174
        text = k.get_text('text-1')
2175
        self.assertEqual(TEXT_1, text)
2176
        self.assertEqual({}, k._data._cache)
2102.2.1 by John Arbash Meinel
Fix bug #64789 _KnitIndex.add_versions() should dict compress new revisions
2177
2178
2179
class TestKnitIndex(KnitTests):
2180
2181
    def test_add_versions_dictionary_compresses(self):
2182
        """Adding versions to the index should update the lookup dict"""
2183
        knit = self.make_test_knit()
2184
        idx = knit._index
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
2185
        idx.add_version('a-1', ['fulltext'], (None, 0, 0), [])
2102.2.1 by John Arbash Meinel
Fix bug #64789 _KnitIndex.add_versions() should dict compress new revisions
2186
        self.check_file_contents('test.kndx',
2187
            '# bzr knit index 8\n'
2188
            '\n'
2189
            'a-1 fulltext 0 0  :'
2190
            )
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
2191
        idx.add_versions([('a-2', ['fulltext'], (None, 0, 0), ['a-1']),
2192
                          ('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
2193
                         ])
2194
        self.check_file_contents('test.kndx',
2195
            '# bzr knit index 8\n'
2196
            '\n'
2197
            'a-1 fulltext 0 0  :\n'
2198
            'a-2 fulltext 0 0 0 :\n'
2199
            'a-3 fulltext 0 0 1 :'
2200
            )
2201
        self.assertEqual(['a-1', 'a-2', 'a-3'], idx._history)
3287.5.5 by Robert Collins
Refactor internals of knit implementations to implement get_parents_with_ghosts in terms of get_parent_map.
2202
        self.assertEqual({'a-1':('a-1', ['fulltext'], 0, 0, (), 0),
2203
                          'a-2':('a-2', ['fulltext'], 0, 0, ('a-1',), 1),
2204
                          'a-3':('a-3', ['fulltext'], 0, 0, ('a-2',), 2),
2102.2.1 by John Arbash Meinel
Fix bug #64789 _KnitIndex.add_versions() should dict compress new revisions
2205
                         }, idx._cache)
2206
2207
    def test_add_versions_fails_clean(self):
2208
        """If add_versions fails in the middle, it restores a pristine state.
2209
2210
        Any modifications that are made to the index are reset if all versions
2211
        cannot be added.
2212
        """
2213
        # This cheats a little bit by passing in a generator which will
2214
        # raise an exception before the processing finishes
2215
        # Other possibilities would be to have an version with the wrong number
2216
        # of entries, or to make the backing transport unable to write any
2217
        # files.
2218
2219
        knit = self.make_test_knit()
2220
        idx = knit._index
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
2221
        idx.add_version('a-1', ['fulltext'], (None, 0, 0), [])
2102.2.1 by John Arbash Meinel
Fix bug #64789 _KnitIndex.add_versions() should dict compress new revisions
2222
2223
        class StopEarly(Exception):
2224
            pass
2225
2226
        def generate_failure():
2227
            """Add some entries and then raise an exception"""
3287.5.5 by Robert Collins
Refactor internals of knit implementations to implement get_parents_with_ghosts in terms of get_parent_map.
2228
            yield ('a-2', ['fulltext'], (None, 0, 0), ('a-1',))
2229
            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
2230
            raise StopEarly()
2231
2232
        # Assert the pre-condition
2233
        self.assertEqual(['a-1'], idx._history)
3287.5.5 by Robert Collins
Refactor internals of knit implementations to implement get_parents_with_ghosts in terms of get_parent_map.
2234
        self.assertEqual({'a-1':('a-1', ['fulltext'], 0, 0, (), 0)}, idx._cache)
2102.2.1 by John Arbash Meinel
Fix bug #64789 _KnitIndex.add_versions() should dict compress new revisions
2235
2236
        self.assertRaises(StopEarly, idx.add_versions, generate_failure())
2237
2238
        # And it shouldn't be modified
2239
        self.assertEqual(['a-1'], idx._history)
3287.5.5 by Robert Collins
Refactor internals of knit implementations to implement get_parents_with_ghosts in terms of get_parent_map.
2240
        self.assertEqual({'a-1':('a-1', ['fulltext'], 0, 0, (), 0)}, idx._cache)
2171.1.1 by John Arbash Meinel
Knit index files should ignore empty indexes rather than consider them corrupt.
2241
2242
    def test_knit_index_ignores_empty_files(self):
2243
        # There was a race condition in older bzr, where a ^C at the right time
2244
        # could leave an empty .kndx file, which bzr would later claim was a
2245
        # corrupted file since the header was not present. In reality, the file
2246
        # just wasn't created, so it should be ignored.
2247
        t = get_transport('.')
2248
        t.put_bytes('test.kndx', '')
2249
2250
        knit = self.make_test_knit()
2251
2252
    def test_knit_index_checks_header(self):
2253
        t = get_transport('.')
2254
        t.put_bytes('test.kndx', '# not really a knit header\n\n')
2255
2196.2.1 by John Arbash Meinel
Merge Dmitry's optimizations and minimize the actual diff.
2256
        self.assertRaises(KnitHeaderError, self.make_test_knit)
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.
2257
2258
2259
class TestGraphIndexKnit(KnitTests):
2260
    """Tests for knits using a GraphIndex rather than a KnitIndex."""
2261
2262
    def make_g_index(self, name, ref_lists=0, nodes=[]):
2263
        builder = GraphIndexBuilder(ref_lists)
2264
        for node, references, value in nodes:
2265
            builder.add_node(node, references, value)
2266
        stream = builder.finish()
2267
        trans = self.get_transport()
2890.2.1 by Robert Collins
* ``bzrlib.index.GraphIndex`` now requires a size parameter to the
2268
        size = trans.put_file(name, stream)
2269
        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.
2270
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2271
    def two_graph_index(self, deltas=False, catch_adds=False):
2592.3.13 by Robert Collins
Implement KnitGraphIndex.get_method.
2272
        """Build a two-graph index.
2273
2274
        :param deltas: If true, use underlying indices with two node-ref
2275
            lists and 'parent' set to a delta-compressed against tail.
2276
        """
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.
2277
        # build a complex graph across several indices.
2592.3.13 by Robert Collins
Implement KnitGraphIndex.get_method.
2278
        if deltas:
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2279
            # delta compression inn the index
2592.3.13 by Robert Collins
Implement KnitGraphIndex.get_method.
2280
            index1 = self.make_g_index('1', 2, [
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2281
                (('tip', ), 'N0 100', ([('parent', )], [], )),
2282
                (('tail', ), '', ([], []))])
2592.3.13 by Robert Collins
Implement KnitGraphIndex.get_method.
2283
            index2 = self.make_g_index('2', 2, [
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2284
                (('parent', ), ' 100 78', ([('tail', ), ('ghost', )], [('tail', )])),
2285
                (('separate', ), '', ([], []))])
2592.3.13 by Robert Collins
Implement KnitGraphIndex.get_method.
2286
        else:
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2287
            # just blob location and graph in the index.
2592.3.13 by Robert Collins
Implement KnitGraphIndex.get_method.
2288
            index1 = self.make_g_index('1', 1, [
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2289
                (('tip', ), 'N0 100', ([('parent', )], )),
2290
                (('tail', ), '', ([], ))])
2592.3.13 by Robert Collins
Implement KnitGraphIndex.get_method.
2291
            index2 = self.make_g_index('2', 1, [
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2292
                (('parent', ), ' 100 78', ([('tail', ), ('ghost', )], )),
2293
                (('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.
2294
        combined_index = CombinedGraphIndex([index1, index2])
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2295
        if catch_adds:
2296
            self.combined_index = combined_index
2297
            self.caught_entries = []
2298
            add_callback = self.catch_add
2299
        else:
2300
            add_callback = None
2301
        return KnitGraphIndex(combined_index, deltas=deltas,
2302
            add_callback=add_callback)
2592.3.4 by Robert Collins
Implement get_ancestry/get_ancestry_with_ghosts for KnitGraphIndex.
2303
2304
    def test_get_ancestry(self):
2592.3.30 by Robert Collins
Make GraphKnitIndex get_ancestry the same as regular knits.
2305
        # get_ancestry is defined as eliding ghosts, not erroring.
2306
        index = self.two_graph_index()
2592.3.4 by Robert Collins
Implement get_ancestry/get_ancestry_with_ghosts for KnitGraphIndex.
2307
        self.assertEqual([], index.get_ancestry([]))
2308
        self.assertEqual(['separate'], index.get_ancestry(['separate']))
2309
        self.assertEqual(['tail'], index.get_ancestry(['tail']))
2310
        self.assertEqual(['tail', 'parent'], index.get_ancestry(['parent']))
2311
        self.assertEqual(['tail', 'parent', 'tip'], index.get_ancestry(['tip']))
2312
        self.assertTrue(index.get_ancestry(['tip', 'separate']) in
2313
            (['tail', 'parent', 'tip', 'separate'],
2314
             ['separate', 'tail', 'parent', 'tip'],
2315
            ))
2316
        # and without topo_sort
2317
        self.assertEqual(set(['separate']),
2318
            set(index.get_ancestry(['separate'], topo_sorted=False)))
2319
        self.assertEqual(set(['tail']),
2320
            set(index.get_ancestry(['tail'], topo_sorted=False)))
2321
        self.assertEqual(set(['tail', 'parent']),
2322
            set(index.get_ancestry(['parent'], topo_sorted=False)))
2323
        self.assertEqual(set(['tail', 'parent', 'tip']),
2324
            set(index.get_ancestry(['tip'], topo_sorted=False)))
2325
        self.assertEqual(set(['separate', 'tail', 'parent', 'tip']),
2326
            set(index.get_ancestry(['tip', 'separate'])))
2592.3.30 by Robert Collins
Make GraphKnitIndex get_ancestry the same as regular knits.
2327
        # asking for a ghost makes it go boom.
2328
        self.assertRaises(errors.RevisionNotPresent, index.get_ancestry, ['ghost'])
2592.3.4 by Robert Collins
Implement get_ancestry/get_ancestry_with_ghosts for KnitGraphIndex.
2329
2330
    def test_get_ancestry_with_ghosts(self):
2331
        index = self.two_graph_index()
2332
        self.assertEqual([], index.get_ancestry_with_ghosts([]))
2333
        self.assertEqual(['separate'], index.get_ancestry_with_ghosts(['separate']))
2334
        self.assertEqual(['tail'], index.get_ancestry_with_ghosts(['tail']))
2335
        self.assertTrue(index.get_ancestry_with_ghosts(['parent']) in
2336
            (['tail', 'ghost', 'parent'],
2337
             ['ghost', 'tail', 'parent'],
2338
            ))
2339
        self.assertTrue(index.get_ancestry_with_ghosts(['tip']) in
2340
            (['tail', 'ghost', 'parent', 'tip'],
2341
             ['ghost', 'tail', 'parent', 'tip'],
2342
            ))
2343
        self.assertTrue(index.get_ancestry_with_ghosts(['tip', 'separate']) in
2344
            (['tail', 'ghost', 'parent', 'tip', 'separate'],
2345
             ['ghost', 'tail', 'parent', 'tip', 'separate'],
2346
             ['separate', 'tail', 'ghost', 'parent', 'tip'],
2347
             ['separate', 'ghost', 'tail', 'parent', 'tip'],
2348
            ))
2592.3.30 by Robert Collins
Make GraphKnitIndex get_ancestry the same as regular knits.
2349
        # asking for a ghost makes it go boom.
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2350
        self.assertRaises(errors.RevisionNotPresent, index.get_ancestry_with_ghosts, ['ghost'])
2592.3.5 by Robert Collins
Implement KnitGraphIndex.num_versions.
2351
2352
    def test_num_versions(self):
2353
        index = self.two_graph_index()
2354
        self.assertEqual(4, index.num_versions())
2592.3.6 by Robert Collins
Implement KnitGraphIndex.get_versions.
2355
2356
    def test_get_versions(self):
2357
        index = self.two_graph_index()
2358
        self.assertEqual(set(['tail', 'tip', 'parent', 'separate']),
2359
            set(index.get_versions()))
2360
2592.3.9 by Robert Collins
Implement KnitGraphIndex.has_version.
2361
    def test_has_version(self):
2362
        index = self.two_graph_index()
2363
        self.assertTrue(index.has_version('tail'))
2364
        self.assertFalse(index.has_version('ghost'))
2365
2592.3.10 by Robert Collins
Implement KnitGraphIndex.get_position.
2366
    def test_get_position(self):
2367
        index = self.two_graph_index()
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
2368
        self.assertEqual((index._graph_index._indices[0], 0, 100), index.get_position('tip'))
2369
        self.assertEqual((index._graph_index._indices[1], 100, 78), index.get_position('parent'))
2592.3.10 by Robert Collins
Implement KnitGraphIndex.get_position.
2370
2592.3.13 by Robert Collins
Implement KnitGraphIndex.get_method.
2371
    def test_get_method_deltas(self):
2372
        index = self.two_graph_index(deltas=True)
2592.3.11 by Robert Collins
Implement KnitGraphIndex.get_method.
2373
        self.assertEqual('fulltext', index.get_method('tip'))
2374
        self.assertEqual('line-delta', index.get_method('parent'))
2375
2592.3.13 by Robert Collins
Implement KnitGraphIndex.get_method.
2376
    def test_get_method_no_deltas(self):
2377
        # check that the parent-history lookup is ignored with deltas=False.
2378
        index = self.two_graph_index(deltas=False)
2379
        self.assertEqual('fulltext', index.get_method('tip'))
2380
        self.assertEqual('fulltext', index.get_method('parent'))
2381
2592.3.14 by Robert Collins
Implement KnitGraphIndex.get_options.
2382
    def test_get_options_deltas(self):
2383
        index = self.two_graph_index(deltas=True)
2658.2.1 by Robert Collins
Fix mismatch between KnitGraphIndex and KnitIndex in get_options.
2384
        self.assertEqual(['fulltext', 'no-eol'], index.get_options('tip'))
2385
        self.assertEqual(['line-delta'], index.get_options('parent'))
2592.3.14 by Robert Collins
Implement KnitGraphIndex.get_options.
2386
2387
    def test_get_options_no_deltas(self):
2388
        # check that the parent-history lookup is ignored with deltas=False.
2389
        index = self.two_graph_index(deltas=False)
2658.2.1 by Robert Collins
Fix mismatch between KnitGraphIndex and KnitIndex in get_options.
2390
        self.assertEqual(['fulltext', 'no-eol'], index.get_options('tip'))
2391
        self.assertEqual(['fulltext'], index.get_options('parent'))
2592.3.14 by Robert Collins
Implement KnitGraphIndex.get_options.
2392
2592.3.15 by Robert Collins
Implement KnitGraphIndex.get_parents/get_parents_with_ghosts.
2393
    def test_get_parents_with_ghosts(self):
2394
        index = self.two_graph_index()
2395
        self.assertEqual(('tail', 'ghost'), index.get_parents_with_ghosts('parent'))
2592.3.43 by Robert Collins
A knit iter_parents API.
2396
        # and errors on ghosts.
2397
        self.assertRaises(errors.RevisionNotPresent,
2398
            index.get_parents_with_ghosts, 'ghost')
2592.3.15 by Robert Collins
Implement KnitGraphIndex.get_parents/get_parents_with_ghosts.
2399
2592.3.16 by Robert Collins
Implement KnitGraphIndex.check_versions_present.
2400
    def test_check_versions_present(self):
2401
        # ghosts should not be considered present
2402
        index = self.two_graph_index()
2403
        self.assertRaises(RevisionNotPresent, index.check_versions_present,
2404
            ['ghost'])
2405
        self.assertRaises(RevisionNotPresent, index.check_versions_present,
2406
            ['tail', 'ghost'])
2407
        index.check_versions_present(['tail', 'separate'])
2592.3.14 by Robert Collins
Implement KnitGraphIndex.get_options.
2408
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2409
    def catch_add(self, entries):
2410
        self.caught_entries.append(entries)
2411
2412
    def test_add_no_callback_errors(self):
2413
        index = self.two_graph_index()
2414
        self.assertRaises(errors.ReadOnlyError, index.add_version,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2415
            '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.
2416
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
2417
    def test_add_version_smoke(self):
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2418
        index = self.two_graph_index(catch_adds=True)
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2419
        index.add_version('new', 'fulltext,no-eol', (None, 50, 60), ['separate'])
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2420
        self.assertEqual([[(('new', ), 'N50 60', ((('separate',),),))]],
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2421
            self.caught_entries)
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
2422
2423
    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.
2424
        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.
2425
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2426
            '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.
2427
        self.assertEqual([], self.caught_entries)
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
2428
2429
    def test_add_version_same_dup(self):
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2430
        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.
2431
        # options can be spelt two different ways
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2432
        index.add_version('tip', 'fulltext,no-eol', (None, 0, 100), ['parent'])
2433
        index.add_version('tip', 'no-eol,fulltext', (None, 0, 100), ['parent'])
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2434
        # but neither should have added data.
2435
        self.assertEqual([[], []], self.caught_entries)
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
2436
        
2437
    def test_add_version_different_dup(self):
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2438
        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.
2439
        # change options
2440
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2441
            'tip', 'no-eol,line-delta', (None, 0, 100), ['parent'])
2442
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2443
            'tip', 'line-delta,no-eol', (None, 0, 100), ['parent'])
2444
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2445
            'tip', 'fulltext', (None, 0, 100), ['parent'])
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
2446
        # position/length
2447
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2448
            'tip', 'fulltext,no-eol', (None, 50, 100), ['parent'])
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
2449
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2450
            'tip', 'fulltext,no-eol', (None, 0, 1000), ['parent'])
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
2451
        # parents
2452
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2453
            'tip', 'fulltext,no-eol', (None, 0, 100), [])
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2454
        self.assertEqual([], self.caught_entries)
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
2455
        
2456
    def test_add_versions_nodeltas(self):
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2457
        index = self.two_graph_index(catch_adds=True)
2458
        index.add_versions([
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2459
                ('new', 'fulltext,no-eol', (None, 50, 60), ['separate']),
2460
                ('new2', 'fulltext', (None, 0, 6), ['new']),
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2461
                ])
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2462
        self.assertEqual([(('new', ), 'N50 60', ((('separate',),),)),
2463
            (('new2', ), ' 0 6', ((('new',),),))],
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2464
            sorted(self.caught_entries[0]))
2465
        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.
2466
2467
    def test_add_versions_deltas(self):
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2468
        index = self.two_graph_index(deltas=True, catch_adds=True)
2469
        index.add_versions([
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2470
                ('new', 'fulltext,no-eol', (None, 50, 60), ['separate']),
2471
                ('new2', 'line-delta', (None, 0, 6), ['new']),
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2472
                ])
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2473
        self.assertEqual([(('new', ), 'N50 60', ((('separate',),), ())),
2474
            (('new2', ), ' 0 6', ((('new',),), (('new',),), ))],
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2475
            sorted(self.caught_entries[0]))
2476
        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.
2477
2478
    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.
2479
        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.
2480
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2481
            [('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.
2482
        self.assertEqual([], self.caught_entries)
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
2483
2841.2.1 by Robert Collins
* Commit no longer checks for new text keys during insertion when the
2484
    def test_add_versions_random_id_accepted(self):
2485
        index = self.two_graph_index(catch_adds=True)
2486
        index.add_versions([], random_id=True)
2487
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
2488
    def test_add_versions_same_dup(self):
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2489
        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.
2490
        # options can be spelt two different ways
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2491
        index.add_versions([('tip', 'fulltext,no-eol', (None, 0, 100), ['parent'])])
2492
        index.add_versions([('tip', 'no-eol,fulltext', (None, 0, 100), ['parent'])])
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2493
        # but neither should have added data.
2494
        self.assertEqual([[], []], self.caught_entries)
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
2495
        
2496
    def test_add_versions_different_dup(self):
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2497
        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.
2498
        # change options
2499
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2500
            [('tip', 'no-eol,line-delta', (None, 0, 100), ['parent'])])
2501
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2502
            [('tip', 'line-delta,no-eol', (None, 0, 100), ['parent'])])
2503
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2504
            [('tip', 'fulltext', (None, 0, 100), ['parent'])])
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
2505
        # position/length
2506
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2507
            [('tip', 'fulltext,no-eol', (None, 50, 100), ['parent'])])
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
2508
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2509
            [('tip', 'fulltext,no-eol', (None, 0, 1000), ['parent'])])
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
2510
        # parents
2511
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2512
            [('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.
2513
        # change options in the second record
2514
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2515
            [('tip', 'fulltext,no-eol', (None, 0, 100), ['parent']),
2516
             ('tip', '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.
2517
        self.assertEqual([], self.caught_entries)
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2518
2592.3.43 by Robert Collins
A knit iter_parents API.
2519
    def test_iter_parents(self):
2520
        index1 = self.make_g_index('1', 1, [
2521
        # no parents
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2522
            (('r0', ), 'N0 100', ([], )),
2592.3.43 by Robert Collins
A knit iter_parents API.
2523
        # 1 parent
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2524
            (('r1', ), '', ([('r0', )], ))])
2592.3.43 by Robert Collins
A knit iter_parents API.
2525
        index2 = self.make_g_index('2', 1, [
2526
        # 2 parents
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2527
            (('r2', ), 'N0 100', ([('r1', ), ('r0', )], )),
2592.3.43 by Robert Collins
A knit iter_parents API.
2528
            ])
2529
        combined_index = CombinedGraphIndex([index1, index2])
2530
        index = KnitGraphIndex(combined_index)
2531
        # XXX TODO a ghost
2532
        # cases: each sample data individually:
2533
        self.assertEqual(set([('r0', ())]),
2534
            set(index.iter_parents(['r0'])))
2535
        self.assertEqual(set([('r1', ('r0', ))]),
2536
            set(index.iter_parents(['r1'])))
2537
        self.assertEqual(set([('r2', ('r1', 'r0'))]),
2538
            set(index.iter_parents(['r2'])))
2539
        # no nodes returned for a missing node
2540
        self.assertEqual(set(),
2541
            set(index.iter_parents(['missing'])))
2542
        # 1 node returned with missing nodes skipped
2543
        self.assertEqual(set([('r1', ('r0', ))]),
2544
            set(index.iter_parents(['ghost1', 'r1', 'ghost'])))
2545
        # 2 nodes returned
2546
        self.assertEqual(set([('r0', ()), ('r1', ('r0', ))]),
2547
            set(index.iter_parents(['r0', 'r1'])))
2548
        # 2 nodes returned, missing skipped
2549
        self.assertEqual(set([('r0', ()), ('r1', ('r0', ))]),
2550
            set(index.iter_parents(['a', 'r0', 'b', 'r1', 'c'])))
2551
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2552
2553
class TestNoParentsGraphIndexKnit(KnitTests):
2554
    """Tests for knits using KnitGraphIndex with no parents."""
2555
2556
    def make_g_index(self, name, ref_lists=0, nodes=[]):
2557
        builder = GraphIndexBuilder(ref_lists)
2558
        for node, references in nodes:
2559
            builder.add_node(node, references)
2560
        stream = builder.finish()
2561
        trans = self.get_transport()
2890.2.1 by Robert Collins
* ``bzrlib.index.GraphIndex`` now requires a size parameter to the
2562
        size = trans.put_file(name, stream)
2563
        return GraphIndex(trans, name, size)
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2564
2565
    def test_parents_deltas_incompatible(self):
2566
        index = CombinedGraphIndex([])
2567
        self.assertRaises(errors.KnitError, KnitGraphIndex, index,
2568
            deltas=True, parents=False)
2569
2570
    def two_graph_index(self, catch_adds=False):
2571
        """Build a two-graph index.
2572
2573
        :param deltas: If true, use underlying indices with two node-ref
2574
            lists and 'parent' set to a delta-compressed against tail.
2575
        """
2576
        # put several versions in the index.
2577
        index1 = self.make_g_index('1', 0, [
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2578
            (('tip', ), 'N0 100'),
2579
            (('tail', ), '')])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2580
        index2 = self.make_g_index('2', 0, [
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2581
            (('parent', ), ' 100 78'),
2582
            (('separate', ), '')])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2583
        combined_index = CombinedGraphIndex([index1, index2])
2584
        if catch_adds:
2585
            self.combined_index = combined_index
2586
            self.caught_entries = []
2587
            add_callback = self.catch_add
2588
        else:
2589
            add_callback = None
2590
        return KnitGraphIndex(combined_index, parents=False,
2591
            add_callback=add_callback)
2592
2593
    def test_get_ancestry(self):
2594
        # with no parents, ancestry is always just the key.
2595
        index = self.two_graph_index()
2596
        self.assertEqual([], index.get_ancestry([]))
2597
        self.assertEqual(['separate'], index.get_ancestry(['separate']))
2598
        self.assertEqual(['tail'], index.get_ancestry(['tail']))
2599
        self.assertEqual(['parent'], index.get_ancestry(['parent']))
2600
        self.assertEqual(['tip'], index.get_ancestry(['tip']))
2601
        self.assertTrue(index.get_ancestry(['tip', 'separate']) in
2602
            (['tip', 'separate'],
2603
             ['separate', 'tip'],
2604
            ))
2605
        # asking for a ghost makes it go boom.
2606
        self.assertRaises(errors.RevisionNotPresent, index.get_ancestry, ['ghost'])
2607
2608
    def test_get_ancestry_with_ghosts(self):
2609
        index = self.two_graph_index()
2610
        self.assertEqual([], index.get_ancestry_with_ghosts([]))
2611
        self.assertEqual(['separate'], index.get_ancestry_with_ghosts(['separate']))
2612
        self.assertEqual(['tail'], index.get_ancestry_with_ghosts(['tail']))
2613
        self.assertEqual(['parent'], index.get_ancestry_with_ghosts(['parent']))
2614
        self.assertEqual(['tip'], index.get_ancestry_with_ghosts(['tip']))
2615
        self.assertTrue(index.get_ancestry_with_ghosts(['tip', 'separate']) in
2616
            (['tip', 'separate'],
2617
             ['separate', 'tip'],
2618
            ))
2619
        # asking for a ghost makes it go boom.
2620
        self.assertRaises(errors.RevisionNotPresent, index.get_ancestry_with_ghosts, ['ghost'])
2621
2622
    def test_num_versions(self):
2623
        index = self.two_graph_index()
2624
        self.assertEqual(4, index.num_versions())
2625
2626
    def test_get_versions(self):
2627
        index = self.two_graph_index()
2628
        self.assertEqual(set(['tail', 'tip', 'parent', 'separate']),
2629
            set(index.get_versions()))
2630
2631
    def test_has_version(self):
2632
        index = self.two_graph_index()
2633
        self.assertTrue(index.has_version('tail'))
2634
        self.assertFalse(index.has_version('ghost'))
2635
2636
    def test_get_position(self):
2637
        index = self.two_graph_index()
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
2638
        self.assertEqual((index._graph_index._indices[0], 0, 100), index.get_position('tip'))
2639
        self.assertEqual((index._graph_index._indices[1], 100, 78), index.get_position('parent'))
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2640
2641
    def test_get_method(self):
2642
        index = self.two_graph_index()
2643
        self.assertEqual('fulltext', index.get_method('tip'))
2658.2.1 by Robert Collins
Fix mismatch between KnitGraphIndex and KnitIndex in get_options.
2644
        self.assertEqual(['fulltext'], index.get_options('parent'))
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2645
2646
    def test_get_options(self):
2647
        index = self.two_graph_index()
2658.2.1 by Robert Collins
Fix mismatch between KnitGraphIndex and KnitIndex in get_options.
2648
        self.assertEqual(['fulltext', 'no-eol'], index.get_options('tip'))
2649
        self.assertEqual(['fulltext'], index.get_options('parent'))
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2650
2651
    def test_get_parents_with_ghosts(self):
2652
        index = self.two_graph_index()
2653
        self.assertEqual((), index.get_parents_with_ghosts('parent'))
2592.3.43 by Robert Collins
A knit iter_parents API.
2654
        # and errors on ghosts.
2655
        self.assertRaises(errors.RevisionNotPresent,
2656
            index.get_parents_with_ghosts, 'ghost')
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2657
2658
    def test_check_versions_present(self):
2659
        index = self.two_graph_index()
2660
        self.assertRaises(RevisionNotPresent, index.check_versions_present,
2661
            ['missing'])
2662
        self.assertRaises(RevisionNotPresent, index.check_versions_present,
2663
            ['tail', 'missing'])
2664
        index.check_versions_present(['tail', 'separate'])
2665
2666
    def catch_add(self, entries):
2667
        self.caught_entries.append(entries)
2668
2669
    def test_add_no_callback_errors(self):
2670
        index = self.two_graph_index()
2671
        self.assertRaises(errors.ReadOnlyError, index.add_version,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2672
            'new', 'fulltext,no-eol', (None, 50, 60), ['separate'])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2673
2674
    def test_add_version_smoke(self):
2675
        index = self.two_graph_index(catch_adds=True)
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2676
        index.add_version('new', 'fulltext,no-eol', (None, 50, 60), [])
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2677
        self.assertEqual([[(('new', ), 'N50 60')]],
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2678
            self.caught_entries)
2679
2680
    def test_add_version_delta_not_delta_index(self):
2681
        index = self.two_graph_index(catch_adds=True)
2682
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2683
            'new', 'no-eol,line-delta', (None, 0, 100), [])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2684
        self.assertEqual([], self.caught_entries)
2685
2686
    def test_add_version_same_dup(self):
2687
        index = self.two_graph_index(catch_adds=True)
2688
        # options can be spelt two different ways
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2689
        index.add_version('tip', 'fulltext,no-eol', (None, 0, 100), [])
2690
        index.add_version('tip', 'no-eol,fulltext', (None, 0, 100), [])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2691
        # but neither should have added data.
2692
        self.assertEqual([[], []], self.caught_entries)
2693
        
2694
    def test_add_version_different_dup(self):
2695
        index = self.two_graph_index(catch_adds=True)
2696
        # change options
2697
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2698
            'tip', 'no-eol,line-delta', (None, 0, 100), [])
2699
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2700
            'tip', 'line-delta,no-eol', (None, 0, 100), [])
2701
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2702
            'tip', 'fulltext', (None, 0, 100), [])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2703
        # position/length
2704
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2705
            'tip', 'fulltext,no-eol', (None, 50, 100), [])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2706
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2707
            'tip', 'fulltext,no-eol', (None, 0, 1000), [])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2708
        # parents
2709
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2710
            'tip', 'fulltext,no-eol', (None, 0, 100), ['parent'])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2711
        self.assertEqual([], self.caught_entries)
2712
        
2713
    def test_add_versions(self):
2714
        index = self.two_graph_index(catch_adds=True)
2715
        index.add_versions([
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2716
                ('new', 'fulltext,no-eol', (None, 50, 60), []),
2717
                ('new2', 'fulltext', (None, 0, 6), []),
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2718
                ])
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2719
        self.assertEqual([(('new', ), 'N50 60'), (('new2', ), ' 0 6')],
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2720
            sorted(self.caught_entries[0]))
2721
        self.assertEqual(1, len(self.caught_entries))
2722
2723
    def test_add_versions_delta_not_delta_index(self):
2724
        index = self.two_graph_index(catch_adds=True)
2725
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2726
            [('new', 'no-eol,line-delta', (None, 0, 100), ['parent'])])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2727
        self.assertEqual([], self.caught_entries)
2728
2729
    def test_add_versions_parents_not_parents_index(self):
2730
        index = self.two_graph_index(catch_adds=True)
2731
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2732
            [('new', 'no-eol,fulltext', (None, 0, 100), ['parent'])])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2733
        self.assertEqual([], self.caught_entries)
2734
2841.2.1 by Robert Collins
* Commit no longer checks for new text keys during insertion when the
2735
    def test_add_versions_random_id_accepted(self):
2736
        index = self.two_graph_index(catch_adds=True)
2737
        index.add_versions([], random_id=True)
2738
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2739
    def test_add_versions_same_dup(self):
2740
        index = self.two_graph_index(catch_adds=True)
2741
        # options can be spelt two different ways
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2742
        index.add_versions([('tip', 'fulltext,no-eol', (None, 0, 100), [])])
2743
        index.add_versions([('tip', 'no-eol,fulltext', (None, 0, 100), [])])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2744
        # but neither should have added data.
2745
        self.assertEqual([[], []], self.caught_entries)
2746
        
2747
    def test_add_versions_different_dup(self):
2748
        index = self.two_graph_index(catch_adds=True)
2749
        # change options
2750
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2751
            [('tip', 'no-eol,line-delta', (None, 0, 100), [])])
2752
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2753
            [('tip', 'line-delta,no-eol', (None, 0, 100), [])])
2754
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2755
            [('tip', 'fulltext', (None, 0, 100), [])])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2756
        # position/length
2757
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2758
            [('tip', 'fulltext,no-eol', (None, 50, 100), [])])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2759
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2760
            [('tip', 'fulltext,no-eol', (None, 0, 1000), [])])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2761
        # parents
2762
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2763
            [('tip', 'fulltext,no-eol', (None, 0, 100), ['parent'])])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2764
        # change options in the second record
2765
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2766
            [('tip', 'fulltext,no-eol', (None, 0, 100), []),
2767
             ('tip', 'no-eol,line-delta', (None, 0, 100), [])])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2768
        self.assertEqual([], self.caught_entries)
2769
2592.3.43 by Robert Collins
A knit iter_parents API.
2770
    def test_iter_parents(self):
2771
        index = self.two_graph_index()
2772
        self.assertEqual(set([
2773
            ('tip', ()), ('tail', ()), ('parent', ()), ('separate', ())
2774
            ]),
2775
            set(index.iter_parents(['tip', 'tail', 'ghost', 'parent', 'separate'])))
2776
        self.assertEqual(set([('tip', ())]),
2777
            set(index.iter_parents(['tip'])))
2778
        self.assertEqual(set(),
2779
            set(index.iter_parents([])))
3015.2.19 by Robert Collins
Don't include the pack container length in the lengths given by get_data_stream.
2780
2781
2782
class TestPackKnits(KnitTests):
2783
    """Tests that use a _PackAccess and KnitGraphIndex."""
2784
2785
    def test_get_data_stream_packs_ignores_pack_overhead(self):
2786
        # Packs have an encoding overhead that should not be included in the
2787
        # 'size' field of a data stream, because it is not returned by the
2788
        # raw_reading functions - it is why index_memo's are opaque, and
2789
        # get_data_stream was abusing this.
2790
        packname = 'test.pack'
2791
        transport = self.get_transport()
2792
        def write_data(bytes):
2793
            transport.append_bytes(packname, bytes)
2794
        writer = pack.ContainerWriter(write_data)
2795
        writer.begin()
2796
        index = InMemoryGraphIndex(2)
2797
        knit_index = KnitGraphIndex(index, add_callback=index.add_nodes,
2798
            deltas=True)
2799
        indices = {index:(transport, packname)}
2800
        access = _PackAccess(indices, writer=(writer, index))
2801
        k = KnitVersionedFile('test', get_transport('.'),
2802
            delta=True, create=True, index=knit_index, access_method=access)
2803
        # insert something into the knit
2804
        k.add_lines('text-1', [], ["foo\n"])
2805
        # get a data stream for it
2806
        stream = k.get_data_stream(['text-1'])
2807
        # if the stream has been incorrectly assembled, we will get a short read
2808
        # reading from the stream (as streams have no trailer)
2809
        expected_length = stream[1][0][2]
2810
        # we use -1 to do the read, so that if a trailer is added this test
2811
        # will fail and we'll adjust it to handle that case correctly, rather
2812
        # than allowing an over-read that is bogus.
2813
        self.assertEqual(expected_length, len(stream[2](-1)))
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2814
2815
2816
class Test_StreamIndex(KnitTests):
2817
2818
    def get_index(self, knit, stream):
2819
        """Get a _StreamIndex from knit and stream."""
2820
        return knit._knit_from_datastream(stream)._index
2821
2822
    def assertIndexVersions(self, knit, versions):
2823
        """Check that the _StreamIndex versions are those of the stream."""
2824
        index = self.get_index(knit, knit.get_data_stream(versions))
2825
        self.assertEqual(set(index.get_versions()), set(versions))
2826
        # check we didn't get duplicates
2827
        self.assertEqual(len(index.get_versions()), len(versions))
2828
2829
    def assertIndexAncestry(self, knit, ancestry_versions, versions, result):
2830
        """Check the result of a get_ancestry call on knit."""
2831
        index = self.get_index(knit, knit.get_data_stream(versions))
3052.2.4 by Andrew Bennetts
Some tweaks suggested by John's review.
2832
        self.assertEqual(
2833
            set(result),
2834
            set(index.get_ancestry(ancestry_versions, False)))
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2835
3052.2.3 by Robert Collins
Handle insert_data_stream of an unannotated stream into an annotated knit.
2836
    def assertIterParents(self, knit, versions, parent_versions, result):
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2837
        """Check the result of an iter_parents call on knit."""
2838
        index = self.get_index(knit, knit.get_data_stream(versions))
3052.2.3 by Robert Collins
Handle insert_data_stream of an unannotated stream into an annotated knit.
2839
        self.assertEqual(result, index.iter_parents(parent_versions))
2840
2841
    def assertGetMethod(self, knit, versions, version, result):
2842
        index = self.get_index(knit, knit.get_data_stream(versions))
2843
        self.assertEqual(result, index.get_method(version))
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2844
2845
    def assertGetOptions(self, knit, version, options):
2846
        index = self.get_index(knit, knit.get_data_stream(version))
2847
        self.assertEqual(options, index.get_options(version))
2848
2849
    def assertGetPosition(self, knit, versions, version, result):
2850
        index = self.get_index(knit, knit.get_data_stream(versions))
3052.2.5 by Andrew Bennetts
Address the rest of the review comments from John and myself.
2851
        if result[1] is None:
2852
            result = (result[0], index, result[2], result[3])
3052.2.3 by Robert Collins
Handle insert_data_stream of an unannotated stream into an annotated knit.
2853
        self.assertEqual(result, index.get_position(version))
2854
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2855
    def assertGetParentsWithGhosts(self, knit, versions, version, parents):
2856
        index = self.get_index(knit, knit.get_data_stream(versions))
2857
        self.assertEqual(parents, index.get_parents_with_ghosts(version))
2858
2859
    def make_knit_with_4_versions_2_dags(self):
2860
        knit = self.make_test_knit()
2861
        knit.add_lines('a', [], ["foo"])
2862
        knit.add_lines('b', [], [])
2863
        knit.add_lines('c', ['b', 'a'], [])
2864
        knit.add_lines_with_ghosts('d', ['e', 'f'], [])
2865
        return knit
2866
2867
    def test_versions(self):
2868
        """The versions of a StreamIndex are those of the datastream."""
2869
        knit = self.make_knit_with_4_versions_2_dags()
2870
        # ask for most permutations, which catches bugs like falling back to the
2871
        # target knit, or showing ghosts, etc.
2872
        self.assertIndexVersions(knit, [])
2873
        self.assertIndexVersions(knit, ['a'])
2874
        self.assertIndexVersions(knit, ['b'])
2875
        self.assertIndexVersions(knit, ['c'])
2876
        self.assertIndexVersions(knit, ['d'])
2877
        self.assertIndexVersions(knit, ['a', 'b'])
2878
        self.assertIndexVersions(knit, ['b', 'c'])
2879
        self.assertIndexVersions(knit, ['a', 'c'])
2880
        self.assertIndexVersions(knit, ['a', 'b', 'c'])
2881
        self.assertIndexVersions(knit, ['a', 'b', 'c', 'd'])
2882
2883
    def test_construct(self):
2884
        """Constructing a StreamIndex generates index data."""
2885
        data_list = [('text-a', ['fulltext'], 127, []),
2886
            ('text-b', ['option'], 128, ['text-c'])]
3224.1.8 by John Arbash Meinel
Add noeol to the return signature of get_build_details.
2887
        index = _StreamIndex(data_list, None)
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2888
        self.assertEqual({'text-a':(['fulltext'], (0, 127), []),
2889
            'text-b':(['option'], (127, 127 + 128), ['text-c'])},
2890
            index._by_version)
2891
2892
    def test_get_ancestry(self):
2893
        knit = self.make_knit_with_4_versions_2_dags()
2894
        self.assertIndexAncestry(knit, ['a'], ['a'], ['a'])
2895
        self.assertIndexAncestry(knit, ['b'], ['b'], ['b'])
2896
        self.assertIndexAncestry(knit, ['c'], ['c'], ['c'])
2897
        self.assertIndexAncestry(knit, ['c'], ['a', 'b', 'c'],
3052.2.4 by Andrew Bennetts
Some tweaks suggested by John's review.
2898
            set(['a', 'b', 'c']))
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2899
        self.assertIndexAncestry(knit, ['c', 'd'], ['a', 'b', 'c', 'd'],
3052.2.4 by Andrew Bennetts
Some tweaks suggested by John's review.
2900
            set(['a', 'b', 'c', 'd']))
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2901
3052.2.3 by Robert Collins
Handle insert_data_stream of an unannotated stream into an annotated knit.
2902
    def test_get_method(self):
2903
        knit = self.make_knit_with_4_versions_2_dags()
2904
        self.assertGetMethod(knit, ['a'], 'a', 'fulltext')
2905
        self.assertGetMethod(knit, ['c'], 'c', 'line-delta')
2906
        # get_method on a basis that is not in the datastream (but in the
2907
        # backing knit) returns 'fulltext', because thats what we'll create as
2908
        # we thunk across.
2909
        self.assertGetMethod(knit, ['c'], 'b', 'fulltext')
2910
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2911
    def test_iter_parents(self):
2912
        knit = self.make_knit_with_4_versions_2_dags()
3287.5.5 by Robert Collins
Refactor internals of knit implementations to implement get_parents_with_ghosts in terms of get_parent_map.
2913
        self.assertIterParents(knit, ['a'], ['a'], [('a', ())])
3052.2.3 by Robert Collins
Handle insert_data_stream of an unannotated stream into an annotated knit.
2914
        self.assertIterParents(knit, ['a', 'b'], ['a', 'b'],
3287.5.5 by Robert Collins
Refactor internals of knit implementations to implement get_parents_with_ghosts in terms of get_parent_map.
2915
            [('a', ()), ('b', ())])
3052.2.3 by Robert Collins
Handle insert_data_stream of an unannotated stream into an annotated knit.
2916
        self.assertIterParents(knit, ['a', 'b', 'c'], ['a', 'b', 'c'],
3287.5.5 by Robert Collins
Refactor internals of knit implementations to implement get_parents_with_ghosts in terms of get_parent_map.
2917
            [('a', ()), ('b', ()), ('c', ('b', 'a'))])
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2918
        self.assertIterParents(knit, ['a', 'b', 'c', 'd'],
3052.2.3 by Robert Collins
Handle insert_data_stream of an unannotated stream into an annotated knit.
2919
            ['a', 'b', 'c', 'd'],
3287.5.5 by Robert Collins
Refactor internals of knit implementations to implement get_parents_with_ghosts in terms of get_parent_map.
2920
            [('a', ()), ('b', ()), ('c', ('b', 'a')), ('d', ('e', 'f'))])
3052.2.3 by Robert Collins
Handle insert_data_stream of an unannotated stream into an annotated knit.
2921
        self.assertIterParents(knit, ['c'], ['a', 'b', 'c'],
3287.5.5 by Robert Collins
Refactor internals of knit implementations to implement get_parents_with_ghosts in terms of get_parent_map.
2922
            [('c', ('b', 'a'))])
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2923
2924
    def test_get_options(self):
2925
        knit = self.make_knit_with_4_versions_2_dags()
2926
        self.assertGetOptions(knit, 'a', ['no-eol', 'fulltext'])
2927
        self.assertGetOptions(knit, 'c', ['line-delta'])
2928
2929
    def test_get_parents_with_ghosts(self):
2930
        knit = self.make_knit_with_4_versions_2_dags()
3287.5.5 by Robert Collins
Refactor internals of knit implementations to implement get_parents_with_ghosts in terms of get_parent_map.
2931
        self.assertGetParentsWithGhosts(knit, ['a'], 'a', ())
2932
        self.assertGetParentsWithGhosts(knit, ['c'], 'c', ('b', 'a'))
2933
        self.assertGetParentsWithGhosts(knit, ['d'], 'd', ('e', 'f'))
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2934
2935
    def test_get_position(self):
2936
        knit = self.make_knit_with_4_versions_2_dags()
3052.2.6 by Andrew Bennetts
Fix typo in comment.
2937
        # get_position returns (thunk_flag, index(can be None), start, end) for
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2938
        # _StreamAccess to use.
3052.2.5 by Andrew Bennetts
Address the rest of the review comments from John and myself.
2939
        self.assertGetPosition(knit, ['a'], 'a', (False, None, 0, 78))
2940
        self.assertGetPosition(knit, ['a', 'c'], 'c', (False, None, 78, 156))
3052.2.3 by Robert Collins
Handle insert_data_stream of an unannotated stream into an annotated knit.
2941
        # get_position on a text that is not in the datastream (but in the
3052.2.5 by Andrew Bennetts
Address the rest of the review comments from John and myself.
2942
        # backing knit) returns (True, 'versionid', None, None) - and then the
3052.2.3 by Robert Collins
Handle insert_data_stream of an unannotated stream into an annotated knit.
2943
        # access object can construct the relevant data as needed.
3052.2.5 by Andrew Bennetts
Address the rest of the review comments from John and myself.
2944
        self.assertGetPosition(knit, ['a', 'c'], 'b', (True, 'b', None, None))
3052.2.3 by Robert Collins
Handle insert_data_stream of an unannotated stream into an annotated knit.
2945
2946
2947
class Test_StreamAccess(KnitTests):
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2948
2949
    def get_index_access(self, knit, stream):
2950
        """Get a _StreamAccess from knit and stream."""
2951
        knit =  knit._knit_from_datastream(stream)
2952
        return knit._index, knit._data._access
2953
2954
    def assertGetRawRecords(self, knit, versions):
2955
        index, access = self.get_index_access(knit,
2956
            knit.get_data_stream(versions))
2957
        # check that every version asked for can be obtained from the resulting
2958
        # access object.
2959
        # batch
2960
        memos = []
2961
        for version in versions:
2962
            memos.append(knit._index.get_position(version))
2963
        original = {}
2964
        for version, data in zip(
2965
            versions, knit._data._access.get_raw_records(memos)):
2966
            original[version] = data
2967
        memos = []
2968
        for version in versions:
2969
            memos.append(index.get_position(version))
2970
        streamed = {}
2971
        for version, data in zip(versions, access.get_raw_records(memos)):
2972
            streamed[version] = data
2973
        self.assertEqual(original, streamed)
2974
        # individually
2975
        for version in versions:
2976
            data = list(access.get_raw_records(
2977
                [index.get_position(version)]))[0]
2978
            self.assertEqual(original[version], data)
2979
2980
    def make_knit_with_two_versions(self):
2981
        knit = self.make_test_knit()
2982
        knit.add_lines('a', [], ["foo"])
2983
        knit.add_lines('b', [], ["bar"])
2984
        return knit
2985
2986
    def test_get_raw_records(self):
2987
        knit = self.make_knit_with_two_versions()
2988
        self.assertGetRawRecords(knit, ['a', 'b'])
2989
        self.assertGetRawRecords(knit, ['a'])
2990
        self.assertGetRawRecords(knit, ['b'])
3052.2.3 by Robert Collins
Handle insert_data_stream of an unannotated stream into an annotated knit.
2991
    
2992
    def test_get_raw_record_from_backing_knit(self):
2993
        # the thunk layer should create an artificial A on-demand when needed.
2994
        source_knit = self.make_test_knit(name='plain', annotate=False)
2995
        target_knit = self.make_test_knit(name='annotated', annotate=True)
2996
        source_knit.add_lines("A", [], ["Foo\n"])
2997
        # Give the target A, so we can try to thunk across to it.
2998
        target_knit.join(source_knit)
2999
        index, access = self.get_index_access(target_knit,
3000
            source_knit.get_data_stream([]))
3052.2.5 by Andrew Bennetts
Address the rest of the review comments from John and myself.
3001
        raw_data = list(access.get_raw_records([(True, "A", None, None)]))[0]
3052.2.3 by Robert Collins
Handle insert_data_stream of an unannotated stream into an annotated knit.
3002
        df = GzipFile(mode='rb', fileobj=StringIO(raw_data))
3003
        self.assertEqual(
3004
            'version A 1 5d36b88bb697a2d778f024048bafabd443d74503\n'
3005
            'Foo\nend A\n',
3006
            df.read())
3007
3008
    def test_asking_for_thunk_stream_is_not_plain_errors(self):
3009
        knit = self.make_test_knit(name='annotated', annotate=True)
3010
        knit.add_lines("A", [], ["Foo\n"])
3011
        index, access = self.get_index_access(knit,
3012
            knit.get_data_stream([]))
3013
        self.assertRaises(errors.KnitCorrupt,
3052.2.5 by Andrew Bennetts
Address the rest of the review comments from John and myself.
3014
            list, access.get_raw_records([(True, "A", None, None)]))