/brz/remove-bazaar

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