/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
2535.3.5 by Andrew Bennetts
Batch writes as much as possible in insert_data_stream.
1927
    #  * test that a stream of "already present version, then new version"
1928
    #    inserts correctly.
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1929
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
1930
1931
    def assertMadeStreamKnit(self, source_knit, versions, target_knit):
1932
        """Assert that a knit made from a stream is as expected."""
1933
        a_stream = source_knit.get_data_stream(versions)
1934
        expected_data = a_stream[2](None)
1935
        a_stream = source_knit.get_data_stream(versions)
1936
        a_knit = target_knit._knit_from_datastream(a_stream)
1937
        self.assertEqual(source_knit.factory.__class__,
1938
            a_knit.factory.__class__)
1939
        self.assertIsInstance(a_knit._data._access, _StreamAccess)
1940
        self.assertIsInstance(a_knit._index, _StreamIndex)
1941
        self.assertEqual(a_knit._index.data_list, a_stream[1])
1942
        self.assertEqual(a_knit._data._access.data, expected_data)
1943
        self.assertEqual(a_knit.filename, target_knit.filename)
1944
        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.
1945
        self.assertEqual(a_knit._index, a_knit._data._access.stream_index)
1946
        self.assertEqual(target_knit, a_knit._data._access.backing_knit)
1947
        self.assertIsInstance(a_knit._data._access.orig_factory,
1948
            source_knit.factory.__class__)
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
1949
1950
    def test__knit_from_data_stream_empty(self):
1951
        """Create a knit object from a datastream."""
1952
        annotated = self.make_test_knit(name='source', annotate=True)
1953
        plain = self.make_test_knit(name='target', annotate=False)
1954
        # case 1: annotated source
1955
        self.assertMadeStreamKnit(annotated, [], annotated)
1956
        self.assertMadeStreamKnit(annotated, [], plain)
1957
        # case 2: plain source
1958
        self.assertMadeStreamKnit(plain, [], annotated)
1959
        self.assertMadeStreamKnit(plain, [], plain)
1960
1961
    def test__knit_from_data_stream_unknown_format(self):
1962
        annotated = self.make_test_knit(name='source', annotate=True)
1963
        self.assertRaises(errors.KnitDataStreamUnknown,
1964
            annotated._knit_from_datastream, ("unknown", None, None))
1965
1966
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1967
TEXT_1 = """\
1968
Banana cup cakes:
1969
1970
- bananas
1971
- eggs
1972
- broken tea cups
1973
"""
1974
1975
TEXT_1A = """\
1976
Banana cup cake recipe
1977
(serves 6)
1978
1979
- bananas
1980
- eggs
1981
- broken tea cups
1982
- self-raising flour
1983
"""
1984
1664.2.1 by Aaron Bentley
Start work on plan_merge test
1985
TEXT_1B = """\
1986
Banana cup cake recipe
1987
1988
- bananas (do not use plantains!!!)
1989
- broken tea cups
1990
- flour
1991
"""
1992
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1993
delta_1_1a = """\
1994
0,1,2
1995
Banana cup cake recipe
1996
(serves 6)
1997
5,5,1
1998
- self-raising flour
1999
"""
2000
2001
TEXT_2 = """\
2002
Boeuf bourguignon
2003
2004
- beef
2005
- red wine
2006
- small onions
2007
- carrot
2008
- mushrooms
2009
"""
2010
1664.2.3 by Aaron Bentley
Add failing test case
2011
AB_MERGE_TEXT="""unchanged|Banana cup cake recipe
2012
new-a|(serves 6)
2013
unchanged|
2014
killed-b|- bananas
2015
killed-b|- eggs
2016
new-b|- bananas (do not use plantains!!!)
2017
unchanged|- broken tea cups
2018
new-a|- self-raising flour
1664.2.6 by Aaron Bentley
Got plan-merge passing tests
2019
new-b|- flour
2020
"""
1664.2.3 by Aaron Bentley
Add failing test case
2021
AB_MERGE=[tuple(l.split('|')) for l in AB_MERGE_TEXT.splitlines(True)]
2022
2023
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
2024
def line_delta(from_lines, to_lines):
2025
    """Generate line-based delta from one text to another"""
2026
    s = difflib.SequenceMatcher(None, from_lines, to_lines)
2027
    for op in s.get_opcodes():
2028
        if op[0] == 'equal':
2029
            continue
2030
        yield '%d,%d,%d\n' % (op[1], op[2], op[4]-op[3])
2031
        for i in range(op[3], op[4]):
2032
            yield to_lines[i]
2033
2034
2035
def apply_line_delta(basis_lines, delta_lines):
2036
    """Apply a line-based perfect diff
2037
    
2038
    basis_lines -- text to apply the patch to
2039
    delta_lines -- diff instructions and content
2040
    """
2041
    out = basis_lines[:]
2042
    i = 0
2043
    offset = 0
2044
    while i < len(delta_lines):
2045
        l = delta_lines[i]
2046
        a, b, c = map(long, l.split(','))
2047
        i = i + 1
2048
        out[offset+a:offset+b] = delta_lines[i:i+c]
2049
        i = i + c
2050
        offset = offset + (b - a) + c
2051
    return out
1684.3.3 by Robert Collins
Add a special cased weaves to knit converter.
2052
2053
2054
class TestWeaveToKnit(KnitTests):
2055
2056
    def test_weave_to_knit_matches(self):
2057
        # check that the WeaveToKnit is_compatible function
2058
        # registers True for a Weave to a Knit.
2059
        w = Weave()
2060
        k = self.make_test_knit()
2061
        self.failUnless(WeaveToKnit.is_compatible(w, k))
2062
        self.failIf(WeaveToKnit.is_compatible(k, w))
2063
        self.failIf(WeaveToKnit.is_compatible(w, w))
2064
        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
2065
2066
2067
class TestKnitCaching(KnitTests):
2068
    
2850.1.1 by Robert Collins
* ``KnitVersionedFile.add*`` will no longer cache added records even when
2069
    def create_knit(self):
1863.1.1 by John Arbash Meinel
Allow Versioned files to do caching if explicitly asked, and implement for Knit
2070
        k = self.make_test_knit(True)
2071
        k.add_lines('text-1', [], split_lines(TEXT_1))
2072
        k.add_lines('text-2', [], split_lines(TEXT_2))
2073
        return k
2074
2075
    def test_no_caching(self):
2076
        k = self.create_knit()
2077
        # Nothing should be cached without setting 'enable_cache'
2078
        self.assertEqual({}, k._data._cache)
2079
2080
    def test_cache_data_read_raw(self):
2081
        k = self.create_knit()
2082
2083
        # Now cache and read
2084
        k.enable_cache()
2085
2086
        def read_one_raw(version):
2087
            pos_map = k._get_components_positions([version])
3224.1.13 by John Arbash Meinel
Revert the _get_component_positions api
2088
            method, index_memo, next = pos_map[version]
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
2089
            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
2090
            self.assertEqual(1, len(lst))
2091
            return lst[0]
2092
2093
        val = read_one_raw('text-1')
1863.1.8 by John Arbash Meinel
Removing disk-backed-cache
2094
        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
2095
2096
        k.clear_cache()
2097
        # After clear, new reads are not cached
2098
        self.assertEqual({}, k._data._cache)
2099
2100
        val2 = read_one_raw('text-1')
2101
        self.assertEqual(val, val2)
2102
        self.assertEqual({}, k._data._cache)
2103
2104
    def test_cache_data_read(self):
2105
        k = self.create_knit()
2106
2107
        def read_one(version):
2108
            pos_map = k._get_components_positions([version])
3224.1.13 by John Arbash Meinel
Revert the _get_component_positions api
2109
            method, index_memo, next = pos_map[version]
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
2110
            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
2111
            self.assertEqual(1, len(lst))
2112
            return lst[0]
2113
2114
        # Now cache and read
2115
        k.enable_cache()
2116
2117
        val = read_one('text-2')
2118
        self.assertEqual(['text-2'], k._data._cache.keys())
2119
        self.assertEqual('text-2', val[0])
2120
        content, digest = k._data._parse_record('text-2',
2121
                                                k._data._cache['text-2'])
2122
        self.assertEqual(content, val[1])
2123
        self.assertEqual(digest, val[2])
2124
2125
        k.clear_cache()
2126
        self.assertEqual({}, k._data._cache)
2127
2128
        val2 = read_one('text-2')
2129
        self.assertEqual(val, val2)
2130
        self.assertEqual({}, k._data._cache)
2131
2132
    def test_cache_read(self):
2133
        k = self.create_knit()
2134
        k.enable_cache()
2135
2136
        text = k.get_text('text-1')
2137
        self.assertEqual(TEXT_1, text)
2138
        self.assertEqual(['text-1'], k._data._cache.keys())
2139
2140
        k.clear_cache()
2141
        self.assertEqual({}, k._data._cache)
2142
2143
        text = k.get_text('text-1')
2144
        self.assertEqual(TEXT_1, text)
2145
        self.assertEqual({}, k._data._cache)
2102.2.1 by John Arbash Meinel
Fix bug #64789 _KnitIndex.add_versions() should dict compress new revisions
2146
2147
2148
class TestKnitIndex(KnitTests):
2149
2150
    def test_add_versions_dictionary_compresses(self):
2151
        """Adding versions to the index should update the lookup dict"""
2152
        knit = self.make_test_knit()
2153
        idx = knit._index
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
2154
        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
2155
        self.check_file_contents('test.kndx',
2156
            '# bzr knit index 8\n'
2157
            '\n'
2158
            'a-1 fulltext 0 0  :'
2159
            )
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
2160
        idx.add_versions([('a-2', ['fulltext'], (None, 0, 0), ['a-1']),
2161
                          ('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
2162
                         ])
2163
        self.check_file_contents('test.kndx',
2164
            '# bzr knit index 8\n'
2165
            '\n'
2166
            'a-1 fulltext 0 0  :\n'
2167
            'a-2 fulltext 0 0 0 :\n'
2168
            'a-3 fulltext 0 0 1 :'
2169
            )
2170
        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.
2171
        self.assertEqual({'a-1':('a-1', ['fulltext'], 0, 0, (), 0),
2172
                          'a-2':('a-2', ['fulltext'], 0, 0, ('a-1',), 1),
2173
                          '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
2174
                         }, idx._cache)
2175
2176
    def test_add_versions_fails_clean(self):
2177
        """If add_versions fails in the middle, it restores a pristine state.
2178
2179
        Any modifications that are made to the index are reset if all versions
2180
        cannot be added.
2181
        """
2182
        # This cheats a little bit by passing in a generator which will
2183
        # raise an exception before the processing finishes
2184
        # Other possibilities would be to have an version with the wrong number
2185
        # of entries, or to make the backing transport unable to write any
2186
        # files.
2187
2188
        knit = self.make_test_knit()
2189
        idx = knit._index
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
2190
        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
2191
2192
        class StopEarly(Exception):
2193
            pass
2194
2195
        def generate_failure():
2196
            """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.
2197
            yield ('a-2', ['fulltext'], (None, 0, 0), ('a-1',))
2198
            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
2199
            raise StopEarly()
2200
2201
        # Assert the pre-condition
2202
        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.
2203
        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
2204
2205
        self.assertRaises(StopEarly, idx.add_versions, generate_failure())
2206
2207
        # And it shouldn't be modified
2208
        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.
2209
        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.
2210
2211
    def test_knit_index_ignores_empty_files(self):
2212
        # There was a race condition in older bzr, where a ^C at the right time
2213
        # could leave an empty .kndx file, which bzr would later claim was a
2214
        # corrupted file since the header was not present. In reality, the file
2215
        # just wasn't created, so it should be ignored.
2216
        t = get_transport('.')
2217
        t.put_bytes('test.kndx', '')
2218
2219
        knit = self.make_test_knit()
2220
2221
    def test_knit_index_checks_header(self):
2222
        t = get_transport('.')
2223
        t.put_bytes('test.kndx', '# not really a knit header\n\n')
2224
2196.2.1 by John Arbash Meinel
Merge Dmitry's optimizations and minimize the actual diff.
2225
        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.
2226
2227
2228
class TestGraphIndexKnit(KnitTests):
2229
    """Tests for knits using a GraphIndex rather than a KnitIndex."""
2230
2231
    def make_g_index(self, name, ref_lists=0, nodes=[]):
2232
        builder = GraphIndexBuilder(ref_lists)
2233
        for node, references, value in nodes:
2234
            builder.add_node(node, references, value)
2235
        stream = builder.finish()
2236
        trans = self.get_transport()
2890.2.1 by Robert Collins
* ``bzrlib.index.GraphIndex`` now requires a size parameter to the
2237
        size = trans.put_file(name, stream)
2238
        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.
2239
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2240
    def two_graph_index(self, deltas=False, catch_adds=False):
2592.3.13 by Robert Collins
Implement KnitGraphIndex.get_method.
2241
        """Build a two-graph index.
2242
2243
        :param deltas: If true, use underlying indices with two node-ref
2244
            lists and 'parent' set to a delta-compressed against tail.
2245
        """
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.
2246
        # build a complex graph across several indices.
2592.3.13 by Robert Collins
Implement KnitGraphIndex.get_method.
2247
        if deltas:
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2248
            # delta compression inn the index
2592.3.13 by Robert Collins
Implement KnitGraphIndex.get_method.
2249
            index1 = self.make_g_index('1', 2, [
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2250
                (('tip', ), 'N0 100', ([('parent', )], [], )),
2251
                (('tail', ), '', ([], []))])
2592.3.13 by Robert Collins
Implement KnitGraphIndex.get_method.
2252
            index2 = self.make_g_index('2', 2, [
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2253
                (('parent', ), ' 100 78', ([('tail', ), ('ghost', )], [('tail', )])),
2254
                (('separate', ), '', ([], []))])
2592.3.13 by Robert Collins
Implement KnitGraphIndex.get_method.
2255
        else:
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2256
            # just blob location and graph in the index.
2592.3.13 by Robert Collins
Implement KnitGraphIndex.get_method.
2257
            index1 = self.make_g_index('1', 1, [
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2258
                (('tip', ), 'N0 100', ([('parent', )], )),
2259
                (('tail', ), '', ([], ))])
2592.3.13 by Robert Collins
Implement KnitGraphIndex.get_method.
2260
            index2 = self.make_g_index('2', 1, [
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2261
                (('parent', ), ' 100 78', ([('tail', ), ('ghost', )], )),
2262
                (('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.
2263
        combined_index = CombinedGraphIndex([index1, index2])
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2264
        if catch_adds:
2265
            self.combined_index = combined_index
2266
            self.caught_entries = []
2267
            add_callback = self.catch_add
2268
        else:
2269
            add_callback = None
2270
        return KnitGraphIndex(combined_index, deltas=deltas,
2271
            add_callback=add_callback)
2592.3.4 by Robert Collins
Implement get_ancestry/get_ancestry_with_ghosts for KnitGraphIndex.
2272
2273
    def test_get_ancestry(self):
2592.3.30 by Robert Collins
Make GraphKnitIndex get_ancestry the same as regular knits.
2274
        # get_ancestry is defined as eliding ghosts, not erroring.
2275
        index = self.two_graph_index()
2592.3.4 by Robert Collins
Implement get_ancestry/get_ancestry_with_ghosts for KnitGraphIndex.
2276
        self.assertEqual([], index.get_ancestry([]))
2277
        self.assertEqual(['separate'], index.get_ancestry(['separate']))
2278
        self.assertEqual(['tail'], index.get_ancestry(['tail']))
2279
        self.assertEqual(['tail', 'parent'], index.get_ancestry(['parent']))
2280
        self.assertEqual(['tail', 'parent', 'tip'], index.get_ancestry(['tip']))
2281
        self.assertTrue(index.get_ancestry(['tip', 'separate']) in
2282
            (['tail', 'parent', 'tip', 'separate'],
2283
             ['separate', 'tail', 'parent', 'tip'],
2284
            ))
2285
        # and without topo_sort
2286
        self.assertEqual(set(['separate']),
2287
            set(index.get_ancestry(['separate'], topo_sorted=False)))
2288
        self.assertEqual(set(['tail']),
2289
            set(index.get_ancestry(['tail'], topo_sorted=False)))
2290
        self.assertEqual(set(['tail', 'parent']),
2291
            set(index.get_ancestry(['parent'], topo_sorted=False)))
2292
        self.assertEqual(set(['tail', 'parent', 'tip']),
2293
            set(index.get_ancestry(['tip'], topo_sorted=False)))
2294
        self.assertEqual(set(['separate', 'tail', 'parent', 'tip']),
2295
            set(index.get_ancestry(['tip', 'separate'])))
2592.3.30 by Robert Collins
Make GraphKnitIndex get_ancestry the same as regular knits.
2296
        # asking for a ghost makes it go boom.
2297
        self.assertRaises(errors.RevisionNotPresent, index.get_ancestry, ['ghost'])
2592.3.4 by Robert Collins
Implement get_ancestry/get_ancestry_with_ghosts for KnitGraphIndex.
2298
2299
    def test_get_ancestry_with_ghosts(self):
2300
        index = self.two_graph_index()
2301
        self.assertEqual([], index.get_ancestry_with_ghosts([]))
2302
        self.assertEqual(['separate'], index.get_ancestry_with_ghosts(['separate']))
2303
        self.assertEqual(['tail'], index.get_ancestry_with_ghosts(['tail']))
2304
        self.assertTrue(index.get_ancestry_with_ghosts(['parent']) in
2305
            (['tail', 'ghost', 'parent'],
2306
             ['ghost', 'tail', 'parent'],
2307
            ))
2308
        self.assertTrue(index.get_ancestry_with_ghosts(['tip']) in
2309
            (['tail', 'ghost', 'parent', 'tip'],
2310
             ['ghost', 'tail', 'parent', 'tip'],
2311
            ))
2312
        self.assertTrue(index.get_ancestry_with_ghosts(['tip', 'separate']) in
2313
            (['tail', 'ghost', 'parent', 'tip', 'separate'],
2314
             ['ghost', 'tail', 'parent', 'tip', 'separate'],
2315
             ['separate', 'tail', 'ghost', 'parent', 'tip'],
2316
             ['separate', 'ghost', 'tail', 'parent', 'tip'],
2317
            ))
2592.3.30 by Robert Collins
Make GraphKnitIndex get_ancestry the same as regular knits.
2318
        # asking for a ghost makes it go boom.
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2319
        self.assertRaises(errors.RevisionNotPresent, index.get_ancestry_with_ghosts, ['ghost'])
2592.3.5 by Robert Collins
Implement KnitGraphIndex.num_versions.
2320
2321
    def test_num_versions(self):
2322
        index = self.two_graph_index()
2323
        self.assertEqual(4, index.num_versions())
2592.3.6 by Robert Collins
Implement KnitGraphIndex.get_versions.
2324
2325
    def test_get_versions(self):
2326
        index = self.two_graph_index()
2327
        self.assertEqual(set(['tail', 'tip', 'parent', 'separate']),
2328
            set(index.get_versions()))
2329
2592.3.9 by Robert Collins
Implement KnitGraphIndex.has_version.
2330
    def test_has_version(self):
2331
        index = self.two_graph_index()
2332
        self.assertTrue(index.has_version('tail'))
2333
        self.assertFalse(index.has_version('ghost'))
2334
2592.3.10 by Robert Collins
Implement KnitGraphIndex.get_position.
2335
    def test_get_position(self):
2336
        index = self.two_graph_index()
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
2337
        self.assertEqual((index._graph_index._indices[0], 0, 100), index.get_position('tip'))
2338
        self.assertEqual((index._graph_index._indices[1], 100, 78), index.get_position('parent'))
2592.3.10 by Robert Collins
Implement KnitGraphIndex.get_position.
2339
2592.3.13 by Robert Collins
Implement KnitGraphIndex.get_method.
2340
    def test_get_method_deltas(self):
2341
        index = self.two_graph_index(deltas=True)
2592.3.11 by Robert Collins
Implement KnitGraphIndex.get_method.
2342
        self.assertEqual('fulltext', index.get_method('tip'))
2343
        self.assertEqual('line-delta', index.get_method('parent'))
2344
2592.3.13 by Robert Collins
Implement KnitGraphIndex.get_method.
2345
    def test_get_method_no_deltas(self):
2346
        # check that the parent-history lookup is ignored with deltas=False.
2347
        index = self.two_graph_index(deltas=False)
2348
        self.assertEqual('fulltext', index.get_method('tip'))
2349
        self.assertEqual('fulltext', index.get_method('parent'))
2350
2592.3.14 by Robert Collins
Implement KnitGraphIndex.get_options.
2351
    def test_get_options_deltas(self):
2352
        index = self.two_graph_index(deltas=True)
2658.2.1 by Robert Collins
Fix mismatch between KnitGraphIndex and KnitIndex in get_options.
2353
        self.assertEqual(['fulltext', 'no-eol'], index.get_options('tip'))
2354
        self.assertEqual(['line-delta'], index.get_options('parent'))
2592.3.14 by Robert Collins
Implement KnitGraphIndex.get_options.
2355
2356
    def test_get_options_no_deltas(self):
2357
        # check that the parent-history lookup is ignored with deltas=False.
2358
        index = self.two_graph_index(deltas=False)
2658.2.1 by Robert Collins
Fix mismatch between KnitGraphIndex and KnitIndex in get_options.
2359
        self.assertEqual(['fulltext', 'no-eol'], index.get_options('tip'))
2360
        self.assertEqual(['fulltext'], index.get_options('parent'))
2592.3.14 by Robert Collins
Implement KnitGraphIndex.get_options.
2361
2592.3.15 by Robert Collins
Implement KnitGraphIndex.get_parents/get_parents_with_ghosts.
2362
    def test_get_parents_with_ghosts(self):
2363
        index = self.two_graph_index()
2364
        self.assertEqual(('tail', 'ghost'), index.get_parents_with_ghosts('parent'))
2592.3.43 by Robert Collins
A knit iter_parents API.
2365
        # and errors on ghosts.
2366
        self.assertRaises(errors.RevisionNotPresent,
2367
            index.get_parents_with_ghosts, 'ghost')
2592.3.15 by Robert Collins
Implement KnitGraphIndex.get_parents/get_parents_with_ghosts.
2368
2592.3.16 by Robert Collins
Implement KnitGraphIndex.check_versions_present.
2369
    def test_check_versions_present(self):
2370
        # ghosts should not be considered present
2371
        index = self.two_graph_index()
2372
        self.assertRaises(RevisionNotPresent, index.check_versions_present,
2373
            ['ghost'])
2374
        self.assertRaises(RevisionNotPresent, index.check_versions_present,
2375
            ['tail', 'ghost'])
2376
        index.check_versions_present(['tail', 'separate'])
2592.3.14 by Robert Collins
Implement KnitGraphIndex.get_options.
2377
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2378
    def catch_add(self, entries):
2379
        self.caught_entries.append(entries)
2380
2381
    def test_add_no_callback_errors(self):
2382
        index = self.two_graph_index()
2383
        self.assertRaises(errors.ReadOnlyError, index.add_version,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2384
            '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.
2385
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
2386
    def test_add_version_smoke(self):
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2387
        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
2388
        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.
2389
        self.assertEqual([[(('new', ), 'N50 60', ((('separate',),),))]],
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2390
            self.caught_entries)
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
2391
2392
    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.
2393
        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.
2394
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2395
            '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.
2396
        self.assertEqual([], self.caught_entries)
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
2397
2398
    def test_add_version_same_dup(self):
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2399
        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.
2400
        # options can be spelt two different ways
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2401
        index.add_version('tip', 'fulltext,no-eol', (None, 0, 100), ['parent'])
2402
        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.
2403
        # but neither should have added data.
2404
        self.assertEqual([[], []], self.caught_entries)
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
2405
        
2406
    def test_add_version_different_dup(self):
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2407
        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.
2408
        # change options
2409
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2410
            'tip', 'no-eol,line-delta', (None, 0, 100), ['parent'])
2411
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2412
            'tip', 'line-delta,no-eol', (None, 0, 100), ['parent'])
2413
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2414
            'tip', 'fulltext', (None, 0, 100), ['parent'])
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
2415
        # position/length
2416
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2417
            '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.
2418
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2419
            '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.
2420
        # parents
2421
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2422
            'tip', 'fulltext,no-eol', (None, 0, 100), [])
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2423
        self.assertEqual([], self.caught_entries)
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
2424
        
2425
    def test_add_versions_nodeltas(self):
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2426
        index = self.two_graph_index(catch_adds=True)
2427
        index.add_versions([
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2428
                ('new', 'fulltext,no-eol', (None, 50, 60), ['separate']),
2429
                ('new2', 'fulltext', (None, 0, 6), ['new']),
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2430
                ])
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2431
        self.assertEqual([(('new', ), 'N50 60', ((('separate',),),)),
2432
            (('new2', ), ' 0 6', ((('new',),),))],
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2433
            sorted(self.caught_entries[0]))
2434
        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.
2435
2436
    def test_add_versions_deltas(self):
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2437
        index = self.two_graph_index(deltas=True, catch_adds=True)
2438
        index.add_versions([
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2439
                ('new', 'fulltext,no-eol', (None, 50, 60), ['separate']),
2440
                ('new2', 'line-delta', (None, 0, 6), ['new']),
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2441
                ])
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2442
        self.assertEqual([(('new', ), 'N50 60', ((('separate',),), ())),
2443
            (('new2', ), ' 0 6', ((('new',),), (('new',),), ))],
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2444
            sorted(self.caught_entries[0]))
2445
        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.
2446
2447
    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.
2448
        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.
2449
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2450
            [('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.
2451
        self.assertEqual([], self.caught_entries)
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
2452
2841.2.1 by Robert Collins
* Commit no longer checks for new text keys during insertion when the
2453
    def test_add_versions_random_id_accepted(self):
2454
        index = self.two_graph_index(catch_adds=True)
2455
        index.add_versions([], random_id=True)
2456
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
2457
    def test_add_versions_same_dup(self):
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2458
        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.
2459
        # options can be spelt two different ways
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2460
        index.add_versions([('tip', 'fulltext,no-eol', (None, 0, 100), ['parent'])])
2461
        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.
2462
        # but neither should have added data.
2463
        self.assertEqual([[], []], self.caught_entries)
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
2464
        
2465
    def test_add_versions_different_dup(self):
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2466
        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.
2467
        # change options
2468
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2469
            [('tip', 'no-eol,line-delta', (None, 0, 100), ['parent'])])
2470
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2471
            [('tip', 'line-delta,no-eol', (None, 0, 100), ['parent'])])
2472
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2473
            [('tip', 'fulltext', (None, 0, 100), ['parent'])])
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
2474
        # position/length
2475
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2476
            [('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.
2477
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2478
            [('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.
2479
        # parents
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
            [('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.
2482
        # change options in the second record
2483
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2484
            [('tip', 'fulltext,no-eol', (None, 0, 100), ['parent']),
2485
             ('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.
2486
        self.assertEqual([], self.caught_entries)
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2487
2592.3.43 by Robert Collins
A knit iter_parents API.
2488
    def test_iter_parents(self):
2489
        index1 = self.make_g_index('1', 1, [
2490
        # no parents
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2491
            (('r0', ), 'N0 100', ([], )),
2592.3.43 by Robert Collins
A knit iter_parents API.
2492
        # 1 parent
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2493
            (('r1', ), '', ([('r0', )], ))])
2592.3.43 by Robert Collins
A knit iter_parents API.
2494
        index2 = self.make_g_index('2', 1, [
2495
        # 2 parents
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2496
            (('r2', ), 'N0 100', ([('r1', ), ('r0', )], )),
2592.3.43 by Robert Collins
A knit iter_parents API.
2497
            ])
2498
        combined_index = CombinedGraphIndex([index1, index2])
2499
        index = KnitGraphIndex(combined_index)
2500
        # XXX TODO a ghost
2501
        # cases: each sample data individually:
2502
        self.assertEqual(set([('r0', ())]),
2503
            set(index.iter_parents(['r0'])))
2504
        self.assertEqual(set([('r1', ('r0', ))]),
2505
            set(index.iter_parents(['r1'])))
2506
        self.assertEqual(set([('r2', ('r1', 'r0'))]),
2507
            set(index.iter_parents(['r2'])))
2508
        # no nodes returned for a missing node
2509
        self.assertEqual(set(),
2510
            set(index.iter_parents(['missing'])))
2511
        # 1 node returned with missing nodes skipped
2512
        self.assertEqual(set([('r1', ('r0', ))]),
2513
            set(index.iter_parents(['ghost1', 'r1', 'ghost'])))
2514
        # 2 nodes returned
2515
        self.assertEqual(set([('r0', ()), ('r1', ('r0', ))]),
2516
            set(index.iter_parents(['r0', 'r1'])))
2517
        # 2 nodes returned, missing skipped
2518
        self.assertEqual(set([('r0', ()), ('r1', ('r0', ))]),
2519
            set(index.iter_parents(['a', 'r0', 'b', 'r1', 'c'])))
2520
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2521
2522
class TestNoParentsGraphIndexKnit(KnitTests):
2523
    """Tests for knits using KnitGraphIndex with no parents."""
2524
2525
    def make_g_index(self, name, ref_lists=0, nodes=[]):
2526
        builder = GraphIndexBuilder(ref_lists)
2527
        for node, references in nodes:
2528
            builder.add_node(node, references)
2529
        stream = builder.finish()
2530
        trans = self.get_transport()
2890.2.1 by Robert Collins
* ``bzrlib.index.GraphIndex`` now requires a size parameter to the
2531
        size = trans.put_file(name, stream)
2532
        return GraphIndex(trans, name, size)
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2533
2534
    def test_parents_deltas_incompatible(self):
2535
        index = CombinedGraphIndex([])
2536
        self.assertRaises(errors.KnitError, KnitGraphIndex, index,
2537
            deltas=True, parents=False)
2538
2539
    def two_graph_index(self, catch_adds=False):
2540
        """Build a two-graph index.
2541
2542
        :param deltas: If true, use underlying indices with two node-ref
2543
            lists and 'parent' set to a delta-compressed against tail.
2544
        """
2545
        # put several versions in the index.
2546
        index1 = self.make_g_index('1', 0, [
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2547
            (('tip', ), 'N0 100'),
2548
            (('tail', ), '')])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2549
        index2 = self.make_g_index('2', 0, [
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2550
            (('parent', ), ' 100 78'),
2551
            (('separate', ), '')])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2552
        combined_index = CombinedGraphIndex([index1, index2])
2553
        if catch_adds:
2554
            self.combined_index = combined_index
2555
            self.caught_entries = []
2556
            add_callback = self.catch_add
2557
        else:
2558
            add_callback = None
2559
        return KnitGraphIndex(combined_index, parents=False,
2560
            add_callback=add_callback)
2561
2562
    def test_get_ancestry(self):
2563
        # with no parents, ancestry is always just the key.
2564
        index = self.two_graph_index()
2565
        self.assertEqual([], index.get_ancestry([]))
2566
        self.assertEqual(['separate'], index.get_ancestry(['separate']))
2567
        self.assertEqual(['tail'], index.get_ancestry(['tail']))
2568
        self.assertEqual(['parent'], index.get_ancestry(['parent']))
2569
        self.assertEqual(['tip'], index.get_ancestry(['tip']))
2570
        self.assertTrue(index.get_ancestry(['tip', 'separate']) in
2571
            (['tip', 'separate'],
2572
             ['separate', 'tip'],
2573
            ))
2574
        # asking for a ghost makes it go boom.
2575
        self.assertRaises(errors.RevisionNotPresent, index.get_ancestry, ['ghost'])
2576
2577
    def test_get_ancestry_with_ghosts(self):
2578
        index = self.two_graph_index()
2579
        self.assertEqual([], index.get_ancestry_with_ghosts([]))
2580
        self.assertEqual(['separate'], index.get_ancestry_with_ghosts(['separate']))
2581
        self.assertEqual(['tail'], index.get_ancestry_with_ghosts(['tail']))
2582
        self.assertEqual(['parent'], index.get_ancestry_with_ghosts(['parent']))
2583
        self.assertEqual(['tip'], index.get_ancestry_with_ghosts(['tip']))
2584
        self.assertTrue(index.get_ancestry_with_ghosts(['tip', 'separate']) in
2585
            (['tip', 'separate'],
2586
             ['separate', 'tip'],
2587
            ))
2588
        # asking for a ghost makes it go boom.
2589
        self.assertRaises(errors.RevisionNotPresent, index.get_ancestry_with_ghosts, ['ghost'])
2590
2591
    def test_num_versions(self):
2592
        index = self.two_graph_index()
2593
        self.assertEqual(4, index.num_versions())
2594
2595
    def test_get_versions(self):
2596
        index = self.two_graph_index()
2597
        self.assertEqual(set(['tail', 'tip', 'parent', 'separate']),
2598
            set(index.get_versions()))
2599
2600
    def test_has_version(self):
2601
        index = self.two_graph_index()
2602
        self.assertTrue(index.has_version('tail'))
2603
        self.assertFalse(index.has_version('ghost'))
2604
2605
    def test_get_position(self):
2606
        index = self.two_graph_index()
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
2607
        self.assertEqual((index._graph_index._indices[0], 0, 100), index.get_position('tip'))
2608
        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.
2609
2610
    def test_get_method(self):
2611
        index = self.two_graph_index()
2612
        self.assertEqual('fulltext', index.get_method('tip'))
2658.2.1 by Robert Collins
Fix mismatch between KnitGraphIndex and KnitIndex in get_options.
2613
        self.assertEqual(['fulltext'], index.get_options('parent'))
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2614
2615
    def test_get_options(self):
2616
        index = self.two_graph_index()
2658.2.1 by Robert Collins
Fix mismatch between KnitGraphIndex and KnitIndex in get_options.
2617
        self.assertEqual(['fulltext', 'no-eol'], index.get_options('tip'))
2618
        self.assertEqual(['fulltext'], index.get_options('parent'))
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2619
2620
    def test_get_parents_with_ghosts(self):
2621
        index = self.two_graph_index()
2622
        self.assertEqual((), index.get_parents_with_ghosts('parent'))
2592.3.43 by Robert Collins
A knit iter_parents API.
2623
        # and errors on ghosts.
2624
        self.assertRaises(errors.RevisionNotPresent,
2625
            index.get_parents_with_ghosts, 'ghost')
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2626
2627
    def test_check_versions_present(self):
2628
        index = self.two_graph_index()
2629
        self.assertRaises(RevisionNotPresent, index.check_versions_present,
2630
            ['missing'])
2631
        self.assertRaises(RevisionNotPresent, index.check_versions_present,
2632
            ['tail', 'missing'])
2633
        index.check_versions_present(['tail', 'separate'])
2634
2635
    def catch_add(self, entries):
2636
        self.caught_entries.append(entries)
2637
2638
    def test_add_no_callback_errors(self):
2639
        index = self.two_graph_index()
2640
        self.assertRaises(errors.ReadOnlyError, index.add_version,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2641
            'new', 'fulltext,no-eol', (None, 50, 60), ['separate'])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2642
2643
    def test_add_version_smoke(self):
2644
        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
2645
        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.
2646
        self.assertEqual([[(('new', ), 'N50 60')]],
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2647
            self.caught_entries)
2648
2649
    def test_add_version_delta_not_delta_index(self):
2650
        index = self.two_graph_index(catch_adds=True)
2651
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2652
            'new', 'no-eol,line-delta', (None, 0, 100), [])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2653
        self.assertEqual([], self.caught_entries)
2654
2655
    def test_add_version_same_dup(self):
2656
        index = self.two_graph_index(catch_adds=True)
2657
        # options can be spelt two different ways
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2658
        index.add_version('tip', 'fulltext,no-eol', (None, 0, 100), [])
2659
        index.add_version('tip', 'no-eol,fulltext', (None, 0, 100), [])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2660
        # but neither should have added data.
2661
        self.assertEqual([[], []], self.caught_entries)
2662
        
2663
    def test_add_version_different_dup(self):
2664
        index = self.two_graph_index(catch_adds=True)
2665
        # change options
2666
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2667
            'tip', 'no-eol,line-delta', (None, 0, 100), [])
2668
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2669
            'tip', 'line-delta,no-eol', (None, 0, 100), [])
2670
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2671
            'tip', 'fulltext', (None, 0, 100), [])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2672
        # position/length
2673
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2674
            'tip', 'fulltext,no-eol', (None, 50, 100), [])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2675
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2676
            'tip', 'fulltext,no-eol', (None, 0, 1000), [])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2677
        # parents
2678
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2679
            'tip', 'fulltext,no-eol', (None, 0, 100), ['parent'])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2680
        self.assertEqual([], self.caught_entries)
2681
        
2682
    def test_add_versions(self):
2683
        index = self.two_graph_index(catch_adds=True)
2684
        index.add_versions([
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2685
                ('new', 'fulltext,no-eol', (None, 50, 60), []),
2686
                ('new2', 'fulltext', (None, 0, 6), []),
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2687
                ])
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2688
        self.assertEqual([(('new', ), 'N50 60'), (('new2', ), ' 0 6')],
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2689
            sorted(self.caught_entries[0]))
2690
        self.assertEqual(1, len(self.caught_entries))
2691
2692
    def test_add_versions_delta_not_delta_index(self):
2693
        index = self.two_graph_index(catch_adds=True)
2694
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2695
            [('new', 'no-eol,line-delta', (None, 0, 100), ['parent'])])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2696
        self.assertEqual([], self.caught_entries)
2697
2698
    def test_add_versions_parents_not_parents_index(self):
2699
        index = self.two_graph_index(catch_adds=True)
2700
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2701
            [('new', 'no-eol,fulltext', (None, 0, 100), ['parent'])])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2702
        self.assertEqual([], self.caught_entries)
2703
2841.2.1 by Robert Collins
* Commit no longer checks for new text keys during insertion when the
2704
    def test_add_versions_random_id_accepted(self):
2705
        index = self.two_graph_index(catch_adds=True)
2706
        index.add_versions([], random_id=True)
2707
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2708
    def test_add_versions_same_dup(self):
2709
        index = self.two_graph_index(catch_adds=True)
2710
        # options can be spelt two different ways
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2711
        index.add_versions([('tip', 'fulltext,no-eol', (None, 0, 100), [])])
2712
        index.add_versions([('tip', 'no-eol,fulltext', (None, 0, 100), [])])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2713
        # but neither should have added data.
2714
        self.assertEqual([[], []], self.caught_entries)
2715
        
2716
    def test_add_versions_different_dup(self):
2717
        index = self.two_graph_index(catch_adds=True)
2718
        # change options
2719
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2720
            [('tip', 'no-eol,line-delta', (None, 0, 100), [])])
2721
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2722
            [('tip', 'line-delta,no-eol', (None, 0, 100), [])])
2723
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2724
            [('tip', 'fulltext', (None, 0, 100), [])])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2725
        # position/length
2726
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2727
            [('tip', 'fulltext,no-eol', (None, 50, 100), [])])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2728
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2729
            [('tip', 'fulltext,no-eol', (None, 0, 1000), [])])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2730
        # parents
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
            [('tip', 'fulltext,no-eol', (None, 0, 100), ['parent'])])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2733
        # change options in the second record
2734
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2735
            [('tip', 'fulltext,no-eol', (None, 0, 100), []),
2736
             ('tip', 'no-eol,line-delta', (None, 0, 100), [])])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2737
        self.assertEqual([], self.caught_entries)
2738
2592.3.43 by Robert Collins
A knit iter_parents API.
2739
    def test_iter_parents(self):
2740
        index = self.two_graph_index()
2741
        self.assertEqual(set([
2742
            ('tip', ()), ('tail', ()), ('parent', ()), ('separate', ())
2743
            ]),
2744
            set(index.iter_parents(['tip', 'tail', 'ghost', 'parent', 'separate'])))
2745
        self.assertEqual(set([('tip', ())]),
2746
            set(index.iter_parents(['tip'])))
2747
        self.assertEqual(set(),
2748
            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.
2749
2750
2751
class TestPackKnits(KnitTests):
2752
    """Tests that use a _PackAccess and KnitGraphIndex."""
2753
2754
    def test_get_data_stream_packs_ignores_pack_overhead(self):
2755
        # Packs have an encoding overhead that should not be included in the
2756
        # 'size' field of a data stream, because it is not returned by the
2757
        # raw_reading functions - it is why index_memo's are opaque, and
2758
        # get_data_stream was abusing this.
2759
        packname = 'test.pack'
2760
        transport = self.get_transport()
2761
        def write_data(bytes):
2762
            transport.append_bytes(packname, bytes)
2763
        writer = pack.ContainerWriter(write_data)
2764
        writer.begin()
2765
        index = InMemoryGraphIndex(2)
2766
        knit_index = KnitGraphIndex(index, add_callback=index.add_nodes,
2767
            deltas=True)
2768
        indices = {index:(transport, packname)}
2769
        access = _PackAccess(indices, writer=(writer, index))
2770
        k = KnitVersionedFile('test', get_transport('.'),
2771
            delta=True, create=True, index=knit_index, access_method=access)
2772
        # insert something into the knit
2773
        k.add_lines('text-1', [], ["foo\n"])
2774
        # get a data stream for it
2775
        stream = k.get_data_stream(['text-1'])
2776
        # if the stream has been incorrectly assembled, we will get a short read
2777
        # reading from the stream (as streams have no trailer)
2778
        expected_length = stream[1][0][2]
2779
        # we use -1 to do the read, so that if a trailer is added this test
2780
        # will fail and we'll adjust it to handle that case correctly, rather
2781
        # than allowing an over-read that is bogus.
2782
        self.assertEqual(expected_length, len(stream[2](-1)))
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2783
2784
2785
class Test_StreamIndex(KnitTests):
2786
2787
    def get_index(self, knit, stream):
2788
        """Get a _StreamIndex from knit and stream."""
2789
        return knit._knit_from_datastream(stream)._index
2790
2791
    def assertIndexVersions(self, knit, versions):
2792
        """Check that the _StreamIndex versions are those of the stream."""
2793
        index = self.get_index(knit, knit.get_data_stream(versions))
2794
        self.assertEqual(set(index.get_versions()), set(versions))
2795
        # check we didn't get duplicates
2796
        self.assertEqual(len(index.get_versions()), len(versions))
2797
2798
    def assertIndexAncestry(self, knit, ancestry_versions, versions, result):
2799
        """Check the result of a get_ancestry call on knit."""
2800
        index = self.get_index(knit, knit.get_data_stream(versions))
3052.2.4 by Andrew Bennetts
Some tweaks suggested by John's review.
2801
        self.assertEqual(
2802
            set(result),
2803
            set(index.get_ancestry(ancestry_versions, False)))
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2804
3052.2.3 by Robert Collins
Handle insert_data_stream of an unannotated stream into an annotated knit.
2805
    def assertIterParents(self, knit, versions, parent_versions, result):
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2806
        """Check the result of an iter_parents call on knit."""
2807
        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.
2808
        self.assertEqual(result, index.iter_parents(parent_versions))
2809
2810
    def assertGetMethod(self, knit, versions, version, result):
2811
        index = self.get_index(knit, knit.get_data_stream(versions))
2812
        self.assertEqual(result, index.get_method(version))
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2813
2814
    def assertGetOptions(self, knit, version, options):
2815
        index = self.get_index(knit, knit.get_data_stream(version))
2816
        self.assertEqual(options, index.get_options(version))
2817
2818
    def assertGetPosition(self, knit, versions, version, result):
2819
        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.
2820
        if result[1] is None:
2821
            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.
2822
        self.assertEqual(result, index.get_position(version))
2823
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2824
    def assertGetParentsWithGhosts(self, knit, versions, version, parents):
2825
        index = self.get_index(knit, knit.get_data_stream(versions))
2826
        self.assertEqual(parents, index.get_parents_with_ghosts(version))
2827
2828
    def make_knit_with_4_versions_2_dags(self):
2829
        knit = self.make_test_knit()
2830
        knit.add_lines('a', [], ["foo"])
2831
        knit.add_lines('b', [], [])
2832
        knit.add_lines('c', ['b', 'a'], [])
2833
        knit.add_lines_with_ghosts('d', ['e', 'f'], [])
2834
        return knit
2835
2836
    def test_versions(self):
2837
        """The versions of a StreamIndex are those of the datastream."""
2838
        knit = self.make_knit_with_4_versions_2_dags()
2839
        # ask for most permutations, which catches bugs like falling back to the
2840
        # target knit, or showing ghosts, etc.
2841
        self.assertIndexVersions(knit, [])
2842
        self.assertIndexVersions(knit, ['a'])
2843
        self.assertIndexVersions(knit, ['b'])
2844
        self.assertIndexVersions(knit, ['c'])
2845
        self.assertIndexVersions(knit, ['d'])
2846
        self.assertIndexVersions(knit, ['a', 'b'])
2847
        self.assertIndexVersions(knit, ['b', 'c'])
2848
        self.assertIndexVersions(knit, ['a', 'c'])
2849
        self.assertIndexVersions(knit, ['a', 'b', 'c'])
2850
        self.assertIndexVersions(knit, ['a', 'b', 'c', 'd'])
2851
2852
    def test_construct(self):
2853
        """Constructing a StreamIndex generates index data."""
2854
        data_list = [('text-a', ['fulltext'], 127, []),
2855
            ('text-b', ['option'], 128, ['text-c'])]
3224.1.8 by John Arbash Meinel
Add noeol to the return signature of get_build_details.
2856
        index = _StreamIndex(data_list, None)
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2857
        self.assertEqual({'text-a':(['fulltext'], (0, 127), []),
2858
            'text-b':(['option'], (127, 127 + 128), ['text-c'])},
2859
            index._by_version)
2860
2861
    def test_get_ancestry(self):
2862
        knit = self.make_knit_with_4_versions_2_dags()
2863
        self.assertIndexAncestry(knit, ['a'], ['a'], ['a'])
2864
        self.assertIndexAncestry(knit, ['b'], ['b'], ['b'])
2865
        self.assertIndexAncestry(knit, ['c'], ['c'], ['c'])
2866
        self.assertIndexAncestry(knit, ['c'], ['a', 'b', 'c'],
3052.2.4 by Andrew Bennetts
Some tweaks suggested by John's review.
2867
            set(['a', 'b', 'c']))
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2868
        self.assertIndexAncestry(knit, ['c', 'd'], ['a', 'b', 'c', 'd'],
3052.2.4 by Andrew Bennetts
Some tweaks suggested by John's review.
2869
            set(['a', 'b', 'c', 'd']))
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2870
3052.2.3 by Robert Collins
Handle insert_data_stream of an unannotated stream into an annotated knit.
2871
    def test_get_method(self):
2872
        knit = self.make_knit_with_4_versions_2_dags()
2873
        self.assertGetMethod(knit, ['a'], 'a', 'fulltext')
2874
        self.assertGetMethod(knit, ['c'], 'c', 'line-delta')
2875
        # get_method on a basis that is not in the datastream (but in the
2876
        # backing knit) returns 'fulltext', because thats what we'll create as
2877
        # we thunk across.
2878
        self.assertGetMethod(knit, ['c'], 'b', 'fulltext')
2879
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2880
    def test_iter_parents(self):
2881
        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.
2882
        self.assertIterParents(knit, ['a'], ['a'], [('a', ())])
3052.2.3 by Robert Collins
Handle insert_data_stream of an unannotated stream into an annotated knit.
2883
        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.
2884
            [('a', ()), ('b', ())])
3052.2.3 by Robert Collins
Handle insert_data_stream of an unannotated stream into an annotated knit.
2885
        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.
2886
            [('a', ()), ('b', ()), ('c', ('b', 'a'))])
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2887
        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.
2888
            ['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.
2889
            [('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.
2890
        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.
2891
            [('c', ('b', 'a'))])
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2892
2893
    def test_get_options(self):
2894
        knit = self.make_knit_with_4_versions_2_dags()
2895
        self.assertGetOptions(knit, 'a', ['no-eol', 'fulltext'])
2896
        self.assertGetOptions(knit, 'c', ['line-delta'])
2897
2898
    def test_get_parents_with_ghosts(self):
2899
        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.
2900
        self.assertGetParentsWithGhosts(knit, ['a'], 'a', ())
2901
        self.assertGetParentsWithGhosts(knit, ['c'], 'c', ('b', 'a'))
2902
        self.assertGetParentsWithGhosts(knit, ['d'], 'd', ('e', 'f'))
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2903
2904
    def test_get_position(self):
2905
        knit = self.make_knit_with_4_versions_2_dags()
3052.2.6 by Andrew Bennetts
Fix typo in comment.
2906
        # 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
2907
        # _StreamAccess to use.
3052.2.5 by Andrew Bennetts
Address the rest of the review comments from John and myself.
2908
        self.assertGetPosition(knit, ['a'], 'a', (False, None, 0, 78))
2909
        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.
2910
        # 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.
2911
        # 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.
2912
        # 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.
2913
        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.
2914
2915
2916
class Test_StreamAccess(KnitTests):
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2917
2918
    def get_index_access(self, knit, stream):
2919
        """Get a _StreamAccess from knit and stream."""
2920
        knit =  knit._knit_from_datastream(stream)
2921
        return knit._index, knit._data._access
2922
2923
    def assertGetRawRecords(self, knit, versions):
2924
        index, access = self.get_index_access(knit,
2925
            knit.get_data_stream(versions))
2926
        # check that every version asked for can be obtained from the resulting
2927
        # access object.
2928
        # batch
2929
        memos = []
2930
        for version in versions:
2931
            memos.append(knit._index.get_position(version))
2932
        original = {}
2933
        for version, data in zip(
2934
            versions, knit._data._access.get_raw_records(memos)):
2935
            original[version] = data
2936
        memos = []
2937
        for version in versions:
2938
            memos.append(index.get_position(version))
2939
        streamed = {}
2940
        for version, data in zip(versions, access.get_raw_records(memos)):
2941
            streamed[version] = data
2942
        self.assertEqual(original, streamed)
2943
        # individually
2944
        for version in versions:
2945
            data = list(access.get_raw_records(
2946
                [index.get_position(version)]))[0]
2947
            self.assertEqual(original[version], data)
2948
2949
    def make_knit_with_two_versions(self):
2950
        knit = self.make_test_knit()
2951
        knit.add_lines('a', [], ["foo"])
2952
        knit.add_lines('b', [], ["bar"])
2953
        return knit
2954
2955
    def test_get_raw_records(self):
2956
        knit = self.make_knit_with_two_versions()
2957
        self.assertGetRawRecords(knit, ['a', 'b'])
2958
        self.assertGetRawRecords(knit, ['a'])
2959
        self.assertGetRawRecords(knit, ['b'])
3052.2.3 by Robert Collins
Handle insert_data_stream of an unannotated stream into an annotated knit.
2960
    
2961
    def test_get_raw_record_from_backing_knit(self):
2962
        # the thunk layer should create an artificial A on-demand when needed.
2963
        source_knit = self.make_test_knit(name='plain', annotate=False)
2964
        target_knit = self.make_test_knit(name='annotated', annotate=True)
2965
        source_knit.add_lines("A", [], ["Foo\n"])
2966
        # Give the target A, so we can try to thunk across to it.
2967
        target_knit.join(source_knit)
2968
        index, access = self.get_index_access(target_knit,
2969
            source_knit.get_data_stream([]))
3052.2.5 by Andrew Bennetts
Address the rest of the review comments from John and myself.
2970
        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.
2971
        df = GzipFile(mode='rb', fileobj=StringIO(raw_data))
2972
        self.assertEqual(
2973
            'version A 1 5d36b88bb697a2d778f024048bafabd443d74503\n'
2974
            'Foo\nend A\n',
2975
            df.read())
2976
2977
    def test_asking_for_thunk_stream_is_not_plain_errors(self):
2978
        knit = self.make_test_knit(name='annotated', annotate=True)
2979
        knit.add_lines("A", [], ["Foo\n"])
2980
        index, access = self.get_index_access(knit,
2981
            knit.get_data_stream([]))
2982
        self.assertRaises(errors.KnitCorrupt,
3052.2.5 by Andrew Bennetts
Address the rest of the review comments from John and myself.
2983
            list, access.get_raw_records([(True, "A", None, None)]))