/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/per_versionedfile.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-05-27 01:58:44 UTC
  • mfrom: (5193.4.24 gio-transport)
  • Revision ID: pqm@pqm.ubuntu.com-20100527015844-ya4jahnwjx5y8ej2
(lifeless) Add a gio based transport using gio+ as a prefix to get at the
 gio logic. (Mattias Eriksson)

Show diffs side-by-side

added added

removed removed

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