/brz/remove-bazaar

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