/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_graph(self):
632
        transport = MockTransport()
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
633
        index = self.get_knit_index(transport, "filename", "w", create=True)
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
634
        self.assertEqual([], index.get_graph())
635
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
636
        index.add_version("a", ["option"], (None, 0, 1), ["b"])
3287.5.5 by Robert Collins
Refactor internals of knit implementations to implement get_parents_with_ghosts in terms of get_parent_map.
637
        self.assertEqual([("a", ("b",))], index.get_graph())
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
638
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
639
        index.add_version("c", ["option"], (None, 0, 1), ["d"])
3287.5.5 by Robert Collins
Refactor internals of knit implementations to implement get_parents_with_ghosts in terms of get_parent_map.
640
        self.assertEqual([("a", ("b",)), ("c", ("d",))],
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
641
            sorted(index.get_graph()))
642
643
    def test_get_ancestry(self):
644
        transport = MockTransport([
645
            _KnitIndex.HEADER,
646
            "a option 0 1 :",
647
            "b option 0 1 0 .e :",
648
            "c option 0 1 1 0 :",
649
            "d option 0 1 2 .f :"
650
            ])
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
651
        index = self.get_knit_index(transport, "filename", "r")
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
652
653
        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
654
        self.assertEqual(["a"], index.get_ancestry(["a"]))
655
        self.assertEqual(["a", "b"], index.get_ancestry(["b"]))
656
        self.assertEqual(["a", "b", "c"], index.get_ancestry(["c"]))
657
        self.assertEqual(["a", "b", "c", "d"], index.get_ancestry(["d"]))
658
        self.assertEqual(["a", "b"], index.get_ancestry(["a", "b"]))
659
        self.assertEqual(["a", "b", "c"], index.get_ancestry(["a", "c"]))
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
660
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
661
        self.assertRaises(RevisionNotPresent, index.get_ancestry, ["e"])
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
662
663
    def test_get_ancestry_with_ghosts(self):
664
        transport = MockTransport([
665
            _KnitIndex.HEADER,
666
            "a option 0 1 :",
667
            "b option 0 1 0 .e :",
668
            "c option 0 1 0 .f .g :",
669
            "d option 0 1 2 .h .j .k :"
670
            ])
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
671
        index = self.get_knit_index(transport, "filename", "r")
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
672
673
        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
674
        self.assertEqual(["a"], index.get_ancestry_with_ghosts(["a"]))
675
        self.assertEqual(["a", "e", "b"],
676
            index.get_ancestry_with_ghosts(["b"]))
677
        self.assertEqual(["a", "g", "f", "c"],
678
            index.get_ancestry_with_ghosts(["c"]))
679
        self.assertEqual(["a", "g", "f", "c", "k", "j", "h", "d"],
680
            index.get_ancestry_with_ghosts(["d"]))
681
        self.assertEqual(["a", "e", "b"],
682
            index.get_ancestry_with_ghosts(["a", "b"]))
683
        self.assertEqual(["a", "g", "f", "c"],
684
            index.get_ancestry_with_ghosts(["a", "c"]))
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
685
        self.assertEqual(
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
686
            ["a", "g", "f", "c", "e", "b", "k", "j", "h", "d"],
687
            index.get_ancestry_with_ghosts(["b", "d"]))
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
688
689
        self.assertRaises(RevisionNotPresent,
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
690
            index.get_ancestry_with_ghosts, ["e"])
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
691
2592.3.43 by Robert Collins
A knit iter_parents API.
692
    def test_iter_parents(self):
693
        transport = MockTransport()
694
        index = self.get_knit_index(transport, "filename", "w", create=True)
695
        # no parents
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
696
        index.add_version('r0', ['option'], (None, 0, 1), [])
2592.3.43 by Robert Collins
A knit iter_parents API.
697
        # 1 parent
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
698
        index.add_version('r1', ['option'], (None, 0, 1), ['r0'])
2592.3.43 by Robert Collins
A knit iter_parents API.
699
        # 2 parents
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
700
        index.add_version('r2', ['option'], (None, 0, 1), ['r1', 'r0'])
2592.3.43 by Robert Collins
A knit iter_parents API.
701
        # XXX TODO a ghost
702
        # cases: each sample data individually:
703
        self.assertEqual(set([('r0', ())]),
704
            set(index.iter_parents(['r0'])))
705
        self.assertEqual(set([('r1', ('r0', ))]),
706
            set(index.iter_parents(['r1'])))
707
        self.assertEqual(set([('r2', ('r1', 'r0'))]),
708
            set(index.iter_parents(['r2'])))
709
        # no nodes returned for a missing node
710
        self.assertEqual(set(),
711
            set(index.iter_parents(['missing'])))
712
        # 1 node returned with missing nodes skipped
713
        self.assertEqual(set([('r1', ('r0', ))]),
714
            set(index.iter_parents(['ghost1', 'r1', 'ghost'])))
715
        # 2 nodes returned
716
        self.assertEqual(set([('r0', ()), ('r1', ('r0', ))]),
717
            set(index.iter_parents(['r0', 'r1'])))
718
        # 2 nodes returned, missing skipped
719
        self.assertEqual(set([('r0', ()), ('r1', ('r0', ))]),
720
            set(index.iter_parents(['a', 'r0', 'b', 'r1', 'c'])))
721
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
722
    def test_num_versions(self):
723
        transport = MockTransport([
724
            _KnitIndex.HEADER
725
            ])
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
726
        index = self.get_knit_index(transport, "filename", "r")
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
727
728
        self.assertEqual(0, index.num_versions())
729
        self.assertEqual(0, len(index))
730
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
731
        index.add_version("a", ["option"], (None, 0, 1), [])
732
        self.assertEqual(1, index.num_versions())
733
        self.assertEqual(1, len(index))
734
735
        index.add_version("a", ["option2"], (None, 1, 2), [])
736
        self.assertEqual(1, index.num_versions())
737
        self.assertEqual(1, len(index))
738
739
        index.add_version("b", ["option"], (None, 0, 1), [])
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
740
        self.assertEqual(2, index.num_versions())
741
        self.assertEqual(2, len(index))
742
743
    def test_get_versions(self):
744
        transport = MockTransport([
745
            _KnitIndex.HEADER
746
            ])
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
747
        index = self.get_knit_index(transport, "filename", "r")
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
748
749
        self.assertEqual([], index.get_versions())
750
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
751
        index.add_version("a", ["option"], (None, 0, 1), [])
752
        self.assertEqual(["a"], index.get_versions())
753
754
        index.add_version("a", ["option"], (None, 0, 1), [])
755
        self.assertEqual(["a"], index.get_versions())
756
757
        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
758
        self.assertEqual(["a", "b"], index.get_versions())
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
759
760
    def test_add_version(self):
761
        transport = MockTransport([
762
            _KnitIndex.HEADER
763
            ])
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
764
        index = self.get_knit_index(transport, "filename", "r")
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
765
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
766
        index.add_version("a", ["option"], (None, 0, 1), ["b"])
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
767
        self.assertEqual(("append_bytes",
768
            ("filename", "\na option 0 1 .b :"),
769
            {}), transport.calls.pop(0))
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
770
        self.assertTrue(index.has_version("a"))
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
771
        self.assertEqual(1, index.num_versions())
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
772
        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
773
        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.
774
        self.assertEqual(("b",), index.get_parents_with_ghosts("a"))
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
775
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
776
        index.add_version("a", ["opt"], (None, 1, 2), ["c"])
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
777
        self.assertEqual(("append_bytes",
778
            ("filename", "\na opt 1 2 .c :"),
779
            {}), transport.calls.pop(0))
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
780
        self.assertTrue(index.has_version("a"))
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
781
        self.assertEqual(1, index.num_versions())
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
782
        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
783
        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.
784
        self.assertEqual(("c",), index.get_parents_with_ghosts("a"))
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
785
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
786
        index.add_version("b", ["option"], (None, 2, 3), ["a"])
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
787
        self.assertEqual(("append_bytes",
788
            ("filename", "\nb option 2 3 0 :"),
789
            {}), transport.calls.pop(0))
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
790
        self.assertTrue(index.has_version("b"))
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
791
        self.assertEqual(2, index.num_versions())
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
792
        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
793
        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.
794
        self.assertEqual(("a",), index.get_parents_with_ghosts("b"))
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
795
796
    def test_add_versions(self):
797
        transport = MockTransport([
798
            _KnitIndex.HEADER
799
            ])
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
800
        index = self.get_knit_index(transport, "filename", "r")
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
801
802
        index.add_versions([
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
803
            ("a", ["option"], (None, 0, 1), ["b"]),
804
            ("a", ["opt"], (None, 1, 2), ["c"]),
805
            ("b", ["option"], (None, 2, 3), ["a"])
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
806
            ])
807
        self.assertEqual(("append_bytes", ("filename",
808
            "\na option 0 1 .b :"
809
            "\na opt 1 2 .c :"
810
            "\nb option 2 3 0 :"
811
            ), {}), transport.calls.pop(0))
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
812
        self.assertTrue(index.has_version("a"))
813
        self.assertTrue(index.has_version("b"))
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
814
        self.assertEqual(2, index.num_versions())
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
815
        self.assertEqual((None, 1, 2), index.get_position("a"))
816
        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
817
        self.assertEqual(["opt"], index.get_options("a"))
818
        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.
819
        self.assertEqual(("c",), index.get_parents_with_ghosts("a"))
820
        self.assertEqual(("a",), index.get_parents_with_ghosts("b"))
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
821
2841.2.1 by Robert Collins
* Commit no longer checks for new text keys during insertion when the
822
    def test_add_versions_random_id_is_accepted(self):
823
        transport = MockTransport([
824
            _KnitIndex.HEADER
825
            ])
826
        index = self.get_knit_index(transport, "filename", "r")
827
828
        index.add_versions([
829
            ("a", ["option"], (None, 0, 1), ["b"]),
830
            ("a", ["opt"], (None, 1, 2), ["c"]),
831
            ("b", ["option"], (None, 2, 3), ["a"])
832
            ], random_id=True)
833
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
834
    def test_delay_create_and_add_versions(self):
835
        transport = MockTransport()
836
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
837
        index = self.get_knit_index(transport, "filename", "w",
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
838
            create=True, file_mode="wb", create_parent_dir=True,
839
            delay_create=True, dir_mode=0777)
840
        self.assertEqual([], transport.calls)
841
842
        index.add_versions([
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
843
            ("a", ["option"], (None, 0, 1), ["b"]),
844
            ("a", ["opt"], (None, 1, 2), ["c"]),
845
            ("b", ["option"], (None, 2, 3), ["a"])
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
846
            ])
847
        name, (filename, f), kwargs = transport.calls.pop(0)
848
        self.assertEqual("put_file_non_atomic", name)
849
        self.assertEqual(
850
            {"dir_mode": 0777, "create_parent_dir": True, "mode": "wb"},
851
            kwargs)
852
        self.assertEqual("filename", filename)
853
        self.assertEqual(
854
            index.HEADER +
855
            "\na option 0 1 .b :"
856
            "\na opt 1 2 .c :"
857
            "\nb option 2 3 0 :",
858
            f.read())
859
860
    def test_has_version(self):
861
        transport = MockTransport([
862
            _KnitIndex.HEADER,
863
            "a option 0 1 :"
864
            ])
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
865
        index = self.get_knit_index(transport, "filename", "r")
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
866
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
867
        self.assertTrue(index.has_version("a"))
868
        self.assertFalse(index.has_version("b"))
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
869
870
    def test_get_position(self):
871
        transport = MockTransport([
872
            _KnitIndex.HEADER,
873
            "a option 0 1 :",
874
            "b option 1 2 :"
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
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
878
        self.assertEqual((None, 0, 1), index.get_position("a"))
879
        self.assertEqual((None, 1, 2), index.get_position("b"))
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
880
881
    def test_get_method(self):
882
        transport = MockTransport([
883
            _KnitIndex.HEADER,
884
            "a fulltext,unknown 0 1 :",
885
            "b unknown,line-delta 1 2 :",
886
            "c bad 3 4 :"
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("fulltext", index.get_method("a"))
891
        self.assertEqual("line-delta", index.get_method("b"))
892
        self.assertRaises(errors.KnitIndexUnknownMethod, index.get_method, "c")
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
893
894
    def test_get_options(self):
895
        transport = MockTransport([
896
            _KnitIndex.HEADER,
897
            "a opt1 0 1 :",
898
            "b opt2,opt3 1 2 :"
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
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
902
        self.assertEqual(["opt1"], index.get_options("a"))
903
        self.assertEqual(["opt2", "opt3"], index.get_options("b"))
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
904
3287.5.6 by Robert Collins
Remove _KnitIndex.get_parents.
905
    def test_get_parent_map(self):
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
906
        transport = MockTransport([
907
            _KnitIndex.HEADER,
908
            "a option 0 1 :",
909
            "b option 1 2 0 .c :",
910
            "c option 1 2 1 0 .e :"
911
            ])
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
912
        index = self.get_knit_index(transport, "filename", "r")
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
913
3287.5.6 by Robert Collins
Remove _KnitIndex.get_parents.
914
        self.assertEqual({
915
            "a":(),
916
            "b":("a", "c"),
917
            "c":("b", "a", "e"),
918
            }, index.get_parent_map(["a", "b", "c"]))
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
919
920
    def test_get_parents_with_ghosts(self):
921
        transport = MockTransport([
922
            _KnitIndex.HEADER,
923
            "a option 0 1 :",
924
            "b option 1 2 0 .c :",
925
            "c option 1 2 1 0 .e :"
926
            ])
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
927
        index = self.get_knit_index(transport, "filename", "r")
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
928
3287.5.5 by Robert Collins
Refactor internals of knit implementations to implement get_parents_with_ghosts in terms of get_parent_map.
929
        self.assertEqual((), index.get_parents_with_ghosts("a"))
930
        self.assertEqual(("a", "c"), index.get_parents_with_ghosts("b"))
931
        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
932
            index.get_parents_with_ghosts("c"))
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
933
934
    def test_check_versions_present(self):
935
        transport = MockTransport([
936
            _KnitIndex.HEADER,
937
            "a option 0 1 :",
938
            "b option 0 1 :"
939
            ])
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
940
        index = self.get_knit_index(transport, "filename", "r")
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
941
942
        check = index.check_versions_present
943
944
        check([])
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
945
        check(["a"])
946
        check(["b"])
947
        check(["a", "b"])
948
        self.assertRaises(RevisionNotPresent, check, ["c"])
949
        self.assertRaises(RevisionNotPresent, check, ["a", "b", "c"])
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
950
2484.1.13 by John Arbash Meinel
Add a test that KnitCorrupt is raised when parent strings are invalid.
951
    def test_impossible_parent(self):
952
        """Test we get KnitCorrupt if the parent couldn't possibly exist."""
953
        transport = MockTransport([
954
            _KnitIndex.HEADER,
955
            "a option 0 1 :",
956
            "b option 0 1 4 :"  # We don't have a 4th record
957
            ])
2484.1.17 by John Arbash Meinel
Workaround for Pyrex <0.9.5 and python >=2.5 incompatibilities.
958
        try:
959
            self.assertRaises(errors.KnitCorrupt,
960
                              self.get_knit_index, transport, 'filename', 'r')
961
        except TypeError, e:
962
            if (str(e) == ('exceptions must be strings, classes, or instances,'
963
                           ' not exceptions.IndexError')
964
                and sys.version_info[0:2] >= (2,5)):
965
                self.knownFailure('Pyrex <0.9.5 fails with TypeError when'
966
                                  ' raising new style exceptions with python'
967
                                  ' >=2.5')
2484.1.19 by John Arbash Meinel
Don't suppress the TypeError if it doesn't match our requirements.
968
            else:
969
                raise
2484.1.13 by John Arbash Meinel
Add a test that KnitCorrupt is raised when parent strings are invalid.
970
971
    def test_corrupted_parent(self):
972
        transport = MockTransport([
973
            _KnitIndex.HEADER,
974
            "a option 0 1 :",
975
            "b option 0 1 :",
976
            "c option 0 1 1v :", # Can't have a parent of '1v'
977
            ])
2484.1.17 by John Arbash Meinel
Workaround for Pyrex <0.9.5 and python >=2.5 incompatibilities.
978
        try:
979
            self.assertRaises(errors.KnitCorrupt,
980
                              self.get_knit_index, transport, 'filename', 'r')
981
        except TypeError, e:
982
            if (str(e) == ('exceptions must be strings, classes, or instances,'
983
                           ' not exceptions.ValueError')
984
                and sys.version_info[0:2] >= (2,5)):
985
                self.knownFailure('Pyrex <0.9.5 fails with TypeError when'
986
                                  ' raising new style exceptions with python'
987
                                  ' >=2.5')
2484.1.19 by John Arbash Meinel
Don't suppress the TypeError if it doesn't match our requirements.
988
            else:
989
                raise
2484.1.13 by John Arbash Meinel
Add a test that KnitCorrupt is raised when parent strings are invalid.
990
991
    def test_corrupted_parent_in_list(self):
992
        transport = MockTransport([
993
            _KnitIndex.HEADER,
994
            "a option 0 1 :",
995
            "b option 0 1 :",
2484.1.17 by John Arbash Meinel
Workaround for Pyrex <0.9.5 and python >=2.5 incompatibilities.
996
            "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.
997
            ])
2484.1.17 by John Arbash Meinel
Workaround for Pyrex <0.9.5 and python >=2.5 incompatibilities.
998
        try:
999
            self.assertRaises(errors.KnitCorrupt,
1000
                              self.get_knit_index, transport, 'filename', 'r')
1001
        except TypeError, e:
1002
            if (str(e) == ('exceptions must be strings, classes, or instances,'
1003
                           ' not exceptions.ValueError')
1004
                and sys.version_info[0:2] >= (2,5)):
1005
                self.knownFailure('Pyrex <0.9.5 fails with TypeError when'
1006
                                  ' raising new style exceptions with python'
1007
                                  ' >=2.5')
2484.1.19 by John Arbash Meinel
Don't suppress the TypeError if it doesn't match our requirements.
1008
            else:
1009
                raise
2484.1.13 by John Arbash Meinel
Add a test that KnitCorrupt is raised when parent strings are invalid.
1010
2484.1.18 by John Arbash Meinel
Test that we properly verify the size and position strings.
1011
    def test_invalid_position(self):
1012
        transport = MockTransport([
1013
            _KnitIndex.HEADER,
1014
            "a option 1v 1 :",
1015
            ])
1016
        try:
1017
            self.assertRaises(errors.KnitCorrupt,
1018
                              self.get_knit_index, transport, 'filename', 'r')
1019
        except TypeError, e:
1020
            if (str(e) == ('exceptions must be strings, classes, or instances,'
1021
                           ' not exceptions.ValueError')
1022
                and sys.version_info[0:2] >= (2,5)):
1023
                self.knownFailure('Pyrex <0.9.5 fails with TypeError when'
1024
                                  ' raising new style exceptions with python'
1025
                                  ' >=2.5')
2484.1.19 by John Arbash Meinel
Don't suppress the TypeError if it doesn't match our requirements.
1026
            else:
1027
                raise
2484.1.18 by John Arbash Meinel
Test that we properly verify the size and position strings.
1028
1029
    def test_invalid_size(self):
1030
        transport = MockTransport([
1031
            _KnitIndex.HEADER,
1032
            "a option 1 1v :",
1033
            ])
1034
        try:
1035
            self.assertRaises(errors.KnitCorrupt,
1036
                              self.get_knit_index, transport, 'filename', 'r')
1037
        except TypeError, e:
1038
            if (str(e) == ('exceptions must be strings, classes, or instances,'
1039
                           ' not exceptions.ValueError')
1040
                and sys.version_info[0:2] >= (2,5)):
1041
                self.knownFailure('Pyrex <0.9.5 fails with TypeError when'
1042
                                  ' raising new style exceptions with python'
1043
                                  ' >=2.5')
2484.1.19 by John Arbash Meinel
Don't suppress the TypeError if it doesn't match our requirements.
1044
            else:
1045
                raise
2484.1.18 by John Arbash Meinel
Test that we properly verify the size and position strings.
1046
2484.1.24 by John Arbash Meinel
Add direct tests of how we handle incomplete/'broken' lines
1047
    def test_short_line(self):
1048
        transport = MockTransport([
1049
            _KnitIndex.HEADER,
1050
            "a option 0 10  :",
1051
            "b option 10 10 0", # This line isn't terminated, ignored
1052
            ])
1053
        index = self.get_knit_index(transport, "filename", "r")
1054
        self.assertEqual(['a'], index.get_versions())
1055
1056
    def test_skip_incomplete_record(self):
1057
        # A line with bogus data should just be skipped
1058
        transport = MockTransport([
1059
            _KnitIndex.HEADER,
1060
            "a option 0 10  :",
1061
            "b option 10 10 0", # This line isn't terminated, ignored
1062
            "c option 20 10 0 :", # Properly terminated, and starts with '\n'
1063
            ])
1064
        index = self.get_knit_index(transport, "filename", "r")
1065
        self.assertEqual(['a', 'c'], index.get_versions())
1066
1067
    def test_trailing_characters(self):
1068
        # A line with bogus data should just be skipped
1069
        transport = MockTransport([
1070
            _KnitIndex.HEADER,
1071
            "a option 0 10  :",
1072
            "b option 10 10 0 :a", # This line has extra trailing characters
1073
            "c option 20 10 0 :", # Properly terminated, and starts with '\n'
1074
            ])
1075
        index = self.get_knit_index(transport, "filename", "r")
1076
        self.assertEqual(['a', 'c'], index.get_versions())
1077
2158.3.1 by Dmitry Vasiliev
KnitIndex tests/fixes/optimizations
1078
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
1079
class LowLevelKnitIndexTests_c(LowLevelKnitIndexTests):
1080
1081
    _test_needs_features = [CompiledKnitFeature]
1082
1083
    def get_knit_index(self, *args, **kwargs):
1084
        orig = knit._load_data
1085
        def reset():
1086
            knit._load_data = orig
1087
        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
1088
        from bzrlib._knit_load_data_c import _load_data_c
1089
        knit._load_data = _load_data_c
2484.1.1 by John Arbash Meinel
Add an initial function to read knit indexes in pyrex.
1090
        return _KnitIndex(*args, **kwargs)
1091
1092
1093
1684.3.3 by Robert Collins
Add a special cased weaves to knit converter.
1094
class KnitTests(TestCaseWithTransport):
1095
    """Class containing knit test helper routines."""
1563.2.16 by Robert Collins
Change WeaveStore into VersionedFileStore and make its versoined file class parameterisable.
1096
2535.3.27 by Andrew Bennetts
Merge from bzr.dev.
1097
    def make_test_knit(self, annotate=False, delay_create=False, index=None,
1098
                       name='test'):
1563.2.16 by Robert Collins
Change WeaveStore into VersionedFileStore and make its versoined file class parameterisable.
1099
        if not annotate:
1100
            factory = KnitPlainFactory()
1101
        else:
1102
            factory = None
2535.3.4 by Andrew Bennetts
Simple implementation of Knit.insert_data_stream.
1103
        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
1104
                                 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.
1105
                                 delay_create=delay_create, index=index)
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1106
2535.3.53 by Andrew Bennetts
Remove get_stream_as_bytes from KnitVersionedFile's API, make it a function in knitrepo.py instead.
1107
    def assertRecordContentEqual(self, knit, version_id, candidate_content):
1108
        """Assert that some raw record content matches the raw record content
1109
        for a particular version_id in the given knit.
1110
        """
1111
        index_memo = knit._index.get_position(version_id)
1112
        record = (version_id, index_memo)
1113
        [(_, expected_content)] = list(knit._data.read_records_iter_raw([record]))
1114
        self.assertEqual(expected_content, candidate_content)
1115
1684.3.3 by Robert Collins
Add a special cased weaves to knit converter.
1116
1117
class BasicKnitTests(KnitTests):
1118
1119
    def add_stock_one_and_one_a(self, k):
1120
        k.add_lines('text-1', [], split_lines(TEXT_1))
1121
        k.add_lines('text-1a', ['text-1'], split_lines(TEXT_1A))
1122
1123
    def test_knit_constructor(self):
1124
        """Construct empty k"""
1125
        self.make_test_knit()
1126
2592.3.1 by Robert Collins
Allow giving KnitVersionedFile an index object to use rather than implicitly creating one.
1127
    def test_make_explicit_index(self):
1128
        """We can supply an index to use."""
1129
        knit = KnitVersionedFile('test', get_transport('.'),
1130
            index='strangelove')
1131
        self.assertEqual(knit._index, 'strangelove')
1132
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1133
    def test_knit_add(self):
1134
        """Store one text in knit and retrieve"""
1563.2.16 by Robert Collins
Change WeaveStore into VersionedFileStore and make its versoined file class parameterisable.
1135
        k = self.make_test_knit()
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1136
        k.add_lines('text-1', [], split_lines(TEXT_1))
1137
        self.assertTrue(k.has_version('text-1'))
1138
        self.assertEqualDiff(''.join(k.get_lines('text-1')), TEXT_1)
1139
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
1140
    def test_newline_empty_lines(self):
1141
        # ensure that ["\n"] round trips ok.
1142
        knit = self.make_test_knit()
1143
        knit.add_lines('a', [], ["\n"])
1144
        knit.add_lines_with_ghosts('b', [], ["\n"])
1145
        self.assertEqual(["\n"], knit.get_lines('a'))
1146
        self.assertEqual(["\n"], knit.get_lines('b'))
1147
        self.assertEqual(['fulltext'], knit._index.get_options('a'))
1148
        self.assertEqual(['fulltext'], knit._index.get_options('b'))
1149
        knit.add_lines('c', ['a'], ["\n"])
1150
        knit.add_lines_with_ghosts('d', ['b'], ["\n"])
1151
        self.assertEqual(["\n"], knit.get_lines('c'))
1152
        self.assertEqual(["\n"], knit.get_lines('d'))
1153
        self.assertEqual(['line-delta'], knit._index.get_options('c'))
1154
        self.assertEqual(['line-delta'], knit._index.get_options('d'))
1155
1156
    def test_empty_lines(self):
1157
        # bizarrely, [] is not listed as having no-eol. 
1158
        knit = self.make_test_knit()
1159
        knit.add_lines('a', [], [])
1160
        knit.add_lines_with_ghosts('b', [], [])
1161
        self.assertEqual([], knit.get_lines('a'))
1162
        self.assertEqual([], knit.get_lines('b'))
1163
        self.assertEqual(['fulltext'], knit._index.get_options('a'))
1164
        self.assertEqual(['fulltext'], knit._index.get_options('b'))
1165
        knit.add_lines('c', ['a'], [])
1166
        knit.add_lines_with_ghosts('d', ['b'], [])
1167
        self.assertEqual([], knit.get_lines('c'))
1168
        self.assertEqual([], knit.get_lines('d'))
1169
        self.assertEqual(['line-delta'], knit._index.get_options('c'))
1170
        self.assertEqual(['line-delta'], knit._index.get_options('d'))
1171
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1172
    def test_knit_reload(self):
1641.1.2 by Robert Collins
Change knit index files to be robust in the presence of partial writes.
1173
        # 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.
1174
        k = self.make_test_knit()
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1175
        k.add_lines('text-1', [], split_lines(TEXT_1))
1176
        del k
1685.1.39 by John Arbash Meinel
Updating test_knit to not instantiate a LocalTransport directly.
1177
        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.
1178
        self.assertTrue(k2.has_version('text-1'))
1179
        self.assertEqualDiff(''.join(k2.get_lines('text-1')), TEXT_1)
1180
1181
    def test_knit_several(self):
1182
        """Store several texts in a knit"""
1563.2.16 by Robert Collins
Change WeaveStore into VersionedFileStore and make its versoined file class parameterisable.
1183
        k = self.make_test_knit()
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1184
        k.add_lines('text-1', [], split_lines(TEXT_1))
1185
        k.add_lines('text-2', [], split_lines(TEXT_2))
1186
        self.assertEqualDiff(''.join(k.get_lines('text-1')), TEXT_1)
1187
        self.assertEqualDiff(''.join(k.get_lines('text-2')), TEXT_2)
1188
        
1189
    def test_repeated_add(self):
1190
        """Knit traps attempt to replace existing version"""
1563.2.16 by Robert Collins
Change WeaveStore into VersionedFileStore and make its versoined file class parameterisable.
1191
        k = self.make_test_knit()
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1192
        k.add_lines('text-1', [], split_lines(TEXT_1))
1193
        self.assertRaises(RevisionAlreadyPresent, 
1194
                k.add_lines,
1195
                'text-1', [], split_lines(TEXT_1))
1196
1197
    def test_empty(self):
1563.2.16 by Robert Collins
Change WeaveStore into VersionedFileStore and make its versoined file class parameterisable.
1198
        k = self.make_test_knit(True)
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1199
        k.add_lines('text-1', [], [])
1200
        self.assertEquals(k.get_lines('text-1'), [])
1201
1202
    def test_incomplete(self):
1203
        """Test if texts without a ending line-end can be inserted and
1204
        extracted."""
1685.1.39 by John Arbash Meinel
Updating test_knit to not instantiate a LocalTransport directly.
1205
        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.
1206
        k.add_lines('text-1', [], ['a\n',    'b'  ])
1207
        k.add_lines('text-2', ['text-1'], ['a\rb\n', 'b\n'])
1666.1.6 by Robert Collins
Make knit the default format.
1208
        # reopening ensures maximum room for confusion
1685.1.39 by John Arbash Meinel
Updating test_knit to not instantiate a LocalTransport directly.
1209
        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.
1210
        self.assertEquals(k.get_lines('text-1'), ['a\n',    'b'  ])
1211
        self.assertEquals(k.get_lines('text-2'), ['a\rb\n', 'b\n'])
1212
1213
    def test_delta(self):
1214
        """Expression of knit delta as lines"""
1563.2.16 by Robert Collins
Change WeaveStore into VersionedFileStore and make its versoined file class parameterisable.
1215
        k = self.make_test_knit()
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1216
        td = list(line_delta(TEXT_1.splitlines(True),
1217
                             TEXT_1A.splitlines(True)))
1218
        self.assertEqualDiff(''.join(td), delta_1_1a)
1219
        out = apply_line_delta(TEXT_1.splitlines(True), td)
1220
        self.assertEqualDiff(''.join(out), TEXT_1A)
1221
1222
    def test_add_with_parents(self):
1223
        """Store in knit with parents"""
1563.2.16 by Robert Collins
Change WeaveStore into VersionedFileStore and make its versoined file class parameterisable.
1224
        k = self.make_test_knit()
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1225
        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.
1226
        self.assertEqual({'text-1':(), 'text-1a':('text-1',)},
1227
            k.get_parent_map(['text-1', 'text-1a']))
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1228
1229
    def test_ancestry(self):
1230
        """Store in knit with parents"""
1563.2.16 by Robert Collins
Change WeaveStore into VersionedFileStore and make its versoined file class parameterisable.
1231
        k = self.make_test_knit()
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1232
        self.add_stock_one_and_one_a(k)
1233
        self.assertEquals(set(k.get_ancestry(['text-1a'])), set(['text-1a', 'text-1']))
1234
1235
    def test_add_delta(self):
1236
        """Store in knit with parents"""
1685.1.39 by John Arbash Meinel
Updating test_knit to not instantiate a LocalTransport directly.
1237
        k = KnitVersionedFile('test', get_transport('.'), factory=KnitPlainFactory(),
1563.2.25 by Robert Collins
Merge in upstream.
1238
            delta=True, create=True)
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1239
        self.add_stock_one_and_one_a(k)
1596.2.7 by Robert Collins
Remove the requirement for reannotation in knit joins.
1240
        k.clear_cache()
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1241
        self.assertEqualDiff(''.join(k.get_lines('text-1a')), TEXT_1A)
1242
2592.3.21 by Robert Collins
Test using a KnitGraphIndex for storage.
1243
    def test_add_delta_knit_graph_index(self):
1244
        """Does adding work with a KnitGraphIndex."""
1245
        index = InMemoryGraphIndex(2)
1246
        knit_index = KnitGraphIndex(index, add_callback=index.add_nodes,
1247
            deltas=True)
1248
        k = KnitVersionedFile('test', get_transport('.'),
1249
            delta=True, create=True, index=knit_index)
1250
        self.add_stock_one_and_one_a(k)
1251
        k.clear_cache()
1252
        self.assertEqualDiff(''.join(k.get_lines('text-1a')), TEXT_1A)
1253
        # check the index had the right data added.
1254
        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.
1255
            (index, ('text-1', ), ' 0 127', ((), ())),
1256
            (index, ('text-1a', ), ' 127 140', ((('text-1', ),), (('text-1', ),))),
2592.3.21 by Robert Collins
Test using a KnitGraphIndex for storage.
1257
            ]), set(index.iter_all_entries()))
1258
        # we should not have a .kndx file
1259
        self.assertFalse(get_transport('.').has('test.kndx'))
1260
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1261
    def test_annotate(self):
1262
        """Annotations"""
1685.1.39 by John Arbash Meinel
Updating test_knit to not instantiate a LocalTransport directly.
1263
        k = KnitVersionedFile('knit', get_transport('.'), factory=KnitAnnotateFactory(),
1563.2.25 by Robert Collins
Merge in upstream.
1264
            delta=True, create=True)
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1265
        self.insert_and_test_small_annotate(k)
1266
1267
    def insert_and_test_small_annotate(self, k):
1268
        """test annotation with k works correctly."""
1269
        k.add_lines('text-1', [], ['a\n', 'b\n'])
1270
        k.add_lines('text-2', ['text-1'], ['a\n', 'c\n'])
1271
1272
        origins = k.annotate('text-2')
1273
        self.assertEquals(origins[0], ('text-1', 'a\n'))
1274
        self.assertEquals(origins[1], ('text-2', 'c\n'))
1275
1276
    def test_annotate_fulltext(self):
1277
        """Annotations"""
1685.1.39 by John Arbash Meinel
Updating test_knit to not instantiate a LocalTransport directly.
1278
        k = KnitVersionedFile('knit', get_transport('.'), factory=KnitAnnotateFactory(),
1563.2.25 by Robert Collins
Merge in upstream.
1279
            delta=False, create=True)
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1280
        self.insert_and_test_small_annotate(k)
1281
1282
    def test_annotate_merge_1(self):
1563.2.16 by Robert Collins
Change WeaveStore into VersionedFileStore and make its versoined file class parameterisable.
1283
        k = self.make_test_knit(True)
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1284
        k.add_lines('text-a1', [], ['a\n', 'b\n'])
1285
        k.add_lines('text-a2', [], ['d\n', 'c\n'])
1286
        k.add_lines('text-am', ['text-a1', 'text-a2'], ['d\n', 'b\n'])
1287
        origins = k.annotate('text-am')
1288
        self.assertEquals(origins[0], ('text-a2', 'd\n'))
1289
        self.assertEquals(origins[1], ('text-a1', 'b\n'))
1290
1291
    def test_annotate_merge_2(self):
1563.2.16 by Robert Collins
Change WeaveStore into VersionedFileStore and make its versoined file class parameterisable.
1292
        k = self.make_test_knit(True)
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1293
        k.add_lines('text-a1', [], ['a\n', 'b\n', 'c\n'])
1294
        k.add_lines('text-a2', [], ['x\n', 'y\n', 'z\n'])
1295
        k.add_lines('text-am', ['text-a1', 'text-a2'], ['a\n', 'y\n', 'c\n'])
1296
        origins = k.annotate('text-am')
1297
        self.assertEquals(origins[0], ('text-a1', 'a\n'))
1298
        self.assertEquals(origins[1], ('text-a2', 'y\n'))
1299
        self.assertEquals(origins[2], ('text-a1', 'c\n'))
1300
1301
    def test_annotate_merge_9(self):
1563.2.16 by Robert Collins
Change WeaveStore into VersionedFileStore and make its versoined file class parameterisable.
1302
        k = self.make_test_knit(True)
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1303
        k.add_lines('text-a1', [], ['a\n', 'b\n', 'c\n'])
1304
        k.add_lines('text-a2', [], ['x\n', 'y\n', 'z\n'])
1305
        k.add_lines('text-am', ['text-a1', 'text-a2'], ['k\n', 'y\n', 'c\n'])
1306
        origins = k.annotate('text-am')
1307
        self.assertEquals(origins[0], ('text-am', 'k\n'))
1308
        self.assertEquals(origins[1], ('text-a2', 'y\n'))
1309
        self.assertEquals(origins[2], ('text-a1', 'c\n'))
1310
1311
    def test_annotate_merge_3(self):
1563.2.16 by Robert Collins
Change WeaveStore into VersionedFileStore and make its versoined file class parameterisable.
1312
        k = self.make_test_knit(True)
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1313
        k.add_lines('text-a1', [], ['a\n', 'b\n', 'c\n'])
1314
        k.add_lines('text-a2', [] ,['x\n', 'y\n', 'z\n'])
1315
        k.add_lines('text-am', ['text-a1', 'text-a2'], ['k\n', 'y\n', 'z\n'])
1316
        origins = k.annotate('text-am')
1317
        self.assertEquals(origins[0], ('text-am', 'k\n'))
1318
        self.assertEquals(origins[1], ('text-a2', 'y\n'))
1319
        self.assertEquals(origins[2], ('text-a2', 'z\n'))
1320
1321
    def test_annotate_merge_4(self):
1563.2.16 by Robert Collins
Change WeaveStore into VersionedFileStore and make its versoined file class parameterisable.
1322
        k = self.make_test_knit(True)
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1323
        k.add_lines('text-a1', [], ['a\n', 'b\n', 'c\n'])
1324
        k.add_lines('text-a2', [], ['x\n', 'y\n', 'z\n'])
1325
        k.add_lines('text-a3', ['text-a1'], ['a\n', 'b\n', 'p\n'])
1326
        k.add_lines('text-am', ['text-a2', 'text-a3'], ['a\n', 'b\n', 'z\n'])
1327
        origins = k.annotate('text-am')
1328
        self.assertEquals(origins[0], ('text-a1', 'a\n'))
1329
        self.assertEquals(origins[1], ('text-a1', 'b\n'))
1330
        self.assertEquals(origins[2], ('text-a2', 'z\n'))
1331
1332
    def test_annotate_merge_5(self):
1563.2.16 by Robert Collins
Change WeaveStore into VersionedFileStore and make its versoined file class parameterisable.
1333
        k = self.make_test_knit(True)
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1334
        k.add_lines('text-a1', [], ['a\n', 'b\n', 'c\n'])
1335
        k.add_lines('text-a2', [], ['d\n', 'e\n', 'f\n'])
1336
        k.add_lines('text-a3', [], ['x\n', 'y\n', 'z\n'])
1337
        k.add_lines('text-am',
1338
                    ['text-a1', 'text-a2', 'text-a3'],
1339
                    ['a\n', 'e\n', 'z\n'])
1340
        origins = k.annotate('text-am')
1341
        self.assertEquals(origins[0], ('text-a1', 'a\n'))
1342
        self.assertEquals(origins[1], ('text-a2', 'e\n'))
1343
        self.assertEquals(origins[2], ('text-a3', 'z\n'))
1344
1345
    def test_annotate_file_cherry_pick(self):
1563.2.16 by Robert Collins
Change WeaveStore into VersionedFileStore and make its versoined file class parameterisable.
1346
        k = self.make_test_knit(True)
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1347
        k.add_lines('text-1', [], ['a\n', 'b\n', 'c\n'])
1348
        k.add_lines('text-2', ['text-1'], ['d\n', 'e\n', 'f\n'])
1349
        k.add_lines('text-3', ['text-2', 'text-1'], ['a\n', 'b\n', 'c\n'])
1350
        origins = k.annotate('text-3')
1351
        self.assertEquals(origins[0], ('text-1', 'a\n'))
1352
        self.assertEquals(origins[1], ('text-1', 'b\n'))
1353
        self.assertEquals(origins[2], ('text-1', 'c\n'))
1354
2851.4.6 by Ian Clatworthy
review tweaks
1355
    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
1356
        k1 = KnitVersionedFile('test1', get_transport('.'), factory=k1_factory, create=True)
1357
        k1.add_lines('text-a', [], ['a1\n', 'a2\n', 'a3\n'])
1358
        k1.add_lines('text-b', ['text-a'], ['a1\n', 'b2\n', 'a3\n'])
1359
        k1.add_lines('text-c', [], ['c1\n', 'c2\n', 'c3\n'])
1360
        k1.add_lines('text-d', ['text-c'], ['c1\n', 'd2\n', 'd3\n'])
1361
        k1.add_lines('text-m', ['text-b', 'text-d'], ['a1\n', 'b2\n', 'd3\n'])
1362
        k2 = KnitVersionedFile('test2', get_transport('.'), factory=k2_factory, create=True)
2851.4.6 by Ian Clatworthy
review tweaks
1363
        count = k2.join(k1, version_ids=['text-m'])
1364
        self.assertEquals(count, 5)
1365
        self.assertTrue(k2.has_version('text-a'))
1366
        self.assertTrue(k2.has_version('text-c'))
1367
        origins = k2.annotate('text-m')
1368
        self.assertEquals(origins[0], ('text-a', 'a1\n'))
1369
        self.assertEquals(origins[1], ('text-b', 'b2\n'))
1370
        self.assertEquals(origins[2], ('text-d', 'd3\n'))
2851.4.1 by Ian Clatworthy
Support joining plain knits to annotated knits and vice versa
1371
1372
    def test_knit_join_plain_to_plain(self):
1373
        """Test joining a plain knit with a plain knit."""
2851.4.6 by Ian Clatworthy
review tweaks
1374
        self._test_join_with_factories(KnitPlainFactory(), KnitPlainFactory())
2851.4.1 by Ian Clatworthy
Support joining plain knits to annotated knits and vice versa
1375
1376
    def test_knit_join_anno_to_anno(self):
1377
        """Test joining an annotated knit with an annotated knit."""
2851.4.6 by Ian Clatworthy
review tweaks
1378
        self._test_join_with_factories(None, None)
2851.4.1 by Ian Clatworthy
Support joining plain knits to annotated knits and vice versa
1379
1380
    def test_knit_join_anno_to_plain(self):
1381
        """Test joining an annotated knit with a plain knit."""
2851.4.6 by Ian Clatworthy
review tweaks
1382
        self._test_join_with_factories(None, KnitPlainFactory())
2851.4.1 by Ian Clatworthy
Support joining plain knits to annotated knits and vice versa
1383
1384
    def test_knit_join_plain_to_anno(self):
1385
        """Test joining a plain knit with an annotated knit."""
2851.4.6 by Ian Clatworthy
review tweaks
1386
        self._test_join_with_factories(KnitPlainFactory(), None)
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1387
1388
    def test_reannotate(self):
1685.1.39 by John Arbash Meinel
Updating test_knit to not instantiate a LocalTransport directly.
1389
        k1 = KnitVersionedFile('knit1', get_transport('.'),
1563.2.25 by Robert Collins
Merge in upstream.
1390
                               factory=KnitAnnotateFactory(), create=True)
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1391
        # 0
1392
        k1.add_lines('text-a', [], ['a\n', 'b\n'])
1393
        # 1
1394
        k1.add_lines('text-b', ['text-a'], ['a\n', 'c\n'])
1395
1685.1.39 by John Arbash Meinel
Updating test_knit to not instantiate a LocalTransport directly.
1396
        k2 = KnitVersionedFile('test2', get_transport('.'),
1563.2.25 by Robert Collins
Merge in upstream.
1397
                               factory=KnitAnnotateFactory(), create=True)
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1398
        k2.join(k1, version_ids=['text-b'])
1399
1400
        # 2
1401
        k1.add_lines('text-X', ['text-b'], ['a\n', 'b\n'])
1402
        # 2
1403
        k2.add_lines('text-c', ['text-b'], ['z\n', 'c\n'])
1404
        # 3
1405
        k2.add_lines('text-Y', ['text-b'], ['b\n', 'c\n'])
1406
1407
        # test-c will have index 3
1408
        k1.join(k2, version_ids=['text-c'])
1409
1410
        lines = k1.get_lines('text-c')
1411
        self.assertEquals(lines, ['z\n', 'c\n'])
1412
1413
        origins = k1.annotate('text-c')
1594.2.24 by Robert Collins
Make use of the transaction finalisation warning support to implement in-knit caching.
1414
        self.assertEquals(origins[0], ('text-c', 'z\n'))
1415
        self.assertEquals(origins[1], ('text-b', 'c\n'))
1416
1756.3.4 by Aaron Bentley
Fix bug getting texts when line deltas were reused
1417
    def test_get_line_delta_texts(self):
1418
        """Make sure we can call get_texts on text with reused line deltas"""
1419
        k1 = KnitVersionedFile('test1', get_transport('.'), 
1420
                               factory=KnitPlainFactory(), create=True)
1421
        for t in range(3):
1422
            if t == 0:
1423
                parents = []
1424
            else:
1425
                parents = ['%d' % (t-1)]
1426
            k1.add_lines('%d' % t, parents, ['hello\n'] * t)
1427
        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.
1428
        
1429
    def test_iter_lines_reads_in_order(self):
2745.5.3 by Robert Collins
* Move transport logging into a new transport class
1430
        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.
1431
        k1 = KnitVersionedFile('id', instrumented_t, create=True, delta=True)
2745.5.3 by Robert Collins
* Move transport logging into a new transport class
1432
        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.
1433
        # add texts with no required ordering
1434
        k1.add_lines('base', [], ['text\n'])
1435
        k1.add_lines('base2', [], ['text2\n'])
1436
        k1.clear_cache()
2745.5.4 by Robert Collins
Review feedback.
1437
        # clear the logged activity, but preserve the list instance in case of
1438
        # clones pointing at it.
2745.5.3 by Robert Collins
* Move transport logging into a new transport class
1439
        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.
1440
        # request a last-first iteration
2745.5.3 by Robert Collins
* Move transport logging into a new transport class
1441
        results = list(k1.iter_lines_added_or_present_in_versions(
1442
            ['base2', 'base']))
2745.5.6 by Robert Collins
Fix knit test fallout from final readv api change.
1443
        self.assertEqual(
1444
            [('readv', 'id.knit', [(0, 87), (87, 89)], False, None)],
2745.5.3 by Robert Collins
* Move transport logging into a new transport class
1445
            instrumented_t._activity)
2975.3.1 by Robert Collins
Change (without backwards compatibility) the
1446
        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.
1447
1563.2.13 by Robert Collins
InterVersionedFile implemented.
1448
    def test_create_empty_annotated(self):
1563.2.16 by Robert Collins
Change WeaveStore into VersionedFileStore and make its versoined file class parameterisable.
1449
        k1 = self.make_test_knit(True)
1563.2.13 by Robert Collins
InterVersionedFile implemented.
1450
        # 0
1451
        k1.add_lines('text-a', [], ['a\n', 'b\n'])
1452
        k2 = k1.create_empty('t', MemoryTransport())
1453
        self.assertTrue(isinstance(k2.factory, KnitAnnotateFactory))
1454
        self.assertEqual(k1.delta, k2.delta)
1455
        # the generic test checks for empty content and file class
1456
1641.1.2 by Robert Collins
Change knit index files to be robust in the presence of partial writes.
1457
    def test_knit_format(self):
1458
        # this tests that a new knit index file has the expected content
1459
        # and that is writes the data we expect as records are added.
1460
        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
1461
        # Now knit files are not created until we first add data to them
1666.1.6 by Robert Collins
Make knit the default format.
1462
        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.
1463
        knit.add_lines_with_ghosts('revid', ['a_ghost'], ['a\n'])
1464
        self.assertFileEqual(
1666.1.6 by Robert Collins
Make knit the default format.
1465
            "# bzr knit index 8\n"
1641.1.2 by Robert Collins
Change knit index files to be robust in the presence of partial writes.
1466
            "\n"
1467
            "revid fulltext 0 84 .a_ghost :",
1468
            'test.kndx')
1469
        knit.add_lines_with_ghosts('revid2', ['revid'], ['a\n'])
1470
        self.assertFileEqual(
1666.1.6 by Robert Collins
Make knit the default format.
1471
            "# bzr knit index 8\n"
1641.1.2 by Robert Collins
Change knit index files to be robust in the presence of partial writes.
1472
            "\nrevid fulltext 0 84 .a_ghost :"
1473
            "\nrevid2 line-delta 84 82 0 :",
1474
            'test.kndx')
1475
        # 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.
1476
        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.
1477
        self.assertEqual(['revid', 'revid2'], knit.versions())
1478
        # 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
1479
        indexfile = file('test.kndx', 'ab')
1641.1.2 by Robert Collins
Change knit index files to be robust in the presence of partial writes.
1480
        indexfile.write('\nrevid3 line-delta 166 82 1 2 3 4 5 .phwoar:demo ')
1481
        indexfile.close()
1482
        # 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.
1483
        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.
1484
        self.assertEqual(['revid', 'revid2'], knit.versions())
1485
        # and add a revision with the same id the failed write had
1486
        knit.add_lines('revid3', ['revid2'], ['a\n'])
1487
        # and when reading it revid3 should now appear.
1685.1.39 by John Arbash Meinel
Updating test_knit to not instantiate a LocalTransport directly.
1488
        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.
1489
        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.
1490
        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.
1491
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
1492
    def test_delay_create(self):
1493
        """Test that passing delay_create=True creates files late"""
1494
        knit = self.make_test_knit(annotate=True, delay_create=True)
1495
        self.failIfExists('test.knit')
1496
        self.failIfExists('test.kndx')
1497
        knit.add_lines_with_ghosts('revid', ['a_ghost'], ['a\n'])
1498
        self.failUnlessExists('test.knit')
1499
        self.assertFileEqual(
1500
            "# bzr knit index 8\n"
1501
            "\n"
1502
            "revid fulltext 0 84 .a_ghost :",
1503
            'test.kndx')
1504
1946.2.2 by John Arbash Meinel
test delay_create does the right thing
1505
    def test_create_parent_dir(self):
1506
        """create_parent_dir can create knits in nonexistant dirs"""
1507
        # Has no effect if we don't set 'delay_create'
1508
        trans = get_transport('.')
1509
        self.assertRaises(NoSuchFile, KnitVersionedFile, 'dir/test',
1510
                          trans, access_mode='w', factory=None,
1511
                          create=True, create_parent_dir=True)
1512
        # Nothing should have changed yet
1513
        knit = KnitVersionedFile('dir/test', trans, access_mode='w',
1514
                                 factory=None, create=True,
1515
                                 create_parent_dir=True,
1516
                                 delay_create=True)
1517
        self.failIfExists('dir/test.knit')
1518
        self.failIfExists('dir/test.kndx')
1519
        self.failIfExists('dir')
1520
        knit.add_lines('revid', [], ['a\n'])
1521
        self.failUnlessExists('dir')
1522
        self.failUnlessExists('dir/test.knit')
1523
        self.assertFileEqual(
1524
            "# bzr knit index 8\n"
1525
            "\n"
1526
            "revid fulltext 0 84  :",
1527
            'dir/test.kndx')
1528
1946.2.13 by John Arbash Meinel
Test that passing modes does the right thing for knits.
1529
    def test_create_mode_700(self):
1530
        trans = get_transport('.')
1531
        if not trans._can_roundtrip_unix_modebits():
1532
            # Can't roundtrip, so no need to run this test
1533
            return
1534
        knit = KnitVersionedFile('dir/test', trans, access_mode='w',
1535
                                 factory=None, create=True,
1536
                                 create_parent_dir=True,
1537
                                 delay_create=True,
1538
                                 file_mode=0600,
1539
                                 dir_mode=0700)
1540
        knit.add_lines('revid', [], ['a\n'])
1541
        self.assertTransportMode(trans, 'dir', 0700)
1542
        self.assertTransportMode(trans, 'dir/test.knit', 0600)
1543
        self.assertTransportMode(trans, 'dir/test.kndx', 0600)
1544
1545
    def test_create_mode_770(self):
1546
        trans = get_transport('.')
1547
        if not trans._can_roundtrip_unix_modebits():
1548
            # Can't roundtrip, so no need to run this test
1549
            return
1550
        knit = KnitVersionedFile('dir/test', trans, access_mode='w',
1551
                                 factory=None, create=True,
1552
                                 create_parent_dir=True,
1553
                                 delay_create=True,
1554
                                 file_mode=0660,
1555
                                 dir_mode=0770)
1556
        knit.add_lines('revid', [], ['a\n'])
1557
        self.assertTransportMode(trans, 'dir', 0770)
1558
        self.assertTransportMode(trans, 'dir/test.knit', 0660)
1559
        self.assertTransportMode(trans, 'dir/test.kndx', 0660)
1560
1561
    def test_create_mode_777(self):
1562
        trans = get_transport('.')
1563
        if not trans._can_roundtrip_unix_modebits():
1564
            # Can't roundtrip, so no need to run this test
1565
            return
1566
        knit = KnitVersionedFile('dir/test', trans, access_mode='w',
1567
                                 factory=None, create=True,
1568
                                 create_parent_dir=True,
1569
                                 delay_create=True,
1570
                                 file_mode=0666,
1571
                                 dir_mode=0777)
1572
        knit.add_lines('revid', [], ['a\n'])
1573
        self.assertTransportMode(trans, 'dir', 0777)
1574
        self.assertTransportMode(trans, 'dir/test.knit', 0666)
1575
        self.assertTransportMode(trans, 'dir/test.kndx', 0666)
1576
1664.2.1 by Aaron Bentley
Start work on plan_merge test
1577
    def test_plan_merge(self):
1578
        my_knit = self.make_test_knit(annotate=True)
1579
        my_knit.add_lines('text1', [], split_lines(TEXT_1))
1580
        my_knit.add_lines('text1a', ['text1'], split_lines(TEXT_1A))
1581
        my_knit.add_lines('text1b', ['text1'], split_lines(TEXT_1B))
1664.2.3 by Aaron Bentley
Add failing test case
1582
        plan = list(my_knit.plan_merge('text1a', 'text1b'))
1664.2.6 by Aaron Bentley
Got plan-merge passing tests
1583
        for plan_line, expected_line in zip(plan, AB_MERGE):
1584
            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.
1585
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1586
    def test_get_stream_empty(self):
1587
        """Get a data stream for an empty knit file."""
1588
        k1 = self.make_test_knit()
1589
        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.
1590
        self.assertEqual('knit-plain', format)
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1591
        self.assertEqual([], data_list)
1592
        content = reader_callable(None)
1593
        self.assertEqual('', content)
1594
        self.assertIsInstance(content, str)
1595
1596
    def test_get_stream_one_version(self):
1597
        """Get a data stream for a single record out of a knit containing just
1598
        one record.
1599
        """
1600
        k1 = self.make_test_knit()
1601
        test_data = [
1602
            ('text-a', [], TEXT_1),
1603
            ]
1604
        expected_data_list = [
1605
            # 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.
1606
            ('text-a', ['fulltext'], 122, ()),
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1607
           ]
1608
        for version_id, parents, lines in test_data:
1609
            k1.add_lines(version_id, parents, split_lines(lines))
1610
1611
        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.
1612
        self.assertEqual('knit-plain', format)
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1613
        self.assertEqual(expected_data_list, data_list)
1614
        # There's only one record in the knit, so the content should be the
1615
        # entire knit data file's contents.
2535.3.36 by Andrew Bennetts
Merge bzr.dev
1616
        self.assertEqual(k1.transport.get_bytes(k1._data._access._filename),
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1617
                         reader_callable(None))
1618
        
1619
    def test_get_stream_get_one_version_of_many(self):
1620
        """Get a data stream for just one version out of a knit containing many
1621
        versions.
1622
        """
1623
        k1 = self.make_test_knit()
1624
        # Insert the same data as test_knit_join, as they seem to cover a range
1625
        # of cases (no parents, one parent, multiple parents).
1626
        test_data = [
1627
            ('text-a', [], TEXT_1),
1628
            ('text-b', ['text-a'], TEXT_1),
1629
            ('text-c', [], TEXT_1),
1630
            ('text-d', ['text-c'], TEXT_1),
1631
            ('text-m', ['text-b', 'text-d'], TEXT_1),
1632
            ]
1633
        expected_data_list = [
1634
            # 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.
1635
            ('text-m', ['line-delta'], 84, ('text-b', 'text-d')),
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1636
            ]
1637
        for version_id, parents, lines in test_data:
1638
            k1.add_lines(version_id, parents, split_lines(lines))
1639
1640
        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.
1641
        self.assertEqual('knit-plain', format)
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1642
        self.assertEqual(expected_data_list, data_list)
1643
        self.assertRecordContentEqual(k1, 'text-m', reader_callable(None))
1644
        
3023.2.2 by Martin Pool
Fix KnitVersionedFile.get_data_stream to not assume .versions() is sorted. (lp:165106)
1645
    def test_get_data_stream_unordered_index(self):
1646
        """Get a data stream when the knit index reports versions out of order.
1647
1648
        https://bugs.launchpad.net/bzr/+bug/164637
1649
        """
1650
        k1 = self.make_test_knit()
1651
        test_data = [
1652
            ('text-a', [], TEXT_1),
1653
            ('text-b', ['text-a'], TEXT_1),
1654
            ('text-c', [], TEXT_1),
1655
            ('text-d', ['text-c'], TEXT_1),
1656
            ('text-m', ['text-b', 'text-d'], TEXT_1),
1657
            ]
1658
        for version_id, parents, lines in test_data:
1659
            k1.add_lines(version_id, parents, split_lines(lines))
1660
        # monkey-patch versions method to return out of order, as if coming
1661
        # from multiple independently indexed packs
1662
        original_versions = k1.versions
1663
        k1.versions = lambda: reversed(original_versions())
1664
        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.
1665
            ('text-a', ['fulltext'], 122, ()),
1666
            ('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)
1667
        # now check the fulltext is first and the delta second
1668
        format, data_list, _ = k1.get_data_stream(['text-a', 'text-b'])
1669
        self.assertEqual('knit-plain', format)
1670
        self.assertEqual(expected_data_list, data_list)
1671
        # and that's true if we ask for them in the opposite order too
1672
        format, data_list, _ = k1.get_data_stream(['text-b', 'text-a'])
1673
        self.assertEqual(expected_data_list, data_list)
1674
        # also try requesting more versions
1675
        format, data_list, _ = k1.get_data_stream([
1676
            'text-m', 'text-b', 'text-a'])
1677
        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.
1678
            ('text-a', ['fulltext'], 122, ()),
1679
            ('text-b', ['line-delta'], 84, ('text-a',)),
1680
            ('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)
1681
            ], data_list)
1682
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1683
    def test_get_stream_ghost_parent(self):
1684
        """Get a data stream for a version with a ghost parent."""
1685
        k1 = self.make_test_knit()
1686
        # Test data
1687
        k1.add_lines('text-a', [], split_lines(TEXT_1))
1688
        k1.add_lines_with_ghosts('text-b', ['text-a', 'text-ghost'],
1689
                                 split_lines(TEXT_1))
1690
        # Expected data
1691
        expected_data_list = [
1692
            # 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.
1693
            ('text-b', ['line-delta'], 84, ('text-a', 'text-ghost')),
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1694
            ]
1695
        
1696
        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.
1697
        self.assertEqual('knit-plain', format)
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1698
        self.assertEqual(expected_data_list, data_list)
1699
        self.assertRecordContentEqual(k1, 'text-b', reader_callable(None))
1700
    
1701
    def test_get_stream_get_multiple_records(self):
1702
        """Get a stream for multiple records of a knit."""
1703
        k1 = self.make_test_knit()
1704
        # Insert the same data as test_knit_join, as they seem to cover a range
1705
        # of cases (no parents, one parent, multiple parents).
1706
        test_data = [
1707
            ('text-a', [], TEXT_1),
1708
            ('text-b', ['text-a'], TEXT_1),
1709
            ('text-c', [], TEXT_1),
1710
            ('text-d', ['text-c'], TEXT_1),
1711
            ('text-m', ['text-b', 'text-d'], TEXT_1),
1712
            ]
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
1713
        for version_id, parents, lines in test_data:
1714
            k1.add_lines(version_id, parents, split_lines(lines))
1715
1716
        # This test is actually a bit strict as the order in which they're
1717
        # returned is not defined.  This matches the current (deterministic)
1718
        # behaviour.
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1719
        expected_data_list = [
1720
            # 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.
1721
            ('text-d', ['line-delta'], 84, ('text-c',)),
1722
            ('text-b', ['line-delta'], 84, ('text-a',)),
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1723
            ]
1724
        # Note that even though we request the revision IDs in a particular
1725
        # order, the data stream may return them in any order it likes.  In this
1726
        # case, they'll be in the order they were inserted into the knit.
1727
        format, data_list, reader_callable = k1.get_data_stream(
1728
            ['text-d', 'text-b'])
2535.3.17 by Andrew Bennetts
[broken] Closer to a working Repository.fetch_revisions smart request.
1729
        self.assertEqual('knit-plain', format)
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1730
        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
1731
        # must match order they're returned
1732
        self.assertRecordContentEqual(k1, 'text-d', reader_callable(84))
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1733
        self.assertRecordContentEqual(k1, 'text-b', reader_callable(84))
1734
        self.assertEqual('', reader_callable(None),
1735
                         "There should be no more bytes left to read.")
1736
1737
    def test_get_stream_all(self):
1738
        """Get a data stream for all the records in a knit.
1739
1740
        This exercises fulltext records, line-delta records, records with
1741
        various numbers of parents, and reading multiple records out of the
1742
        callable.  These cases ought to all be exercised individually by the
1743
        other test_get_stream_* tests; this test is basically just paranoia.
1744
        """
1745
        k1 = self.make_test_knit()
1746
        # Insert the same data as test_knit_join, as they seem to cover a range
1747
        # of cases (no parents, one parent, multiple parents).
1748
        test_data = [
1749
            ('text-a', [], TEXT_1),
1750
            ('text-b', ['text-a'], TEXT_1),
1751
            ('text-c', [], TEXT_1),
1752
            ('text-d', ['text-c'], TEXT_1),
1753
            ('text-m', ['text-b', 'text-d'], TEXT_1),
1754
           ]
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
1755
        for version_id, parents, lines in test_data:
1756
            k1.add_lines(version_id, parents, split_lines(lines))
1757
1758
        # This test is actually a bit strict as the order in which they're
1759
        # returned is not defined.  This matches the current (deterministic)
1760
        # behaviour.
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1761
        expected_data_list = [
1762
            # 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.
1763
            ('text-a', ['fulltext'], 122, ()),
1764
            ('text-b', ['line-delta'], 84, ('text-a',)),
1765
            ('text-m', ['line-delta'], 84, ('text-b', 'text-d')),
1766
            ('text-c', ['fulltext'], 121, ()),
1767
            ('text-d', ['line-delta'], 84, ('text-c',)),
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1768
            ]
1769
        format, data_list, reader_callable = k1.get_data_stream(
1770
            ['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.
1771
        self.assertEqual('knit-plain', format)
2535.3.3 by Andrew Bennetts
Add Knit.get_data_stream.
1772
        self.assertEqual(expected_data_list, data_list)
1773
        for version_id, options, length, parents in expected_data_list:
1774
            bytes = reader_callable(length)
1775
            self.assertRecordContentEqual(k1, version_id, bytes)
1776
2535.3.4 by Andrew Bennetts
Simple implementation of Knit.insert_data_stream.
1777
    def assertKnitFilesEqual(self, knit1, knit2):
1778
        """Assert that the contents of the index and data files of two knits are
1779
        equal.
1780
        """
1781
        self.assertEqual(
2535.3.36 by Andrew Bennetts
Merge bzr.dev
1782
            knit1.transport.get_bytes(knit1._data._access._filename),
1783
            knit2.transport.get_bytes(knit2._data._access._filename))
2535.3.4 by Andrew Bennetts
Simple implementation of Knit.insert_data_stream.
1784
        self.assertEqual(
1785
            knit1.transport.get_bytes(knit1._index._filename),
1786
            knit2.transport.get_bytes(knit2._index._filename))
1787
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
1788
    def assertKnitValuesEqual(self, left, right):
3052.2.5 by Andrew Bennetts
Address the rest of the review comments from John and myself.
1789
        """Assert that the texts, annotations and graph of left and right are
1790
        the same.
1791
        """
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
1792
        self.assertEqual(set(left.versions()), set(right.versions()))
1793
        for version in left.versions():
1794
            self.assertEqual(left.get_parents_with_ghosts(version),
1795
                right.get_parents_with_ghosts(version))
1796
            self.assertEqual(left.get_lines(version),
1797
                right.get_lines(version))
1798
            self.assertEqual(left.annotate(version),
1799
                right.annotate(version))
1800
2535.3.4 by Andrew Bennetts
Simple implementation of Knit.insert_data_stream.
1801
    def test_insert_data_stream_empty(self):
1802
        """Inserting a data stream with no records should not put any data into
1803
        the knit.
1804
        """
1805
        k1 = self.make_test_knit()
1806
        k1.insert_data_stream(
1807
            (k1.get_format_signature(), [], lambda ignored: ''))
2535.3.36 by Andrew Bennetts
Merge bzr.dev
1808
        self.assertEqual('', k1.transport.get_bytes(k1._data._access._filename),
2535.3.4 by Andrew Bennetts
Simple implementation of Knit.insert_data_stream.
1809
                         "The .knit should be completely empty.")
1810
        self.assertEqual(k1._index.HEADER,
1811
                         k1.transport.get_bytes(k1._index._filename),
1812
                         "The .kndx should have nothing apart from the header.")
1813
1814
    def test_insert_data_stream_one_record(self):
1815
        """Inserting a data stream with one record from a knit with one record
1816
        results in byte-identical files.
1817
        """
1818
        source = self.make_test_knit(name='source')
1819
        source.add_lines('text-a', [], split_lines(TEXT_1))
1820
        data_stream = source.get_data_stream(['text-a'])
1821
        target = self.make_test_knit(name='target')
1822
        target.insert_data_stream(data_stream)
1823
        self.assertKnitFilesEqual(source, target)
1824
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
1825
    def test_insert_data_stream_annotated_unannotated(self):
1826
        """Inserting an annotated datastream to an unannotated knit works."""
1827
        # case one - full texts.
1828
        source = self.make_test_knit(name='source', annotate=True)
1829
        target = self.make_test_knit(name='target', annotate=False)
1830
        source.add_lines('text-a', [], split_lines(TEXT_1))
1831
        target.insert_data_stream(source.get_data_stream(['text-a']))
1832
        self.assertKnitValuesEqual(source, target)
1833
        # case two - deltas.
1834
        source.add_lines('text-b', ['text-a'], split_lines(TEXT_2))
1835
        target.insert_data_stream(source.get_data_stream(['text-b']))
1836
        self.assertKnitValuesEqual(source, target)
1837
3052.2.3 by Robert Collins
Handle insert_data_stream of an unannotated stream into an annotated knit.
1838
    def test_insert_data_stream_unannotated_annotated(self):
1839
        """Inserting an unannotated datastream to an annotated knit works."""
1840
        # case one - full texts.
1841
        source = self.make_test_knit(name='source', annotate=False)
1842
        target = self.make_test_knit(name='target', annotate=True)
1843
        source.add_lines('text-a', [], split_lines(TEXT_1))
1844
        target.insert_data_stream(source.get_data_stream(['text-a']))
1845
        self.assertKnitValuesEqual(source, target)
1846
        # case two - deltas.
1847
        source.add_lines('text-b', ['text-a'], split_lines(TEXT_2))
1848
        target.insert_data_stream(source.get_data_stream(['text-b']))
1849
        self.assertKnitValuesEqual(source, target)
1850
2535.3.4 by Andrew Bennetts
Simple implementation of Knit.insert_data_stream.
1851
    def test_insert_data_stream_records_already_present(self):
1852
        """Insert a data stream where some records are alreday present in the
1853
        target, and some not.  Only the new records are inserted.
1854
        """
1855
        source = self.make_test_knit(name='source')
1856
        target = self.make_test_knit(name='target')
1857
        # Insert 'text-a' into both source and target
1858
        source.add_lines('text-a', [], split_lines(TEXT_1))
1859
        target.insert_data_stream(source.get_data_stream(['text-a']))
1860
        # Insert 'text-b' into just the source.
1861
        source.add_lines('text-b', ['text-a'], split_lines(TEXT_1))
1862
        # Get a data stream of both text-a and text-b, and insert it.
1863
        data_stream = source.get_data_stream(['text-a', 'text-b'])
1864
        target.insert_data_stream(data_stream)
1865
        # The source and target will now be identical.  This means the text-a
1866
        # record was not added a second time.
1867
        self.assertKnitFilesEqual(source, target)
1868
1869
    def test_insert_data_stream_multiple_records(self):
1870
        """Inserting a data stream of all records from a knit with multiple
1871
        records results in byte-identical files.
1872
        """
1873
        source = self.make_test_knit(name='source')
1874
        source.add_lines('text-a', [], split_lines(TEXT_1))
1875
        source.add_lines('text-b', ['text-a'], split_lines(TEXT_1))
1876
        source.add_lines('text-c', [], split_lines(TEXT_1))
1877
        data_stream = source.get_data_stream(['text-a', 'text-b', 'text-c'])
1878
        
1879
        target = self.make_test_knit(name='target')
1880
        target.insert_data_stream(data_stream)
1881
        
1882
        self.assertKnitFilesEqual(source, target)
1883
1884
    def test_insert_data_stream_ghost_parent(self):
1885
        """Insert a data stream with a record that has a ghost parent."""
1886
        # Make a knit with a record, text-a, that has a ghost parent.
1887
        source = self.make_test_knit(name='source')
1888
        source.add_lines_with_ghosts('text-a', ['text-ghost'],
1889
                                     split_lines(TEXT_1))
1890
        data_stream = source.get_data_stream(['text-a'])
1891
1892
        target = self.make_test_knit(name='target')
1893
        target.insert_data_stream(data_stream)
1894
1895
        self.assertKnitFilesEqual(source, target)
1896
1897
        # The target knit object is in a consistent state, i.e. the record we
1898
        # just added is immediately visible.
1899
        self.assertTrue(target.has_version('text-a'))
1900
        self.assertTrue(target.has_ghost('text-ghost'))
1901
        self.assertEqual(split_lines(TEXT_1), target.get_lines('text-a'))
1902
1903
    def test_insert_data_stream_inconsistent_version_lines(self):
1904
        """Inserting a data stream which has different content for a version_id
1905
        than already exists in the knit will raise KnitCorrupt.
1906
        """
1907
        source = self.make_test_knit(name='source')
1908
        target = self.make_test_knit(name='target')
1909
        # Insert a different 'text-a' into both source and target
1910
        source.add_lines('text-a', [], split_lines(TEXT_1))
1911
        target.add_lines('text-a', [], split_lines(TEXT_2))
1912
        # Insert a data stream with conflicting content into the target
1913
        data_stream = source.get_data_stream(['text-a'])
1914
        self.assertRaises(
1915
            errors.KnitCorrupt, target.insert_data_stream, data_stream)
1916
1917
    def test_insert_data_stream_inconsistent_version_parents(self):
1918
        """Inserting a data stream which has different parents for a version_id
1919
        than already exists in the knit will raise KnitCorrupt.
1920
        """
1921
        source = self.make_test_knit(name='source')
1922
        target = self.make_test_knit(name='target')
1923
        # Insert a different 'text-a' into both source and target.  They differ
1924
        # only by the parents list, the content is the same.
1925
        source.add_lines_with_ghosts('text-a', [], split_lines(TEXT_1))
1926
        target.add_lines_with_ghosts('text-a', ['a-ghost'], split_lines(TEXT_1))
1927
        # Insert a data stream with conflicting content into the target
1928
        data_stream = source.get_data_stream(['text-a'])
1929
        self.assertRaises(
1930
            errors.KnitCorrupt, target.insert_data_stream, data_stream)
1931
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
1932
    def test_insert_data_stream_unknown_format(self):
2535.3.4 by Andrew Bennetts
Simple implementation of Knit.insert_data_stream.
1933
        """A data stream in a different format to the target knit cannot be
1934
        inserted.
1935
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
1936
        It will raise KnitDataStreamUnknown because the fallback code will fail
1937
        to make a knit. In future we may need KnitDataStreamIncompatible again,
1938
        for more exotic cases.
2535.3.4 by Andrew Bennetts
Simple implementation of Knit.insert_data_stream.
1939
        """
1940
        data_stream = ('fake-format-signature', [], lambda _: '')
1941
        target = self.make_test_knit(name='target')
1942
        self.assertRaises(
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
1943
            errors.KnitDataStreamUnknown,
2535.3.4 by Andrew Bennetts
Simple implementation of Knit.insert_data_stream.
1944
            target.insert_data_stream, data_stream)
1945
2535.3.5 by Andrew Bennetts
Batch writes as much as possible in insert_data_stream.
1946
    #  * test that a stream of "already present version, then new version"
1947
    #    inserts correctly.
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1948
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
1949
1950
    def assertMadeStreamKnit(self, source_knit, versions, target_knit):
1951
        """Assert that a knit made from a stream is as expected."""
1952
        a_stream = source_knit.get_data_stream(versions)
1953
        expected_data = a_stream[2](None)
1954
        a_stream = source_knit.get_data_stream(versions)
1955
        a_knit = target_knit._knit_from_datastream(a_stream)
1956
        self.assertEqual(source_knit.factory.__class__,
1957
            a_knit.factory.__class__)
1958
        self.assertIsInstance(a_knit._data._access, _StreamAccess)
1959
        self.assertIsInstance(a_knit._index, _StreamIndex)
1960
        self.assertEqual(a_knit._index.data_list, a_stream[1])
1961
        self.assertEqual(a_knit._data._access.data, expected_data)
1962
        self.assertEqual(a_knit.filename, target_knit.filename)
1963
        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.
1964
        self.assertEqual(a_knit._index, a_knit._data._access.stream_index)
1965
        self.assertEqual(target_knit, a_knit._data._access.backing_knit)
1966
        self.assertIsInstance(a_knit._data._access.orig_factory,
1967
            source_knit.factory.__class__)
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
1968
1969
    def test__knit_from_data_stream_empty(self):
1970
        """Create a knit object from a datastream."""
1971
        annotated = self.make_test_knit(name='source', annotate=True)
1972
        plain = self.make_test_knit(name='target', annotate=False)
1973
        # case 1: annotated source
1974
        self.assertMadeStreamKnit(annotated, [], annotated)
1975
        self.assertMadeStreamKnit(annotated, [], plain)
1976
        # case 2: plain source
1977
        self.assertMadeStreamKnit(plain, [], annotated)
1978
        self.assertMadeStreamKnit(plain, [], plain)
1979
1980
    def test__knit_from_data_stream_unknown_format(self):
1981
        annotated = self.make_test_knit(name='source', annotate=True)
1982
        self.assertRaises(errors.KnitDataStreamUnknown,
1983
            annotated._knit_from_datastream, ("unknown", None, None))
1984
1985
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
1986
TEXT_1 = """\
1987
Banana cup cakes:
1988
1989
- bananas
1990
- eggs
1991
- broken tea cups
1992
"""
1993
1994
TEXT_1A = """\
1995
Banana cup cake recipe
1996
(serves 6)
1997
1998
- bananas
1999
- eggs
2000
- broken tea cups
2001
- self-raising flour
2002
"""
2003
1664.2.1 by Aaron Bentley
Start work on plan_merge test
2004
TEXT_1B = """\
2005
Banana cup cake recipe
2006
2007
- bananas (do not use plantains!!!)
2008
- broken tea cups
2009
- flour
2010
"""
2011
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
2012
delta_1_1a = """\
2013
0,1,2
2014
Banana cup cake recipe
2015
(serves 6)
2016
5,5,1
2017
- self-raising flour
2018
"""
2019
2020
TEXT_2 = """\
2021
Boeuf bourguignon
2022
2023
- beef
2024
- red wine
2025
- small onions
2026
- carrot
2027
- mushrooms
2028
"""
2029
1664.2.3 by Aaron Bentley
Add failing test case
2030
AB_MERGE_TEXT="""unchanged|Banana cup cake recipe
2031
new-a|(serves 6)
2032
unchanged|
2033
killed-b|- bananas
2034
killed-b|- eggs
2035
new-b|- bananas (do not use plantains!!!)
2036
unchanged|- broken tea cups
2037
new-a|- self-raising flour
1664.2.6 by Aaron Bentley
Got plan-merge passing tests
2038
new-b|- flour
2039
"""
1664.2.3 by Aaron Bentley
Add failing test case
2040
AB_MERGE=[tuple(l.split('|')) for l in AB_MERGE_TEXT.splitlines(True)]
2041
2042
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
2043
def line_delta(from_lines, to_lines):
2044
    """Generate line-based delta from one text to another"""
2045
    s = difflib.SequenceMatcher(None, from_lines, to_lines)
2046
    for op in s.get_opcodes():
2047
        if op[0] == 'equal':
2048
            continue
2049
        yield '%d,%d,%d\n' % (op[1], op[2], op[4]-op[3])
2050
        for i in range(op[3], op[4]):
2051
            yield to_lines[i]
2052
2053
2054
def apply_line_delta(basis_lines, delta_lines):
2055
    """Apply a line-based perfect diff
2056
    
2057
    basis_lines -- text to apply the patch to
2058
    delta_lines -- diff instructions and content
2059
    """
2060
    out = basis_lines[:]
2061
    i = 0
2062
    offset = 0
2063
    while i < len(delta_lines):
2064
        l = delta_lines[i]
2065
        a, b, c = map(long, l.split(','))
2066
        i = i + 1
2067
        out[offset+a:offset+b] = delta_lines[i:i+c]
2068
        i = i + c
2069
        offset = offset + (b - a) + c
2070
    return out
1684.3.3 by Robert Collins
Add a special cased weaves to knit converter.
2071
2072
2073
class TestWeaveToKnit(KnitTests):
2074
2075
    def test_weave_to_knit_matches(self):
2076
        # check that the WeaveToKnit is_compatible function
2077
        # registers True for a Weave to a Knit.
2078
        w = Weave()
2079
        k = self.make_test_knit()
2080
        self.failUnless(WeaveToKnit.is_compatible(w, k))
2081
        self.failIf(WeaveToKnit.is_compatible(k, w))
2082
        self.failIf(WeaveToKnit.is_compatible(w, w))
2083
        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
2084
2085
2086
class TestKnitCaching(KnitTests):
2087
    
2850.1.1 by Robert Collins
* ``KnitVersionedFile.add*`` will no longer cache added records even when
2088
    def create_knit(self):
1863.1.1 by John Arbash Meinel
Allow Versioned files to do caching if explicitly asked, and implement for Knit
2089
        k = self.make_test_knit(True)
2090
        k.add_lines('text-1', [], split_lines(TEXT_1))
2091
        k.add_lines('text-2', [], split_lines(TEXT_2))
2092
        return k
2093
2094
    def test_no_caching(self):
2095
        k = self.create_knit()
2096
        # Nothing should be cached without setting 'enable_cache'
2097
        self.assertEqual({}, k._data._cache)
2098
2099
    def test_cache_data_read_raw(self):
2100
        k = self.create_knit()
2101
2102
        # Now cache and read
2103
        k.enable_cache()
2104
2105
        def read_one_raw(version):
2106
            pos_map = k._get_components_positions([version])
3224.1.13 by John Arbash Meinel
Revert the _get_component_positions api
2107
            method, index_memo, next = pos_map[version]
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
2108
            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
2109
            self.assertEqual(1, len(lst))
2110
            return lst[0]
2111
2112
        val = read_one_raw('text-1')
1863.1.8 by John Arbash Meinel
Removing disk-backed-cache
2113
        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
2114
2115
        k.clear_cache()
2116
        # After clear, new reads are not cached
2117
        self.assertEqual({}, k._data._cache)
2118
2119
        val2 = read_one_raw('text-1')
2120
        self.assertEqual(val, val2)
2121
        self.assertEqual({}, k._data._cache)
2122
2123
    def test_cache_data_read(self):
2124
        k = self.create_knit()
2125
2126
        def read_one(version):
2127
            pos_map = k._get_components_positions([version])
3224.1.13 by John Arbash Meinel
Revert the _get_component_positions api
2128
            method, index_memo, next = pos_map[version]
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
2129
            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
2130
            self.assertEqual(1, len(lst))
2131
            return lst[0]
2132
2133
        # Now cache and read
2134
        k.enable_cache()
2135
2136
        val = read_one('text-2')
2137
        self.assertEqual(['text-2'], k._data._cache.keys())
2138
        self.assertEqual('text-2', val[0])
2139
        content, digest = k._data._parse_record('text-2',
2140
                                                k._data._cache['text-2'])
2141
        self.assertEqual(content, val[1])
2142
        self.assertEqual(digest, val[2])
2143
2144
        k.clear_cache()
2145
        self.assertEqual({}, k._data._cache)
2146
2147
        val2 = read_one('text-2')
2148
        self.assertEqual(val, val2)
2149
        self.assertEqual({}, k._data._cache)
2150
2151
    def test_cache_read(self):
2152
        k = self.create_knit()
2153
        k.enable_cache()
2154
2155
        text = k.get_text('text-1')
2156
        self.assertEqual(TEXT_1, text)
2157
        self.assertEqual(['text-1'], k._data._cache.keys())
2158
2159
        k.clear_cache()
2160
        self.assertEqual({}, k._data._cache)
2161
2162
        text = k.get_text('text-1')
2163
        self.assertEqual(TEXT_1, text)
2164
        self.assertEqual({}, k._data._cache)
2102.2.1 by John Arbash Meinel
Fix bug #64789 _KnitIndex.add_versions() should dict compress new revisions
2165
2166
2167
class TestKnitIndex(KnitTests):
2168
2169
    def test_add_versions_dictionary_compresses(self):
2170
        """Adding versions to the index should update the lookup dict"""
2171
        knit = self.make_test_knit()
2172
        idx = knit._index
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
2173
        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
2174
        self.check_file_contents('test.kndx',
2175
            '# bzr knit index 8\n'
2176
            '\n'
2177
            'a-1 fulltext 0 0  :'
2178
            )
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
2179
        idx.add_versions([('a-2', ['fulltext'], (None, 0, 0), ['a-1']),
2180
                          ('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
2181
                         ])
2182
        self.check_file_contents('test.kndx',
2183
            '# bzr knit index 8\n'
2184
            '\n'
2185
            'a-1 fulltext 0 0  :\n'
2186
            'a-2 fulltext 0 0 0 :\n'
2187
            'a-3 fulltext 0 0 1 :'
2188
            )
2189
        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.
2190
        self.assertEqual({'a-1':('a-1', ['fulltext'], 0, 0, (), 0),
2191
                          'a-2':('a-2', ['fulltext'], 0, 0, ('a-1',), 1),
2192
                          '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
2193
                         }, idx._cache)
2194
2195
    def test_add_versions_fails_clean(self):
2196
        """If add_versions fails in the middle, it restores a pristine state.
2197
2198
        Any modifications that are made to the index are reset if all versions
2199
        cannot be added.
2200
        """
2201
        # This cheats a little bit by passing in a generator which will
2202
        # raise an exception before the processing finishes
2203
        # Other possibilities would be to have an version with the wrong number
2204
        # of entries, or to make the backing transport unable to write any
2205
        # files.
2206
2207
        knit = self.make_test_knit()
2208
        idx = knit._index
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
2209
        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
2210
2211
        class StopEarly(Exception):
2212
            pass
2213
2214
        def generate_failure():
2215
            """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.
2216
            yield ('a-2', ['fulltext'], (None, 0, 0), ('a-1',))
2217
            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
2218
            raise StopEarly()
2219
2220
        # Assert the pre-condition
2221
        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.
2222
        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
2223
2224
        self.assertRaises(StopEarly, idx.add_versions, generate_failure())
2225
2226
        # And it shouldn't be modified
2227
        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.
2228
        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.
2229
2230
    def test_knit_index_ignores_empty_files(self):
2231
        # There was a race condition in older bzr, where a ^C at the right time
2232
        # could leave an empty .kndx file, which bzr would later claim was a
2233
        # corrupted file since the header was not present. In reality, the file
2234
        # just wasn't created, so it should be ignored.
2235
        t = get_transport('.')
2236
        t.put_bytes('test.kndx', '')
2237
2238
        knit = self.make_test_knit()
2239
2240
    def test_knit_index_checks_header(self):
2241
        t = get_transport('.')
2242
        t.put_bytes('test.kndx', '# not really a knit header\n\n')
2243
2196.2.1 by John Arbash Meinel
Merge Dmitry's optimizations and minimize the actual diff.
2244
        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.
2245
2246
2247
class TestGraphIndexKnit(KnitTests):
2248
    """Tests for knits using a GraphIndex rather than a KnitIndex."""
2249
2250
    def make_g_index(self, name, ref_lists=0, nodes=[]):
2251
        builder = GraphIndexBuilder(ref_lists)
2252
        for node, references, value in nodes:
2253
            builder.add_node(node, references, value)
2254
        stream = builder.finish()
2255
        trans = self.get_transport()
2890.2.1 by Robert Collins
* ``bzrlib.index.GraphIndex`` now requires a size parameter to the
2256
        size = trans.put_file(name, stream)
2257
        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.
2258
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2259
    def two_graph_index(self, deltas=False, catch_adds=False):
2592.3.13 by Robert Collins
Implement KnitGraphIndex.get_method.
2260
        """Build a two-graph index.
2261
2262
        :param deltas: If true, use underlying indices with two node-ref
2263
            lists and 'parent' set to a delta-compressed against tail.
2264
        """
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.
2265
        # build a complex graph across several indices.
2592.3.13 by Robert Collins
Implement KnitGraphIndex.get_method.
2266
        if deltas:
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2267
            # delta compression inn the index
2592.3.13 by Robert Collins
Implement KnitGraphIndex.get_method.
2268
            index1 = self.make_g_index('1', 2, [
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2269
                (('tip', ), 'N0 100', ([('parent', )], [], )),
2270
                (('tail', ), '', ([], []))])
2592.3.13 by Robert Collins
Implement KnitGraphIndex.get_method.
2271
            index2 = self.make_g_index('2', 2, [
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2272
                (('parent', ), ' 100 78', ([('tail', ), ('ghost', )], [('tail', )])),
2273
                (('separate', ), '', ([], []))])
2592.3.13 by Robert Collins
Implement KnitGraphIndex.get_method.
2274
        else:
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2275
            # just blob location and graph in the index.
2592.3.13 by Robert Collins
Implement KnitGraphIndex.get_method.
2276
            index1 = self.make_g_index('1', 1, [
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2277
                (('tip', ), 'N0 100', ([('parent', )], )),
2278
                (('tail', ), '', ([], ))])
2592.3.13 by Robert Collins
Implement KnitGraphIndex.get_method.
2279
            index2 = self.make_g_index('2', 1, [
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2280
                (('parent', ), ' 100 78', ([('tail', ), ('ghost', )], )),
2281
                (('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.
2282
        combined_index = CombinedGraphIndex([index1, index2])
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2283
        if catch_adds:
2284
            self.combined_index = combined_index
2285
            self.caught_entries = []
2286
            add_callback = self.catch_add
2287
        else:
2288
            add_callback = None
2289
        return KnitGraphIndex(combined_index, deltas=deltas,
2290
            add_callback=add_callback)
2592.3.4 by Robert Collins
Implement get_ancestry/get_ancestry_with_ghosts for KnitGraphIndex.
2291
2292
    def test_get_graph(self):
2293
        index = self.two_graph_index()
2592.3.2 by Robert Collins
Implement a get_graph for a new KnitGraphIndex that will implement a KnitIndex on top of the GraphIndex API.
2294
        self.assertEqual(set([
2295
            ('tip', ('parent', )),
2296
            ('tail', ()),
2297
            ('parent', ('tail', 'ghost')),
2298
            ('separate', ()),
2299
            ]), set(index.get_graph()))
2592.3.4 by Robert Collins
Implement get_ancestry/get_ancestry_with_ghosts for KnitGraphIndex.
2300
2301
    def test_get_ancestry(self):
2592.3.30 by Robert Collins
Make GraphKnitIndex get_ancestry the same as regular knits.
2302
        # get_ancestry is defined as eliding ghosts, not erroring.
2303
        index = self.two_graph_index()
2592.3.4 by Robert Collins
Implement get_ancestry/get_ancestry_with_ghosts for KnitGraphIndex.
2304
        self.assertEqual([], index.get_ancestry([]))
2305
        self.assertEqual(['separate'], index.get_ancestry(['separate']))
2306
        self.assertEqual(['tail'], index.get_ancestry(['tail']))
2307
        self.assertEqual(['tail', 'parent'], index.get_ancestry(['parent']))
2308
        self.assertEqual(['tail', 'parent', 'tip'], index.get_ancestry(['tip']))
2309
        self.assertTrue(index.get_ancestry(['tip', 'separate']) in
2310
            (['tail', 'parent', 'tip', 'separate'],
2311
             ['separate', 'tail', 'parent', 'tip'],
2312
            ))
2313
        # and without topo_sort
2314
        self.assertEqual(set(['separate']),
2315
            set(index.get_ancestry(['separate'], topo_sorted=False)))
2316
        self.assertEqual(set(['tail']),
2317
            set(index.get_ancestry(['tail'], topo_sorted=False)))
2318
        self.assertEqual(set(['tail', 'parent']),
2319
            set(index.get_ancestry(['parent'], topo_sorted=False)))
2320
        self.assertEqual(set(['tail', 'parent', 'tip']),
2321
            set(index.get_ancestry(['tip'], topo_sorted=False)))
2322
        self.assertEqual(set(['separate', 'tail', 'parent', 'tip']),
2323
            set(index.get_ancestry(['tip', 'separate'])))
2592.3.30 by Robert Collins
Make GraphKnitIndex get_ancestry the same as regular knits.
2324
        # asking for a ghost makes it go boom.
2325
        self.assertRaises(errors.RevisionNotPresent, index.get_ancestry, ['ghost'])
2592.3.4 by Robert Collins
Implement get_ancestry/get_ancestry_with_ghosts for KnitGraphIndex.
2326
2327
    def test_get_ancestry_with_ghosts(self):
2328
        index = self.two_graph_index()
2329
        self.assertEqual([], index.get_ancestry_with_ghosts([]))
2330
        self.assertEqual(['separate'], index.get_ancestry_with_ghosts(['separate']))
2331
        self.assertEqual(['tail'], index.get_ancestry_with_ghosts(['tail']))
2332
        self.assertTrue(index.get_ancestry_with_ghosts(['parent']) in
2333
            (['tail', 'ghost', 'parent'],
2334
             ['ghost', 'tail', 'parent'],
2335
            ))
2336
        self.assertTrue(index.get_ancestry_with_ghosts(['tip']) in
2337
            (['tail', 'ghost', 'parent', 'tip'],
2338
             ['ghost', 'tail', 'parent', 'tip'],
2339
            ))
2340
        self.assertTrue(index.get_ancestry_with_ghosts(['tip', 'separate']) in
2341
            (['tail', 'ghost', 'parent', 'tip', 'separate'],
2342
             ['ghost', 'tail', 'parent', 'tip', 'separate'],
2343
             ['separate', 'tail', 'ghost', 'parent', 'tip'],
2344
             ['separate', 'ghost', 'tail', 'parent', 'tip'],
2345
            ))
2592.3.30 by Robert Collins
Make GraphKnitIndex get_ancestry the same as regular knits.
2346
        # asking for a ghost makes it go boom.
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2347
        self.assertRaises(errors.RevisionNotPresent, index.get_ancestry_with_ghosts, ['ghost'])
2592.3.5 by Robert Collins
Implement KnitGraphIndex.num_versions.
2348
2349
    def test_num_versions(self):
2350
        index = self.two_graph_index()
2351
        self.assertEqual(4, index.num_versions())
2592.3.6 by Robert Collins
Implement KnitGraphIndex.get_versions.
2352
2353
    def test_get_versions(self):
2354
        index = self.two_graph_index()
2355
        self.assertEqual(set(['tail', 'tip', 'parent', 'separate']),
2356
            set(index.get_versions()))
2357
2592.3.9 by Robert Collins
Implement KnitGraphIndex.has_version.
2358
    def test_has_version(self):
2359
        index = self.two_graph_index()
2360
        self.assertTrue(index.has_version('tail'))
2361
        self.assertFalse(index.has_version('ghost'))
2362
2592.3.10 by Robert Collins
Implement KnitGraphIndex.get_position.
2363
    def test_get_position(self):
2364
        index = self.two_graph_index()
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
2365
        self.assertEqual((index._graph_index._indices[0], 0, 100), index.get_position('tip'))
2366
        self.assertEqual((index._graph_index._indices[1], 100, 78), index.get_position('parent'))
2592.3.10 by Robert Collins
Implement KnitGraphIndex.get_position.
2367
2592.3.13 by Robert Collins
Implement KnitGraphIndex.get_method.
2368
    def test_get_method_deltas(self):
2369
        index = self.two_graph_index(deltas=True)
2592.3.11 by Robert Collins
Implement KnitGraphIndex.get_method.
2370
        self.assertEqual('fulltext', index.get_method('tip'))
2371
        self.assertEqual('line-delta', index.get_method('parent'))
2372
2592.3.13 by Robert Collins
Implement KnitGraphIndex.get_method.
2373
    def test_get_method_no_deltas(self):
2374
        # check that the parent-history lookup is ignored with deltas=False.
2375
        index = self.two_graph_index(deltas=False)
2376
        self.assertEqual('fulltext', index.get_method('tip'))
2377
        self.assertEqual('fulltext', index.get_method('parent'))
2378
2592.3.14 by Robert Collins
Implement KnitGraphIndex.get_options.
2379
    def test_get_options_deltas(self):
2380
        index = self.two_graph_index(deltas=True)
2658.2.1 by Robert Collins
Fix mismatch between KnitGraphIndex and KnitIndex in get_options.
2381
        self.assertEqual(['fulltext', 'no-eol'], index.get_options('tip'))
2382
        self.assertEqual(['line-delta'], index.get_options('parent'))
2592.3.14 by Robert Collins
Implement KnitGraphIndex.get_options.
2383
2384
    def test_get_options_no_deltas(self):
2385
        # check that the parent-history lookup is ignored with deltas=False.
2386
        index = self.two_graph_index(deltas=False)
2658.2.1 by Robert Collins
Fix mismatch between KnitGraphIndex and KnitIndex in get_options.
2387
        self.assertEqual(['fulltext', 'no-eol'], index.get_options('tip'))
2388
        self.assertEqual(['fulltext'], index.get_options('parent'))
2592.3.14 by Robert Collins
Implement KnitGraphIndex.get_options.
2389
2592.3.15 by Robert Collins
Implement KnitGraphIndex.get_parents/get_parents_with_ghosts.
2390
    def test_get_parents_with_ghosts(self):
2391
        index = self.two_graph_index()
2392
        self.assertEqual(('tail', 'ghost'), index.get_parents_with_ghosts('parent'))
2592.3.43 by Robert Collins
A knit iter_parents API.
2393
        # and errors on ghosts.
2394
        self.assertRaises(errors.RevisionNotPresent,
2395
            index.get_parents_with_ghosts, 'ghost')
2592.3.15 by Robert Collins
Implement KnitGraphIndex.get_parents/get_parents_with_ghosts.
2396
2592.3.16 by Robert Collins
Implement KnitGraphIndex.check_versions_present.
2397
    def test_check_versions_present(self):
2398
        # ghosts should not be considered present
2399
        index = self.two_graph_index()
2400
        self.assertRaises(RevisionNotPresent, index.check_versions_present,
2401
            ['ghost'])
2402
        self.assertRaises(RevisionNotPresent, index.check_versions_present,
2403
            ['tail', 'ghost'])
2404
        index.check_versions_present(['tail', 'separate'])
2592.3.14 by Robert Collins
Implement KnitGraphIndex.get_options.
2405
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2406
    def catch_add(self, entries):
2407
        self.caught_entries.append(entries)
2408
2409
    def test_add_no_callback_errors(self):
2410
        index = self.two_graph_index()
2411
        self.assertRaises(errors.ReadOnlyError, index.add_version,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2412
            '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.
2413
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
2414
    def test_add_version_smoke(self):
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2415
        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
2416
        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.
2417
        self.assertEqual([[(('new', ), 'N50 60', ((('separate',),),))]],
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2418
            self.caught_entries)
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
2419
2420
    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.
2421
        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.
2422
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2423
            '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.
2424
        self.assertEqual([], self.caught_entries)
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
2425
2426
    def test_add_version_same_dup(self):
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2427
        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.
2428
        # options can be spelt two different ways
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2429
        index.add_version('tip', 'fulltext,no-eol', (None, 0, 100), ['parent'])
2430
        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.
2431
        # but neither should have added data.
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_version_different_dup(self):
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2435
        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.
2436
        # change options
2437
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2438
            'tip', 'no-eol,line-delta', (None, 0, 100), ['parent'])
2439
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2440
            'tip', 'line-delta,no-eol', (None, 0, 100), ['parent'])
2441
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2442
            'tip', 'fulltext', (None, 0, 100), ['parent'])
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
2443
        # position/length
2444
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2445
            '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.
2446
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2447
            '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.
2448
        # parents
2449
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2450
            'tip', 'fulltext,no-eol', (None, 0, 100), [])
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2451
        self.assertEqual([], self.caught_entries)
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
2452
        
2453
    def test_add_versions_nodeltas(self):
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2454
        index = self.two_graph_index(catch_adds=True)
2455
        index.add_versions([
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2456
                ('new', 'fulltext,no-eol', (None, 50, 60), ['separate']),
2457
                ('new2', 'fulltext', (None, 0, 6), ['new']),
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2458
                ])
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2459
        self.assertEqual([(('new', ), 'N50 60', ((('separate',),),)),
2460
            (('new2', ), ' 0 6', ((('new',),),))],
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2461
            sorted(self.caught_entries[0]))
2462
        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.
2463
2464
    def test_add_versions_deltas(self):
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2465
        index = self.two_graph_index(deltas=True, catch_adds=True)
2466
        index.add_versions([
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2467
                ('new', 'fulltext,no-eol', (None, 50, 60), ['separate']),
2468
                ('new2', 'line-delta', (None, 0, 6), ['new']),
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2469
                ])
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2470
        self.assertEqual([(('new', ), 'N50 60', ((('separate',),), ())),
2471
            (('new2', ), ' 0 6', ((('new',),), (('new',),), ))],
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2472
            sorted(self.caught_entries[0]))
2473
        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.
2474
2475
    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.
2476
        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.
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
            [('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.
2479
        self.assertEqual([], self.caught_entries)
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
2480
2841.2.1 by Robert Collins
* Commit no longer checks for new text keys during insertion when the
2481
    def test_add_versions_random_id_accepted(self):
2482
        index = self.two_graph_index(catch_adds=True)
2483
        index.add_versions([], random_id=True)
2484
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
2485
    def test_add_versions_same_dup(self):
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2486
        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.
2487
        # options can be spelt two different ways
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2488
        index.add_versions([('tip', 'fulltext,no-eol', (None, 0, 100), ['parent'])])
2489
        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.
2490
        # but neither should have added data.
2491
        self.assertEqual([[], []], self.caught_entries)
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
2492
        
2493
    def test_add_versions_different_dup(self):
2592.3.19 by Robert Collins
Change KnitGraphIndex from returning data to performing a callback on insertions.
2494
        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.
2495
        # change options
2496
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2497
            [('tip', 'no-eol,line-delta', (None, 0, 100), ['parent'])])
2498
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2499
            [('tip', 'line-delta,no-eol', (None, 0, 100), ['parent'])])
2500
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2501
            [('tip', 'fulltext', (None, 0, 100), ['parent'])])
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
2502
        # position/length
2503
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2504
            [('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.
2505
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2506
            [('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.
2507
        # parents
2508
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2509
            [('tip', 'fulltext,no-eol', (None, 0, 100), [])])
2592.3.17 by Robert Collins
Add add_version(s) to KnitGraphIndex, completing the required api for KnitVersionedFile.
2510
        # change options in the second record
2511
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2512
            [('tip', 'fulltext,no-eol', (None, 0, 100), ['parent']),
2513
             ('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.
2514
        self.assertEqual([], self.caught_entries)
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2515
2592.3.43 by Robert Collins
A knit iter_parents API.
2516
    def test_iter_parents(self):
2517
        index1 = self.make_g_index('1', 1, [
2518
        # no parents
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2519
            (('r0', ), 'N0 100', ([], )),
2592.3.43 by Robert Collins
A knit iter_parents API.
2520
        # 1 parent
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2521
            (('r1', ), '', ([('r0', )], ))])
2592.3.43 by Robert Collins
A knit iter_parents API.
2522
        index2 = self.make_g_index('2', 1, [
2523
        # 2 parents
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2524
            (('r2', ), 'N0 100', ([('r1', ), ('r0', )], )),
2592.3.43 by Robert Collins
A knit iter_parents API.
2525
            ])
2526
        combined_index = CombinedGraphIndex([index1, index2])
2527
        index = KnitGraphIndex(combined_index)
2528
        # XXX TODO a ghost
2529
        # cases: each sample data individually:
2530
        self.assertEqual(set([('r0', ())]),
2531
            set(index.iter_parents(['r0'])))
2532
        self.assertEqual(set([('r1', ('r0', ))]),
2533
            set(index.iter_parents(['r1'])))
2534
        self.assertEqual(set([('r2', ('r1', 'r0'))]),
2535
            set(index.iter_parents(['r2'])))
2536
        # no nodes returned for a missing node
2537
        self.assertEqual(set(),
2538
            set(index.iter_parents(['missing'])))
2539
        # 1 node returned with missing nodes skipped
2540
        self.assertEqual(set([('r1', ('r0', ))]),
2541
            set(index.iter_parents(['ghost1', 'r1', 'ghost'])))
2542
        # 2 nodes returned
2543
        self.assertEqual(set([('r0', ()), ('r1', ('r0', ))]),
2544
            set(index.iter_parents(['r0', 'r1'])))
2545
        # 2 nodes returned, missing skipped
2546
        self.assertEqual(set([('r0', ()), ('r1', ('r0', ))]),
2547
            set(index.iter_parents(['a', 'r0', 'b', 'r1', 'c'])))
2548
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2549
2550
class TestNoParentsGraphIndexKnit(KnitTests):
2551
    """Tests for knits using KnitGraphIndex with no parents."""
2552
2553
    def make_g_index(self, name, ref_lists=0, nodes=[]):
2554
        builder = GraphIndexBuilder(ref_lists)
2555
        for node, references in nodes:
2556
            builder.add_node(node, references)
2557
        stream = builder.finish()
2558
        trans = self.get_transport()
2890.2.1 by Robert Collins
* ``bzrlib.index.GraphIndex`` now requires a size parameter to the
2559
        size = trans.put_file(name, stream)
2560
        return GraphIndex(trans, name, size)
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2561
2562
    def test_parents_deltas_incompatible(self):
2563
        index = CombinedGraphIndex([])
2564
        self.assertRaises(errors.KnitError, KnitGraphIndex, index,
2565
            deltas=True, parents=False)
2566
2567
    def two_graph_index(self, catch_adds=False):
2568
        """Build a two-graph index.
2569
2570
        :param deltas: If true, use underlying indices with two node-ref
2571
            lists and 'parent' set to a delta-compressed against tail.
2572
        """
2573
        # put several versions in the index.
2574
        index1 = self.make_g_index('1', 0, [
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2575
            (('tip', ), 'N0 100'),
2576
            (('tail', ), '')])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2577
        index2 = self.make_g_index('2', 0, [
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2578
            (('parent', ), ' 100 78'),
2579
            (('separate', ), '')])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2580
        combined_index = CombinedGraphIndex([index1, index2])
2581
        if catch_adds:
2582
            self.combined_index = combined_index
2583
            self.caught_entries = []
2584
            add_callback = self.catch_add
2585
        else:
2586
            add_callback = None
2587
        return KnitGraphIndex(combined_index, parents=False,
2588
            add_callback=add_callback)
2589
2590
    def test_get_graph(self):
2591
        index = self.two_graph_index()
2592
        self.assertEqual(set([
2593
            ('tip', ()),
2594
            ('tail', ()),
2595
            ('parent', ()),
2596
            ('separate', ()),
2597
            ]), set(index.get_graph()))
2598
2599
    def test_get_ancestry(self):
2600
        # with no parents, ancestry is always just the key.
2601
        index = self.two_graph_index()
2602
        self.assertEqual([], index.get_ancestry([]))
2603
        self.assertEqual(['separate'], index.get_ancestry(['separate']))
2604
        self.assertEqual(['tail'], index.get_ancestry(['tail']))
2605
        self.assertEqual(['parent'], index.get_ancestry(['parent']))
2606
        self.assertEqual(['tip'], index.get_ancestry(['tip']))
2607
        self.assertTrue(index.get_ancestry(['tip', 'separate']) in
2608
            (['tip', 'separate'],
2609
             ['separate', 'tip'],
2610
            ))
2611
        # asking for a ghost makes it go boom.
2612
        self.assertRaises(errors.RevisionNotPresent, index.get_ancestry, ['ghost'])
2613
2614
    def test_get_ancestry_with_ghosts(self):
2615
        index = self.two_graph_index()
2616
        self.assertEqual([], index.get_ancestry_with_ghosts([]))
2617
        self.assertEqual(['separate'], index.get_ancestry_with_ghosts(['separate']))
2618
        self.assertEqual(['tail'], index.get_ancestry_with_ghosts(['tail']))
2619
        self.assertEqual(['parent'], index.get_ancestry_with_ghosts(['parent']))
2620
        self.assertEqual(['tip'], index.get_ancestry_with_ghosts(['tip']))
2621
        self.assertTrue(index.get_ancestry_with_ghosts(['tip', 'separate']) in
2622
            (['tip', 'separate'],
2623
             ['separate', 'tip'],
2624
            ))
2625
        # asking for a ghost makes it go boom.
2626
        self.assertRaises(errors.RevisionNotPresent, index.get_ancestry_with_ghosts, ['ghost'])
2627
2628
    def test_num_versions(self):
2629
        index = self.two_graph_index()
2630
        self.assertEqual(4, index.num_versions())
2631
2632
    def test_get_versions(self):
2633
        index = self.two_graph_index()
2634
        self.assertEqual(set(['tail', 'tip', 'parent', 'separate']),
2635
            set(index.get_versions()))
2636
2637
    def test_has_version(self):
2638
        index = self.two_graph_index()
2639
        self.assertTrue(index.has_version('tail'))
2640
        self.assertFalse(index.has_version('ghost'))
2641
2642
    def test_get_position(self):
2643
        index = self.two_graph_index()
2592.3.71 by Robert Collins
Basic version of knit-based repository operating, many tests failing.
2644
        self.assertEqual((index._graph_index._indices[0], 0, 100), index.get_position('tip'))
2645
        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.
2646
2647
    def test_get_method(self):
2648
        index = self.two_graph_index()
2649
        self.assertEqual('fulltext', index.get_method('tip'))
2658.2.1 by Robert Collins
Fix mismatch between KnitGraphIndex and KnitIndex in get_options.
2650
        self.assertEqual(['fulltext'], index.get_options('parent'))
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2651
2652
    def test_get_options(self):
2653
        index = self.two_graph_index()
2658.2.1 by Robert Collins
Fix mismatch between KnitGraphIndex and KnitIndex in get_options.
2654
        self.assertEqual(['fulltext', 'no-eol'], index.get_options('tip'))
2655
        self.assertEqual(['fulltext'], index.get_options('parent'))
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2656
2657
    def test_get_parents_with_ghosts(self):
2658
        index = self.two_graph_index()
2659
        self.assertEqual((), index.get_parents_with_ghosts('parent'))
2592.3.43 by Robert Collins
A knit iter_parents API.
2660
        # and errors on ghosts.
2661
        self.assertRaises(errors.RevisionNotPresent,
2662
            index.get_parents_with_ghosts, 'ghost')
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2663
2664
    def test_check_versions_present(self):
2665
        index = self.two_graph_index()
2666
        self.assertRaises(RevisionNotPresent, index.check_versions_present,
2667
            ['missing'])
2668
        self.assertRaises(RevisionNotPresent, index.check_versions_present,
2669
            ['tail', 'missing'])
2670
        index.check_versions_present(['tail', 'separate'])
2671
2672
    def catch_add(self, entries):
2673
        self.caught_entries.append(entries)
2674
2675
    def test_add_no_callback_errors(self):
2676
        index = self.two_graph_index()
2677
        self.assertRaises(errors.ReadOnlyError, index.add_version,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2678
            'new', 'fulltext,no-eol', (None, 50, 60), ['separate'])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2679
2680
    def test_add_version_smoke(self):
2681
        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
2682
        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.
2683
        self.assertEqual([[(('new', ), 'N50 60')]],
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2684
            self.caught_entries)
2685
2686
    def test_add_version_delta_not_delta_index(self):
2687
        index = self.two_graph_index(catch_adds=True)
2688
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2689
            'new', 'no-eol,line-delta', (None, 0, 100), [])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2690
        self.assertEqual([], self.caught_entries)
2691
2692
    def test_add_version_same_dup(self):
2693
        index = self.two_graph_index(catch_adds=True)
2694
        # options can be spelt two different ways
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2695
        index.add_version('tip', 'fulltext,no-eol', (None, 0, 100), [])
2696
        index.add_version('tip', 'no-eol,fulltext', (None, 0, 100), [])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2697
        # but neither should have added data.
2698
        self.assertEqual([[], []], self.caught_entries)
2699
        
2700
    def test_add_version_different_dup(self):
2701
        index = self.two_graph_index(catch_adds=True)
2702
        # change options
2703
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2704
            'tip', 'no-eol,line-delta', (None, 0, 100), [])
2705
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2706
            'tip', 'line-delta,no-eol', (None, 0, 100), [])
2707
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2708
            'tip', 'fulltext', (None, 0, 100), [])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2709
        # position/length
2710
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2711
            'tip', 'fulltext,no-eol', (None, 50, 100), [])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2712
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2713
            'tip', 'fulltext,no-eol', (None, 0, 1000), [])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2714
        # parents
2715
        self.assertRaises(errors.KnitCorrupt, index.add_version,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2716
            'tip', 'fulltext,no-eol', (None, 0, 100), ['parent'])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2717
        self.assertEqual([], self.caught_entries)
2718
        
2719
    def test_add_versions(self):
2720
        index = self.two_graph_index(catch_adds=True)
2721
        index.add_versions([
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2722
                ('new', 'fulltext,no-eol', (None, 50, 60), []),
2723
                ('new2', 'fulltext', (None, 0, 6), []),
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2724
                ])
2624.2.5 by Robert Collins
Change bzrlib.index.Index keys to be 1-tuples, not strings.
2725
        self.assertEqual([(('new', ), 'N50 60'), (('new2', ), ' 0 6')],
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2726
            sorted(self.caught_entries[0]))
2727
        self.assertEqual(1, len(self.caught_entries))
2728
2729
    def test_add_versions_delta_not_delta_index(self):
2730
        index = self.two_graph_index(catch_adds=True)
2731
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2732
            [('new', 'no-eol,line-delta', (None, 0, 100), ['parent'])])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2733
        self.assertEqual([], self.caught_entries)
2734
2735
    def test_add_versions_parents_not_parents_index(self):
2736
        index = self.two_graph_index(catch_adds=True)
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
            [('new', 'no-eol,fulltext', (None, 0, 100), ['parent'])])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2739
        self.assertEqual([], self.caught_entries)
2740
2841.2.1 by Robert Collins
* Commit no longer checks for new text keys during insertion when the
2741
    def test_add_versions_random_id_accepted(self):
2742
        index = self.two_graph_index(catch_adds=True)
2743
        index.add_versions([], random_id=True)
2744
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2745
    def test_add_versions_same_dup(self):
2746
        index = self.two_graph_index(catch_adds=True)
2747
        # options can be spelt two different ways
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2748
        index.add_versions([('tip', 'fulltext,no-eol', (None, 0, 100), [])])
2749
        index.add_versions([('tip', 'no-eol,fulltext', (None, 0, 100), [])])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2750
        # but neither should have added data.
2751
        self.assertEqual([[], []], self.caught_entries)
2752
        
2753
    def test_add_versions_different_dup(self):
2754
        index = self.two_graph_index(catch_adds=True)
2755
        # change options
2756
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2757
            [('tip', 'no-eol,line-delta', (None, 0, 100), [])])
2758
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2759
            [('tip', 'line-delta,no-eol', (None, 0, 100), [])])
2760
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2761
            [('tip', 'fulltext', (None, 0, 100), [])])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2762
        # position/length
2763
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2764
            [('tip', 'fulltext,no-eol', (None, 50, 100), [])])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2765
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2766
            [('tip', 'fulltext,no-eol', (None, 0, 1000), [])])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2767
        # parents
2768
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2769
            [('tip', 'fulltext,no-eol', (None, 0, 100), ['parent'])])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2770
        # change options in the second record
2771
        self.assertRaises(errors.KnitCorrupt, index.add_versions,
2670.2.2 by Robert Collins
* In ``bzrlib.knit`` the internal interface has been altered to use
2772
            [('tip', 'fulltext,no-eol', (None, 0, 100), []),
2773
             ('tip', 'no-eol,line-delta', (None, 0, 100), [])])
2592.3.34 by Robert Collins
Rough unfactored support for parentless KnitGraphIndexs.
2774
        self.assertEqual([], self.caught_entries)
2775
2592.3.43 by Robert Collins
A knit iter_parents API.
2776
    def test_iter_parents(self):
2777
        index = self.two_graph_index()
2778
        self.assertEqual(set([
2779
            ('tip', ()), ('tail', ()), ('parent', ()), ('separate', ())
2780
            ]),
2781
            set(index.iter_parents(['tip', 'tail', 'ghost', 'parent', 'separate'])))
2782
        self.assertEqual(set([('tip', ())]),
2783
            set(index.iter_parents(['tip'])))
2784
        self.assertEqual(set(),
2785
            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.
2786
2787
2788
class TestPackKnits(KnitTests):
2789
    """Tests that use a _PackAccess and KnitGraphIndex."""
2790
2791
    def test_get_data_stream_packs_ignores_pack_overhead(self):
2792
        # Packs have an encoding overhead that should not be included in the
2793
        # 'size' field of a data stream, because it is not returned by the
2794
        # raw_reading functions - it is why index_memo's are opaque, and
2795
        # get_data_stream was abusing this.
2796
        packname = 'test.pack'
2797
        transport = self.get_transport()
2798
        def write_data(bytes):
2799
            transport.append_bytes(packname, bytes)
2800
        writer = pack.ContainerWriter(write_data)
2801
        writer.begin()
2802
        index = InMemoryGraphIndex(2)
2803
        knit_index = KnitGraphIndex(index, add_callback=index.add_nodes,
2804
            deltas=True)
2805
        indices = {index:(transport, packname)}
2806
        access = _PackAccess(indices, writer=(writer, index))
2807
        k = KnitVersionedFile('test', get_transport('.'),
2808
            delta=True, create=True, index=knit_index, access_method=access)
2809
        # insert something into the knit
2810
        k.add_lines('text-1', [], ["foo\n"])
2811
        # get a data stream for it
2812
        stream = k.get_data_stream(['text-1'])
2813
        # if the stream has been incorrectly assembled, we will get a short read
2814
        # reading from the stream (as streams have no trailer)
2815
        expected_length = stream[1][0][2]
2816
        # we use -1 to do the read, so that if a trailer is added this test
2817
        # will fail and we'll adjust it to handle that case correctly, rather
2818
        # than allowing an over-read that is bogus.
2819
        self.assertEqual(expected_length, len(stream[2](-1)))
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2820
2821
2822
class Test_StreamIndex(KnitTests):
2823
2824
    def get_index(self, knit, stream):
2825
        """Get a _StreamIndex from knit and stream."""
2826
        return knit._knit_from_datastream(stream)._index
2827
2828
    def assertIndexVersions(self, knit, versions):
2829
        """Check that the _StreamIndex versions are those of the stream."""
2830
        index = self.get_index(knit, knit.get_data_stream(versions))
2831
        self.assertEqual(set(index.get_versions()), set(versions))
2832
        # check we didn't get duplicates
2833
        self.assertEqual(len(index.get_versions()), len(versions))
2834
2835
    def assertIndexAncestry(self, knit, ancestry_versions, versions, result):
2836
        """Check the result of a get_ancestry call on knit."""
2837
        index = self.get_index(knit, knit.get_data_stream(versions))
3052.2.4 by Andrew Bennetts
Some tweaks suggested by John's review.
2838
        self.assertEqual(
2839
            set(result),
2840
            set(index.get_ancestry(ancestry_versions, False)))
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2841
3052.2.3 by Robert Collins
Handle insert_data_stream of an unannotated stream into an annotated knit.
2842
    def assertIterParents(self, knit, versions, parent_versions, result):
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2843
        """Check the result of an iter_parents call on knit."""
2844
        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.
2845
        self.assertEqual(result, index.iter_parents(parent_versions))
2846
2847
    def assertGetMethod(self, knit, versions, version, result):
2848
        index = self.get_index(knit, knit.get_data_stream(versions))
2849
        self.assertEqual(result, index.get_method(version))
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2850
2851
    def assertGetOptions(self, knit, version, options):
2852
        index = self.get_index(knit, knit.get_data_stream(version))
2853
        self.assertEqual(options, index.get_options(version))
2854
2855
    def assertGetPosition(self, knit, versions, version, result):
2856
        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.
2857
        if result[1] is None:
2858
            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.
2859
        self.assertEqual(result, index.get_position(version))
2860
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2861
    def assertGetParentsWithGhosts(self, knit, versions, version, parents):
2862
        index = self.get_index(knit, knit.get_data_stream(versions))
2863
        self.assertEqual(parents, index.get_parents_with_ghosts(version))
2864
2865
    def make_knit_with_4_versions_2_dags(self):
2866
        knit = self.make_test_knit()
2867
        knit.add_lines('a', [], ["foo"])
2868
        knit.add_lines('b', [], [])
2869
        knit.add_lines('c', ['b', 'a'], [])
2870
        knit.add_lines_with_ghosts('d', ['e', 'f'], [])
2871
        return knit
2872
2873
    def test_versions(self):
2874
        """The versions of a StreamIndex are those of the datastream."""
2875
        knit = self.make_knit_with_4_versions_2_dags()
2876
        # ask for most permutations, which catches bugs like falling back to the
2877
        # target knit, or showing ghosts, etc.
2878
        self.assertIndexVersions(knit, [])
2879
        self.assertIndexVersions(knit, ['a'])
2880
        self.assertIndexVersions(knit, ['b'])
2881
        self.assertIndexVersions(knit, ['c'])
2882
        self.assertIndexVersions(knit, ['d'])
2883
        self.assertIndexVersions(knit, ['a', 'b'])
2884
        self.assertIndexVersions(knit, ['b', 'c'])
2885
        self.assertIndexVersions(knit, ['a', 'c'])
2886
        self.assertIndexVersions(knit, ['a', 'b', 'c'])
2887
        self.assertIndexVersions(knit, ['a', 'b', 'c', 'd'])
2888
2889
    def test_construct(self):
2890
        """Constructing a StreamIndex generates index data."""
2891
        data_list = [('text-a', ['fulltext'], 127, []),
2892
            ('text-b', ['option'], 128, ['text-c'])]
3224.1.8 by John Arbash Meinel
Add noeol to the return signature of get_build_details.
2893
        index = _StreamIndex(data_list, None)
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2894
        self.assertEqual({'text-a':(['fulltext'], (0, 127), []),
2895
            'text-b':(['option'], (127, 127 + 128), ['text-c'])},
2896
            index._by_version)
2897
2898
    def test_get_ancestry(self):
2899
        knit = self.make_knit_with_4_versions_2_dags()
2900
        self.assertIndexAncestry(knit, ['a'], ['a'], ['a'])
2901
        self.assertIndexAncestry(knit, ['b'], ['b'], ['b'])
2902
        self.assertIndexAncestry(knit, ['c'], ['c'], ['c'])
2903
        self.assertIndexAncestry(knit, ['c'], ['a', 'b', 'c'],
3052.2.4 by Andrew Bennetts
Some tweaks suggested by John's review.
2904
            set(['a', 'b', 'c']))
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2905
        self.assertIndexAncestry(knit, ['c', 'd'], ['a', 'b', 'c', 'd'],
3052.2.4 by Andrew Bennetts
Some tweaks suggested by John's review.
2906
            set(['a', 'b', 'c', 'd']))
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2907
3052.2.3 by Robert Collins
Handle insert_data_stream of an unannotated stream into an annotated knit.
2908
    def test_get_method(self):
2909
        knit = self.make_knit_with_4_versions_2_dags()
2910
        self.assertGetMethod(knit, ['a'], 'a', 'fulltext')
2911
        self.assertGetMethod(knit, ['c'], 'c', 'line-delta')
2912
        # get_method on a basis that is not in the datastream (but in the
2913
        # backing knit) returns 'fulltext', because thats what we'll create as
2914
        # we thunk across.
2915
        self.assertGetMethod(knit, ['c'], 'b', 'fulltext')
2916
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2917
    def test_iter_parents(self):
2918
        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.
2919
        self.assertIterParents(knit, ['a'], ['a'], [('a', ())])
3052.2.3 by Robert Collins
Handle insert_data_stream of an unannotated stream into an annotated knit.
2920
        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.
2921
            [('a', ()), ('b', ())])
3052.2.3 by Robert Collins
Handle insert_data_stream of an unannotated stream into an annotated knit.
2922
        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.
2923
            [('a', ()), ('b', ()), ('c', ('b', 'a'))])
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2924
        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.
2925
            ['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.
2926
            [('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.
2927
        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.
2928
            [('c', ('b', 'a'))])
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2929
2930
    def test_get_options(self):
2931
        knit = self.make_knit_with_4_versions_2_dags()
2932
        self.assertGetOptions(knit, 'a', ['no-eol', 'fulltext'])
2933
        self.assertGetOptions(knit, 'c', ['line-delta'])
2934
2935
    def test_get_parents_with_ghosts(self):
2936
        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.
2937
        self.assertGetParentsWithGhosts(knit, ['a'], 'a', ())
2938
        self.assertGetParentsWithGhosts(knit, ['c'], 'c', ('b', 'a'))
2939
        self.assertGetParentsWithGhosts(knit, ['d'], 'd', ('e', 'f'))
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2940
2941
    def test_get_position(self):
2942
        knit = self.make_knit_with_4_versions_2_dags()
3052.2.6 by Andrew Bennetts
Fix typo in comment.
2943
        # 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
2944
        # _StreamAccess to use.
3052.2.5 by Andrew Bennetts
Address the rest of the review comments from John and myself.
2945
        self.assertGetPosition(knit, ['a'], 'a', (False, None, 0, 78))
2946
        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.
2947
        # 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.
2948
        # 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.
2949
        # 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.
2950
        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.
2951
2952
2953
class Test_StreamAccess(KnitTests):
3052.2.2 by Robert Collins
* Operations pulling data from a smart server where the underlying
2954
2955
    def get_index_access(self, knit, stream):
2956
        """Get a _StreamAccess from knit and stream."""
2957
        knit =  knit._knit_from_datastream(stream)
2958
        return knit._index, knit._data._access
2959
2960
    def assertGetRawRecords(self, knit, versions):
2961
        index, access = self.get_index_access(knit,
2962
            knit.get_data_stream(versions))
2963
        # check that every version asked for can be obtained from the resulting
2964
        # access object.
2965
        # batch
2966
        memos = []
2967
        for version in versions:
2968
            memos.append(knit._index.get_position(version))
2969
        original = {}
2970
        for version, data in zip(
2971
            versions, knit._data._access.get_raw_records(memos)):
2972
            original[version] = data
2973
        memos = []
2974
        for version in versions:
2975
            memos.append(index.get_position(version))
2976
        streamed = {}
2977
        for version, data in zip(versions, access.get_raw_records(memos)):
2978
            streamed[version] = data
2979
        self.assertEqual(original, streamed)
2980
        # individually
2981
        for version in versions:
2982
            data = list(access.get_raw_records(
2983
                [index.get_position(version)]))[0]
2984
            self.assertEqual(original[version], data)
2985
2986
    def make_knit_with_two_versions(self):
2987
        knit = self.make_test_knit()
2988
        knit.add_lines('a', [], ["foo"])
2989
        knit.add_lines('b', [], ["bar"])
2990
        return knit
2991
2992
    def test_get_raw_records(self):
2993
        knit = self.make_knit_with_two_versions()
2994
        self.assertGetRawRecords(knit, ['a', 'b'])
2995
        self.assertGetRawRecords(knit, ['a'])
2996
        self.assertGetRawRecords(knit, ['b'])
3052.2.3 by Robert Collins
Handle insert_data_stream of an unannotated stream into an annotated knit.
2997
    
2998
    def test_get_raw_record_from_backing_knit(self):
2999
        # the thunk layer should create an artificial A on-demand when needed.
3000
        source_knit = self.make_test_knit(name='plain', annotate=False)
3001
        target_knit = self.make_test_knit(name='annotated', annotate=True)
3002
        source_knit.add_lines("A", [], ["Foo\n"])
3003
        # Give the target A, so we can try to thunk across to it.
3004
        target_knit.join(source_knit)
3005
        index, access = self.get_index_access(target_knit,
3006
            source_knit.get_data_stream([]))
3052.2.5 by Andrew Bennetts
Address the rest of the review comments from John and myself.
3007
        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.
3008
        df = GzipFile(mode='rb', fileobj=StringIO(raw_data))
3009
        self.assertEqual(
3010
            'version A 1 5d36b88bb697a2d778f024048bafabd443d74503\n'
3011
            'Foo\nend A\n',
3012
            df.read())
3013
3014
    def test_asking_for_thunk_stream_is_not_plain_errors(self):
3015
        knit = self.make_test_knit(name='annotated', annotate=True)
3016
        knit.add_lines("A", [], ["Foo\n"])
3017
        index, access = self.get_index_access(knit,
3018
            knit.get_data_stream([]))
3019
        self.assertRaises(errors.KnitCorrupt,
3052.2.5 by Andrew Bennetts
Address the rest of the review comments from John and myself.
3020
            list, access.get_raw_records([(True, "A", None, None)]))