/brz/remove-bazaar

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