/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_versionedfile.py

Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005 Canonical Ltd
 
2
#
 
3
# Authors:
 
4
#   Johan Rydberg <jrydberg@gnu.org>
 
5
#
 
6
# This program is free software; you can redistribute it and/or modify
 
7
# it under the terms of the GNU General Public License as published by
 
8
# the Free Software Foundation; either version 2 of the License, or
 
9
# (at your option) any later version.
 
10
#
 
11
# This program is distributed in the hope that it will be useful,
 
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
# GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License
 
17
# along with this program; if not, write to the Free Software
 
18
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
19
 
 
20
 
 
21
# TODO: might be nice to create a versionedfile with some type of corruption
 
22
# considered typical and check that it can be detected/corrected.
 
23
 
 
24
from itertools import chain
 
25
from StringIO import StringIO
 
26
 
 
27
import bzrlib
 
28
from bzrlib import (
 
29
    errors,
 
30
    osutils,
 
31
    progress,
 
32
    )
 
33
from bzrlib.errors import (
 
34
                           RevisionNotPresent,
 
35
                           RevisionAlreadyPresent,
 
36
                           WeaveParentMismatch
 
37
                           )
 
38
from bzrlib import knit as _mod_knit
 
39
from bzrlib.knit import (
 
40
    make_file_knit,
 
41
    KnitAnnotateFactory,
 
42
    KnitPlainFactory,
 
43
    )
 
44
from bzrlib.symbol_versioning import one_four, one_five
 
45
from bzrlib.tests import TestCaseWithMemoryTransport, TestSkipped
 
46
from bzrlib.tests.http_utils import TestCaseWithWebserver
 
47
from bzrlib.trace import mutter
 
48
from bzrlib.transport import get_transport
 
49
from bzrlib.transport.memory import MemoryTransport
 
50
from bzrlib.tsort import topo_sort
 
51
from bzrlib.tuned_gzip import GzipFile
 
52
import bzrlib.versionedfile as versionedfile
 
53
from bzrlib.weave import WeaveFile
 
54
from bzrlib.weavefile import read_weave, write_weave
 
55
 
 
56
 
 
57
def get_diamond_vf(f, trailing_eol=True, left_only=False):
 
58
    """Get a diamond graph to exercise deltas and merges.
 
59
    
 
60
    :param trailing_eol: If True end the last line with \n.
 
61
    """
 
62
    parents = {
 
63
        'origin': (),
 
64
        'base': (('origin',),),
 
65
        'left': (('base',),),
 
66
        'right': (('base',),),
 
67
        'merged': (('left',), ('right',)),
 
68
        }
 
69
    # insert a diamond graph to exercise deltas and merges.
 
70
    if trailing_eol:
 
71
        last_char = '\n'
 
72
    else:
 
73
        last_char = ''
 
74
    f.add_lines('origin', [], ['origin' + last_char])
 
75
    f.add_lines('base', ['origin'], ['base' + last_char])
 
76
    f.add_lines('left', ['base'], ['base\n', 'left' + last_char])
 
77
    if not left_only:
 
78
        f.add_lines('right', ['base'],
 
79
            ['base\n', 'right' + last_char])
 
80
        f.add_lines('merged', ['left', 'right'],
 
81
            ['base\n', 'left\n', 'right\n', 'merged' + last_char])
 
82
    return f, parents
 
83
 
 
84
 
 
85
class VersionedFileTestMixIn(object):
 
86
    """A mixin test class for testing VersionedFiles.
 
87
 
 
88
    This is not an adaptor-style test at this point because
 
89
    theres no dynamic substitution of versioned file implementations,
 
90
    they are strictly controlled by their owning repositories.
 
91
    """
 
92
 
 
93
    def get_transaction(self):
 
94
        if not hasattr(self, '_transaction'):
 
95
            self._transaction = None
 
96
        return self._transaction
 
97
 
 
98
    def test_add(self):
 
99
        f = self.get_file()
 
100
        f.add_lines('r0', [], ['a\n', 'b\n'])
 
101
        f.add_lines('r1', ['r0'], ['b\n', 'c\n'])
 
102
        def verify_file(f):
 
103
            versions = f.versions()
 
104
            self.assertTrue('r0' in versions)
 
105
            self.assertTrue('r1' in versions)
 
106
            self.assertEquals(f.get_lines('r0'), ['a\n', 'b\n'])
 
107
            self.assertEquals(f.get_text('r0'), 'a\nb\n')
 
108
            self.assertEquals(f.get_lines('r1'), ['b\n', 'c\n'])
 
109
            self.assertEqual(2, len(f))
 
110
            self.assertEqual(2, f.num_versions())
 
111
    
 
112
            self.assertRaises(RevisionNotPresent,
 
113
                f.add_lines, 'r2', ['foo'], [])
 
114
            self.assertRaises(RevisionAlreadyPresent,
 
115
                f.add_lines, 'r1', [], [])
 
116
        verify_file(f)
 
117
        # this checks that reopen with create=True does not break anything.
 
118
        f = self.reopen_file(create=True)
 
119
        verify_file(f)
 
120
 
 
121
    def test_get_record_stream_empty(self):
 
122
        """get_record_stream is a replacement for get_data_stream."""
 
123
        f = self.get_file()
 
124
        entries = f.get_record_stream([], 'unordered', False)
 
125
        self.assertEqual([], list(entries))
 
126
 
 
127
    def assertValidStorageKind(self, storage_kind):
 
128
        """Assert that storage_kind is a valid storage_kind."""
 
129
        self.assertSubset([storage_kind],
 
130
            ['mpdiff', 'knit-annotated-ft', 'knit-annotated-delta',
 
131
             'knit-ft', 'knit-delta', 'fulltext', 'knit-annotated-ft-gz',
 
132
             'knit-annotated-delta-gz', 'knit-ft-gz', 'knit-delta-gz'])
 
133
 
 
134
    def capture_stream(self, f, entries, on_seen, parents):
 
135
        """Capture a stream for testing."""
 
136
        for factory in entries:
 
137
            on_seen(factory.key)
 
138
            self.assertValidStorageKind(factory.storage_kind)
 
139
            self.assertEqual(f.get_sha1s([factory.key[0]])[0], factory.sha1)
 
140
            self.assertEqual(parents[factory.key[0]], factory.parents)
 
141
            self.assertIsInstance(factory.get_bytes_as(factory.storage_kind),
 
142
                str)
 
143
 
 
144
    def test_get_record_stream_interface(self):
 
145
        """each item in a stream has to provide a regular interface."""
 
146
        f, parents = get_diamond_vf(self.get_file())
 
147
        entries = f.get_record_stream(['merged', 'left', 'right', 'base'],
 
148
            'unordered', False)
 
149
        seen = set()
 
150
        self.capture_stream(f, entries, seen.add, parents)
 
151
        self.assertEqual(set([('base',), ('left',), ('right',), ('merged',)]),
 
152
            seen)
 
153
 
 
154
    def test_get_record_stream_interface_ordered(self):
 
155
        """each item in a stream has to provide a regular interface."""
 
156
        f, parents = get_diamond_vf(self.get_file())
 
157
        entries = f.get_record_stream(['merged', 'left', 'right', 'base'],
 
158
            'topological', False)
 
159
        seen = []
 
160
        self.capture_stream(f, entries, seen.append, parents)
 
161
        self.assertSubset([tuple(seen)],
 
162
            (
 
163
             (('base',), ('left',), ('right',), ('merged',)),
 
164
             (('base',), ('right',), ('left',), ('merged',)),
 
165
            ))
 
166
 
 
167
    def test_get_record_stream_interface_ordered_with_delta_closure(self):
 
168
        """each item in a stream has to provide a regular interface."""
 
169
        f, parents = get_diamond_vf(self.get_file())
 
170
        entries = f.get_record_stream(['merged', 'left', 'right', 'base'],
 
171
            'topological', True)
 
172
        seen = []
 
173
        for factory in entries:
 
174
            seen.append(factory.key)
 
175
            self.assertValidStorageKind(factory.storage_kind)
 
176
            self.assertEqual(f.get_sha1s([factory.key[0]])[0], factory.sha1)
 
177
            self.assertEqual(parents[factory.key[0]], factory.parents)
 
178
            self.assertEqual(f.get_text(factory.key[0]),
 
179
                factory.get_bytes_as('fulltext'))
 
180
            self.assertIsInstance(factory.get_bytes_as(factory.storage_kind),
 
181
                str)
 
182
        self.assertSubset([tuple(seen)],
 
183
            (
 
184
             (('base',), ('left',), ('right',), ('merged',)),
 
185
             (('base',), ('right',), ('left',), ('merged',)),
 
186
            ))
 
187
 
 
188
    def test_get_record_stream_unknown_storage_kind_raises(self):
 
189
        """Asking for a storage kind that the stream cannot supply raises."""
 
190
        f, parents = get_diamond_vf(self.get_file())
 
191
        entries = f.get_record_stream(['merged', 'left', 'right', 'base'],
 
192
            'unordered', False)
 
193
        # We track the contents because we should be able to try, fail a
 
194
        # particular kind and then ask for one that works and continue.
 
195
        seen = set()
 
196
        for factory in entries:
 
197
            seen.add(factory.key)
 
198
            self.assertValidStorageKind(factory.storage_kind)
 
199
            self.assertEqual(f.get_sha1s([factory.key[0]])[0], factory.sha1)
 
200
            self.assertEqual(parents[factory.key[0]], factory.parents)
 
201
            # currently no stream emits mpdiff
 
202
            self.assertRaises(errors.UnavailableRepresentation,
 
203
                factory.get_bytes_as, 'mpdiff')
 
204
            self.assertIsInstance(factory.get_bytes_as(factory.storage_kind),
 
205
                str)
 
206
        self.assertEqual(set([('base',), ('left',), ('right',), ('merged',)]),
 
207
            seen)
 
208
 
 
209
    def test_get_record_stream_missing_records_are_absent(self):
 
210
        f, parents = get_diamond_vf(self.get_file())
 
211
        entries = f.get_record_stream(['merged', 'left', 'right', 'or', 'base'],
 
212
            'unordered', False)
 
213
        self.assertAbsentRecord(f, parents, entries)
 
214
        entries = f.get_record_stream(['merged', 'left', 'right', 'or', 'base'],
 
215
            'topological', False)
 
216
        self.assertAbsentRecord(f, parents, entries)
 
217
 
 
218
    def assertAbsentRecord(self, f, parents, entries):
 
219
        """Helper for test_get_record_stream_missing_records_are_absent."""
 
220
        seen = set()
 
221
        for factory in entries:
 
222
            seen.add(factory.key)
 
223
            if factory.key == ('or',):
 
224
                self.assertEqual('absent', factory.storage_kind)
 
225
                self.assertEqual(None, factory.sha1)
 
226
                self.assertEqual(None, factory.parents)
 
227
            else:
 
228
                self.assertValidStorageKind(factory.storage_kind)
 
229
                self.assertEqual(f.get_sha1s([factory.key[0]])[0], factory.sha1)
 
230
                self.assertEqual(parents[factory.key[0]], factory.parents)
 
231
                self.assertIsInstance(factory.get_bytes_as(factory.storage_kind),
 
232
                    str)
 
233
        self.assertEqual(
 
234
            set([('base',), ('left',), ('right',), ('merged',), ('or',)]),
 
235
            seen)
 
236
 
 
237
    def test_filter_absent_records(self):
 
238
        """Requested missing records can be filter trivially."""
 
239
        f, parents = get_diamond_vf(self.get_file())
 
240
        entries = f.get_record_stream(['merged', 'left', 'right', 'extra', 'base'],
 
241
            'unordered', False)
 
242
        seen = set()
 
243
        self.capture_stream(f, versionedfile.filter_absent(entries), seen.add,
 
244
            parents)
 
245
        self.assertEqual(set([('base',), ('left',), ('right',), ('merged',)]),
 
246
            seen)
 
247
 
 
248
    def test_insert_record_stream_empty(self):
 
249
        """Inserting an empty record stream should work."""
 
250
        f = self.get_file()
 
251
        stream = []
 
252
        f.insert_record_stream([])
 
253
 
 
254
    def assertIdenticalVersionedFile(self, left, right):
 
255
        """Assert that left and right have the same contents."""
 
256
        self.assertEqual(set(left.versions()), set(right.versions()))
 
257
        self.assertEqual(left.get_parent_map(left.versions()),
 
258
            right.get_parent_map(right.versions()))
 
259
        for v in left.versions():
 
260
            self.assertEqual(left.get_text(v), right.get_text(v))
 
261
 
 
262
    def test_insert_record_stream_fulltexts(self):
 
263
        """Any file should accept a stream of fulltexts."""
 
264
        f = self.get_file()
 
265
        weave_vf = WeaveFile('source', get_transport(self.get_url('.')),
 
266
            create=True, get_scope=self.get_transaction)
 
267
        source, _ = get_diamond_vf(weave_vf)
 
268
        stream = source.get_record_stream(source.versions(), 'topological',
 
269
            False)
 
270
        f.insert_record_stream(stream)
 
271
        self.assertIdenticalVersionedFile(f, source)
 
272
 
 
273
    def test_insert_record_stream_fulltexts_noeol(self):
 
274
        """Any file should accept a stream of fulltexts."""
 
275
        f = self.get_file()
 
276
        weave_vf = WeaveFile('source', get_transport(self.get_url('.')),
 
277
            create=True, get_scope=self.get_transaction)
 
278
        source, _ = get_diamond_vf(weave_vf, trailing_eol=False)
 
279
        stream = source.get_record_stream(source.versions(), 'topological',
 
280
            False)
 
281
        f.insert_record_stream(stream)
 
282
        self.assertIdenticalVersionedFile(f, source)
 
283
 
 
284
    def test_insert_record_stream_annotated_knits(self):
 
285
        """Any file should accept a stream from plain knits."""
 
286
        f = self.get_file()
 
287
        source = make_file_knit('source', get_transport(self.get_url('.')),
 
288
            create=True)
 
289
        get_diamond_vf(source)
 
290
        stream = source.get_record_stream(source.versions(), 'topological',
 
291
            False)
 
292
        f.insert_record_stream(stream)
 
293
        self.assertIdenticalVersionedFile(f, source)
 
294
 
 
295
    def test_insert_record_stream_annotated_knits_noeol(self):
 
296
        """Any file should accept a stream from plain knits."""
 
297
        f = self.get_file()
 
298
        source = make_file_knit('source', get_transport(self.get_url('.')),
 
299
            create=True)
 
300
        get_diamond_vf(source, trailing_eol=False)
 
301
        stream = source.get_record_stream(source.versions(), 'topological',
 
302
            False)
 
303
        f.insert_record_stream(stream)
 
304
        self.assertIdenticalVersionedFile(f, source)
 
305
 
 
306
    def test_insert_record_stream_plain_knits(self):
 
307
        """Any file should accept a stream from plain knits."""
 
308
        f = self.get_file()
 
309
        source = make_file_knit('source', get_transport(self.get_url('.')),
 
310
            create=True, factory=KnitPlainFactory())
 
311
        get_diamond_vf(source)
 
312
        stream = source.get_record_stream(source.versions(), 'topological',
 
313
            False)
 
314
        f.insert_record_stream(stream)
 
315
        self.assertIdenticalVersionedFile(f, source)
 
316
 
 
317
    def test_insert_record_stream_plain_knits_noeol(self):
 
318
        """Any file should accept a stream from plain knits."""
 
319
        f = self.get_file()
 
320
        source = make_file_knit('source', get_transport(self.get_url('.')),
 
321
            create=True, factory=KnitPlainFactory())
 
322
        get_diamond_vf(source, trailing_eol=False)
 
323
        stream = source.get_record_stream(source.versions(), 'topological',
 
324
            False)
 
325
        f.insert_record_stream(stream)
 
326
        self.assertIdenticalVersionedFile(f, source)
 
327
 
 
328
    def test_insert_record_stream_existing_keys(self):
 
329
        """Inserting keys already in a file should not error."""
 
330
        f = self.get_file()
 
331
        source = make_file_knit('source', get_transport(self.get_url('.')),
 
332
            create=True, factory=KnitPlainFactory())
 
333
        get_diamond_vf(source)
 
334
        # insert some keys into f.
 
335
        get_diamond_vf(f, left_only=True)
 
336
        stream = source.get_record_stream(source.versions(), 'topological',
 
337
            False)
 
338
        f.insert_record_stream(stream)
 
339
        self.assertIdenticalVersionedFile(f, source)
 
340
 
 
341
    def test_insert_record_stream_missing_keys(self):
 
342
        """Inserting a stream with absent keys should raise an error."""
 
343
        f = self.get_file()
 
344
        source = make_file_knit('source', get_transport(self.get_url('.')),
 
345
            create=True, factory=KnitPlainFactory())
 
346
        stream = source.get_record_stream(['missing'], 'topological',
 
347
            False)
 
348
        self.assertRaises(errors.RevisionNotPresent, f.insert_record_stream,
 
349
            stream)
 
350
 
 
351
    def test_insert_record_stream_out_of_order(self):
 
352
        """An out of order stream can either error or work."""
 
353
        f, parents = get_diamond_vf(self.get_file())
 
354
        origin_entries = f.get_record_stream(['origin'], 'unordered', False)
 
355
        end_entries = f.get_record_stream(['merged', 'left'],
 
356
            'topological', False)
 
357
        start_entries = f.get_record_stream(['right', 'base'],
 
358
            'topological', False)
 
359
        entries = chain(origin_entries, end_entries, start_entries)
 
360
        target = self.get_file('target')
 
361
        try:
 
362
            target.insert_record_stream(entries)
 
363
        except RevisionNotPresent:
 
364
            # Must not have corrupted the file.
 
365
            target.check()
 
366
        else:
 
367
            self.assertIdenticalVersionedFile(f, target)
 
368
 
 
369
    def test_insert_record_stream_delta_missing_basis_no_corruption(self):
 
370
        """Insertion where a needed basis is not included aborts safely."""
 
371
        # Annotated source - deltas can be used in any knit.
 
372
        source = make_file_knit('source', get_transport(self.get_url('.')),
 
373
            create=True)
 
374
        get_diamond_vf(source)
 
375
        entries = source.get_record_stream(['origin', 'merged'], 'unordered', False)
 
376
        f = self.get_file()
 
377
        self.assertRaises(RevisionNotPresent, f.insert_record_stream, entries)
 
378
        f.check()
 
379
        self.assertFalse(f.has_version('merged'))
 
380
 
 
381
    def test_adds_with_parent_texts(self):
 
382
        f = self.get_file()
 
383
        parent_texts = {}
 
384
        _, _, parent_texts['r0'] = f.add_lines('r0', [], ['a\n', 'b\n'])
 
385
        try:
 
386
            _, _, parent_texts['r1'] = f.add_lines_with_ghosts('r1',
 
387
                ['r0', 'ghost'], ['b\n', 'c\n'], parent_texts=parent_texts)
 
388
        except NotImplementedError:
 
389
            # if the format doesn't support ghosts, just add normally.
 
390
            _, _, parent_texts['r1'] = f.add_lines('r1',
 
391
                ['r0'], ['b\n', 'c\n'], parent_texts=parent_texts)
 
392
        f.add_lines('r2', ['r1'], ['c\n', 'd\n'], parent_texts=parent_texts)
 
393
        self.assertNotEqual(None, parent_texts['r0'])
 
394
        self.assertNotEqual(None, parent_texts['r1'])
 
395
        def verify_file(f):
 
396
            versions = f.versions()
 
397
            self.assertTrue('r0' in versions)
 
398
            self.assertTrue('r1' in versions)
 
399
            self.assertTrue('r2' in versions)
 
400
            self.assertEquals(f.get_lines('r0'), ['a\n', 'b\n'])
 
401
            self.assertEquals(f.get_lines('r1'), ['b\n', 'c\n'])
 
402
            self.assertEquals(f.get_lines('r2'), ['c\n', 'd\n'])
 
403
            self.assertEqual(3, f.num_versions())
 
404
            origins = f.annotate('r1')
 
405
            self.assertEquals(origins[0][0], 'r0')
 
406
            self.assertEquals(origins[1][0], 'r1')
 
407
            origins = f.annotate('r2')
 
408
            self.assertEquals(origins[0][0], 'r1')
 
409
            self.assertEquals(origins[1][0], 'r2')
 
410
 
 
411
        verify_file(f)
 
412
        f = self.reopen_file()
 
413
        verify_file(f)
 
414
 
 
415
    def test_add_unicode_content(self):
 
416
        # unicode content is not permitted in versioned files. 
 
417
        # versioned files version sequences of bytes only.
 
418
        vf = self.get_file()
 
419
        self.assertRaises(errors.BzrBadParameterUnicode,
 
420
            vf.add_lines, 'a', [], ['a\n', u'b\n', 'c\n'])
 
421
        self.assertRaises(
 
422
            (errors.BzrBadParameterUnicode, NotImplementedError),
 
423
            vf.add_lines_with_ghosts, 'a', [], ['a\n', u'b\n', 'c\n'])
 
424
 
 
425
    def test_add_follows_left_matching_blocks(self):
 
426
        """If we change left_matching_blocks, delta changes
 
427
 
 
428
        Note: There are multiple correct deltas in this case, because
 
429
        we start with 1 "a" and we get 3.
 
430
        """
 
431
        vf = self.get_file()
 
432
        if isinstance(vf, WeaveFile):
 
433
            raise TestSkipped("WeaveFile ignores left_matching_blocks")
 
434
        vf.add_lines('1', [], ['a\n'])
 
435
        vf.add_lines('2', ['1'], ['a\n', 'a\n', 'a\n'],
 
436
                     left_matching_blocks=[(0, 0, 1), (1, 3, 0)])
 
437
        self.assertEqual(['a\n', 'a\n', 'a\n'], vf.get_lines('2'))
 
438
        vf.add_lines('3', ['1'], ['a\n', 'a\n', 'a\n'],
 
439
                     left_matching_blocks=[(0, 2, 1), (1, 3, 0)])
 
440
        self.assertEqual(['a\n', 'a\n', 'a\n'], vf.get_lines('3'))
 
441
 
 
442
    def test_inline_newline_throws(self):
 
443
        # \r characters are not permitted in lines being added
 
444
        vf = self.get_file()
 
445
        self.assertRaises(errors.BzrBadParameterContainsNewline, 
 
446
            vf.add_lines, 'a', [], ['a\n\n'])
 
447
        self.assertRaises(
 
448
            (errors.BzrBadParameterContainsNewline, NotImplementedError),
 
449
            vf.add_lines_with_ghosts, 'a', [], ['a\n\n'])
 
450
        # but inline CR's are allowed
 
451
        vf.add_lines('a', [], ['a\r\n'])
 
452
        try:
 
453
            vf.add_lines_with_ghosts('b', [], ['a\r\n'])
 
454
        except NotImplementedError:
 
455
            pass
 
456
 
 
457
    def test_add_reserved(self):
 
458
        vf = self.get_file()
 
459
        self.assertRaises(errors.ReservedId,
 
460
            vf.add_lines, 'a:', [], ['a\n', 'b\n', 'c\n'])
 
461
 
 
462
    def test_add_lines_nostoresha(self):
 
463
        """When nostore_sha is supplied using old content raises."""
 
464
        vf = self.get_file()
 
465
        empty_text = ('a', [])
 
466
        sample_text_nl = ('b', ["foo\n", "bar\n"])
 
467
        sample_text_no_nl = ('c', ["foo\n", "bar"])
 
468
        shas = []
 
469
        for version, lines in (empty_text, sample_text_nl, sample_text_no_nl):
 
470
            sha, _, _ = vf.add_lines(version, [], lines)
 
471
            shas.append(sha)
 
472
        # we now have a copy of all the lines in the vf.
 
473
        for sha, (version, lines) in zip(
 
474
            shas, (empty_text, sample_text_nl, sample_text_no_nl)):
 
475
            self.assertRaises(errors.ExistingContent,
 
476
                vf.add_lines, version + "2", [], lines,
 
477
                nostore_sha=sha)
 
478
            # and no new version should have been added.
 
479
            self.assertRaises(errors.RevisionNotPresent, vf.get_lines,
 
480
                version + "2")
 
481
 
 
482
    def test_add_lines_with_ghosts_nostoresha(self):
 
483
        """When nostore_sha is supplied using old content raises."""
 
484
        vf = self.get_file()
 
485
        empty_text = ('a', [])
 
486
        sample_text_nl = ('b', ["foo\n", "bar\n"])
 
487
        sample_text_no_nl = ('c', ["foo\n", "bar"])
 
488
        shas = []
 
489
        for version, lines in (empty_text, sample_text_nl, sample_text_no_nl):
 
490
            sha, _, _ = vf.add_lines(version, [], lines)
 
491
            shas.append(sha)
 
492
        # we now have a copy of all the lines in the vf.
 
493
        # is the test applicable to this vf implementation?
 
494
        try:
 
495
            vf.add_lines_with_ghosts('d', [], [])
 
496
        except NotImplementedError:
 
497
            raise TestSkipped("add_lines_with_ghosts is optional")
 
498
        for sha, (version, lines) in zip(
 
499
            shas, (empty_text, sample_text_nl, sample_text_no_nl)):
 
500
            self.assertRaises(errors.ExistingContent,
 
501
                vf.add_lines_with_ghosts, version + "2", [], lines,
 
502
                nostore_sha=sha)
 
503
            # and no new version should have been added.
 
504
            self.assertRaises(errors.RevisionNotPresent, vf.get_lines,
 
505
                version + "2")
 
506
 
 
507
    def test_add_lines_return_value(self):
 
508
        # add_lines should return the sha1 and the text size.
 
509
        vf = self.get_file()
 
510
        empty_text = ('a', [])
 
511
        sample_text_nl = ('b', ["foo\n", "bar\n"])
 
512
        sample_text_no_nl = ('c', ["foo\n", "bar"])
 
513
        # check results for the three cases:
 
514
        for version, lines in (empty_text, sample_text_nl, sample_text_no_nl):
 
515
            # the first two elements are the same for all versioned files:
 
516
            # - the digest and the size of the text. For some versioned files
 
517
            #   additional data is returned in additional tuple elements.
 
518
            result = vf.add_lines(version, [], lines)
 
519
            self.assertEqual(3, len(result))
 
520
            self.assertEqual((osutils.sha_strings(lines), sum(map(len, lines))),
 
521
                result[0:2])
 
522
        # parents should not affect the result:
 
523
        lines = sample_text_nl[1]
 
524
        self.assertEqual((osutils.sha_strings(lines), sum(map(len, lines))),
 
525
            vf.add_lines('d', ['b', 'c'], lines)[0:2])
 
526
 
 
527
    def test_get_reserved(self):
 
528
        vf = self.get_file()
 
529
        self.assertRaises(errors.ReservedId, vf.get_texts, ['b:'])
 
530
        self.assertRaises(errors.ReservedId, vf.get_lines, 'b:')
 
531
        self.assertRaises(errors.ReservedId, vf.get_text, 'b:')
 
532
 
 
533
    def test_make_mpdiffs(self):
 
534
        from bzrlib import multiparent
 
535
        vf = self.get_file('foo')
 
536
        sha1s = self._setup_for_deltas(vf)
 
537
        new_vf = self.get_file('bar')
 
538
        for version in multiparent.topo_iter(vf):
 
539
            mpdiff = vf.make_mpdiffs([version])[0]
 
540
            new_vf.add_mpdiffs([(version, vf.get_parent_map([version])[version],
 
541
                                 vf.get_sha1s([version])[0], mpdiff)])
 
542
            self.assertEqualDiff(vf.get_text(version),
 
543
                                 new_vf.get_text(version))
 
544
 
 
545
    def _setup_for_deltas(self, f):
 
546
        self.assertFalse(f.has_version('base'))
 
547
        # add texts that should trip the knit maximum delta chain threshold
 
548
        # as well as doing parallel chains of data in knits.
 
549
        # this is done by two chains of 25 insertions
 
550
        f.add_lines('base', [], ['line\n'])
 
551
        f.add_lines('noeol', ['base'], ['line'])
 
552
        # detailed eol tests:
 
553
        # shared last line with parent no-eol
 
554
        f.add_lines('noeolsecond', ['noeol'], ['line\n', 'line'])
 
555
        # differing last line with parent, both no-eol
 
556
        f.add_lines('noeolnotshared', ['noeolsecond'], ['line\n', 'phone'])
 
557
        # add eol following a noneol parent, change content
 
558
        f.add_lines('eol', ['noeol'], ['phone\n'])
 
559
        # add eol following a noneol parent, no change content
 
560
        f.add_lines('eolline', ['noeol'], ['line\n'])
 
561
        # noeol with no parents:
 
562
        f.add_lines('noeolbase', [], ['line'])
 
563
        # noeol preceeding its leftmost parent in the output:
 
564
        # this is done by making it a merge of two parents with no common
 
565
        # anestry: noeolbase and noeol with the 
 
566
        # later-inserted parent the leftmost.
 
567
        f.add_lines('eolbeforefirstparent', ['noeolbase', 'noeol'], ['line'])
 
568
        # two identical eol texts
 
569
        f.add_lines('noeoldup', ['noeol'], ['line'])
 
570
        next_parent = 'base'
 
571
        text_name = 'chain1-'
 
572
        text = ['line\n']
 
573
        sha1s = {0 :'da6d3141cb4a5e6f464bf6e0518042ddc7bfd079',
 
574
                 1 :'45e21ea146a81ea44a821737acdb4f9791c8abe7',
 
575
                 2 :'e1f11570edf3e2a070052366c582837a4fe4e9fa',
 
576
                 3 :'26b4b8626da827088c514b8f9bbe4ebf181edda1',
 
577
                 4 :'e28a5510be25ba84d31121cff00956f9970ae6f6',
 
578
                 5 :'d63ec0ce22e11dcf65a931b69255d3ac747a318d',
 
579
                 6 :'2c2888d288cb5e1d98009d822fedfe6019c6a4ea',
 
580
                 7 :'95c14da9cafbf828e3e74a6f016d87926ba234ab',
 
581
                 8 :'779e9a0b28f9f832528d4b21e17e168c67697272',
 
582
                 9 :'1f8ff4e5c6ff78ac106fcfe6b1e8cb8740ff9a8f',
 
583
                 10:'131a2ae712cf51ed62f143e3fbac3d4206c25a05',
 
584
                 11:'c5a9d6f520d2515e1ec401a8f8a67e6c3c89f199',
 
585
                 12:'31a2286267f24d8bedaa43355f8ad7129509ea85',
 
586
                 13:'dc2a7fe80e8ec5cae920973973a8ee28b2da5e0a',
 
587
                 14:'2c4b1736566b8ca6051e668de68650686a3922f2',
 
588
                 15:'5912e4ecd9b0c07be4d013e7e2bdcf9323276cde',
 
589
                 16:'b0d2e18d3559a00580f6b49804c23fea500feab3',
 
590
                 17:'8e1d43ad72f7562d7cb8f57ee584e20eb1a69fc7',
 
591
                 18:'5cf64a3459ae28efa60239e44b20312d25b253f3',
 
592
                 19:'1ebed371807ba5935958ad0884595126e8c4e823',
 
593
                 20:'2aa62a8b06fb3b3b892a3292a068ade69d5ee0d3',
 
594
                 21:'01edc447978004f6e4e962b417a4ae1955b6fe5d',
 
595
                 22:'d8d8dc49c4bf0bab401e0298bb5ad827768618bb',
 
596
                 23:'c21f62b1c482862983a8ffb2b0c64b3451876e3f',
 
597
                 24:'c0593fe795e00dff6b3c0fe857a074364d5f04fc',
 
598
                 25:'dd1a1cf2ba9cc225c3aff729953e6364bf1d1855',
 
599
                 }
 
600
        for depth in range(26):
 
601
            new_version = text_name + '%s' % depth
 
602
            text = text + ['line\n']
 
603
            f.add_lines(new_version, [next_parent], text)
 
604
            next_parent = new_version
 
605
        next_parent = 'base'
 
606
        text_name = 'chain2-'
 
607
        text = ['line\n']
 
608
        for depth in range(26):
 
609
            new_version = text_name + '%s' % depth
 
610
            text = text + ['line\n']
 
611
            f.add_lines(new_version, [next_parent], text)
 
612
            next_parent = new_version
 
613
        return sha1s
 
614
 
 
615
    def test_ancestry(self):
 
616
        f = self.get_file()
 
617
        self.assertEqual([], f.get_ancestry([]))
 
618
        f.add_lines('r0', [], ['a\n', 'b\n'])
 
619
        f.add_lines('r1', ['r0'], ['b\n', 'c\n'])
 
620
        f.add_lines('r2', ['r0'], ['b\n', 'c\n'])
 
621
        f.add_lines('r3', ['r2'], ['b\n', 'c\n'])
 
622
        f.add_lines('rM', ['r1', 'r2'], ['b\n', 'c\n'])
 
623
        self.assertEqual([], f.get_ancestry([]))
 
624
        versions = f.get_ancestry(['rM'])
 
625
        # there are some possibilities:
 
626
        # r0 r1 r2 rM r3
 
627
        # r0 r1 r2 r3 rM
 
628
        # etc
 
629
        # so we check indexes
 
630
        r0 = versions.index('r0')
 
631
        r1 = versions.index('r1')
 
632
        r2 = versions.index('r2')
 
633
        self.assertFalse('r3' in versions)
 
634
        rM = versions.index('rM')
 
635
        self.assertTrue(r0 < r1)
 
636
        self.assertTrue(r0 < r2)
 
637
        self.assertTrue(r1 < rM)
 
638
        self.assertTrue(r2 < rM)
 
639
 
 
640
        self.assertRaises(RevisionNotPresent,
 
641
            f.get_ancestry, ['rM', 'rX'])
 
642
 
 
643
        self.assertEqual(set(f.get_ancestry('rM')),
 
644
            set(f.get_ancestry('rM', topo_sorted=False)))
 
645
 
 
646
    def test_mutate_after_finish(self):
 
647
        self._transaction = 'before'
 
648
        f = self.get_file()
 
649
        self._transaction = 'after'
 
650
        self.assertRaises(errors.OutSideTransaction, f.add_lines, '', [], [])
 
651
        self.assertRaises(errors.OutSideTransaction, f.add_lines_with_ghosts, '', [], [])
 
652
        self.assertRaises(errors.OutSideTransaction, self.applyDeprecated,
 
653
            one_five, f.join, '')
 
654
        
 
655
    def test_clone_text(self):
 
656
        f = self.get_file()
 
657
        f.add_lines('r0', [], ['a\n', 'b\n'])
 
658
        self.applyDeprecated(one_four, f.clone_text, 'r1', 'r0', ['r0'])
 
659
        def verify_file(f):
 
660
            self.assertEquals(f.get_lines('r1'), f.get_lines('r0'))
 
661
            self.assertEquals(f.get_lines('r1'), ['a\n', 'b\n'])
 
662
            self.assertEqual({'r1':('r0',)}, f.get_parent_map(['r1']))
 
663
            self.assertRaises(RevisionNotPresent,
 
664
                self.applyDeprecated, one_four, f.clone_text, 'r2', 'rX', [])
 
665
            self.assertRaises(RevisionAlreadyPresent,
 
666
                self.applyDeprecated, one_four, f.clone_text, 'r1', 'r0', [])
 
667
        verify_file(f)
 
668
        verify_file(self.reopen_file())
 
669
 
 
670
    def test_copy_to(self):
 
671
        f = self.get_file()
 
672
        f.add_lines('0', [], ['a\n'])
 
673
        t = MemoryTransport()
 
674
        f.copy_to('foo', t)
 
675
        for suffix in self.get_factory().get_suffixes():
 
676
            self.assertTrue(t.has('foo' + suffix))
 
677
 
 
678
    def test_get_suffixes(self):
 
679
        f = self.get_file()
 
680
        # and should be a list
 
681
        self.assertTrue(isinstance(self.get_factory().get_suffixes(), list))
 
682
 
 
683
    def build_graph(self, file, graph):
 
684
        for node in topo_sort(graph.items()):
 
685
            file.add_lines(node, graph[node], [])
 
686
 
 
687
    def test_get_graph(self):
 
688
        f = self.get_file()
 
689
        graph = {
 
690
            'v1': (),
 
691
            'v2': ('v1', ),
 
692
            'v3': ('v2', )}
 
693
        self.build_graph(f, graph)
 
694
        self.assertEqual(graph, self.applyDeprecated(one_four, f.get_graph))
 
695
    
 
696
    def test_get_graph_partial(self):
 
697
        f = self.get_file()
 
698
        complex_graph = {}
 
699
        simple_a = {
 
700
            'c': (),
 
701
            'b': ('c', ),
 
702
            'a': ('b', ),
 
703
            }
 
704
        complex_graph.update(simple_a)
 
705
        simple_b = {
 
706
            'c': (),
 
707
            'b': ('c', ),
 
708
            }
 
709
        complex_graph.update(simple_b)
 
710
        simple_gam = {
 
711
            'c': (),
 
712
            'oo': (),
 
713
            'bar': ('oo', 'c'),
 
714
            'gam': ('bar', ),
 
715
            }
 
716
        complex_graph.update(simple_gam)
 
717
        simple_b_gam = {}
 
718
        simple_b_gam.update(simple_gam)
 
719
        simple_b_gam.update(simple_b)
 
720
        self.build_graph(f, complex_graph)
 
721
        self.assertEqual(simple_a, self.applyDeprecated(one_four, f.get_graph,
 
722
            ['a']))
 
723
        self.assertEqual(simple_b, self.applyDeprecated(one_four, f.get_graph,
 
724
            ['b']))
 
725
        self.assertEqual(simple_gam, self.applyDeprecated(one_four,
 
726
            f.get_graph, ['gam']))
 
727
        self.assertEqual(simple_b_gam, self.applyDeprecated(one_four,
 
728
            f.get_graph, ['b', 'gam']))
 
729
 
 
730
    def test_get_parents(self):
 
731
        f = self.get_file()
 
732
        f.add_lines('r0', [], ['a\n', 'b\n'])
 
733
        f.add_lines('r1', [], ['a\n', 'b\n'])
 
734
        f.add_lines('r2', [], ['a\n', 'b\n'])
 
735
        f.add_lines('r3', [], ['a\n', 'b\n'])
 
736
        f.add_lines('m', ['r0', 'r1', 'r2', 'r3'], ['a\n', 'b\n'])
 
737
        self.assertEqual(['r0', 'r1', 'r2', 'r3'],
 
738
            self.applyDeprecated(one_four, f.get_parents, 'm'))
 
739
        self.assertRaises(RevisionNotPresent,
 
740
            self.applyDeprecated, one_four, f.get_parents, 'y')
 
741
 
 
742
    def test_get_parent_map(self):
 
743
        f = self.get_file()
 
744
        f.add_lines('r0', [], ['a\n', 'b\n'])
 
745
        self.assertEqual(
 
746
            {'r0':()}, f.get_parent_map(['r0']))
 
747
        f.add_lines('r1', ['r0'], ['a\n', 'b\n'])
 
748
        self.assertEqual(
 
749
            {'r1':('r0',)}, f.get_parent_map(['r1']))
 
750
        self.assertEqual(
 
751
            {'r0':(),
 
752
             'r1':('r0',)},
 
753
            f.get_parent_map(['r0', 'r1']))
 
754
        f.add_lines('r2', [], ['a\n', 'b\n'])
 
755
        f.add_lines('r3', [], ['a\n', 'b\n'])
 
756
        f.add_lines('m', ['r0', 'r1', 'r2', 'r3'], ['a\n', 'b\n'])
 
757
        self.assertEqual(
 
758
            {'m':('r0', 'r1', 'r2', 'r3')}, f.get_parent_map(['m']))
 
759
        self.assertEqual({}, f.get_parent_map('y'))
 
760
        self.assertEqual(
 
761
            {'r0':(),
 
762
             'r1':('r0',)},
 
763
            f.get_parent_map(['r0', 'y', 'r1']))
 
764
 
 
765
    def test_annotate(self):
 
766
        f = self.get_file()
 
767
        f.add_lines('r0', [], ['a\n', 'b\n'])
 
768
        f.add_lines('r1', ['r0'], ['c\n', 'b\n'])
 
769
        origins = f.annotate('r1')
 
770
        self.assertEquals(origins[0][0], 'r1')
 
771
        self.assertEquals(origins[1][0], 'r0')
 
772
 
 
773
        self.assertRaises(RevisionNotPresent,
 
774
            f.annotate, 'foo')
 
775
 
 
776
    def test_detection(self):
 
777
        # Test weaves detect corruption.
 
778
        #
 
779
        # Weaves contain a checksum of their texts.
 
780
        # When a text is extracted, this checksum should be
 
781
        # verified.
 
782
 
 
783
        w = self.get_file_corrupted_text()
 
784
 
 
785
        self.assertEqual('hello\n', w.get_text('v1'))
 
786
        self.assertRaises(errors.WeaveInvalidChecksum, w.get_text, 'v2')
 
787
        self.assertRaises(errors.WeaveInvalidChecksum, w.get_lines, 'v2')
 
788
        self.assertRaises(errors.WeaveInvalidChecksum, w.check)
 
789
 
 
790
        w = self.get_file_corrupted_checksum()
 
791
 
 
792
        self.assertEqual('hello\n', w.get_text('v1'))
 
793
        self.assertRaises(errors.WeaveInvalidChecksum, w.get_text, 'v2')
 
794
        self.assertRaises(errors.WeaveInvalidChecksum, w.get_lines, 'v2')
 
795
        self.assertRaises(errors.WeaveInvalidChecksum, w.check)
 
796
 
 
797
    def get_file_corrupted_text(self):
 
798
        """Return a versioned file with corrupt text but valid metadata."""
 
799
        raise NotImplementedError(self.get_file_corrupted_text)
 
800
 
 
801
    def reopen_file(self, name='foo'):
 
802
        """Open the versioned file from disk again."""
 
803
        raise NotImplementedError(self.reopen_file)
 
804
 
 
805
    def test_iter_parents(self):
 
806
        """iter_parents returns the parents for many nodes."""
 
807
        f = self.get_file()
 
808
        # sample data:
 
809
        # no parents
 
810
        f.add_lines('r0', [], ['a\n', 'b\n'])
 
811
        # 1 parents
 
812
        f.add_lines('r1', ['r0'], ['a\n', 'b\n'])
 
813
        # 2 parents
 
814
        f.add_lines('r2', ['r1', 'r0'], ['a\n', 'b\n'])
 
815
        # XXX TODO a ghost
 
816
        # cases: each sample data individually:
 
817
        self.assertEqual(set([('r0', ())]),
 
818
            set(self.applyDeprecated(one_four, f.iter_parents, ['r0'])))
 
819
        self.assertEqual(set([('r1', ('r0', ))]),
 
820
            set(self.applyDeprecated(one_four, f.iter_parents, ['r1'])))
 
821
        self.assertEqual(set([('r2', ('r1', 'r0'))]),
 
822
            set(self.applyDeprecated(one_four, f.iter_parents, ['r2'])))
 
823
        # no nodes returned for a missing node
 
824
        self.assertEqual(set(),
 
825
            set(self.applyDeprecated(one_four, f.iter_parents, ['missing'])))
 
826
        # 1 node returned with missing nodes skipped
 
827
        self.assertEqual(set([('r1', ('r0', ))]),
 
828
            set(self.applyDeprecated(one_four, f.iter_parents, ['ghost1', 'r1',
 
829
                'ghost'])))
 
830
        # 2 nodes returned
 
831
        self.assertEqual(set([('r0', ()), ('r1', ('r0', ))]),
 
832
            set(self.applyDeprecated(one_four, f.iter_parents, ['r0', 'r1'])))
 
833
        # 2 nodes returned, missing skipped
 
834
        self.assertEqual(set([('r0', ()), ('r1', ('r0', ))]),
 
835
            set(self.applyDeprecated(one_four, f.iter_parents,
 
836
                ['a', 'r0', 'b', 'r1', 'c'])))
 
837
 
 
838
    def test_iter_lines_added_or_present_in_versions(self):
 
839
        # test that we get at least an equalset of the lines added by
 
840
        # versions in the weave 
 
841
        # the ordering here is to make a tree so that dumb searches have
 
842
        # more changes to muck up.
 
843
 
 
844
        class InstrumentedProgress(progress.DummyProgress):
 
845
 
 
846
            def __init__(self):
 
847
 
 
848
                progress.DummyProgress.__init__(self)
 
849
                self.updates = []
 
850
 
 
851
            def update(self, msg=None, current=None, total=None):
 
852
                self.updates.append((msg, current, total))
 
853
 
 
854
        vf = self.get_file()
 
855
        # add a base to get included
 
856
        vf.add_lines('base', [], ['base\n'])
 
857
        # add a ancestor to be included on one side
 
858
        vf.add_lines('lancestor', [], ['lancestor\n'])
 
859
        # add a ancestor to be included on the other side
 
860
        vf.add_lines('rancestor', ['base'], ['rancestor\n'])
 
861
        # add a child of rancestor with no eofile-nl
 
862
        vf.add_lines('child', ['rancestor'], ['base\n', 'child\n'])
 
863
        # add a child of lancestor and base to join the two roots
 
864
        vf.add_lines('otherchild',
 
865
                     ['lancestor', 'base'],
 
866
                     ['base\n', 'lancestor\n', 'otherchild\n'])
 
867
        def iter_with_versions(versions, expected):
 
868
            # now we need to see what lines are returned, and how often.
 
869
            lines = {}
 
870
            progress = InstrumentedProgress()
 
871
            # iterate over the lines
 
872
            for line in vf.iter_lines_added_or_present_in_versions(versions,
 
873
                pb=progress):
 
874
                lines.setdefault(line, 0)
 
875
                lines[line] += 1
 
876
            if []!= progress.updates:
 
877
                self.assertEqual(expected, progress.updates)
 
878
            return lines
 
879
        lines = iter_with_versions(['child', 'otherchild'],
 
880
                                   [('Walking content.', 0, 2),
 
881
                                    ('Walking content.', 1, 2),
 
882
                                    ('Walking content.', 2, 2)])
 
883
        # we must see child and otherchild
 
884
        self.assertTrue(lines[('child\n', 'child')] > 0)
 
885
        self.assertTrue(lines[('otherchild\n', 'otherchild')] > 0)
 
886
        # we dont care if we got more than that.
 
887
        
 
888
        # test all lines
 
889
        lines = iter_with_versions(None, [('Walking content.', 0, 5),
 
890
                                          ('Walking content.', 1, 5),
 
891
                                          ('Walking content.', 2, 5),
 
892
                                          ('Walking content.', 3, 5),
 
893
                                          ('Walking content.', 4, 5),
 
894
                                          ('Walking content.', 5, 5)])
 
895
        # all lines must be seen at least once
 
896
        self.assertTrue(lines[('base\n', 'base')] > 0)
 
897
        self.assertTrue(lines[('lancestor\n', 'lancestor')] > 0)
 
898
        self.assertTrue(lines[('rancestor\n', 'rancestor')] > 0)
 
899
        self.assertTrue(lines[('child\n', 'child')] > 0)
 
900
        self.assertTrue(lines[('otherchild\n', 'otherchild')] > 0)
 
901
 
 
902
    def test_add_lines_with_ghosts(self):
 
903
        # some versioned file formats allow lines to be added with parent
 
904
        # information that is > than that in the format. Formats that do
 
905
        # not support this need to raise NotImplementedError on the
 
906
        # add_lines_with_ghosts api.
 
907
        vf = self.get_file()
 
908
        # add a revision with ghost parents
 
909
        # The preferred form is utf8, but we should translate when needed
 
910
        parent_id_unicode = u'b\xbfse'
 
911
        parent_id_utf8 = parent_id_unicode.encode('utf8')
 
912
        try:
 
913
            vf.add_lines_with_ghosts('notbxbfse', [parent_id_utf8], [])
 
914
        except NotImplementedError:
 
915
            # check the other ghost apis are also not implemented
 
916
            self.assertRaises(NotImplementedError, vf.get_ancestry_with_ghosts, ['foo'])
 
917
            self.assertRaises(NotImplementedError, vf.get_parents_with_ghosts, 'foo')
 
918
            return
 
919
        vf = self.reopen_file()
 
920
        # test key graph related apis: getncestry, _graph, get_parents
 
921
        # has_version
 
922
        # - these are ghost unaware and must not be reflect ghosts
 
923
        self.assertEqual(['notbxbfse'], vf.get_ancestry('notbxbfse'))
 
924
        self.assertEqual([],
 
925
            self.applyDeprecated(one_four, vf.get_parents, 'notbxbfse'))
 
926
        self.assertEqual({'notbxbfse':()}, self.applyDeprecated(one_four,
 
927
            vf.get_graph))
 
928
        self.assertFalse(vf.has_version(parent_id_utf8))
 
929
        # we have _with_ghost apis to give us ghost information.
 
930
        self.assertEqual([parent_id_utf8, 'notbxbfse'], vf.get_ancestry_with_ghosts(['notbxbfse']))
 
931
        self.assertEqual([parent_id_utf8], vf.get_parents_with_ghosts('notbxbfse'))
 
932
        self.assertEqual({'notbxbfse':(parent_id_utf8,)},
 
933
            self.applyDeprecated(one_four, vf.get_graph_with_ghosts))
 
934
        self.assertTrue(self.applyDeprecated(one_four, vf.has_ghost,
 
935
            parent_id_utf8))
 
936
        # if we add something that is a ghost of another, it should correct the
 
937
        # results of the prior apis
 
938
        vf.add_lines(parent_id_utf8, [], [])
 
939
        self.assertEqual([parent_id_utf8, 'notbxbfse'], vf.get_ancestry(['notbxbfse']))
 
940
        self.assertEqual({'notbxbfse':(parent_id_utf8,)},
 
941
            vf.get_parent_map(['notbxbfse']))
 
942
        self.assertEqual({parent_id_utf8:(),
 
943
                          'notbxbfse':(parent_id_utf8, ),
 
944
                          },
 
945
                         self.applyDeprecated(one_four, vf.get_graph))
 
946
        self.assertTrue(vf.has_version(parent_id_utf8))
 
947
        # we have _with_ghost apis to give us ghost information.
 
948
        self.assertEqual([parent_id_utf8, 'notbxbfse'],
 
949
            vf.get_ancestry_with_ghosts(['notbxbfse']))
 
950
        self.assertEqual([parent_id_utf8], vf.get_parents_with_ghosts('notbxbfse'))
 
951
        self.assertEqual({parent_id_utf8:(),
 
952
                          'notbxbfse':(parent_id_utf8,),
 
953
                          },
 
954
            self.applyDeprecated(one_four, vf.get_graph_with_ghosts))
 
955
        self.assertFalse(self.applyDeprecated(one_four, vf.has_ghost,
 
956
            parent_id_utf8))
 
957
 
 
958
    def test_add_lines_with_ghosts_after_normal_revs(self):
 
959
        # some versioned file formats allow lines to be added with parent
 
960
        # information that is > than that in the format. Formats that do
 
961
        # not support this need to raise NotImplementedError on the
 
962
        # add_lines_with_ghosts api.
 
963
        vf = self.get_file()
 
964
        # probe for ghost support
 
965
        try:
 
966
            vf.add_lines_with_ghosts('base', [], ['line\n', 'line_b\n'])
 
967
        except NotImplementedError:
 
968
            return
 
969
        vf.add_lines_with_ghosts('references_ghost',
 
970
                                 ['base', 'a_ghost'],
 
971
                                 ['line\n', 'line_b\n', 'line_c\n'])
 
972
        origins = vf.annotate('references_ghost')
 
973
        self.assertEquals(('base', 'line\n'), origins[0])
 
974
        self.assertEquals(('base', 'line_b\n'), origins[1])
 
975
        self.assertEquals(('references_ghost', 'line_c\n'), origins[2])
 
976
 
 
977
    def test_readonly_mode(self):
 
978
        transport = get_transport(self.get_url('.'))
 
979
        factory = self.get_factory()
 
980
        vf = factory('id', transport, 0777, create=True, access_mode='w')
 
981
        vf = factory('id', transport, access_mode='r')
 
982
        self.assertRaises(errors.ReadOnlyError, vf.add_lines, 'base', [], [])
 
983
        self.assertRaises(errors.ReadOnlyError,
 
984
                          vf.add_lines_with_ghosts,
 
985
                          'base',
 
986
                          [],
 
987
                          [])
 
988
        self.assertRaises(errors.ReadOnlyError, self.applyDeprecated, one_five,
 
989
            vf.join, 'base')
 
990
    
 
991
    def test_get_sha1s(self):
 
992
        # check the sha1 data is available
 
993
        vf = self.get_file()
 
994
        # a simple file
 
995
        vf.add_lines('a', [], ['a\n'])
 
996
        # the same file, different metadata
 
997
        vf.add_lines('b', ['a'], ['a\n'])
 
998
        # a file differing only in last newline.
 
999
        vf.add_lines('c', [], ['a'])
 
1000
        # Deprecasted single-version API.
 
1001
        self.assertEqual(
 
1002
            '3f786850e387550fdab836ed7e6dc881de23001b',
 
1003
            self.applyDeprecated(one_four, vf.get_sha1, 'a'))
 
1004
        self.assertEqual(
 
1005
            '3f786850e387550fdab836ed7e6dc881de23001b',
 
1006
            self.applyDeprecated(one_four, vf.get_sha1, 'b'))
 
1007
        self.assertEqual(
 
1008
            '86f7e437faa5a7fce15d1ddcb9eaeaea377667b8',
 
1009
            self.applyDeprecated(one_four, vf.get_sha1, 'c'))
 
1010
        self.assertEqual(['3f786850e387550fdab836ed7e6dc881de23001b',
 
1011
                          '86f7e437faa5a7fce15d1ddcb9eaeaea377667b8',
 
1012
                          '3f786850e387550fdab836ed7e6dc881de23001b'],
 
1013
                          vf.get_sha1s(['a', 'c', 'b']))
 
1014
        
 
1015
 
 
1016
class TestWeave(TestCaseWithMemoryTransport, VersionedFileTestMixIn):
 
1017
 
 
1018
    def get_file(self, name='foo'):
 
1019
        return WeaveFile(name, get_transport(self.get_url('.')), create=True,
 
1020
            get_scope=self.get_transaction)
 
1021
 
 
1022
    def get_file_corrupted_text(self):
 
1023
        w = WeaveFile('foo', get_transport(self.get_url('.')), create=True,
 
1024
            get_scope=self.get_transaction)
 
1025
        w.add_lines('v1', [], ['hello\n'])
 
1026
        w.add_lines('v2', ['v1'], ['hello\n', 'there\n'])
 
1027
        
 
1028
        # We are going to invasively corrupt the text
 
1029
        # Make sure the internals of weave are the same
 
1030
        self.assertEqual([('{', 0)
 
1031
                        , 'hello\n'
 
1032
                        , ('}', None)
 
1033
                        , ('{', 1)
 
1034
                        , 'there\n'
 
1035
                        , ('}', None)
 
1036
                        ], w._weave)
 
1037
        
 
1038
        self.assertEqual(['f572d396fae9206628714fb2ce00f72e94f2258f'
 
1039
                        , '90f265c6e75f1c8f9ab76dcf85528352c5f215ef'
 
1040
                        ], w._sha1s)
 
1041
        w.check()
 
1042
        
 
1043
        # Corrupted
 
1044
        w._weave[4] = 'There\n'
 
1045
        return w
 
1046
 
 
1047
    def get_file_corrupted_checksum(self):
 
1048
        w = self.get_file_corrupted_text()
 
1049
        # Corrected
 
1050
        w._weave[4] = 'there\n'
 
1051
        self.assertEqual('hello\nthere\n', w.get_text('v2'))
 
1052
        
 
1053
        #Invalid checksum, first digit changed
 
1054
        w._sha1s[1] =  'f0f265c6e75f1c8f9ab76dcf85528352c5f215ef'
 
1055
        return w
 
1056
 
 
1057
    def reopen_file(self, name='foo', create=False):
 
1058
        return WeaveFile(name, get_transport(self.get_url('.')), create=create,
 
1059
            get_scope=self.get_transaction)
 
1060
 
 
1061
    def test_no_implicit_create(self):
 
1062
        self.assertRaises(errors.NoSuchFile,
 
1063
                          WeaveFile,
 
1064
                          'foo',
 
1065
                          get_transport(self.get_url('.')),
 
1066
                          get_scope=self.get_transaction)
 
1067
 
 
1068
    def get_factory(self):
 
1069
        return WeaveFile
 
1070
 
 
1071
 
 
1072
class TestKnit(TestCaseWithMemoryTransport, VersionedFileTestMixIn):
 
1073
 
 
1074
    def get_file(self, name='foo', create=True):
 
1075
        return make_file_knit(name, get_transport(self.get_url('.')),
 
1076
            delta=True, create=True, get_scope=self.get_transaction)
 
1077
 
 
1078
    def get_factory(self):
 
1079
        return make_file_knit
 
1080
 
 
1081
    def get_file_corrupted_text(self):
 
1082
        knit = self.get_file()
 
1083
        knit.add_lines('v1', [], ['hello\n'])
 
1084
        knit.add_lines('v2', ['v1'], ['hello\n', 'there\n'])
 
1085
        return knit
 
1086
 
 
1087
    def reopen_file(self, name='foo', create=False):
 
1088
        return self.get_file(name, create)
 
1089
 
 
1090
    def test_detection(self):
 
1091
        knit = self.get_file()
 
1092
        knit.check()
 
1093
 
 
1094
    def test_no_implicit_create(self):
 
1095
        self.assertRaises(errors.NoSuchFile, self.get_factory(), 'foo',
 
1096
            get_transport(self.get_url('.')))
 
1097
 
 
1098
 
 
1099
class TestPlaintextKnit(TestKnit):
 
1100
    """Test a knit with no cached annotations"""
 
1101
 
 
1102
    def get_file(self, name='foo', create=True):
 
1103
        return make_file_knit(name, get_transport(self.get_url('.')),
 
1104
            delta=True, create=create, get_scope=self.get_transaction,
 
1105
            factory=_mod_knit.KnitPlainFactory())
 
1106
 
 
1107
 
 
1108
class TestPlanMergeVersionedFile(TestCaseWithMemoryTransport):
 
1109
 
 
1110
    def setUp(self):
 
1111
        TestCaseWithMemoryTransport.setUp(self)
 
1112
        self.vf1 = make_file_knit('root', self.get_transport(), create=True)
 
1113
        self.vf2 = make_file_knit('root', self.get_transport(), create=True)
 
1114
        self.plan_merge_vf = versionedfile._PlanMergeVersionedFile('root',
 
1115
            [self.vf1, self.vf2])
 
1116
 
 
1117
    def test_add_lines(self):
 
1118
        self.plan_merge_vf.add_lines('a:', [], [])
 
1119
        self.assertRaises(ValueError, self.plan_merge_vf.add_lines, 'a', [],
 
1120
                          [])
 
1121
        self.assertRaises(ValueError, self.plan_merge_vf.add_lines, 'a:', None,
 
1122
                          [])
 
1123
        self.assertRaises(ValueError, self.plan_merge_vf.add_lines, 'a:', [],
 
1124
                          None)
 
1125
 
 
1126
    def test_ancestry(self):
 
1127
        self.vf1.add_lines('A', [], [])
 
1128
        self.vf1.add_lines('B', ['A'], [])
 
1129
        self.plan_merge_vf.add_lines('C:', ['B'], [])
 
1130
        self.plan_merge_vf.add_lines('D:', ['C:'], [])
 
1131
        self.assertEqual(set(['A', 'B', 'C:', 'D:']),
 
1132
            self.plan_merge_vf.get_ancestry('D:', topo_sorted=False))
 
1133
 
 
1134
    def setup_abcde(self):
 
1135
        self.vf1.add_lines('A', [], ['a'])
 
1136
        self.vf1.add_lines('B', ['A'], ['b'])
 
1137
        self.vf2.add_lines('C', [], ['c'])
 
1138
        self.vf2.add_lines('D', ['C'], ['d'])
 
1139
        self.plan_merge_vf.add_lines('E:', ['B', 'D'], ['e'])
 
1140
 
 
1141
    def test_ancestry_uses_all_versionedfiles(self):
 
1142
        self.setup_abcde()
 
1143
        self.assertEqual(set(['A', 'B', 'C', 'D', 'E:']),
 
1144
            self.plan_merge_vf.get_ancestry('E:', topo_sorted=False))
 
1145
 
 
1146
    def test_ancestry_raises_revision_not_present(self):
 
1147
        error = self.assertRaises(errors.RevisionNotPresent,
 
1148
                                  self.plan_merge_vf.get_ancestry, 'E:', False)
 
1149
        self.assertContainsRe(str(error), '{E:} not present in "root"')
 
1150
 
 
1151
    def test_get_parents(self):
 
1152
        self.setup_abcde()
 
1153
        self.assertEqual({'B':('A',)}, self.plan_merge_vf.get_parent_map(['B']))
 
1154
        self.assertEqual({'D':('C',)}, self.plan_merge_vf.get_parent_map(['D']))
 
1155
        self.assertEqual({'E:':('B', 'D')},
 
1156
            self.plan_merge_vf.get_parent_map(['E:']))
 
1157
        self.assertEqual({}, self.plan_merge_vf.get_parent_map(['F']))
 
1158
        self.assertEqual({
 
1159
                'B':('A',),
 
1160
                'D':('C',),
 
1161
                'E:':('B', 'D'),
 
1162
                }, self.plan_merge_vf.get_parent_map(['B', 'D', 'E:', 'F']))
 
1163
 
 
1164
    def test_get_lines(self):
 
1165
        self.setup_abcde()
 
1166
        self.assertEqual(['a'], self.plan_merge_vf.get_lines('A'))
 
1167
        self.assertEqual(['c'], self.plan_merge_vf.get_lines('C'))
 
1168
        self.assertEqual(['e'], self.plan_merge_vf.get_lines('E:'))
 
1169
        error = self.assertRaises(errors.RevisionNotPresent,
 
1170
                                  self.plan_merge_vf.get_lines, 'F')
 
1171
        self.assertContainsRe(str(error), '{F} not present in "root"')
 
1172
 
 
1173
 
 
1174
class InterString(versionedfile.InterVersionedFile):
 
1175
    """An inter-versionedfile optimised code path for strings.
 
1176
 
 
1177
    This is for use during testing where we use strings as versionedfiles
 
1178
    so that none of the default regsitered interversionedfile classes will
 
1179
    match - which lets us test the match logic.
 
1180
    """
 
1181
 
 
1182
    @staticmethod
 
1183
    def is_compatible(source, target):
 
1184
        """InterString is compatible with strings-as-versionedfiles."""
 
1185
        return isinstance(source, str) and isinstance(target, str)
 
1186
 
 
1187
 
 
1188
# TODO this and the InterRepository core logic should be consolidatable
 
1189
# if we make the registry a separate class though we still need to 
 
1190
# test the behaviour in the active registry to catch failure-to-handle-
 
1191
# stange-objects
 
1192
class TestInterVersionedFile(TestCaseWithMemoryTransport):
 
1193
 
 
1194
    def test_get_default_inter_versionedfile(self):
 
1195
        # test that the InterVersionedFile.get(a, b) probes
 
1196
        # for a class where is_compatible(a, b) returns
 
1197
        # true and returns a default interversionedfile otherwise.
 
1198
        # This also tests that the default registered optimised interversionedfile
 
1199
        # classes do not barf inappropriately when a surprising versionedfile type
 
1200
        # is handed to them.
 
1201
        dummy_a = "VersionedFile 1."
 
1202
        dummy_b = "VersionedFile 2."
 
1203
        self.assertGetsDefaultInterVersionedFile(dummy_a, dummy_b)
 
1204
 
 
1205
    def assertGetsDefaultInterVersionedFile(self, a, b):
 
1206
        """Asserts that InterVersionedFile.get(a, b) -> the default."""
 
1207
        inter = versionedfile.InterVersionedFile.get(a, b)
 
1208
        self.assertEqual(versionedfile.InterVersionedFile,
 
1209
                         inter.__class__)
 
1210
        self.assertEqual(a, inter.source)
 
1211
        self.assertEqual(b, inter.target)
 
1212
 
 
1213
    def test_register_inter_versionedfile_class(self):
 
1214
        # test that a optimised code path provider - a
 
1215
        # InterVersionedFile subclass can be registered and unregistered
 
1216
        # and that it is correctly selected when given a versionedfile
 
1217
        # pair that it returns true on for the is_compatible static method
 
1218
        # check
 
1219
        dummy_a = "VersionedFile 1."
 
1220
        dummy_b = "VersionedFile 2."
 
1221
        versionedfile.InterVersionedFile.register_optimiser(InterString)
 
1222
        try:
 
1223
            # we should get the default for something InterString returns False
 
1224
            # to
 
1225
            self.assertFalse(InterString.is_compatible(dummy_a, None))
 
1226
            self.assertGetsDefaultInterVersionedFile(dummy_a, None)
 
1227
            # and we should get an InterString for a pair it 'likes'
 
1228
            self.assertTrue(InterString.is_compatible(dummy_a, dummy_b))
 
1229
            inter = versionedfile.InterVersionedFile.get(dummy_a, dummy_b)
 
1230
            self.assertEqual(InterString, inter.__class__)
 
1231
            self.assertEqual(dummy_a, inter.source)
 
1232
            self.assertEqual(dummy_b, inter.target)
 
1233
        finally:
 
1234
            versionedfile.InterVersionedFile.unregister_optimiser(InterString)
 
1235
        # now we should get the default InterVersionedFile object again.
 
1236
        self.assertGetsDefaultInterVersionedFile(dummy_a, dummy_b)
 
1237
 
 
1238
 
 
1239
class TestReadonlyHttpMixin(object):
 
1240
 
 
1241
    def get_transaction(self):
 
1242
        return 1
 
1243
 
 
1244
    def test_readonly_http_works(self):
 
1245
        # we should be able to read from http with a versioned file.
 
1246
        vf = self.get_file()
 
1247
        # try an empty file access
 
1248
        readonly_vf = self.get_factory()('foo', get_transport(self.get_readonly_url('.')))
 
1249
        self.assertEqual([], readonly_vf.versions())
 
1250
        # now with feeling.
 
1251
        vf.add_lines('1', [], ['a\n'])
 
1252
        vf.add_lines('2', ['1'], ['b\n', 'a\n'])
 
1253
        readonly_vf = self.get_factory()('foo', get_transport(self.get_readonly_url('.')))
 
1254
        self.assertEqual(['1', '2'], vf.versions())
 
1255
        for version in readonly_vf.versions():
 
1256
            readonly_vf.get_lines(version)
 
1257
 
 
1258
 
 
1259
class TestWeaveHTTP(TestCaseWithWebserver, TestReadonlyHttpMixin):
 
1260
 
 
1261
    def get_file(self):
 
1262
        return WeaveFile('foo', get_transport(self.get_url('.')), create=True,
 
1263
            get_scope=self.get_transaction)
 
1264
 
 
1265
    def get_factory(self):
 
1266
        return WeaveFile
 
1267
 
 
1268
 
 
1269
class TestKnitHTTP(TestCaseWithWebserver, TestReadonlyHttpMixin):
 
1270
 
 
1271
    def get_file(self):
 
1272
        return make_file_knit('foo', get_transport(self.get_url('.')),
 
1273
            delta=True, create=True, get_scope=self.get_transaction)
 
1274
 
 
1275
    def get_factory(self):
 
1276
        return make_file_knit
 
1277
 
 
1278
 
 
1279
class MergeCasesMixin(object):
 
1280
 
 
1281
    def doMerge(self, base, a, b, mp):
 
1282
        from cStringIO import StringIO
 
1283
        from textwrap import dedent
 
1284
 
 
1285
        def addcrlf(x):
 
1286
            return x + '\n'
 
1287
        
 
1288
        w = self.get_file()
 
1289
        w.add_lines('text0', [], map(addcrlf, base))
 
1290
        w.add_lines('text1', ['text0'], map(addcrlf, a))
 
1291
        w.add_lines('text2', ['text0'], map(addcrlf, b))
 
1292
 
 
1293
        self.log_contents(w)
 
1294
 
 
1295
        self.log('merge plan:')
 
1296
        p = list(w.plan_merge('text1', 'text2'))
 
1297
        for state, line in p:
 
1298
            if line:
 
1299
                self.log('%12s | %s' % (state, line[:-1]))
 
1300
 
 
1301
        self.log('merge:')
 
1302
        mt = StringIO()
 
1303
        mt.writelines(w.weave_merge(p))
 
1304
        mt.seek(0)
 
1305
        self.log(mt.getvalue())
 
1306
 
 
1307
        mp = map(addcrlf, mp)
 
1308
        self.assertEqual(mt.readlines(), mp)
 
1309
        
 
1310
        
 
1311
    def testOneInsert(self):
 
1312
        self.doMerge([],
 
1313
                     ['aa'],
 
1314
                     [],
 
1315
                     ['aa'])
 
1316
 
 
1317
    def testSeparateInserts(self):
 
1318
        self.doMerge(['aaa', 'bbb', 'ccc'],
 
1319
                     ['aaa', 'xxx', 'bbb', 'ccc'],
 
1320
                     ['aaa', 'bbb', 'yyy', 'ccc'],
 
1321
                     ['aaa', 'xxx', 'bbb', 'yyy', 'ccc'])
 
1322
 
 
1323
    def testSameInsert(self):
 
1324
        self.doMerge(['aaa', 'bbb', 'ccc'],
 
1325
                     ['aaa', 'xxx', 'bbb', 'ccc'],
 
1326
                     ['aaa', 'xxx', 'bbb', 'yyy', 'ccc'],
 
1327
                     ['aaa', 'xxx', 'bbb', 'yyy', 'ccc'])
 
1328
    overlappedInsertExpected = ['aaa', 'xxx', 'yyy', 'bbb']
 
1329
    def testOverlappedInsert(self):
 
1330
        self.doMerge(['aaa', 'bbb'],
 
1331
                     ['aaa', 'xxx', 'yyy', 'bbb'],
 
1332
                     ['aaa', 'xxx', 'bbb'], self.overlappedInsertExpected)
 
1333
 
 
1334
        # really it ought to reduce this to 
 
1335
        # ['aaa', 'xxx', 'yyy', 'bbb']
 
1336
 
 
1337
 
 
1338
    def testClashReplace(self):
 
1339
        self.doMerge(['aaa'],
 
1340
                     ['xxx'],
 
1341
                     ['yyy', 'zzz'],
 
1342
                     ['<<<<<<< ', 'xxx', '=======', 'yyy', 'zzz', 
 
1343
                      '>>>>>>> '])
 
1344
 
 
1345
    def testNonClashInsert1(self):
 
1346
        self.doMerge(['aaa'],
 
1347
                     ['xxx', 'aaa'],
 
1348
                     ['yyy', 'zzz'],
 
1349
                     ['<<<<<<< ', 'xxx', 'aaa', '=======', 'yyy', 'zzz', 
 
1350
                      '>>>>>>> '])
 
1351
 
 
1352
    def testNonClashInsert2(self):
 
1353
        self.doMerge(['aaa'],
 
1354
                     ['aaa'],
 
1355
                     ['yyy', 'zzz'],
 
1356
                     ['yyy', 'zzz'])
 
1357
 
 
1358
 
 
1359
    def testDeleteAndModify(self):
 
1360
        """Clashing delete and modification.
 
1361
 
 
1362
        If one side modifies a region and the other deletes it then
 
1363
        there should be a conflict with one side blank.
 
1364
        """
 
1365
 
 
1366
        #######################################
 
1367
        # skippd, not working yet
 
1368
        return
 
1369
        
 
1370
        self.doMerge(['aaa', 'bbb', 'ccc'],
 
1371
                     ['aaa', 'ddd', 'ccc'],
 
1372
                     ['aaa', 'ccc'],
 
1373
                     ['<<<<<<<< ', 'aaa', '=======', '>>>>>>> ', 'ccc'])
 
1374
 
 
1375
    def _test_merge_from_strings(self, base, a, b, expected):
 
1376
        w = self.get_file()
 
1377
        w.add_lines('text0', [], base.splitlines(True))
 
1378
        w.add_lines('text1', ['text0'], a.splitlines(True))
 
1379
        w.add_lines('text2', ['text0'], b.splitlines(True))
 
1380
        self.log('merge plan:')
 
1381
        p = list(w.plan_merge('text1', 'text2'))
 
1382
        for state, line in p:
 
1383
            if line:
 
1384
                self.log('%12s | %s' % (state, line[:-1]))
 
1385
        self.log('merge result:')
 
1386
        result_text = ''.join(w.weave_merge(p))
 
1387
        self.log(result_text)
 
1388
        self.assertEqualDiff(result_text, expected)
 
1389
 
 
1390
    def test_weave_merge_conflicts(self):
 
1391
        # does weave merge properly handle plans that end with unchanged?
 
1392
        result = ''.join(self.get_file().weave_merge([('new-a', 'hello\n')]))
 
1393
        self.assertEqual(result, 'hello\n')
 
1394
 
 
1395
    def test_deletion_extended(self):
 
1396
        """One side deletes, the other deletes more.
 
1397
        """
 
1398
        base = """\
 
1399
            line 1
 
1400
            line 2
 
1401
            line 3
 
1402
            """
 
1403
        a = """\
 
1404
            line 1
 
1405
            line 2
 
1406
            """
 
1407
        b = """\
 
1408
            line 1
 
1409
            """
 
1410
        result = """\
 
1411
            line 1
 
1412
            """
 
1413
        self._test_merge_from_strings(base, a, b, result)
 
1414
 
 
1415
    def test_deletion_overlap(self):
 
1416
        """Delete overlapping regions with no other conflict.
 
1417
 
 
1418
        Arguably it'd be better to treat these as agreement, rather than 
 
1419
        conflict, but for now conflict is safer.
 
1420
        """
 
1421
        base = """\
 
1422
            start context
 
1423
            int a() {}
 
1424
            int b() {}
 
1425
            int c() {}
 
1426
            end context
 
1427
            """
 
1428
        a = """\
 
1429
            start context
 
1430
            int a() {}
 
1431
            end context
 
1432
            """
 
1433
        b = """\
 
1434
            start context
 
1435
            int c() {}
 
1436
            end context
 
1437
            """
 
1438
        result = """\
 
1439
            start context
 
1440
<<<<<<< 
 
1441
            int a() {}
 
1442
=======
 
1443
            int c() {}
 
1444
>>>>>>> 
 
1445
            end context
 
1446
            """
 
1447
        self._test_merge_from_strings(base, a, b, result)
 
1448
 
 
1449
    def test_agreement_deletion(self):
 
1450
        """Agree to delete some lines, without conflicts."""
 
1451
        base = """\
 
1452
            start context
 
1453
            base line 1
 
1454
            base line 2
 
1455
            end context
 
1456
            """
 
1457
        a = """\
 
1458
            start context
 
1459
            base line 1
 
1460
            end context
 
1461
            """
 
1462
        b = """\
 
1463
            start context
 
1464
            base line 1
 
1465
            end context
 
1466
            """
 
1467
        result = """\
 
1468
            start context
 
1469
            base line 1
 
1470
            end context
 
1471
            """
 
1472
        self._test_merge_from_strings(base, a, b, result)
 
1473
 
 
1474
    def test_sync_on_deletion(self):
 
1475
        """Specific case of merge where we can synchronize incorrectly.
 
1476
        
 
1477
        A previous version of the weave merge concluded that the two versions
 
1478
        agreed on deleting line 2, and this could be a synchronization point.
 
1479
        Line 1 was then considered in isolation, and thought to be deleted on 
 
1480
        both sides.
 
1481
 
 
1482
        It's better to consider the whole thing as a disagreement region.
 
1483
        """
 
1484
        base = """\
 
1485
            start context
 
1486
            base line 1
 
1487
            base line 2
 
1488
            end context
 
1489
            """
 
1490
        a = """\
 
1491
            start context
 
1492
            base line 1
 
1493
            a's replacement line 2
 
1494
            end context
 
1495
            """
 
1496
        b = """\
 
1497
            start context
 
1498
            b replaces
 
1499
            both lines
 
1500
            end context
 
1501
            """
 
1502
        result = """\
 
1503
            start context
 
1504
<<<<<<< 
 
1505
            base line 1
 
1506
            a's replacement line 2
 
1507
=======
 
1508
            b replaces
 
1509
            both lines
 
1510
>>>>>>> 
 
1511
            end context
 
1512
            """
 
1513
        self._test_merge_from_strings(base, a, b, result)
 
1514
 
 
1515
 
 
1516
class TestKnitMerge(TestCaseWithMemoryTransport, MergeCasesMixin):
 
1517
 
 
1518
    def get_file(self, name='foo'):
 
1519
        return make_file_knit(name, get_transport(self.get_url('.')),
 
1520
                                 delta=True, create=True)
 
1521
 
 
1522
    def log_contents(self, w):
 
1523
        pass
 
1524
 
 
1525
 
 
1526
class TestWeaveMerge(TestCaseWithMemoryTransport, MergeCasesMixin):
 
1527
 
 
1528
    def get_file(self, name='foo'):
 
1529
        return WeaveFile(name, get_transport(self.get_url('.')), create=True)
 
1530
 
 
1531
    def log_contents(self, w):
 
1532
        self.log('weave is:')
 
1533
        tmpf = StringIO()
 
1534
        write_weave(w, tmpf)
 
1535
        self.log(tmpf.getvalue())
 
1536
 
 
1537
    overlappedInsertExpected = ['aaa', '<<<<<<< ', 'xxx', 'yyy', '=======', 
 
1538
                                'xxx', '>>>>>>> ', 'bbb']
 
1539
 
 
1540
 
 
1541
class TestContentFactoryAdaption(TestCaseWithMemoryTransport):
 
1542
 
 
1543
    def test_select_adaptor(self):
 
1544
        """Test expected adapters exist."""
 
1545
        # One scenario for each lookup combination we expect to use.
 
1546
        # Each is source_kind, requested_kind, adapter class
 
1547
        scenarios = [
 
1548
            ('knit-delta-gz', 'fulltext', _mod_knit.DeltaPlainToFullText),
 
1549
            ('knit-ft-gz', 'fulltext', _mod_knit.FTPlainToFullText),
 
1550
            ('knit-annotated-delta-gz', 'knit-delta-gz',
 
1551
                _mod_knit.DeltaAnnotatedToUnannotated),
 
1552
            ('knit-annotated-delta-gz', 'fulltext',
 
1553
                _mod_knit.DeltaAnnotatedToFullText),
 
1554
            ('knit-annotated-ft-gz', 'knit-ft-gz',
 
1555
                _mod_knit.FTAnnotatedToUnannotated),
 
1556
            ('knit-annotated-ft-gz', 'fulltext',
 
1557
                _mod_knit.FTAnnotatedToFullText),
 
1558
            ]
 
1559
        for source, requested, klass in scenarios:
 
1560
            adapter_factory = versionedfile.adapter_registry.get(
 
1561
                (source, requested))
 
1562
            adapter = adapter_factory(None)
 
1563
            self.assertIsInstance(adapter, klass)
 
1564
 
 
1565
    def get_knit(self, annotated=True):
 
1566
        if annotated:
 
1567
            factory = KnitAnnotateFactory()
 
1568
        else:
 
1569
            factory = KnitPlainFactory()
 
1570
        return make_file_knit('knit', self.get_transport('.'), delta=True,
 
1571
            create=True, factory=factory)
 
1572
 
 
1573
    def helpGetBytes(self, f, ft_adapter, delta_adapter):
 
1574
        """grab the interested adapted texts for tests."""
 
1575
        # origin is a fulltext
 
1576
        entries = f.get_record_stream(['origin'], 'unordered', False)
 
1577
        base = entries.next()
 
1578
        ft_data = ft_adapter.get_bytes(base, base.get_bytes_as(base.storage_kind))
 
1579
        # merged is both a delta and multiple parents.
 
1580
        entries = f.get_record_stream(['merged'], 'unordered', False)
 
1581
        merged = entries.next()
 
1582
        delta_data = delta_adapter.get_bytes(merged,
 
1583
            merged.get_bytes_as(merged.storage_kind))
 
1584
        return ft_data, delta_data
 
1585
 
 
1586
    def test_deannotation_noeol(self):
 
1587
        """Test converting annotated knits to unannotated knits."""
 
1588
        # we need a full text, and a delta
 
1589
        f, parents = get_diamond_vf(self.get_knit(), trailing_eol=False)
 
1590
        ft_data, delta_data = self.helpGetBytes(f,
 
1591
            _mod_knit.FTAnnotatedToUnannotated(None),
 
1592
            _mod_knit.DeltaAnnotatedToUnannotated(None))
 
1593
        self.assertEqual(
 
1594
            'version origin 1 b284f94827db1fa2970d9e2014f080413b547a7e\n'
 
1595
            'origin\n'
 
1596
            'end origin\n',
 
1597
            GzipFile(mode='rb', fileobj=StringIO(ft_data)).read())
 
1598
        self.assertEqual(
 
1599
            'version merged 4 32c2e79763b3f90e8ccde37f9710b6629c25a796\n'
 
1600
            '1,2,3\nleft\nright\nmerged\nend merged\n',
 
1601
            GzipFile(mode='rb', fileobj=StringIO(delta_data)).read())
 
1602
 
 
1603
    def test_deannotation(self):
 
1604
        """Test converting annotated knits to unannotated knits."""
 
1605
        # we need a full text, and a delta
 
1606
        f, parents = get_diamond_vf(self.get_knit())
 
1607
        ft_data, delta_data = self.helpGetBytes(f,
 
1608
            _mod_knit.FTAnnotatedToUnannotated(None),
 
1609
            _mod_knit.DeltaAnnotatedToUnannotated(None))
 
1610
        self.assertEqual(
 
1611
            'version origin 1 00e364d235126be43292ab09cb4686cf703ddc17\n'
 
1612
            'origin\n'
 
1613
            'end origin\n',
 
1614
            GzipFile(mode='rb', fileobj=StringIO(ft_data)).read())
 
1615
        self.assertEqual(
 
1616
            'version merged 3 ed8bce375198ea62444dc71952b22cfc2b09226d\n'
 
1617
            '2,2,2\nright\nmerged\nend merged\n',
 
1618
            GzipFile(mode='rb', fileobj=StringIO(delta_data)).read())
 
1619
 
 
1620
    def test_annotated_to_fulltext_no_eol(self):
 
1621
        """Test adapting annotated knits to full texts (for -> weaves)."""
 
1622
        # we need a full text, and a delta
 
1623
        f, parents = get_diamond_vf(self.get_knit(), trailing_eol=False)
 
1624
        # Reconstructing a full text requires a backing versioned file, and it
 
1625
        # must have the base lines requested from it.
 
1626
        logged_vf = versionedfile.RecordingVersionedFileDecorator(f)
 
1627
        ft_data, delta_data = self.helpGetBytes(f,
 
1628
            _mod_knit.FTAnnotatedToFullText(None),
 
1629
            _mod_knit.DeltaAnnotatedToFullText(logged_vf))
 
1630
        self.assertEqual('origin', ft_data)
 
1631
        self.assertEqual('base\nleft\nright\nmerged', delta_data)
 
1632
        self.assertEqual([('get_lines', 'left')], logged_vf.calls)
 
1633
 
 
1634
    def test_annotated_to_fulltext(self):
 
1635
        """Test adapting annotated knits to full texts (for -> weaves)."""
 
1636
        # we need a full text, and a delta
 
1637
        f, parents = get_diamond_vf(self.get_knit())
 
1638
        # Reconstructing a full text requires a backing versioned file, and it
 
1639
        # must have the base lines requested from it.
 
1640
        logged_vf = versionedfile.RecordingVersionedFileDecorator(f)
 
1641
        ft_data, delta_data = self.helpGetBytes(f,
 
1642
            _mod_knit.FTAnnotatedToFullText(None),
 
1643
            _mod_knit.DeltaAnnotatedToFullText(logged_vf))
 
1644
        self.assertEqual('origin\n', ft_data)
 
1645
        self.assertEqual('base\nleft\nright\nmerged\n', delta_data)
 
1646
        self.assertEqual([('get_lines', 'left')], logged_vf.calls)
 
1647
 
 
1648
    def test_unannotated_to_fulltext(self):
 
1649
        """Test adapting unannotated knits to full texts.
 
1650
        
 
1651
        This is used for -> weaves, and for -> annotated knits.
 
1652
        """
 
1653
        # we need a full text, and a delta
 
1654
        f, parents = get_diamond_vf(self.get_knit(annotated=False))
 
1655
        # Reconstructing a full text requires a backing versioned file, and it
 
1656
        # must have the base lines requested from it.
 
1657
        logged_vf = versionedfile.RecordingVersionedFileDecorator(f)
 
1658
        ft_data, delta_data = self.helpGetBytes(f,
 
1659
            _mod_knit.FTPlainToFullText(None),
 
1660
            _mod_knit.DeltaPlainToFullText(logged_vf))
 
1661
        self.assertEqual('origin\n', ft_data)
 
1662
        self.assertEqual('base\nleft\nright\nmerged\n', delta_data)
 
1663
        self.assertEqual([('get_lines', 'left')], logged_vf.calls)
 
1664
 
 
1665
    def test_unannotated_to_fulltext_no_eol(self):
 
1666
        """Test adapting unannotated knits to full texts.
 
1667
        
 
1668
        This is used for -> weaves, and for -> annotated knits.
 
1669
        """
 
1670
        # we need a full text, and a delta
 
1671
        f, parents = get_diamond_vf(self.get_knit(annotated=False),
 
1672
            trailing_eol=False)
 
1673
        # Reconstructing a full text requires a backing versioned file, and it
 
1674
        # must have the base lines requested from it.
 
1675
        logged_vf = versionedfile.RecordingVersionedFileDecorator(f)
 
1676
        ft_data, delta_data = self.helpGetBytes(f,
 
1677
            _mod_knit.FTPlainToFullText(None),
 
1678
            _mod_knit.DeltaPlainToFullText(logged_vf))
 
1679
        self.assertEqual('origin', ft_data)
 
1680
        self.assertEqual('base\nleft\nright\nmerged', delta_data)
 
1681
        self.assertEqual([('get_lines', 'left')], logged_vf.calls)
 
1682