/brz/remove-bazaar

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