~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_versionedfile.py

  • Committer: Aaron Bentley
  • Date: 2006-06-21 14:30:57 UTC
  • mfrom: (1801.1.1 bzr.dev)
  • mto: This revision was merged to the branch mainline in revision 1803.
  • Revision ID: abentley@panoramicfeedback.com-20060621143057-776e4b8d707e430e
Install benchmarks. (Jelmer Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2009 Canonical Ltd
 
1
# Copyright (C) 2005 by Canonical Ltd
2
2
#
3
3
# Authors:
4
4
#   Johan Rydberg <jrydberg@gnu.org>
7
7
# it under the terms of the GNU General Public License as published by
8
8
# the Free Software Foundation; either version 2 of the License, or
9
9
# (at your option) any later version.
10
 
#
 
10
 
11
11
# This program is distributed in the hope that it will be useful,
12
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
14
# GNU General Public License for more details.
15
 
#
 
15
 
16
16
# You should have received a copy of the GNU General Public License
17
17
# along with this program; if not, write to the Free Software
18
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
18
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
19
 
20
20
 
21
21
# TODO: might be nice to create a versionedfile with some type of corruption
22
22
# considered typical and check that it can be detected/corrected.
23
23
 
24
 
from itertools import chain, izip
25
24
from StringIO import StringIO
26
25
 
27
 
from bzrlib import (
28
 
    errors,
29
 
    graph as _mod_graph,
30
 
    groupcompress,
31
 
    knit as _mod_knit,
32
 
    osutils,
33
 
    progress,
34
 
    ui,
35
 
    )
 
26
import bzrlib
 
27
import bzrlib.errors as errors
36
28
from bzrlib.errors import (
37
 
                           RevisionNotPresent,
 
29
                           RevisionNotPresent, 
38
30
                           RevisionAlreadyPresent,
39
31
                           WeaveParentMismatch
40
32
                           )
41
 
from bzrlib.knit import (
42
 
    cleanup_pack_knit,
43
 
    make_file_factory,
44
 
    make_pack_factory,
45
 
    KnitAnnotateFactory,
46
 
    KnitPlainFactory,
47
 
    )
48
 
from bzrlib.tests import (
49
 
    TestCase,
50
 
    TestCaseWithMemoryTransport,
51
 
    TestNotApplicable,
52
 
    TestSkipped,
53
 
    condition_isinstance,
54
 
    split_suite_by_condition,
55
 
    multiply_tests,
56
 
    )
57
 
from bzrlib.tests.http_utils import TestCaseWithWebserver
 
33
from bzrlib.knit import KnitVersionedFile, \
 
34
     KnitAnnotateFactory
 
35
from bzrlib.tests import TestCaseWithTransport
 
36
from bzrlib.tests.HTTPTestUtil import TestCaseWithWebserver
58
37
from bzrlib.trace import mutter
59
38
from bzrlib.transport import get_transport
60
39
from bzrlib.transport.memory import MemoryTransport
61
40
from bzrlib.tsort import topo_sort
62
 
from bzrlib.tuned_gzip import GzipFile
63
41
import bzrlib.versionedfile as versionedfile
64
 
from bzrlib.versionedfile import (
65
 
    ConstantMapper,
66
 
    HashEscapedPrefixMapper,
67
 
    PrefixMapper,
68
 
    VirtualVersionedFiles,
69
 
    make_versioned_files_factory,
70
 
    )
71
42
from bzrlib.weave import WeaveFile
72
43
from bzrlib.weavefile import read_weave, write_weave
73
44
 
74
45
 
75
 
def load_tests(standard_tests, module, loader):
76
 
    """Parameterize VersionedFiles tests for different implementations."""
77
 
    to_adapt, result = split_suite_by_condition(
78
 
        standard_tests, condition_isinstance(TestVersionedFiles))
79
 
    # We want to be sure of behaviour for:
80
 
    # weaves prefix layout (weave texts)
81
 
    # individually named weaves (weave inventories)
82
 
    # annotated knits - prefix|hash|hash-escape layout, we test the third only
83
 
    #                   as it is the most complex mapper.
84
 
    # individually named knits
85
 
    # individual no-graph knits in packs (signatures)
86
 
    # individual graph knits in packs (inventories)
87
 
    # individual graph nocompression knits in packs (revisions)
88
 
    # plain text knits in packs (texts)
89
 
    len_one_scenarios = [
90
 
        ('weave-named', {
91
 
            'cleanup':None,
92
 
            'factory':make_versioned_files_factory(WeaveFile,
93
 
                ConstantMapper('inventory')),
94
 
            'graph':True,
95
 
            'key_length':1,
96
 
            'support_partial_insertion': False,
97
 
            }),
98
 
        ('named-knit', {
99
 
            'cleanup':None,
100
 
            'factory':make_file_factory(False, ConstantMapper('revisions')),
101
 
            'graph':True,
102
 
            'key_length':1,
103
 
            'support_partial_insertion': False,
104
 
            }),
105
 
        ('named-nograph-nodelta-knit-pack', {
106
 
            'cleanup':cleanup_pack_knit,
107
 
            'factory':make_pack_factory(False, False, 1),
108
 
            'graph':False,
109
 
            'key_length':1,
110
 
            'support_partial_insertion': False,
111
 
            }),
112
 
        ('named-graph-knit-pack', {
113
 
            'cleanup':cleanup_pack_knit,
114
 
            'factory':make_pack_factory(True, True, 1),
115
 
            'graph':True,
116
 
            'key_length':1,
117
 
            'support_partial_insertion': True,
118
 
            }),
119
 
        ('named-graph-nodelta-knit-pack', {
120
 
            'cleanup':cleanup_pack_knit,
121
 
            'factory':make_pack_factory(True, False, 1),
122
 
            'graph':True,
123
 
            'key_length':1,
124
 
            'support_partial_insertion': False,
125
 
            }),
126
 
        ('groupcompress-nograph', {
127
 
            'cleanup':groupcompress.cleanup_pack_group,
128
 
            'factory':groupcompress.make_pack_factory(False, False, 1),
129
 
            'graph': False,
130
 
            'key_length':1,
131
 
            'support_partial_insertion':False,
132
 
            }),
133
 
        ]
134
 
    len_two_scenarios = [
135
 
        ('weave-prefix', {
136
 
            'cleanup':None,
137
 
            'factory':make_versioned_files_factory(WeaveFile,
138
 
                PrefixMapper()),
139
 
            'graph':True,
140
 
            'key_length':2,
141
 
            'support_partial_insertion': False,
142
 
            }),
143
 
        ('annotated-knit-escape', {
144
 
            'cleanup':None,
145
 
            'factory':make_file_factory(True, HashEscapedPrefixMapper()),
146
 
            'graph':True,
147
 
            'key_length':2,
148
 
            'support_partial_insertion': False,
149
 
            }),
150
 
        ('plain-knit-pack', {
151
 
            'cleanup':cleanup_pack_knit,
152
 
            'factory':make_pack_factory(True, True, 2),
153
 
            'graph':True,
154
 
            'key_length':2,
155
 
            'support_partial_insertion': True,
156
 
            }),
157
 
        ('groupcompress', {
158
 
            'cleanup':groupcompress.cleanup_pack_group,
159
 
            'factory':groupcompress.make_pack_factory(True, False, 1),
160
 
            'graph': True,
161
 
            'key_length':1,
162
 
            'support_partial_insertion':False,
163
 
            }),
164
 
        ]
165
 
    scenarios = len_one_scenarios + len_two_scenarios
166
 
    return multiply_tests(to_adapt, scenarios, result)
167
 
 
168
 
 
169
 
def get_diamond_vf(f, trailing_eol=True, left_only=False):
170
 
    """Get a diamond graph to exercise deltas and merges.
171
 
 
172
 
    :param trailing_eol: If True end the last line with \n.
173
 
    """
174
 
    parents = {
175
 
        'origin': (),
176
 
        'base': (('origin',),),
177
 
        'left': (('base',),),
178
 
        'right': (('base',),),
179
 
        'merged': (('left',), ('right',)),
180
 
        }
181
 
    # insert a diamond graph to exercise deltas and merges.
182
 
    if trailing_eol:
183
 
        last_char = '\n'
184
 
    else:
185
 
        last_char = ''
186
 
    f.add_lines('origin', [], ['origin' + last_char])
187
 
    f.add_lines('base', ['origin'], ['base' + last_char])
188
 
    f.add_lines('left', ['base'], ['base\n', 'left' + last_char])
189
 
    if not left_only:
190
 
        f.add_lines('right', ['base'],
191
 
            ['base\n', 'right' + last_char])
192
 
        f.add_lines('merged', ['left', 'right'],
193
 
            ['base\n', 'left\n', 'right\n', 'merged' + last_char])
194
 
    return f, parents
195
 
 
196
 
 
197
 
def get_diamond_files(files, key_length, trailing_eol=True, left_only=False,
198
 
    nograph=False, nokeys=False):
199
 
    """Get a diamond graph to exercise deltas and merges.
200
 
 
201
 
    This creates a 5-node graph in files. If files supports 2-length keys two
202
 
    graphs are made to exercise the support for multiple ids.
203
 
 
204
 
    :param trailing_eol: If True end the last line with \n.
205
 
    :param key_length: The length of keys in files. Currently supports length 1
206
 
        and 2 keys.
207
 
    :param left_only: If True do not add the right and merged nodes.
208
 
    :param nograph: If True, do not provide parents to the add_lines calls;
209
 
        this is useful for tests that need inserted data but have graphless
210
 
        stores.
211
 
    :param nokeys: If True, pass None is as the key for all insertions.
212
 
        Currently implies nograph.
213
 
    :return: The results of the add_lines calls.
214
 
    """
215
 
    if nokeys:
216
 
        nograph = True
217
 
    if key_length == 1:
218
 
        prefixes = [()]
219
 
    else:
220
 
        prefixes = [('FileA',), ('FileB',)]
221
 
    # insert a diamond graph to exercise deltas and merges.
222
 
    if trailing_eol:
223
 
        last_char = '\n'
224
 
    else:
225
 
        last_char = ''
226
 
    result = []
227
 
    def get_parents(suffix_list):
228
 
        if nograph:
229
 
            return ()
230
 
        else:
231
 
            result = [prefix + suffix for suffix in suffix_list]
232
 
            return result
233
 
    def get_key(suffix):
234
 
        if nokeys:
235
 
            return (None, )
236
 
        else:
237
 
            return (suffix,)
238
 
    # we loop over each key because that spreads the inserts across prefixes,
239
 
    # which is how commit operates.
240
 
    for prefix in prefixes:
241
 
        result.append(files.add_lines(prefix + get_key('origin'), (),
242
 
            ['origin' + last_char]))
243
 
    for prefix in prefixes:
244
 
        result.append(files.add_lines(prefix + get_key('base'),
245
 
            get_parents([('origin',)]), ['base' + last_char]))
246
 
    for prefix in prefixes:
247
 
        result.append(files.add_lines(prefix + get_key('left'),
248
 
            get_parents([('base',)]),
249
 
            ['base\n', 'left' + last_char]))
250
 
    if not left_only:
251
 
        for prefix in prefixes:
252
 
            result.append(files.add_lines(prefix + get_key('right'),
253
 
                get_parents([('base',)]),
254
 
                ['base\n', 'right' + last_char]))
255
 
        for prefix in prefixes:
256
 
            result.append(files.add_lines(prefix + get_key('merged'),
257
 
                get_parents([('left',), ('right',)]),
258
 
                ['base\n', 'left\n', 'right\n', 'merged' + last_char]))
259
 
    return result
260
 
 
261
 
 
262
46
class VersionedFileTestMixIn(object):
263
47
    """A mixin test class for testing VersionedFiles.
264
48
 
267
51
    they are strictly controlled by their owning repositories.
268
52
    """
269
53
 
270
 
    def get_transaction(self):
271
 
        if not hasattr(self, '_transaction'):
272
 
            self._transaction = None
273
 
        return self._transaction
274
 
 
275
54
    def test_add(self):
276
55
        f = self.get_file()
277
56
        f.add_lines('r0', [], ['a\n', 'b\n'])
285
64
            self.assertEquals(f.get_lines('r1'), ['b\n', 'c\n'])
286
65
            self.assertEqual(2, len(f))
287
66
            self.assertEqual(2, f.num_versions())
288
 
 
 
67
    
289
68
            self.assertRaises(RevisionNotPresent,
290
69
                f.add_lines, 'r2', ['foo'], [])
291
70
            self.assertRaises(RevisionAlreadyPresent,
298
77
    def test_adds_with_parent_texts(self):
299
78
        f = self.get_file()
300
79
        parent_texts = {}
301
 
        _, _, parent_texts['r0'] = f.add_lines('r0', [], ['a\n', 'b\n'])
 
80
        parent_texts['r0'] = f.add_lines('r0', [], ['a\n', 'b\n'])
302
81
        try:
303
 
            _, _, parent_texts['r1'] = f.add_lines_with_ghosts('r1',
304
 
                ['r0', 'ghost'], ['b\n', 'c\n'], parent_texts=parent_texts)
 
82
            parent_texts['r1'] = f.add_lines_with_ghosts('r1',
 
83
                                                         ['r0', 'ghost'], 
 
84
                                                         ['b\n', 'c\n'],
 
85
                                                         parent_texts=parent_texts)
305
86
        except NotImplementedError:
306
87
            # if the format doesn't support ghosts, just add normally.
307
 
            _, _, parent_texts['r1'] = f.add_lines('r1',
308
 
                ['r0'], ['b\n', 'c\n'], parent_texts=parent_texts)
 
88
            parent_texts['r1'] = f.add_lines('r1',
 
89
                                             ['r0'], 
 
90
                                             ['b\n', 'c\n'],
 
91
                                             parent_texts=parent_texts)
309
92
        f.add_lines('r2', ['r1'], ['c\n', 'd\n'], parent_texts=parent_texts)
310
93
        self.assertNotEqual(None, parent_texts['r0'])
311
94
        self.assertNotEqual(None, parent_texts['r1'])
330
113
        verify_file(f)
331
114
 
332
115
    def test_add_unicode_content(self):
333
 
        # unicode content is not permitted in versioned files.
 
116
        # unicode content is not permitted in versioned files. 
334
117
        # versioned files version sequences of bytes only.
335
118
        vf = self.get_file()
336
119
        self.assertRaises(errors.BzrBadParameterUnicode,
339
122
            (errors.BzrBadParameterUnicode, NotImplementedError),
340
123
            vf.add_lines_with_ghosts, 'a', [], ['a\n', u'b\n', 'c\n'])
341
124
 
342
 
    def test_add_follows_left_matching_blocks(self):
343
 
        """If we change left_matching_blocks, delta changes
344
 
 
345
 
        Note: There are multiple correct deltas in this case, because
346
 
        we start with 1 "a" and we get 3.
347
 
        """
348
 
        vf = self.get_file()
349
 
        if isinstance(vf, WeaveFile):
350
 
            raise TestSkipped("WeaveFile ignores left_matching_blocks")
351
 
        vf.add_lines('1', [], ['a\n'])
352
 
        vf.add_lines('2', ['1'], ['a\n', 'a\n', 'a\n'],
353
 
                     left_matching_blocks=[(0, 0, 1), (1, 3, 0)])
354
 
        self.assertEqual(['a\n', 'a\n', 'a\n'], vf.get_lines('2'))
355
 
        vf.add_lines('3', ['1'], ['a\n', 'a\n', 'a\n'],
356
 
                     left_matching_blocks=[(0, 2, 1), (1, 3, 0)])
357
 
        self.assertEqual(['a\n', 'a\n', 'a\n'], vf.get_lines('3'))
358
 
 
359
125
    def test_inline_newline_throws(self):
360
126
        # \r characters are not permitted in lines being added
361
127
        vf = self.get_file()
362
 
        self.assertRaises(errors.BzrBadParameterContainsNewline,
 
128
        self.assertRaises(errors.BzrBadParameterContainsNewline, 
363
129
            vf.add_lines, 'a', [], ['a\n\n'])
364
130
        self.assertRaises(
365
131
            (errors.BzrBadParameterContainsNewline, NotImplementedError),
371
137
        except NotImplementedError:
372
138
            pass
373
139
 
374
 
    def test_add_reserved(self):
375
 
        vf = self.get_file()
376
 
        self.assertRaises(errors.ReservedId,
377
 
            vf.add_lines, 'a:', [], ['a\n', 'b\n', 'c\n'])
378
 
 
379
 
    def test_add_lines_nostoresha(self):
380
 
        """When nostore_sha is supplied using old content raises."""
381
 
        vf = self.get_file()
382
 
        empty_text = ('a', [])
383
 
        sample_text_nl = ('b', ["foo\n", "bar\n"])
384
 
        sample_text_no_nl = ('c', ["foo\n", "bar"])
385
 
        shas = []
386
 
        for version, lines in (empty_text, sample_text_nl, sample_text_no_nl):
387
 
            sha, _, _ = vf.add_lines(version, [], lines)
388
 
            shas.append(sha)
389
 
        # we now have a copy of all the lines in the vf.
390
 
        for sha, (version, lines) in zip(
391
 
            shas, (empty_text, sample_text_nl, sample_text_no_nl)):
392
 
            self.assertRaises(errors.ExistingContent,
393
 
                vf.add_lines, version + "2", [], lines,
394
 
                nostore_sha=sha)
395
 
            # and no new version should have been added.
396
 
            self.assertRaises(errors.RevisionNotPresent, vf.get_lines,
397
 
                version + "2")
398
 
 
399
 
    def test_add_lines_with_ghosts_nostoresha(self):
400
 
        """When nostore_sha is supplied using old content raises."""
401
 
        vf = self.get_file()
402
 
        empty_text = ('a', [])
403
 
        sample_text_nl = ('b', ["foo\n", "bar\n"])
404
 
        sample_text_no_nl = ('c', ["foo\n", "bar"])
405
 
        shas = []
406
 
        for version, lines in (empty_text, sample_text_nl, sample_text_no_nl):
407
 
            sha, _, _ = vf.add_lines(version, [], lines)
408
 
            shas.append(sha)
409
 
        # we now have a copy of all the lines in the vf.
410
 
        # is the test applicable to this vf implementation?
411
 
        try:
412
 
            vf.add_lines_with_ghosts('d', [], [])
413
 
        except NotImplementedError:
414
 
            raise TestSkipped("add_lines_with_ghosts is optional")
415
 
        for sha, (version, lines) in zip(
416
 
            shas, (empty_text, sample_text_nl, sample_text_no_nl)):
417
 
            self.assertRaises(errors.ExistingContent,
418
 
                vf.add_lines_with_ghosts, version + "2", [], lines,
419
 
                nostore_sha=sha)
420
 
            # and no new version should have been added.
421
 
            self.assertRaises(errors.RevisionNotPresent, vf.get_lines,
422
 
                version + "2")
423
 
 
424
 
    def test_add_lines_return_value(self):
425
 
        # add_lines should return the sha1 and the text size.
426
 
        vf = self.get_file()
427
 
        empty_text = ('a', [])
428
 
        sample_text_nl = ('b', ["foo\n", "bar\n"])
429
 
        sample_text_no_nl = ('c', ["foo\n", "bar"])
430
 
        # check results for the three cases:
431
 
        for version, lines in (empty_text, sample_text_nl, sample_text_no_nl):
432
 
            # the first two elements are the same for all versioned files:
433
 
            # - the digest and the size of the text. For some versioned files
434
 
            #   additional data is returned in additional tuple elements.
435
 
            result = vf.add_lines(version, [], lines)
436
 
            self.assertEqual(3, len(result))
437
 
            self.assertEqual((osutils.sha_strings(lines), sum(map(len, lines))),
438
 
                result[0:2])
439
 
        # parents should not affect the result:
440
 
        lines = sample_text_nl[1]
441
 
        self.assertEqual((osutils.sha_strings(lines), sum(map(len, lines))),
442
 
            vf.add_lines('d', ['b', 'c'], lines)[0:2])
443
 
 
444
 
    def test_get_reserved(self):
445
 
        vf = self.get_file()
446
 
        self.assertRaises(errors.ReservedId, vf.get_texts, ['b:'])
447
 
        self.assertRaises(errors.ReservedId, vf.get_lines, 'b:')
448
 
        self.assertRaises(errors.ReservedId, vf.get_text, 'b:')
449
 
 
450
 
    def test_add_unchanged_last_line_noeol_snapshot(self):
451
 
        """Add a text with an unchanged last line with no eol should work."""
452
 
        # Test adding this in a number of chain lengths; because the interface
453
 
        # for VersionedFile does not allow forcing a specific chain length, we
454
 
        # just use a small base to get the first snapshot, then a much longer
455
 
        # first line for the next add (which will make the third add snapshot)
456
 
        # and so on. 20 has been chosen as an aribtrary figure - knits use 200
457
 
        # as a capped delta length, but ideally we would have some way of
458
 
        # tuning the test to the store (e.g. keep going until a snapshot
459
 
        # happens).
460
 
        for length in range(20):
461
 
            version_lines = {}
462
 
            vf = self.get_file('case-%d' % length)
463
 
            prefix = 'step-%d'
464
 
            parents = []
465
 
            for step in range(length):
466
 
                version = prefix % step
467
 
                lines = (['prelude \n'] * step) + ['line']
468
 
                vf.add_lines(version, parents, lines)
469
 
                version_lines[version] = lines
470
 
                parents = [version]
471
 
            vf.add_lines('no-eol', parents, ['line'])
472
 
            vf.get_texts(version_lines.keys())
473
 
            self.assertEqualDiff('line', vf.get_text('no-eol'))
474
 
 
475
 
    def test_get_texts_eol_variation(self):
476
 
        # similar to the failure in <http://bugs.launchpad.net/234748>
477
 
        vf = self.get_file()
478
 
        sample_text_nl = ["line\n"]
479
 
        sample_text_no_nl = ["line"]
480
 
        versions = []
481
 
        version_lines = {}
482
 
        parents = []
483
 
        for i in range(4):
484
 
            version = 'v%d' % i
485
 
            if i % 2:
486
 
                lines = sample_text_nl
487
 
            else:
488
 
                lines = sample_text_no_nl
489
 
            # left_matching blocks is an internal api; it operates on the
490
 
            # *internal* representation for a knit, which is with *all* lines
491
 
            # being normalised to end with \n - even the final line in a no_nl
492
 
            # file. Using it here ensures that a broken internal implementation
493
 
            # (which is what this test tests) will generate a correct line
494
 
            # delta (which is to say, an empty delta).
495
 
            vf.add_lines(version, parents, lines,
496
 
                left_matching_blocks=[(0, 0, 1)])
497
 
            parents = [version]
498
 
            versions.append(version)
499
 
            version_lines[version] = lines
500
 
        vf.check()
501
 
        vf.get_texts(versions)
502
 
        vf.get_texts(reversed(versions))
503
 
 
504
 
    def test_add_lines_with_matching_blocks_noeol_last_line(self):
505
 
        """Add a text with an unchanged last line with no eol should work."""
506
 
        from bzrlib import multiparent
507
 
        # Hand verified sha1 of the text we're adding.
508
 
        sha1 = '6a1d115ec7b60afb664dc14890b5af5ce3c827a4'
509
 
        # Create a mpdiff which adds a new line before the trailing line, and
510
 
        # reuse the last line unaltered (which can cause annotation reuse).
511
 
        # Test adding this in two situations:
512
 
        # On top of a new insertion
513
 
        vf = self.get_file('fulltext')
514
 
        vf.add_lines('noeol', [], ['line'])
515
 
        vf.add_lines('noeol2', ['noeol'], ['newline\n', 'line'],
516
 
            left_matching_blocks=[(0, 1, 1)])
517
 
        self.assertEqualDiff('newline\nline', vf.get_text('noeol2'))
518
 
        # On top of a delta
519
 
        vf = self.get_file('delta')
520
 
        vf.add_lines('base', [], ['line'])
521
 
        vf.add_lines('noeol', ['base'], ['prelude\n', 'line'])
522
 
        vf.add_lines('noeol2', ['noeol'], ['newline\n', 'line'],
523
 
            left_matching_blocks=[(1, 1, 1)])
524
 
        self.assertEqualDiff('newline\nline', vf.get_text('noeol2'))
525
 
 
526
 
    def test_make_mpdiffs(self):
527
 
        from bzrlib import multiparent
528
 
        vf = self.get_file('foo')
529
 
        sha1s = self._setup_for_deltas(vf)
530
 
        new_vf = self.get_file('bar')
531
 
        for version in multiparent.topo_iter(vf):
532
 
            mpdiff = vf.make_mpdiffs([version])[0]
533
 
            new_vf.add_mpdiffs([(version, vf.get_parent_map([version])[version],
534
 
                                 vf.get_sha1s([version])[version], mpdiff)])
535
 
            self.assertEqualDiff(vf.get_text(version),
536
 
                                 new_vf.get_text(version))
537
 
 
538
 
    def test_make_mpdiffs_with_ghosts(self):
539
 
        vf = self.get_file('foo')
540
 
        try:
541
 
            vf.add_lines_with_ghosts('text', ['ghost'], ['line\n'])
542
 
        except NotImplementedError:
543
 
            # old Weave formats do not allow ghosts
544
 
            return
545
 
        self.assertRaises(errors.RevisionNotPresent, vf.make_mpdiffs, ['ghost'])
 
140
    def test_get_delta(self):
 
141
        f = self.get_file()
 
142
        sha1s = self._setup_for_deltas(f)
 
143
        expected_delta = (None, '6bfa09d82ce3e898ad4641ae13dd4fdb9cf0d76b', False, 
 
144
                          [(0, 0, 1, [('base', 'line\n')])])
 
145
        self.assertEqual(expected_delta, f.get_delta('base'))
 
146
        next_parent = 'base'
 
147
        text_name = 'chain1-'
 
148
        for depth in range(26):
 
149
            new_version = text_name + '%s' % depth
 
150
            expected_delta = (next_parent, sha1s[depth], 
 
151
                              False,
 
152
                              [(depth + 1, depth + 1, 1, [(new_version, 'line\n')])])
 
153
            self.assertEqual(expected_delta, f.get_delta(new_version))
 
154
            next_parent = new_version
 
155
        next_parent = 'base'
 
156
        text_name = 'chain2-'
 
157
        for depth in range(26):
 
158
            new_version = text_name + '%s' % depth
 
159
            expected_delta = (next_parent, sha1s[depth], False,
 
160
                              [(depth + 1, depth + 1, 1, [(new_version, 'line\n')])])
 
161
            self.assertEqual(expected_delta, f.get_delta(new_version))
 
162
            next_parent = new_version
 
163
        # smoke test for eol support
 
164
        expected_delta = ('base', '264f39cab871e4cfd65b3a002f7255888bb5ed97', True, [])
 
165
        self.assertEqual(['line'], f.get_lines('noeol'))
 
166
        self.assertEqual(expected_delta, f.get_delta('noeol'))
 
167
 
 
168
    def test_get_deltas(self):
 
169
        f = self.get_file()
 
170
        sha1s = self._setup_for_deltas(f)
 
171
        deltas = f.get_deltas(f.versions())
 
172
        expected_delta = (None, '6bfa09d82ce3e898ad4641ae13dd4fdb9cf0d76b', False, 
 
173
                          [(0, 0, 1, [('base', 'line\n')])])
 
174
        self.assertEqual(expected_delta, deltas['base'])
 
175
        next_parent = 'base'
 
176
        text_name = 'chain1-'
 
177
        for depth in range(26):
 
178
            new_version = text_name + '%s' % depth
 
179
            expected_delta = (next_parent, sha1s[depth], 
 
180
                              False,
 
181
                              [(depth + 1, depth + 1, 1, [(new_version, 'line\n')])])
 
182
            self.assertEqual(expected_delta, deltas[new_version])
 
183
            next_parent = new_version
 
184
        next_parent = 'base'
 
185
        text_name = 'chain2-'
 
186
        for depth in range(26):
 
187
            new_version = text_name + '%s' % depth
 
188
            expected_delta = (next_parent, sha1s[depth], False,
 
189
                              [(depth + 1, depth + 1, 1, [(new_version, 'line\n')])])
 
190
            self.assertEqual(expected_delta, deltas[new_version])
 
191
            next_parent = new_version
 
192
        # smoke tests for eol support
 
193
        expected_delta = ('base', '264f39cab871e4cfd65b3a002f7255888bb5ed97', True, [])
 
194
        self.assertEqual(['line'], f.get_lines('noeol'))
 
195
        self.assertEqual(expected_delta, deltas['noeol'])
 
196
        # smoke tests for eol support - two noeol in a row same content
 
197
        expected_deltas = (('noeol', '3ad7ee82dbd8f29ecba073f96e43e414b3f70a4d', True, 
 
198
                          [(0, 1, 2, [(u'noeolsecond', 'line\n'), (u'noeolsecond', 'line\n')])]),
 
199
                          ('noeol', '3ad7ee82dbd8f29ecba073f96e43e414b3f70a4d', True, 
 
200
                           [(0, 0, 1, [('noeolsecond', 'line\n')]), (1, 1, 0, [])]))
 
201
        self.assertEqual(['line\n', 'line'], f.get_lines('noeolsecond'))
 
202
        self.assertTrue(deltas['noeolsecond'] in expected_deltas)
 
203
        # two no-eol in a row, different content
 
204
        expected_delta = ('noeolsecond', '8bb553a84e019ef1149db082d65f3133b195223b', True, 
 
205
                          [(1, 2, 1, [(u'noeolnotshared', 'phone\n')])])
 
206
        self.assertEqual(['line\n', 'phone'], f.get_lines('noeolnotshared'))
 
207
        self.assertEqual(expected_delta, deltas['noeolnotshared'])
 
208
        # eol folling a no-eol with content change
 
209
        expected_delta = ('noeol', 'a61f6fb6cfc4596e8d88c34a308d1e724caf8977', False, 
 
210
                          [(0, 1, 1, [(u'eol', 'phone\n')])])
 
211
        self.assertEqual(['phone\n'], f.get_lines('eol'))
 
212
        self.assertEqual(expected_delta, deltas['eol'])
 
213
        # eol folling a no-eol with content change
 
214
        expected_delta = ('noeol', '6bfa09d82ce3e898ad4641ae13dd4fdb9cf0d76b', False, 
 
215
                          [(0, 1, 1, [(u'eolline', 'line\n')])])
 
216
        self.assertEqual(['line\n'], f.get_lines('eolline'))
 
217
        self.assertEqual(expected_delta, deltas['eolline'])
 
218
        # eol with no parents
 
219
        expected_delta = (None, '264f39cab871e4cfd65b3a002f7255888bb5ed97', True, 
 
220
                          [(0, 0, 1, [(u'noeolbase', 'line\n')])])
 
221
        self.assertEqual(['line'], f.get_lines('noeolbase'))
 
222
        self.assertEqual(expected_delta, deltas['noeolbase'])
 
223
        # eol with two parents, in inverse insertion order
 
224
        expected_deltas = (('noeolbase', '264f39cab871e4cfd65b3a002f7255888bb5ed97', True,
 
225
                            [(0, 1, 1, [(u'eolbeforefirstparent', 'line\n')])]),
 
226
                           ('noeolbase', '264f39cab871e4cfd65b3a002f7255888bb5ed97', True,
 
227
                            [(0, 1, 1, [(u'eolbeforefirstparent', 'line\n')])]))
 
228
        self.assertEqual(['line'], f.get_lines('eolbeforefirstparent'))
 
229
        #self.assertTrue(deltas['eolbeforefirstparent'] in expected_deltas)
546
230
 
547
231
    def _setup_for_deltas(self, f):
548
 
        self.assertFalse(f.has_version('base'))
 
232
        self.assertRaises(errors.RevisionNotPresent, f.get_delta, 'base')
549
233
        # add texts that should trip the knit maximum delta chain threshold
550
234
        # as well as doing parallel chains of data in knits.
551
235
        # this is done by two chains of 25 insertions
564
248
        f.add_lines('noeolbase', [], ['line'])
565
249
        # noeol preceeding its leftmost parent in the output:
566
250
        # this is done by making it a merge of two parents with no common
567
 
        # anestry: noeolbase and noeol with the
 
251
        # anestry: noeolbase and noeol with the 
568
252
        # later-inserted parent the leftmost.
569
253
        f.add_lines('eolbeforefirstparent', ['noeolbase', 'noeol'], ['line'])
570
254
        # two identical eol texts
614
298
            next_parent = new_version
615
299
        return sha1s
616
300
 
 
301
    def test_add_delta(self):
 
302
        # tests for the add-delta facility.
 
303
        # at this point, optimising for speed, we assume no checks when deltas are inserted.
 
304
        # this may need to be revisited.
 
305
        source = self.get_file('source')
 
306
        source.add_lines('base', [], ['line\n'])
 
307
        next_parent = 'base'
 
308
        text_name = 'chain1-'
 
309
        text = ['line\n']
 
310
        for depth in range(26):
 
311
            new_version = text_name + '%s' % depth
 
312
            text = text + ['line\n']
 
313
            source.add_lines(new_version, [next_parent], text)
 
314
            next_parent = new_version
 
315
        next_parent = 'base'
 
316
        text_name = 'chain2-'
 
317
        text = ['line\n']
 
318
        for depth in range(26):
 
319
            new_version = text_name + '%s' % depth
 
320
            text = text + ['line\n']
 
321
            source.add_lines(new_version, [next_parent], text)
 
322
            next_parent = new_version
 
323
        source.add_lines('noeol', ['base'], ['line'])
 
324
        
 
325
        target = self.get_file('target')
 
326
        for version in source.versions():
 
327
            parent, sha1, noeol, delta = source.get_delta(version)
 
328
            target.add_delta(version,
 
329
                             source.get_parents(version),
 
330
                             parent,
 
331
                             sha1,
 
332
                             noeol,
 
333
                             delta)
 
334
        self.assertRaises(RevisionAlreadyPresent,
 
335
                          target.add_delta, 'base', [], None, '', False, [])
 
336
        for version in source.versions():
 
337
            self.assertEqual(source.get_lines(version),
 
338
                             target.get_lines(version))
 
339
 
617
340
    def test_ancestry(self):
618
341
        f = self.get_file()
619
342
        self.assertEqual([], f.get_ancestry([]))
642
365
        self.assertRaises(RevisionNotPresent,
643
366
            f.get_ancestry, ['rM', 'rX'])
644
367
 
645
 
        self.assertEqual(set(f.get_ancestry('rM')),
646
 
            set(f.get_ancestry('rM', topo_sorted=False)))
647
 
 
648
368
    def test_mutate_after_finish(self):
649
 
        self._transaction = 'before'
650
369
        f = self.get_file()
651
 
        self._transaction = 'after'
 
370
        f.transaction_finished()
 
371
        self.assertRaises(errors.OutSideTransaction, f.add_delta, '', [], '', '', False, [])
652
372
        self.assertRaises(errors.OutSideTransaction, f.add_lines, '', [], [])
653
373
        self.assertRaises(errors.OutSideTransaction, f.add_lines_with_ghosts, '', [], [])
 
374
        self.assertRaises(errors.OutSideTransaction, f.fix_parents, '', [])
 
375
        self.assertRaises(errors.OutSideTransaction, f.join, '')
 
376
        self.assertRaises(errors.OutSideTransaction, f.clone_text, 'base', 'bar', ['foo'])
 
377
        
 
378
    def test_clear_cache(self):
 
379
        f = self.get_file()
 
380
        # on a new file it should not error
 
381
        f.clear_cache()
 
382
        # and after adding content, doing a clear_cache and a get should work.
 
383
        f.add_lines('0', [], ['a'])
 
384
        f.clear_cache()
 
385
        self.assertEqual(['a'], f.get_lines('0'))
 
386
 
 
387
    def test_clone_text(self):
 
388
        f = self.get_file()
 
389
        f.add_lines('r0', [], ['a\n', 'b\n'])
 
390
        f.clone_text('r1', 'r0', ['r0'])
 
391
        def verify_file(f):
 
392
            self.assertEquals(f.get_lines('r1'), f.get_lines('r0'))
 
393
            self.assertEquals(f.get_lines('r1'), ['a\n', 'b\n'])
 
394
            self.assertEquals(f.get_parents('r1'), ['r0'])
 
395
    
 
396
            self.assertRaises(RevisionNotPresent,
 
397
                f.clone_text, 'r2', 'rX', [])
 
398
            self.assertRaises(RevisionAlreadyPresent,
 
399
                f.clone_text, 'r1', 'r0', [])
 
400
        verify_file(f)
 
401
        verify_file(self.reopen_file())
 
402
 
 
403
    def test_create_empty(self):
 
404
        f = self.get_file()
 
405
        f.add_lines('0', [], ['a\n'])
 
406
        new_f = f.create_empty('t', MemoryTransport())
 
407
        # smoke test, specific types should check it is honoured correctly for
 
408
        # non type attributes
 
409
        self.assertEqual([], new_f.versions())
 
410
        self.assertTrue(isinstance(new_f, f.__class__))
654
411
 
655
412
    def test_copy_to(self):
656
413
        f = self.get_file()
657
414
        f.add_lines('0', [], ['a\n'])
658
415
        t = MemoryTransport()
659
416
        f.copy_to('foo', t)
660
 
        for suffix in self.get_factory().get_suffixes():
 
417
        for suffix in f.__class__.get_suffixes():
661
418
            self.assertTrue(t.has('foo' + suffix))
662
419
 
663
420
    def test_get_suffixes(self):
664
421
        f = self.get_file()
 
422
        # should be the same
 
423
        self.assertEqual(f.__class__.get_suffixes(), f.__class__.get_suffixes())
665
424
        # and should be a list
666
 
        self.assertTrue(isinstance(self.get_factory().get_suffixes(), list))
667
 
 
668
 
    def test_get_parent_map(self):
 
425
        self.assertTrue(isinstance(f.__class__.get_suffixes(), list))
 
426
 
 
427
    def build_graph(self, file, graph):
 
428
        for node in topo_sort(graph.items()):
 
429
            file.add_lines(node, graph[node], [])
 
430
 
 
431
    def test_get_graph(self):
 
432
        f = self.get_file()
 
433
        graph = {
 
434
            'v1': [],
 
435
            'v2': ['v1'],
 
436
            'v3': ['v2']}
 
437
        self.build_graph(f, graph)
 
438
        self.assertEqual(graph, f.get_graph())
 
439
    
 
440
    def test_get_graph_partial(self):
 
441
        f = self.get_file()
 
442
        complex_graph = {}
 
443
        simple_a = {
 
444
            'c': [],
 
445
            'b': ['c'],
 
446
            'a': ['b'],
 
447
            }
 
448
        complex_graph.update(simple_a)
 
449
        simple_b = {
 
450
            'c': [],
 
451
            'b': ['c'],
 
452
            }
 
453
        complex_graph.update(simple_b)
 
454
        simple_gam = {
 
455
            'c': [],
 
456
            'oo': [],
 
457
            'bar': ['oo', 'c'],
 
458
            'gam': ['bar'],
 
459
            }
 
460
        complex_graph.update(simple_gam)
 
461
        simple_b_gam = {}
 
462
        simple_b_gam.update(simple_gam)
 
463
        simple_b_gam.update(simple_b)
 
464
        self.build_graph(f, complex_graph)
 
465
        self.assertEqual(simple_a, f.get_graph(['a']))
 
466
        self.assertEqual(simple_b, f.get_graph(['b']))
 
467
        self.assertEqual(simple_gam, f.get_graph(['gam']))
 
468
        self.assertEqual(simple_b_gam, f.get_graph(['b', 'gam']))
 
469
 
 
470
    def test_get_parents(self):
669
471
        f = self.get_file()
670
472
        f.add_lines('r0', [], ['a\n', 'b\n'])
671
 
        self.assertEqual(
672
 
            {'r0':()}, f.get_parent_map(['r0']))
673
 
        f.add_lines('r1', ['r0'], ['a\n', 'b\n'])
674
 
        self.assertEqual(
675
 
            {'r1':('r0',)}, f.get_parent_map(['r1']))
676
 
        self.assertEqual(
677
 
            {'r0':(),
678
 
             'r1':('r0',)},
679
 
            f.get_parent_map(['r0', 'r1']))
 
473
        f.add_lines('r1', [], ['a\n', 'b\n'])
680
474
        f.add_lines('r2', [], ['a\n', 'b\n'])
681
475
        f.add_lines('r3', [], ['a\n', 'b\n'])
682
476
        f.add_lines('m', ['r0', 'r1', 'r2', 'r3'], ['a\n', 'b\n'])
683
 
        self.assertEqual(
684
 
            {'m':('r0', 'r1', 'r2', 'r3')}, f.get_parent_map(['m']))
685
 
        self.assertEqual({}, f.get_parent_map('y'))
686
 
        self.assertEqual(
687
 
            {'r0':(),
688
 
             'r1':('r0',)},
689
 
            f.get_parent_map(['r0', 'y', 'r1']))
 
477
        self.assertEquals(f.get_parents('m'), ['r0', 'r1', 'r2', 'r3'])
 
478
 
 
479
        self.assertRaises(RevisionNotPresent,
 
480
            f.get_parents, 'y')
690
481
 
691
482
    def test_annotate(self):
692
483
        f = self.get_file()
699
490
        self.assertRaises(RevisionNotPresent,
700
491
            f.annotate, 'foo')
701
492
 
 
493
    def test_walk(self):
 
494
        # tests that walk returns all the inclusions for the requested
 
495
        # revisions as well as the revisions changes themselves.
 
496
        f = self.get_file('1')
 
497
        f.add_lines('r0', [], ['a\n', 'b\n'])
 
498
        f.add_lines('r1', ['r0'], ['c\n', 'b\n'])
 
499
        f.add_lines('rX', ['r1'], ['d\n', 'b\n'])
 
500
        f.add_lines('rY', ['r1'], ['c\n', 'e\n'])
 
501
 
 
502
        lines = {}
 
503
        for lineno, insert, dset, text in f.walk(['rX', 'rY']):
 
504
            lines[text] = (insert, dset)
 
505
 
 
506
        self.assertTrue(lines['a\n'], ('r0', set(['r1'])))
 
507
        self.assertTrue(lines['b\n'], ('r0', set(['rY'])))
 
508
        self.assertTrue(lines['c\n'], ('r1', set(['rX'])))
 
509
        self.assertTrue(lines['d\n'], ('rX', set([])))
 
510
        self.assertTrue(lines['e\n'], ('rY', set([])))
 
511
 
702
512
    def test_detection(self):
703
513
        # Test weaves detect corruption.
704
514
        #
730
540
 
731
541
    def test_iter_lines_added_or_present_in_versions(self):
732
542
        # test that we get at least an equalset of the lines added by
733
 
        # versions in the weave
 
543
        # versions in the weave 
734
544
        # the ordering here is to make a tree so that dumb searches have
735
545
        # more changes to muck up.
736
 
 
737
 
        class InstrumentedProgress(progress.DummyProgress):
738
 
 
739
 
            def __init__(self):
740
 
 
741
 
                progress.DummyProgress.__init__(self)
742
 
                self.updates = []
743
 
 
744
 
            def update(self, msg=None, current=None, total=None):
745
 
                self.updates.append((msg, current, total))
746
 
 
747
546
        vf = self.get_file()
748
547
        # add a base to get included
749
548
        vf.add_lines('base', [], ['base\n'])
757
556
        vf.add_lines('otherchild',
758
557
                     ['lancestor', 'base'],
759
558
                     ['base\n', 'lancestor\n', 'otherchild\n'])
760
 
        def iter_with_versions(versions, expected):
 
559
        def iter_with_versions(versions):
761
560
            # now we need to see what lines are returned, and how often.
762
 
            lines = {}
763
 
            progress = InstrumentedProgress()
 
561
            lines = {'base\n':0,
 
562
                     'lancestor\n':0,
 
563
                     'rancestor\n':0,
 
564
                     'child\n':0,
 
565
                     'otherchild\n':0,
 
566
                     }
764
567
            # iterate over the lines
765
 
            for line in vf.iter_lines_added_or_present_in_versions(versions,
766
 
                pb=progress):
767
 
                lines.setdefault(line, 0)
 
568
            for line in vf.iter_lines_added_or_present_in_versions(versions):
768
569
                lines[line] += 1
769
 
            if []!= progress.updates:
770
 
                self.assertEqual(expected, progress.updates)
771
570
            return lines
772
 
        lines = iter_with_versions(['child', 'otherchild'],
773
 
                                   [('Walking content', 0, 2),
774
 
                                    ('Walking content', 1, 2),
775
 
                                    ('Walking content', 2, 2)])
 
571
        lines = iter_with_versions(['child', 'otherchild'])
776
572
        # we must see child and otherchild
777
 
        self.assertTrue(lines[('child\n', 'child')] > 0)
778
 
        self.assertTrue(lines[('otherchild\n', 'otherchild')] > 0)
 
573
        self.assertTrue(lines['child\n'] > 0)
 
574
        self.assertTrue(lines['otherchild\n'] > 0)
779
575
        # we dont care if we got more than that.
780
 
 
 
576
        
781
577
        # test all lines
782
 
        lines = iter_with_versions(None, [('Walking content', 0, 5),
783
 
                                          ('Walking content', 1, 5),
784
 
                                          ('Walking content', 2, 5),
785
 
                                          ('Walking content', 3, 5),
786
 
                                          ('Walking content', 4, 5),
787
 
                                          ('Walking content', 5, 5)])
 
578
        lines = iter_with_versions(None)
788
579
        # all lines must be seen at least once
789
 
        self.assertTrue(lines[('base\n', 'base')] > 0)
790
 
        self.assertTrue(lines[('lancestor\n', 'lancestor')] > 0)
791
 
        self.assertTrue(lines[('rancestor\n', 'rancestor')] > 0)
792
 
        self.assertTrue(lines[('child\n', 'child')] > 0)
793
 
        self.assertTrue(lines[('otherchild\n', 'otherchild')] > 0)
 
580
        self.assertTrue(lines['base\n'] > 0)
 
581
        self.assertTrue(lines['lancestor\n'] > 0)
 
582
        self.assertTrue(lines['rancestor\n'] > 0)
 
583
        self.assertTrue(lines['child\n'] > 0)
 
584
        self.assertTrue(lines['otherchild\n'] > 0)
 
585
 
 
586
    def test_fix_parents(self):
 
587
        # some versioned files allow incorrect parents to be corrected after
 
588
        # insertion - this may not fix ancestry..
 
589
        # if they do not supported, they just do not implement it.
 
590
        # we test this as an interface test to ensure that those that *do*
 
591
        # implementent it get it right.
 
592
        vf = self.get_file()
 
593
        vf.add_lines('notbase', [], [])
 
594
        vf.add_lines('base', [], [])
 
595
        try:
 
596
            vf.fix_parents('notbase', ['base'])
 
597
        except NotImplementedError:
 
598
            return
 
599
        self.assertEqual(['base'], vf.get_parents('notbase'))
 
600
        # open again, check it stuck.
 
601
        vf = self.get_file()
 
602
        self.assertEqual(['base'], vf.get_parents('notbase'))
 
603
 
 
604
    def test_fix_parents_with_ghosts(self):
 
605
        # when fixing parents, ghosts that are listed should not be ghosts
 
606
        # anymore.
 
607
        vf = self.get_file()
 
608
 
 
609
        try:
 
610
            vf.add_lines_with_ghosts('notbase', ['base', 'stillghost'], [])
 
611
        except NotImplementedError:
 
612
            return
 
613
        vf.add_lines('base', [], [])
 
614
        vf.fix_parents('notbase', ['base', 'stillghost'])
 
615
        self.assertEqual(['base'], vf.get_parents('notbase'))
 
616
        # open again, check it stuck.
 
617
        vf = self.get_file()
 
618
        self.assertEqual(['base'], vf.get_parents('notbase'))
 
619
        # and check the ghosts
 
620
        self.assertEqual(['base', 'stillghost'],
 
621
                         vf.get_parents_with_ghosts('notbase'))
794
622
 
795
623
    def test_add_lines_with_ghosts(self):
796
624
        # some versioned file formats allow lines to be added with parent
799
627
        # add_lines_with_ghosts api.
800
628
        vf = self.get_file()
801
629
        # add a revision with ghost parents
802
 
        # The preferred form is utf8, but we should translate when needed
803
 
        parent_id_unicode = u'b\xbfse'
804
 
        parent_id_utf8 = parent_id_unicode.encode('utf8')
805
630
        try:
806
 
            vf.add_lines_with_ghosts('notbxbfse', [parent_id_utf8], [])
 
631
            vf.add_lines_with_ghosts(u'notbxbfse', [u'b\xbfse'], [])
807
632
        except NotImplementedError:
808
633
            # check the other ghost apis are also not implemented
 
634
            self.assertRaises(NotImplementedError, vf.has_ghost, 'foo')
809
635
            self.assertRaises(NotImplementedError, vf.get_ancestry_with_ghosts, ['foo'])
810
636
            self.assertRaises(NotImplementedError, vf.get_parents_with_ghosts, 'foo')
 
637
            self.assertRaises(NotImplementedError, vf.get_graph_with_ghosts)
811
638
            return
812
 
        vf = self.reopen_file()
813
639
        # test key graph related apis: getncestry, _graph, get_parents
814
640
        # has_version
815
641
        # - these are ghost unaware and must not be reflect ghosts
816
 
        self.assertEqual(['notbxbfse'], vf.get_ancestry('notbxbfse'))
817
 
        self.assertFalse(vf.has_version(parent_id_utf8))
 
642
        self.assertEqual([u'notbxbfse'], vf.get_ancestry(u'notbxbfse'))
 
643
        self.assertEqual([], vf.get_parents(u'notbxbfse'))
 
644
        self.assertEqual({u'notbxbfse':[]}, vf.get_graph())
 
645
        self.assertFalse(vf.has_version(u'b\xbfse'))
818
646
        # we have _with_ghost apis to give us ghost information.
819
 
        self.assertEqual([parent_id_utf8, 'notbxbfse'], vf.get_ancestry_with_ghosts(['notbxbfse']))
820
 
        self.assertEqual([parent_id_utf8], vf.get_parents_with_ghosts('notbxbfse'))
 
647
        self.assertEqual([u'b\xbfse', u'notbxbfse'], vf.get_ancestry_with_ghosts([u'notbxbfse']))
 
648
        self.assertEqual([u'b\xbfse'], vf.get_parents_with_ghosts(u'notbxbfse'))
 
649
        self.assertEqual({u'notbxbfse':[u'b\xbfse']}, vf.get_graph_with_ghosts())
 
650
        self.assertTrue(vf.has_ghost(u'b\xbfse'))
821
651
        # if we add something that is a ghost of another, it should correct the
822
652
        # results of the prior apis
823
 
        vf.add_lines(parent_id_utf8, [], [])
824
 
        self.assertEqual([parent_id_utf8, 'notbxbfse'], vf.get_ancestry(['notbxbfse']))
825
 
        self.assertEqual({'notbxbfse':(parent_id_utf8,)},
826
 
            vf.get_parent_map(['notbxbfse']))
827
 
        self.assertTrue(vf.has_version(parent_id_utf8))
 
653
        vf.add_lines(u'b\xbfse', [], [])
 
654
        self.assertEqual([u'b\xbfse', u'notbxbfse'], vf.get_ancestry([u'notbxbfse']))
 
655
        self.assertEqual([u'b\xbfse'], vf.get_parents(u'notbxbfse'))
 
656
        self.assertEqual({u'b\xbfse':[],
 
657
                          u'notbxbfse':[u'b\xbfse'],
 
658
                          },
 
659
                         vf.get_graph())
 
660
        self.assertTrue(vf.has_version(u'b\xbfse'))
828
661
        # we have _with_ghost apis to give us ghost information.
829
 
        self.assertEqual([parent_id_utf8, 'notbxbfse'],
830
 
            vf.get_ancestry_with_ghosts(['notbxbfse']))
831
 
        self.assertEqual([parent_id_utf8], vf.get_parents_with_ghosts('notbxbfse'))
 
662
        self.assertEqual([u'b\xbfse', u'notbxbfse'], vf.get_ancestry_with_ghosts([u'notbxbfse']))
 
663
        self.assertEqual([u'b\xbfse'], vf.get_parents_with_ghosts(u'notbxbfse'))
 
664
        self.assertEqual({u'b\xbfse':[],
 
665
                          u'notbxbfse':[u'b\xbfse'],
 
666
                          },
 
667
                         vf.get_graph_with_ghosts())
 
668
        self.assertFalse(vf.has_ghost(u'b\xbfse'))
832
669
 
833
670
    def test_add_lines_with_ghosts_after_normal_revs(self):
834
671
        # some versioned file formats allow lines to be added with parent
838
675
        vf = self.get_file()
839
676
        # probe for ghost support
840
677
        try:
841
 
            vf.add_lines_with_ghosts('base', [], ['line\n', 'line_b\n'])
 
678
            vf.has_ghost('hoo')
842
679
        except NotImplementedError:
843
680
            return
 
681
        vf.add_lines_with_ghosts('base', [], ['line\n', 'line_b\n'])
844
682
        vf.add_lines_with_ghosts('references_ghost',
845
683
                                 ['base', 'a_ghost'],
846
684
                                 ['line\n', 'line_b\n', 'line_c\n'])
854
692
        factory = self.get_factory()
855
693
        vf = factory('id', transport, 0777, create=True, access_mode='w')
856
694
        vf = factory('id', transport, access_mode='r')
 
695
        self.assertRaises(errors.ReadOnlyError, vf.add_delta, '', [], '', '', False, [])
857
696
        self.assertRaises(errors.ReadOnlyError, vf.add_lines, 'base', [], [])
858
697
        self.assertRaises(errors.ReadOnlyError,
859
698
                          vf.add_lines_with_ghosts,
860
699
                          'base',
861
700
                          [],
862
701
                          [])
863
 
 
864
 
    def test_get_sha1s(self):
 
702
        self.assertRaises(errors.ReadOnlyError, vf.fix_parents, 'base', [])
 
703
        self.assertRaises(errors.ReadOnlyError, vf.join, 'base')
 
704
        self.assertRaises(errors.ReadOnlyError, vf.clone_text, 'base', 'bar', ['foo'])
 
705
    
 
706
    def test_get_sha1(self):
865
707
        # check the sha1 data is available
866
708
        vf = self.get_file()
867
709
        # a simple file
870
712
        vf.add_lines('b', ['a'], ['a\n'])
871
713
        # a file differing only in last newline.
872
714
        vf.add_lines('c', [], ['a'])
873
 
        self.assertEqual({
874
 
            'a': '3f786850e387550fdab836ed7e6dc881de23001b',
875
 
            'c': '86f7e437faa5a7fce15d1ddcb9eaeaea377667b8',
876
 
            'b': '3f786850e387550fdab836ed7e6dc881de23001b',
877
 
            },
878
 
            vf.get_sha1s(['a', 'c', 'b']))
879
 
 
880
 
 
881
 
class TestWeave(TestCaseWithMemoryTransport, VersionedFileTestMixIn):
 
715
        self.assertEqual(
 
716
            '3f786850e387550fdab836ed7e6dc881de23001b', vf.get_sha1('a'))
 
717
        self.assertEqual(
 
718
            '3f786850e387550fdab836ed7e6dc881de23001b', vf.get_sha1('b'))
 
719
        self.assertEqual(
 
720
            '86f7e437faa5a7fce15d1ddcb9eaeaea377667b8', vf.get_sha1('c'))
 
721
        
 
722
 
 
723
class TestWeave(TestCaseWithTransport, VersionedFileTestMixIn):
882
724
 
883
725
    def get_file(self, name='foo'):
884
 
        return WeaveFile(name, get_transport(self.get_url('.')), create=True,
885
 
            get_scope=self.get_transaction)
 
726
        return WeaveFile(name, get_transport(self.get_url('.')), create=True)
886
727
 
887
728
    def get_file_corrupted_text(self):
888
 
        w = WeaveFile('foo', get_transport(self.get_url('.')), create=True,
889
 
            get_scope=self.get_transaction)
 
729
        w = WeaveFile('foo', get_transport(self.get_url('.')), create=True)
890
730
        w.add_lines('v1', [], ['hello\n'])
891
731
        w.add_lines('v2', ['v1'], ['hello\n', 'there\n'])
892
 
 
 
732
        
893
733
        # We are going to invasively corrupt the text
894
734
        # Make sure the internals of weave are the same
895
735
        self.assertEqual([('{', 0)
899
739
                        , 'there\n'
900
740
                        , ('}', None)
901
741
                        ], w._weave)
902
 
 
 
742
        
903
743
        self.assertEqual(['f572d396fae9206628714fb2ce00f72e94f2258f'
904
744
                        , '90f265c6e75f1c8f9ab76dcf85528352c5f215ef'
905
745
                        ], w._sha1s)
906
746
        w.check()
907
 
 
 
747
        
908
748
        # Corrupted
909
749
        w._weave[4] = 'There\n'
910
750
        return w
914
754
        # Corrected
915
755
        w._weave[4] = 'there\n'
916
756
        self.assertEqual('hello\nthere\n', w.get_text('v2'))
917
 
 
 
757
        
918
758
        #Invalid checksum, first digit changed
919
759
        w._sha1s[1] =  'f0f265c6e75f1c8f9ab76dcf85528352c5f215ef'
920
760
        return w
921
761
 
922
762
    def reopen_file(self, name='foo', create=False):
923
 
        return WeaveFile(name, get_transport(self.get_url('.')), create=create,
924
 
            get_scope=self.get_transaction)
 
763
        return WeaveFile(name, get_transport(self.get_url('.')), create=create)
925
764
 
926
765
    def test_no_implicit_create(self):
927
766
        self.assertRaises(errors.NoSuchFile,
928
767
                          WeaveFile,
929
768
                          'foo',
930
 
                          get_transport(self.get_url('.')),
931
 
                          get_scope=self.get_transaction)
 
769
                          get_transport(self.get_url('.')))
932
770
 
933
771
    def get_factory(self):
934
772
        return WeaveFile
935
773
 
936
774
 
937
 
class TestPlanMergeVersionedFile(TestCaseWithMemoryTransport):
938
 
 
939
 
    def setUp(self):
940
 
        TestCaseWithMemoryTransport.setUp(self)
941
 
        mapper = PrefixMapper()
942
 
        factory = make_file_factory(True, mapper)
943
 
        self.vf1 = factory(self.get_transport('root-1'))
944
 
        self.vf2 = factory(self.get_transport('root-2'))
945
 
        self.plan_merge_vf = versionedfile._PlanMergeVersionedFile('root')
946
 
        self.plan_merge_vf.fallback_versionedfiles.extend([self.vf1, self.vf2])
947
 
 
948
 
    def test_add_lines(self):
949
 
        self.plan_merge_vf.add_lines(('root', 'a:'), [], [])
950
 
        self.assertRaises(ValueError, self.plan_merge_vf.add_lines,
951
 
            ('root', 'a'), [], [])
952
 
        self.assertRaises(ValueError, self.plan_merge_vf.add_lines,
953
 
            ('root', 'a:'), None, [])
954
 
        self.assertRaises(ValueError, self.plan_merge_vf.add_lines,
955
 
            ('root', 'a:'), [], None)
956
 
 
957
 
    def setup_abcde(self):
958
 
        self.vf1.add_lines(('root', 'A'), [], ['a'])
959
 
        self.vf1.add_lines(('root', 'B'), [('root', 'A')], ['b'])
960
 
        self.vf2.add_lines(('root', 'C'), [], ['c'])
961
 
        self.vf2.add_lines(('root', 'D'), [('root', 'C')], ['d'])
962
 
        self.plan_merge_vf.add_lines(('root', 'E:'),
963
 
            [('root', 'B'), ('root', 'D')], ['e'])
964
 
 
965
 
    def test_get_parents(self):
966
 
        self.setup_abcde()
967
 
        self.assertEqual({('root', 'B'):(('root', 'A'),)},
968
 
            self.plan_merge_vf.get_parent_map([('root', 'B')]))
969
 
        self.assertEqual({('root', 'D'):(('root', 'C'),)},
970
 
            self.plan_merge_vf.get_parent_map([('root', 'D')]))
971
 
        self.assertEqual({('root', 'E:'):(('root', 'B'),('root', 'D'))},
972
 
            self.plan_merge_vf.get_parent_map([('root', 'E:')]))
973
 
        self.assertEqual({},
974
 
            self.plan_merge_vf.get_parent_map([('root', 'F')]))
975
 
        self.assertEqual({
976
 
                ('root', 'B'):(('root', 'A'),),
977
 
                ('root', 'D'):(('root', 'C'),),
978
 
                ('root', 'E:'):(('root', 'B'),('root', 'D')),
979
 
                },
980
 
            self.plan_merge_vf.get_parent_map(
981
 
                [('root', 'B'), ('root', 'D'), ('root', 'E:'), ('root', 'F')]))
982
 
 
983
 
    def test_get_record_stream(self):
984
 
        self.setup_abcde()
985
 
        def get_record(suffix):
986
 
            return self.plan_merge_vf.get_record_stream(
987
 
                [('root', suffix)], 'unordered', True).next()
988
 
        self.assertEqual('a', get_record('A').get_bytes_as('fulltext'))
989
 
        self.assertEqual('c', get_record('C').get_bytes_as('fulltext'))
990
 
        self.assertEqual('e', get_record('E:').get_bytes_as('fulltext'))
991
 
        self.assertEqual('absent', get_record('F').storage_kind)
 
775
class TestKnit(TestCaseWithTransport, VersionedFileTestMixIn):
 
776
 
 
777
    def get_file(self, name='foo'):
 
778
        return KnitVersionedFile(name, get_transport(self.get_url('.')),
 
779
                                 delta=True, create=True)
 
780
 
 
781
    def get_factory(self):
 
782
        return KnitVersionedFile
 
783
 
 
784
    def get_file_corrupted_text(self):
 
785
        knit = self.get_file()
 
786
        knit.add_lines('v1', [], ['hello\n'])
 
787
        knit.add_lines('v2', ['v1'], ['hello\n', 'there\n'])
 
788
        return knit
 
789
 
 
790
    def reopen_file(self, name='foo', create=False):
 
791
        return KnitVersionedFile(name, get_transport(self.get_url('.')),
 
792
            delta=True,
 
793
            create=create)
 
794
 
 
795
    def test_detection(self):
 
796
        knit = self.get_file()
 
797
        knit.check()
 
798
 
 
799
    def test_no_implicit_create(self):
 
800
        self.assertRaises(errors.NoSuchFile,
 
801
                          KnitVersionedFile,
 
802
                          'foo',
 
803
                          get_transport(self.get_url('.')))
 
804
 
 
805
 
 
806
class InterString(versionedfile.InterVersionedFile):
 
807
    """An inter-versionedfile optimised code path for strings.
 
808
 
 
809
    This is for use during testing where we use strings as versionedfiles
 
810
    so that none of the default regsitered interversionedfile classes will
 
811
    match - which lets us test the match logic.
 
812
    """
 
813
 
 
814
    @staticmethod
 
815
    def is_compatible(source, target):
 
816
        """InterString is compatible with strings-as-versionedfiles."""
 
817
        return isinstance(source, str) and isinstance(target, str)
 
818
 
 
819
 
 
820
# TODO this and the InterRepository core logic should be consolidatable
 
821
# if we make the registry a separate class though we still need to 
 
822
# test the behaviour in the active registry to catch failure-to-handle-
 
823
# stange-objects
 
824
class TestInterVersionedFile(TestCaseWithTransport):
 
825
 
 
826
    def test_get_default_inter_versionedfile(self):
 
827
        # test that the InterVersionedFile.get(a, b) probes
 
828
        # for a class where is_compatible(a, b) returns
 
829
        # true and returns a default interversionedfile otherwise.
 
830
        # This also tests that the default registered optimised interversionedfile
 
831
        # classes do not barf inappropriately when a surprising versionedfile type
 
832
        # is handed to them.
 
833
        dummy_a = "VersionedFile 1."
 
834
        dummy_b = "VersionedFile 2."
 
835
        self.assertGetsDefaultInterVersionedFile(dummy_a, dummy_b)
 
836
 
 
837
    def assertGetsDefaultInterVersionedFile(self, a, b):
 
838
        """Asserts that InterVersionedFile.get(a, b) -> the default."""
 
839
        inter = versionedfile.InterVersionedFile.get(a, b)
 
840
        self.assertEqual(versionedfile.InterVersionedFile,
 
841
                         inter.__class__)
 
842
        self.assertEqual(a, inter.source)
 
843
        self.assertEqual(b, inter.target)
 
844
 
 
845
    def test_register_inter_versionedfile_class(self):
 
846
        # test that a optimised code path provider - a
 
847
        # InterVersionedFile subclass can be registered and unregistered
 
848
        # and that it is correctly selected when given a versionedfile
 
849
        # pair that it returns true on for the is_compatible static method
 
850
        # check
 
851
        dummy_a = "VersionedFile 1."
 
852
        dummy_b = "VersionedFile 2."
 
853
        versionedfile.InterVersionedFile.register_optimiser(InterString)
 
854
        try:
 
855
            # we should get the default for something InterString returns False
 
856
            # to
 
857
            self.assertFalse(InterString.is_compatible(dummy_a, None))
 
858
            self.assertGetsDefaultInterVersionedFile(dummy_a, None)
 
859
            # and we should get an InterString for a pair it 'likes'
 
860
            self.assertTrue(InterString.is_compatible(dummy_a, dummy_b))
 
861
            inter = versionedfile.InterVersionedFile.get(dummy_a, dummy_b)
 
862
            self.assertEqual(InterString, inter.__class__)
 
863
            self.assertEqual(dummy_a, inter.source)
 
864
            self.assertEqual(dummy_b, inter.target)
 
865
        finally:
 
866
            versionedfile.InterVersionedFile.unregister_optimiser(InterString)
 
867
        # now we should get the default InterVersionedFile object again.
 
868
        self.assertGetsDefaultInterVersionedFile(dummy_a, dummy_b)
992
869
 
993
870
 
994
871
class TestReadonlyHttpMixin(object):
995
872
 
996
 
    def get_transaction(self):
997
 
        return 1
998
 
 
999
873
    def test_readonly_http_works(self):
1000
874
        # we should be able to read from http with a versioned file.
1001
875
        vf = self.get_file()
1014
888
class TestWeaveHTTP(TestCaseWithWebserver, TestReadonlyHttpMixin):
1015
889
 
1016
890
    def get_file(self):
1017
 
        return WeaveFile('foo', get_transport(self.get_url('.')), create=True,
1018
 
            get_scope=self.get_transaction)
 
891
        return WeaveFile('foo', get_transport(self.get_url('.')), create=True)
1019
892
 
1020
893
    def get_factory(self):
1021
894
        return WeaveFile
1022
895
 
1023
896
 
 
897
class TestKnitHTTP(TestCaseWithWebserver, TestReadonlyHttpMixin):
 
898
 
 
899
    def get_file(self):
 
900
        return KnitVersionedFile('foo', get_transport(self.get_url('.')),
 
901
                                 delta=True, create=True)
 
902
 
 
903
    def get_factory(self):
 
904
        return KnitVersionedFile
 
905
 
 
906
 
1024
907
class MergeCasesMixin(object):
1025
908
 
1026
909
    def doMerge(self, base, a, b, mp):
1029
912
 
1030
913
        def addcrlf(x):
1031
914
            return x + '\n'
1032
 
 
 
915
        
1033
916
        w = self.get_file()
1034
917
        w.add_lines('text0', [], map(addcrlf, base))
1035
918
        w.add_lines('text1', ['text0'], map(addcrlf, a))
1051
934
 
1052
935
        mp = map(addcrlf, mp)
1053
936
        self.assertEqual(mt.readlines(), mp)
1054
 
 
1055
 
 
 
937
        
 
938
        
1056
939
    def testOneInsert(self):
1057
940
        self.doMerge([],
1058
941
                     ['aa'],
1076
959
                     ['aaa', 'xxx', 'yyy', 'bbb'],
1077
960
                     ['aaa', 'xxx', 'bbb'], self.overlappedInsertExpected)
1078
961
 
1079
 
        # really it ought to reduce this to
 
962
        # really it ought to reduce this to 
1080
963
        # ['aaa', 'xxx', 'yyy', 'bbb']
1081
964
 
1082
965
 
1084
967
        self.doMerge(['aaa'],
1085
968
                     ['xxx'],
1086
969
                     ['yyy', 'zzz'],
1087
 
                     ['<<<<<<< ', 'xxx', '=======', 'yyy', 'zzz',
 
970
                     ['<<<<<<< ', 'xxx', '=======', 'yyy', 'zzz', 
1088
971
                      '>>>>>>> '])
1089
972
 
1090
973
    def testNonClashInsert1(self):
1091
974
        self.doMerge(['aaa'],
1092
975
                     ['xxx', 'aaa'],
1093
976
                     ['yyy', 'zzz'],
1094
 
                     ['<<<<<<< ', 'xxx', 'aaa', '=======', 'yyy', 'zzz',
 
977
                     ['<<<<<<< ', 'xxx', 'aaa', '=======', 'yyy', 'zzz', 
1095
978
                      '>>>>>>> '])
1096
979
 
1097
980
    def testNonClashInsert2(self):
1111
994
        #######################################
1112
995
        # skippd, not working yet
1113
996
        return
1114
 
 
 
997
        
1115
998
        self.doMerge(['aaa', 'bbb', 'ccc'],
1116
999
                     ['aaa', 'ddd', 'ccc'],
1117
1000
                     ['aaa', 'ccc'],
1154
1037
            """
1155
1038
        result = """\
1156
1039
            line 1
1157
 
<<<<<<<\x20
1158
 
            line 2
1159
 
=======
1160
 
>>>>>>>\x20
1161
1040
            """
1162
1041
        self._test_merge_from_strings(base, a, b, result)
1163
1042
 
1164
1043
    def test_deletion_overlap(self):
1165
1044
        """Delete overlapping regions with no other conflict.
1166
1045
 
1167
 
        Arguably it'd be better to treat these as agreement, rather than
 
1046
        Arguably it'd be better to treat these as agreement, rather than 
1168
1047
        conflict, but for now conflict is safer.
1169
1048
        """
1170
1049
        base = """\
1186
1065
            """
1187
1066
        result = """\
1188
1067
            start context
1189
 
<<<<<<<\x20
 
1068
<<<<<<< 
1190
1069
            int a() {}
1191
1070
=======
1192
1071
            int c() {}
1193
 
>>>>>>>\x20
 
1072
>>>>>>> 
1194
1073
            end context
1195
1074
            """
1196
1075
        self._test_merge_from_strings(base, a, b, result)
1222
1101
 
1223
1102
    def test_sync_on_deletion(self):
1224
1103
        """Specific case of merge where we can synchronize incorrectly.
1225
 
 
 
1104
        
1226
1105
        A previous version of the weave merge concluded that the two versions
1227
1106
        agreed on deleting line 2, and this could be a synchronization point.
1228
 
        Line 1 was then considered in isolation, and thought to be deleted on
 
1107
        Line 1 was then considered in isolation, and thought to be deleted on 
1229
1108
        both sides.
1230
1109
 
1231
1110
        It's better to consider the whole thing as a disagreement region.
1250
1129
            """
1251
1130
        result = """\
1252
1131
            start context
1253
 
<<<<<<<\x20
 
1132
<<<<<<< 
1254
1133
            base line 1
1255
1134
            a's replacement line 2
1256
1135
=======
1257
1136
            b replaces
1258
1137
            both lines
1259
 
>>>>>>>\x20
 
1138
>>>>>>> 
1260
1139
            end context
1261
1140
            """
1262
1141
        self._test_merge_from_strings(base, a, b, result)
1263
1142
 
1264
1143
 
1265
 
class TestWeaveMerge(TestCaseWithMemoryTransport, MergeCasesMixin):
 
1144
class TestKnitMerge(TestCaseWithTransport, MergeCasesMixin):
 
1145
 
 
1146
    def get_file(self, name='foo'):
 
1147
        return KnitVersionedFile(name, get_transport(self.get_url('.')),
 
1148
                                 delta=True, create=True)
 
1149
 
 
1150
    def log_contents(self, w):
 
1151
        pass
 
1152
 
 
1153
 
 
1154
class TestWeaveMerge(TestCaseWithTransport, MergeCasesMixin):
1266
1155
 
1267
1156
    def get_file(self, name='foo'):
1268
1157
        return WeaveFile(name, get_transport(self.get_url('.')), create=True)
1273
1162
        write_weave(w, tmpf)
1274
1163
        self.log(tmpf.getvalue())
1275
1164
 
1276
 
    overlappedInsertExpected = ['aaa', '<<<<<<< ', 'xxx', 'yyy', '=======',
 
1165
    overlappedInsertExpected = ['aaa', '<<<<<<< ', 'xxx', 'yyy', '=======', 
1277
1166
                                'xxx', '>>>>>>> ', 'bbb']
1278
 
 
1279
 
 
1280
 
class TestContentFactoryAdaption(TestCaseWithMemoryTransport):
1281
 
 
1282
 
    def test_select_adaptor(self):
1283
 
        """Test expected adapters exist."""
1284
 
        # One scenario for each lookup combination we expect to use.
1285
 
        # Each is source_kind, requested_kind, adapter class
1286
 
        scenarios = [
1287
 
            ('knit-delta-gz', 'fulltext', _mod_knit.DeltaPlainToFullText),
1288
 
            ('knit-ft-gz', 'fulltext', _mod_knit.FTPlainToFullText),
1289
 
            ('knit-annotated-delta-gz', 'knit-delta-gz',
1290
 
                _mod_knit.DeltaAnnotatedToUnannotated),
1291
 
            ('knit-annotated-delta-gz', 'fulltext',
1292
 
                _mod_knit.DeltaAnnotatedToFullText),
1293
 
            ('knit-annotated-ft-gz', 'knit-ft-gz',
1294
 
                _mod_knit.FTAnnotatedToUnannotated),
1295
 
            ('knit-annotated-ft-gz', 'fulltext',
1296
 
                _mod_knit.FTAnnotatedToFullText),
1297
 
            ]
1298
 
        for source, requested, klass in scenarios:
1299
 
            adapter_factory = versionedfile.adapter_registry.get(
1300
 
                (source, requested))
1301
 
            adapter = adapter_factory(None)
1302
 
            self.assertIsInstance(adapter, klass)
1303
 
 
1304
 
    def get_knit(self, annotated=True):
1305
 
        mapper = ConstantMapper('knit')
1306
 
        transport = self.get_transport()
1307
 
        return make_file_factory(annotated, mapper)(transport)
1308
 
 
1309
 
    def helpGetBytes(self, f, ft_adapter, delta_adapter):
1310
 
        """Grab the interested adapted texts for tests."""
1311
 
        # origin is a fulltext
1312
 
        entries = f.get_record_stream([('origin',)], 'unordered', False)
1313
 
        base = entries.next()
1314
 
        ft_data = ft_adapter.get_bytes(base)
1315
 
        # merged is both a delta and multiple parents.
1316
 
        entries = f.get_record_stream([('merged',)], 'unordered', False)
1317
 
        merged = entries.next()
1318
 
        delta_data = delta_adapter.get_bytes(merged)
1319
 
        return ft_data, delta_data
1320
 
 
1321
 
    def test_deannotation_noeol(self):
1322
 
        """Test converting annotated knits to unannotated knits."""
1323
 
        # we need a full text, and a delta
1324
 
        f = self.get_knit()
1325
 
        get_diamond_files(f, 1, trailing_eol=False)
1326
 
        ft_data, delta_data = self.helpGetBytes(f,
1327
 
            _mod_knit.FTAnnotatedToUnannotated(None),
1328
 
            _mod_knit.DeltaAnnotatedToUnannotated(None))
1329
 
        self.assertEqual(
1330
 
            'version origin 1 b284f94827db1fa2970d9e2014f080413b547a7e\n'
1331
 
            'origin\n'
1332
 
            'end origin\n',
1333
 
            GzipFile(mode='rb', fileobj=StringIO(ft_data)).read())
1334
 
        self.assertEqual(
1335
 
            'version merged 4 32c2e79763b3f90e8ccde37f9710b6629c25a796\n'
1336
 
            '1,2,3\nleft\nright\nmerged\nend merged\n',
1337
 
            GzipFile(mode='rb', fileobj=StringIO(delta_data)).read())
1338
 
 
1339
 
    def test_deannotation(self):
1340
 
        """Test converting annotated knits to unannotated knits."""
1341
 
        # we need a full text, and a delta
1342
 
        f = self.get_knit()
1343
 
        get_diamond_files(f, 1)
1344
 
        ft_data, delta_data = self.helpGetBytes(f,
1345
 
            _mod_knit.FTAnnotatedToUnannotated(None),
1346
 
            _mod_knit.DeltaAnnotatedToUnannotated(None))
1347
 
        self.assertEqual(
1348
 
            'version origin 1 00e364d235126be43292ab09cb4686cf703ddc17\n'
1349
 
            'origin\n'
1350
 
            'end origin\n',
1351
 
            GzipFile(mode='rb', fileobj=StringIO(ft_data)).read())
1352
 
        self.assertEqual(
1353
 
            'version merged 3 ed8bce375198ea62444dc71952b22cfc2b09226d\n'
1354
 
            '2,2,2\nright\nmerged\nend merged\n',
1355
 
            GzipFile(mode='rb', fileobj=StringIO(delta_data)).read())
1356
 
 
1357
 
    def test_annotated_to_fulltext_no_eol(self):
1358
 
        """Test adapting annotated knits to full texts (for -> weaves)."""
1359
 
        # we need a full text, and a delta
1360
 
        f = self.get_knit()
1361
 
        get_diamond_files(f, 1, trailing_eol=False)
1362
 
        # Reconstructing a full text requires a backing versioned file, and it
1363
 
        # must have the base lines requested from it.
1364
 
        logged_vf = versionedfile.RecordingVersionedFilesDecorator(f)
1365
 
        ft_data, delta_data = self.helpGetBytes(f,
1366
 
            _mod_knit.FTAnnotatedToFullText(None),
1367
 
            _mod_knit.DeltaAnnotatedToFullText(logged_vf))
1368
 
        self.assertEqual('origin', ft_data)
1369
 
        self.assertEqual('base\nleft\nright\nmerged', delta_data)
1370
 
        self.assertEqual([('get_record_stream', [('left',)], 'unordered',
1371
 
            True)], logged_vf.calls)
1372
 
 
1373
 
    def test_annotated_to_fulltext(self):
1374
 
        """Test adapting annotated knits to full texts (for -> weaves)."""
1375
 
        # we need a full text, and a delta
1376
 
        f = self.get_knit()
1377
 
        get_diamond_files(f, 1)
1378
 
        # Reconstructing a full text requires a backing versioned file, and it
1379
 
        # must have the base lines requested from it.
1380
 
        logged_vf = versionedfile.RecordingVersionedFilesDecorator(f)
1381
 
        ft_data, delta_data = self.helpGetBytes(f,
1382
 
            _mod_knit.FTAnnotatedToFullText(None),
1383
 
            _mod_knit.DeltaAnnotatedToFullText(logged_vf))
1384
 
        self.assertEqual('origin\n', ft_data)
1385
 
        self.assertEqual('base\nleft\nright\nmerged\n', delta_data)
1386
 
        self.assertEqual([('get_record_stream', [('left',)], 'unordered',
1387
 
            True)], logged_vf.calls)
1388
 
 
1389
 
    def test_unannotated_to_fulltext(self):
1390
 
        """Test adapting unannotated knits to full texts.
1391
 
 
1392
 
        This is used for -> weaves, and for -> annotated knits.
1393
 
        """
1394
 
        # we need a full text, and a delta
1395
 
        f = self.get_knit(annotated=False)
1396
 
        get_diamond_files(f, 1)
1397
 
        # Reconstructing a full text requires a backing versioned file, and it
1398
 
        # must have the base lines requested from it.
1399
 
        logged_vf = versionedfile.RecordingVersionedFilesDecorator(f)
1400
 
        ft_data, delta_data = self.helpGetBytes(f,
1401
 
            _mod_knit.FTPlainToFullText(None),
1402
 
            _mod_knit.DeltaPlainToFullText(logged_vf))
1403
 
        self.assertEqual('origin\n', ft_data)
1404
 
        self.assertEqual('base\nleft\nright\nmerged\n', delta_data)
1405
 
        self.assertEqual([('get_record_stream', [('left',)], 'unordered',
1406
 
            True)], logged_vf.calls)
1407
 
 
1408
 
    def test_unannotated_to_fulltext_no_eol(self):
1409
 
        """Test adapting unannotated knits to full texts.
1410
 
 
1411
 
        This is used for -> weaves, and for -> annotated knits.
1412
 
        """
1413
 
        # we need a full text, and a delta
1414
 
        f = self.get_knit(annotated=False)
1415
 
        get_diamond_files(f, 1, trailing_eol=False)
1416
 
        # Reconstructing a full text requires a backing versioned file, and it
1417
 
        # must have the base lines requested from it.
1418
 
        logged_vf = versionedfile.RecordingVersionedFilesDecorator(f)
1419
 
        ft_data, delta_data = self.helpGetBytes(f,
1420
 
            _mod_knit.FTPlainToFullText(None),
1421
 
            _mod_knit.DeltaPlainToFullText(logged_vf))
1422
 
        self.assertEqual('origin', ft_data)
1423
 
        self.assertEqual('base\nleft\nright\nmerged', delta_data)
1424
 
        self.assertEqual([('get_record_stream', [('left',)], 'unordered',
1425
 
            True)], logged_vf.calls)
1426
 
 
1427
 
 
1428
 
class TestKeyMapper(TestCaseWithMemoryTransport):
1429
 
    """Tests for various key mapping logic."""
1430
 
 
1431
 
    def test_identity_mapper(self):
1432
 
        mapper = versionedfile.ConstantMapper("inventory")
1433
 
        self.assertEqual("inventory", mapper.map(('foo@ar',)))
1434
 
        self.assertEqual("inventory", mapper.map(('quux',)))
1435
 
 
1436
 
    def test_prefix_mapper(self):
1437
 
        #format5: plain
1438
 
        mapper = versionedfile.PrefixMapper()
1439
 
        self.assertEqual("file-id", mapper.map(("file-id", "revision-id")))
1440
 
        self.assertEqual("new-id", mapper.map(("new-id", "revision-id")))
1441
 
        self.assertEqual(('file-id',), mapper.unmap("file-id"))
1442
 
        self.assertEqual(('new-id',), mapper.unmap("new-id"))
1443
 
 
1444
 
    def test_hash_prefix_mapper(self):
1445
 
        #format6: hash + plain
1446
 
        mapper = versionedfile.HashPrefixMapper()
1447
 
        self.assertEqual("9b/file-id", mapper.map(("file-id", "revision-id")))
1448
 
        self.assertEqual("45/new-id", mapper.map(("new-id", "revision-id")))
1449
 
        self.assertEqual(('file-id',), mapper.unmap("9b/file-id"))
1450
 
        self.assertEqual(('new-id',), mapper.unmap("45/new-id"))
1451
 
 
1452
 
    def test_hash_escaped_mapper(self):
1453
 
        #knit1: hash + escaped
1454
 
        mapper = versionedfile.HashEscapedPrefixMapper()
1455
 
        self.assertEqual("88/%2520", mapper.map((" ", "revision-id")))
1456
 
        self.assertEqual("ed/fil%2545-%2549d", mapper.map(("filE-Id",
1457
 
            "revision-id")))
1458
 
        self.assertEqual("88/ne%2557-%2549d", mapper.map(("neW-Id",
1459
 
            "revision-id")))
1460
 
        self.assertEqual(('filE-Id',), mapper.unmap("ed/fil%2545-%2549d"))
1461
 
        self.assertEqual(('neW-Id',), mapper.unmap("88/ne%2557-%2549d"))
1462
 
 
1463
 
 
1464
 
class TestVersionedFiles(TestCaseWithMemoryTransport):
1465
 
    """Tests for the multiple-file variant of VersionedFile."""
1466
 
 
1467
 
    def get_versionedfiles(self, relpath='files'):
1468
 
        transport = self.get_transport(relpath)
1469
 
        if relpath != '.':
1470
 
            transport.mkdir('.')
1471
 
        files = self.factory(transport)
1472
 
        if self.cleanup is not None:
1473
 
            self.addCleanup(lambda:self.cleanup(files))
1474
 
        return files
1475
 
 
1476
 
    def get_simple_key(self, suffix):
1477
 
        """Return a key for the object under test."""
1478
 
        if self.key_length == 1:
1479
 
            return (suffix,)
1480
 
        else:
1481
 
            return ('FileA',) + (suffix,)
1482
 
 
1483
 
    def test_add_lines(self):
1484
 
        f = self.get_versionedfiles()
1485
 
        key0 = self.get_simple_key('r0')
1486
 
        key1 = self.get_simple_key('r1')
1487
 
        key2 = self.get_simple_key('r2')
1488
 
        keyf = self.get_simple_key('foo')
1489
 
        f.add_lines(key0, [], ['a\n', 'b\n'])
1490
 
        if self.graph:
1491
 
            f.add_lines(key1, [key0], ['b\n', 'c\n'])
1492
 
        else:
1493
 
            f.add_lines(key1, [], ['b\n', 'c\n'])
1494
 
        keys = f.keys()
1495
 
        self.assertTrue(key0 in keys)
1496
 
        self.assertTrue(key1 in keys)
1497
 
        records = []
1498
 
        for record in f.get_record_stream([key0, key1], 'unordered', True):
1499
 
            records.append((record.key, record.get_bytes_as('fulltext')))
1500
 
        records.sort()
1501
 
        self.assertEqual([(key0, 'a\nb\n'), (key1, 'b\nc\n')], records)
1502
 
 
1503
 
    def test__add_text(self):
1504
 
        f = self.get_versionedfiles()
1505
 
        key0 = self.get_simple_key('r0')
1506
 
        key1 = self.get_simple_key('r1')
1507
 
        key2 = self.get_simple_key('r2')
1508
 
        keyf = self.get_simple_key('foo')
1509
 
        f._add_text(key0, [], 'a\nb\n')
1510
 
        if self.graph:
1511
 
            f._add_text(key1, [key0], 'b\nc\n')
1512
 
        else:
1513
 
            f._add_text(key1, [], 'b\nc\n')
1514
 
        keys = f.keys()
1515
 
        self.assertTrue(key0 in keys)
1516
 
        self.assertTrue(key1 in keys)
1517
 
        records = []
1518
 
        for record in f.get_record_stream([key0, key1], 'unordered', True):
1519
 
            records.append((record.key, record.get_bytes_as('fulltext')))
1520
 
        records.sort()
1521
 
        self.assertEqual([(key0, 'a\nb\n'), (key1, 'b\nc\n')], records)
1522
 
 
1523
 
    def test_annotate(self):
1524
 
        files = self.get_versionedfiles()
1525
 
        self.get_diamond_files(files)
1526
 
        if self.key_length == 1:
1527
 
            prefix = ()
1528
 
        else:
1529
 
            prefix = ('FileA',)
1530
 
        # introduced full text
1531
 
        origins = files.annotate(prefix + ('origin',))
1532
 
        self.assertEqual([
1533
 
            (prefix + ('origin',), 'origin\n')],
1534
 
            origins)
1535
 
        # a delta
1536
 
        origins = files.annotate(prefix + ('base',))
1537
 
        self.assertEqual([
1538
 
            (prefix + ('base',), 'base\n')],
1539
 
            origins)
1540
 
        # a merge
1541
 
        origins = files.annotate(prefix + ('merged',))
1542
 
        if self.graph:
1543
 
            self.assertEqual([
1544
 
                (prefix + ('base',), 'base\n'),
1545
 
                (prefix + ('left',), 'left\n'),
1546
 
                (prefix + ('right',), 'right\n'),
1547
 
                (prefix + ('merged',), 'merged\n')
1548
 
                ],
1549
 
                origins)
1550
 
        else:
1551
 
            # Without a graph everything is new.
1552
 
            self.assertEqual([
1553
 
                (prefix + ('merged',), 'base\n'),
1554
 
                (prefix + ('merged',), 'left\n'),
1555
 
                (prefix + ('merged',), 'right\n'),
1556
 
                (prefix + ('merged',), 'merged\n')
1557
 
                ],
1558
 
                origins)
1559
 
        self.assertRaises(RevisionNotPresent,
1560
 
            files.annotate, prefix + ('missing-key',))
1561
 
 
1562
 
    def test_check_no_parameters(self):
1563
 
        files = self.get_versionedfiles()
1564
 
 
1565
 
    def test_check_progressbar_parameter(self):
1566
 
        """A progress bar can be supplied because check can be a generator."""
1567
 
        pb = ui.ui_factory.nested_progress_bar()
1568
 
        self.addCleanup(pb.finished)
1569
 
        files = self.get_versionedfiles()
1570
 
        files.check(progress_bar=pb)
1571
 
 
1572
 
    def test_check_with_keys_becomes_generator(self):
1573
 
        files = self.get_versionedfiles()
1574
 
        self.get_diamond_files(files)
1575
 
        keys = files.keys()
1576
 
        entries = files.check(keys=keys)
1577
 
        seen = set()
1578
 
        # Texts output should be fulltexts.
1579
 
        self.capture_stream(files, entries, seen.add,
1580
 
            files.get_parent_map(keys), require_fulltext=True)
1581
 
        # All texts should be output.
1582
 
        self.assertEqual(set(keys), seen)
1583
 
 
1584
 
    def test_construct(self):
1585
 
        """Each parameterised test can be constructed on a transport."""
1586
 
        files = self.get_versionedfiles()
1587
 
 
1588
 
    def get_diamond_files(self, files, trailing_eol=True, left_only=False,
1589
 
        nokeys=False):
1590
 
        return get_diamond_files(files, self.key_length,
1591
 
            trailing_eol=trailing_eol, nograph=not self.graph,
1592
 
            left_only=left_only, nokeys=nokeys)
1593
 
 
1594
 
    def _add_content_nostoresha(self, add_lines):
1595
 
        """When nostore_sha is supplied using old content raises."""
1596
 
        vf = self.get_versionedfiles()
1597
 
        empty_text = ('a', [])
1598
 
        sample_text_nl = ('b', ["foo\n", "bar\n"])
1599
 
        sample_text_no_nl = ('c', ["foo\n", "bar"])
1600
 
        shas = []
1601
 
        for version, lines in (empty_text, sample_text_nl, sample_text_no_nl):
1602
 
            if add_lines:
1603
 
                sha, _, _ = vf.add_lines(self.get_simple_key(version), [],
1604
 
                                         lines)
1605
 
            else:
1606
 
                sha, _, _ = vf._add_text(self.get_simple_key(version), [],
1607
 
                                         ''.join(lines))
1608
 
            shas.append(sha)
1609
 
        # we now have a copy of all the lines in the vf.
1610
 
        for sha, (version, lines) in zip(
1611
 
            shas, (empty_text, sample_text_nl, sample_text_no_nl)):
1612
 
            new_key = self.get_simple_key(version + "2")
1613
 
            self.assertRaises(errors.ExistingContent,
1614
 
                vf.add_lines, new_key, [], lines,
1615
 
                nostore_sha=sha)
1616
 
            self.assertRaises(errors.ExistingContent,
1617
 
                vf._add_text, new_key, [], ''.join(lines),
1618
 
                nostore_sha=sha)
1619
 
            # and no new version should have been added.
1620
 
            record = vf.get_record_stream([new_key], 'unordered', True).next()
1621
 
            self.assertEqual('absent', record.storage_kind)
1622
 
 
1623
 
    def test_add_lines_nostoresha(self):
1624
 
        self._add_content_nostoresha(add_lines=True)
1625
 
 
1626
 
    def test__add_text_nostoresha(self):
1627
 
        self._add_content_nostoresha(add_lines=False)
1628
 
 
1629
 
    def test_add_lines_return(self):
1630
 
        files = self.get_versionedfiles()
1631
 
        # save code by using the stock data insertion helper.
1632
 
        adds = self.get_diamond_files(files)
1633
 
        results = []
1634
 
        # We can only validate the first 2 elements returned from add_lines.
1635
 
        for add in adds:
1636
 
            self.assertEqual(3, len(add))
1637
 
            results.append(add[:2])
1638
 
        if self.key_length == 1:
1639
 
            self.assertEqual([
1640
 
                ('00e364d235126be43292ab09cb4686cf703ddc17', 7),
1641
 
                ('51c64a6f4fc375daf0d24aafbabe4d91b6f4bb44', 5),
1642
 
                ('a8478686da38e370e32e42e8a0c220e33ee9132f', 10),
1643
 
                ('9ef09dfa9d86780bdec9219a22560c6ece8e0ef1', 11),
1644
 
                ('ed8bce375198ea62444dc71952b22cfc2b09226d', 23)],
1645
 
                results)
1646
 
        elif self.key_length == 2:
1647
 
            self.assertEqual([
1648
 
                ('00e364d235126be43292ab09cb4686cf703ddc17', 7),
1649
 
                ('00e364d235126be43292ab09cb4686cf703ddc17', 7),
1650
 
                ('51c64a6f4fc375daf0d24aafbabe4d91b6f4bb44', 5),
1651
 
                ('51c64a6f4fc375daf0d24aafbabe4d91b6f4bb44', 5),
1652
 
                ('a8478686da38e370e32e42e8a0c220e33ee9132f', 10),
1653
 
                ('a8478686da38e370e32e42e8a0c220e33ee9132f', 10),
1654
 
                ('9ef09dfa9d86780bdec9219a22560c6ece8e0ef1', 11),
1655
 
                ('9ef09dfa9d86780bdec9219a22560c6ece8e0ef1', 11),
1656
 
                ('ed8bce375198ea62444dc71952b22cfc2b09226d', 23),
1657
 
                ('ed8bce375198ea62444dc71952b22cfc2b09226d', 23)],
1658
 
                results)
1659
 
 
1660
 
    def test_add_lines_no_key_generates_chk_key(self):
1661
 
        files = self.get_versionedfiles()
1662
 
        # save code by using the stock data insertion helper.
1663
 
        adds = self.get_diamond_files(files, nokeys=True)
1664
 
        results = []
1665
 
        # We can only validate the first 2 elements returned from add_lines.
1666
 
        for add in adds:
1667
 
            self.assertEqual(3, len(add))
1668
 
            results.append(add[:2])
1669
 
        if self.key_length == 1:
1670
 
            self.assertEqual([
1671
 
                ('00e364d235126be43292ab09cb4686cf703ddc17', 7),
1672
 
                ('51c64a6f4fc375daf0d24aafbabe4d91b6f4bb44', 5),
1673
 
                ('a8478686da38e370e32e42e8a0c220e33ee9132f', 10),
1674
 
                ('9ef09dfa9d86780bdec9219a22560c6ece8e0ef1', 11),
1675
 
                ('ed8bce375198ea62444dc71952b22cfc2b09226d', 23)],
1676
 
                results)
1677
 
            # Check the added items got CHK keys.
1678
 
            self.assertEqual(set([
1679
 
                ('sha1:00e364d235126be43292ab09cb4686cf703ddc17',),
1680
 
                ('sha1:51c64a6f4fc375daf0d24aafbabe4d91b6f4bb44',),
1681
 
                ('sha1:9ef09dfa9d86780bdec9219a22560c6ece8e0ef1',),
1682
 
                ('sha1:a8478686da38e370e32e42e8a0c220e33ee9132f',),
1683
 
                ('sha1:ed8bce375198ea62444dc71952b22cfc2b09226d',),
1684
 
                ]),
1685
 
                files.keys())
1686
 
        elif self.key_length == 2:
1687
 
            self.assertEqual([
1688
 
                ('00e364d235126be43292ab09cb4686cf703ddc17', 7),
1689
 
                ('00e364d235126be43292ab09cb4686cf703ddc17', 7),
1690
 
                ('51c64a6f4fc375daf0d24aafbabe4d91b6f4bb44', 5),
1691
 
                ('51c64a6f4fc375daf0d24aafbabe4d91b6f4bb44', 5),
1692
 
                ('a8478686da38e370e32e42e8a0c220e33ee9132f', 10),
1693
 
                ('a8478686da38e370e32e42e8a0c220e33ee9132f', 10),
1694
 
                ('9ef09dfa9d86780bdec9219a22560c6ece8e0ef1', 11),
1695
 
                ('9ef09dfa9d86780bdec9219a22560c6ece8e0ef1', 11),
1696
 
                ('ed8bce375198ea62444dc71952b22cfc2b09226d', 23),
1697
 
                ('ed8bce375198ea62444dc71952b22cfc2b09226d', 23)],
1698
 
                results)
1699
 
            # Check the added items got CHK keys.
1700
 
            self.assertEqual(set([
1701
 
                ('FileA', 'sha1:00e364d235126be43292ab09cb4686cf703ddc17'),
1702
 
                ('FileA', 'sha1:51c64a6f4fc375daf0d24aafbabe4d91b6f4bb44'),
1703
 
                ('FileA', 'sha1:9ef09dfa9d86780bdec9219a22560c6ece8e0ef1'),
1704
 
                ('FileA', 'sha1:a8478686da38e370e32e42e8a0c220e33ee9132f'),
1705
 
                ('FileA', 'sha1:ed8bce375198ea62444dc71952b22cfc2b09226d'),
1706
 
                ('FileB', 'sha1:00e364d235126be43292ab09cb4686cf703ddc17'),
1707
 
                ('FileB', 'sha1:51c64a6f4fc375daf0d24aafbabe4d91b6f4bb44'),
1708
 
                ('FileB', 'sha1:9ef09dfa9d86780bdec9219a22560c6ece8e0ef1'),
1709
 
                ('FileB', 'sha1:a8478686da38e370e32e42e8a0c220e33ee9132f'),
1710
 
                ('FileB', 'sha1:ed8bce375198ea62444dc71952b22cfc2b09226d'),
1711
 
                ]),
1712
 
                files.keys())
1713
 
 
1714
 
    def test_empty_lines(self):
1715
 
        """Empty files can be stored."""
1716
 
        f = self.get_versionedfiles()
1717
 
        key_a = self.get_simple_key('a')
1718
 
        f.add_lines(key_a, [], [])
1719
 
        self.assertEqual('',
1720
 
            f.get_record_stream([key_a], 'unordered', True
1721
 
                ).next().get_bytes_as('fulltext'))
1722
 
        key_b = self.get_simple_key('b')
1723
 
        f.add_lines(key_b, self.get_parents([key_a]), [])
1724
 
        self.assertEqual('',
1725
 
            f.get_record_stream([key_b], 'unordered', True
1726
 
                ).next().get_bytes_as('fulltext'))
1727
 
 
1728
 
    def test_newline_only(self):
1729
 
        f = self.get_versionedfiles()
1730
 
        key_a = self.get_simple_key('a')
1731
 
        f.add_lines(key_a, [], ['\n'])
1732
 
        self.assertEqual('\n',
1733
 
            f.get_record_stream([key_a], 'unordered', True
1734
 
                ).next().get_bytes_as('fulltext'))
1735
 
        key_b = self.get_simple_key('b')
1736
 
        f.add_lines(key_b, self.get_parents([key_a]), ['\n'])
1737
 
        self.assertEqual('\n',
1738
 
            f.get_record_stream([key_b], 'unordered', True
1739
 
                ).next().get_bytes_as('fulltext'))
1740
 
 
1741
 
    def test_get_known_graph_ancestry(self):
1742
 
        f = self.get_versionedfiles()
1743
 
        if not self.graph:
1744
 
            raise TestNotApplicable('ancestry info only relevant with graph.')
1745
 
        key_a = self.get_simple_key('a')
1746
 
        key_b = self.get_simple_key('b')
1747
 
        key_c = self.get_simple_key('c')
1748
 
        # A
1749
 
        # |\
1750
 
        # | B
1751
 
        # |/
1752
 
        # C
1753
 
        f.add_lines(key_a, [], ['\n'])
1754
 
        f.add_lines(key_b, [key_a], ['\n'])
1755
 
        f.add_lines(key_c, [key_a, key_b], ['\n'])
1756
 
        kg = f.get_known_graph_ancestry([key_c])
1757
 
        self.assertIsInstance(kg, _mod_graph.KnownGraph)
1758
 
        self.assertEqual([key_a, key_b, key_c], list(kg.topo_sort()))
1759
 
 
1760
 
    def test_known_graph_with_fallbacks(self):
1761
 
        f = self.get_versionedfiles('files')
1762
 
        if not self.graph:
1763
 
            raise TestNotApplicable('ancestry info only relevant with graph.')
1764
 
        if getattr(f, 'add_fallback_versioned_files', None) is None:
1765
 
            raise TestNotApplicable("%s doesn't support fallbacks"
1766
 
                                    % (f.__class__.__name__,))
1767
 
        key_a = self.get_simple_key('a')
1768
 
        key_b = self.get_simple_key('b')
1769
 
        key_c = self.get_simple_key('c')
1770
 
        # A     only in fallback
1771
 
        # |\
1772
 
        # | B
1773
 
        # |/
1774
 
        # C
1775
 
        g = self.get_versionedfiles('fallback')
1776
 
        g.add_lines(key_a, [], ['\n'])
1777
 
        f.add_fallback_versioned_files(g)
1778
 
        f.add_lines(key_b, [key_a], ['\n'])
1779
 
        f.add_lines(key_c, [key_a, key_b], ['\n'])
1780
 
        kg = f.get_known_graph_ancestry([key_c])
1781
 
        self.assertEqual([key_a, key_b, key_c], list(kg.topo_sort()))
1782
 
 
1783
 
    def test_get_record_stream_empty(self):
1784
 
        """An empty stream can be requested without error."""
1785
 
        f = self.get_versionedfiles()
1786
 
        entries = f.get_record_stream([], 'unordered', False)
1787
 
        self.assertEqual([], list(entries))
1788
 
 
1789
 
    def assertValidStorageKind(self, storage_kind):
1790
 
        """Assert that storage_kind is a valid storage_kind."""
1791
 
        self.assertSubset([storage_kind],
1792
 
            ['mpdiff', 'knit-annotated-ft', 'knit-annotated-delta',
1793
 
             'knit-ft', 'knit-delta', 'chunked', 'fulltext',
1794
 
             'knit-annotated-ft-gz', 'knit-annotated-delta-gz', 'knit-ft-gz',
1795
 
             'knit-delta-gz',
1796
 
             'knit-delta-closure', 'knit-delta-closure-ref',
1797
 
             'groupcompress-block', 'groupcompress-block-ref'])
1798
 
 
1799
 
    def capture_stream(self, f, entries, on_seen, parents,
1800
 
        require_fulltext=False):
1801
 
        """Capture a stream for testing."""
1802
 
        for factory in entries:
1803
 
            on_seen(factory.key)
1804
 
            self.assertValidStorageKind(factory.storage_kind)
1805
 
            if factory.sha1 is not None:
1806
 
                self.assertEqual(f.get_sha1s([factory.key])[factory.key],
1807
 
                    factory.sha1)
1808
 
            self.assertEqual(parents[factory.key], factory.parents)
1809
 
            self.assertIsInstance(factory.get_bytes_as(factory.storage_kind),
1810
 
                str)
1811
 
            if require_fulltext:
1812
 
                factory.get_bytes_as('fulltext')
1813
 
 
1814
 
    def test_get_record_stream_interface(self):
1815
 
        """each item in a stream has to provide a regular interface."""
1816
 
        files = self.get_versionedfiles()
1817
 
        self.get_diamond_files(files)
1818
 
        keys, _ = self.get_keys_and_sort_order()
1819
 
        parent_map = files.get_parent_map(keys)
1820
 
        entries = files.get_record_stream(keys, 'unordered', False)
1821
 
        seen = set()
1822
 
        self.capture_stream(files, entries, seen.add, parent_map)
1823
 
        self.assertEqual(set(keys), seen)
1824
 
 
1825
 
    def get_keys_and_sort_order(self):
1826
 
        """Get diamond test keys list, and their sort ordering."""
1827
 
        if self.key_length == 1:
1828
 
            keys = [('merged',), ('left',), ('right',), ('base',)]
1829
 
            sort_order = {('merged',):2, ('left',):1, ('right',):1, ('base',):0}
1830
 
        else:
1831
 
            keys = [
1832
 
                ('FileA', 'merged'), ('FileA', 'left'), ('FileA', 'right'),
1833
 
                ('FileA', 'base'),
1834
 
                ('FileB', 'merged'), ('FileB', 'left'), ('FileB', 'right'),
1835
 
                ('FileB', 'base'),
1836
 
                ]
1837
 
            sort_order = {
1838
 
                ('FileA', 'merged'):2, ('FileA', 'left'):1, ('FileA', 'right'):1,
1839
 
                ('FileA', 'base'):0,
1840
 
                ('FileB', 'merged'):2, ('FileB', 'left'):1, ('FileB', 'right'):1,
1841
 
                ('FileB', 'base'):0,
1842
 
                }
1843
 
        return keys, sort_order
1844
 
 
1845
 
    def get_keys_and_groupcompress_sort_order(self):
1846
 
        """Get diamond test keys list, and their groupcompress sort ordering."""
1847
 
        if self.key_length == 1:
1848
 
            keys = [('merged',), ('left',), ('right',), ('base',)]
1849
 
            sort_order = {('merged',):0, ('left',):1, ('right',):1, ('base',):2}
1850
 
        else:
1851
 
            keys = [
1852
 
                ('FileA', 'merged'), ('FileA', 'left'), ('FileA', 'right'),
1853
 
                ('FileA', 'base'),
1854
 
                ('FileB', 'merged'), ('FileB', 'left'), ('FileB', 'right'),
1855
 
                ('FileB', 'base'),
1856
 
                ]
1857
 
            sort_order = {
1858
 
                ('FileA', 'merged'):0, ('FileA', 'left'):1, ('FileA', 'right'):1,
1859
 
                ('FileA', 'base'):2,
1860
 
                ('FileB', 'merged'):3, ('FileB', 'left'):4, ('FileB', 'right'):4,
1861
 
                ('FileB', 'base'):5,
1862
 
                }
1863
 
        return keys, sort_order
1864
 
 
1865
 
    def test_get_record_stream_interface_ordered(self):
1866
 
        """each item in a stream has to provide a regular interface."""
1867
 
        files = self.get_versionedfiles()
1868
 
        self.get_diamond_files(files)
1869
 
        keys, sort_order = self.get_keys_and_sort_order()
1870
 
        parent_map = files.get_parent_map(keys)
1871
 
        entries = files.get_record_stream(keys, 'topological', False)
1872
 
        seen = []
1873
 
        self.capture_stream(files, entries, seen.append, parent_map)
1874
 
        self.assertStreamOrder(sort_order, seen, keys)
1875
 
 
1876
 
    def test_get_record_stream_interface_ordered_with_delta_closure(self):
1877
 
        """each item must be accessible as a fulltext."""
1878
 
        files = self.get_versionedfiles()
1879
 
        self.get_diamond_files(files)
1880
 
        keys, sort_order = self.get_keys_and_sort_order()
1881
 
        parent_map = files.get_parent_map(keys)
1882
 
        entries = files.get_record_stream(keys, 'topological', True)
1883
 
        seen = []
1884
 
        for factory in entries:
1885
 
            seen.append(factory.key)
1886
 
            self.assertValidStorageKind(factory.storage_kind)
1887
 
            self.assertSubset([factory.sha1],
1888
 
                [None, files.get_sha1s([factory.key])[factory.key]])
1889
 
            self.assertEqual(parent_map[factory.key], factory.parents)
1890
 
            # self.assertEqual(files.get_text(factory.key),
1891
 
            ft_bytes = factory.get_bytes_as('fulltext')
1892
 
            self.assertIsInstance(ft_bytes, str)
1893
 
            chunked_bytes = factory.get_bytes_as('chunked')
1894
 
            self.assertEqualDiff(ft_bytes, ''.join(chunked_bytes))
1895
 
 
1896
 
        self.assertStreamOrder(sort_order, seen, keys)
1897
 
 
1898
 
    def test_get_record_stream_interface_groupcompress(self):
1899
 
        """each item in a stream has to provide a regular interface."""
1900
 
        files = self.get_versionedfiles()
1901
 
        self.get_diamond_files(files)
1902
 
        keys, sort_order = self.get_keys_and_groupcompress_sort_order()
1903
 
        parent_map = files.get_parent_map(keys)
1904
 
        entries = files.get_record_stream(keys, 'groupcompress', False)
1905
 
        seen = []
1906
 
        self.capture_stream(files, entries, seen.append, parent_map)
1907
 
        self.assertStreamOrder(sort_order, seen, keys)
1908
 
 
1909
 
    def assertStreamOrder(self, sort_order, seen, keys):
1910
 
        self.assertEqual(len(set(seen)), len(keys))
1911
 
        if self.key_length == 1:
1912
 
            lows = {():0}
1913
 
        else:
1914
 
            lows = {('FileA',):0, ('FileB',):0}
1915
 
        if not self.graph:
1916
 
            self.assertEqual(set(keys), set(seen))
1917
 
        else:
1918
 
            for key in seen:
1919
 
                sort_pos = sort_order[key]
1920
 
                self.assertTrue(sort_pos >= lows[key[:-1]],
1921
 
                    "Out of order in sorted stream: %r, %r" % (key, seen))
1922
 
                lows[key[:-1]] = sort_pos
1923
 
 
1924
 
    def test_get_record_stream_unknown_storage_kind_raises(self):
1925
 
        """Asking for a storage kind that the stream cannot supply raises."""
1926
 
        files = self.get_versionedfiles()
1927
 
        self.get_diamond_files(files)
1928
 
        if self.key_length == 1:
1929
 
            keys = [('merged',), ('left',), ('right',), ('base',)]
1930
 
        else:
1931
 
            keys = [
1932
 
                ('FileA', 'merged'), ('FileA', 'left'), ('FileA', 'right'),
1933
 
                ('FileA', 'base'),
1934
 
                ('FileB', 'merged'), ('FileB', 'left'), ('FileB', 'right'),
1935
 
                ('FileB', 'base'),
1936
 
                ]
1937
 
        parent_map = files.get_parent_map(keys)
1938
 
        entries = files.get_record_stream(keys, 'unordered', False)
1939
 
        # We track the contents because we should be able to try, fail a
1940
 
        # particular kind and then ask for one that works and continue.
1941
 
        seen = set()
1942
 
        for factory in entries:
1943
 
            seen.add(factory.key)
1944
 
            self.assertValidStorageKind(factory.storage_kind)
1945
 
            if factory.sha1 is not None:
1946
 
                self.assertEqual(files.get_sha1s([factory.key])[factory.key],
1947
 
                                 factory.sha1)
1948
 
            self.assertEqual(parent_map[factory.key], factory.parents)
1949
 
            # currently no stream emits mpdiff
1950
 
            self.assertRaises(errors.UnavailableRepresentation,
1951
 
                factory.get_bytes_as, 'mpdiff')
1952
 
            self.assertIsInstance(factory.get_bytes_as(factory.storage_kind),
1953
 
                str)
1954
 
        self.assertEqual(set(keys), seen)
1955
 
 
1956
 
    def test_get_record_stream_missing_records_are_absent(self):
1957
 
        files = self.get_versionedfiles()
1958
 
        self.get_diamond_files(files)
1959
 
        if self.key_length == 1:
1960
 
            keys = [('merged',), ('left',), ('right',), ('absent',), ('base',)]
1961
 
        else:
1962
 
            keys = [
1963
 
                ('FileA', 'merged'), ('FileA', 'left'), ('FileA', 'right'),
1964
 
                ('FileA', 'absent'), ('FileA', 'base'),
1965
 
                ('FileB', 'merged'), ('FileB', 'left'), ('FileB', 'right'),
1966
 
                ('FileB', 'absent'), ('FileB', 'base'),
1967
 
                ('absent', 'absent'),
1968
 
                ]
1969
 
        parent_map = files.get_parent_map(keys)
1970
 
        entries = files.get_record_stream(keys, 'unordered', False)
1971
 
        self.assertAbsentRecord(files, keys, parent_map, entries)
1972
 
        entries = files.get_record_stream(keys, 'topological', False)
1973
 
        self.assertAbsentRecord(files, keys, parent_map, entries)
1974
 
 
1975
 
    def assertRecordHasContent(self, record, bytes):
1976
 
        """Assert that record has the bytes bytes."""
1977
 
        self.assertEqual(bytes, record.get_bytes_as('fulltext'))
1978
 
        self.assertEqual(bytes, ''.join(record.get_bytes_as('chunked')))
1979
 
 
1980
 
    def test_get_record_stream_native_formats_are_wire_ready_one_ft(self):
1981
 
        files = self.get_versionedfiles()
1982
 
        key = self.get_simple_key('foo')
1983
 
        files.add_lines(key, (), ['my text\n', 'content'])
1984
 
        stream = files.get_record_stream([key], 'unordered', False)
1985
 
        record = stream.next()
1986
 
        if record.storage_kind in ('chunked', 'fulltext'):
1987
 
            # chunked and fulltext representations are for direct use not wire
1988
 
            # serialisation: check they are able to be used directly. To send
1989
 
            # such records over the wire translation will be needed.
1990
 
            self.assertRecordHasContent(record, "my text\ncontent")
1991
 
        else:
1992
 
            bytes = [record.get_bytes_as(record.storage_kind)]
1993
 
            network_stream = versionedfile.NetworkRecordStream(bytes).read()
1994
 
            source_record = record
1995
 
            records = []
1996
 
            for record in network_stream:
1997
 
                records.append(record)
1998
 
                self.assertEqual(source_record.storage_kind,
1999
 
                    record.storage_kind)
2000
 
                self.assertEqual(source_record.parents, record.parents)
2001
 
                self.assertEqual(
2002
 
                    source_record.get_bytes_as(source_record.storage_kind),
2003
 
                    record.get_bytes_as(record.storage_kind))
2004
 
            self.assertEqual(1, len(records))
2005
 
 
2006
 
    def assertStreamMetaEqual(self, records, expected, stream):
2007
 
        """Assert that streams expected and stream have the same records.
2008
 
 
2009
 
        :param records: A list to collect the seen records.
2010
 
        :return: A generator of the records in stream.
2011
 
        """
2012
 
        # We make assertions during copying to catch things early for
2013
 
        # easier debugging.
2014
 
        for record, ref_record in izip(stream, expected):
2015
 
            records.append(record)
2016
 
            self.assertEqual(ref_record.key, record.key)
2017
 
            self.assertEqual(ref_record.storage_kind, record.storage_kind)
2018
 
            self.assertEqual(ref_record.parents, record.parents)
2019
 
            yield record
2020
 
 
2021
 
    def stream_to_bytes_or_skip_counter(self, skipped_records, full_texts,
2022
 
        stream):
2023
 
        """Convert a stream to a bytes iterator.
2024
 
 
2025
 
        :param skipped_records: A list with one element to increment when a
2026
 
            record is skipped.
2027
 
        :param full_texts: A dict from key->fulltext representation, for
2028
 
            checking chunked or fulltext stored records.
2029
 
        :param stream: A record_stream.
2030
 
        :return: An iterator over the bytes of each record.
2031
 
        """
2032
 
        for record in stream:
2033
 
            if record.storage_kind in ('chunked', 'fulltext'):
2034
 
                skipped_records[0] += 1
2035
 
                # check the content is correct for direct use.
2036
 
                self.assertRecordHasContent(record, full_texts[record.key])
2037
 
            else:
2038
 
                yield record.get_bytes_as(record.storage_kind)
2039
 
 
2040
 
    def test_get_record_stream_native_formats_are_wire_ready_ft_delta(self):
2041
 
        files = self.get_versionedfiles()
2042
 
        target_files = self.get_versionedfiles('target')
2043
 
        key = self.get_simple_key('ft')
2044
 
        key_delta = self.get_simple_key('delta')
2045
 
        files.add_lines(key, (), ['my text\n', 'content'])
2046
 
        if self.graph:
2047
 
            delta_parents = (key,)
2048
 
        else:
2049
 
            delta_parents = ()
2050
 
        files.add_lines(key_delta, delta_parents, ['different\n', 'content\n'])
2051
 
        local = files.get_record_stream([key, key_delta], 'unordered', False)
2052
 
        ref = files.get_record_stream([key, key_delta], 'unordered', False)
2053
 
        skipped_records = [0]
2054
 
        full_texts = {
2055
 
            key: "my text\ncontent",
2056
 
            key_delta: "different\ncontent\n",
2057
 
            }
2058
 
        byte_stream = self.stream_to_bytes_or_skip_counter(
2059
 
            skipped_records, full_texts, local)
2060
 
        network_stream = versionedfile.NetworkRecordStream(byte_stream).read()
2061
 
        records = []
2062
 
        # insert the stream from the network into a versioned files object so we can
2063
 
        # check the content was carried across correctly without doing delta
2064
 
        # inspection.
2065
 
        target_files.insert_record_stream(
2066
 
            self.assertStreamMetaEqual(records, ref, network_stream))
2067
 
        # No duplicates on the wire thank you!
2068
 
        self.assertEqual(2, len(records) + skipped_records[0])
2069
 
        if len(records):
2070
 
            # if any content was copied it all must have all been.
2071
 
            self.assertIdenticalVersionedFile(files, target_files)
2072
 
 
2073
 
    def test_get_record_stream_native_formats_are_wire_ready_delta(self):
2074
 
        # copy a delta over the wire
2075
 
        files = self.get_versionedfiles()
2076
 
        target_files = self.get_versionedfiles('target')
2077
 
        key = self.get_simple_key('ft')
2078
 
        key_delta = self.get_simple_key('delta')
2079
 
        files.add_lines(key, (), ['my text\n', 'content'])
2080
 
        if self.graph:
2081
 
            delta_parents = (key,)
2082
 
        else:
2083
 
            delta_parents = ()
2084
 
        files.add_lines(key_delta, delta_parents, ['different\n', 'content\n'])
2085
 
        # Copy the basis text across so we can reconstruct the delta during
2086
 
        # insertion into target.
2087
 
        target_files.insert_record_stream(files.get_record_stream([key],
2088
 
            'unordered', False))
2089
 
        local = files.get_record_stream([key_delta], 'unordered', False)
2090
 
        ref = files.get_record_stream([key_delta], 'unordered', False)
2091
 
        skipped_records = [0]
2092
 
        full_texts = {
2093
 
            key_delta: "different\ncontent\n",
2094
 
            }
2095
 
        byte_stream = self.stream_to_bytes_or_skip_counter(
2096
 
            skipped_records, full_texts, local)
2097
 
        network_stream = versionedfile.NetworkRecordStream(byte_stream).read()
2098
 
        records = []
2099
 
        # insert the stream from the network into a versioned files object so we can
2100
 
        # check the content was carried across correctly without doing delta
2101
 
        # inspection during check_stream.
2102
 
        target_files.insert_record_stream(
2103
 
            self.assertStreamMetaEqual(records, ref, network_stream))
2104
 
        # No duplicates on the wire thank you!
2105
 
        self.assertEqual(1, len(records) + skipped_records[0])
2106
 
        if len(records):
2107
 
            # if any content was copied it all must have all been
2108
 
            self.assertIdenticalVersionedFile(files, target_files)
2109
 
 
2110
 
    def test_get_record_stream_wire_ready_delta_closure_included(self):
2111
 
        # copy a delta over the wire with the ability to get its full text.
2112
 
        files = self.get_versionedfiles()
2113
 
        key = self.get_simple_key('ft')
2114
 
        key_delta = self.get_simple_key('delta')
2115
 
        files.add_lines(key, (), ['my text\n', 'content'])
2116
 
        if self.graph:
2117
 
            delta_parents = (key,)
2118
 
        else:
2119
 
            delta_parents = ()
2120
 
        files.add_lines(key_delta, delta_parents, ['different\n', 'content\n'])
2121
 
        local = files.get_record_stream([key_delta], 'unordered', True)
2122
 
        ref = files.get_record_stream([key_delta], 'unordered', True)
2123
 
        skipped_records = [0]
2124
 
        full_texts = {
2125
 
            key_delta: "different\ncontent\n",
2126
 
            }
2127
 
        byte_stream = self.stream_to_bytes_or_skip_counter(
2128
 
            skipped_records, full_texts, local)
2129
 
        network_stream = versionedfile.NetworkRecordStream(byte_stream).read()
2130
 
        records = []
2131
 
        # insert the stream from the network into a versioned files object so we can
2132
 
        # check the content was carried across correctly without doing delta
2133
 
        # inspection during check_stream.
2134
 
        for record in self.assertStreamMetaEqual(records, ref, network_stream):
2135
 
            # we have to be able to get the full text out:
2136
 
            self.assertRecordHasContent(record, full_texts[record.key])
2137
 
        # No duplicates on the wire thank you!
2138
 
        self.assertEqual(1, len(records) + skipped_records[0])
2139
 
 
2140
 
    def assertAbsentRecord(self, files, keys, parents, entries):
2141
 
        """Helper for test_get_record_stream_missing_records_are_absent."""
2142
 
        seen = set()
2143
 
        for factory in entries:
2144
 
            seen.add(factory.key)
2145
 
            if factory.key[-1] == 'absent':
2146
 
                self.assertEqual('absent', factory.storage_kind)
2147
 
                self.assertEqual(None, factory.sha1)
2148
 
                self.assertEqual(None, factory.parents)
2149
 
            else:
2150
 
                self.assertValidStorageKind(factory.storage_kind)
2151
 
                if factory.sha1 is not None:
2152
 
                    sha1 = files.get_sha1s([factory.key])[factory.key]
2153
 
                    self.assertEqual(sha1, factory.sha1)
2154
 
                self.assertEqual(parents[factory.key], factory.parents)
2155
 
                self.assertIsInstance(factory.get_bytes_as(factory.storage_kind),
2156
 
                    str)
2157
 
        self.assertEqual(set(keys), seen)
2158
 
 
2159
 
    def test_filter_absent_records(self):
2160
 
        """Requested missing records can be filter trivially."""
2161
 
        files = self.get_versionedfiles()
2162
 
        self.get_diamond_files(files)
2163
 
        keys, _ = self.get_keys_and_sort_order()
2164
 
        parent_map = files.get_parent_map(keys)
2165
 
        # Add an absent record in the middle of the present keys. (We don't ask
2166
 
        # for just absent keys to ensure that content before and after the
2167
 
        # absent keys is still delivered).
2168
 
        present_keys = list(keys)
2169
 
        if self.key_length == 1:
2170
 
            keys.insert(2, ('extra',))
2171
 
        else:
2172
 
            keys.insert(2, ('extra', 'extra'))
2173
 
        entries = files.get_record_stream(keys, 'unordered', False)
2174
 
        seen = set()
2175
 
        self.capture_stream(files, versionedfile.filter_absent(entries), seen.add,
2176
 
            parent_map)
2177
 
        self.assertEqual(set(present_keys), seen)
2178
 
 
2179
 
    def get_mapper(self):
2180
 
        """Get a mapper suitable for the key length of the test interface."""
2181
 
        if self.key_length == 1:
2182
 
            return ConstantMapper('source')
2183
 
        else:
2184
 
            return HashEscapedPrefixMapper()
2185
 
 
2186
 
    def get_parents(self, parents):
2187
 
        """Get parents, taking self.graph into consideration."""
2188
 
        if self.graph:
2189
 
            return parents
2190
 
        else:
2191
 
            return None
2192
 
 
2193
 
    def test_get_annotator(self):
2194
 
        files = self.get_versionedfiles()
2195
 
        self.get_diamond_files(files)
2196
 
        origin_key = self.get_simple_key('origin')
2197
 
        base_key = self.get_simple_key('base')
2198
 
        left_key = self.get_simple_key('left')
2199
 
        right_key = self.get_simple_key('right')
2200
 
        merged_key = self.get_simple_key('merged')
2201
 
        # annotator = files.get_annotator()
2202
 
        # introduced full text
2203
 
        origins, lines = files.get_annotator().annotate(origin_key)
2204
 
        self.assertEqual([(origin_key,)], origins)
2205
 
        self.assertEqual(['origin\n'], lines)
2206
 
        # a delta
2207
 
        origins, lines = files.get_annotator().annotate(base_key)
2208
 
        self.assertEqual([(base_key,)], origins)
2209
 
        # a merge
2210
 
        origins, lines = files.get_annotator().annotate(merged_key)
2211
 
        if self.graph:
2212
 
            self.assertEqual([
2213
 
                (base_key,),
2214
 
                (left_key,),
2215
 
                (right_key,),
2216
 
                (merged_key,),
2217
 
                ], origins)
2218
 
        else:
2219
 
            # Without a graph everything is new.
2220
 
            self.assertEqual([
2221
 
                (merged_key,),
2222
 
                (merged_key,),
2223
 
                (merged_key,),
2224
 
                (merged_key,),
2225
 
                ], origins)
2226
 
        self.assertRaises(RevisionNotPresent,
2227
 
            files.get_annotator().annotate, self.get_simple_key('missing-key'))
2228
 
 
2229
 
    def test_get_parent_map(self):
2230
 
        files = self.get_versionedfiles()
2231
 
        if self.key_length == 1:
2232
 
            parent_details = [
2233
 
                (('r0',), self.get_parents(())),
2234
 
                (('r1',), self.get_parents((('r0',),))),
2235
 
                (('r2',), self.get_parents(())),
2236
 
                (('r3',), self.get_parents(())),
2237
 
                (('m',), self.get_parents((('r0',),('r1',),('r2',),('r3',)))),
2238
 
                ]
2239
 
        else:
2240
 
            parent_details = [
2241
 
                (('FileA', 'r0'), self.get_parents(())),
2242
 
                (('FileA', 'r1'), self.get_parents((('FileA', 'r0'),))),
2243
 
                (('FileA', 'r2'), self.get_parents(())),
2244
 
                (('FileA', 'r3'), self.get_parents(())),
2245
 
                (('FileA', 'm'), self.get_parents((('FileA', 'r0'),
2246
 
                    ('FileA', 'r1'), ('FileA', 'r2'), ('FileA', 'r3')))),
2247
 
                ]
2248
 
        for key, parents in parent_details:
2249
 
            files.add_lines(key, parents, [])
2250
 
            # immediately after adding it should be queryable.
2251
 
            self.assertEqual({key:parents}, files.get_parent_map([key]))
2252
 
        # We can ask for an empty set
2253
 
        self.assertEqual({}, files.get_parent_map([]))
2254
 
        # We can ask for many keys
2255
 
        all_parents = dict(parent_details)
2256
 
        self.assertEqual(all_parents, files.get_parent_map(all_parents.keys()))
2257
 
        # Absent keys are just not included in the result.
2258
 
        keys = all_parents.keys()
2259
 
        if self.key_length == 1:
2260
 
            keys.insert(1, ('missing',))
2261
 
        else:
2262
 
            keys.insert(1, ('missing', 'missing'))
2263
 
        # Absent keys are just ignored
2264
 
        self.assertEqual(all_parents, files.get_parent_map(keys))
2265
 
 
2266
 
    def test_get_sha1s(self):
2267
 
        files = self.get_versionedfiles()
2268
 
        self.get_diamond_files(files)
2269
 
        if self.key_length == 1:
2270
 
            keys = [('base',), ('origin',), ('left',), ('merged',), ('right',)]
2271
 
        else:
2272
 
            # ask for shas from different prefixes.
2273
 
            keys = [
2274
 
                ('FileA', 'base'), ('FileB', 'origin'), ('FileA', 'left'),
2275
 
                ('FileA', 'merged'), ('FileB', 'right'),
2276
 
                ]
2277
 
        self.assertEqual({
2278
 
            keys[0]: '51c64a6f4fc375daf0d24aafbabe4d91b6f4bb44',
2279
 
            keys[1]: '00e364d235126be43292ab09cb4686cf703ddc17',
2280
 
            keys[2]: 'a8478686da38e370e32e42e8a0c220e33ee9132f',
2281
 
            keys[3]: 'ed8bce375198ea62444dc71952b22cfc2b09226d',
2282
 
            keys[4]: '9ef09dfa9d86780bdec9219a22560c6ece8e0ef1',
2283
 
            },
2284
 
            files.get_sha1s(keys))
2285
 
 
2286
 
    def test_insert_record_stream_empty(self):
2287
 
        """Inserting an empty record stream should work."""
2288
 
        files = self.get_versionedfiles()
2289
 
        files.insert_record_stream([])
2290
 
 
2291
 
    def assertIdenticalVersionedFile(self, expected, actual):
2292
 
        """Assert that left and right have the same contents."""
2293
 
        self.assertEqual(set(actual.keys()), set(expected.keys()))
2294
 
        actual_parents = actual.get_parent_map(actual.keys())
2295
 
        if self.graph:
2296
 
            self.assertEqual(actual_parents, expected.get_parent_map(expected.keys()))
2297
 
        else:
2298
 
            for key, parents in actual_parents.items():
2299
 
                self.assertEqual(None, parents)
2300
 
        for key in actual.keys():
2301
 
            actual_text = actual.get_record_stream(
2302
 
                [key], 'unordered', True).next().get_bytes_as('fulltext')
2303
 
            expected_text = expected.get_record_stream(
2304
 
                [key], 'unordered', True).next().get_bytes_as('fulltext')
2305
 
            self.assertEqual(actual_text, expected_text)
2306
 
 
2307
 
    def test_insert_record_stream_fulltexts(self):
2308
 
        """Any file should accept a stream of fulltexts."""
2309
 
        files = self.get_versionedfiles()
2310
 
        mapper = self.get_mapper()
2311
 
        source_transport = self.get_transport('source')
2312
 
        source_transport.mkdir('.')
2313
 
        # weaves always output fulltexts.
2314
 
        source = make_versioned_files_factory(WeaveFile, mapper)(
2315
 
            source_transport)
2316
 
        self.get_diamond_files(source, trailing_eol=False)
2317
 
        stream = source.get_record_stream(source.keys(), 'topological',
2318
 
            False)
2319
 
        files.insert_record_stream(stream)
2320
 
        self.assertIdenticalVersionedFile(source, files)
2321
 
 
2322
 
    def test_insert_record_stream_fulltexts_noeol(self):
2323
 
        """Any file should accept a stream of fulltexts."""
2324
 
        files = self.get_versionedfiles()
2325
 
        mapper = self.get_mapper()
2326
 
        source_transport = self.get_transport('source')
2327
 
        source_transport.mkdir('.')
2328
 
        # weaves always output fulltexts.
2329
 
        source = make_versioned_files_factory(WeaveFile, mapper)(
2330
 
            source_transport)
2331
 
        self.get_diamond_files(source, trailing_eol=False)
2332
 
        stream = source.get_record_stream(source.keys(), 'topological',
2333
 
            False)
2334
 
        files.insert_record_stream(stream)
2335
 
        self.assertIdenticalVersionedFile(source, files)
2336
 
 
2337
 
    def test_insert_record_stream_annotated_knits(self):
2338
 
        """Any file should accept a stream from plain knits."""
2339
 
        files = self.get_versionedfiles()
2340
 
        mapper = self.get_mapper()
2341
 
        source_transport = self.get_transport('source')
2342
 
        source_transport.mkdir('.')
2343
 
        source = make_file_factory(True, mapper)(source_transport)
2344
 
        self.get_diamond_files(source)
2345
 
        stream = source.get_record_stream(source.keys(), 'topological',
2346
 
            False)
2347
 
        files.insert_record_stream(stream)
2348
 
        self.assertIdenticalVersionedFile(source, files)
2349
 
 
2350
 
    def test_insert_record_stream_annotated_knits_noeol(self):
2351
 
        """Any file should accept a stream from plain knits."""
2352
 
        files = self.get_versionedfiles()
2353
 
        mapper = self.get_mapper()
2354
 
        source_transport = self.get_transport('source')
2355
 
        source_transport.mkdir('.')
2356
 
        source = make_file_factory(True, mapper)(source_transport)
2357
 
        self.get_diamond_files(source, trailing_eol=False)
2358
 
        stream = source.get_record_stream(source.keys(), 'topological',
2359
 
            False)
2360
 
        files.insert_record_stream(stream)
2361
 
        self.assertIdenticalVersionedFile(source, files)
2362
 
 
2363
 
    def test_insert_record_stream_plain_knits(self):
2364
 
        """Any file should accept a stream from plain knits."""
2365
 
        files = self.get_versionedfiles()
2366
 
        mapper = self.get_mapper()
2367
 
        source_transport = self.get_transport('source')
2368
 
        source_transport.mkdir('.')
2369
 
        source = make_file_factory(False, mapper)(source_transport)
2370
 
        self.get_diamond_files(source)
2371
 
        stream = source.get_record_stream(source.keys(), 'topological',
2372
 
            False)
2373
 
        files.insert_record_stream(stream)
2374
 
        self.assertIdenticalVersionedFile(source, files)
2375
 
 
2376
 
    def test_insert_record_stream_plain_knits_noeol(self):
2377
 
        """Any file should accept a stream from plain knits."""
2378
 
        files = self.get_versionedfiles()
2379
 
        mapper = self.get_mapper()
2380
 
        source_transport = self.get_transport('source')
2381
 
        source_transport.mkdir('.')
2382
 
        source = make_file_factory(False, mapper)(source_transport)
2383
 
        self.get_diamond_files(source, trailing_eol=False)
2384
 
        stream = source.get_record_stream(source.keys(), 'topological',
2385
 
            False)
2386
 
        files.insert_record_stream(stream)
2387
 
        self.assertIdenticalVersionedFile(source, files)
2388
 
 
2389
 
    def test_insert_record_stream_existing_keys(self):
2390
 
        """Inserting keys already in a file should not error."""
2391
 
        files = self.get_versionedfiles()
2392
 
        source = self.get_versionedfiles('source')
2393
 
        self.get_diamond_files(source)
2394
 
        # insert some keys into f.
2395
 
        self.get_diamond_files(files, left_only=True)
2396
 
        stream = source.get_record_stream(source.keys(), 'topological',
2397
 
            False)
2398
 
        files.insert_record_stream(stream)
2399
 
        self.assertIdenticalVersionedFile(source, files)
2400
 
 
2401
 
    def test_insert_record_stream_missing_keys(self):
2402
 
        """Inserting a stream with absent keys should raise an error."""
2403
 
        files = self.get_versionedfiles()
2404
 
        source = self.get_versionedfiles('source')
2405
 
        stream = source.get_record_stream([('missing',) * self.key_length],
2406
 
            'topological', False)
2407
 
        self.assertRaises(errors.RevisionNotPresent, files.insert_record_stream,
2408
 
            stream)
2409
 
 
2410
 
    def test_insert_record_stream_out_of_order(self):
2411
 
        """An out of order stream can either error or work."""
2412
 
        files = self.get_versionedfiles()
2413
 
        source = self.get_versionedfiles('source')
2414
 
        self.get_diamond_files(source)
2415
 
        if self.key_length == 1:
2416
 
            origin_keys = [('origin',)]
2417
 
            end_keys = [('merged',), ('left',)]
2418
 
            start_keys = [('right',), ('base',)]
2419
 
        else:
2420
 
            origin_keys = [('FileA', 'origin'), ('FileB', 'origin')]
2421
 
            end_keys = [('FileA', 'merged',), ('FileA', 'left',),
2422
 
                ('FileB', 'merged',), ('FileB', 'left',)]
2423
 
            start_keys = [('FileA', 'right',), ('FileA', 'base',),
2424
 
                ('FileB', 'right',), ('FileB', 'base',)]
2425
 
        origin_entries = source.get_record_stream(origin_keys, 'unordered', False)
2426
 
        end_entries = source.get_record_stream(end_keys, 'topological', False)
2427
 
        start_entries = source.get_record_stream(start_keys, 'topological', False)
2428
 
        entries = chain(origin_entries, end_entries, start_entries)
2429
 
        try:
2430
 
            files.insert_record_stream(entries)
2431
 
        except RevisionNotPresent:
2432
 
            # Must not have corrupted the file.
2433
 
            files.check()
2434
 
        else:
2435
 
            self.assertIdenticalVersionedFile(source, files)
2436
 
 
2437
 
    def get_knit_delta_source(self):
2438
 
        """Get a source that can produce a stream with knit delta records,
2439
 
        regardless of this test's scenario.
2440
 
        """
2441
 
        mapper = self.get_mapper()
2442
 
        source_transport = self.get_transport('source')
2443
 
        source_transport.mkdir('.')
2444
 
        source = make_file_factory(False, mapper)(source_transport)
2445
 
        get_diamond_files(source, self.key_length, trailing_eol=True,
2446
 
            nograph=False, left_only=False)
2447
 
        return source
2448
 
 
2449
 
    def test_insert_record_stream_delta_missing_basis_no_corruption(self):
2450
 
        """Insertion where a needed basis is not included notifies the caller
2451
 
        of the missing basis.  In the meantime a record missing its basis is
2452
 
        not added.
2453
 
        """
2454
 
        source = self.get_knit_delta_source()
2455
 
        keys = [self.get_simple_key('origin'), self.get_simple_key('merged')]
2456
 
        entries = source.get_record_stream(keys, 'unordered', False)
2457
 
        files = self.get_versionedfiles()
2458
 
        if self.support_partial_insertion:
2459
 
            self.assertEqual([],
2460
 
                list(files.get_missing_compression_parent_keys()))
2461
 
            files.insert_record_stream(entries)
2462
 
            missing_bases = files.get_missing_compression_parent_keys()
2463
 
            self.assertEqual(set([self.get_simple_key('left')]),
2464
 
                set(missing_bases))
2465
 
            self.assertEqual(set(keys), set(files.get_parent_map(keys)))
2466
 
        else:
2467
 
            self.assertRaises(
2468
 
                errors.RevisionNotPresent, files.insert_record_stream, entries)
2469
 
            files.check()
2470
 
 
2471
 
    def test_insert_record_stream_delta_missing_basis_can_be_added_later(self):
2472
 
        """Insertion where a needed basis is not included notifies the caller
2473
 
        of the missing basis.  That basis can be added in a second
2474
 
        insert_record_stream call that does not need to repeat records present
2475
 
        in the previous stream.  The record(s) that required that basis are
2476
 
        fully inserted once their basis is no longer missing.
2477
 
        """
2478
 
        if not self.support_partial_insertion:
2479
 
            raise TestNotApplicable(
2480
 
                'versioned file scenario does not support partial insertion')
2481
 
        source = self.get_knit_delta_source()
2482
 
        entries = source.get_record_stream([self.get_simple_key('origin'),
2483
 
            self.get_simple_key('merged')], 'unordered', False)
2484
 
        files = self.get_versionedfiles()
2485
 
        files.insert_record_stream(entries)
2486
 
        missing_bases = files.get_missing_compression_parent_keys()
2487
 
        self.assertEqual(set([self.get_simple_key('left')]),
2488
 
            set(missing_bases))
2489
 
        # 'merged' is inserted (although a commit of a write group involving
2490
 
        # this versionedfiles would fail).
2491
 
        merged_key = self.get_simple_key('merged')
2492
 
        self.assertEqual(
2493
 
            [merged_key], files.get_parent_map([merged_key]).keys())
2494
 
        # Add the full delta closure of the missing records
2495
 
        missing_entries = source.get_record_stream(
2496
 
            missing_bases, 'unordered', True)
2497
 
        files.insert_record_stream(missing_entries)
2498
 
        # Now 'merged' is fully inserted (and a commit would succeed).
2499
 
        self.assertEqual([], list(files.get_missing_compression_parent_keys()))
2500
 
        self.assertEqual(
2501
 
            [merged_key], files.get_parent_map([merged_key]).keys())
2502
 
        files.check()
2503
 
 
2504
 
    def test_iter_lines_added_or_present_in_keys(self):
2505
 
        # test that we get at least an equalset of the lines added by
2506
 
        # versions in the store.
2507
 
        # the ordering here is to make a tree so that dumb searches have
2508
 
        # more changes to muck up.
2509
 
 
2510
 
        class InstrumentedProgress(progress.DummyProgress):
2511
 
 
2512
 
            def __init__(self):
2513
 
 
2514
 
                progress.DummyProgress.__init__(self)
2515
 
                self.updates = []
2516
 
 
2517
 
            def update(self, msg=None, current=None, total=None):
2518
 
                self.updates.append((msg, current, total))
2519
 
 
2520
 
        files = self.get_versionedfiles()
2521
 
        # add a base to get included
2522
 
        files.add_lines(self.get_simple_key('base'), (), ['base\n'])
2523
 
        # add a ancestor to be included on one side
2524
 
        files.add_lines(self.get_simple_key('lancestor'), (), ['lancestor\n'])
2525
 
        # add a ancestor to be included on the other side
2526
 
        files.add_lines(self.get_simple_key('rancestor'),
2527
 
            self.get_parents([self.get_simple_key('base')]), ['rancestor\n'])
2528
 
        # add a child of rancestor with no eofile-nl
2529
 
        files.add_lines(self.get_simple_key('child'),
2530
 
            self.get_parents([self.get_simple_key('rancestor')]),
2531
 
            ['base\n', 'child\n'])
2532
 
        # add a child of lancestor and base to join the two roots
2533
 
        files.add_lines(self.get_simple_key('otherchild'),
2534
 
            self.get_parents([self.get_simple_key('lancestor'),
2535
 
                self.get_simple_key('base')]),
2536
 
            ['base\n', 'lancestor\n', 'otherchild\n'])
2537
 
        def iter_with_keys(keys, expected):
2538
 
            # now we need to see what lines are returned, and how often.
2539
 
            lines = {}
2540
 
            progress = InstrumentedProgress()
2541
 
            # iterate over the lines
2542
 
            for line in files.iter_lines_added_or_present_in_keys(keys,
2543
 
                pb=progress):
2544
 
                lines.setdefault(line, 0)
2545
 
                lines[line] += 1
2546
 
            if []!= progress.updates:
2547
 
                self.assertEqual(expected, progress.updates)
2548
 
            return lines
2549
 
        lines = iter_with_keys(
2550
 
            [self.get_simple_key('child'), self.get_simple_key('otherchild')],
2551
 
            [('Walking content', 0, 2),
2552
 
             ('Walking content', 1, 2),
2553
 
             ('Walking content', 2, 2)])
2554
 
        # we must see child and otherchild
2555
 
        self.assertTrue(lines[('child\n', self.get_simple_key('child'))] > 0)
2556
 
        self.assertTrue(
2557
 
            lines[('otherchild\n', self.get_simple_key('otherchild'))] > 0)
2558
 
        # we dont care if we got more than that.
2559
 
 
2560
 
        # test all lines
2561
 
        lines = iter_with_keys(files.keys(),
2562
 
            [('Walking content', 0, 5),
2563
 
             ('Walking content', 1, 5),
2564
 
             ('Walking content', 2, 5),
2565
 
             ('Walking content', 3, 5),
2566
 
             ('Walking content', 4, 5),
2567
 
             ('Walking content', 5, 5)])
2568
 
        # all lines must be seen at least once
2569
 
        self.assertTrue(lines[('base\n', self.get_simple_key('base'))] > 0)
2570
 
        self.assertTrue(
2571
 
            lines[('lancestor\n', self.get_simple_key('lancestor'))] > 0)
2572
 
        self.assertTrue(
2573
 
            lines[('rancestor\n', self.get_simple_key('rancestor'))] > 0)
2574
 
        self.assertTrue(lines[('child\n', self.get_simple_key('child'))] > 0)
2575
 
        self.assertTrue(
2576
 
            lines[('otherchild\n', self.get_simple_key('otherchild'))] > 0)
2577
 
 
2578
 
    def test_make_mpdiffs(self):
2579
 
        from bzrlib import multiparent
2580
 
        files = self.get_versionedfiles('source')
2581
 
        # add texts that should trip the knit maximum delta chain threshold
2582
 
        # as well as doing parallel chains of data in knits.
2583
 
        # this is done by two chains of 25 insertions
2584
 
        files.add_lines(self.get_simple_key('base'), [], ['line\n'])
2585
 
        files.add_lines(self.get_simple_key('noeol'),
2586
 
            self.get_parents([self.get_simple_key('base')]), ['line'])
2587
 
        # detailed eol tests:
2588
 
        # shared last line with parent no-eol
2589
 
        files.add_lines(self.get_simple_key('noeolsecond'),
2590
 
            self.get_parents([self.get_simple_key('noeol')]),
2591
 
                ['line\n', 'line'])
2592
 
        # differing last line with parent, both no-eol
2593
 
        files.add_lines(self.get_simple_key('noeolnotshared'),
2594
 
            self.get_parents([self.get_simple_key('noeolsecond')]),
2595
 
                ['line\n', 'phone'])
2596
 
        # add eol following a noneol parent, change content
2597
 
        files.add_lines(self.get_simple_key('eol'),
2598
 
            self.get_parents([self.get_simple_key('noeol')]), ['phone\n'])
2599
 
        # add eol following a noneol parent, no change content
2600
 
        files.add_lines(self.get_simple_key('eolline'),
2601
 
            self.get_parents([self.get_simple_key('noeol')]), ['line\n'])
2602
 
        # noeol with no parents:
2603
 
        files.add_lines(self.get_simple_key('noeolbase'), [], ['line'])
2604
 
        # noeol preceeding its leftmost parent in the output:
2605
 
        # this is done by making it a merge of two parents with no common
2606
 
        # anestry: noeolbase and noeol with the
2607
 
        # later-inserted parent the leftmost.
2608
 
        files.add_lines(self.get_simple_key('eolbeforefirstparent'),
2609
 
            self.get_parents([self.get_simple_key('noeolbase'),
2610
 
                self.get_simple_key('noeol')]),
2611
 
            ['line'])
2612
 
        # two identical eol texts
2613
 
        files.add_lines(self.get_simple_key('noeoldup'),
2614
 
            self.get_parents([self.get_simple_key('noeol')]), ['line'])
2615
 
        next_parent = self.get_simple_key('base')
2616
 
        text_name = 'chain1-'
2617
 
        text = ['line\n']
2618
 
        sha1s = {0 :'da6d3141cb4a5e6f464bf6e0518042ddc7bfd079',
2619
 
                 1 :'45e21ea146a81ea44a821737acdb4f9791c8abe7',
2620
 
                 2 :'e1f11570edf3e2a070052366c582837a4fe4e9fa',
2621
 
                 3 :'26b4b8626da827088c514b8f9bbe4ebf181edda1',
2622
 
                 4 :'e28a5510be25ba84d31121cff00956f9970ae6f6',
2623
 
                 5 :'d63ec0ce22e11dcf65a931b69255d3ac747a318d',
2624
 
                 6 :'2c2888d288cb5e1d98009d822fedfe6019c6a4ea',
2625
 
                 7 :'95c14da9cafbf828e3e74a6f016d87926ba234ab',
2626
 
                 8 :'779e9a0b28f9f832528d4b21e17e168c67697272',
2627
 
                 9 :'1f8ff4e5c6ff78ac106fcfe6b1e8cb8740ff9a8f',
2628
 
                 10:'131a2ae712cf51ed62f143e3fbac3d4206c25a05',
2629
 
                 11:'c5a9d6f520d2515e1ec401a8f8a67e6c3c89f199',
2630
 
                 12:'31a2286267f24d8bedaa43355f8ad7129509ea85',
2631
 
                 13:'dc2a7fe80e8ec5cae920973973a8ee28b2da5e0a',
2632
 
                 14:'2c4b1736566b8ca6051e668de68650686a3922f2',
2633
 
                 15:'5912e4ecd9b0c07be4d013e7e2bdcf9323276cde',
2634
 
                 16:'b0d2e18d3559a00580f6b49804c23fea500feab3',
2635
 
                 17:'8e1d43ad72f7562d7cb8f57ee584e20eb1a69fc7',
2636
 
                 18:'5cf64a3459ae28efa60239e44b20312d25b253f3',
2637
 
                 19:'1ebed371807ba5935958ad0884595126e8c4e823',
2638
 
                 20:'2aa62a8b06fb3b3b892a3292a068ade69d5ee0d3',
2639
 
                 21:'01edc447978004f6e4e962b417a4ae1955b6fe5d',
2640
 
                 22:'d8d8dc49c4bf0bab401e0298bb5ad827768618bb',
2641
 
                 23:'c21f62b1c482862983a8ffb2b0c64b3451876e3f',
2642
 
                 24:'c0593fe795e00dff6b3c0fe857a074364d5f04fc',
2643
 
                 25:'dd1a1cf2ba9cc225c3aff729953e6364bf1d1855',
2644
 
                 }
2645
 
        for depth in range(26):
2646
 
            new_version = self.get_simple_key(text_name + '%s' % depth)
2647
 
            text = text + ['line\n']
2648
 
            files.add_lines(new_version, self.get_parents([next_parent]), text)
2649
 
            next_parent = new_version
2650
 
        next_parent = self.get_simple_key('base')
2651
 
        text_name = 'chain2-'
2652
 
        text = ['line\n']
2653
 
        for depth in range(26):
2654
 
            new_version = self.get_simple_key(text_name + '%s' % depth)
2655
 
            text = text + ['line\n']
2656
 
            files.add_lines(new_version, self.get_parents([next_parent]), text)
2657
 
            next_parent = new_version
2658
 
        target = self.get_versionedfiles('target')
2659
 
        for key in multiparent.topo_iter_keys(files, files.keys()):
2660
 
            mpdiff = files.make_mpdiffs([key])[0]
2661
 
            parents = files.get_parent_map([key])[key] or []
2662
 
            target.add_mpdiffs(
2663
 
                [(key, parents, files.get_sha1s([key])[key], mpdiff)])
2664
 
            self.assertEqualDiff(
2665
 
                files.get_record_stream([key], 'unordered',
2666
 
                    True).next().get_bytes_as('fulltext'),
2667
 
                target.get_record_stream([key], 'unordered',
2668
 
                    True).next().get_bytes_as('fulltext')
2669
 
                )
2670
 
 
2671
 
    def test_keys(self):
2672
 
        # While use is discouraged, versions() is still needed by aspects of
2673
 
        # bzr.
2674
 
        files = self.get_versionedfiles()
2675
 
        self.assertEqual(set(), set(files.keys()))
2676
 
        if self.key_length == 1:
2677
 
            key = ('foo',)
2678
 
        else:
2679
 
            key = ('foo', 'bar',)
2680
 
        files.add_lines(key, (), [])
2681
 
        self.assertEqual(set([key]), set(files.keys()))
2682
 
 
2683
 
 
2684
 
class VirtualVersionedFilesTests(TestCase):
2685
 
    """Basic tests for the VirtualVersionedFiles implementations."""
2686
 
 
2687
 
    def _get_parent_map(self, keys):
2688
 
        ret = {}
2689
 
        for k in keys:
2690
 
            if k in self._parent_map:
2691
 
                ret[k] = self._parent_map[k]
2692
 
        return ret
2693
 
 
2694
 
    def setUp(self):
2695
 
        TestCase.setUp(self)
2696
 
        self._lines = {}
2697
 
        self._parent_map = {}
2698
 
        self.texts = VirtualVersionedFiles(self._get_parent_map,
2699
 
                                           self._lines.get)
2700
 
 
2701
 
    def test_add_lines(self):
2702
 
        self.assertRaises(NotImplementedError,
2703
 
                self.texts.add_lines, "foo", [], [])
2704
 
 
2705
 
    def test_add_mpdiffs(self):
2706
 
        self.assertRaises(NotImplementedError,
2707
 
                self.texts.add_mpdiffs, [])
2708
 
 
2709
 
    def test_check_noerrors(self):
2710
 
        self.texts.check()
2711
 
 
2712
 
    def test_insert_record_stream(self):
2713
 
        self.assertRaises(NotImplementedError, self.texts.insert_record_stream,
2714
 
                          [])
2715
 
 
2716
 
    def test_get_sha1s_nonexistent(self):
2717
 
        self.assertEquals({}, self.texts.get_sha1s([("NONEXISTENT",)]))
2718
 
 
2719
 
    def test_get_sha1s(self):
2720
 
        self._lines["key"] = ["dataline1", "dataline2"]
2721
 
        self.assertEquals({("key",): osutils.sha_strings(self._lines["key"])},
2722
 
                           self.texts.get_sha1s([("key",)]))
2723
 
 
2724
 
    def test_get_parent_map(self):
2725
 
        self._parent_map = {"G": ("A", "B")}
2726
 
        self.assertEquals({("G",): (("A",),("B",))},
2727
 
                          self.texts.get_parent_map([("G",), ("L",)]))
2728
 
 
2729
 
    def test_get_record_stream(self):
2730
 
        self._lines["A"] = ["FOO", "BAR"]
2731
 
        it = self.texts.get_record_stream([("A",)], "unordered", True)
2732
 
        record = it.next()
2733
 
        self.assertEquals("chunked", record.storage_kind)
2734
 
        self.assertEquals("FOOBAR", record.get_bytes_as("fulltext"))
2735
 
        self.assertEquals(["FOO", "BAR"], record.get_bytes_as("chunked"))
2736
 
 
2737
 
    def test_get_record_stream_absent(self):
2738
 
        it = self.texts.get_record_stream([("A",)], "unordered", True)
2739
 
        record = it.next()
2740
 
        self.assertEquals("absent", record.storage_kind)
2741
 
 
2742
 
    def test_iter_lines_added_or_present_in_keys(self):
2743
 
        self._lines["A"] = ["FOO", "BAR"]
2744
 
        self._lines["B"] = ["HEY"]
2745
 
        self._lines["C"] = ["Alberta"]
2746
 
        it = self.texts.iter_lines_added_or_present_in_keys([("A",), ("B",)])
2747
 
        self.assertEquals(sorted([("FOO", "A"), ("BAR", "A"), ("HEY", "B")]),
2748
 
            sorted(list(it)))
2749
 
 
2750
 
 
2751
 
class TestOrderingVersionedFilesDecorator(TestCaseWithMemoryTransport):
2752
 
 
2753
 
    def get_ordering_vf(self, key_priority):
2754
 
        builder = self.make_branch_builder('test')
2755
 
        builder.start_series()
2756
 
        builder.build_snapshot('A', None, [
2757
 
            ('add', ('', 'TREE_ROOT', 'directory', None))])
2758
 
        builder.build_snapshot('B', ['A'], [])
2759
 
        builder.build_snapshot('C', ['B'], [])
2760
 
        builder.build_snapshot('D', ['C'], [])
2761
 
        builder.finish_series()
2762
 
        b = builder.get_branch()
2763
 
        b.lock_read()
2764
 
        self.addCleanup(b.unlock)
2765
 
        vf = b.repository.inventories
2766
 
        return versionedfile.OrderingVersionedFilesDecorator(vf, key_priority)
2767
 
 
2768
 
    def test_get_empty(self):
2769
 
        vf = self.get_ordering_vf({})
2770
 
        self.assertEqual([], vf.calls)
2771
 
 
2772
 
    def test_get_record_stream_topological(self):
2773
 
        vf = self.get_ordering_vf({('A',): 3, ('B',): 2, ('C',): 4, ('D',): 1})
2774
 
        request_keys = [('B',), ('C',), ('D',), ('A',)]
2775
 
        keys = [r.key for r in vf.get_record_stream(request_keys,
2776
 
                                    'topological', False)]
2777
 
        # We should have gotten the keys in topological order
2778
 
        self.assertEqual([('A',), ('B',), ('C',), ('D',)], keys)
2779
 
        # And recorded that the request was made
2780
 
        self.assertEqual([('get_record_stream', request_keys, 'topological',
2781
 
                           False)], vf.calls)
2782
 
 
2783
 
    def test_get_record_stream_ordered(self):
2784
 
        vf = self.get_ordering_vf({('A',): 3, ('B',): 2, ('C',): 4, ('D',): 1})
2785
 
        request_keys = [('B',), ('C',), ('D',), ('A',)]
2786
 
        keys = [r.key for r in vf.get_record_stream(request_keys,
2787
 
                                   'unordered', False)]
2788
 
        # They should be returned based on their priority
2789
 
        self.assertEqual([('D',), ('B',), ('A',), ('C',)], keys)
2790
 
        # And the request recorded
2791
 
        self.assertEqual([('get_record_stream', request_keys, 'unordered',
2792
 
                           False)], vf.calls)
2793
 
 
2794
 
    def test_get_record_stream_implicit_order(self):
2795
 
        vf = self.get_ordering_vf({('B',): 2, ('D',): 1})
2796
 
        request_keys = [('B',), ('C',), ('D',), ('A',)]
2797
 
        keys = [r.key for r in vf.get_record_stream(request_keys,
2798
 
                                   'unordered', False)]
2799
 
        # A and C are not in the map, so they get sorted to the front. A comes
2800
 
        # before C alphabetically, so it comes back first
2801
 
        self.assertEqual([('A',), ('C',), ('D',), ('B',)], keys)
2802
 
        # And the request recorded
2803
 
        self.assertEqual([('get_record_stream', request_keys, 'unordered',
2804
 
                           False)], vf.calls)