/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 = {
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
85
        b'origin': (),
86
        b'base': ((b'origin',),),
87
        b'left': ((b'base',),),
88
        b'right': ((b'base',),),
89
        b'merged': ((b'left',), (b'right',)),
3350.3.4 by Robert Collins
Finish adapters for annotated knits to unannotated knits and full texts.
90
        }
91
    # insert a diamond graph to exercise deltas and merges.
92
    if trailing_eol:
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
93
        last_char = b'\n'
3350.3.4 by Robert Collins
Finish adapters for annotated knits to unannotated knits and full texts.
94
    else:
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
95
        last_char = b''
96
    f.add_lines(b'origin', [], [b'origin' + last_char])
97
    f.add_lines(b'base', [b'origin'], [b'base' + last_char])
98
    f.add_lines(b'left', [b'base'], [b'base\n', b'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:
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
100
        f.add_lines(b'right', [b'base'],
101
            [b'base\n', b'right' + last_char])
102
        f.add_lines(b'merged', [b'left', b'right'],
103
            [b'base\n', b'left\n', b'right\n', b'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:
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
130
        prefixes = [(b'FileA',), (b'FileB',)]
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.
131
    # insert a diamond graph to exercise deltas and merges.
132
    if trailing_eol:
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
133
        last_char = b'\n'
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.
134
    else:
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
135
        last_char = b''
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.
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:
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
151
        result.append(files.add_lines(prefix + get_key(b'origin'), (),
152
            [b'origin' + last_char]))
153
    for prefix in prefixes:
154
        result.append(files.add_lines(prefix + get_key(b'base'),
155
            get_parents([(b'origin',)]), [b'base' + last_char]))
156
    for prefix in prefixes:
157
        result.append(files.add_lines(prefix + get_key(b'left'),
158
            get_parents([(b'base',)]),
159
            [b'base\n', b'left' + last_char]))
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.
160
    if not left_only:
161
        for prefix in prefixes:
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
162
            result.append(files.add_lines(prefix + get_key(b'right'),
163
                get_parents([(b'base',)]),
164
                [b'base\n', b'right' + last_char]))
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.
165
        for prefix in prefixes:
6963.2.10 by Jelmer Vernooij
Fix a bunch of tests.
166
            result.append(files.add_lines(prefix + get_key(b'merged'),
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
167
                get_parents([(b'left',), (b'right',)]),
168
                [b'base\n', b'left\n', b'right\n', b'merged' + last_char]))
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.
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()
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
187
        f.add_lines(b'r0', [], [b'a\n', b'b\n'])
188
        f.add_lines(b'r1', [b'r0'], [b'b\n', b'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()
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
191
            self.assertTrue(b'r0' in versions)
192
            self.assertTrue(b'r1' in versions)
193
            self.assertEqual(f.get_lines(b'r0'), [b'a\n', b'b\n'])
194
            self.assertEqual(f.get_text(b'r0'), b'a\nb\n')
195
            self.assertEqual(f.get_lines(b'r1'), [b'b\n', b'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,
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
200
                f.add_lines, b'r2', [b'foo'], [])
1563.2.9 by Robert Collins
Update versionedfile api tests to ensure that data is available after every operation.
201
            self.assertRaises(RevisionAlreadyPresent,
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
202
                f.add_lines, b'r1', [], [])
1563.2.9 by Robert Collins
Update versionedfile api tests to ensure that data is available after every operation.
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 = {}
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
211
        _, _, parent_texts[b'r0'] = f.add_lines(b'r0', [], [b'a\n', b'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:
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
213
            _, _, parent_texts[b'r1'] = f.add_lines_with_ghosts(b'r1',
214
                [b'r0', b'ghost'], [b'b\n', b'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.
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
217
            _, _, parent_texts[b'r1'] = f.add_lines(b'r1',
218
                [b'r0'], [b'b\n', b'c\n'], parent_texts=parent_texts)
219
        f.add_lines(b'r2', [b'r1'], [b'c\n', b'd\n'], parent_texts=parent_texts)
220
        self.assertNotEqual(None, parent_texts[b'r0'])
221
        self.assertNotEqual(None, parent_texts[b'r1'])
1596.2.32 by Robert Collins
Reduce re-extraction of texts during weave to knit joins by providing a memoisation facility.
222
        def verify_file(f):
223
            versions = f.versions()
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
224
            self.assertTrue(b'r0' in versions)
225
            self.assertTrue(b'r1' in versions)
226
            self.assertTrue(b'r2' in versions)
227
            self.assertEqual(f.get_lines(b'r0'), [b'a\n', b'b\n'])
228
            self.assertEqual(f.get_lines(b'r1'), [b'b\n', b'c\n'])
229
            self.assertEqual(f.get_lines(b'r2'), [b'c\n', b'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())
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
231
            origins = f.annotate(b'r1')
232
            self.assertEqual(origins[0][0], b'r0')
233
            self.assertEqual(origins[1][0], b'r1')
234
            origins = f.annotate(b'r2')
235
            self.assertEqual(origins[0][0], b'r1')
236
            self.assertEqual(origins[1][0], b'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,
6973.14.4 by Jelmer Vernooij
Fix checking for options.
247
            vf.add_lines, b'a', [], [b'a\n', u'b\n', b'c\n'])
2805.6.7 by Robert Collins
Review feedback.
248
        self.assertRaises(
249
            (errors.BzrBadParameterUnicode, NotImplementedError),
6973.14.4 by Jelmer Vernooij
Fix checking for options.
250
            vf.add_lines_with_ghosts, b'a', [], [b'a\n', u'b\n', b'c\n'])
2805.6.7 by Robert Collins
Review feedback.
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")
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
261
        vf.add_lines(b'1', [], [b'a\n'])
262
        vf.add_lines(b'2', [b'1'], [b'a\n', b'a\n', b'a\n'],
2520.4.150 by Aaron Bentley
Test that non-Weave uses left_matching_blocks for add_lines
263
                     left_matching_blocks=[(0, 0, 1), (1, 3, 0)])
6963.2.4 by Jelmer Vernooij
Add bees.
264
        self.assertEqual([b'a\n', b'a\n', b'a\n'], vf.get_lines(b'2'))
265
        vf.add_lines(b'3', [b'1'], [b'a\n', b'a\n', b'a\n'],
2520.4.150 by Aaron Bentley
Test that non-Weave uses left_matching_blocks for add_lines
266
                     left_matching_blocks=[(0, 2, 1), (1, 3, 0)])
6963.2.4 by Jelmer Vernooij
Add bees.
267
        self.assertEqual([b'a\n', b'a\n', b'a\n'], vf.get_lines(b'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,
6963.2.3 by Jelmer Vernooij
Fix some more tests.
273
            vf.add_lines, b'a', [], [b'a\n\n'])
2805.6.7 by Robert Collins
Review feedback.
274
        self.assertRaises(
275
            (errors.BzrBadParameterContainsNewline, NotImplementedError),
6963.2.3 by Jelmer Vernooij
Fix some more tests.
276
            vf.add_lines_with_ghosts, b'a', [], [b'a\n\n'])
2805.6.7 by Robert Collins
Review feedback.
277
        # but inline CR's are allowed
6963.2.3 by Jelmer Vernooij
Fix some more tests.
278
        vf.add_lines(b'a', [], [b'a\r\n'])
2805.6.7 by Robert Collins
Review feedback.
279
        try:
6963.2.3 by Jelmer Vernooij
Fix some more tests.
280
            vf.add_lines_with_ghosts(b'b', [], [b'a\r\n'])
2805.6.7 by Robert Collins
Review feedback.
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,
6963.2.3 by Jelmer Vernooij
Fix some more tests.
287
            vf.add_lines, b'a:', [], [b'a\n', b'b\n', b'c\n'])
2229.2.1 by Aaron Bentley
Reject reserved ids in versiondfile, tree, branch and repository
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()
6963.2.4 by Jelmer Vernooij
Add bees.
292
        empty_text = (b'a', [])
293
        sample_text_nl = (b'b', [b"foo\n", b"bar\n"])
294
        sample_text_no_nl = (b'c', [b"foo\n", b"bar"])
2794.1.1 by Robert Collins
Allow knits to be instructed not to add a text based on a sha, for commit.
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,
6963.2.4 by Jelmer Vernooij
Add bees.
303
                vf.add_lines, version + b"2", [], lines,
2794.1.1 by Robert Collins
Allow knits to be instructed not to add a text based on a sha, for commit.
304
                nostore_sha=sha)
305
            # and no new version should have been added.
306
            self.assertRaises(errors.RevisionNotPresent, vf.get_lines,
6963.2.4 by Jelmer Vernooij
Add bees.
307
                version + b"2")
2794.1.1 by Robert Collins
Allow knits to be instructed not to add a text based on a sha, for commit.
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()
6963.2.4 by Jelmer Vernooij
Add bees.
312
        empty_text = (b'a', [])
313
        sample_text_nl = (b'b', [b"foo\n", b"bar\n"])
314
        sample_text_no_nl = (b'c', [b"foo\n", b"bar"])
2794.1.1 by Robert Collins
Allow knits to be instructed not to add a text based on a sha, for commit.
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:
6963.2.4 by Jelmer Vernooij
Add bees.
322
            vf.add_lines_with_ghosts(b'd', [], [])
2794.1.1 by Robert Collins
Allow knits to be instructed not to add a text based on a sha, for commit.
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,
6963.2.4 by Jelmer Vernooij
Add bees.
328
                vf.add_lines_with_ghosts, version + b"2", [], lines,
2794.1.1 by Robert Collins
Allow knits to be instructed not to add a text based on a sha, for commit.
329
                nostore_sha=sha)
330
            # and no new version should have been added.
331
            self.assertRaises(errors.RevisionNotPresent, vf.get_lines,
6963.2.4 by Jelmer Vernooij
Add bees.
332
                version + b"2")
2794.1.1 by Robert Collins
Allow knits to be instructed not to add a text based on a sha, for commit.
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()
6963.2.4 by Jelmer Vernooij
Add bees.
337
        empty_text = (b'a', [])
338
        sample_text_nl = (b'b', [b"foo\n", b"bar\n"])
339
        sample_text_no_nl = (b'c', [b"foo\n", b"bar"])
2776.1.1 by Robert Collins
* The ``add_lines`` methods on ``VersionedFile`` implementations has changed
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))),
6963.2.4 by Jelmer Vernooij
Add bees.
352
            vf.add_lines(b'd', [b'b', b'c'], lines)[0:2])
2776.1.1 by Robert Collins
* The ``add_lines`` methods on ``VersionedFile`` implementations has changed
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()
6963.2.4 by Jelmer Vernooij
Add bees.
356
        self.assertRaises(errors.ReservedId, vf.get_texts, [b'b:'])
357
        self.assertRaises(errors.ReservedId, vf.get_lines, b'b:')
358
        self.assertRaises(errors.ReservedId, vf.get_text, b'b:')
2229.2.1 by Aaron Bentley
Reject reserved ids in versiondfile, tree, branch and repository
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)
6963.2.4 by Jelmer Vernooij
Add bees.
373
            prefix = b'step-%d'
3468.2.4 by Martin Pool
Test and fix #234748 problems in trailing newline diffs
374
            parents = []
375
            for step in range(length):
376
                version = prefix % step
6963.2.4 by Jelmer Vernooij
Add bees.
377
                lines = ([b'prelude \n'] * step) + [b'line']
3468.2.4 by Martin Pool
Test and fix #234748 problems in trailing newline diffs
378
                vf.add_lines(version, parents, lines)
379
                version_lines[version] = lines
380
                parents = [version]
6963.2.4 by Jelmer Vernooij
Add bees.
381
            vf.add_lines(b'no-eol', parents, [b'line'])
3468.2.4 by Martin Pool
Test and fix #234748 problems in trailing newline diffs
382
            vf.get_texts(version_lines.keys())
6963.2.4 by Jelmer Vernooij
Add bees.
383
            self.assertEqualDiff(b'line', vf.get_text(b'no-eol'))
3468.2.4 by Martin Pool
Test and fix #234748 problems in trailing newline diffs
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()
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
388
        sample_text_nl = [b"line\n"]
389
        sample_text_no_nl = [b"line"]
3468.2.4 by Martin Pool
Test and fix #234748 problems in trailing newline diffs
390
        versions = []
391
        version_lines = {}
392
        parents = []
393
        for i in range(4):
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
394
            version = b'v%d' % i
3468.2.4 by Martin Pool
Test and fix #234748 problems in trailing newline diffs
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')
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
424
        vf.add_lines(b'noeol', [], [b'line'])
425
        vf.add_lines(b'noeol2', [b'noeol'], [b'newline\n', b'line'],
3460.2.1 by Robert Collins
* Inserting a bundle which changes the contents of a file with no trailing
426
            left_matching_blocks=[(0, 1, 1)])
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
427
        self.assertEqualDiff(b'newline\nline', vf.get_text(b'noeol2'))
3460.2.1 by Robert Collins
* Inserting a bundle which changes the contents of a file with no trailing
428
        # On top of a delta
429
        vf = self.get_file('delta')
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
430
        vf.add_lines(b'base', [], [b'line'])
431
        vf.add_lines(b'noeol', [b'base'], [b'prelude\n', b'line'])
432
        vf.add_lines(b'noeol2', [b'noeol'], [b'newline\n', b'line'],
3460.2.1 by Robert Collins
* Inserting a bundle which changes the contents of a file with no trailing
433
            left_matching_blocks=[(1, 1, 1)])
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
434
        self.assertEqualDiff(b'newline\nline', vf.get_text(b'noeol2'))
3460.2.1 by Robert Collins
* Inserting a bundle which changes the contents of a file with no trailing
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:
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
451
            vf.add_lines_with_ghosts(b'text', [b'ghost'], [b'line\n'])
3453.3.4 by John Arbash Meinel
Skip the new test for old weave formats that don't support ghosts
452
        except NotImplementedError:
453
            # old Weave formats do not allow ghosts
454
            return
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
455
        self.assertRaises(errors.RevisionNotPresent, vf.make_mpdiffs, [b'ghost'])
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
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
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
462
        f.add_lines(b'base', [], [b'line\n'])
463
        f.add_lines(b'noeol', [b'base'], [b'line'])
1596.2.38 by Robert Collins
rollback from using deltas to using fulltexts - deltas need more work to be ready.
464
        # detailed eol tests:
465
        # shared last line with parent no-eol
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
466
        f.add_lines(b'noeolsecond', [b'noeol'], [b'line\n', b'line'])
1596.2.38 by Robert Collins
rollback from using deltas to using fulltexts - deltas need more work to be ready.
467
        # differing last line with parent, both no-eol
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
468
        f.add_lines(b'noeolnotshared', [b'noeolsecond'], [b'line\n', b'phone'])
1596.2.38 by Robert Collins
rollback from using deltas to using fulltexts - deltas need more work to be ready.
469
        # add eol following a noneol parent, change content
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
470
        f.add_lines(b'eol', [b'noeol'], [b'phone\n'])
1596.2.38 by Robert Collins
rollback from using deltas to using fulltexts - deltas need more work to be ready.
471
        # add eol following a noneol parent, no change content
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
472
        f.add_lines(b'eolline', [b'noeol'], [b'line\n'])
1596.2.38 by Robert Collins
rollback from using deltas to using fulltexts - deltas need more work to be ready.
473
        # noeol with no parents:
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
474
        f.add_lines(b'noeolbase', [], [b'line'])
1596.2.38 by Robert Collins
rollback from using deltas to using fulltexts - deltas need more work to be ready.
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.
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
479
        f.add_lines(b'eolbeforefirstparent', [b'noeolbase', b'noeol'], [b'line'])
1596.2.38 by Robert Collins
rollback from using deltas to using fulltexts - deltas need more work to be ready.
480
        # two identical eol texts
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
481
        f.add_lines(b'noeoldup', [b'noeol'], [b'line'])
482
        next_parent = b'base'
483
        text_name = b'chain1-'
484
        text = [b'line\n']
485
        sha1s = {0: b'da6d3141cb4a5e6f464bf6e0518042ddc7bfd079',
486
                 1: b'45e21ea146a81ea44a821737acdb4f9791c8abe7',
487
                 2: b'e1f11570edf3e2a070052366c582837a4fe4e9fa',
488
                 3: b'26b4b8626da827088c514b8f9bbe4ebf181edda1',
489
                 4: b'e28a5510be25ba84d31121cff00956f9970ae6f6',
490
                 5: b'd63ec0ce22e11dcf65a931b69255d3ac747a318d',
491
                 6: b'2c2888d288cb5e1d98009d822fedfe6019c6a4ea',
492
                 7: b'95c14da9cafbf828e3e74a6f016d87926ba234ab',
493
                 8: b'779e9a0b28f9f832528d4b21e17e168c67697272',
494
                 9: b'1f8ff4e5c6ff78ac106fcfe6b1e8cb8740ff9a8f',
495
                 10: b'131a2ae712cf51ed62f143e3fbac3d4206c25a05',
496
                 11: b'c5a9d6f520d2515e1ec401a8f8a67e6c3c89f199',
497
                 12: b'31a2286267f24d8bedaa43355f8ad7129509ea85',
498
                 13: b'dc2a7fe80e8ec5cae920973973a8ee28b2da5e0a',
499
                 14: b'2c4b1736566b8ca6051e668de68650686a3922f2',
500
                 15: b'5912e4ecd9b0c07be4d013e7e2bdcf9323276cde',
501
                 16: b'b0d2e18d3559a00580f6b49804c23fea500feab3',
502
                 17: b'8e1d43ad72f7562d7cb8f57ee584e20eb1a69fc7',
503
                 18: b'5cf64a3459ae28efa60239e44b20312d25b253f3',
504
                 19: b'1ebed371807ba5935958ad0884595126e8c4e823',
505
                 20: b'2aa62a8b06fb3b3b892a3292a068ade69d5ee0d3',
506
                 21: b'01edc447978004f6e4e962b417a4ae1955b6fe5d',
507
                 22: b'd8d8dc49c4bf0bab401e0298bb5ad827768618bb',
508
                 23: b'c21f62b1c482862983a8ffb2b0c64b3451876e3f',
509
                 24: b'c0593fe795e00dff6b3c0fe857a074364d5f04fc',
6986.1.1 by Jelmer Vernooij
Address review comments.
510
                 25: b'dd1a1cf2ba9cc225c3aff729953e6364bf1d1855',
1596.2.36 by Robert Collins
add a get_delta api to versioned_file.
511
                 }
512
        for depth in range(26):
6963.2.12 by Jelmer Vernooij
Fix some more tests.
513
            new_version = text_name + b'%d' % depth
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
514
            text = text + [b'line\n']
1596.2.36 by Robert Collins
add a get_delta api to versioned_file.
515
            f.add_lines(new_version, [next_parent], text)
516
            next_parent = new_version
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
517
        next_parent = b'base'
518
        text_name = b'chain2-'
519
        text = [b'line\n']
1596.2.36 by Robert Collins
add a get_delta api to versioned_file.
520
        for depth in range(26):
6963.2.12 by Jelmer Vernooij
Fix some more tests.
521
            new_version = text_name + b'%d' % depth
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
522
            text = text + [b'line\n']
1596.2.36 by Robert Collins
add a get_delta api to versioned_file.
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([]))
6963.2.4 by Jelmer Vernooij
Add bees.
530
        f.add_lines(b'r0', [], [b'a\n', b'b\n'])
531
        f.add_lines(b'r1', [b'r0'], [b'b\n', b'c\n'])
532
        f.add_lines(b'r2', [b'r0'], [b'b\n', b'c\n'])
533
        f.add_lines(b'r3', [b'r2'], [b'b\n', b'c\n'])
534
        f.add_lines(b'rM', [b'r1', b'r2'], [b'b\n', b'c\n'])
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
535
        self.assertEqual([], f.get_ancestry([]))
6963.2.4 by Jelmer Vernooij
Add bees.
536
        versions = f.get_ancestry([b'rM'])
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
537
        # there are some possibilities:
538
        # r0 r1 r2 rM r3
539
        # r0 r1 r2 r3 rM
540
        # etc
541
        # so we check indexes
6963.2.4 by Jelmer Vernooij
Add bees.
542
        r0 = versions.index(b'r0')
543
        r1 = versions.index(b'r1')
544
        r2 = versions.index(b'r2')
545
        self.assertFalse(b'r3' in versions)
546
        rM = versions.index(b'rM')
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
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,
6963.2.4 by Jelmer Vernooij
Add bees.
553
            f.get_ancestry, [b'rM', b'rX'])
1594.2.21 by Robert Collins
Teach versioned files to prevent mutation after finishing.
554
6963.2.4 by Jelmer Vernooij
Add bees.
555
        self.assertEqual(set(f.get_ancestry(b'rM')),
556
            set(f.get_ancestry(b'rM', topo_sorted=False)))
2530.1.1 by Aaron Bentley
Make topological sorting optional for get_ancestry
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'
6963.2.4 by Jelmer Vernooij
Add bees.
562
        self.assertRaises(errors.OutSideTransaction, f.add_lines, b'', [], [])
563
        self.assertRaises(errors.OutSideTransaction, f.add_lines_with_ghosts, b'', [], [])
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()
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
567
        f.add_lines(b'0', [], [b'a\n'])
1563.2.15 by Robert Collins
remove the weavestore assumptions about the number and nature of files it manages.
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()
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
580
        f.add_lines(b'r0', [], [b'a\n', b'b\n'])
581
        self.assertEqual(
582
            {b'r0':()}, f.get_parent_map([b'r0']))
583
        f.add_lines(b'r1', [b'r0'], [b'a\n', b'b\n'])
584
        self.assertEqual(
585
            {b'r1':(b'r0',)}, f.get_parent_map([b'r1']))
586
        self.assertEqual(
587
            {b'r0':(),
588
             b'r1':(b'r0',)},
589
            f.get_parent_map([b'r0', b'r1']))
590
        f.add_lines(b'r2', [], [b'a\n', b'b\n'])
591
        f.add_lines(b'r3', [], [b'a\n', b'b\n'])
592
        f.add_lines(b'm', [b'r0', b'r1', b'r2', b'r3'], [b'a\n', b'b\n'])
593
        self.assertEqual(
594
            {b'm':(b'r0', b'r1', b'r2', b'r3')}, f.get_parent_map([b'm']))
595
        self.assertEqual({}, f.get_parent_map(b'y'))
596
        self.assertEqual(
597
            {b'r0':(),
598
             b'r1':(b'r0',)},
599
            f.get_parent_map([b'r0', b'y', b'r1']))
3287.5.1 by Robert Collins
Add VersionedFile.get_parent_map.
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()
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
603
        f.add_lines(b'r0', [], [b'a\n', b'b\n'])
604
        f.add_lines(b'r1', [b'r0'], [b'c\n', b'b\n'])
605
        origins = f.annotate(b'r1')
606
        self.assertEqual(origins[0][0], b'r1')
607
        self.assertEqual(origins[1][0], b'r0')
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
608
609
        self.assertRaises(RevisionNotPresent,
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
610
            f.annotate, b'foo')
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
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
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
621
        self.assertEqual(b'hello\n', w.get_text(b'v1'))
622
        self.assertRaises(WeaveInvalidChecksum, w.get_text, b'v2')
623
        self.assertRaises(WeaveInvalidChecksum, w.get_lines, b'v2')
6731 by Jelmer Vernooij
Move weave errors to breezy.bzr.weave.
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
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
628
        self.assertEqual(b'hello\n', w.get_text(b'v1'))
629
        self.assertRaises(WeaveInvalidChecksum, w.get_text, b'v2')
630
        self.assertRaises(WeaveInvalidChecksum, w.get_lines, b'v2')
6731 by Jelmer Vernooij
Move weave errors to breezy.bzr.weave.
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
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
658
        vf.add_lines(b'base', [], [b'base\n'])
1594.2.6 by Robert Collins
Introduce a api specifically for looking at lines in some versions of the inventory, for fileid_involved.
659
        # add a ancestor to be included on one side
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
660
        vf.add_lines(b'lancestor', [], [b'lancestor\n'])
1594.2.6 by Robert Collins
Introduce a api specifically for looking at lines in some versions of the inventory, for fileid_involved.
661
        # add a ancestor to be included on the other side
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
662
        vf.add_lines(b'rancestor', [b'base'], [b'rancestor\n'])
1594.2.6 by Robert Collins
Introduce a api specifically for looking at lines in some versions of the inventory, for fileid_involved.
663
        # add a child of rancestor with no eofile-nl
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
664
        vf.add_lines(b'child', [b'rancestor'], [b'base\n', b'child\n'])
1594.2.6 by Robert Collins
Introduce a api specifically for looking at lines in some versions of the inventory, for fileid_involved.
665
        # add a child of lancestor and base to join the two roots
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
666
        vf.add_lines(b'otherchild',
667
                     [b'lancestor', b'base'],
668
                     [b'base\n', b'lancestor\n', b'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
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
681
        lines = iter_with_versions([b'child', b'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
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
686
        self.assertTrue(lines[(b'child\n', b'child')] > 0)
687
        self.assertTrue(lines[(b'otherchild\n', b'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
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
698
        self.assertTrue(lines[(b'base\n', b'base')] > 0)
699
        self.assertTrue(lines[(b'lancestor\n', b'lancestor')] > 0)
700
        self.assertTrue(lines[(b'rancestor\n', b'rancestor')] > 0)
701
        self.assertTrue(lines[(b'child\n', b'child')] > 0)
702
        self.assertTrue(lines[(b'otherchild\n', b'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:
6963.2.4 by Jelmer Vernooij
Add bees.
715
            vf.add_lines_with_ghosts(b'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
6963.2.4 by Jelmer Vernooij
Add bees.
718
            self.assertRaises(NotImplementedError, vf.get_ancestry_with_ghosts, [b'foo'])
719
            self.assertRaises(NotImplementedError, vf.get_parents_with_ghosts, b'foo')
1594.2.8 by Robert Collins
add ghost aware apis to knits.
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
6963.2.4 by Jelmer Vernooij
Add bees.
725
        self.assertEqual([b'notbxbfse'], vf.get_ancestry(b'notbxbfse'))
2249.5.12 by John Arbash Meinel
Change the APIs for VersionedFile, Store, and some of Repository into utf-8
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.
6963.2.4 by Jelmer Vernooij
Add bees.
728
        self.assertEqual([parent_id_utf8, b'notbxbfse'], vf.get_ancestry_with_ghosts([b'notbxbfse']))
729
        self.assertEqual([parent_id_utf8], vf.get_parents_with_ghosts(b'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, [], [])
6963.2.4 by Jelmer Vernooij
Add bees.
733
        self.assertEqual([parent_id_utf8, b'notbxbfse'], vf.get_ancestry([b'notbxbfse']))
734
        self.assertEqual({b'notbxbfse':(parent_id_utf8,)},
735
            vf.get_parent_map([b'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.
6963.2.4 by Jelmer Vernooij
Add bees.
738
        self.assertEqual([parent_id_utf8, b'notbxbfse'],
739
            vf.get_ancestry_with_ghosts([b'notbxbfse']))
740
        self.assertEqual([parent_id_utf8], vf.get_parents_with_ghosts(b'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:
6963.2.4 by Jelmer Vernooij
Add bees.
750
            vf.add_lines_with_ghosts(b'base', [], [b'line\n', b'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
6963.2.4 by Jelmer Vernooij
Add bees.
753
        vf.add_lines_with_ghosts(b'references_ghost',
754
                                 [b'base', b'a_ghost'],
755
                                 [b'line\n', b'line_b\n', b'line_c\n'])
756
        origins = vf.annotate(b'references_ghost')
757
        self.assertEqual((b'base', b'line\n'), origins[0])
758
        self.assertEqual((b'base', b'line_b\n'), origins[1])
759
        self.assertEqual((b'references_ghost', b'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')
6963.2.4 by Jelmer Vernooij
Add bees.
766
        self.assertRaises(errors.ReadOnlyError, vf.add_lines, b'base', [], [])
1594.2.23 by Robert Collins
Test versioned file storage handling of clean/dirty status for accessed versioned files.
767
        self.assertRaises(errors.ReadOnlyError,
768
                          vf.add_lines_with_ghosts,
6963.2.4 by Jelmer Vernooij
Add bees.
769
                          b'base',
1594.2.23 by Robert Collins
Test versioned file storage handling of clean/dirty status for accessed versioned files.
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
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
777
        vf.add_lines(b'a', [], [b'a\n'])
1666.1.6 by Robert Collins
Make knit the default format.
778
        # the same file, different metadata
6973.14.4 by Jelmer Vernooij
Fix checking for options.
779
        vf.add_lines(b'b', [b'a'], [b'a\n'])
1666.1.6 by Robert Collins
Make knit the default format.
780
        # a file differing only in last newline.
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
781
        vf.add_lines(b'c', [], [b'a'])
3350.8.3 by Robert Collins
VF.get_sha1s needed changing to be stackable.
782
        self.assertEqual({
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
783
            b'a': b'3f786850e387550fdab836ed7e6dc881de23001b',
784
            b'c': b'86f7e437faa5a7fce15d1ddcb9eaeaea377667b8',
785
            b'b': b'3f786850e387550fdab836ed7e6dc881de23001b',
3350.8.3 by Robert Collins
VF.get_sha1s needed changing to be stackable.
786
            },
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
787
            vf.get_sha1s([b'a', b'c', b'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)
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
801
        w.add_lines(b'v1', [], [b'hello\n'])
802
        w.add_lines(b'v2', [b'v1'], [b'hello\n', b'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
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
806
        self.assertEqual([(b'{', 0)
807
                         , b'hello\n'
808
                        , (b'}', None)
809
                        , (b'{', 1)
810
                        , b'there\n'
811
                        , (b'}', None)
1563.2.6 by Robert Collins
Start check tests for knits (pending), and remove dead code.
812
                        ], w._weave)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
813
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
814
        self.assertEqual([b'f572d396fae9206628714fb2ce00f72e94f2258f'
815
                        , b'90f265c6e75f1c8f9ab76dcf85528352c5f215ef'
1563.2.6 by Robert Collins
Start check tests for knits (pending), and remove dead code.
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
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
820
        w._weave[4] = b'There\n'
1563.2.6 by Robert Collins
Start check tests for knits (pending), and remove dead code.
821
        return w
822
823
    def get_file_corrupted_checksum(self):
824
        w = self.get_file_corrupted_text()
825
        # Corrected
6963.2.4 by Jelmer Vernooij
Add bees.
826
        w._weave[4] = b'there\n'
827
        self.assertEqual(b'hello\nthere\n', w.get_text(b'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
6963.2.4 by Jelmer Vernooij
Add bees.
830
        w._sha1s[1] =b'f0f265c6e75f1c8f9ab76dcf85528352c5f215ef'
1563.2.6 by Robert Collins
Start check tests for knits (pending), and remove dead code.
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):
6963.2.4 by Jelmer Vernooij
Add bees.
861
        self.plan_merge_vf.add_lines((b'root', b'a:'), [], [])
862
        self.assertRaises(ValueError, self.plan_merge_vf.add_lines,
863
            (b'root', b'a'), [], [])
864
        self.assertRaises(ValueError, self.plan_merge_vf.add_lines,
865
            (b'root', b'a:'), None, [])
866
        self.assertRaises(ValueError, self.plan_merge_vf.add_lines,
867
            (b'root', b'a:'), [], None)
3062.1.9 by Aaron Bentley
Move PlanMerge into merge and _PlanMergeVersionedFile into versionedfile
868
869
    def setup_abcde(self):
6963.2.4 by Jelmer Vernooij
Add bees.
870
        self.vf1.add_lines((b'root', b'A'), [], [b'a'])
871
        self.vf1.add_lines((b'root', b'B'), [(b'root', b'A')], [b'b'])
872
        self.vf2.add_lines((b'root', b'C'), [], [b'c'])
873
        self.vf2.add_lines((b'root', b'D'), [(b'root', b'C')], [b'd'])
874
        self.plan_merge_vf.add_lines((b'root', b'E:'),
6973.14.4 by Jelmer Vernooij
Fix checking for options.
875
            [(b'root', b'B'), (b'root', b'D')], [b'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()
6963.2.4 by Jelmer Vernooij
Add bees.
879
        self.assertEqual({(b'root', b'B'): ((b'root', b'A'),)},
880
            self.plan_merge_vf.get_parent_map([(b'root', b'B')]))
881
        self.assertEqual({(b'root', b'D'): ((b'root', b'C'),)},
6973.14.4 by Jelmer Vernooij
Fix checking for options.
882
            self.plan_merge_vf.get_parent_map([(b'root', b'D')]))
6963.2.4 by Jelmer Vernooij
Add bees.
883
        self.assertEqual({(b'root', b'E:'): ((b'root', b'B'), (b'root', b'D'))},
884
            self.plan_merge_vf.get_parent_map([(b'root', b'E:')]))
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.
885
        self.assertEqual({},
6963.2.4 by Jelmer Vernooij
Add bees.
886
            self.plan_merge_vf.get_parent_map([(b'root', b'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({
6963.2.4 by Jelmer Vernooij
Add bees.
888
                (b'root', b'B'): ((b'root', b'A'),),
889
                (b'root', b'D'): ((b'root', b'C'),),
890
                (b'root', b'E:'): ((b'root', b'B'), (b'root', b'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(
6963.2.4 by Jelmer Vernooij
Add bees.
893
                [(b'root', b'B'), (b'root', b'D'), (b'root', b'E:'), (b'root', b'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(
6963.2.4 by Jelmer Vernooij
Add bees.
899
                [(b'root', suffix)], 'unordered', True))
6963.2.13 by Jelmer Vernooij
Fix more tests.
900
        self.assertEqual(b'a', get_record(b'A').get_bytes_as('fulltext'))
901
        self.assertEqual(b'c', get_record(b'C').get_bytes_as('fulltext'))
902
        self.assertEqual(b'e', get_record(b'E:').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.
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.
6963.2.4 by Jelmer Vernooij
Add bees.
923
        vf.add_lines(b'1', [], [b'a\n'])
924
        vf.add_lines(b'2', [b'1'], [b'b\n', b'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('.')))
6963.2.4 by Jelmer Vernooij
Add bees.
927
        self.assertEqual([b'1', b'2'], vf.versions())
928
        self.assertEqual([b'1', b'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):
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
950
            return x + b'\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()
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
953
        w.add_lines(b'text0', [], list(map(addcrlf, base)))
954
        w.add_lines(b'text1', [b'text0'], list(map(addcrlf, a)))
955
        w.add_lines(b'text2', [b'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:')
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
960
        p = list(w.plan_merge(b'text1', b'text2'))
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
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
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
974
    def testOneInsert(self):
975
        self.doMerge([],
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
976
                     [b'aa'],
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
977
                     [],
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
978
                     [b'aa'])
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
979
980
    def testSeparateInserts(self):
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
981
        self.doMerge([b'aaa', b'bbb', b'ccc'],
982
                     [b'aaa', b'xxx', b'bbb', b'ccc'],
983
                     [b'aaa', b'bbb', b'yyy', b'ccc'],
984
                     [b'aaa', b'xxx', b'bbb', b'yyy', b'ccc'])
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
985
986
    def testSameInsert(self):
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
987
        self.doMerge([b'aaa', b'bbb', b'ccc'],
988
                     [b'aaa', b'xxx', b'bbb', b'ccc'],
989
                     [b'aaa', b'xxx', b'bbb', b'yyy', b'ccc'],
990
                     [b'aaa', b'xxx', b'bbb', b'yyy', b'ccc'])
991
    overlappedInsertExpected = [b'aaa', b'xxx', b'yyy', b'bbb']
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
992
    def testOverlappedInsert(self):
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
993
        self.doMerge([b'aaa', b'bbb'],
994
                     [b'aaa', b'xxx', b'yyy', b'bbb'],
995
                     [b'aaa', b'xxx', b'bbb'], self.overlappedInsertExpected)
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
996
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
997
        # really it ought to reduce this to
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
998
        # [b'aaa', b'xxx', b'yyy', b'bbb']
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
999
1000
1001
    def testClashReplace(self):
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
1002
        self.doMerge([b'aaa'],
1003
                     [b'xxx'],
1004
                     [b'yyy', b'zzz'],
1005
                     [b'<<<<<<< ', b'xxx', b'=======', b'yyy', b'zzz',
1006
                      b'>>>>>>> '])
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1007
1008
    def testNonClashInsert1(self):
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
1009
        self.doMerge([b'aaa'],
1010
                     [b'xxx', b'aaa'],
1011
                     [b'yyy', b'zzz'],
1012
                     [b'<<<<<<< ', b'xxx', b'aaa', b'=======', b'yyy', b'zzz',
1013
                      b'>>>>>>> '])
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1014
1015
    def testNonClashInsert2(self):
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
1016
        self.doMerge([b'aaa'],
1017
                     [b'aaa'],
1018
                     [b'yyy', b'zzz'],
1019
                     [b'yyy', b'zzz'])
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1020
1021
    def testDeleteAndModify(self):
1022
        """Clashing delete and modification.
1023
1024
        If one side modifies a region and the other deletes it then
1025
        there should be a conflict with one side blank.
1026
        """
1027
1028
        #######################################
1029
        # skippd, not working yet
1030
        return
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1031
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
1032
        self.doMerge([b'aaa', b'bbb', b'ccc'],
1033
                     [b'aaa', b'ddd', b'ccc'],
1034
                     [b'aaa', b'ccc'],
1035
                     [b'<<<<<<<< ', b'aaa', b'=======', b'>>>>>>> ', b'ccc'])
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1036
1037
    def _test_merge_from_strings(self, base, a, b, expected):
1038
        w = self.get_file()
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
1039
        w.add_lines(b'text0', [], base.splitlines(True))
1040
        w.add_lines(b'text1', [b'text0'], a.splitlines(True))
1041
        w.add_lines(b'text2', [b'text0'], b.splitlines(True))
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1042
        self.log('merge plan:')
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
1043
        p = list(w.plan_merge(b'text1', b'text2'))
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1044
        for state, line in p:
1045
            if line:
1046
                self.log('%12s | %s' % (state, line[:-1]))
1047
        self.log('merge result:')
6963.2.10 by Jelmer Vernooij
Fix a bunch of tests.
1048
        result_text = b''.join(w.weave_merge(p))
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1049
        self.log(result_text)
1050
        self.assertEqualDiff(result_text, expected)
1051
1052
    def test_weave_merge_conflicts(self):
1053
        # does weave merge properly handle plans that end with unchanged?
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
1054
        result = b''.join(self.get_file().weave_merge([('new-a', b'hello\n')]))
1055
        self.assertEqual(result, b'hello\n')
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1056
1057
    def test_deletion_extended(self):
1058
        """One side deletes, the other deletes more.
1059
        """
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
1060
        base = b"""\
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1061
            line 1
1062
            line 2
1063
            line 3
1064
            """
6963.2.4 by Jelmer Vernooij
Add bees.
1065
        a = b"""\
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1066
            line 1
1067
            line 2
1068
            """
6963.2.4 by Jelmer Vernooij
Add bees.
1069
        b = b"""\
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1070
            line 1
1071
            """
6963.2.4 by Jelmer Vernooij
Add bees.
1072
        result = b"""\
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1073
            line 1
4312.1.3 by John Arbash Meinel
Different sides deleting different amounts is now a conflict.
1074
<<<<<<<\x20
1075
            line 2
1076
=======
1077
>>>>>>>\x20
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1078
            """
1079
        self._test_merge_from_strings(base, a, b, result)
1080
1081
    def test_deletion_overlap(self):
1082
        """Delete overlapping regions with no other conflict.
1083
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1084
        Arguably it'd be better to treat these as agreement, rather than
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1085
        conflict, but for now conflict is safer.
1086
        """
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
1087
        base = b"""\
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1088
            start context
1089
            int a() {}
1090
            int b() {}
1091
            int c() {}
1092
            end context
1093
            """
6963.2.4 by Jelmer Vernooij
Add bees.
1094
        a = b"""\
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1095
            start context
1096
            int a() {}
1097
            end context
1098
            """
6963.2.4 by Jelmer Vernooij
Add bees.
1099
        b = b"""\
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1100
            start context
1101
            int c() {}
1102
            end context
1103
            """
6963.2.4 by Jelmer Vernooij
Add bees.
1104
        result = b"""\
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1105
            start context
3943.8.2 by Marius Kruger
fix tests relying on trailing whitespace by replacing it with \x20.
1106
<<<<<<<\x20
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1107
            int a() {}
1108
=======
1109
            int c() {}
3943.8.2 by Marius Kruger
fix tests relying on trailing whitespace by replacing it with \x20.
1110
>>>>>>>\x20
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1111
            end context
1112
            """
1113
        self._test_merge_from_strings(base, a, b, result)
1114
1115
    def test_agreement_deletion(self):
1116
        """Agree to delete some lines, without conflicts."""
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
1117
        base = b"""\
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1118
            start context
1119
            base line 1
1120
            base line 2
1121
            end context
1122
            """
6963.2.4 by Jelmer Vernooij
Add bees.
1123
        a = b"""\
1124
            start context
1125
            base line 1
1126
            end context
1127
            """
1128
        b = b"""\
1129
            start context
1130
            base line 1
1131
            end context
1132
            """
1133
        result = b"""\
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1134
            start context
1135
            base line 1
1136
            end context
1137
            """
1138
        self._test_merge_from_strings(base, a, b, result)
1139
1140
    def test_sync_on_deletion(self):
1141
        """Specific case of merge where we can synchronize incorrectly.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1142
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1143
        A previous version of the weave merge concluded that the two versions
1144
        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
1145
        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
1146
        both sides.
1147
1148
        It's better to consider the whole thing as a disagreement region.
1149
        """
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
1150
        base = b"""\
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1151
            start context
1152
            base line 1
1153
            base line 2
1154
            end context
1155
            """
6963.2.4 by Jelmer Vernooij
Add bees.
1156
        a = b"""\
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1157
            start context
1158
            base line 1
1159
            a's replacement line 2
1160
            end context
1161
            """
6963.2.4 by Jelmer Vernooij
Add bees.
1162
        b = b"""\
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1163
            start context
1164
            b replaces
1165
            both lines
1166
            end context
1167
            """
6963.2.4 by Jelmer Vernooij
Add bees.
1168
        result = b"""\
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1169
            start context
3943.8.2 by Marius Kruger
fix tests relying on trailing whitespace by replacing it with \x20.
1170
<<<<<<<\x20
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1171
            base line 1
1172
            a's replacement line 2
1173
=======
1174
            b replaces
1175
            both lines
3943.8.2 by Marius Kruger
fix tests relying on trailing whitespace by replacing it with \x20.
1176
>>>>>>>\x20
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1177
            end context
1178
            """
1179
        self._test_merge_from_strings(base, a, b, result)
1180
1181
2535.3.1 by Andrew Bennetts
Add get_format_signature to VersionedFile
1182
class TestWeaveMerge(TestCaseWithMemoryTransport, MergeCasesMixin):
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1183
1184
    def get_file(self, name='foo'):
5609.9.4 by Vincent Ladeuil
Use self.get_transport instead of transport.get_transport where possible.
1185
        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
1186
                         create=True)
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1187
1188
    def log_contents(self, w):
1189
        self.log('weave is:')
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
1190
        tmpf = BytesIO()
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
1191
        write_weave(w, tmpf)
1192
        self.log(tmpf.getvalue())
1193
6963.2.4 by Jelmer Vernooij
Add bees.
1194
    overlappedInsertExpected = [b'aaa', b'<<<<<<< ', b'xxx', b'yyy', b'=======',
1195
                                b'xxx', b'>>>>>>> ', b'bbb']
3350.3.4 by Robert Collins
Finish adapters for annotated knits to unannotated knits and full texts.
1196
1197
1198
class TestContentFactoryAdaption(TestCaseWithMemoryTransport):
1199
1200
    def test_select_adaptor(self):
3350.3.7 by Robert Collins
Create a registry of versioned file record adapters.
1201
        """Test expected adapters exist."""
1202
        # One scenario for each lookup combination we expect to use.
1203
        # Each is source_kind, requested_kind, adapter class
1204
        scenarios = [
1205
            ('knit-delta-gz', 'fulltext', _mod_knit.DeltaPlainToFullText),
1206
            ('knit-ft-gz', 'fulltext', _mod_knit.FTPlainToFullText),
1207
            ('knit-annotated-delta-gz', 'knit-delta-gz',
1208
                _mod_knit.DeltaAnnotatedToUnannotated),
1209
            ('knit-annotated-delta-gz', 'fulltext',
1210
                _mod_knit.DeltaAnnotatedToFullText),
1211
            ('knit-annotated-ft-gz', 'knit-ft-gz',
1212
                _mod_knit.FTAnnotatedToUnannotated),
1213
            ('knit-annotated-ft-gz', 'fulltext',
1214
                _mod_knit.FTAnnotatedToFullText),
1215
            ]
1216
        for source, requested, klass in scenarios:
1217
            adapter_factory = versionedfile.adapter_registry.get(
1218
                (source, requested))
1219
            adapter = adapter_factory(None)
1220
            self.assertIsInstance(adapter, klass)
3350.3.4 by Robert Collins
Finish adapters for annotated knits to unannotated knits and full texts.
1221
3350.3.5 by Robert Collins
Create adapters from plain compressed knit content.
1222
    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.
1223
        mapper = ConstantMapper('knit')
1224
        transport = self.get_transport()
1225
        return make_file_factory(annotated, mapper)(transport)
3350.3.4 by Robert Collins
Finish adapters for annotated knits to unannotated knits and full texts.
1226
1227
    def helpGetBytes(self, f, ft_adapter, delta_adapter):
3350.3.22 by Robert Collins
Review feedback.
1228
        """Grab the interested adapted texts for tests."""
3350.3.4 by Robert Collins
Finish adapters for annotated knits to unannotated knits and full texts.
1229
        # origin is a fulltext
6963.2.4 by Jelmer Vernooij
Add bees.
1230
        entries = f.get_record_stream([(b'origin',)], 'unordered', False)
6634.2.1 by Martin
Apply 2to3 next fixer and make compatible
1231
        base = next(entries)
4005.3.1 by Robert Collins
Change the signature on VersionedFiles adapters to allow less typing and more flexability inside adapters.
1232
        ft_data = ft_adapter.get_bytes(base)
3350.3.4 by Robert Collins
Finish adapters for annotated knits to unannotated knits and full texts.
1233
        # merged is both a delta and multiple parents.
6963.2.4 by Jelmer Vernooij
Add bees.
1234
        entries = f.get_record_stream([(b'merged',)], 'unordered', False)
6634.2.1 by Martin
Apply 2to3 next fixer and make compatible
1235
        merged = next(entries)
4005.3.1 by Robert Collins
Change the signature on VersionedFiles adapters to allow less typing and more flexability inside adapters.
1236
        delta_data = delta_adapter.get_bytes(merged)
3350.3.4 by Robert Collins
Finish adapters for annotated knits to unannotated knits and full texts.
1237
        return ft_data, delta_data
1238
1239
    def test_deannotation_noeol(self):
1240
        """Test converting annotated knits to unannotated knits."""
1241
        # 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.
1242
        f = self.get_knit()
1243
        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.
1244
        ft_data, delta_data = self.helpGetBytes(f,
3350.3.7 by Robert Collins
Create a registry of versioned file record adapters.
1245
            _mod_knit.FTAnnotatedToUnannotated(None),
1246
            _mod_knit.DeltaAnnotatedToUnannotated(None))
3350.3.4 by Robert Collins
Finish adapters for annotated knits to unannotated knits and full texts.
1247
        self.assertEqual(
6963.2.4 by Jelmer Vernooij
Add bees.
1248
            b'version origin 1 b284f94827db1fa2970d9e2014f080413b547a7e\n'
1249
            b'origin\n'
1250
            b'end origin\n',
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
1251
            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.
1252
        self.assertEqual(
6963.2.4 by Jelmer Vernooij
Add bees.
1253
            b'version merged 4 32c2e79763b3f90e8ccde37f9710b6629c25a796\n'
1254
            b'1,2,3\nleft\nright\nmerged\nend merged\n',
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
1255
            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.
1256
1257
    def test_deannotation(self):
1258
        """Test converting annotated knits to unannotated knits."""
1259
        # 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.
1260
        f = self.get_knit()
1261
        get_diamond_files(f, 1)
3350.3.4 by Robert Collins
Finish adapters for annotated knits to unannotated knits and full texts.
1262
        ft_data, delta_data = self.helpGetBytes(f,
3350.3.7 by Robert Collins
Create a registry of versioned file record adapters.
1263
            _mod_knit.FTAnnotatedToUnannotated(None),
1264
            _mod_knit.DeltaAnnotatedToUnannotated(None))
3350.3.4 by Robert Collins
Finish adapters for annotated knits to unannotated knits and full texts.
1265
        self.assertEqual(
6963.2.4 by Jelmer Vernooij
Add bees.
1266
            b'version origin 1 00e364d235126be43292ab09cb4686cf703ddc17\n'
1267
            b'origin\n'
1268
            b'end origin\n',
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
1269
            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.
1270
        self.assertEqual(
6963.2.4 by Jelmer Vernooij
Add bees.
1271
            b'version merged 3 ed8bce375198ea62444dc71952b22cfc2b09226d\n'
1272
            b'2,2,2\nright\nmerged\nend merged\n',
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
1273
            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.
1274
1275
    def test_annotated_to_fulltext_no_eol(self):
1276
        """Test adapting annotated knits to full texts (for -> weaves)."""
1277
        # 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.
1278
        f = self.get_knit()
1279
        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.
1280
        # Reconstructing a full text requires a backing versioned file, and it
1281
        # 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.
1282
        logged_vf = versionedfile.RecordingVersionedFilesDecorator(f)
3350.3.4 by Robert Collins
Finish adapters for annotated knits to unannotated knits and full texts.
1283
        ft_data, delta_data = self.helpGetBytes(f,
3350.3.7 by Robert Collins
Create a registry of versioned file record adapters.
1284
            _mod_knit.FTAnnotatedToFullText(None),
3350.3.4 by Robert Collins
Finish adapters for annotated knits to unannotated knits and full texts.
1285
            _mod_knit.DeltaAnnotatedToFullText(logged_vf))
6963.2.4 by Jelmer Vernooij
Add bees.
1286
        self.assertEqual(b'origin', ft_data)
1287
        self.assertEqual(b'base\nleft\nright\nmerged', delta_data)
1288
        self.assertEqual([('get_record_stream', [(b'left',)], 'unordered',
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.
1289
            True)], logged_vf.calls)
3350.3.4 by Robert Collins
Finish adapters for annotated knits to unannotated knits and full texts.
1290
1291
    def test_annotated_to_fulltext(self):
1292
        """Test adapting annotated knits to full texts (for -> weaves)."""
1293
        # 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.
1294
        f = self.get_knit()
1295
        get_diamond_files(f, 1)
3350.3.4 by Robert Collins
Finish adapters for annotated knits to unannotated knits and full texts.
1296
        # Reconstructing a full text requires a backing versioned file, and it
1297
        # 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.
1298
        logged_vf = versionedfile.RecordingVersionedFilesDecorator(f)
3350.3.4 by Robert Collins
Finish adapters for annotated knits to unannotated knits and full texts.
1299
        ft_data, delta_data = self.helpGetBytes(f,
3350.3.7 by Robert Collins
Create a registry of versioned file record adapters.
1300
            _mod_knit.FTAnnotatedToFullText(None),
3350.3.4 by Robert Collins
Finish adapters for annotated knits to unannotated knits and full texts.
1301
            _mod_knit.DeltaAnnotatedToFullText(logged_vf))
6963.2.4 by Jelmer Vernooij
Add bees.
1302
        self.assertEqual(b'origin\n', ft_data)
1303
        self.assertEqual(b'base\nleft\nright\nmerged\n', delta_data)
1304
        self.assertEqual([('get_record_stream', [(b'left',)], 'unordered',
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.
1305
            True)], logged_vf.calls)
3350.3.5 by Robert Collins
Create adapters from plain compressed knit content.
1306
1307
    def test_unannotated_to_fulltext(self):
1308
        """Test adapting unannotated knits to full texts.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1309
3350.3.5 by Robert Collins
Create adapters from plain compressed knit content.
1310
        This is used for -> weaves, and for -> annotated knits.
1311
        """
1312
        # 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.
1313
        f = self.get_knit(annotated=False)
1314
        get_diamond_files(f, 1)
3350.3.5 by Robert Collins
Create adapters from plain compressed knit content.
1315
        # Reconstructing a full text requires a backing versioned file, and it
1316
        # 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.
1317
        logged_vf = versionedfile.RecordingVersionedFilesDecorator(f)
3350.3.5 by Robert Collins
Create adapters from plain compressed knit content.
1318
        ft_data, delta_data = self.helpGetBytes(f,
3350.3.7 by Robert Collins
Create a registry of versioned file record adapters.
1319
            _mod_knit.FTPlainToFullText(None),
3350.3.5 by Robert Collins
Create adapters from plain compressed knit content.
1320
            _mod_knit.DeltaPlainToFullText(logged_vf))
6963.2.4 by Jelmer Vernooij
Add bees.
1321
        self.assertEqual(b'origin\n', ft_data)
1322
        self.assertEqual(b'base\nleft\nright\nmerged\n', delta_data)
1323
        self.assertEqual([('get_record_stream', [(b'left',)], 'unordered',
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.
1324
            True)], logged_vf.calls)
3350.3.5 by Robert Collins
Create adapters from plain compressed knit content.
1325
3350.3.6 by Robert Collins
Test EOL behaviour of plain knit record adapters.
1326
    def test_unannotated_to_fulltext_no_eol(self):
1327
        """Test adapting unannotated knits to full texts.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1328
3350.3.6 by Robert Collins
Test EOL behaviour of plain knit record adapters.
1329
        This is used for -> weaves, and for -> annotated knits.
1330
        """
1331
        # 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.
1332
        f = self.get_knit(annotated=False)
1333
        get_diamond_files(f, 1, trailing_eol=False)
3350.3.6 by Robert Collins
Test EOL behaviour of plain knit record adapters.
1334
        # Reconstructing a full text requires a backing versioned file, and it
1335
        # 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.
1336
        logged_vf = versionedfile.RecordingVersionedFilesDecorator(f)
3350.3.6 by Robert Collins
Test EOL behaviour of plain knit record adapters.
1337
        ft_data, delta_data = self.helpGetBytes(f,
3350.3.7 by Robert Collins
Create a registry of versioned file record adapters.
1338
            _mod_knit.FTPlainToFullText(None),
3350.3.6 by Robert Collins
Test EOL behaviour of plain knit record adapters.
1339
            _mod_knit.DeltaPlainToFullText(logged_vf))
6963.2.4 by Jelmer Vernooij
Add bees.
1340
        self.assertEqual(b'origin', ft_data)
1341
        self.assertEqual(b'base\nleft\nright\nmerged', delta_data)
1342
        self.assertEqual([('get_record_stream', [(b'left',)], 'unordered',
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.
1343
            True)], logged_vf.calls)
3350.3.6 by Robert Collins
Test EOL behaviour of plain knit record adapters.
1344
3350.6.1 by Robert Collins
* New ``versionedfile.KeyMapper`` interface to abstract out the access to
1345
1346
class TestKeyMapper(TestCaseWithMemoryTransport):
1347
    """Tests for various key mapping logic."""
1348
1349
    def test_identity_mapper(self):
1350
        mapper = versionedfile.ConstantMapper("inventory")
6963.2.4 by Jelmer Vernooij
Add bees.
1351
        self.assertEqual("inventory", mapper.map((b'foo@ar',)))
1352
        self.assertEqual("inventory", mapper.map((b'quux',)))
3350.6.1 by Robert Collins
* New ``versionedfile.KeyMapper`` interface to abstract out the access to
1353
1354
    def test_prefix_mapper(self):
1355
        #format5: plain
1356
        mapper = versionedfile.PrefixMapper()
6963.2.10 by Jelmer Vernooij
Fix a bunch of tests.
1357
        self.assertEqual("file-id", mapper.map((b"file-id", b"revision-id")))
1358
        self.assertEqual("new-id", mapper.map((b"new-id", b"revision-id")))
1359
        self.assertEqual((b'file-id',), mapper.unmap("file-id"))
1360
        self.assertEqual((b'new-id',), mapper.unmap("new-id"))
3350.6.1 by Robert Collins
* New ``versionedfile.KeyMapper`` interface to abstract out the access to
1361
1362
    def test_hash_prefix_mapper(self):
1363
        #format6: hash + plain
1364
        mapper = versionedfile.HashPrefixMapper()
6963.2.10 by Jelmer Vernooij
Fix a bunch of tests.
1365
        self.assertEqual("9b/file-id", mapper.map((b"file-id", b"revision-id")))
1366
        self.assertEqual("45/new-id", mapper.map((b"new-id", b"revision-id")))
6963.2.12 by Jelmer Vernooij
Fix some more tests.
1367
        self.assertEqual((b'file-id',), mapper.unmap("9b/file-id"))
1368
        self.assertEqual((b'new-id',), mapper.unmap("45/new-id"))
3350.6.1 by Robert Collins
* New ``versionedfile.KeyMapper`` interface to abstract out the access to
1369
1370
    def test_hash_escaped_mapper(self):
1371
        #knit1: hash + escaped
1372
        mapper = versionedfile.HashEscapedPrefixMapper()
6963.2.10 by Jelmer Vernooij
Fix a bunch of tests.
1373
        self.assertEqual("88/%2520", mapper.map((b" ", b"revision-id")))
1374
        self.assertEqual("ed/fil%2545-%2549d", mapper.map((b"filE-Id",
6963.2.4 by Jelmer Vernooij
Add bees.
1375
            b"revision-id")))
6963.2.13 by Jelmer Vernooij
Fix more tests.
1376
        self.assertEqual("88/ne%2557-%2549d", mapper.map((b"neW-Id",
6963.2.4 by Jelmer Vernooij
Add bees.
1377
            b"revision-id")))
6963.2.12 by Jelmer Vernooij
Fix some more tests.
1378
        self.assertEqual((b'filE-Id',), mapper.unmap("ed/fil%2545-%2549d"))
1379
        self.assertEqual((b'neW-Id',), mapper.unmap("88/ne%2557-%2549d"))
3350.6.2 by Robert Collins
Prepare parameterised test environment.
1380
1381
1382
class TestVersionedFiles(TestCaseWithMemoryTransport):
1383
    """Tests for the multiple-file variant of VersionedFile."""
1384
5559.2.2 by Martin Pool
Change to using standard load_tests_apply_scenarios.
1385
    # We want to be sure of behaviour for:
1386
    # weaves prefix layout (weave texts)
1387
    # individually named weaves (weave inventories)
1388
    # annotated knits - prefix|hash|hash-escape layout, we test the third only
1389
    #                   as it is the most complex mapper.
1390
    # individually named knits
1391
    # individual no-graph knits in packs (signatures)
1392
    # individual graph knits in packs (inventories)
1393
    # individual graph nocompression knits in packs (revisions)
1394
    # plain text knits in packs (texts)
1395
    len_one_scenarios = [
1396
        ('weave-named', {
6809.1.1 by Martin
Apply 2to3 ws_comma fixer
1397
            'cleanup': None,
1398
            'factory': make_versioned_files_factory(WeaveFile,
5559.2.2 by Martin Pool
Change to using standard load_tests_apply_scenarios.
1399
                ConstantMapper('inventory')),
6809.1.1 by Martin
Apply 2to3 ws_comma fixer
1400
            'graph': True,
1401
            'key_length': 1,
5559.2.2 by Martin Pool
Change to using standard load_tests_apply_scenarios.
1402
            'support_partial_insertion': False,
1403
            }),
1404
        ('named-knit', {
6809.1.1 by Martin
Apply 2to3 ws_comma fixer
1405
            'cleanup': None,
1406
            'factory': make_file_factory(False, ConstantMapper('revisions')),
1407
            'graph': True,
1408
            'key_length': 1,
5559.2.2 by Martin Pool
Change to using standard load_tests_apply_scenarios.
1409
            'support_partial_insertion': False,
1410
            }),
1411
        ('named-nograph-nodelta-knit-pack', {
6809.1.1 by Martin
Apply 2to3 ws_comma fixer
1412
            'cleanup': cleanup_pack_knit,
1413
            'factory': make_pack_factory(False, False, 1),
1414
            'graph': False,
1415
            'key_length': 1,
5559.2.2 by Martin Pool
Change to using standard load_tests_apply_scenarios.
1416
            'support_partial_insertion': False,
1417
            }),
1418
        ('named-graph-knit-pack', {
6809.1.1 by Martin
Apply 2to3 ws_comma fixer
1419
            'cleanup': cleanup_pack_knit,
1420
            'factory': make_pack_factory(True, True, 1),
1421
            'graph': True,
1422
            'key_length': 1,
5559.2.2 by Martin Pool
Change to using standard load_tests_apply_scenarios.
1423
            'support_partial_insertion': True,
1424
            }),
1425
        ('named-graph-nodelta-knit-pack', {
6809.1.1 by Martin
Apply 2to3 ws_comma fixer
1426
            'cleanup': cleanup_pack_knit,
1427
            'factory': make_pack_factory(True, False, 1),
1428
            'graph': True,
1429
            'key_length': 1,
5559.2.2 by Martin Pool
Change to using standard load_tests_apply_scenarios.
1430
            'support_partial_insertion': False,
1431
            }),
1432
        ('groupcompress-nograph', {
6809.1.1 by Martin
Apply 2to3 ws_comma fixer
1433
            'cleanup': groupcompress.cleanup_pack_group,
1434
            'factory': groupcompress.make_pack_factory(False, False, 1),
5559.2.2 by Martin Pool
Change to using standard load_tests_apply_scenarios.
1435
            'graph': False,
6809.1.1 by Martin
Apply 2to3 ws_comma fixer
1436
            'key_length': 1,
1437
            'support_partial_insertion': False,
5559.2.2 by Martin Pool
Change to using standard load_tests_apply_scenarios.
1438
            }),
1439
        ]
1440
    len_two_scenarios = [
1441
        ('weave-prefix', {
6809.1.1 by Martin
Apply 2to3 ws_comma fixer
1442
            'cleanup': None,
1443
            'factory': make_versioned_files_factory(WeaveFile,
5559.2.2 by Martin Pool
Change to using standard load_tests_apply_scenarios.
1444
                PrefixMapper()),
6809.1.1 by Martin
Apply 2to3 ws_comma fixer
1445
            'graph': True,
1446
            'key_length': 2,
5559.2.2 by Martin Pool
Change to using standard load_tests_apply_scenarios.
1447
            'support_partial_insertion': False,
1448
            }),
1449
        ('annotated-knit-escape', {
6809.1.1 by Martin
Apply 2to3 ws_comma fixer
1450
            'cleanup': None,
1451
            'factory': make_file_factory(True, HashEscapedPrefixMapper()),
1452
            'graph': True,
1453
            'key_length': 2,
5559.2.2 by Martin Pool
Change to using standard load_tests_apply_scenarios.
1454
            'support_partial_insertion': False,
1455
            }),
1456
        ('plain-knit-pack', {
6809.1.1 by Martin
Apply 2to3 ws_comma fixer
1457
            'cleanup': cleanup_pack_knit,
1458
            'factory': make_pack_factory(True, True, 2),
1459
            'graph': True,
1460
            'key_length': 2,
5559.2.2 by Martin Pool
Change to using standard load_tests_apply_scenarios.
1461
            'support_partial_insertion': True,
1462
            }),
1463
        ('groupcompress', {
6809.1.1 by Martin
Apply 2to3 ws_comma fixer
1464
            'cleanup': groupcompress.cleanup_pack_group,
1465
            'factory': groupcompress.make_pack_factory(True, False, 1),
5559.2.2 by Martin Pool
Change to using standard load_tests_apply_scenarios.
1466
            'graph': True,
6809.1.1 by Martin
Apply 2to3 ws_comma fixer
1467
            'key_length': 1,
1468
            'support_partial_insertion': False,
5559.2.2 by Martin Pool
Change to using standard load_tests_apply_scenarios.
1469
            }),
1470
        ]
1471
1472
    scenarios = len_one_scenarios + len_two_scenarios
1473
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.
1474
    def get_versionedfiles(self, relpath='files'):
1475
        transport = self.get_transport(relpath)
1476
        if relpath != '.':
1477
            transport.mkdir('.')
1478
        files = self.factory(transport)
1479
        if self.cleanup is not None:
4985.2.1 by Vincent Ladeuil
Deploy addAttrCleanup on the whole test suite.
1480
            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.
1481
        return files
1482
4398.8.8 by John Arbash Meinel
Respond to Andrew's review comments.
1483
    def get_simple_key(self, suffix):
1484
        """Return a key for the object under test."""
1485
        if self.key_length == 1:
1486
            return (suffix,)
1487
        else:
6963.2.3 by Jelmer Vernooij
Fix some more tests.
1488
            return (b'FileA',) + (suffix,)
4398.8.8 by John Arbash Meinel
Respond to Andrew's review comments.
1489
5816.8.2 by Andrew Bennetts
Add test for without_fallbacks.
1490
    def test_add_fallback_implies_without_fallbacks(self):
1491
        f = self.get_versionedfiles('files')
1492
        if getattr(f, 'add_fallback_versioned_files', None) is None:
1493
            raise TestNotApplicable("%s doesn't support fallbacks"
1494
                                    % (f.__class__.__name__,))
1495
        g = self.get_versionedfiles('fallback')
6963.2.4 by Jelmer Vernooij
Add bees.
1496
        key_a = self.get_simple_key(b'a')
1497
        g.add_lines(key_a, [], [b'\n'])
5816.8.2 by Andrew Bennetts
Add test for without_fallbacks.
1498
        f.add_fallback_versioned_files(g)
1499
        self.assertTrue(key_a in f.get_parent_map([key_a]))
1500
        self.assertFalse(key_a in f.without_fallbacks().get_parent_map([key_a]))
1501
4398.8.1 by John Arbash Meinel
Add a VersionedFile.add_text() api.
1502
    def test_add_lines(self):
1503
        f = self.get_versionedfiles()
6963.2.4 by Jelmer Vernooij
Add bees.
1504
        key0 = self.get_simple_key(b'r0')
1505
        key1 = self.get_simple_key(b'r1')
1506
        key2 = self.get_simple_key(b'r2')
1507
        keyf = self.get_simple_key(b'foo')
1508
        f.add_lines(key0, [], [b'a\n', b'b\n'])
4398.8.1 by John Arbash Meinel
Add a VersionedFile.add_text() api.
1509
        if self.graph:
6963.2.4 by Jelmer Vernooij
Add bees.
1510
            f.add_lines(key1, [key0], [b'b\n', b'c\n'])
4398.8.1 by John Arbash Meinel
Add a VersionedFile.add_text() api.
1511
        else:
6963.2.4 by Jelmer Vernooij
Add bees.
1512
            f.add_lines(key1, [], [b'b\n', b'c\n'])
4398.8.1 by John Arbash Meinel
Add a VersionedFile.add_text() api.
1513
        keys = f.keys()
1514
        self.assertTrue(key0 in keys)
1515
        self.assertTrue(key1 in keys)
1516
        records = []
1517
        for record in f.get_record_stream([key0, key1], 'unordered', True):
1518
            records.append((record.key, record.get_bytes_as('fulltext')))
1519
        records.sort()
6963.2.4 by Jelmer Vernooij
Add bees.
1520
        self.assertEqual([(key0, b'a\nb\n'), (key1, b'b\nc\n')], records)
4398.8.1 by John Arbash Meinel
Add a VersionedFile.add_text() api.
1521
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.
1522
    def test_annotate(self):
1523
        files = self.get_versionedfiles()
1524
        self.get_diamond_files(files)
1525
        if self.key_length == 1:
1526
            prefix = ()
1527
        else:
6963.2.3 by Jelmer Vernooij
Fix some more tests.
1528
            prefix = (b'FileA',)
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.
1529
        # introduced full text
6963.2.3 by Jelmer Vernooij
Fix some more tests.
1530
        origins = files.annotate(prefix + (b'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.
1531
        self.assertEqual([
6963.2.3 by Jelmer Vernooij
Fix some more tests.
1532
            (prefix + (b'origin',), b'origin\n')],
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.
1533
            origins)
1534
        # a delta
6963.2.3 by Jelmer Vernooij
Fix some more tests.
1535
        origins = files.annotate(prefix + (b'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.
1536
        self.assertEqual([
6963.2.3 by Jelmer Vernooij
Fix some more tests.
1537
            (prefix + (b'base',), b'base\n')],
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.
1538
            origins)
1539
        # a merge
6963.2.3 by Jelmer Vernooij
Fix some more tests.
1540
        origins = files.annotate(prefix + (b'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.
1541
        if self.graph:
1542
            self.assertEqual([
6963.2.3 by Jelmer Vernooij
Fix some more tests.
1543
                (prefix + (b'base',), b'base\n'),
1544
                (prefix + (b'left',), b'left\n'),
1545
                (prefix + (b'right',), b'right\n'),
1546
                (prefix + (b'merged',), b'merged\n')
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.
1547
                ],
1548
                origins)
1549
        else:
1550
            # Without a graph everything is new.
1551
            self.assertEqual([
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
1552
                (prefix + (b'merged',), b'base\n'),
1553
                (prefix + (b'merged',), b'left\n'),
1554
                (prefix + (b'merged',), b'right\n'),
1555
                (prefix + (b'merged',), b'merged\n')
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.
1556
                ],
1557
                origins)
1558
        self.assertRaises(RevisionNotPresent,
1559
            files.annotate, prefix + ('missing-key',))
1560
4332.3.26 by Robert Collins
Allow passing keys to check to VersionedFile.check().
1561
    def test_check_no_parameters(self):
1562
        files = self.get_versionedfiles()
1563
1564
    def test_check_progressbar_parameter(self):
1565
        """A progress bar can be supplied because check can be a generator."""
1566
        pb = ui.ui_factory.nested_progress_bar()
1567
        self.addCleanup(pb.finished)
1568
        files = self.get_versionedfiles()
1569
        files.check(progress_bar=pb)
1570
1571
    def test_check_with_keys_becomes_generator(self):
4454.3.65 by John Arbash Meinel
Tests that VF implementations support .get_annotator()
1572
        files = self.get_versionedfiles()
1573
        self.get_diamond_files(files)
4332.3.26 by Robert Collins
Allow passing keys to check to VersionedFile.check().
1574
        keys = files.keys()
1575
        entries = files.check(keys=keys)
1576
        seen = set()
1577
        # Texts output should be fulltexts.
1578
        self.capture_stream(files, entries, seen.add,
1579
            files.get_parent_map(keys), require_fulltext=True)
1580
        # All texts should be output.
1581
        self.assertEqual(set(keys), seen)
4454.3.65 by John Arbash Meinel
Tests that VF implementations support .get_annotator()
1582
4744.2.5 by John Arbash Meinel
Change to a generic 'VersionedFiles.clear_cache()' api.
1583
    def test_clear_cache(self):
1584
        files = self.get_versionedfiles()
1585
        files.clear_cache()
1586
3350.6.2 by Robert Collins
Prepare parameterised test environment.
1587
    def test_construct(self):
1588
        """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.
1589
        files = self.get_versionedfiles()
1590
4241.4.1 by Ian Clatworthy
add sha generation support to versionedfiles
1591
    def get_diamond_files(self, files, trailing_eol=True, left_only=False,
1592
        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.
1593
        return get_diamond_files(files, self.key_length,
1594
            trailing_eol=trailing_eol, nograph=not self.graph,
4241.4.1 by Ian Clatworthy
add sha generation support to versionedfiles
1595
            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.
1596
4398.8.4 by John Arbash Meinel
Implement add_text for GroupCompressVersionedFiles
1597
    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.
1598
        """When nostore_sha is supplied using old content raises."""
1599
        vf = self.get_versionedfiles()
6963.2.4 by Jelmer Vernooij
Add bees.
1600
        empty_text = (b'a', [])
1601
        sample_text_nl = (b'b', [b"foo\n", b"bar\n"])
1602
        sample_text_no_nl = (b'c', [b"foo\n", b"bar"])
4119.1.1 by John Arbash Meinel
Move the 'add_lines_nostoresha' to being tested against all VF implementations.
1603
        shas = []
1604
        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
1605
            if add_lines:
1606
                sha, _, _ = vf.add_lines(self.get_simple_key(version), [],
1607
                                         lines)
1608
            else:
6839 by Jelmer Vernooij
Merge lp:~jelmer/brz/get-full.
1609
                sha, _, _ = vf.add_lines(self.get_simple_key(version), [],
1610
                                         lines)
4119.1.1 by John Arbash Meinel
Move the 'add_lines_nostoresha' to being tested against all VF implementations.
1611
            shas.append(sha)
1612
        # we now have a copy of all the lines in the vf.
1613
        for sha, (version, lines) in zip(
1614
            shas, (empty_text, sample_text_nl, sample_text_no_nl)):
6963.2.4 by Jelmer Vernooij
Add bees.
1615
            new_key = self.get_simple_key(version + b"2")
4119.1.1 by John Arbash Meinel
Move the 'add_lines_nostoresha' to being tested against all VF implementations.
1616
            self.assertRaises(errors.ExistingContent,
1617
                vf.add_lines, new_key, [], lines,
1618
                nostore_sha=sha)
4398.8.4 by John Arbash Meinel
Implement add_text for GroupCompressVersionedFiles
1619
            self.assertRaises(errors.ExistingContent,
6839 by Jelmer Vernooij
Merge lp:~jelmer/brz/get-full.
1620
                vf.add_lines, new_key, [], lines,
4398.8.4 by John Arbash Meinel
Implement add_text for GroupCompressVersionedFiles
1621
                nostore_sha=sha)
4119.1.1 by John Arbash Meinel
Move the 'add_lines_nostoresha' to being tested against all VF implementations.
1622
            # and no new version should have been added.
6634.2.1 by Martin
Apply 2to3 next fixer and make compatible
1623
            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.
1624
            self.assertEqual('absent', record.storage_kind)
1625
4398.8.4 by John Arbash Meinel
Implement add_text for GroupCompressVersionedFiles
1626
    def test_add_lines_nostoresha(self):
1627
        self._add_content_nostoresha(add_lines=True)
1628
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.
1629
    def test_add_lines_return(self):
1630
        files = self.get_versionedfiles()
1631
        # save code by using the stock data insertion helper.
1632
        adds = self.get_diamond_files(files)
1633
        results = []
1634
        # We can only validate the first 2 elements returned from add_lines.
1635
        for add in adds:
1636
            self.assertEqual(3, len(add))
1637
            results.append(add[:2])
1638
        if self.key_length == 1:
1639
            self.assertEqual([
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
1640
                (b'00e364d235126be43292ab09cb4686cf703ddc17', 7),
1641
                (b'51c64a6f4fc375daf0d24aafbabe4d91b6f4bb44', 5),
1642
                (b'a8478686da38e370e32e42e8a0c220e33ee9132f', 10),
1643
                (b'9ef09dfa9d86780bdec9219a22560c6ece8e0ef1', 11),
1644
                (b'ed8bce375198ea62444dc71952b22cfc2b09226d', 23)],
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.
1645
                results)
1646
        elif self.key_length == 2:
1647
            self.assertEqual([
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
1648
                (b'00e364d235126be43292ab09cb4686cf703ddc17', 7),
1649
                (b'00e364d235126be43292ab09cb4686cf703ddc17', 7),
1650
                (b'51c64a6f4fc375daf0d24aafbabe4d91b6f4bb44', 5),
1651
                (b'51c64a6f4fc375daf0d24aafbabe4d91b6f4bb44', 5),
1652
                (b'a8478686da38e370e32e42e8a0c220e33ee9132f', 10),
1653
                (b'a8478686da38e370e32e42e8a0c220e33ee9132f', 10),
1654
                (b'9ef09dfa9d86780bdec9219a22560c6ece8e0ef1', 11),
1655
                (b'9ef09dfa9d86780bdec9219a22560c6ece8e0ef1', 11),
1656
                (b'ed8bce375198ea62444dc71952b22cfc2b09226d', 23),
1657
                (b'ed8bce375198ea62444dc71952b22cfc2b09226d', 23)],
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.
1658
                results)
1659
4241.4.1 by Ian Clatworthy
add sha generation support to versionedfiles
1660
    def test_add_lines_no_key_generates_chk_key(self):
1661
        files = self.get_versionedfiles()
1662
        # save code by using the stock data insertion helper.
1663
        adds = self.get_diamond_files(files, nokeys=True)
1664
        results = []
1665
        # We can only validate the first 2 elements returned from add_lines.
1666
        for add in adds:
1667
            self.assertEqual(3, len(add))
1668
            results.append(add[:2])
1669
        if self.key_length == 1:
1670
            self.assertEqual([
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
1671
                (b'00e364d235126be43292ab09cb4686cf703ddc17', 7),
1672
                (b'51c64a6f4fc375daf0d24aafbabe4d91b6f4bb44', 5),
1673
                (b'a8478686da38e370e32e42e8a0c220e33ee9132f', 10),
1674
                (b'9ef09dfa9d86780bdec9219a22560c6ece8e0ef1', 11),
1675
                (b'ed8bce375198ea62444dc71952b22cfc2b09226d', 23)],
4241.4.1 by Ian Clatworthy
add sha generation support to versionedfiles
1676
                results)
1677
            # Check the added items got CHK keys.
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
1678
            self.assertEqual({
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
1679
                (b'sha1:00e364d235126be43292ab09cb4686cf703ddc17',),
1680
                (b'sha1:51c64a6f4fc375daf0d24aafbabe4d91b6f4bb44',),
1681
                (b'sha1:9ef09dfa9d86780bdec9219a22560c6ece8e0ef1',),
1682
                (b'sha1:a8478686da38e370e32e42e8a0c220e33ee9132f',),
1683
                (b'sha1:ed8bce375198ea62444dc71952b22cfc2b09226d',),
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
1684
                },
4241.4.1 by Ian Clatworthy
add sha generation support to versionedfiles
1685
                files.keys())
1686
        elif self.key_length == 2:
1687
            self.assertEqual([
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
1688
                (b'00e364d235126be43292ab09cb4686cf703ddc17', 7),
1689
                (b'00e364d235126be43292ab09cb4686cf703ddc17', 7),
1690
                (b'51c64a6f4fc375daf0d24aafbabe4d91b6f4bb44', 5),
1691
                (b'51c64a6f4fc375daf0d24aafbabe4d91b6f4bb44', 5),
1692
                (b'a8478686da38e370e32e42e8a0c220e33ee9132f', 10),
1693
                (b'a8478686da38e370e32e42e8a0c220e33ee9132f', 10),
1694
                (b'9ef09dfa9d86780bdec9219a22560c6ece8e0ef1', 11),
1695
                (b'9ef09dfa9d86780bdec9219a22560c6ece8e0ef1', 11),
1696
                (b'ed8bce375198ea62444dc71952b22cfc2b09226d', 23),
1697
                (b'ed8bce375198ea62444dc71952b22cfc2b09226d', 23)],
4241.4.1 by Ian Clatworthy
add sha generation support to versionedfiles
1698
                results)
1699
            # Check the added items got CHK keys.
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
1700
            self.assertEqual({
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
1701
                (b'FileA', b'sha1:00e364d235126be43292ab09cb4686cf703ddc17'),
1702
                (b'FileA', b'sha1:51c64a6f4fc375daf0d24aafbabe4d91b6f4bb44'),
1703
                (b'FileA', b'sha1:9ef09dfa9d86780bdec9219a22560c6ece8e0ef1'),
1704
                (b'FileA', b'sha1:a8478686da38e370e32e42e8a0c220e33ee9132f'),
1705
                (b'FileA', b'sha1:ed8bce375198ea62444dc71952b22cfc2b09226d'),
1706
                (b'FileB', b'sha1:00e364d235126be43292ab09cb4686cf703ddc17'),
1707
                (b'FileB', b'sha1:51c64a6f4fc375daf0d24aafbabe4d91b6f4bb44'),
1708
                (b'FileB', b'sha1:9ef09dfa9d86780bdec9219a22560c6ece8e0ef1'),
1709
                (b'FileB', b'sha1:a8478686da38e370e32e42e8a0c220e33ee9132f'),
1710
                (b'FileB', b'sha1:ed8bce375198ea62444dc71952b22cfc2b09226d'),
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
1711
                },
4241.4.1 by Ian Clatworthy
add sha generation support to versionedfiles
1712
                files.keys())
1713
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.
1714
    def test_empty_lines(self):
1715
        """Empty files can be stored."""
1716
        f = self.get_versionedfiles()
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
1717
        key_a = self.get_simple_key(b'a')
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.
1718
        f.add_lines(key_a, [], [])
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
1719
        self.assertEqual(b'',
1720
            next(f.get_record_stream([key_a], 'unordered', True
1721
                )).get_bytes_as('fulltext'))
1722
        key_b = self.get_simple_key(b'b')
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.
1723
        f.add_lines(key_b, self.get_parents([key_a]), [])
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
1724
        self.assertEqual(b'',
1725
            next(f.get_record_stream([key_b], 'unordered', True
1726
                )).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.
1727
1728
    def test_newline_only(self):
1729
        f = self.get_versionedfiles()
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
1730
        key_a = self.get_simple_key(b'a')
1731
        f.add_lines(key_a, [], [b'\n'])
1732
        self.assertEqual(b'\n',
1733
            next(f.get_record_stream([key_a], 'unordered', True
1734
                )).get_bytes_as('fulltext'))
1735
        key_b = self.get_simple_key(b'b')
1736
        f.add_lines(key_b, self.get_parents([key_a]), [b'\n'])
1737
        self.assertEqual(b'\n',
1738
            next(f.get_record_stream([key_b], 'unordered', True
1739
                )).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.
1740
4593.5.35 by John Arbash Meinel
Start working on a per-vf implementation test of find_ancestry.
1741
    def test_get_known_graph_ancestry(self):
1742
        f = self.get_versionedfiles()
4593.5.36 by John Arbash Meinel
a few more implementations of the interface.
1743
        if not self.graph:
1744
            raise TestNotApplicable('ancestry info only relevant with graph.')
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
1745
        key_a = self.get_simple_key(b'a')
1746
        key_b = self.get_simple_key(b'b')
1747
        key_c = self.get_simple_key(b'c')
4593.5.35 by John Arbash Meinel
Start working on a per-vf implementation test of find_ancestry.
1748
        # A
1749
        # |\
1750
        # | B
1751
        # |/
1752
        # C
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
1753
        f.add_lines(key_a, [], [b'\n'])
1754
        f.add_lines(key_b, [key_a], [b'\n'])
1755
        f.add_lines(key_c, [key_a, key_b], [b'\n'])
4593.5.35 by John Arbash Meinel
Start working on a per-vf implementation test of find_ancestry.
1756
        kg = f.get_known_graph_ancestry([key_c])
1757
        self.assertIsInstance(kg, _mod_graph.KnownGraph)
1758
        self.assertEqual([key_a, key_b, key_c], list(kg.topo_sort()))
1759
4634.11.2 by John Arbash Meinel
Teach VF.get_known_graph_ancestry to go to fallbacks (bug #419241)
1760
    def test_known_graph_with_fallbacks(self):
1761
        f = self.get_versionedfiles('files')
1762
        if not self.graph:
1763
            raise TestNotApplicable('ancestry info only relevant with graph.')
1764
        if getattr(f, 'add_fallback_versioned_files', None) is None:
1765
            raise TestNotApplicable("%s doesn't support fallbacks"
1766
                                    % (f.__class__.__name__,))
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
1767
        key_a = self.get_simple_key(b'a')
1768
        key_b = self.get_simple_key(b'b')
1769
        key_c = self.get_simple_key(b'c')
4634.11.2 by John Arbash Meinel
Teach VF.get_known_graph_ancestry to go to fallbacks (bug #419241)
1770
        # A     only in fallback
1771
        # |\
1772
        # | B
1773
        # |/
1774
        # C
1775
        g = self.get_versionedfiles('fallback')
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
1776
        g.add_lines(key_a, [], [b'\n'])
4634.11.2 by John Arbash Meinel
Teach VF.get_known_graph_ancestry to go to fallbacks (bug #419241)
1777
        f.add_fallback_versioned_files(g)
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
1778
        f.add_lines(key_b, [key_a], [b'\n'])
1779
        f.add_lines(key_c, [key_a, key_b], [b'\n'])
4634.11.2 by John Arbash Meinel
Teach VF.get_known_graph_ancestry to go to fallbacks (bug #419241)
1780
        kg = f.get_known_graph_ancestry([key_c])
1781
        self.assertEqual([key_a, key_b, key_c], list(kg.topo_sort()))
1782
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.
1783
    def test_get_record_stream_empty(self):
1784
        """An empty stream can be requested without error."""
1785
        f = self.get_versionedfiles()
1786
        entries = f.get_record_stream([], 'unordered', False)
1787
        self.assertEqual([], list(entries))
1788
1789
    def assertValidStorageKind(self, storage_kind):
1790
        """Assert that storage_kind is a valid storage_kind."""
1791
        self.assertSubset([storage_kind],
1792
            ['mpdiff', 'knit-annotated-ft', 'knit-annotated-delta',
3890.2.1 by John Arbash Meinel
Start working on a ChunkedContentFactory.
1793
             'knit-ft', 'knit-delta', 'chunked', 'fulltext',
1794
             '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.
1795
             'knit-delta-gz',
3735.32.12 by John Arbash Meinel
Add groupcompress-block[-ref] as valid stream types.
1796
             'knit-delta-closure', 'knit-delta-closure-ref',
1797
             '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.
1798
4332.3.26 by Robert Collins
Allow passing keys to check to VersionedFile.check().
1799
    def capture_stream(self, f, entries, on_seen, parents,
1800
        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.
1801
        """Capture a stream for testing."""
1802
        for factory in entries:
1803
            on_seen(factory.key)
1804
            self.assertValidStorageKind(factory.storage_kind)
4241.4.1 by Ian Clatworthy
add sha generation support to versionedfiles
1805
            if factory.sha1 is not None:
1806
                self.assertEqual(f.get_sha1s([factory.key])[factory.key],
1807
                    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.
1808
            self.assertEqual(parents[factory.key], factory.parents)
1809
            self.assertIsInstance(factory.get_bytes_as(factory.storage_kind),
6963.2.10 by Jelmer Vernooij
Fix a bunch of tests.
1810
                bytes)
4332.3.26 by Robert Collins
Allow passing keys to check to VersionedFile.check().
1811
            if require_fulltext:
1812
                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.
1813
1814
    def test_get_record_stream_interface(self):
1815
        """each item in a stream has to provide a regular interface."""
1816
        files = self.get_versionedfiles()
1817
        self.get_diamond_files(files)
1818
        keys, _ = self.get_keys_and_sort_order()
1819
        parent_map = files.get_parent_map(keys)
1820
        entries = files.get_record_stream(keys, 'unordered', False)
1821
        seen = set()
1822
        self.capture_stream(files, entries, seen.add, parent_map)
1823
        self.assertEqual(set(keys), seen)
1824
1825
    def get_keys_and_sort_order(self):
1826
        """Get diamond test keys list, and their sort ordering."""
1827
        if self.key_length == 1:
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
1828
            keys = [(b'merged',), (b'left',), (b'right',), (b'base',)]
1829
            sort_order = {(b'merged',):2, (b'left',):1, (b'right',):1, (b'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.
1830
        else:
1831
            keys = [
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
1832
                (b'FileA', b'merged'), (b'FileA', b'left'), (b'FileA', b'right'),
1833
                (b'FileA', b'base'),
1834
                (b'FileB', b'merged'), (b'FileB', b'left'), (b'FileB', b'right'),
1835
                (b'FileB', b'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.
1836
                ]
1837
            sort_order = {
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
1838
                (b'FileA', b'merged'): 2, (b'FileA', b'left'): 1, (b'FileA', b'right'): 1,
1839
                (b'FileA', b'base'): 0,
1840
                (b'FileB', b'merged'): 2, (b'FileB', b'left'): 1, (b'FileB', b'right'): 1,
1841
                (b'FileB', b'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.
1842
                }
1843
        return keys, sort_order
1844
4111.1.1 by Robert Collins
Add a groupcompress sort order.
1845
    def get_keys_and_groupcompress_sort_order(self):
1846
        """Get diamond test keys list, and their groupcompress sort ordering."""
1847
        if self.key_length == 1:
6963.2.4 by Jelmer Vernooij
Add bees.
1848
            keys = [(b'merged',), (b'left',), (b'right',), (b'base',)]
1849
            sort_order = {(b'merged',): 0, (b'left',): 1, (b'right',): 1, (b'base',):2}
4111.1.1 by Robert Collins
Add a groupcompress sort order.
1850
        else:
1851
            keys = [
6963.2.4 by Jelmer Vernooij
Add bees.
1852
                (b'FileA', b'merged'), (b'FileA', b'left'), (b'FileA', b'right'),
1853
                (b'FileA', b'base'),
1854
                (b'FileB', b'merged'), (b'FileB', b'left'), (b'FileB', b'right'),
1855
                (b'FileB', b'base'),
4111.1.1 by Robert Collins
Add a groupcompress sort order.
1856
                ]
1857
            sort_order = {
6963.2.4 by Jelmer Vernooij
Add bees.
1858
                (b'FileA', b'merged'): 0, (b'FileA', b'left'): 1, (b'FileA', b'right'): 1,
1859
                (b'FileA', b'base'): 2,
1860
                (b'FileB', b'merged'): 3, (b'FileB', b'left'): 4, (b'FileB', b'right'): 4,
1861
                (b'FileB', b'base'): 5,
4111.1.1 by Robert Collins
Add a groupcompress sort order.
1862
                }
1863
        return keys, sort_order
1864
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.
1865
    def test_get_record_stream_interface_ordered(self):
1866
        """each item in a stream has to provide a regular interface."""
1867
        files = self.get_versionedfiles()
1868
        self.get_diamond_files(files)
1869
        keys, sort_order = self.get_keys_and_sort_order()
1870
        parent_map = files.get_parent_map(keys)
1871
        entries = files.get_record_stream(keys, 'topological', False)
1872
        seen = []
1873
        self.capture_stream(files, entries, seen.append, parent_map)
1874
        self.assertStreamOrder(sort_order, seen, keys)
1875
1876
    def test_get_record_stream_interface_ordered_with_delta_closure(self):
1877
        """each item must be accessible as a fulltext."""
1878
        files = self.get_versionedfiles()
1879
        self.get_diamond_files(files)
1880
        keys, sort_order = self.get_keys_and_sort_order()
1881
        parent_map = files.get_parent_map(keys)
1882
        entries = files.get_record_stream(keys, 'topological', True)
1883
        seen = []
1884
        for factory in entries:
1885
            seen.append(factory.key)
1886
            self.assertValidStorageKind(factory.storage_kind)
3350.8.3 by Robert Collins
VF.get_sha1s needed changing to be stackable.
1887
            self.assertSubset([factory.sha1],
1888
                [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.
1889
            self.assertEqual(parent_map[factory.key], factory.parents)
1890
            # self.assertEqual(files.get_text(factory.key),
3890.2.1 by John Arbash Meinel
Start working on a ChunkedContentFactory.
1891
            ft_bytes = factory.get_bytes_as('fulltext')
6963.2.10 by Jelmer Vernooij
Fix a bunch of tests.
1892
            self.assertIsInstance(ft_bytes, bytes)
3890.2.1 by John Arbash Meinel
Start working on a ChunkedContentFactory.
1893
            chunked_bytes = factory.get_bytes_as('chunked')
6963.2.4 by Jelmer Vernooij
Add bees.
1894
            self.assertEqualDiff(ft_bytes, b''.join(chunked_bytes))
3890.2.1 by John Arbash Meinel
Start working on a ChunkedContentFactory.
1895
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.
1896
        self.assertStreamOrder(sort_order, seen, keys)
1897
4111.1.1 by Robert Collins
Add a groupcompress sort order.
1898
    def test_get_record_stream_interface_groupcompress(self):
1899
        """each item in a stream has to provide a regular interface."""
1900
        files = self.get_versionedfiles()
1901
        self.get_diamond_files(files)
1902
        keys, sort_order = self.get_keys_and_groupcompress_sort_order()
1903
        parent_map = files.get_parent_map(keys)
1904
        entries = files.get_record_stream(keys, 'groupcompress', False)
1905
        seen = []
1906
        self.capture_stream(files, entries, seen.append, parent_map)
1907
        self.assertStreamOrder(sort_order, seen, keys)
1908
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.
1909
    def assertStreamOrder(self, sort_order, seen, keys):
1910
        self.assertEqual(len(set(seen)), len(keys))
1911
        if self.key_length == 1:
1912
            lows = {():0}
1913
        else:
6963.2.3 by Jelmer Vernooij
Fix some more tests.
1914
            lows = {(b'FileA',):0, (b'FileB',):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.
1915
        if not self.graph:
1916
            self.assertEqual(set(keys), set(seen))
1917
        else:
1918
            for key in seen:
1919
                sort_pos = sort_order[key]
1920
                self.assertTrue(sort_pos >= lows[key[:-1]],
1921
                    "Out of order in sorted stream: %r, %r" % (key, seen))
1922
                lows[key[:-1]] = sort_pos
1923
1924
    def test_get_record_stream_unknown_storage_kind_raises(self):
1925
        """Asking for a storage kind that the stream cannot supply raises."""
1926
        files = self.get_versionedfiles()
1927
        self.get_diamond_files(files)
1928
        if self.key_length == 1:
6963.2.4 by Jelmer Vernooij
Add bees.
1929
            keys = [(b'merged',), (b'left',), (b'right',), (b'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.
1930
        else:
1931
            keys = [
6963.2.4 by Jelmer Vernooij
Add bees.
1932
                (b'FileA', b'merged'), (b'FileA', b'left'), (b'FileA', b'right'),
1933
                (b'FileA', b'base'),
1934
                (b'FileB', b'merged'), (b'FileB', b'left'), (b'FileB', b'right'),
1935
                (b'FileB', b'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.
1936
                ]
1937
        parent_map = files.get_parent_map(keys)
1938
        entries = files.get_record_stream(keys, 'unordered', False)
1939
        # We track the contents because we should be able to try, fail a
1940
        # particular kind and then ask for one that works and continue.
1941
        seen = set()
1942
        for factory in entries:
1943
            seen.add(factory.key)
1944
            self.assertValidStorageKind(factory.storage_kind)
4241.4.1 by Ian Clatworthy
add sha generation support to versionedfiles
1945
            if factory.sha1 is not None:
1946
                self.assertEqual(files.get_sha1s([factory.key])[factory.key],
1947
                                 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.
1948
            self.assertEqual(parent_map[factory.key], factory.parents)
1949
            # currently no stream emits mpdiff
1950
            self.assertRaises(errors.UnavailableRepresentation,
1951
                factory.get_bytes_as, 'mpdiff')
1952
            self.assertIsInstance(factory.get_bytes_as(factory.storage_kind),
6963.2.10 by Jelmer Vernooij
Fix a bunch of tests.
1953
                bytes)
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.
1954
        self.assertEqual(set(keys), seen)
1955
1956
    def test_get_record_stream_missing_records_are_absent(self):
1957
        files = self.get_versionedfiles()
1958
        self.get_diamond_files(files)
1959
        if self.key_length == 1:
6963.2.4 by Jelmer Vernooij
Add bees.
1960
            keys = [(b'merged',), (b'left',), (b'right',), (b'absent',), (b'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.
1961
        else:
1962
            keys = [
6963.2.4 by Jelmer Vernooij
Add bees.
1963
                (b'FileA', b'merged'), (b'FileA', b'left'), (b'FileA', b'right'),
1964
                (b'FileA', b'absent'), (b'FileA', b'base'),
1965
                (b'FileB', b'merged'), (b'FileB', b'left'), (b'FileB', b'right'),
1966
                (b'FileB', b'absent'), (b'FileB', b'base'),
1967
                (b'absent', b'absent'),
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.
1968
                ]
1969
        parent_map = files.get_parent_map(keys)
1970
        entries = files.get_record_stream(keys, 'unordered', False)
1971
        self.assertAbsentRecord(files, keys, parent_map, entries)
1972
        entries = files.get_record_stream(keys, 'topological', False)
1973
        self.assertAbsentRecord(files, keys, parent_map, entries)
1974
4005.3.3 by Robert Collins
Test NetworkRecordStream with delta'd texts.
1975
    def assertRecordHasContent(self, record, bytes):
1976
        """Assert that record has the bytes bytes."""
1977
        self.assertEqual(bytes, record.get_bytes_as('fulltext'))
6963.2.4 by Jelmer Vernooij
Add bees.
1978
        self.assertEqual(bytes, b''.join(record.get_bytes_as('chunked')))
4005.3.3 by Robert Collins
Test NetworkRecordStream with delta'd texts.
1979
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.
1980
    def test_get_record_stream_native_formats_are_wire_ready_one_ft(self):
1981
        files = self.get_versionedfiles()
6963.2.3 by Jelmer Vernooij
Fix some more tests.
1982
        key = self.get_simple_key(b'foo')
1983
        files.add_lines(key, (), [b'my text\n', b'content'])
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.
1984
        stream = files.get_record_stream([key], 'unordered', False)
6634.2.1 by Martin
Apply 2to3 next fixer and make compatible
1985
        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.
1986
        if record.storage_kind in ('chunked', 'fulltext'):
1987
            # chunked and fulltext representations are for direct use not wire
4005.3.3 by Robert Collins
Test NetworkRecordStream with delta'd texts.
1988
            # serialisation: check they are able to be used directly. To send
1989
            # such records over the wire translation will be needed.
6963.2.3 by Jelmer Vernooij
Fix some more tests.
1990
            self.assertRecordHasContent(record, b"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.
1991
        else:
1992
            bytes = [record.get_bytes_as(record.storage_kind)]
1993
            network_stream = versionedfile.NetworkRecordStream(bytes).read()
1994
            source_record = record
1995
            records = []
1996
            for record in network_stream:
1997
                records.append(record)
1998
                self.assertEqual(source_record.storage_kind,
1999
                    record.storage_kind)
2000
                self.assertEqual(source_record.parents, record.parents)
2001
                self.assertEqual(
2002
                    source_record.get_bytes_as(source_record.storage_kind),
2003
                    record.get_bytes_as(record.storage_kind))
2004
            self.assertEqual(1, len(records))
2005
4005.3.5 by Robert Collins
Interface level test for using delta_closure=True over the network.
2006
    def assertStreamMetaEqual(self, records, expected, stream):
2007
        """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.
2008
4005.3.5 by Robert Collins
Interface level test for using delta_closure=True over the network.
2009
        :param records: A list to collect the seen records.
2010
        :return: A generator of the records in stream.
2011
        """
6631.2.3 by Martin
Fix per_versionedfile test failures and rethink future_builtins
2012
        # We make assertions during copying to catch things early for easier
2013
        # debugging. This must use the iterating zip() from the future.
6631.2.2 by Martin
Run 2to3 itertools fixer and refactor
2014
        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.
2015
            records.append(record)
2016
            self.assertEqual(ref_record.key, record.key)
2017
            self.assertEqual(ref_record.storage_kind, record.storage_kind)
2018
            self.assertEqual(ref_record.parents, record.parents)
2019
            yield record
2020
2021
    def stream_to_bytes_or_skip_counter(self, skipped_records, full_texts,
2022
        stream):
2023
        """Convert a stream to a bytes iterator.
2024
2025
        :param skipped_records: A list with one element to increment when a
2026
            record is skipped.
4032.1.2 by John Arbash Meinel
Track down a few more files that have trailing whitespace.
2027
        :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.
2028
            checking chunked or fulltext stored records.
2029
        :param stream: A record_stream.
2030
        :return: An iterator over the bytes of each record.
2031
        """
2032
        for record in stream:
2033
            if record.storage_kind in ('chunked', 'fulltext'):
2034
                skipped_records[0] += 1
2035
                # check the content is correct for direct use.
2036
                self.assertRecordHasContent(record, full_texts[record.key])
2037
            else:
2038
                yield record.get_bytes_as(record.storage_kind)
2039
4005.3.3 by Robert Collins
Test NetworkRecordStream with delta'd texts.
2040
    def test_get_record_stream_native_formats_are_wire_ready_ft_delta(self):
2041
        files = self.get_versionedfiles()
2042
        target_files = self.get_versionedfiles('target')
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2043
        key = self.get_simple_key(b'ft')
2044
        key_delta = self.get_simple_key(b'delta')
2045
        files.add_lines(key, (), [b'my text\n', b'content'])
4005.3.3 by Robert Collins
Test NetworkRecordStream with delta'd texts.
2046
        if self.graph:
2047
            delta_parents = (key,)
2048
        else:
2049
            delta_parents = ()
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2050
        files.add_lines(key_delta, delta_parents, [b'different\n', b'content\n'])
4005.3.3 by Robert Collins
Test NetworkRecordStream with delta'd texts.
2051
        local = files.get_record_stream([key, key_delta], 'unordered', False)
2052
        ref = files.get_record_stream([key, key_delta], 'unordered', False)
2053
        skipped_records = [0]
4005.3.5 by Robert Collins
Interface level test for using delta_closure=True over the network.
2054
        full_texts = {
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2055
            key: b"my text\ncontent",
2056
            key_delta: b"different\ncontent\n",
4005.3.5 by Robert Collins
Interface level test for using delta_closure=True over the network.
2057
            }
2058
        byte_stream = self.stream_to_bytes_or_skip_counter(
2059
            skipped_records, full_texts, local)
4005.3.3 by Robert Collins
Test NetworkRecordStream with delta'd texts.
2060
        network_stream = versionedfile.NetworkRecordStream(byte_stream).read()
2061
        records = []
2062
        # insert the stream from the network into a versioned files object so we can
2063
        # check the content was carried across correctly without doing delta
2064
        # inspection.
4005.3.5 by Robert Collins
Interface level test for using delta_closure=True over the network.
2065
        target_files.insert_record_stream(
2066
            self.assertStreamMetaEqual(records, ref, network_stream))
4005.3.3 by Robert Collins
Test NetworkRecordStream with delta'd texts.
2067
        # No duplicates on the wire thank you!
2068
        self.assertEqual(2, len(records) + skipped_records[0])
2069
        if len(records):
4005.3.4 by Robert Collins
Test copying just a delta over the wire.
2070
            # if any content was copied it all must have all been.
2071
            self.assertIdenticalVersionedFile(files, target_files)
2072
2073
    def test_get_record_stream_native_formats_are_wire_ready_delta(self):
2074
        # copy a delta over the wire
2075
        files = self.get_versionedfiles()
2076
        target_files = self.get_versionedfiles('target')
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2077
        key = self.get_simple_key(b'ft')
2078
        key_delta = self.get_simple_key(b'delta')
2079
        files.add_lines(key, (), [b'my text\n', b'content'])
4005.3.4 by Robert Collins
Test copying just a delta over the wire.
2080
        if self.graph:
2081
            delta_parents = (key,)
2082
        else:
2083
            delta_parents = ()
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2084
        files.add_lines(key_delta, delta_parents, [b'different\n', b'content\n'])
4005.3.5 by Robert Collins
Interface level test for using delta_closure=True over the network.
2085
        # Copy the basis text across so we can reconstruct the delta during
2086
        # insertion into target.
4005.3.4 by Robert Collins
Test copying just a delta over the wire.
2087
        target_files.insert_record_stream(files.get_record_stream([key],
2088
            'unordered', False))
2089
        local = files.get_record_stream([key_delta], 'unordered', False)
2090
        ref = files.get_record_stream([key_delta], 'unordered', False)
2091
        skipped_records = [0]
4005.3.5 by Robert Collins
Interface level test for using delta_closure=True over the network.
2092
        full_texts = {
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2093
            key_delta: b"different\ncontent\n",
4005.3.5 by Robert Collins
Interface level test for using delta_closure=True over the network.
2094
            }
2095
        byte_stream = self.stream_to_bytes_or_skip_counter(
2096
            skipped_records, full_texts, local)
4005.3.4 by Robert Collins
Test copying just a delta over the wire.
2097
        network_stream = versionedfile.NetworkRecordStream(byte_stream).read()
2098
        records = []
2099
        # insert the stream from the network into a versioned files object so we can
2100
        # check the content was carried across correctly without doing delta
2101
        # inspection during check_stream.
4005.3.5 by Robert Collins
Interface level test for using delta_closure=True over the network.
2102
        target_files.insert_record_stream(
2103
            self.assertStreamMetaEqual(records, ref, network_stream))
4005.3.4 by Robert Collins
Test copying just a delta over the wire.
2104
        # No duplicates on the wire thank you!
2105
        self.assertEqual(1, len(records) + skipped_records[0])
2106
        if len(records):
2107
            # if any content was copied it all must have all been
4005.3.3 by Robert Collins
Test NetworkRecordStream with delta'd texts.
2108
            self.assertIdenticalVersionedFile(files, target_files)
2109
4005.3.5 by Robert Collins
Interface level test for using delta_closure=True over the network.
2110
    def test_get_record_stream_wire_ready_delta_closure_included(self):
2111
        # copy a delta over the wire with the ability to get its full text.
2112
        files = self.get_versionedfiles()
6963.2.12 by Jelmer Vernooij
Fix some more tests.
2113
        key = self.get_simple_key(b'ft')
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2114
        key_delta = self.get_simple_key(b'delta')
2115
        files.add_lines(key, (), [b'my text\n', b'content'])
4005.3.5 by Robert Collins
Interface level test for using delta_closure=True over the network.
2116
        if self.graph:
2117
            delta_parents = (key,)
2118
        else:
2119
            delta_parents = ()
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2120
        files.add_lines(key_delta, delta_parents, [b'different\n', b'content\n'])
4005.3.5 by Robert Collins
Interface level test for using delta_closure=True over the network.
2121
        local = files.get_record_stream([key_delta], 'unordered', True)
2122
        ref = files.get_record_stream([key_delta], 'unordered', True)
2123
        skipped_records = [0]
2124
        full_texts = {
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2125
            key_delta: b"different\ncontent\n",
4005.3.5 by Robert Collins
Interface level test for using delta_closure=True over the network.
2126
            }
2127
        byte_stream = self.stream_to_bytes_or_skip_counter(
2128
            skipped_records, full_texts, local)
2129
        network_stream = versionedfile.NetworkRecordStream(byte_stream).read()
2130
        records = []
2131
        # insert the stream from the network into a versioned files object so we can
2132
        # check the content was carried across correctly without doing delta
2133
        # inspection during check_stream.
2134
        for record in self.assertStreamMetaEqual(records, ref, network_stream):
2135
            # we have to be able to get the full text out:
2136
            self.assertRecordHasContent(record, full_texts[record.key])
2137
        # No duplicates on the wire thank you!
2138
        self.assertEqual(1, len(records) + skipped_records[0])
2139
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.
2140
    def assertAbsentRecord(self, files, keys, parents, entries):
2141
        """Helper for test_get_record_stream_missing_records_are_absent."""
2142
        seen = set()
2143
        for factory in entries:
2144
            seen.add(factory.key)
6963.2.4 by Jelmer Vernooij
Add bees.
2145
            if factory.key[-1] == b'absent':
6963.2.10 by Jelmer Vernooij
Fix a bunch of tests.
2146
                self.assertEqual('absent', factory.storage_kind)
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.
2147
                self.assertEqual(None, factory.sha1)
2148
                self.assertEqual(None, factory.parents)
2149
            else:
2150
                self.assertValidStorageKind(factory.storage_kind)
4241.4.1 by Ian Clatworthy
add sha generation support to versionedfiles
2151
                if factory.sha1 is not None:
2152
                    sha1 = files.get_sha1s([factory.key])[factory.key]
2153
                    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.
2154
                self.assertEqual(parents[factory.key], factory.parents)
2155
                self.assertIsInstance(factory.get_bytes_as(factory.storage_kind),
6963.2.10 by Jelmer Vernooij
Fix a bunch of tests.
2156
                    bytes)
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.
2157
        self.assertEqual(set(keys), seen)
2158
2159
    def test_filter_absent_records(self):
2160
        """Requested missing records can be filter trivially."""
2161
        files = self.get_versionedfiles()
2162
        self.get_diamond_files(files)
2163
        keys, _ = self.get_keys_and_sort_order()
2164
        parent_map = files.get_parent_map(keys)
2165
        # Add an absent record in the middle of the present keys. (We don't ask
2166
        # for just absent keys to ensure that content before and after the
2167
        # absent keys is still delivered).
2168
        present_keys = list(keys)
2169
        if self.key_length == 1:
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2170
            keys.insert(2, (b'extra',))
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.
2171
        else:
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2172
            keys.insert(2, (b'extra', b'extra'))
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.
2173
        entries = files.get_record_stream(keys, 'unordered', False)
2174
        seen = set()
2175
        self.capture_stream(files, versionedfile.filter_absent(entries), seen.add,
2176
            parent_map)
2177
        self.assertEqual(set(present_keys), seen)
2178
2179
    def get_mapper(self):
2180
        """Get a mapper suitable for the key length of the test interface."""
2181
        if self.key_length == 1:
2182
            return ConstantMapper('source')
2183
        else:
2184
            return HashEscapedPrefixMapper()
2185
2186
    def get_parents(self, parents):
2187
        """Get parents, taking self.graph into consideration."""
2188
        if self.graph:
2189
            return parents
2190
        else:
2191
            return None
2192
4332.3.32 by Robert Collins
Merge bzr.dev.
2193
    def test_get_annotator(self):
2194
        files = self.get_versionedfiles()
2195
        self.get_diamond_files(files)
6963.2.4 by Jelmer Vernooij
Add bees.
2196
        origin_key = self.get_simple_key(b'origin')
2197
        base_key = self.get_simple_key(b'base')
2198
        left_key = self.get_simple_key(b'left')
2199
        right_key = self.get_simple_key(b'right')
2200
        merged_key = self.get_simple_key(b'merged')
4332.3.32 by Robert Collins
Merge bzr.dev.
2201
        # annotator = files.get_annotator()
2202
        # introduced full text
2203
        origins, lines = files.get_annotator().annotate(origin_key)
2204
        self.assertEqual([(origin_key,)], origins)
6963.2.4 by Jelmer Vernooij
Add bees.
2205
        self.assertEqual([b'origin\n'], lines)
4332.3.32 by Robert Collins
Merge bzr.dev.
2206
        # a delta
2207
        origins, lines = files.get_annotator().annotate(base_key)
2208
        self.assertEqual([(base_key,)], origins)
2209
        # a merge
2210
        origins, lines = files.get_annotator().annotate(merged_key)
2211
        if self.graph:
2212
            self.assertEqual([
2213
                (base_key,),
2214
                (left_key,),
2215
                (right_key,),
2216
                (merged_key,),
2217
                ], origins)
2218
        else:
2219
            # Without a graph everything is new.
2220
            self.assertEqual([
2221
                (merged_key,),
2222
                (merged_key,),
2223
                (merged_key,),
2224
                (merged_key,),
2225
                ], origins)
2226
        self.assertRaises(RevisionNotPresent,
6963.2.12 by Jelmer Vernooij
Fix some more tests.
2227
            files.get_annotator().annotate, self.get_simple_key(b'missing-key'))
4332.3.32 by Robert Collins
Merge bzr.dev.
2228
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.
2229
    def test_get_parent_map(self):
2230
        files = self.get_versionedfiles()
2231
        if self.key_length == 1:
2232
            parent_details = [
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2233
                ((b'r0',), self.get_parents(())),
2234
                ((b'r1',), self.get_parents(((b'r0',),))),
2235
                ((b'r2',), self.get_parents(())),
2236
                ((b'r3',), self.get_parents(())),
2237
                ((b'm',), self.get_parents(((b'r0',), (b'r1',), (b'r2',), (b'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.
2238
                ]
2239
        else:
2240
            parent_details = [
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2241
                ((b'FileA', b'r0'), self.get_parents(())),
2242
                ((b'FileA', b'r1'), self.get_parents(((b'FileA', b'r0'),))),
2243
                ((b'FileA', b'r2'), self.get_parents(())),
2244
                ((b'FileA', b'r3'), self.get_parents(())),
2245
                ((b'FileA', b'm'), self.get_parents(((b'FileA', b'r0'),
2246
                    (b'FileA', b'r1'), (b'FileA', b'r2'), (b'FileA', b'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.
2247
                ]
2248
        for key, parents in parent_details:
2249
            files.add_lines(key, parents, [])
2250
            # immediately after adding it should be queryable.
2251
            self.assertEqual({key:parents}, files.get_parent_map([key]))
2252
        # We can ask for an empty set
2253
        self.assertEqual({}, files.get_parent_map([]))
2254
        # We can ask for many keys
2255
        all_parents = dict(parent_details)
2256
        self.assertEqual(all_parents, files.get_parent_map(all_parents.keys()))
2257
        # Absent keys are just not included in the result.
6963.2.10 by Jelmer Vernooij
Fix a bunch of tests.
2258
        keys = list(all_parents.keys())
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.
2259
        if self.key_length == 1:
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2260
            keys.insert(1, (b'missing',))
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.
2261
        else:
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2262
            keys.insert(1, (b'missing', b'missing'))
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.
2263
        # Absent keys are just ignored
2264
        self.assertEqual(all_parents, files.get_parent_map(keys))
2265
2266
    def test_get_sha1s(self):
2267
        files = self.get_versionedfiles()
2268
        self.get_diamond_files(files)
2269
        if self.key_length == 1:
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2270
            keys = [(b'base',), (b'origin',), (b'left',), (b'merged',), (b'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.
2271
        else:
2272
            # ask for shas from different prefixes.
2273
            keys = [
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2274
                (b'FileA', b'base'), (b'FileB', b'origin'), (b'FileA', b'left'),
2275
                (b'FileA', b'merged'), (b'FileB', b'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.
2276
                ]
3350.8.3 by Robert Collins
VF.get_sha1s needed changing to be stackable.
2277
        self.assertEqual({
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2278
            keys[0]: b'51c64a6f4fc375daf0d24aafbabe4d91b6f4bb44',
2279
            keys[1]: b'00e364d235126be43292ab09cb4686cf703ddc17',
2280
            keys[2]: b'a8478686da38e370e32e42e8a0c220e33ee9132f',
2281
            keys[3]: b'ed8bce375198ea62444dc71952b22cfc2b09226d',
2282
            keys[4]: b'9ef09dfa9d86780bdec9219a22560c6ece8e0ef1',
3350.8.3 by Robert Collins
VF.get_sha1s needed changing to be stackable.
2283
            },
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.
2284
            files.get_sha1s(keys))
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
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
    def test_insert_record_stream_empty(self):
2287
        """Inserting an empty record stream should work."""
2288
        files = self.get_versionedfiles()
2289
        files.insert_record_stream([])
2290
2291
    def assertIdenticalVersionedFile(self, expected, actual):
2292
        """Assert that left and right have the same contents."""
2293
        self.assertEqual(set(actual.keys()), set(expected.keys()))
2294
        actual_parents = actual.get_parent_map(actual.keys())
2295
        if self.graph:
2296
            self.assertEqual(actual_parents, expected.get_parent_map(expected.keys()))
2297
        else:
2298
            for key, parents in actual_parents.items():
2299
                self.assertEqual(None, parents)
2300
        for key in actual.keys():
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2301
            actual_text = next(actual.get_record_stream(
2302
                [key], 'unordered', True)).get_bytes_as('fulltext')
2303
            expected_text = next(expected.get_record_stream(
2304
                [key], 'unordered', True)).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.
2305
            self.assertEqual(actual_text, expected_text)
2306
2307
    def test_insert_record_stream_fulltexts(self):
2308
        """Any file should accept a stream of fulltexts."""
2309
        files = self.get_versionedfiles()
2310
        mapper = self.get_mapper()
2311
        source_transport = self.get_transport('source')
2312
        source_transport.mkdir('.')
2313
        # weaves always output fulltexts.
2314
        source = make_versioned_files_factory(WeaveFile, mapper)(
2315
            source_transport)
2316
        self.get_diamond_files(source, trailing_eol=False)
2317
        stream = source.get_record_stream(source.keys(), 'topological',
2318
            False)
2319
        files.insert_record_stream(stream)
2320
        self.assertIdenticalVersionedFile(source, files)
2321
2322
    def test_insert_record_stream_fulltexts_noeol(self):
2323
        """Any file should accept a stream of fulltexts."""
2324
        files = self.get_versionedfiles()
2325
        mapper = self.get_mapper()
2326
        source_transport = self.get_transport('source')
2327
        source_transport.mkdir('.')
2328
        # weaves always output fulltexts.
2329
        source = make_versioned_files_factory(WeaveFile, mapper)(
2330
            source_transport)
2331
        self.get_diamond_files(source, trailing_eol=False)
2332
        stream = source.get_record_stream(source.keys(), 'topological',
2333
            False)
2334
        files.insert_record_stream(stream)
2335
        self.assertIdenticalVersionedFile(source, files)
2336
2337
    def test_insert_record_stream_annotated_knits(self):
2338
        """Any file should accept a stream from plain knits."""
2339
        files = self.get_versionedfiles()
2340
        mapper = self.get_mapper()
2341
        source_transport = self.get_transport('source')
2342
        source_transport.mkdir('.')
2343
        source = make_file_factory(True, mapper)(source_transport)
2344
        self.get_diamond_files(source)
2345
        stream = source.get_record_stream(source.keys(), 'topological',
2346
            False)
2347
        files.insert_record_stream(stream)
2348
        self.assertIdenticalVersionedFile(source, files)
2349
2350
    def test_insert_record_stream_annotated_knits_noeol(self):
2351
        """Any file should accept a stream from plain knits."""
2352
        files = self.get_versionedfiles()
2353
        mapper = self.get_mapper()
2354
        source_transport = self.get_transport('source')
2355
        source_transport.mkdir('.')
2356
        source = make_file_factory(True, mapper)(source_transport)
2357
        self.get_diamond_files(source, trailing_eol=False)
2358
        stream = source.get_record_stream(source.keys(), 'topological',
2359
            False)
2360
        files.insert_record_stream(stream)
2361
        self.assertIdenticalVersionedFile(source, files)
2362
2363
    def test_insert_record_stream_plain_knits(self):
2364
        """Any file should accept a stream from plain knits."""
2365
        files = self.get_versionedfiles()
2366
        mapper = self.get_mapper()
2367
        source_transport = self.get_transport('source')
2368
        source_transport.mkdir('.')
2369
        source = make_file_factory(False, mapper)(source_transport)
2370
        self.get_diamond_files(source)
2371
        stream = source.get_record_stream(source.keys(), 'topological',
2372
            False)
2373
        files.insert_record_stream(stream)
2374
        self.assertIdenticalVersionedFile(source, files)
2375
2376
    def test_insert_record_stream_plain_knits_noeol(self):
2377
        """Any file should accept a stream from plain knits."""
2378
        files = self.get_versionedfiles()
2379
        mapper = self.get_mapper()
2380
        source_transport = self.get_transport('source')
2381
        source_transport.mkdir('.')
2382
        source = make_file_factory(False, mapper)(source_transport)
2383
        self.get_diamond_files(source, trailing_eol=False)
2384
        stream = source.get_record_stream(source.keys(), 'topological',
2385
            False)
2386
        files.insert_record_stream(stream)
2387
        self.assertIdenticalVersionedFile(source, files)
2388
2389
    def test_insert_record_stream_existing_keys(self):
2390
        """Inserting keys already in a file should not error."""
2391
        files = self.get_versionedfiles()
2392
        source = self.get_versionedfiles('source')
2393
        self.get_diamond_files(source)
2394
        # insert some keys into f.
2395
        self.get_diamond_files(files, left_only=True)
2396
        stream = source.get_record_stream(source.keys(), 'topological',
2397
            False)
2398
        files.insert_record_stream(stream)
2399
        self.assertIdenticalVersionedFile(source, files)
2400
2401
    def test_insert_record_stream_missing_keys(self):
2402
        """Inserting a stream with absent keys should raise an error."""
2403
        files = self.get_versionedfiles()
2404
        source = self.get_versionedfiles('source')
6963.2.11 by Jelmer Vernooij
Fix some more tests.
2405
        stream = source.get_record_stream([(b'missing',) * self.key_length],
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.
2406
            'topological', False)
2407
        self.assertRaises(errors.RevisionNotPresent, files.insert_record_stream,
2408
            stream)
2409
2410
    def test_insert_record_stream_out_of_order(self):
2411
        """An out of order stream can either error or work."""
2412
        files = self.get_versionedfiles()
2413
        source = self.get_versionedfiles('source')
2414
        self.get_diamond_files(source)
2415
        if self.key_length == 1:
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2416
            origin_keys = [(b'origin',)]
2417
            end_keys = [(b'merged',), (b'left',)]
2418
            start_keys = [(b'right',), (b'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.
2419
        else:
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2420
            origin_keys = [(b'FileA', b'origin'), (b'FileB', b'origin')]
2421
            end_keys = [(b'FileA', b'merged',), (b'FileA', b'left',),
2422
                (b'FileB', b'merged',), (b'FileB', b'left',)]
2423
            start_keys = [(b'FileA', b'right',), (b'FileA', b'base',),
2424
                (b'FileB', b'right',), (b'FileB', b'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.
2425
        origin_entries = source.get_record_stream(origin_keys, 'unordered', False)
2426
        end_entries = source.get_record_stream(end_keys, 'topological', False)
2427
        start_entries = source.get_record_stream(start_keys, 'topological', False)
6631.2.3 by Martin
Fix per_versionedfile test failures and rethink future_builtins
2428
        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.
2429
        try:
2430
            files.insert_record_stream(entries)
2431
        except RevisionNotPresent:
2432
            # Must not have corrupted the file.
2433
            files.check()
2434
        else:
2435
            self.assertIdenticalVersionedFile(source, files)
2436
4634.84.2 by Andrew Bennetts
Add test.
2437
    def test_insert_record_stream_long_parent_chain_out_of_order(self):
2438
        """An out of order stream can either error or work."""
2439
        if not self.graph:
2440
            raise TestNotApplicable('ancestry info only relevant with graph.')
2441
        # Create a reasonably long chain of records based on each other, where
2442
        # most will be deltas.
2443
        source = self.get_versionedfiles('source')
2444
        parents = ()
2445
        keys = []
6963.2.4 by Jelmer Vernooij
Add bees.
2446
        content = [(b'same same %d\n' % n) for n in range(500)]
6963.2.10 by Jelmer Vernooij
Fix a bunch of tests.
2447
        letters = b'abcdefghijklmnopqrstuvwxyz'
2448
        for i in range(len(letters)):
2449
            letter = letters[i:i+1]
2450
            key = (b'key-' + letter,)
4634.84.2 by Andrew Bennetts
Add test.
2451
            if self.key_length == 2:
6963.2.4 by Jelmer Vernooij
Add bees.
2452
                key = (b'prefix',) + key
2453
            content.append(b'content for ' + letter + b'\n')
4634.84.2 by Andrew Bennetts
Add test.
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()
6963.2.4 by Jelmer Vernooij
Add bees.
2494
        keys = [self.get_simple_key(b'origin'), self.get_simple_key(b'merged')]
4009.3.7 by Andrew Bennetts
Most tests passing.
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()
6963.2.4 by Jelmer Vernooij
Add bees.
2502
            self.assertEqual({self.get_simple_key(b'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()
6963.2.4 by Jelmer Vernooij
Add bees.
2521
        entries = source.get_record_stream([self.get_simple_key(b'origin'),
2522
            self.get_simple_key(b'merged')], 'unordered', False)
4009.3.2 by Andrew Bennetts
Add test_insert_record_stream_delta_missing_basis_can_be_added_later.
2523
        files = self.get_versionedfiles()
2524
        files.insert_record_stream(entries)
2525
        missing_bases = files.get_missing_compression_parent_keys()
6963.2.4 by Jelmer Vernooij
Add bees.
2526
        self.assertEqual({self.get_simple_key(b'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).
6963.2.4 by Jelmer Vernooij
Add bees.
2530
        merged_key = self.get_simple_key(b'merged')
4009.3.7 by Andrew Bennetts
Most tests passing.
2531
        self.assertEqual(
6973.14.4 by Jelmer Vernooij
Fix checking for options.
2532
            [merged_key], list(files.get_parent_map([merged_key]).keys()))
4009.3.7 by Andrew Bennetts
Most tests passing.
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(
6973.14.4 by Jelmer Vernooij
Fix checking for options.
2540
            [merged_key], list(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
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2560
        files.add_lines(self.get_simple_key(b'base'), (), [b'base\n'])
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.
2561
        # add a ancestor to be included on one side
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2562
        files.add_lines(self.get_simple_key(b'lancestor'), (), [b'lancestor\n'])
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.
2563
        # add a ancestor to be included on the other side
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2564
        files.add_lines(self.get_simple_key(b'rancestor'),
2565
            self.get_parents([self.get_simple_key(b'base')]), [b'rancestor\n'])
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.
2566
        # add a child of rancestor with no eofile-nl
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2567
        files.add_lines(self.get_simple_key(b'child'),
2568
            self.get_parents([self.get_simple_key(b'rancestor')]),
2569
            [b'base\n', b'child\n'])
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.
2570
        # add a child of lancestor and base to join the two roots
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2571
        files.add_lines(self.get_simple_key(b'otherchild'),
2572
            self.get_parents([self.get_simple_key(b'lancestor'),
2573
                self.get_simple_key(b'base')]),
2574
            [b'base\n', b'lancestor\n', b'otherchild\n'])
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.
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(
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2588
            [self.get_simple_key(b'child'), self.get_simple_key(b'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
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2593
        self.assertTrue(lines[(b'child\n', self.get_simple_key(b'child'))] > 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.
2594
        self.assertTrue(
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2595
            lines[(b'otherchild\n', self.get_simple_key(b'otherchild'))] > 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.
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
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2607
        self.assertTrue(lines[(b'base\n', self.get_simple_key(b'base'))] > 0)
2608
        self.assertTrue(
2609
            lines[(b'lancestor\n', self.get_simple_key(b'lancestor'))] > 0)
2610
        self.assertTrue(
2611
            lines[(b'rancestor\n', self.get_simple_key(b'rancestor'))] > 0)
2612
        self.assertTrue(lines[(b'child\n', self.get_simple_key(b'child'))] > 0)
2613
        self.assertTrue(
2614
            lines[(b'otherchild\n', self.get_simple_key(b'otherchild'))] > 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.
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
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2622
        files.add_lines(self.get_simple_key(b'base'), [], [b'line\n'])
2623
        files.add_lines(self.get_simple_key(b'noeol'),
2624
            self.get_parents([self.get_simple_key(b'base')]), [b'line'])
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.
2625
        # detailed eol tests:
2626
        # shared last line with parent no-eol
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2627
        files.add_lines(self.get_simple_key(b'noeolsecond'),
2628
            self.get_parents([self.get_simple_key(b'noeol')]),
2629
                [b'line\n', b'line'])
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.
2630
        # differing last line with parent, both no-eol
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2631
        files.add_lines(self.get_simple_key(b'noeolnotshared'),
2632
            self.get_parents([self.get_simple_key(b'noeolsecond')]),
2633
                [b'line\n', b'phone'])
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.
2634
        # add eol following a noneol parent, change content
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2635
        files.add_lines(self.get_simple_key(b'eol'),
2636
            self.get_parents([self.get_simple_key(b'noeol')]), [b'phone\n'])
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.
2637
        # add eol following a noneol parent, no change content
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2638
        files.add_lines(self.get_simple_key(b'eolline'),
2639
            self.get_parents([self.get_simple_key(b'noeol')]), [b'line\n'])
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.
2640
        # noeol with no parents:
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2641
        files.add_lines(self.get_simple_key(b'noeolbase'), [], [b'line'])
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.
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.
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2646
        files.add_lines(self.get_simple_key(b'eolbeforefirstparent'),
2647
            self.get_parents([self.get_simple_key(b'noeolbase'),
2648
                self.get_simple_key(b'noeol')]),
2649
            [b'line'])
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.
2650
        # two identical eol texts
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2651
        files.add_lines(self.get_simple_key(b'noeoldup'),
2652
            self.get_parents([self.get_simple_key(b'noeol')]), [b'line'])
2653
        next_parent = self.get_simple_key(b'base')
2654
        text_name = b'chain1-'
2655
        text = [b'line\n']
2656
        sha1s = {0: b'da6d3141cb4a5e6f464bf6e0518042ddc7bfd079',
2657
                 1: b'45e21ea146a81ea44a821737acdb4f9791c8abe7',
2658
                 2: b'e1f11570edf3e2a070052366c582837a4fe4e9fa',
2659
                 3: b'26b4b8626da827088c514b8f9bbe4ebf181edda1',
2660
                 4: b'e28a5510be25ba84d31121cff00956f9970ae6f6',
2661
                 5: b'd63ec0ce22e11dcf65a931b69255d3ac747a318d',
2662
                 6: b'2c2888d288cb5e1d98009d822fedfe6019c6a4ea',
2663
                 7: b'95c14da9cafbf828e3e74a6f016d87926ba234ab',
2664
                 8: b'779e9a0b28f9f832528d4b21e17e168c67697272',
2665
                 9: b'1f8ff4e5c6ff78ac106fcfe6b1e8cb8740ff9a8f',
2666
                 10: b'131a2ae712cf51ed62f143e3fbac3d4206c25a05',
2667
                 11: b'c5a9d6f520d2515e1ec401a8f8a67e6c3c89f199',
2668
                 12: b'31a2286267f24d8bedaa43355f8ad7129509ea85',
2669
                 13: b'dc2a7fe80e8ec5cae920973973a8ee28b2da5e0a',
2670
                 14: b'2c4b1736566b8ca6051e668de68650686a3922f2',
2671
                 15: b'5912e4ecd9b0c07be4d013e7e2bdcf9323276cde',
2672
                 16: b'b0d2e18d3559a00580f6b49804c23fea500feab3',
2673
                 17: b'8e1d43ad72f7562d7cb8f57ee584e20eb1a69fc7',
2674
                 18: b'5cf64a3459ae28efa60239e44b20312d25b253f3',
2675
                 19: b'1ebed371807ba5935958ad0884595126e8c4e823',
2676
                 20: b'2aa62a8b06fb3b3b892a3292a068ade69d5ee0d3',
2677
                 21: b'01edc447978004f6e4e962b417a4ae1955b6fe5d',
2678
                 22: b'd8d8dc49c4bf0bab401e0298bb5ad827768618bb',
2679
                 23: b'c21f62b1c482862983a8ffb2b0c64b3451876e3f',
2680
                 24: b'c0593fe795e00dff6b3c0fe857a074364d5f04fc',
2681
                 25: b'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):
6963.2.6 by Jelmer Vernooij
Fix some more tests.
2684
            new_version = self.get_simple_key(text_name + b'%d' % depth)
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2685
            text = text + [b'line\n']
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.
2686
            files.add_lines(new_version, self.get_parents([next_parent]), text)
2687
            next_parent = new_version
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2688
        next_parent = self.get_simple_key(b'base')
2689
        text_name = b'chain2-'
2690
        text = [b'line\n']
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.
2691
        for depth in range(26):
6963.2.10 by Jelmer Vernooij
Fix a bunch of tests.
2692
            new_version = self.get_simple_key(text_name + b'%d' % depth)
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2693
            text = text + [b'line\n']
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.
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(
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2703
                next(files.get_record_stream([key], 'unordered',
2704
                    True)).get_bytes_as('fulltext'),
2705
                next(target.get_record_stream([key], 'unordered',
2706
                    True)).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.
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:
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2715
            key = (b'foo',)
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.
2716
        else:
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2717
            key = (b'foo', b'bar',)
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.
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,
6963.2.4 by Jelmer Vernooij
Add bees.
2741
                self.texts.add_lines, b"foo", [], [])
3518.1.1 by Jelmer Vernooij
Add VirtualVersionedFiles class.
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):
6963.2.4 by Jelmer Vernooij
Add bees.
2755
        self.assertEqual({}, self.texts.get_sha1s([(b"NONEXISTENT",)]))
3518.1.1 by Jelmer Vernooij
Add VirtualVersionedFiles class.
2756
2757
    def test_get_sha1s(self):
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2758
        self._lines[b"key"] = [b"dataline1", b"dataline2"]
2759
        self.assertEqual({(b"key",): osutils.sha_strings(self._lines[b"key"])},
2760
                           self.texts.get_sha1s([(b"key",)]))
3518.1.1 by Jelmer Vernooij
Add VirtualVersionedFiles class.
2761
2762
    def test_get_parent_map(self):
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2763
        self._parent_map = {b"G": (b"A", b"B")}
2764
        self.assertEqual({(b"G",): ((b"A",), (b"B",))},
2765
                          self.texts.get_parent_map([(b"G",), (b"L",)]))
3518.1.1 by Jelmer Vernooij
Add VirtualVersionedFiles class.
2766
2767
    def test_get_record_stream(self):
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2768
        self._lines[b"A"] = [b"FOO", b"BAR"]
2769
        it = self.texts.get_record_stream([(b"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)
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2772
        self.assertEqual(b"FOOBAR", record.get_bytes_as("fulltext"))
2773
        self.assertEqual([b"FOO", b"BAR"], record.get_bytes_as("chunked"))
3518.1.1 by Jelmer Vernooij
Add VirtualVersionedFiles class.
2774
2775
    def test_get_record_stream_absent(self):
6963.2.4 by Jelmer Vernooij
Add bees.
2776
        it = self.texts.get_record_stream([(b"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):
6963.2.4 by Jelmer Vernooij
Add bees.
2781
        self._lines[b"A"] = [b"FOO", b"BAR"]
2782
        self._lines[b"B"] = [b"HEY"]
2783
        self._lines[b"C"] = [b"Alberta"]
2784
        it = self.texts.iter_lines_added_or_present_in_keys([(b"A",), (b"B",)])
2785
        self.assertEqual(sorted([(b"FOO", b"A"), (b"BAR", b"A"), (b"HEY", b"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, [
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2795
            ('add', ('', b'TREE_ROOT', 'directory', None))],
2796
            revision_id=b'A')
2797
        builder.build_snapshot([b'A'], [], revision_id=b'B')
2798
        builder.build_snapshot([b'B'], [], revision_id=b'C')
2799
        builder.build_snapshot([b'C'], [], revision_id=b'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):
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2812
        vf = self.get_ordering_vf({(b'A',): 3, (b'B',): 2, (b'C',): 4, (b'D',): 1})
2813
        request_keys = [(b'B',), (b'C',), (b'D',), (b'A',)]
3871.4.1 by John Arbash Meinel
Add a VFDecorator that can yield records in a specified order
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
6963.2.1 by Jelmer Vernooij
Fix more python3 tests.
2817
        self.assertEqual([(b'A',), (b'B',), (b'C',), (b'D',)], keys)
3871.4.1 by John Arbash Meinel
Add a VFDecorator that can yield records in a specified order
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):
6963.2.4 by Jelmer Vernooij
Add bees.
2823
        vf = self.get_ordering_vf({(b'A',): 3, (b'B',): 2, (b'C',): 4, (b'D',): 1})
2824
        request_keys = [(b'B',), (b'C',), (b'D',), (b'A',)]
3871.4.1 by John Arbash Meinel
Add a VFDecorator that can yield records in a specified order
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
6963.2.4 by Jelmer Vernooij
Add bees.
2828
        self.assertEqual([(b'D',), (b'B',), (b'A',), (b'C',)], keys)
3871.4.1 by John Arbash Meinel
Add a VFDecorator that can yield records in a specified order
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):
6963.2.4 by Jelmer Vernooij
Add bees.
2834
        vf = self.get_ordering_vf({(b'B',): 2, (b'D',): 1})
2835
        request_keys = [(b'B',), (b'C',), (b'D',), (b'A',)]
3871.4.1 by John Arbash Meinel
Add a VFDecorator that can yield records in a specified order
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
6963.2.4 by Jelmer Vernooij
Add bees.
2840
        self.assertEqual([(b'A',), (b'C',), (b'D',), (b'B',)], keys)
3871.4.1 by John Arbash Meinel
Add a VFDecorator that can yield records in a specified order
2841
        # And the request recorded
2842
        self.assertEqual([('get_record_stream', request_keys, 'unordered',
2843
                           False)], vf.calls)