/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
1563.2.13 by Robert Collins
InterVersionedFile implemented.
1436
    def test_create_empty_annotated(self):
1563.2.16 by Robert Collins
Change WeaveStore into VersionedFileStore and make its versoined file class parameterisable.
1437
        k1 = self.make_test_knit(True)
1563.2.13 by Robert Collins
InterVersionedFile implemented.
1438
        # 0
1439
        k1.add_lines('text-a', [], ['a\n', 'b\n'])
1440
        k2 = k1.create_empty('t', MemoryTransport())
1441
        self.assertTrue(isinstance(k2.factory, KnitAnnotateFactory))
1442
        self.assertEqual(k1.delta, k2.delta)
1443
        # the generic test checks for empty content and file class
1444
1641.1.2 by Robert Collins
Change knit index files to be robust in the presence of partial writes.
1445
    def test_knit_format(self):
1446
        # this tests that a new knit index file has the expected content
1447
        # and that is writes the data we expect as records are added.
1448
        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
1449
        # Now knit files are not created until we first add data to them
1666.1.6 by Robert Collins
Make knit the default format.
1450
        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.
1451
        knit.add_lines_with_ghosts('revid', ['a_ghost'], ['a\n'])
1452
        self.assertFileEqual(
1666.1.6 by Robert Collins
Make knit the default format.
1453
            "# bzr knit index 8\n"
1641.1.2 by Robert Collins
Change knit index files to be robust in the presence of partial writes.
1454
            "\n"
1455
            "revid fulltext 0 84 .a_ghost :",
1456
            'test.kndx')
1457
        knit.add_lines_with_ghosts('revid2', ['revid'], ['a\n'])
1458
        self.assertFileEqual(
1666.1.6 by Robert Collins
Make knit the default format.
1459
            "# bzr knit index 8\n"
1641.1.2 by Robert Collins
Change knit index files to be robust in the presence of partial writes.
1460
            "\nrevid fulltext 0 84 .a_ghost :"
1461
            "\nrevid2 line-delta 84 82 0 :",
1462
            'test.kndx')
1463
        # 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.
1464
        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.
1465
        self.assertEqual(['revid', 'revid2'], knit.versions())
1466
        # 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
1467
        indexfile = file('test.kndx', 'ab')
1641.1.2 by Robert Collins
Change knit index files to be robust in the presence of partial writes.
1468
        indexfile.write('\nrevid3 line-delta 166 82 1 2 3 4 5 .phwoar:demo ')
1469
        indexfile.close()
1470
        # 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.
1471
        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.
1472
        self.assertEqual(['revid', 'revid2'], knit.versions())
1473
        # and add a revision with the same id the failed write had
1474
        knit.add_lines('revid3', ['revid2'], ['a\n'])
1475
        # and when reading it revid3 should now appear.
1685.1.39 by John Arbash Meinel
Updating test_knit to not instantiate a LocalTransport directly.
1476
        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.
1477
        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.
1478
        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.
1479
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
1480
    def test_delay_create(self):
1481
        """Test that passing delay_create=True creates files late"""
1482
        knit = self.make_test_knit(annotate=True, delay_create=True)
1483
        self.failIfExists('test.knit')
1484
        self.failIfExists('test.kndx')
1485
        knit.add_lines_with_ghosts('revid', ['a_ghost'], ['a\n'])
1486
        self.failUnlessExists('test.knit')
1487
        self.assertFileEqual(
1488
            "# bzr knit index 8\n"
1489
            "\n"
1490
            "revid fulltext 0 84 .a_ghost :",
1491
            'test.kndx')
1492
1946.2.2 by John Arbash Meinel
test delay_create does the right thing
1493
    def test_create_parent_dir(self):
1494
        """create_parent_dir can create knits in nonexistant dirs"""
1495
        # Has no effect if we don't set 'delay_create'
1496
        trans = get_transport('.')
1497
        self.assertRaises(NoSuchFile, KnitVersionedFile, 'dir/test',
1498
                          trans, access_mode='w', factory=None,
1499
                          create=True, create_parent_dir=True)
1500
        # Nothing should have changed yet
1501
        knit = KnitVersionedFile('dir/test', trans, access_mode='w',
1502
                                 factory=None, create=True,
1503
                                 create_parent_dir=True,
1504
                                 delay_create=True)
1505
        self.failIfExists('dir/test.knit')
1506
        self.failIfExists('dir/test.kndx')
1507
        self.failIfExists('dir')
1508
        knit.add_lines('revid', [], ['a\n'])
1509
        self.failUnlessExists('dir')
1510
        self.failUnlessExists('dir/test.knit')
1511
        self.assertFileEqual(
1512
            "# bzr knit index 8\n"
1513
            "\n"
1514
            "revid fulltext 0 84  :",
1515
            'dir/test.kndx')
1516
1946.2.13 by John Arbash Meinel
Test that passing modes does the right thing for knits.
1517
    def test_create_mode_700(self):
1518
        trans = get_transport('.')
1519
        if not trans._can_roundtrip_unix_modebits():
1520
            # Can't roundtrip, so no need to run this test
1521
            return
1522
        knit = KnitVersionedFile('dir/test', trans, access_mode='w',
1523
                                 factory=None, create=True,
1524
                                 create_parent_dir=True,
1525
                                 delay_create=True,
1526
                                 file_mode=0600,
1527
                                 dir_mode=0700)
1528
        knit.add_lines('revid', [], ['a\n'])
1529
        self.assertTransportMode(trans, 'dir', 0700)
1530
        self.assertTransportMode(trans, 'dir/test.knit', 0600)
1531
        self.assertTransportMode(trans, 'dir/test.kndx', 0600)
1532
1533
    def test_create_mode_770(self):
1534
        trans = get_transport('.')
1535
        if not trans._can_roundtrip_unix_modebits():
1536
            # Can't roundtrip, so no need to run this test
1537
            return
1538
        knit = KnitVersionedFile('dir/test', trans, access_mode='w',
1539
                                 factory=None, create=True,
1540
                                 create_parent_dir=True,
1541
                                 delay_create=True,
1542
                                 file_mode=0660,
1543
                                 dir_mode=0770)
1544
        knit.add_lines('revid', [], ['a\n'])
1545
        self.assertTransportMode(trans, 'dir', 0770)
1546
        self.assertTransportMode(trans, 'dir/test.knit', 0660)
1547
        self.assertTransportMode(trans, 'dir/test.kndx', 0660)
1548
1549
    def test_create_mode_777(self):
1550
        trans = get_transport('.')
1551
        if not trans._can_roundtrip_unix_modebits():
1552
            # Can't roundtrip, so no need to run this test
1553
            return
1554
        knit = KnitVersionedFile('dir/test', trans, access_mode='w',
1555
                                 factory=None, create=True,
1556
                                 create_parent_dir=True,
1557
                                 delay_create=True,
1558
                                 file_mode=0666,
1559
                                 dir_mode=0777)
1560
        knit.add_lines('revid', [], ['a\n'])
1561
        self.assertTransportMode(trans, 'dir', 0777)
1562
        self.assertTransportMode(trans, 'dir/test.knit', 0666)
1563
        self.assertTransportMode(trans, 'dir/test.kndx', 0666)
1564
1664.2.1 by Aaron Bentley
Start work on plan_merge test
1565
    def test_plan_merge(self):
1566
        my_knit = self.make_test_knit(annotate=True)
1567
        my_knit.add_lines('text1', [], split_lines(TEXT_1))
1568
        my_knit.add_lines('text1a', ['text1'], split_lines(TEXT_1A))
1569
        my_knit.add_lines('text1b', ['text1'], split_lines(TEXT_1B))
1664.2.3 by Aaron Bentley
Add failing test case
1570
        plan = list(my_knit.plan_merge('text1a', 'text1b'))
1664.2.6 by Aaron Bentley
Got plan-merge passing tests
1571
        for plan_line, expected_line in zip(plan, AB_MERGE):
1572
            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.
1573
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1574
    def test_get_stream_empty(self):
1575
        """Get a data stream for an empty knit file."""
1576
        k1 = self.make_test_knit()
1577
        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.
1578
        self.assertEqual('knit-plain', format)
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1579
        self.assertEqual([], data_list)
1580
        content = reader_callable(None)
1581
        self.assertEqual('', content)
1582
        self.assertIsInstance(content, str)
1583
1584
    def test_get_stream_one_version(self):
1585
        """Get a data stream for a single record out of a knit containing just
1586
        one record.
1587
        """
1588
        k1 = self.make_test_knit()
1589
        test_data = [
1590
            ('text-a', [], TEXT_1),
1591
            ]
1592
        expected_data_list = [
1593
            # 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.
1594
            ('text-a', ['fulltext'], 122, ()),
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1595
           ]
1596
        for version_id, parents, lines in test_data:
1597
            k1.add_lines(version_id, parents, split_lines(lines))
1598
1599
        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.
1600
        self.assertEqual('knit-plain', format)
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1601
        self.assertEqual(expected_data_list, data_list)
1602
        # There's only one record in the knit, so the content should be the
1603
        # entire knit data file's contents.
2535.3.36 by Andrew Bennetts
Merge bzr.dev
1604
        self.assertEqual(k1.transport.get_bytes(k1._data._access._filename),
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1605
                         reader_callable(None))
1606
        
1607
    def test_get_stream_get_one_version_of_many(self):
1608
        """Get a data stream for just one version out of a knit containing many
1609
        versions.
1610
        """
1611
        k1 = self.make_test_knit()
1612
        # Insert the same data as test_knit_join, as they seem to cover a range
1613
        # of cases (no parents, one parent, multiple parents).
1614
        test_data = [
1615
            ('text-a', [], TEXT_1),
1616
            ('text-b', ['text-a'], TEXT_1),
1617
            ('text-c', [], TEXT_1),
1618
            ('text-d', ['text-c'], TEXT_1),
1619
            ('text-m', ['text-b', 'text-d'], TEXT_1),
1620
            ]
1621
        expected_data_list = [
1622
            # 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.
1623
            ('text-m', ['line-delta'], 84, ('text-b', 'text-d')),
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1624
            ]
1625
        for version_id, parents, lines in test_data:
1626
            k1.add_lines(version_id, parents, split_lines(lines))
1627
1628
        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.
1629
        self.assertEqual('knit-plain', format)
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1630
        self.assertEqual(expected_data_list, data_list)
1631
        self.assertRecordContentEqual(k1, 'text-m', reader_callable(None))
1632
        
3023.2.2 by Martin Pool
Fix KnitVersionedFile.get_data_stream to not assume .versions() is sorted. (lp:165106)
1633
    def test_get_data_stream_unordered_index(self):
1634
        """Get a data stream when the knit index reports versions out of order.
1635
1636
        https://bugs.launchpad.net/bzr/+bug/164637
1637
        """
1638
        k1 = self.make_test_knit()
1639
        test_data = [
1640
            ('text-a', [], TEXT_1),
1641
            ('text-b', ['text-a'], TEXT_1),
1642
            ('text-c', [], TEXT_1),
1643
            ('text-d', ['text-c'], TEXT_1),
1644
            ('text-m', ['text-b', 'text-d'], TEXT_1),
1645
            ]
1646
        for version_id, parents, lines in test_data:
1647
            k1.add_lines(version_id, parents, split_lines(lines))
1648
        # monkey-patch versions method to return out of order, as if coming
1649
        # from multiple independently indexed packs
1650
        original_versions = k1.versions
1651
        k1.versions = lambda: reversed(original_versions())
1652
        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.
1653
            ('text-a', ['fulltext'], 122, ()),
1654
            ('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)
1655
        # now check the fulltext is first and the delta second
1656
        format, data_list, _ = k1.get_data_stream(['text-a', 'text-b'])
1657
        self.assertEqual('knit-plain', format)
1658
        self.assertEqual(expected_data_list, data_list)
1659
        # and that's true if we ask for them in the opposite order too
1660
        format, data_list, _ = k1.get_data_stream(['text-b', 'text-a'])
1661
        self.assertEqual(expected_data_list, data_list)
1662
        # also try requesting more versions
1663
        format, data_list, _ = k1.get_data_stream([
1664
            'text-m', 'text-b', 'text-a'])
1665
        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.
1666
            ('text-a', ['fulltext'], 122, ()),
1667
            ('text-b', ['line-delta'], 84, ('text-a',)),
1668
            ('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)
1669
            ], data_list)
1670
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1671
    def test_get_stream_ghost_parent(self):
1672
        """Get a data stream for a version with a ghost parent."""
1673
        k1 = self.make_test_knit()
1674
        # Test data
1675
        k1.add_lines('text-a', [], split_lines(TEXT_1))
1676
        k1.add_lines_with_ghosts('text-b', ['text-a', 'text-ghost'],
1677
                                 split_lines(TEXT_1))
1678
        # Expected data
1679
        expected_data_list = [
1680
            # 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.
1681
            ('text-b', ['line-delta'], 84, ('text-a', 'text-ghost')),
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1682
            ]
1683
        
1684
        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.
1685
        self.assertEqual('knit-plain', format)
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1686
        self.assertEqual(expected_data_list, data_list)
1687
        self.assertRecordContentEqual(k1, 'text-b', reader_callable(None))
1688
    
1689
    def test_get_stream_get_multiple_records(self):
1690
        """Get a stream for multiple records of a knit."""
1691
        k1 = self.make_test_knit()
1692
        # Insert the same data as test_knit_join, as they seem to cover a range
1693
        # of cases (no parents, one parent, multiple parents).
1694
        test_data = [
1695
            ('text-a', [], TEXT_1),
1696
            ('text-b', ['text-a'], TEXT_1),
1697
            ('text-c', [], TEXT_1),
1698
            ('text-d', ['text-c'], TEXT_1),
1699
            ('text-m', ['text-b', 'text-d'], TEXT_1),
1700
            ]
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
1701
        for version_id, parents, lines in test_data:
1702
            k1.add_lines(version_id, parents, split_lines(lines))
1703
1704
        # This test is actually a bit strict as the order in which they're
1705
        # returned is not defined.  This matches the current (deterministic)
1706
        # behaviour.
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1707
        expected_data_list = [
1708
            # 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.
1709
            ('text-d', ['line-delta'], 84, ('text-c',)),
1710
            ('text-b', ['line-delta'], 84, ('text-a',)),
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1711
            ]
1712
        # Note that even though we request the revision IDs in a particular
1713
        # order, the data stream may return them in any order it likes.  In this
1714
        # case, they'll be in the order they were inserted into the knit.
1715
        format, data_list, reader_callable = k1.get_data_stream(
1716
            ['text-d', 'text-b'])
2535.3.17 by Andrew Bennetts
[broken] Closer to a working Repository.fetch_revisions smart request.
1717
        self.assertEqual('knit-plain', format)
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1718
        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
1719
        # must match order they're returned
1720
        self.assertRecordContentEqual(k1, 'text-d', reader_callable(84))
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1721
        self.assertRecordContentEqual(k1, 'text-b', reader_callable(84))
1722
        self.assertEqual('', reader_callable(None),
1723
                         "There should be no more bytes left to read.")
1724
1725
    def test_get_stream_all(self):
1726
        """Get a data stream for all the records in a knit.
1727
1728
        This exercises fulltext records, line-delta records, records with
1729
        various numbers of parents, and reading multiple records out of the
1730
        callable.  These cases ought to all be exercised individually by the
1731
        other test_get_stream_* tests; this test is basically just paranoia.
1732
        """
1733
        k1 = self.make_test_knit()
1734
        # Insert the same data as test_knit_join, as they seem to cover a range
1735
        # of cases (no parents, one parent, multiple parents).
1736
        test_data = [
1737
            ('text-a', [], TEXT_1),
1738
            ('text-b', ['text-a'], TEXT_1),
1739
            ('text-c', [], TEXT_1),
1740
            ('text-d', ['text-c'], TEXT_1),
1741
            ('text-m', ['text-b', 'text-d'], TEXT_1),
1742
           ]
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
1743
        for version_id, parents, lines in test_data:
1744
            k1.add_lines(version_id, parents, split_lines(lines))
1745
1746
        # This test is actually a bit strict as the order in which they're
1747
        # returned is not defined.  This matches the current (deterministic)
1748
        # behaviour.
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1749
        expected_data_list = [
1750
            # 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.
1751
            ('text-a', ['fulltext'], 122, ()),
1752
            ('text-b', ['line-delta'], 84, ('text-a',)),
1753
            ('text-m', ['line-delta'], 84, ('text-b', 'text-d')),
1754
            ('text-c', ['fulltext'], 121, ()),
1755
            ('text-d', ['line-delta'], 84, ('text-c',)),
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1756
            ]
1757
        format, data_list, reader_callable = k1.get_data_stream(
1758
            ['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.
1759
        self.assertEqual('knit-plain', format)
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1760
        self.assertEqual(expected_data_list, data_list)
1761
        for version_id, options, length, parents in expected_data_list:
1762
            bytes = reader_callable(length)
1763
            self.assertRecordContentEqual(k1, version_id, bytes)
1764
2535.3.4 by Andrew Bennetts
Simple implementation of Knit.insert_data_stream.
1765
    def assertKnitFilesEqual(self, knit1, knit2):
1766
        """Assert that the contents of the index and data files of two knits are
1767
        equal.
1768
        """
1769
        self.assertEqual(
2535.3.36 by Andrew Bennetts
Merge bzr.dev
1770
            knit1.transport.get_bytes(knit1._data._access._filename),
1771
            knit2.transport.get_bytes(knit2._data._access._filename))
2535.3.4 by Andrew Bennetts
Simple implementation of Knit.insert_data_stream.
1772
        self.assertEqual(
1773
            knit1.transport.get_bytes(knit1._index._filename),
1774
            knit2.transport.get_bytes(knit2._index._filename))
1775
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
1776
    def assertKnitValuesEqual(self, left, right):
3052.2.5 by Andrew Bennetts
Address the rest of the review comments from John and myself.
1777
        """Assert that the texts, annotations and graph of left and right are
1778
        the same.
1779
        """
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
1780
        self.assertEqual(set(left.versions()), set(right.versions()))
1781
        for version in left.versions():
1782
            self.assertEqual(left.get_parents_with_ghosts(version),
1783
                right.get_parents_with_ghosts(version))
1784
            self.assertEqual(left.get_lines(version),
1785
                right.get_lines(version))
1786
            self.assertEqual(left.annotate(version),
1787
                right.annotate(version))
1788
2535.3.4 by Andrew Bennetts
Simple implementation of Knit.insert_data_stream.
1789
    def test_insert_data_stream_empty(self):
1790
        """Inserting a data stream with no records should not put any data into
1791
        the knit.
1792
        """
1793
        k1 = self.make_test_knit()
1794
        k1.insert_data_stream(
1795
            (k1.get_format_signature(), [], lambda ignored: ''))
2535.3.36 by Andrew Bennetts
Merge bzr.dev
1796
        self.assertEqual('', k1.transport.get_bytes(k1._data._access._filename),
2535.3.4 by Andrew Bennetts
Simple implementation of Knit.insert_data_stream.
1797
                         "The .knit should be completely empty.")
1798
        self.assertEqual(k1._index.HEADER,
1799
                         k1.transport.get_bytes(k1._index._filename),
1800
                         "The .kndx should have nothing apart from the header.")
1801
1802
    def test_insert_data_stream_one_record(self):
1803
        """Inserting a data stream with one record from a knit with one record
1804
        results in byte-identical files.
1805
        """
1806
        source = self.make_test_knit(name='source')
1807
        source.add_lines('text-a', [], split_lines(TEXT_1))
1808
        data_stream = source.get_data_stream(['text-a'])
1809
        target = self.make_test_knit(name='target')
1810
        target.insert_data_stream(data_stream)
1811
        self.assertKnitFilesEqual(source, target)
1812
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
1813
    def test_insert_data_stream_annotated_unannotated(self):
1814
        """Inserting an annotated datastream to an unannotated knit works."""
1815
        # case one - full texts.
1816
        source = self.make_test_knit(name='source', annotate=True)
1817
        target = self.make_test_knit(name='target', annotate=False)
1818
        source.add_lines('text-a', [], split_lines(TEXT_1))
1819
        target.insert_data_stream(source.get_data_stream(['text-a']))
1820
        self.assertKnitValuesEqual(source, target)
1821
        # case two - deltas.
1822
        source.add_lines('text-b', ['text-a'], split_lines(TEXT_2))
1823
        target.insert_data_stream(source.get_data_stream(['text-b']))
1824
        self.assertKnitValuesEqual(source, target)
1825
3052.2.3 by Robert Collins
Handle insert_data_stream of an unannotated stream into an annotated knit.
1826
    def test_insert_data_stream_unannotated_annotated(self):
1827
        """Inserting an unannotated datastream to an annotated knit works."""
1828
        # case one - full texts.
1829
        source = self.make_test_knit(name='source', annotate=False)
1830
        target = self.make_test_knit(name='target', annotate=True)
1831
        source.add_lines('text-a', [], split_lines(TEXT_1))
1832
        target.insert_data_stream(source.get_data_stream(['text-a']))
1833
        self.assertKnitValuesEqual(source, target)
1834
        # case two - deltas.
1835
        source.add_lines('text-b', ['text-a'], split_lines(TEXT_2))
1836
        target.insert_data_stream(source.get_data_stream(['text-b']))
1837
        self.assertKnitValuesEqual(source, target)
1838
2535.3.4 by Andrew Bennetts
Simple implementation of Knit.insert_data_stream.
1839
    def test_insert_data_stream_records_already_present(self):
1840
        """Insert a data stream where some records are alreday present in the
1841
        target, and some not.  Only the new records are inserted.
1842
        """
1843
        source = self.make_test_knit(name='source')
1844
        target = self.make_test_knit(name='target')
1845
        # Insert 'text-a' into both source and target
1846
        source.add_lines('text-a', [], split_lines(TEXT_1))
1847
        target.insert_data_stream(source.get_data_stream(['text-a']))
1848
        # Insert 'text-b' into just the source.
1849
        source.add_lines('text-b', ['text-a'], split_lines(TEXT_1))
1850
        # Get a data stream of both text-a and text-b, and insert it.
1851
        data_stream = source.get_data_stream(['text-a', 'text-b'])
1852
        target.insert_data_stream(data_stream)
1853
        # The source and target will now be identical.  This means the text-a
1854
        # record was not added a second time.
1855
        self.assertKnitFilesEqual(source, target)
1856
1857
    def test_insert_data_stream_multiple_records(self):
1858
        """Inserting a data stream of all records from a knit with multiple
1859
        records results in byte-identical files.
1860
        """
1861
        source = self.make_test_knit(name='source')
1862
        source.add_lines('text-a', [], split_lines(TEXT_1))
1863
        source.add_lines('text-b', ['text-a'], split_lines(TEXT_1))
1864
        source.add_lines('text-c', [], split_lines(TEXT_1))
1865
        data_stream = source.get_data_stream(['text-a', 'text-b', 'text-c'])
1866
        
1867
        target = self.make_test_knit(name='target')
1868
        target.insert_data_stream(data_stream)
1869
        
1870
        self.assertKnitFilesEqual(source, target)
1871
1872
    def test_insert_data_stream_ghost_parent(self):
1873
        """Insert a data stream with a record that has a ghost parent."""
1874
        # Make a knit with a record, text-a, that has a ghost parent.
1875
        source = self.make_test_knit(name='source')
1876
        source.add_lines_with_ghosts('text-a', ['text-ghost'],
1877
                                     split_lines(TEXT_1))
1878
        data_stream = source.get_data_stream(['text-a'])
1879
1880
        target = self.make_test_knit(name='target')
1881
        target.insert_data_stream(data_stream)
1882
1883
        self.assertKnitFilesEqual(source, target)
1884
1885
        # The target knit object is in a consistent state, i.e. the record we
1886
        # just added is immediately visible.
1887
        self.assertTrue(target.has_version('text-a'))
3287.6.5 by Robert Collins
Deprecate VersionedFile.has_ghost.
1888
        self.assertFalse(target.has_version('text-ghost'))
1889
        self.assertEqual({'text-a':('text-ghost',)},
1890
            target.get_parent_map(['text-a', 'text-ghost']))
2535.3.4 by Andrew Bennetts
Simple implementation of Knit.insert_data_stream.
1891
        self.assertEqual(split_lines(TEXT_1), target.get_lines('text-a'))
1892
1893
    def test_insert_data_stream_inconsistent_version_lines(self):
1894
        """Inserting a data stream which has different content for a version_id
1895
        than already exists in the knit will raise KnitCorrupt.
1896
        """
1897
        source = self.make_test_knit(name='source')
1898
        target = self.make_test_knit(name='target')
1899
        # Insert a different 'text-a' into both source and target
1900
        source.add_lines('text-a', [], split_lines(TEXT_1))
1901
        target.add_lines('text-a', [], split_lines(TEXT_2))
1902
        # Insert a data stream with conflicting content into the target
1903
        data_stream = source.get_data_stream(['text-a'])
1904
        self.assertRaises(
1905
            errors.KnitCorrupt, target.insert_data_stream, data_stream)
1906
1907
    def test_insert_data_stream_inconsistent_version_parents(self):
1908
        """Inserting a data stream which has different parents for a version_id
1909
        than already exists in the knit will raise KnitCorrupt.
1910
        """
1911
        source = self.make_test_knit(name='source')
1912
        target = self.make_test_knit(name='target')
1913
        # Insert a different 'text-a' into both source and target.  They differ
1914
        # only by the parents list, the content is the same.
1915
        source.add_lines_with_ghosts('text-a', [], split_lines(TEXT_1))
1916
        target.add_lines_with_ghosts('text-a', ['a-ghost'], split_lines(TEXT_1))
1917
        # Insert a data stream with conflicting content into the target
1918
        data_stream = source.get_data_stream(['text-a'])
1919
        self.assertRaises(
1920
            errors.KnitCorrupt, target.insert_data_stream, data_stream)
1921
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
1922
    def test_insert_data_stream_unknown_format(self):
2535.3.4 by Andrew Bennetts
Simple implementation of Knit.insert_data_stream.
1923
        """A data stream in a different format to the target knit cannot be
1924
        inserted.
1925
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
1926
        It will raise KnitDataStreamUnknown because the fallback code will fail
1927
        to make a knit. In future we may need KnitDataStreamIncompatible again,
1928
        for more exotic cases.
2535.3.4 by Andrew Bennetts
Simple implementation of Knit.insert_data_stream.
1929
        """
1930
        data_stream = ('fake-format-signature', [], lambda _: '')
1931
        target = self.make_test_knit(name='target')
1932
        self.assertRaises(
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
1933
            errors.KnitDataStreamUnknown,
2535.3.4 by Andrew Bennetts
Simple implementation of Knit.insert_data_stream.
1934
            target.insert_data_stream, data_stream)
1935
2535.3.5 by Andrew Bennetts
Batch writes as much as possible in insert_data_stream.
1936
    #  * test that a stream of "already present version, then new version"
1937
    #    inserts correctly.
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1938
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
1939
1940
    def assertMadeStreamKnit(self, source_knit, versions, target_knit):
1941
        """Assert that a knit made from a stream is as expected."""
1942
        a_stream = source_knit.get_data_stream(versions)
1943
        expected_data = a_stream[2](None)
1944
        a_stream = source_knit.get_data_stream(versions)
1945
        a_knit = target_knit._knit_from_datastream(a_stream)
1946
        self.assertEqual(source_knit.factory.__class__,
1947
            a_knit.factory.__class__)
1948
        self.assertIsInstance(a_knit._data._access, _StreamAccess)
1949
        self.assertIsInstance(a_knit._index, _StreamIndex)
1950
        self.assertEqual(a_knit._index.data_list, a_stream[1])
1951
        self.assertEqual(a_knit._data._access.data, expected_data)
1952
        self.assertEqual(a_knit.filename, target_knit.filename)
1953
        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.
1954
        self.assertEqual(a_knit._index, a_knit._data._access.stream_index)
1955
        self.assertEqual(target_knit, a_knit._data._access.backing_knit)
1956
        self.assertIsInstance(a_knit._data._access.orig_factory,
1957
            source_knit.factory.__class__)
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
1958
1959
    def test__knit_from_data_stream_empty(self):
1960
        """Create a knit object from a datastream."""
1961
        annotated = self.make_test_knit(name='source', annotate=True)
1962
        plain = self.make_test_knit(name='target', annotate=False)
1963
        # case 1: annotated source
1964
        self.assertMadeStreamKnit(annotated, [], annotated)
1965
        self.assertMadeStreamKnit(annotated, [], plain)
1966
        # case 2: plain source
1967
        self.assertMadeStreamKnit(plain, [], annotated)
1968
        self.assertMadeStreamKnit(plain, [], plain)
1969
1970
    def test__knit_from_data_stream_unknown_format(self):
1971
        annotated = self.make_test_knit(name='source', annotate=True)
1972
        self.assertRaises(errors.KnitDataStreamUnknown,
1973
            annotated._knit_from_datastream, ("unknown", None, None))
1974
1975
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1976
TEXT_1 = """\
1977
Banana cup cakes:
1978
1979
- bananas
1980
- eggs
1981
- broken tea cups
1982
"""
1983
1984
TEXT_1A = """\
1985
Banana cup cake recipe
1986
(serves 6)
1987
1988
- bananas
1989
- eggs
1990
- broken tea cups
1991
- self-raising flour
1992
"""
1993
1664.2.1 by Aaron Bentley
Start work on plan_merge test
1994
TEXT_1B = """\
1995
Banana cup cake recipe
1996
1997
- bananas (do not use plantains!!!)
1998
- broken tea cups
1999
- flour
2000
"""
2001
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
2002
delta_1_1a = """\
2003
0,1,2
2004
Banana cup cake recipe
2005
(serves 6)
2006
5,5,1
2007
- self-raising flour
2008
"""
2009
2010
TEXT_2 = """\
2011
Boeuf bourguignon
2012
2013
- beef
2014
- red wine
2015
- small onions
2016
- carrot
2017
- mushrooms
2018
"""
2019
1664.2.3 by Aaron Bentley
Add failing test case
2020
AB_MERGE_TEXT="""unchanged|Banana cup cake recipe
2021
new-a|(serves 6)
2022
unchanged|
2023
killed-b|- bananas
2024
killed-b|- eggs
2025
new-b|- bananas (do not use plantains!!!)
2026
unchanged|- broken tea cups
2027
new-a|- self-raising flour
1664.2.6 by Aaron Bentley
Got plan-merge passing tests
2028
new-b|- flour
2029
"""
1664.2.3 by Aaron Bentley
Add failing test case
2030
AB_MERGE=[tuple(l.split('|')) for l in AB_MERGE_TEXT.splitlines(True)]
2031
2032
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
2033
def line_delta(from_lines, to_lines):
2034
    """Generate line-based delta from one text to another"""
2035
    s = difflib.SequenceMatcher(None, from_lines, to_lines)
2036
    for op in s.get_opcodes():
2037
        if op[0] == 'equal':
2038
            continue
2039
        yield '%d,%d,%d\n' % (op[1], op[2], op[4]-op[3])
2040
        for i in range(op[3], op[4]):
2041
            yield to_lines[i]
2042
2043
2044
def apply_line_delta(basis_lines, delta_lines):
2045
    """Apply a line-based perfect diff
2046
    
2047
    basis_lines -- text to apply the patch to
2048
    delta_lines -- diff instructions and content
2049
    """
2050
    out = basis_lines[:]
2051
    i = 0
2052
    offset = 0
2053
    while i < len(delta_lines):
2054
        l = delta_lines[i]
2055
        a, b, c = map(long, l.split(','))
2056
        i = i + 1
2057
        out[offset+a:offset+b] = delta_lines[i:i+c]
2058
        i = i + c
2059
        offset = offset + (b - a) + c
2060
    return out
1684.3.3 by Robert Collins
Add a special cased weaves to knit converter.
2061
2062
2063
class TestWeaveToKnit(KnitTests):
2064
2065
    def test_weave_to_knit_matches(self):
2066
        # check that the WeaveToKnit is_compatible function
2067
        # registers True for a Weave to a Knit.
2068
        w = Weave()
2069
        k = self.make_test_knit()
2070
        self.failUnless(WeaveToKnit.is_compatible(w, k))
2071
        self.failIf(WeaveToKnit.is_compatible(k, w))
2072
        self.failIf(WeaveToKnit.is_compatible(w, w))
2073
        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
2074
2075
2076
class TestKnitCaching(KnitTests):
2077
    
2850.1.1 by Robert Collins
* ``KnitVersionedFile.add*`` will no longer cache added records even when
2078
    def create_knit(self):
1863.1.1 by John Arbash Meinel
Allow Versioned files to do caching if explicitly asked, and implement for Knit
2079
        k = self.make_test_knit(True)
2080
        k.add_lines('text-1', [], split_lines(TEXT_1))
2081
        k.add_lines('text-2', [], split_lines(TEXT_2))
2082
        return k
2083
2084
    def test_no_caching(self):
2085
        k = self.create_knit()
2086
        # Nothing should be cached without setting 'enable_cache'
2087
        self.assertEqual({}, k._data._cache)
2088
2089
    def test_cache_data_read_raw(self):
2090
        k = self.create_knit()
2091
2092
        # Now cache and read
2093
        k.enable_cache()
2094
2095
        def read_one_raw(version):
2096
            pos_map = k._get_components_positions([version])
3224.1.13 by John Arbash Meinel
Revert the _get_component_positions api
2097
            method, index_memo, next = pos_map[version]
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
2098
            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
2099
            self.assertEqual(1, len(lst))
2100
            return lst[0]
2101
2102
        val = read_one_raw('text-1')
1863.1.8 by John Arbash Meinel
Removing disk-backed-cache
2103
        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
2104
2105
        k.clear_cache()
2106
        # After clear, new reads are not cached
2107
        self.assertEqual({}, k._data._cache)
2108
2109
        val2 = read_one_raw('text-1')
2110
        self.assertEqual(val, val2)
2111
        self.assertEqual({}, k._data._cache)
2112
2113
    def test_cache_data_read(self):
2114
        k = self.create_knit()
2115
2116
        def read_one(version):
2117
            pos_map = k._get_components_positions([version])
3224.1.13 by John Arbash Meinel
Revert the _get_component_positions api
2118
            method, index_memo, next = pos_map[version]
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
2119
            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
2120
            self.assertEqual(1, len(lst))
2121
            return lst[0]
2122
2123
        # Now cache and read
2124
        k.enable_cache()
2125
2126
        val = read_one('text-2')
2127
        self.assertEqual(['text-2'], k._data._cache.keys())
2128
        self.assertEqual('text-2', val[0])
2129
        content, digest = k._data._parse_record('text-2',
2130
                                                k._data._cache['text-2'])
2131
        self.assertEqual(content, val[1])
2132
        self.assertEqual(digest, val[2])
2133
2134
        k.clear_cache()
2135
        self.assertEqual({}, k._data._cache)
2136
2137
        val2 = read_one('text-2')
2138
        self.assertEqual(val, val2)
2139
        self.assertEqual({}, k._data._cache)
2140
2141
    def test_cache_read(self):
2142
        k = self.create_knit()
2143
        k.enable_cache()
2144
2145
        text = k.get_text('text-1')
2146
        self.assertEqual(TEXT_1, text)
2147
        self.assertEqual(['text-1'], k._data._cache.keys())
2148
2149
        k.clear_cache()
2150
        self.assertEqual({}, k._data._cache)
2151
2152
        text = k.get_text('text-1')
2153
        self.assertEqual(TEXT_1, text)
2154
        self.assertEqual({}, k._data._cache)
2102.2.1 by John Arbash Meinel
Fix bug #64789 _KnitIndex.add_versions() should dict compress new revisions
2155
2156
2157
class TestKnitIndex(KnitTests):
2158
2159
    def test_add_versions_dictionary_compresses(self):
2160
        """Adding versions to the index should update the lookup dict"""
2161
        knit = self.make_test_knit()
2162
        idx = knit._index
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
2163
        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
2164
        self.check_file_contents('test.kndx',
2165
            '# bzr knit index 8\n'
2166
            '\n'
2167
            'a-1 fulltext 0 0  :'
2168
            )
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
2169
        idx.add_versions([('a-2', ['fulltext'], (None, 0, 0), ['a-1']),
2170
                          ('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
2171
                         ])
2172
        self.check_file_contents('test.kndx',
2173
            '# bzr knit index 8\n'
2174
            '\n'
2175
            'a-1 fulltext 0 0  :\n'
2176
            'a-2 fulltext 0 0 0 :\n'
2177
            'a-3 fulltext 0 0 1 :'
2178
            )
2179
        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.
2180
        self.assertEqual({'a-1':('a-1', ['fulltext'], 0, 0, (), 0),
2181
                          'a-2':('a-2', ['fulltext'], 0, 0, ('a-1',), 1),
2182
                          '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
2183
                         }, idx._cache)
2184
2185
    def test_add_versions_fails_clean(self):
2186
        """If add_versions fails in the middle, it restores a pristine state.
2187
2188
        Any modifications that are made to the index are reset if all versions
2189
        cannot be added.
2190
        """
2191
        # This cheats a little bit by passing in a generator which will
2192
        # raise an exception before the processing finishes
2193
        # Other possibilities would be to have an version with the wrong number
2194
        # of entries, or to make the backing transport unable to write any
2195
        # files.
2196
2197
        knit = self.make_test_knit()
2198
        idx = knit._index
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
2199
        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
2200
2201
        class StopEarly(Exception):
2202
            pass
2203
2204
        def generate_failure():
2205
            """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.
2206
            yield ('a-2', ['fulltext'], (None, 0, 0), ('a-1',))
2207
            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
2208
            raise StopEarly()
2209
2210
        # Assert the pre-condition
2211
        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.
2212
        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
2213
2214
        self.assertRaises(StopEarly, idx.add_versions, generate_failure())
2215
2216
        # And it shouldn't be modified
2217
        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.
2218
        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.
2219
2220
    def test_knit_index_ignores_empty_files(self):
2221
        # There was a race condition in older bzr, where a ^C at the right time
2222
        # could leave an empty .kndx file, which bzr would later claim was a
2223
        # corrupted file since the header was not present. In reality, the file
2224
        # just wasn't created, so it should be ignored.
2225
        t = get_transport('.')
2226
        t.put_bytes('test.kndx', '')
2227
2228
        knit = self.make_test_knit()
2229
2230
    def test_knit_index_checks_header(self):
2231
        t = get_transport('.')
2232
        t.put_bytes('test.kndx', '# not really a knit header\n\n')
2233
2196.2.1 by John Arbash Meinel
Merge Dmitry's optimizations and minimize the actual diff.
2234
        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.
2235
2236
2237
class TestGraphIndexKnit(KnitTests):
2238
    """Tests for knits using a GraphIndex rather than a KnitIndex."""
2239
2240
    def make_g_index(self, name, ref_lists=0, nodes=[]):
2241
        builder = GraphIndexBuilder(ref_lists)
2242
        for node, references, value in nodes:
2243
            builder.add_node(node, references, value)
2244
        stream = builder.finish()
2245
        trans = self.get_transport()
2890.2.1 by Robert Collins
* ``bzrlib.index.GraphIndex`` now requires a size parameter to the
2246
        size = trans.put_file(name, stream)
2247
        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.
2248
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2249
    def two_graph_index(self, deltas=False, catch_adds=False):
2592.3.13 by Robert Collins
Implement KnitGraphIndex.get_method.
2250
        """Build a two-graph index.
2251
2252
        :param deltas: If true, use underlying indices with two node-ref
2253
            lists and 'parent' set to a delta-compressed against tail.
2254
        """
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.
2255
        # build a complex graph across several indices.
2592.3.13 by Robert Collins
Implement KnitGraphIndex.get_method.
2256
        if deltas:
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2257
            # delta compression inn the index
2592.3.13 by Robert Collins
Implement KnitGraphIndex.get_method.
2258
            index1 = self.make_g_index('1', 2, [
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2259
                (('tip', ), 'N0 100', ([('parent', )], [], )),
2260
                (('tail', ), '', ([], []))])
2592.3.13 by Robert Collins
Implement KnitGraphIndex.get_method.
2261
            index2 = self.make_g_index('2', 2, [
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2262
                (('parent', ), ' 100 78', ([('tail', ), ('ghost', )], [('tail', )])),
2263
                (('separate', ), '', ([], []))])
2592.3.13 by Robert Collins
Implement KnitGraphIndex.get_method.
2264
        else:
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2265
            # just blob location and graph in the index.
2592.3.13 by Robert Collins
Implement KnitGraphIndex.get_method.
2266
            index1 = self.make_g_index('1', 1, [
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2267
                (('tip', ), 'N0 100', ([('parent', )], )),
2268
                (('tail', ), '', ([], ))])
2592.3.13 by Robert Collins
Implement KnitGraphIndex.get_method.
2269
            index2 = self.make_g_index('2', 1, [
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2270
                (('parent', ), ' 100 78', ([('tail', ), ('ghost', )], )),
2271
                (('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.
2272
        combined_index = CombinedGraphIndex([index1, index2])
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2273
        if catch_adds:
2274
            self.combined_index = combined_index
2275
            self.caught_entries = []
2276
            add_callback = self.catch_add
2277
        else:
2278
            add_callback = None
2279
        return KnitGraphIndex(combined_index, deltas=deltas,
2280
            add_callback=add_callback)
2592.3.4 by Robert Collins
Implement get_ancestry/get_ancestry_with_ghosts for KnitGraphIndex.
2281
2282
    def test_get_ancestry(self):
2592.3.30 by Robert Collins
Make GraphKnitIndex get_ancestry the same as regular knits.
2283
        # get_ancestry is defined as eliding ghosts, not erroring.
2284
        index = self.two_graph_index()
2592.3.4 by Robert Collins
Implement get_ancestry/get_ancestry_with_ghosts for KnitGraphIndex.
2285
        self.assertEqual([], index.get_ancestry([]))
2286
        self.assertEqual(['separate'], index.get_ancestry(['separate']))
2287
        self.assertEqual(['tail'], index.get_ancestry(['tail']))
2288
        self.assertEqual(['tail', 'parent'], index.get_ancestry(['parent']))
2289
        self.assertEqual(['tail', 'parent', 'tip'], index.get_ancestry(['tip']))
2290
        self.assertTrue(index.get_ancestry(['tip', 'separate']) in
2291
            (['tail', 'parent', 'tip', 'separate'],
2292
             ['separate', 'tail', 'parent', 'tip'],
2293
            ))
2294
        # and without topo_sort
2295
        self.assertEqual(set(['separate']),
2296
            set(index.get_ancestry(['separate'], topo_sorted=False)))
2297
        self.assertEqual(set(['tail']),
2298
            set(index.get_ancestry(['tail'], topo_sorted=False)))
2299
        self.assertEqual(set(['tail', 'parent']),
2300
            set(index.get_ancestry(['parent'], topo_sorted=False)))
2301
        self.assertEqual(set(['tail', 'parent', 'tip']),
2302
            set(index.get_ancestry(['tip'], topo_sorted=False)))
2303
        self.assertEqual(set(['separate', 'tail', 'parent', 'tip']),
2304
            set(index.get_ancestry(['tip', 'separate'])))
2592.3.30 by Robert Collins
Make GraphKnitIndex get_ancestry the same as regular knits.
2305
        # asking for a ghost makes it go boom.
2306
        self.assertRaises(errors.RevisionNotPresent, index.get_ancestry, ['ghost'])
2592.3.4 by Robert Collins
Implement get_ancestry/get_ancestry_with_ghosts for KnitGraphIndex.
2307
2308
    def test_get_ancestry_with_ghosts(self):
2309
        index = self.two_graph_index()
2310
        self.assertEqual([], index.get_ancestry_with_ghosts([]))
2311
        self.assertEqual(['separate'], index.get_ancestry_with_ghosts(['separate']))
2312
        self.assertEqual(['tail'], index.get_ancestry_with_ghosts(['tail']))
2313
        self.assertTrue(index.get_ancestry_with_ghosts(['parent']) in
2314
            (['tail', 'ghost', 'parent'],
2315
             ['ghost', 'tail', 'parent'],
2316
            ))
2317
        self.assertTrue(index.get_ancestry_with_ghosts(['tip']) in
2318
            (['tail', 'ghost', 'parent', 'tip'],
2319
             ['ghost', 'tail', 'parent', 'tip'],
2320
            ))
2321
        self.assertTrue(index.get_ancestry_with_ghosts(['tip', 'separate']) in
2322
            (['tail', 'ghost', 'parent', 'tip', 'separate'],
2323
             ['ghost', 'tail', 'parent', 'tip', 'separate'],
2324
             ['separate', 'tail', 'ghost', 'parent', 'tip'],
2325
             ['separate', 'ghost', 'tail', 'parent', 'tip'],
2326
            ))
2592.3.30 by Robert Collins
Make GraphKnitIndex get_ancestry the same as regular knits.
2327
        # asking for a ghost makes it go boom.
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2328
        self.assertRaises(errors.RevisionNotPresent, index.get_ancestry_with_ghosts, ['ghost'])
2592.3.5 by Robert Collins
Implement KnitGraphIndex.num_versions.
2329
2330
    def test_num_versions(self):
2331
        index = self.two_graph_index()
2332
        self.assertEqual(4, index.num_versions())
2592.3.6 by Robert Collins
Implement KnitGraphIndex.get_versions.
2333
2334
    def test_get_versions(self):
2335
        index = self.two_graph_index()
2336
        self.assertEqual(set(['tail', 'tip', 'parent', 'separate']),
2337
            set(index.get_versions()))
2338
2592.3.9 by Robert Collins
Implement KnitGraphIndex.has_version.
2339
    def test_has_version(self):
2340
        index = self.two_graph_index()
2341
        self.assertTrue(index.has_version('tail'))
2342
        self.assertFalse(index.has_version('ghost'))
2343
2592.3.10 by Robert Collins
Implement KnitGraphIndex.get_position.
2344
    def test_get_position(self):
2345
        index = self.two_graph_index()
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
2346
        self.assertEqual((index._graph_index._indices[0], 0, 100), index.get_position('tip'))
2347
        self.assertEqual((index._graph_index._indices[1], 100, 78), index.get_position('parent'))
2592.3.10 by Robert Collins
Implement KnitGraphIndex.get_position.
2348
2592.3.13 by Robert Collins
Implement KnitGraphIndex.get_method.
2349
    def test_get_method_deltas(self):
2350
        index = self.two_graph_index(deltas=True)
2592.3.11 by Robert Collins
Implement KnitGraphIndex.get_method.
2351
        self.assertEqual('fulltext', index.get_method('tip'))
2352
        self.assertEqual('line-delta', index.get_method('parent'))
2353
2592.3.13 by Robert Collins
Implement KnitGraphIndex.get_method.
2354
    def test_get_method_no_deltas(self):
2355
        # check that the parent-history lookup is ignored with deltas=False.
2356
        index = self.two_graph_index(deltas=False)
2357
        self.assertEqual('fulltext', index.get_method('tip'))
2358
        self.assertEqual('fulltext', index.get_method('parent'))
2359
2592.3.14 by Robert Collins
Implement KnitGraphIndex.get_options.
2360
    def test_get_options_deltas(self):
2361
        index = self.two_graph_index(deltas=True)
2658.2.1 by Robert Collins
Fix mismatch between KnitGraphIndex and KnitIndex in get_options.
2362
        self.assertEqual(['fulltext', 'no-eol'], index.get_options('tip'))
2363
        self.assertEqual(['line-delta'], index.get_options('parent'))
2592.3.14 by Robert Collins
Implement KnitGraphIndex.get_options.
2364
2365
    def test_get_options_no_deltas(self):
2366
        # check that the parent-history lookup is ignored with deltas=False.
2367
        index = self.two_graph_index(deltas=False)
2658.2.1 by Robert Collins
Fix mismatch between KnitGraphIndex and KnitIndex in get_options.
2368
        self.assertEqual(['fulltext', 'no-eol'], index.get_options('tip'))
2369
        self.assertEqual(['fulltext'], index.get_options('parent'))
2592.3.14 by Robert Collins
Implement KnitGraphIndex.get_options.
2370
2592.3.15 by Robert Collins
Implement KnitGraphIndex.get_parents/get_parents_with_ghosts.
2371
    def test_get_parents_with_ghosts(self):
2372
        index = self.two_graph_index()
2373
        self.assertEqual(('tail', 'ghost'), index.get_parents_with_ghosts('parent'))
2592.3.43 by Robert Collins
A knit iter_parents API.
2374
        # and errors on ghosts.
2375
        self.assertRaises(errors.RevisionNotPresent,
2376
            index.get_parents_with_ghosts, 'ghost')
2592.3.15 by Robert Collins
Implement KnitGraphIndex.get_parents/get_parents_with_ghosts.
2377
2592.3.16 by Robert Collins
Implement KnitGraphIndex.check_versions_present.
2378
    def test_check_versions_present(self):
2379
        # ghosts should not be considered present
2380
        index = self.two_graph_index()
2381
        self.assertRaises(RevisionNotPresent, index.check_versions_present,
2382
            ['ghost'])
2383
        self.assertRaises(RevisionNotPresent, index.check_versions_present,
2384
            ['tail', 'ghost'])
2385
        index.check_versions_present(['tail', 'separate'])
2592.3.14 by Robert Collins
Implement KnitGraphIndex.get_options.
2386
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2387
    def catch_add(self, entries):
2388
        self.caught_entries.append(entries)
2389
2390
    def test_add_no_callback_errors(self):
2391
        index = self.two_graph_index()
2392
        self.assertRaises(errors.ReadOnlyError, index.add_version,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2393
            '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.
2394
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
2395
    def test_add_version_smoke(self):
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2396
        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
2397
        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.
2398
        self.assertEqual([[(('new', ), 'N50 60', ((('separate',),),))]],
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2399
            self.caught_entries)
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
2400
2401
    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.
2402
        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.
2403
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2404
            '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.
2405
        self.assertEqual([], self.caught_entries)
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
2406
2407
    def test_add_version_same_dup(self):
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2408
        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.
2409
        # options can be spelt two different ways
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2410
        index.add_version('tip', 'fulltext,no-eol', (None, 0, 100), ['parent'])
2411
        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.
2412
        # but neither should have added data.
2413
        self.assertEqual([[], []], self.caught_entries)
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
2414
        
2415
    def test_add_version_different_dup(self):
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2416
        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.
2417
        # change options
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', 'no-eol,line-delta', (None, 0, 100), ['parent'])
2420
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2421
            'tip', 'line-delta,no-eol', (None, 0, 100), ['parent'])
2422
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2423
            'tip', 'fulltext', (None, 0, 100), ['parent'])
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
2424
        # position/length
2425
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2426
            '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.
2427
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2428
            '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.
2429
        # parents
2430
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2431
            'tip', 'fulltext,no-eol', (None, 0, 100), [])
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2432
        self.assertEqual([], self.caught_entries)
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
2433
        
2434
    def test_add_versions_nodeltas(self):
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2435
        index = self.two_graph_index(catch_adds=True)
2436
        index.add_versions([
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2437
                ('new', 'fulltext,no-eol', (None, 50, 60), ['separate']),
2438
                ('new2', 'fulltext', (None, 0, 6), ['new']),
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2439
                ])
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2440
        self.assertEqual([(('new', ), 'N50 60', ((('separate',),),)),
2441
            (('new2', ), ' 0 6', ((('new',),),))],
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2442
            sorted(self.caught_entries[0]))
2443
        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.
2444
2445
    def test_add_versions_deltas(self):
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2446
        index = self.two_graph_index(deltas=True, catch_adds=True)
2447
        index.add_versions([
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2448
                ('new', 'fulltext,no-eol', (None, 50, 60), ['separate']),
2449
                ('new2', 'line-delta', (None, 0, 6), ['new']),
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2450
                ])
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2451
        self.assertEqual([(('new', ), 'N50 60', ((('separate',),), ())),
2452
            (('new2', ), ' 0 6', ((('new',),), (('new',),), ))],
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2453
            sorted(self.caught_entries[0]))
2454
        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.
2455
2456
    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.
2457
        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.
2458
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2459
            [('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.
2460
        self.assertEqual([], self.caught_entries)
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
2461
2841.2.1 by Robert Collins
* Commit no longer checks for new text keys during insertion when the
2462
    def test_add_versions_random_id_accepted(self):
2463
        index = self.two_graph_index(catch_adds=True)
2464
        index.add_versions([], random_id=True)
2465
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
2466
    def test_add_versions_same_dup(self):
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2467
        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.
2468
        # options can be spelt two different ways
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2469
        index.add_versions([('tip', 'fulltext,no-eol', (None, 0, 100), ['parent'])])
2470
        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.
2471
        # but neither should have added data.
2472
        self.assertEqual([[], []], self.caught_entries)
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
2473
        
2474
    def test_add_versions_different_dup(self):
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2475
        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.
2476
        # change options
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', 'no-eol,line-delta', (None, 0, 100), ['parent'])])
2479
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2480
            [('tip', 'line-delta,no-eol', (None, 0, 100), ['parent'])])
2481
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2482
            [('tip', 'fulltext', (None, 0, 100), ['parent'])])
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
2483
        # position/length
2484
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2485
            [('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.
2486
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2487
            [('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.
2488
        # parents
2489
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2490
            [('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.
2491
        # change options in the second record
2492
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2493
            [('tip', 'fulltext,no-eol', (None, 0, 100), ['parent']),
2494
             ('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.
2495
        self.assertEqual([], self.caught_entries)
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2496
2592.3.43 by Robert Collins
A knit iter_parents API.
2497
    def test_iter_parents(self):
2498
        index1 = self.make_g_index('1', 1, [
2499
        # no parents
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2500
            (('r0', ), 'N0 100', ([], )),
2592.3.43 by Robert Collins
A knit iter_parents API.
2501
        # 1 parent
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2502
            (('r1', ), '', ([('r0', )], ))])
2592.3.43 by Robert Collins
A knit iter_parents API.
2503
        index2 = self.make_g_index('2', 1, [
2504
        # 2 parents
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2505
            (('r2', ), 'N0 100', ([('r1', ), ('r0', )], )),
2592.3.43 by Robert Collins
A knit iter_parents API.
2506
            ])
2507
        combined_index = CombinedGraphIndex([index1, index2])
2508
        index = KnitGraphIndex(combined_index)
2509
        # XXX TODO a ghost
2510
        # cases: each sample data individually:
2511
        self.assertEqual(set([('r0', ())]),
2512
            set(index.iter_parents(['r0'])))
2513
        self.assertEqual(set([('r1', ('r0', ))]),
2514
            set(index.iter_parents(['r1'])))
2515
        self.assertEqual(set([('r2', ('r1', 'r0'))]),
2516
            set(index.iter_parents(['r2'])))
2517
        # no nodes returned for a missing node
2518
        self.assertEqual(set(),
2519
            set(index.iter_parents(['missing'])))
2520
        # 1 node returned with missing nodes skipped
2521
        self.assertEqual(set([('r1', ('r0', ))]),
2522
            set(index.iter_parents(['ghost1', 'r1', 'ghost'])))
2523
        # 2 nodes returned
2524
        self.assertEqual(set([('r0', ()), ('r1', ('r0', ))]),
2525
            set(index.iter_parents(['r0', 'r1'])))
2526
        # 2 nodes returned, missing skipped
2527
        self.assertEqual(set([('r0', ()), ('r1', ('r0', ))]),
2528
            set(index.iter_parents(['a', 'r0', 'b', 'r1', 'c'])))
2529
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2530
2531
class TestNoParentsGraphIndexKnit(KnitTests):
2532
    """Tests for knits using KnitGraphIndex with no parents."""
2533
2534
    def make_g_index(self, name, ref_lists=0, nodes=[]):
2535
        builder = GraphIndexBuilder(ref_lists)
2536
        for node, references in nodes:
2537
            builder.add_node(node, references)
2538
        stream = builder.finish()
2539
        trans = self.get_transport()
2890.2.1 by Robert Collins
* ``bzrlib.index.GraphIndex`` now requires a size parameter to the
2540
        size = trans.put_file(name, stream)
2541
        return GraphIndex(trans, name, size)
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2542
2543
    def test_parents_deltas_incompatible(self):
2544
        index = CombinedGraphIndex([])
2545
        self.assertRaises(errors.KnitError, KnitGraphIndex, index,
2546
            deltas=True, parents=False)
2547
2548
    def two_graph_index(self, catch_adds=False):
2549
        """Build a two-graph index.
2550
2551
        :param deltas: If true, use underlying indices with two node-ref
2552
            lists and 'parent' set to a delta-compressed against tail.
2553
        """
2554
        # put several versions in the index.
2555
        index1 = self.make_g_index('1', 0, [
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2556
            (('tip', ), 'N0 100'),
2557
            (('tail', ), '')])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2558
        index2 = self.make_g_index('2', 0, [
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2559
            (('parent', ), ' 100 78'),
2560
            (('separate', ), '')])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2561
        combined_index = CombinedGraphIndex([index1, index2])
2562
        if catch_adds:
2563
            self.combined_index = combined_index
2564
            self.caught_entries = []
2565
            add_callback = self.catch_add
2566
        else:
2567
            add_callback = None
2568
        return KnitGraphIndex(combined_index, parents=False,
2569
            add_callback=add_callback)
2570
2571
    def test_get_ancestry(self):
2572
        # with no parents, ancestry is always just the key.
2573
        index = self.two_graph_index()
2574
        self.assertEqual([], index.get_ancestry([]))
2575
        self.assertEqual(['separate'], index.get_ancestry(['separate']))
2576
        self.assertEqual(['tail'], index.get_ancestry(['tail']))
2577
        self.assertEqual(['parent'], index.get_ancestry(['parent']))
2578
        self.assertEqual(['tip'], index.get_ancestry(['tip']))
2579
        self.assertTrue(index.get_ancestry(['tip', 'separate']) in
2580
            (['tip', 'separate'],
2581
             ['separate', 'tip'],
2582
            ))
2583
        # asking for a ghost makes it go boom.
2584
        self.assertRaises(errors.RevisionNotPresent, index.get_ancestry, ['ghost'])
2585
2586
    def test_get_ancestry_with_ghosts(self):
2587
        index = self.two_graph_index()
2588
        self.assertEqual([], index.get_ancestry_with_ghosts([]))
2589
        self.assertEqual(['separate'], index.get_ancestry_with_ghosts(['separate']))
2590
        self.assertEqual(['tail'], index.get_ancestry_with_ghosts(['tail']))
2591
        self.assertEqual(['parent'], index.get_ancestry_with_ghosts(['parent']))
2592
        self.assertEqual(['tip'], index.get_ancestry_with_ghosts(['tip']))
2593
        self.assertTrue(index.get_ancestry_with_ghosts(['tip', 'separate']) in
2594
            (['tip', 'separate'],
2595
             ['separate', 'tip'],
2596
            ))
2597
        # asking for a ghost makes it go boom.
2598
        self.assertRaises(errors.RevisionNotPresent, index.get_ancestry_with_ghosts, ['ghost'])
2599
2600
    def test_num_versions(self):
2601
        index = self.two_graph_index()
2602
        self.assertEqual(4, index.num_versions())
2603
2604
    def test_get_versions(self):
2605
        index = self.two_graph_index()
2606
        self.assertEqual(set(['tail', 'tip', 'parent', 'separate']),
2607
            set(index.get_versions()))
2608
2609
    def test_has_version(self):
2610
        index = self.two_graph_index()
2611
        self.assertTrue(index.has_version('tail'))
2612
        self.assertFalse(index.has_version('ghost'))
2613
2614
    def test_get_position(self):
2615
        index = self.two_graph_index()
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
2616
        self.assertEqual((index._graph_index._indices[0], 0, 100), index.get_position('tip'))
2617
        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.
2618
2619
    def test_get_method(self):
2620
        index = self.two_graph_index()
2621
        self.assertEqual('fulltext', index.get_method('tip'))
2658.2.1 by Robert Collins
Fix mismatch between KnitGraphIndex and KnitIndex in get_options.
2622
        self.assertEqual(['fulltext'], index.get_options('parent'))
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2623
2624
    def test_get_options(self):
2625
        index = self.two_graph_index()
2658.2.1 by Robert Collins
Fix mismatch between KnitGraphIndex and KnitIndex in get_options.
2626
        self.assertEqual(['fulltext', 'no-eol'], index.get_options('tip'))
2627
        self.assertEqual(['fulltext'], index.get_options('parent'))
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2628
2629
    def test_get_parents_with_ghosts(self):
2630
        index = self.two_graph_index()
2631
        self.assertEqual((), index.get_parents_with_ghosts('parent'))
2592.3.43 by Robert Collins
A knit iter_parents API.
2632
        # and errors on ghosts.
2633
        self.assertRaises(errors.RevisionNotPresent,
2634
            index.get_parents_with_ghosts, 'ghost')
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2635
2636
    def test_check_versions_present(self):
2637
        index = self.two_graph_index()
2638
        self.assertRaises(RevisionNotPresent, index.check_versions_present,
2639
            ['missing'])
2640
        self.assertRaises(RevisionNotPresent, index.check_versions_present,
2641
            ['tail', 'missing'])
2642
        index.check_versions_present(['tail', 'separate'])
2643
2644
    def catch_add(self, entries):
2645
        self.caught_entries.append(entries)
2646
2647
    def test_add_no_callback_errors(self):
2648
        index = self.two_graph_index()
2649
        self.assertRaises(errors.ReadOnlyError, index.add_version,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2650
            'new', 'fulltext,no-eol', (None, 50, 60), ['separate'])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2651
2652
    def test_add_version_smoke(self):
2653
        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
2654
        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.
2655
        self.assertEqual([[(('new', ), 'N50 60')]],
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2656
            self.caught_entries)
2657
2658
    def test_add_version_delta_not_delta_index(self):
2659
        index = self.two_graph_index(catch_adds=True)
2660
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2661
            'new', 'no-eol,line-delta', (None, 0, 100), [])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2662
        self.assertEqual([], self.caught_entries)
2663
2664
    def test_add_version_same_dup(self):
2665
        index = self.two_graph_index(catch_adds=True)
2666
        # options can be spelt two different ways
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2667
        index.add_version('tip', 'fulltext,no-eol', (None, 0, 100), [])
2668
        index.add_version('tip', 'no-eol,fulltext', (None, 0, 100), [])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2669
        # but neither should have added data.
2670
        self.assertEqual([[], []], self.caught_entries)
2671
        
2672
    def test_add_version_different_dup(self):
2673
        index = self.two_graph_index(catch_adds=True)
2674
        # change options
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', 'no-eol,line-delta', (None, 0, 100), [])
2677
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2678
            'tip', 'line-delta,no-eol', (None, 0, 100), [])
2679
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2680
            'tip', 'fulltext', (None, 0, 100), [])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2681
        # position/length
2682
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2683
            'tip', 'fulltext,no-eol', (None, 50, 100), [])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2684
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2685
            'tip', 'fulltext,no-eol', (None, 0, 1000), [])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2686
        # parents
2687
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2688
            'tip', 'fulltext,no-eol', (None, 0, 100), ['parent'])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2689
        self.assertEqual([], self.caught_entries)
2690
        
2691
    def test_add_versions(self):
2692
        index = self.two_graph_index(catch_adds=True)
2693
        index.add_versions([
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2694
                ('new', 'fulltext,no-eol', (None, 50, 60), []),
2695
                ('new2', 'fulltext', (None, 0, 6), []),
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2696
                ])
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2697
        self.assertEqual([(('new', ), 'N50 60'), (('new2', ), ' 0 6')],
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2698
            sorted(self.caught_entries[0]))
2699
        self.assertEqual(1, len(self.caught_entries))
2700
2701
    def test_add_versions_delta_not_delta_index(self):
2702
        index = self.two_graph_index(catch_adds=True)
2703
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2704
            [('new', 'no-eol,line-delta', (None, 0, 100), ['parent'])])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2705
        self.assertEqual([], self.caught_entries)
2706
2707
    def test_add_versions_parents_not_parents_index(self):
2708
        index = self.two_graph_index(catch_adds=True)
2709
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2710
            [('new', 'no-eol,fulltext', (None, 0, 100), ['parent'])])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2711
        self.assertEqual([], self.caught_entries)
2712
2841.2.1 by Robert Collins
* Commit no longer checks for new text keys during insertion when the
2713
    def test_add_versions_random_id_accepted(self):
2714
        index = self.two_graph_index(catch_adds=True)
2715
        index.add_versions([], random_id=True)
2716
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2717
    def test_add_versions_same_dup(self):
2718
        index = self.two_graph_index(catch_adds=True)
2719
        # options can be spelt two different ways
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2720
        index.add_versions([('tip', 'fulltext,no-eol', (None, 0, 100), [])])
2721
        index.add_versions([('tip', 'no-eol,fulltext', (None, 0, 100), [])])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2722
        # but neither should have added data.
2723
        self.assertEqual([[], []], self.caught_entries)
2724
        
2725
    def test_add_versions_different_dup(self):
2726
        index = self.two_graph_index(catch_adds=True)
2727
        # change options
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', 'no-eol,line-delta', (None, 0, 100), [])])
2730
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2731
            [('tip', 'line-delta,no-eol', (None, 0, 100), [])])
2732
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2733
            [('tip', 'fulltext', (None, 0, 100), [])])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2734
        # position/length
2735
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2736
            [('tip', 'fulltext,no-eol', (None, 50, 100), [])])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2737
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2738
            [('tip', 'fulltext,no-eol', (None, 0, 1000), [])])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2739
        # parents
2740
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2741
            [('tip', 'fulltext,no-eol', (None, 0, 100), ['parent'])])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2742
        # change options in the second record
2743
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2744
            [('tip', 'fulltext,no-eol', (None, 0, 100), []),
2745
             ('tip', 'no-eol,line-delta', (None, 0, 100), [])])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2746
        self.assertEqual([], self.caught_entries)
2747
2592.3.43 by Robert Collins
A knit iter_parents API.
2748
    def test_iter_parents(self):
2749
        index = self.two_graph_index()
2750
        self.assertEqual(set([
2751
            ('tip', ()), ('tail', ()), ('parent', ()), ('separate', ())
2752
            ]),
2753
            set(index.iter_parents(['tip', 'tail', 'ghost', 'parent', 'separate'])))
2754
        self.assertEqual(set([('tip', ())]),
2755
            set(index.iter_parents(['tip'])))
2756
        self.assertEqual(set(),
2757
            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.
2758
2759
2760
class TestPackKnits(KnitTests):
2761
    """Tests that use a _PackAccess and KnitGraphIndex."""
2762
2763
    def test_get_data_stream_packs_ignores_pack_overhead(self):
2764
        # Packs have an encoding overhead that should not be included in the
2765
        # 'size' field of a data stream, because it is not returned by the
2766
        # raw_reading functions - it is why index_memo's are opaque, and
2767
        # get_data_stream was abusing this.
2768
        packname = 'test.pack'
2769
        transport = self.get_transport()
2770
        def write_data(bytes):
2771
            transport.append_bytes(packname, bytes)
2772
        writer = pack.ContainerWriter(write_data)
2773
        writer.begin()
2774
        index = InMemoryGraphIndex(2)
2775
        knit_index = KnitGraphIndex(index, add_callback=index.add_nodes,
2776
            deltas=True)
2777
        indices = {index:(transport, packname)}
2778
        access = _PackAccess(indices, writer=(writer, index))
2779
        k = KnitVersionedFile('test', get_transport('.'),
2780
            delta=True, create=True, index=knit_index, access_method=access)
2781
        # insert something into the knit
2782
        k.add_lines('text-1', [], ["foo\n"])
2783
        # get a data stream for it
2784
        stream = k.get_data_stream(['text-1'])
2785
        # if the stream has been incorrectly assembled, we will get a short read
2786
        # reading from the stream (as streams have no trailer)
2787
        expected_length = stream[1][0][2]
2788
        # we use -1 to do the read, so that if a trailer is added this test
2789
        # will fail and we'll adjust it to handle that case correctly, rather
2790
        # than allowing an over-read that is bogus.
2791
        self.assertEqual(expected_length, len(stream[2](-1)))
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2792
2793
2794
class Test_StreamIndex(KnitTests):
2795
2796
    def get_index(self, knit, stream):
2797
        """Get a _StreamIndex from knit and stream."""
2798
        return knit._knit_from_datastream(stream)._index
2799
2800
    def assertIndexVersions(self, knit, versions):
2801
        """Check that the _StreamIndex versions are those of the stream."""
2802
        index = self.get_index(knit, knit.get_data_stream(versions))
2803
        self.assertEqual(set(index.get_versions()), set(versions))
2804
        # check we didn't get duplicates
2805
        self.assertEqual(len(index.get_versions()), len(versions))
2806
2807
    def assertIndexAncestry(self, knit, ancestry_versions, versions, result):
2808
        """Check the result of a get_ancestry call on knit."""
2809
        index = self.get_index(knit, knit.get_data_stream(versions))
3052.2.4 by Andrew Bennetts
Some tweaks suggested by John's review.
2810
        self.assertEqual(
2811
            set(result),
2812
            set(index.get_ancestry(ancestry_versions, False)))
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2813
3052.2.3 by Robert Collins
Handle insert_data_stream of an unannotated stream into an annotated knit.
2814
    def assertIterParents(self, knit, versions, parent_versions, result):
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2815
        """Check the result of an iter_parents call on knit."""
2816
        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.
2817
        self.assertEqual(result, index.iter_parents(parent_versions))
2818
2819
    def assertGetMethod(self, knit, versions, version, result):
2820
        index = self.get_index(knit, knit.get_data_stream(versions))
2821
        self.assertEqual(result, index.get_method(version))
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2822
2823
    def assertGetOptions(self, knit, version, options):
2824
        index = self.get_index(knit, knit.get_data_stream(version))
2825
        self.assertEqual(options, index.get_options(version))
2826
2827
    def assertGetPosition(self, knit, versions, version, result):
2828
        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.
2829
        if result[1] is None:
2830
            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.
2831
        self.assertEqual(result, index.get_position(version))
2832
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2833
    def assertGetParentsWithGhosts(self, knit, versions, version, parents):
2834
        index = self.get_index(knit, knit.get_data_stream(versions))
2835
        self.assertEqual(parents, index.get_parents_with_ghosts(version))
2836
2837
    def make_knit_with_4_versions_2_dags(self):
2838
        knit = self.make_test_knit()
2839
        knit.add_lines('a', [], ["foo"])
2840
        knit.add_lines('b', [], [])
2841
        knit.add_lines('c', ['b', 'a'], [])
2842
        knit.add_lines_with_ghosts('d', ['e', 'f'], [])
2843
        return knit
2844
2845
    def test_versions(self):
2846
        """The versions of a StreamIndex are those of the datastream."""
2847
        knit = self.make_knit_with_4_versions_2_dags()
2848
        # ask for most permutations, which catches bugs like falling back to the
2849
        # target knit, or showing ghosts, etc.
2850
        self.assertIndexVersions(knit, [])
2851
        self.assertIndexVersions(knit, ['a'])
2852
        self.assertIndexVersions(knit, ['b'])
2853
        self.assertIndexVersions(knit, ['c'])
2854
        self.assertIndexVersions(knit, ['d'])
2855
        self.assertIndexVersions(knit, ['a', 'b'])
2856
        self.assertIndexVersions(knit, ['b', 'c'])
2857
        self.assertIndexVersions(knit, ['a', 'c'])
2858
        self.assertIndexVersions(knit, ['a', 'b', 'c'])
2859
        self.assertIndexVersions(knit, ['a', 'b', 'c', 'd'])
2860
2861
    def test_construct(self):
2862
        """Constructing a StreamIndex generates index data."""
2863
        data_list = [('text-a', ['fulltext'], 127, []),
2864
            ('text-b', ['option'], 128, ['text-c'])]
3224.1.8 by John Arbash Meinel
Add noeol to the return signature of get_build_details.
2865
        index = _StreamIndex(data_list, None)
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2866
        self.assertEqual({'text-a':(['fulltext'], (0, 127), []),
2867
            'text-b':(['option'], (127, 127 + 128), ['text-c'])},
2868
            index._by_version)
2869
2870
    def test_get_ancestry(self):
2871
        knit = self.make_knit_with_4_versions_2_dags()
2872
        self.assertIndexAncestry(knit, ['a'], ['a'], ['a'])
2873
        self.assertIndexAncestry(knit, ['b'], ['b'], ['b'])
2874
        self.assertIndexAncestry(knit, ['c'], ['c'], ['c'])
2875
        self.assertIndexAncestry(knit, ['c'], ['a', 'b', 'c'],
3052.2.4 by Andrew Bennetts
Some tweaks suggested by John's review.
2876
            set(['a', 'b', 'c']))
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2877
        self.assertIndexAncestry(knit, ['c', 'd'], ['a', 'b', 'c', 'd'],
3052.2.4 by Andrew Bennetts
Some tweaks suggested by John's review.
2878
            set(['a', 'b', 'c', 'd']))
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2879
3052.2.3 by Robert Collins
Handle insert_data_stream of an unannotated stream into an annotated knit.
2880
    def test_get_method(self):
2881
        knit = self.make_knit_with_4_versions_2_dags()
2882
        self.assertGetMethod(knit, ['a'], 'a', 'fulltext')
2883
        self.assertGetMethod(knit, ['c'], 'c', 'line-delta')
2884
        # get_method on a basis that is not in the datastream (but in the
2885
        # backing knit) returns 'fulltext', because thats what we'll create as
2886
        # we thunk across.
2887
        self.assertGetMethod(knit, ['c'], 'b', 'fulltext')
2888
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2889
    def test_iter_parents(self):
2890
        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.
2891
        self.assertIterParents(knit, ['a'], ['a'], [('a', ())])
3052.2.3 by Robert Collins
Handle insert_data_stream of an unannotated stream into an annotated knit.
2892
        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.
2893
            [('a', ()), ('b', ())])
3052.2.3 by Robert Collins
Handle insert_data_stream of an unannotated stream into an annotated knit.
2894
        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.
2895
            [('a', ()), ('b', ()), ('c', ('b', 'a'))])
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2896
        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.
2897
            ['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.
2898
            [('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.
2899
        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.
2900
            [('c', ('b', 'a'))])
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2901
2902
    def test_get_options(self):
2903
        knit = self.make_knit_with_4_versions_2_dags()
2904
        self.assertGetOptions(knit, 'a', ['no-eol', 'fulltext'])
2905
        self.assertGetOptions(knit, 'c', ['line-delta'])
2906
2907
    def test_get_parents_with_ghosts(self):
2908
        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.
2909
        self.assertGetParentsWithGhosts(knit, ['a'], 'a', ())
2910
        self.assertGetParentsWithGhosts(knit, ['c'], 'c', ('b', 'a'))
2911
        self.assertGetParentsWithGhosts(knit, ['d'], 'd', ('e', 'f'))
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2912
2913
    def test_get_position(self):
2914
        knit = self.make_knit_with_4_versions_2_dags()
3052.2.6 by Andrew Bennetts
Fix typo in comment.
2915
        # 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
2916
        # _StreamAccess to use.
3052.2.5 by Andrew Bennetts
Address the rest of the review comments from John and myself.
2917
        self.assertGetPosition(knit, ['a'], 'a', (False, None, 0, 78))
2918
        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.
2919
        # 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.
2920
        # 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.
2921
        # 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.
2922
        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.
2923
2924
2925
class Test_StreamAccess(KnitTests):
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2926
2927
    def get_index_access(self, knit, stream):
2928
        """Get a _StreamAccess from knit and stream."""
2929
        knit =  knit._knit_from_datastream(stream)
2930
        return knit._index, knit._data._access
2931
2932
    def assertGetRawRecords(self, knit, versions):
2933
        index, access = self.get_index_access(knit,
2934
            knit.get_data_stream(versions))
2935
        # check that every version asked for can be obtained from the resulting
2936
        # access object.
2937
        # batch
2938
        memos = []
2939
        for version in versions:
2940
            memos.append(knit._index.get_position(version))
2941
        original = {}
2942
        for version, data in zip(
2943
            versions, knit._data._access.get_raw_records(memos)):
2944
            original[version] = data
2945
        memos = []
2946
        for version in versions:
2947
            memos.append(index.get_position(version))
2948
        streamed = {}
2949
        for version, data in zip(versions, access.get_raw_records(memos)):
2950
            streamed[version] = data
2951
        self.assertEqual(original, streamed)
2952
        # individually
2953
        for version in versions:
2954
            data = list(access.get_raw_records(
2955
                [index.get_position(version)]))[0]
2956
            self.assertEqual(original[version], data)
2957
2958
    def make_knit_with_two_versions(self):
2959
        knit = self.make_test_knit()
2960
        knit.add_lines('a', [], ["foo"])
2961
        knit.add_lines('b', [], ["bar"])
2962
        return knit
2963
2964
    def test_get_raw_records(self):
2965
        knit = self.make_knit_with_two_versions()
2966
        self.assertGetRawRecords(knit, ['a', 'b'])
2967
        self.assertGetRawRecords(knit, ['a'])
2968
        self.assertGetRawRecords(knit, ['b'])
3052.2.3 by Robert Collins
Handle insert_data_stream of an unannotated stream into an annotated knit.
2969
    
2970
    def test_get_raw_record_from_backing_knit(self):
2971
        # the thunk layer should create an artificial A on-demand when needed.
2972
        source_knit = self.make_test_knit(name='plain', annotate=False)
2973
        target_knit = self.make_test_knit(name='annotated', annotate=True)
2974
        source_knit.add_lines("A", [], ["Foo\n"])
2975
        # Give the target A, so we can try to thunk across to it.
2976
        target_knit.join(source_knit)
2977
        index, access = self.get_index_access(target_knit,
2978
            source_knit.get_data_stream([]))
3052.2.5 by Andrew Bennetts
Address the rest of the review comments from John and myself.
2979
        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.
2980
        df = GzipFile(mode='rb', fileobj=StringIO(raw_data))
2981
        self.assertEqual(
2982
            'version A 1 5d36b88bb697a2d778f024048bafabd443d74503\n'
2983
            'Foo\nend A\n',
2984
            df.read())
2985
2986
    def test_asking_for_thunk_stream_is_not_plain_errors(self):
2987
        knit = self.make_test_knit(name='annotated', annotate=True)
2988
        knit.add_lines("A", [], ["Foo\n"])
2989
        index, access = self.get_index_access(knit,
2990
            knit.get_data_stream([]))
2991
        self.assertRaises(errors.KnitCorrupt,
3052.2.5 by Andrew Bennetts
Address the rest of the review comments from John and myself.
2992
            list, access.get_raw_records([(True, "A", None, None)]))