~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/per_versionedfile.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2011-05-04 12:10:51 UTC
  • mfrom: (5819.1.4 777007-developer-doc)
  • Revision ID: pqm@pqm.ubuntu.com-20110504121051-aovlsmqiivjmc4fc
(jelmer) Small fixes to developer documentation. (Jonathan Riddell)

Show diffs side-by-side

added added

removed removed

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