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