/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2052.3.2 by John Arbash Meinel
Change Copyright .. by Canonical to Copyright ... Canonical
1
# Copyright (C) 2005 Canonical Ltd
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
2
#
3
# Authors:
4
#   Johan Rydberg <jrydberg@gnu.org>
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
10
#
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
15
#
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20
1704.2.15 by Martin Pool
Remove TODO about knit testing printed from test suite
21
# TODO: might be nice to create a versionedfile with some type of corruption
22
# considered typical and check that it can be detected/corrected.
23
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
24
from StringIO import StringIO
25
1563.2.6 by Robert Collins
Start check tests for knits (pending), and remove dead code.
26
import bzrlib
2039.1.1 by Aaron Bentley
Clean up progress properly when interrupted during fetch (#54000)
27
from bzrlib import (
28
    errors,
29
    progress,
30
    )
1563.2.11 by Robert Collins
Consolidate reweave and join as we have no separate usage, make reweave tests apply to all versionedfile implementations and deprecate the old reweave apis.
31
from bzrlib.errors import (
32
                           RevisionNotPresent, 
33
                           RevisionAlreadyPresent,
34
                           WeaveParentMismatch
35
                           )
1563.2.6 by Robert Collins
Start check tests for knits (pending), and remove dead code.
36
from bzrlib.knit import KnitVersionedFile, \
37
     KnitAnnotateFactory
1563.2.12 by Robert Collins
Checkpointing: created InterObject to factor out common inter object worker code, added InterVersionedFile and tests to allow making join work between any versionedfile.
38
from bzrlib.tests import TestCaseWithTransport
1666.1.1 by Robert Collins
Add trivial http-using test for versioned files.
39
from bzrlib.tests.HTTPTestUtil import TestCaseWithWebserver
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
40
from bzrlib.trace import mutter
1563.2.16 by Robert Collins
Change WeaveStore into VersionedFileStore and make its versoined file class parameterisable.
41
from bzrlib.transport import get_transport
1563.2.13 by Robert Collins
InterVersionedFile implemented.
42
from bzrlib.transport.memory import MemoryTransport
1684.3.1 by Robert Collins
Fix versioned file joins with empty targets.
43
from bzrlib.tsort import topo_sort
1563.2.12 by Robert Collins
Checkpointing: created InterObject to factor out common inter object worker code, added InterVersionedFile and tests to allow making join work between any versionedfile.
44
import bzrlib.versionedfile as versionedfile
1563.2.9 by Robert Collins
Update versionedfile api tests to ensure that data is available after every operation.
45
from bzrlib.weave import WeaveFile
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
46
from bzrlib.weavefile import read_weave, write_weave
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
47
48
49
class VersionedFileTestMixIn(object):
50
    """A mixin test class for testing VersionedFiles.
51
52
    This is not an adaptor-style test at this point because
53
    theres no dynamic substitution of versioned file implementations,
54
    they are strictly controlled by their owning repositories.
55
    """
56
57
    def test_add(self):
58
        f = self.get_file()
59
        f.add_lines('r0', [], ['a\n', 'b\n'])
60
        f.add_lines('r1', ['r0'], ['b\n', 'c\n'])
1563.2.9 by Robert Collins
Update versionedfile api tests to ensure that data is available after every operation.
61
        def verify_file(f):
62
            versions = f.versions()
63
            self.assertTrue('r0' in versions)
64
            self.assertTrue('r1' in versions)
65
            self.assertEquals(f.get_lines('r0'), ['a\n', 'b\n'])
66
            self.assertEquals(f.get_text('r0'), 'a\nb\n')
67
            self.assertEquals(f.get_lines('r1'), ['b\n', 'c\n'])
1563.2.18 by Robert Collins
get knit repositories really using knits for text storage.
68
            self.assertEqual(2, len(f))
69
            self.assertEqual(2, f.num_versions())
1563.2.9 by Robert Collins
Update versionedfile api tests to ensure that data is available after every operation.
70
    
71
            self.assertRaises(RevisionNotPresent,
72
                f.add_lines, 'r2', ['foo'], [])
73
            self.assertRaises(RevisionAlreadyPresent,
74
                f.add_lines, 'r1', [], [])
75
        verify_file(f)
1666.1.6 by Robert Collins
Make knit the default format.
76
        # this checks that reopen with create=True does not break anything.
77
        f = self.reopen_file(create=True)
1563.2.9 by Robert Collins
Update versionedfile api tests to ensure that data is available after every operation.
78
        verify_file(f)
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
79
1596.2.32 by Robert Collins
Reduce re-extraction of texts during weave to knit joins by providing a memoisation facility.
80
    def test_adds_with_parent_texts(self):
81
        f = self.get_file()
82
        parent_texts = {}
83
        parent_texts['r0'] = f.add_lines('r0', [], ['a\n', 'b\n'])
84
        try:
85
            parent_texts['r1'] = f.add_lines_with_ghosts('r1',
86
                                                         ['r0', 'ghost'], 
87
                                                         ['b\n', 'c\n'],
88
                                                         parent_texts=parent_texts)
89
        except NotImplementedError:
90
            # if the format doesn't support ghosts, just add normally.
91
            parent_texts['r1'] = f.add_lines('r1',
92
                                             ['r0'], 
93
                                             ['b\n', 'c\n'],
94
                                             parent_texts=parent_texts)
95
        f.add_lines('r2', ['r1'], ['c\n', 'd\n'], parent_texts=parent_texts)
96
        self.assertNotEqual(None, parent_texts['r0'])
97
        self.assertNotEqual(None, parent_texts['r1'])
98
        def verify_file(f):
99
            versions = f.versions()
100
            self.assertTrue('r0' in versions)
101
            self.assertTrue('r1' in versions)
102
            self.assertTrue('r2' in versions)
103
            self.assertEquals(f.get_lines('r0'), ['a\n', 'b\n'])
104
            self.assertEquals(f.get_lines('r1'), ['b\n', 'c\n'])
105
            self.assertEquals(f.get_lines('r2'), ['c\n', 'd\n'])
106
            self.assertEqual(3, f.num_versions())
107
            origins = f.annotate('r1')
108
            self.assertEquals(origins[0][0], 'r0')
109
            self.assertEquals(origins[1][0], 'r1')
110
            origins = f.annotate('r2')
111
            self.assertEquals(origins[0][0], 'r1')
112
            self.assertEquals(origins[1][0], 'r2')
113
114
        verify_file(f)
115
        f = self.reopen_file()
116
        verify_file(f)
117
1666.1.6 by Robert Collins
Make knit the default format.
118
    def test_add_unicode_content(self):
119
        # unicode content is not permitted in versioned files. 
120
        # versioned files version sequences of bytes only.
121
        vf = self.get_file()
122
        self.assertRaises(errors.BzrBadParameterUnicode,
123
            vf.add_lines, 'a', [], ['a\n', u'b\n', 'c\n'])
124
        self.assertRaises(
125
            (errors.BzrBadParameterUnicode, NotImplementedError),
126
            vf.add_lines_with_ghosts, 'a', [], ['a\n', u'b\n', 'c\n'])
127
128
    def test_inline_newline_throws(self):
129
        # \r characters are not permitted in lines being added
130
        vf = self.get_file()
131
        self.assertRaises(errors.BzrBadParameterContainsNewline, 
132
            vf.add_lines, 'a', [], ['a\n\n'])
133
        self.assertRaises(
134
            (errors.BzrBadParameterContainsNewline, NotImplementedError),
135
            vf.add_lines_with_ghosts, 'a', [], ['a\n\n'])
136
        # but inline CR's are allowed
137
        vf.add_lines('a', [], ['a\r\n'])
138
        try:
139
            vf.add_lines_with_ghosts('b', [], ['a\r\n'])
140
        except NotImplementedError:
141
            pass
142
1596.2.37 by Robert Collins
Switch to delta based content copying in the generic versioned file copier.
143
    def test_get_delta(self):
1596.2.36 by Robert Collins
add a get_delta api to versioned_file.
144
        f = self.get_file()
1596.2.38 by Robert Collins
rollback from using deltas to using fulltexts - deltas need more work to be ready.
145
        sha1s = self._setup_for_deltas(f)
146
        expected_delta = (None, '6bfa09d82ce3e898ad4641ae13dd4fdb9cf0d76b', False, 
147
                          [(0, 0, 1, [('base', 'line\n')])])
148
        self.assertEqual(expected_delta, f.get_delta('base'))
149
        next_parent = 'base'
150
        text_name = 'chain1-'
151
        for depth in range(26):
152
            new_version = text_name + '%s' % depth
153
            expected_delta = (next_parent, sha1s[depth], 
154
                              False,
155
                              [(depth + 1, depth + 1, 1, [(new_version, 'line\n')])])
156
            self.assertEqual(expected_delta, f.get_delta(new_version))
157
            next_parent = new_version
158
        next_parent = 'base'
159
        text_name = 'chain2-'
160
        for depth in range(26):
161
            new_version = text_name + '%s' % depth
162
            expected_delta = (next_parent, sha1s[depth], False,
163
                              [(depth + 1, depth + 1, 1, [(new_version, 'line\n')])])
164
            self.assertEqual(expected_delta, f.get_delta(new_version))
165
            next_parent = new_version
166
        # smoke test for eol support
167
        expected_delta = ('base', '264f39cab871e4cfd65b3a002f7255888bb5ed97', True, [])
168
        self.assertEqual(['line'], f.get_lines('noeol'))
169
        self.assertEqual(expected_delta, f.get_delta('noeol'))
170
171
    def test_get_deltas(self):
172
        f = self.get_file()
173
        sha1s = self._setup_for_deltas(f)
174
        deltas = f.get_deltas(f.versions())
175
        expected_delta = (None, '6bfa09d82ce3e898ad4641ae13dd4fdb9cf0d76b', False, 
176
                          [(0, 0, 1, [('base', 'line\n')])])
177
        self.assertEqual(expected_delta, deltas['base'])
178
        next_parent = 'base'
179
        text_name = 'chain1-'
180
        for depth in range(26):
181
            new_version = text_name + '%s' % depth
182
            expected_delta = (next_parent, sha1s[depth], 
183
                              False,
184
                              [(depth + 1, depth + 1, 1, [(new_version, 'line\n')])])
185
            self.assertEqual(expected_delta, deltas[new_version])
186
            next_parent = new_version
187
        next_parent = 'base'
188
        text_name = 'chain2-'
189
        for depth in range(26):
190
            new_version = text_name + '%s' % depth
191
            expected_delta = (next_parent, sha1s[depth], False,
192
                              [(depth + 1, depth + 1, 1, [(new_version, 'line\n')])])
193
            self.assertEqual(expected_delta, deltas[new_version])
194
            next_parent = new_version
195
        # smoke tests for eol support
196
        expected_delta = ('base', '264f39cab871e4cfd65b3a002f7255888bb5ed97', True, [])
197
        self.assertEqual(['line'], f.get_lines('noeol'))
198
        self.assertEqual(expected_delta, deltas['noeol'])
199
        # smoke tests for eol support - two noeol in a row same content
200
        expected_deltas = (('noeol', '3ad7ee82dbd8f29ecba073f96e43e414b3f70a4d', True, 
201
                          [(0, 1, 2, [(u'noeolsecond', 'line\n'), (u'noeolsecond', 'line\n')])]),
202
                          ('noeol', '3ad7ee82dbd8f29ecba073f96e43e414b3f70a4d', True, 
203
                           [(0, 0, 1, [('noeolsecond', 'line\n')]), (1, 1, 0, [])]))
204
        self.assertEqual(['line\n', 'line'], f.get_lines('noeolsecond'))
205
        self.assertTrue(deltas['noeolsecond'] in expected_deltas)
206
        # two no-eol in a row, different content
207
        expected_delta = ('noeolsecond', '8bb553a84e019ef1149db082d65f3133b195223b', True, 
208
                          [(1, 2, 1, [(u'noeolnotshared', 'phone\n')])])
209
        self.assertEqual(['line\n', 'phone'], f.get_lines('noeolnotshared'))
210
        self.assertEqual(expected_delta, deltas['noeolnotshared'])
211
        # eol folling a no-eol with content change
212
        expected_delta = ('noeol', 'a61f6fb6cfc4596e8d88c34a308d1e724caf8977', False, 
213
                          [(0, 1, 1, [(u'eol', 'phone\n')])])
214
        self.assertEqual(['phone\n'], f.get_lines('eol'))
215
        self.assertEqual(expected_delta, deltas['eol'])
216
        # eol folling a no-eol with content change
217
        expected_delta = ('noeol', '6bfa09d82ce3e898ad4641ae13dd4fdb9cf0d76b', False, 
218
                          [(0, 1, 1, [(u'eolline', 'line\n')])])
219
        self.assertEqual(['line\n'], f.get_lines('eolline'))
220
        self.assertEqual(expected_delta, deltas['eolline'])
221
        # eol with no parents
222
        expected_delta = (None, '264f39cab871e4cfd65b3a002f7255888bb5ed97', True, 
223
                          [(0, 0, 1, [(u'noeolbase', 'line\n')])])
224
        self.assertEqual(['line'], f.get_lines('noeolbase'))
225
        self.assertEqual(expected_delta, deltas['noeolbase'])
226
        # eol with two parents, in inverse insertion order
227
        expected_deltas = (('noeolbase', '264f39cab871e4cfd65b3a002f7255888bb5ed97', True,
228
                            [(0, 1, 1, [(u'eolbeforefirstparent', 'line\n')])]),
229
                           ('noeolbase', '264f39cab871e4cfd65b3a002f7255888bb5ed97', True,
230
                            [(0, 1, 1, [(u'eolbeforefirstparent', 'line\n')])]))
231
        self.assertEqual(['line'], f.get_lines('eolbeforefirstparent'))
232
        #self.assertTrue(deltas['eolbeforefirstparent'] in expected_deltas)
233
234
    def _setup_for_deltas(self, f):
1596.2.36 by Robert Collins
add a get_delta api to versioned_file.
235
        self.assertRaises(errors.RevisionNotPresent, f.get_delta, 'base')
236
        # add texts that should trip the knit maximum delta chain threshold
237
        # as well as doing parallel chains of data in knits.
238
        # this is done by two chains of 25 insertions
239
        f.add_lines('base', [], ['line\n'])
1596.2.38 by Robert Collins
rollback from using deltas to using fulltexts - deltas need more work to be ready.
240
        f.add_lines('noeol', ['base'], ['line'])
241
        # detailed eol tests:
242
        # shared last line with parent no-eol
243
        f.add_lines('noeolsecond', ['noeol'], ['line\n', 'line'])
244
        # differing last line with parent, both no-eol
245
        f.add_lines('noeolnotshared', ['noeolsecond'], ['line\n', 'phone'])
246
        # add eol following a noneol parent, change content
247
        f.add_lines('eol', ['noeol'], ['phone\n'])
248
        # add eol following a noneol parent, no change content
249
        f.add_lines('eolline', ['noeol'], ['line\n'])
250
        # noeol with no parents:
251
        f.add_lines('noeolbase', [], ['line'])
252
        # noeol preceeding its leftmost parent in the output:
253
        # this is done by making it a merge of two parents with no common
254
        # anestry: noeolbase and noeol with the 
255
        # later-inserted parent the leftmost.
256
        f.add_lines('eolbeforefirstparent', ['noeolbase', 'noeol'], ['line'])
257
        # two identical eol texts
258
        f.add_lines('noeoldup', ['noeol'], ['line'])
1596.2.36 by Robert Collins
add a get_delta api to versioned_file.
259
        next_parent = 'base'
260
        text_name = 'chain1-'
261
        text = ['line\n']
262
        sha1s = {0 :'da6d3141cb4a5e6f464bf6e0518042ddc7bfd079',
263
                 1 :'45e21ea146a81ea44a821737acdb4f9791c8abe7',
264
                 2 :'e1f11570edf3e2a070052366c582837a4fe4e9fa',
265
                 3 :'26b4b8626da827088c514b8f9bbe4ebf181edda1',
266
                 4 :'e28a5510be25ba84d31121cff00956f9970ae6f6',
267
                 5 :'d63ec0ce22e11dcf65a931b69255d3ac747a318d',
268
                 6 :'2c2888d288cb5e1d98009d822fedfe6019c6a4ea',
269
                 7 :'95c14da9cafbf828e3e74a6f016d87926ba234ab',
270
                 8 :'779e9a0b28f9f832528d4b21e17e168c67697272',
271
                 9 :'1f8ff4e5c6ff78ac106fcfe6b1e8cb8740ff9a8f',
272
                 10:'131a2ae712cf51ed62f143e3fbac3d4206c25a05',
273
                 11:'c5a9d6f520d2515e1ec401a8f8a67e6c3c89f199',
274
                 12:'31a2286267f24d8bedaa43355f8ad7129509ea85',
275
                 13:'dc2a7fe80e8ec5cae920973973a8ee28b2da5e0a',
276
                 14:'2c4b1736566b8ca6051e668de68650686a3922f2',
277
                 15:'5912e4ecd9b0c07be4d013e7e2bdcf9323276cde',
278
                 16:'b0d2e18d3559a00580f6b49804c23fea500feab3',
279
                 17:'8e1d43ad72f7562d7cb8f57ee584e20eb1a69fc7',
280
                 18:'5cf64a3459ae28efa60239e44b20312d25b253f3',
281
                 19:'1ebed371807ba5935958ad0884595126e8c4e823',
282
                 20:'2aa62a8b06fb3b3b892a3292a068ade69d5ee0d3',
283
                 21:'01edc447978004f6e4e962b417a4ae1955b6fe5d',
284
                 22:'d8d8dc49c4bf0bab401e0298bb5ad827768618bb',
285
                 23:'c21f62b1c482862983a8ffb2b0c64b3451876e3f',
286
                 24:'c0593fe795e00dff6b3c0fe857a074364d5f04fc',
287
                 25:'dd1a1cf2ba9cc225c3aff729953e6364bf1d1855',
288
                 }
289
        for depth in range(26):
290
            new_version = text_name + '%s' % depth
291
            text = text + ['line\n']
292
            f.add_lines(new_version, [next_parent], text)
293
            next_parent = new_version
294
        next_parent = 'base'
295
        text_name = 'chain2-'
296
        text = ['line\n']
297
        for depth in range(26):
298
            new_version = text_name + '%s' % depth
299
            text = text + ['line\n']
300
            f.add_lines(new_version, [next_parent], text)
301
            next_parent = new_version
1596.2.38 by Robert Collins
rollback from using deltas to using fulltexts - deltas need more work to be ready.
302
        return sha1s
1596.2.37 by Robert Collins
Switch to delta based content copying in the generic versioned file copier.
303
304
    def test_add_delta(self):
305
        # tests for the add-delta facility.
306
        # at this point, optimising for speed, we assume no checks when deltas are inserted.
307
        # this may need to be revisited.
308
        source = self.get_file('source')
309
        source.add_lines('base', [], ['line\n'])
310
        next_parent = 'base'
311
        text_name = 'chain1-'
312
        text = ['line\n']
313
        for depth in range(26):
314
            new_version = text_name + '%s' % depth
315
            text = text + ['line\n']
316
            source.add_lines(new_version, [next_parent], text)
317
            next_parent = new_version
318
        next_parent = 'base'
319
        text_name = 'chain2-'
320
        text = ['line\n']
321
        for depth in range(26):
322
            new_version = text_name + '%s' % depth
323
            text = text + ['line\n']
324
            source.add_lines(new_version, [next_parent], text)
325
            next_parent = new_version
326
        source.add_lines('noeol', ['base'], ['line'])
327
        
328
        target = self.get_file('target')
329
        for version in source.versions():
330
            parent, sha1, noeol, delta = source.get_delta(version)
331
            target.add_delta(version,
332
                             source.get_parents(version),
333
                             parent,
334
                             sha1,
335
                             noeol,
336
                             delta)
337
        self.assertRaises(RevisionAlreadyPresent,
338
                          target.add_delta, 'base', [], None, '', False, [])
339
        for version in source.versions():
340
            self.assertEqual(source.get_lines(version),
341
                             target.get_lines(version))
1596.2.36 by Robert Collins
add a get_delta api to versioned_file.
342
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
343
    def test_ancestry(self):
344
        f = self.get_file()
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
345
        self.assertEqual([], f.get_ancestry([]))
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
346
        f.add_lines('r0', [], ['a\n', 'b\n'])
347
        f.add_lines('r1', ['r0'], ['b\n', 'c\n'])
348
        f.add_lines('r2', ['r0'], ['b\n', 'c\n'])
349
        f.add_lines('r3', ['r2'], ['b\n', 'c\n'])
350
        f.add_lines('rM', ['r1', 'r2'], ['b\n', 'c\n'])
1563.2.29 by Robert Collins
Remove all but fetch references to repository.revision_store.
351
        self.assertEqual([], f.get_ancestry([]))
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
352
        versions = f.get_ancestry(['rM'])
353
        # there are some possibilities:
354
        # r0 r1 r2 rM r3
355
        # r0 r1 r2 r3 rM
356
        # etc
357
        # so we check indexes
358
        r0 = versions.index('r0')
359
        r1 = versions.index('r1')
360
        r2 = versions.index('r2')
361
        self.assertFalse('r3' in versions)
362
        rM = versions.index('rM')
363
        self.assertTrue(r0 < r1)
364
        self.assertTrue(r0 < r2)
365
        self.assertTrue(r1 < rM)
366
        self.assertTrue(r2 < rM)
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
367
368
        self.assertRaises(RevisionNotPresent,
369
            f.get_ancestry, ['rM', 'rX'])
1594.2.21 by Robert Collins
Teach versioned files to prevent mutation after finishing.
370
371
    def test_mutate_after_finish(self):
372
        f = self.get_file()
373
        f.transaction_finished()
1596.2.37 by Robert Collins
Switch to delta based content copying in the generic versioned file copier.
374
        self.assertRaises(errors.OutSideTransaction, f.add_delta, '', [], '', '', False, [])
1594.2.21 by Robert Collins
Teach versioned files to prevent mutation after finishing.
375
        self.assertRaises(errors.OutSideTransaction, f.add_lines, '', [], [])
376
        self.assertRaises(errors.OutSideTransaction, f.add_lines_with_ghosts, '', [], [])
377
        self.assertRaises(errors.OutSideTransaction, f.fix_parents, '', [])
378
        self.assertRaises(errors.OutSideTransaction, f.join, '')
1594.2.24 by Robert Collins
Make use of the transaction finalisation warning support to implement in-knit caching.
379
        self.assertRaises(errors.OutSideTransaction, f.clone_text, 'base', 'bar', ['foo'])
1563.2.7 by Robert Collins
add versioned file clear_cache entry.
380
        
381
    def test_clear_cache(self):
382
        f = self.get_file()
383
        # on a new file it should not error
384
        f.clear_cache()
385
        # and after adding content, doing a clear_cache and a get should work.
386
        f.add_lines('0', [], ['a'])
387
        f.clear_cache()
388
        self.assertEqual(['a'], f.get_lines('0'))
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
389
390
    def test_clone_text(self):
391
        f = self.get_file()
392
        f.add_lines('r0', [], ['a\n', 'b\n'])
1563.2.5 by Robert Collins
Remove unused transaction references from knit.py and the versionedfile interface.
393
        f.clone_text('r1', 'r0', ['r0'])
1563.2.9 by Robert Collins
Update versionedfile api tests to ensure that data is available after every operation.
394
        def verify_file(f):
395
            self.assertEquals(f.get_lines('r1'), f.get_lines('r0'))
396
            self.assertEquals(f.get_lines('r1'), ['a\n', 'b\n'])
397
            self.assertEquals(f.get_parents('r1'), ['r0'])
398
    
399
            self.assertRaises(RevisionNotPresent,
400
                f.clone_text, 'r2', 'rX', [])
401
            self.assertRaises(RevisionAlreadyPresent,
402
                f.clone_text, 'r1', 'r0', [])
403
        verify_file(f)
404
        verify_file(self.reopen_file())
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
405
1563.2.13 by Robert Collins
InterVersionedFile implemented.
406
    def test_create_empty(self):
407
        f = self.get_file()
408
        f.add_lines('0', [], ['a\n'])
409
        new_f = f.create_empty('t', MemoryTransport())
410
        # smoke test, specific types should check it is honoured correctly for
411
        # non type attributes
412
        self.assertEqual([], new_f.versions())
413
        self.assertTrue(isinstance(new_f, f.__class__))
414
1563.2.15 by Robert Collins
remove the weavestore assumptions about the number and nature of files it manages.
415
    def test_copy_to(self):
416
        f = self.get_file()
417
        f.add_lines('0', [], ['a\n'])
418
        t = MemoryTransport()
419
        f.copy_to('foo', t)
420
        for suffix in f.__class__.get_suffixes():
421
            self.assertTrue(t.has('foo' + suffix))
422
423
    def test_get_suffixes(self):
424
        f = self.get_file()
425
        # should be the same
426
        self.assertEqual(f.__class__.get_suffixes(), f.__class__.get_suffixes())
427
        # and should be a list
428
        self.assertTrue(isinstance(f.__class__.get_suffixes(), list))
429
1684.3.1 by Robert Collins
Fix versioned file joins with empty targets.
430
    def build_graph(self, file, graph):
431
        for node in topo_sort(graph.items()):
432
            file.add_lines(node, graph[node], [])
433
1563.2.13 by Robert Collins
InterVersionedFile implemented.
434
    def test_get_graph(self):
435
        f = self.get_file()
1684.3.1 by Robert Collins
Fix versioned file joins with empty targets.
436
        graph = {
437
            'v1': [],
438
            'v2': ['v1'],
439
            'v3': ['v2']}
440
        self.build_graph(f, graph)
441
        self.assertEqual(graph, f.get_graph())
442
    
443
    def test_get_graph_partial(self):
444
        f = self.get_file()
445
        complex_graph = {}
446
        simple_a = {
447
            'c': [],
448
            'b': ['c'],
449
            'a': ['b'],
450
            }
451
        complex_graph.update(simple_a)
452
        simple_b = {
453
            'c': [],
454
            'b': ['c'],
455
            }
456
        complex_graph.update(simple_b)
457
        simple_gam = {
458
            'c': [],
459
            'oo': [],
460
            'bar': ['oo', 'c'],
461
            'gam': ['bar'],
462
            }
463
        complex_graph.update(simple_gam)
464
        simple_b_gam = {}
465
        simple_b_gam.update(simple_gam)
466
        simple_b_gam.update(simple_b)
467
        self.build_graph(f, complex_graph)
468
        self.assertEqual(simple_a, f.get_graph(['a']))
469
        self.assertEqual(simple_b, f.get_graph(['b']))
470
        self.assertEqual(simple_gam, f.get_graph(['gam']))
471
        self.assertEqual(simple_b_gam, f.get_graph(['b', 'gam']))
1563.2.13 by Robert Collins
InterVersionedFile implemented.
472
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
473
    def test_get_parents(self):
474
        f = self.get_file()
475
        f.add_lines('r0', [], ['a\n', 'b\n'])
476
        f.add_lines('r1', [], ['a\n', 'b\n'])
477
        f.add_lines('r2', [], ['a\n', 'b\n'])
478
        f.add_lines('r3', [], ['a\n', 'b\n'])
479
        f.add_lines('m', ['r0', 'r1', 'r2', 'r3'], ['a\n', 'b\n'])
480
        self.assertEquals(f.get_parents('m'), ['r0', 'r1', 'r2', 'r3'])
481
482
        self.assertRaises(RevisionNotPresent,
483
            f.get_parents, 'y')
484
485
    def test_annotate(self):
486
        f = self.get_file()
487
        f.add_lines('r0', [], ['a\n', 'b\n'])
488
        f.add_lines('r1', ['r0'], ['c\n', 'b\n'])
489
        origins = f.annotate('r1')
490
        self.assertEquals(origins[0][0], 'r1')
491
        self.assertEquals(origins[1][0], 'r0')
492
493
        self.assertRaises(RevisionNotPresent,
494
            f.annotate, 'foo')
495
496
    def test_walk(self):
1563.2.35 by Robert Collins
cleanup deprecation warnings and finish conversion so the inventory is knit based too.
497
        # tests that walk returns all the inclusions for the requested
498
        # revisions as well as the revisions changes themselves.
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
499
        f = self.get_file('1')
500
        f.add_lines('r0', [], ['a\n', 'b\n'])
501
        f.add_lines('r1', ['r0'], ['c\n', 'b\n'])
502
        f.add_lines('rX', ['r1'], ['d\n', 'b\n'])
503
        f.add_lines('rY', ['r1'], ['c\n', 'e\n'])
504
505
        lines = {}
506
        for lineno, insert, dset, text in f.walk(['rX', 'rY']):
507
            lines[text] = (insert, dset)
508
509
        self.assertTrue(lines['a\n'], ('r0', set(['r1'])))
510
        self.assertTrue(lines['b\n'], ('r0', set(['rY'])))
511
        self.assertTrue(lines['c\n'], ('r1', set(['rX'])))
512
        self.assertTrue(lines['d\n'], ('rX', set([])))
513
        self.assertTrue(lines['e\n'], ('rY', set([])))
514
1563.2.6 by Robert Collins
Start check tests for knits (pending), and remove dead code.
515
    def test_detection(self):
516
        # Test weaves detect corruption.
517
        #
518
        # Weaves contain a checksum of their texts.
519
        # When a text is extracted, this checksum should be
520
        # verified.
521
522
        w = self.get_file_corrupted_text()
523
524
        self.assertEqual('hello\n', w.get_text('v1'))
525
        self.assertRaises(errors.WeaveInvalidChecksum, w.get_text, 'v2')
526
        self.assertRaises(errors.WeaveInvalidChecksum, w.get_lines, 'v2')
527
        self.assertRaises(errors.WeaveInvalidChecksum, w.check)
528
529
        w = self.get_file_corrupted_checksum()
530
531
        self.assertEqual('hello\n', w.get_text('v1'))
532
        self.assertRaises(errors.WeaveInvalidChecksum, w.get_text, 'v2')
533
        self.assertRaises(errors.WeaveInvalidChecksum, w.get_lines, 'v2')
534
        self.assertRaises(errors.WeaveInvalidChecksum, w.check)
535
536
    def get_file_corrupted_text(self):
537
        """Return a versioned file with corrupt text but valid metadata."""
538
        raise NotImplementedError(self.get_file_corrupted_text)
539
1563.2.9 by Robert Collins
Update versionedfile api tests to ensure that data is available after every operation.
540
    def reopen_file(self, name='foo'):
541
        """Open the versioned file from disk again."""
542
        raise NotImplementedError(self.reopen_file)
543
1594.2.6 by Robert Collins
Introduce a api specifically for looking at lines in some versions of the inventory, for fileid_involved.
544
    def test_iter_lines_added_or_present_in_versions(self):
545
        # test that we get at least an equalset of the lines added by
546
        # versions in the weave 
547
        # the ordering here is to make a tree so that dumb searches have
548
        # more changes to muck up.
2039.1.1 by Aaron Bentley
Clean up progress properly when interrupted during fetch (#54000)
549
550
        class InstrumentedProgress(progress.DummyProgress):
551
552
            def __init__(self):
553
554
                progress.DummyProgress.__init__(self)
555
                self.updates = []
556
557
            def update(self, msg=None, current=None, total=None):
558
                self.updates.append((msg, current, total))
559
1594.2.6 by Robert Collins
Introduce a api specifically for looking at lines in some versions of the inventory, for fileid_involved.
560
        vf = self.get_file()
561
        # add a base to get included
562
        vf.add_lines('base', [], ['base\n'])
563
        # add a ancestor to be included on one side
564
        vf.add_lines('lancestor', [], ['lancestor\n'])
565
        # add a ancestor to be included on the other side
566
        vf.add_lines('rancestor', ['base'], ['rancestor\n'])
567
        # add a child of rancestor with no eofile-nl
568
        vf.add_lines('child', ['rancestor'], ['base\n', 'child\n'])
569
        # add a child of lancestor and base to join the two roots
570
        vf.add_lines('otherchild',
571
                     ['lancestor', 'base'],
572
                     ['base\n', 'lancestor\n', 'otherchild\n'])
2039.1.1 by Aaron Bentley
Clean up progress properly when interrupted during fetch (#54000)
573
        def iter_with_versions(versions, expected):
1594.2.6 by Robert Collins
Introduce a api specifically for looking at lines in some versions of the inventory, for fileid_involved.
574
            # now we need to see what lines are returned, and how often.
575
            lines = {'base\n':0,
576
                     'lancestor\n':0,
577
                     'rancestor\n':0,
578
                     'child\n':0,
579
                     'otherchild\n':0,
580
                     }
2039.1.1 by Aaron Bentley
Clean up progress properly when interrupted during fetch (#54000)
581
            progress = InstrumentedProgress()
1594.2.6 by Robert Collins
Introduce a api specifically for looking at lines in some versions of the inventory, for fileid_involved.
582
            # iterate over the lines
2039.1.1 by Aaron Bentley
Clean up progress properly when interrupted during fetch (#54000)
583
            for line in vf.iter_lines_added_or_present_in_versions(versions, 
584
                pb=progress):
1594.2.6 by Robert Collins
Introduce a api specifically for looking at lines in some versions of the inventory, for fileid_involved.
585
                lines[line] += 1
2039.1.2 by Aaron Bentley
Tweak test to avoid catching assert
586
            if []!= progress.updates: 
587
                self.assertEqual(expected, progress.updates)
1594.2.6 by Robert Collins
Introduce a api specifically for looking at lines in some versions of the inventory, for fileid_involved.
588
            return lines
2147.1.3 by John Arbash Meinel
In knit.py we were re-using a variable in 2 loops, causing bogus progress messages to be generated.
589
        lines = iter_with_versions(['child', 'otherchild'],
590
                                   [('Walking content.', 0, 2),
591
                                    ('Walking content.', 1, 2),
2039.1.1 by Aaron Bentley
Clean up progress properly when interrupted during fetch (#54000)
592
                                    ('Walking content.', 2, 2)])
1594.2.6 by Robert Collins
Introduce a api specifically for looking at lines in some versions of the inventory, for fileid_involved.
593
        # we must see child and otherchild
594
        self.assertTrue(lines['child\n'] > 0)
595
        self.assertTrue(lines['otherchild\n'] > 0)
596
        # we dont care if we got more than that.
597
        
598
        # test all lines
2147.1.3 by John Arbash Meinel
In knit.py we were re-using a variable in 2 loops, causing bogus progress messages to be generated.
599
        lines = iter_with_versions(None, [('Walking content.', 0, 5),
600
                                          ('Walking content.', 1, 5),
601
                                          ('Walking content.', 2, 5),
602
                                          ('Walking content.', 3, 5),
603
                                          ('Walking content.', 4, 5),
2039.1.1 by Aaron Bentley
Clean up progress properly when interrupted during fetch (#54000)
604
                                          ('Walking content.', 5, 5)])
1594.2.6 by Robert Collins
Introduce a api specifically for looking at lines in some versions of the inventory, for fileid_involved.
605
        # all lines must be seen at least once
606
        self.assertTrue(lines['base\n'] > 0)
607
        self.assertTrue(lines['lancestor\n'] > 0)
608
        self.assertTrue(lines['rancestor\n'] > 0)
609
        self.assertTrue(lines['child\n'] > 0)
610
        self.assertTrue(lines['otherchild\n'] > 0)
1594.2.7 by Robert Collins
Add versionedfile.fix_parents api for correcting data post hoc.
611
612
    def test_fix_parents(self):
613
        # some versioned files allow incorrect parents to be corrected after
614
        # insertion - this may not fix ancestry..
615
        # if they do not supported, they just do not implement it.
1594.2.8 by Robert Collins
add ghost aware apis to knits.
616
        # we test this as an interface test to ensure that those that *do*
617
        # implementent it get it right.
1594.2.7 by Robert Collins
Add versionedfile.fix_parents api for correcting data post hoc.
618
        vf = self.get_file()
619
        vf.add_lines('notbase', [], [])
620
        vf.add_lines('base', [], [])
621
        try:
622
            vf.fix_parents('notbase', ['base'])
623
        except NotImplementedError:
624
            return
625
        self.assertEqual(['base'], vf.get_parents('notbase'))
626
        # open again, check it stuck.
627
        vf = self.get_file()
628
        self.assertEqual(['base'], vf.get_parents('notbase'))
629
1594.2.8 by Robert Collins
add ghost aware apis to knits.
630
    def test_fix_parents_with_ghosts(self):
631
        # when fixing parents, ghosts that are listed should not be ghosts
632
        # anymore.
633
        vf = self.get_file()
634
635
        try:
636
            vf.add_lines_with_ghosts('notbase', ['base', 'stillghost'], [])
637
        except NotImplementedError:
638
            return
639
        vf.add_lines('base', [], [])
640
        vf.fix_parents('notbase', ['base', 'stillghost'])
641
        self.assertEqual(['base'], vf.get_parents('notbase'))
642
        # open again, check it stuck.
643
        vf = self.get_file()
644
        self.assertEqual(['base'], vf.get_parents('notbase'))
645
        # and check the ghosts
646
        self.assertEqual(['base', 'stillghost'],
647
                         vf.get_parents_with_ghosts('notbase'))
648
649
    def test_add_lines_with_ghosts(self):
650
        # some versioned file formats allow lines to be added with parent
651
        # information that is > than that in the format. Formats that do
652
        # not support this need to raise NotImplementedError on the
653
        # add_lines_with_ghosts api.
654
        vf = self.get_file()
655
        # add a revision with ghost parents
656
        try:
1596.2.9 by Robert Collins
Utf8 safety in knit indexes.
657
            vf.add_lines_with_ghosts(u'notbxbfse', [u'b\xbfse'], [])
1594.2.8 by Robert Collins
add ghost aware apis to knits.
658
        except NotImplementedError:
659
            # check the other ghost apis are also not implemented
660
            self.assertRaises(NotImplementedError, vf.has_ghost, 'foo')
661
            self.assertRaises(NotImplementedError, vf.get_ancestry_with_ghosts, ['foo'])
662
            self.assertRaises(NotImplementedError, vf.get_parents_with_ghosts, 'foo')
663
            self.assertRaises(NotImplementedError, vf.get_graph_with_ghosts)
664
            return
2150.2.1 by Robert Collins
Correctly decode utf8 revision ids from knits when parsing, fixes a regression where a unicode revision id is stored correctly, but then indexed by the utf8 value on the next invocation of bzr, rather than the unicode value.
665
        vf = self.reopen_file()
1594.2.8 by Robert Collins
add ghost aware apis to knits.
666
        # test key graph related apis: getncestry, _graph, get_parents
667
        # has_version
668
        # - these are ghost unaware and must not be reflect ghosts
1596.2.9 by Robert Collins
Utf8 safety in knit indexes.
669
        self.assertEqual([u'notbxbfse'], vf.get_ancestry(u'notbxbfse'))
670
        self.assertEqual([], vf.get_parents(u'notbxbfse'))
671
        self.assertEqual({u'notbxbfse':[]}, vf.get_graph())
672
        self.assertFalse(vf.has_version(u'b\xbfse'))
1594.2.8 by Robert Collins
add ghost aware apis to knits.
673
        # we have _with_ghost apis to give us ghost information.
1596.2.9 by Robert Collins
Utf8 safety in knit indexes.
674
        self.assertEqual([u'b\xbfse', u'notbxbfse'], vf.get_ancestry_with_ghosts([u'notbxbfse']))
675
        self.assertEqual([u'b\xbfse'], vf.get_parents_with_ghosts(u'notbxbfse'))
676
        self.assertEqual({u'notbxbfse':[u'b\xbfse']}, vf.get_graph_with_ghosts())
677
        self.assertTrue(vf.has_ghost(u'b\xbfse'))
1594.2.8 by Robert Collins
add ghost aware apis to knits.
678
        # if we add something that is a ghost of another, it should correct the
679
        # results of the prior apis
1596.2.9 by Robert Collins
Utf8 safety in knit indexes.
680
        vf.add_lines(u'b\xbfse', [], [])
681
        self.assertEqual([u'b\xbfse', u'notbxbfse'], vf.get_ancestry([u'notbxbfse']))
682
        self.assertEqual([u'b\xbfse'], vf.get_parents(u'notbxbfse'))
683
        self.assertEqual({u'b\xbfse':[],
684
                          u'notbxbfse':[u'b\xbfse'],
1594.2.8 by Robert Collins
add ghost aware apis to knits.
685
                          },
686
                         vf.get_graph())
1596.2.9 by Robert Collins
Utf8 safety in knit indexes.
687
        self.assertTrue(vf.has_version(u'b\xbfse'))
1594.2.8 by Robert Collins
add ghost aware apis to knits.
688
        # we have _with_ghost apis to give us ghost information.
1596.2.9 by Robert Collins
Utf8 safety in knit indexes.
689
        self.assertEqual([u'b\xbfse', u'notbxbfse'], vf.get_ancestry_with_ghosts([u'notbxbfse']))
690
        self.assertEqual([u'b\xbfse'], vf.get_parents_with_ghosts(u'notbxbfse'))
691
        self.assertEqual({u'b\xbfse':[],
692
                          u'notbxbfse':[u'b\xbfse'],
1594.2.8 by Robert Collins
add ghost aware apis to knits.
693
                          },
694
                         vf.get_graph_with_ghosts())
1596.2.9 by Robert Collins
Utf8 safety in knit indexes.
695
        self.assertFalse(vf.has_ghost(u'b\xbfse'))
1594.2.8 by Robert Collins
add ghost aware apis to knits.
696
1594.2.9 by Robert Collins
Teach Knit repositories how to handle ghosts without corrupting at all.
697
    def test_add_lines_with_ghosts_after_normal_revs(self):
698
        # some versioned file formats allow lines to be added with parent
699
        # information that is > than that in the format. Formats that do
700
        # not support this need to raise NotImplementedError on the
701
        # add_lines_with_ghosts api.
702
        vf = self.get_file()
703
        # probe for ghost support
704
        try:
705
            vf.has_ghost('hoo')
706
        except NotImplementedError:
707
            return
708
        vf.add_lines_with_ghosts('base', [], ['line\n', 'line_b\n'])
709
        vf.add_lines_with_ghosts('references_ghost',
710
                                 ['base', 'a_ghost'],
711
                                 ['line\n', 'line_b\n', 'line_c\n'])
712
        origins = vf.annotate('references_ghost')
713
        self.assertEquals(('base', 'line\n'), origins[0])
714
        self.assertEquals(('base', 'line_b\n'), origins[1])
715
        self.assertEquals(('references_ghost', 'line_c\n'), origins[2])
1594.2.23 by Robert Collins
Test versioned file storage handling of clean/dirty status for accessed versioned files.
716
717
    def test_readonly_mode(self):
718
        transport = get_transport(self.get_url('.'))
719
        factory = self.get_factory()
720
        vf = factory('id', transport, 0777, create=True, access_mode='w')
721
        vf = factory('id', transport, access_mode='r')
1596.2.37 by Robert Collins
Switch to delta based content copying in the generic versioned file copier.
722
        self.assertRaises(errors.ReadOnlyError, vf.add_delta, '', [], '', '', False, [])
1594.2.23 by Robert Collins
Test versioned file storage handling of clean/dirty status for accessed versioned files.
723
        self.assertRaises(errors.ReadOnlyError, vf.add_lines, 'base', [], [])
724
        self.assertRaises(errors.ReadOnlyError,
725
                          vf.add_lines_with_ghosts,
726
                          'base',
727
                          [],
728
                          [])
729
        self.assertRaises(errors.ReadOnlyError, vf.fix_parents, 'base', [])
730
        self.assertRaises(errors.ReadOnlyError, vf.join, 'base')
1594.2.24 by Robert Collins
Make use of the transaction finalisation warning support to implement in-knit caching.
731
        self.assertRaises(errors.ReadOnlyError, vf.clone_text, 'base', 'bar', ['foo'])
1666.1.6 by Robert Collins
Make knit the default format.
732
    
733
    def test_get_sha1(self):
734
        # check the sha1 data is available
735
        vf = self.get_file()
736
        # a simple file
737
        vf.add_lines('a', [], ['a\n'])
738
        # the same file, different metadata
739
        vf.add_lines('b', ['a'], ['a\n'])
740
        # a file differing only in last newline.
741
        vf.add_lines('c', [], ['a'])
742
        self.assertEqual(
743
            '3f786850e387550fdab836ed7e6dc881de23001b', vf.get_sha1('a'))
744
        self.assertEqual(
745
            '3f786850e387550fdab836ed7e6dc881de23001b', vf.get_sha1('b'))
746
        self.assertEqual(
747
            '86f7e437faa5a7fce15d1ddcb9eaeaea377667b8', vf.get_sha1('c'))
1594.2.9 by Robert Collins
Teach Knit repositories how to handle ghosts without corrupting at all.
748
        
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
749
1563.2.12 by Robert Collins
Checkpointing: created InterObject to factor out common inter object worker code, added InterVersionedFile and tests to allow making join work between any versionedfile.
750
class TestWeave(TestCaseWithTransport, VersionedFileTestMixIn):
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
751
752
    def get_file(self, name='foo'):
1563.2.25 by Robert Collins
Merge in upstream.
753
        return WeaveFile(name, get_transport(self.get_url('.')), create=True)
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
754
1563.2.6 by Robert Collins
Start check tests for knits (pending), and remove dead code.
755
    def get_file_corrupted_text(self):
1563.2.25 by Robert Collins
Merge in upstream.
756
        w = WeaveFile('foo', get_transport(self.get_url('.')), create=True)
1563.2.13 by Robert Collins
InterVersionedFile implemented.
757
        w.add_lines('v1', [], ['hello\n'])
758
        w.add_lines('v2', ['v1'], ['hello\n', 'there\n'])
1563.2.6 by Robert Collins
Start check tests for knits (pending), and remove dead code.
759
        
760
        # We are going to invasively corrupt the text
761
        # Make sure the internals of weave are the same
762
        self.assertEqual([('{', 0)
763
                        , 'hello\n'
764
                        , ('}', None)
765
                        , ('{', 1)
766
                        , 'there\n'
767
                        , ('}', None)
768
                        ], w._weave)
769
        
770
        self.assertEqual(['f572d396fae9206628714fb2ce00f72e94f2258f'
771
                        , '90f265c6e75f1c8f9ab76dcf85528352c5f215ef'
772
                        ], w._sha1s)
773
        w.check()
774
        
775
        # Corrupted
776
        w._weave[4] = 'There\n'
777
        return w
778
779
    def get_file_corrupted_checksum(self):
780
        w = self.get_file_corrupted_text()
781
        # Corrected
782
        w._weave[4] = 'there\n'
783
        self.assertEqual('hello\nthere\n', w.get_text('v2'))
784
        
785
        #Invalid checksum, first digit changed
786
        w._sha1s[1] =  'f0f265c6e75f1c8f9ab76dcf85528352c5f215ef'
787
        return w
788
1666.1.6 by Robert Collins
Make knit the default format.
789
    def reopen_file(self, name='foo', create=False):
790
        return WeaveFile(name, get_transport(self.get_url('.')), create=create)
1563.2.9 by Robert Collins
Update versionedfile api tests to ensure that data is available after every operation.
791
1563.2.25 by Robert Collins
Merge in upstream.
792
    def test_no_implicit_create(self):
793
        self.assertRaises(errors.NoSuchFile,
794
                          WeaveFile,
795
                          'foo',
796
                          get_transport(self.get_url('.')))
797
1594.2.23 by Robert Collins
Test versioned file storage handling of clean/dirty status for accessed versioned files.
798
    def get_factory(self):
799
        return WeaveFile
800
1563.2.1 by Robert Collins
Merge in a variation of the versionedfile api from versioned-file.
801
1563.2.12 by Robert Collins
Checkpointing: created InterObject to factor out common inter object worker code, added InterVersionedFile and tests to allow making join work between any versionedfile.
802
class TestKnit(TestCaseWithTransport, VersionedFileTestMixIn):
1563.2.4 by Robert Collins
First cut at including the knit implementation of versioned_file.
803
804
    def get_file(self, name='foo'):
1563.2.16 by Robert Collins
Change WeaveStore into VersionedFileStore and make its versoined file class parameterisable.
805
        return KnitVersionedFile(name, get_transport(self.get_url('.')),
1563.2.25 by Robert Collins
Merge in upstream.
806
                                 delta=True, create=True)
1563.2.6 by Robert Collins
Start check tests for knits (pending), and remove dead code.
807
1594.2.23 by Robert Collins
Test versioned file storage handling of clean/dirty status for accessed versioned files.
808
    def get_factory(self):
809
        return KnitVersionedFile
810
1563.2.6 by Robert Collins
Start check tests for knits (pending), and remove dead code.
811
    def get_file_corrupted_text(self):
812
        knit = self.get_file()
813
        knit.add_lines('v1', [], ['hello\n'])
814
        knit.add_lines('v2', ['v1'], ['hello\n', 'there\n'])
815
        return knit
1563.2.9 by Robert Collins
Update versionedfile api tests to ensure that data is available after every operation.
816
1666.1.6 by Robert Collins
Make knit the default format.
817
    def reopen_file(self, name='foo', create=False):
818
        return KnitVersionedFile(name, get_transport(self.get_url('.')),
819
            delta=True,
820
            create=create)
1563.2.10 by Robert Collins
Change weave store to be a versioned store, using WeaveFiles which maintain integrity without needing explicit 'put' operations.
821
822
    def test_detection(self):
1563.2.19 by Robert Collins
stub out a check for knits.
823
        knit = self.get_file()
824
        knit.check()
1563.2.12 by Robert Collins
Checkpointing: created InterObject to factor out common inter object worker code, added InterVersionedFile and tests to allow making join work between any versionedfile.
825
1563.2.25 by Robert Collins
Merge in upstream.
826
    def test_no_implicit_create(self):
827
        self.assertRaises(errors.NoSuchFile,
828
                          KnitVersionedFile,
829
                          'foo',
830
                          get_transport(self.get_url('.')))
831
1563.2.12 by Robert Collins
Checkpointing: created InterObject to factor out common inter object worker code, added InterVersionedFile and tests to allow making join work between any versionedfile.
832
833
class InterString(versionedfile.InterVersionedFile):
834
    """An inter-versionedfile optimised code path for strings.
835
836
    This is for use during testing where we use strings as versionedfiles
837
    so that none of the default regsitered interversionedfile classes will
838
    match - which lets us test the match logic.
839
    """
840
841
    @staticmethod
842
    def is_compatible(source, target):
843
        """InterString is compatible with strings-as-versionedfiles."""
844
        return isinstance(source, str) and isinstance(target, str)
845
846
847
# TODO this and the InterRepository core logic should be consolidatable
848
# if we make the registry a separate class though we still need to 
849
# test the behaviour in the active registry to catch failure-to-handle-
850
# stange-objects
851
class TestInterVersionedFile(TestCaseWithTransport):
852
853
    def test_get_default_inter_versionedfile(self):
854
        # test that the InterVersionedFile.get(a, b) probes
855
        # for a class where is_compatible(a, b) returns
856
        # true and returns a default interversionedfile otherwise.
857
        # This also tests that the default registered optimised interversionedfile
858
        # classes do not barf inappropriately when a surprising versionedfile type
859
        # is handed to them.
860
        dummy_a = "VersionedFile 1."
861
        dummy_b = "VersionedFile 2."
862
        self.assertGetsDefaultInterVersionedFile(dummy_a, dummy_b)
863
864
    def assertGetsDefaultInterVersionedFile(self, a, b):
865
        """Asserts that InterVersionedFile.get(a, b) -> the default."""
866
        inter = versionedfile.InterVersionedFile.get(a, b)
867
        self.assertEqual(versionedfile.InterVersionedFile,
868
                         inter.__class__)
869
        self.assertEqual(a, inter.source)
870
        self.assertEqual(b, inter.target)
871
872
    def test_register_inter_versionedfile_class(self):
873
        # test that a optimised code path provider - a
874
        # InterVersionedFile subclass can be registered and unregistered
875
        # and that it is correctly selected when given a versionedfile
876
        # pair that it returns true on for the is_compatible static method
877
        # check
878
        dummy_a = "VersionedFile 1."
879
        dummy_b = "VersionedFile 2."
880
        versionedfile.InterVersionedFile.register_optimiser(InterString)
881
        try:
882
            # we should get the default for something InterString returns False
883
            # to
884
            self.assertFalse(InterString.is_compatible(dummy_a, None))
885
            self.assertGetsDefaultInterVersionedFile(dummy_a, None)
886
            # and we should get an InterString for a pair it 'likes'
887
            self.assertTrue(InterString.is_compatible(dummy_a, dummy_b))
888
            inter = versionedfile.InterVersionedFile.get(dummy_a, dummy_b)
889
            self.assertEqual(InterString, inter.__class__)
890
            self.assertEqual(dummy_a, inter.source)
891
            self.assertEqual(dummy_b, inter.target)
892
        finally:
893
            versionedfile.InterVersionedFile.unregister_optimiser(InterString)
894
        # now we should get the default InterVersionedFile object again.
895
        self.assertGetsDefaultInterVersionedFile(dummy_a, dummy_b)
1666.1.1 by Robert Collins
Add trivial http-using test for versioned files.
896
897
898
class TestReadonlyHttpMixin(object):
899
900
    def test_readonly_http_works(self):
901
        # we should be able to read from http with a versioned file.
902
        vf = self.get_file()
1666.1.6 by Robert Collins
Make knit the default format.
903
        # try an empty file access
904
        readonly_vf = self.get_factory()('foo', get_transport(self.get_readonly_url('.')))
905
        self.assertEqual([], readonly_vf.versions())
906
        # now with feeling.
1666.1.1 by Robert Collins
Add trivial http-using test for versioned files.
907
        vf.add_lines('1', [], ['a\n'])
908
        vf.add_lines('2', ['1'], ['b\n', 'a\n'])
909
        readonly_vf = self.get_factory()('foo', get_transport(self.get_readonly_url('.')))
1666.1.6 by Robert Collins
Make knit the default format.
910
        self.assertEqual(['1', '2'], vf.versions())
1666.1.1 by Robert Collins
Add trivial http-using test for versioned files.
911
        for version in readonly_vf.versions():
912
            readonly_vf.get_lines(version)
913
914
915
class TestWeaveHTTP(TestCaseWithWebserver, TestReadonlyHttpMixin):
916
917
    def get_file(self):
918
        return WeaveFile('foo', get_transport(self.get_url('.')), create=True)
919
920
    def get_factory(self):
921
        return WeaveFile
922
923
924
class TestKnitHTTP(TestCaseWithWebserver, TestReadonlyHttpMixin):
925
926
    def get_file(self):
927
        return KnitVersionedFile('foo', get_transport(self.get_url('.')),
928
                                 delta=True, create=True)
929
930
    def get_factory(self):
931
        return KnitVersionedFile
1664.2.9 by Aaron Bentley
Ported weave merge test to versionedfile
932
933
934
class MergeCasesMixin(object):
935
936
    def doMerge(self, base, a, b, mp):
937
        from cStringIO import StringIO
938
        from textwrap import dedent
939
940
        def addcrlf(x):
941
            return x + '\n'
942
        
943
        w = self.get_file()
944
        w.add_lines('text0', [], map(addcrlf, base))
945
        w.add_lines('text1', ['text0'], map(addcrlf, a))
946
        w.add_lines('text2', ['text0'], map(addcrlf, b))
947
948
        self.log_contents(w)
949
950
        self.log('merge plan:')
951
        p = list(w.plan_merge('text1', 'text2'))
952
        for state, line in p:
953
            if line:
954
                self.log('%12s | %s' % (state, line[:-1]))
955
956
        self.log('merge:')
957
        mt = StringIO()
958
        mt.writelines(w.weave_merge(p))
959
        mt.seek(0)
960
        self.log(mt.getvalue())
961
962
        mp = map(addcrlf, mp)
963
        self.assertEqual(mt.readlines(), mp)
964
        
965
        
966
    def testOneInsert(self):
967
        self.doMerge([],
968
                     ['aa'],
969
                     [],
970
                     ['aa'])
971
972
    def testSeparateInserts(self):
973
        self.doMerge(['aaa', 'bbb', 'ccc'],
974
                     ['aaa', 'xxx', 'bbb', 'ccc'],
975
                     ['aaa', 'bbb', 'yyy', 'ccc'],
976
                     ['aaa', 'xxx', 'bbb', 'yyy', 'ccc'])
977
978
    def testSameInsert(self):
979
        self.doMerge(['aaa', 'bbb', 'ccc'],
980
                     ['aaa', 'xxx', 'bbb', 'ccc'],
981
                     ['aaa', 'xxx', 'bbb', 'yyy', 'ccc'],
982
                     ['aaa', 'xxx', 'bbb', 'yyy', 'ccc'])
983
    overlappedInsertExpected = ['aaa', 'xxx', 'yyy', 'bbb']
984
    def testOverlappedInsert(self):
985
        self.doMerge(['aaa', 'bbb'],
986
                     ['aaa', 'xxx', 'yyy', 'bbb'],
987
                     ['aaa', 'xxx', 'bbb'], self.overlappedInsertExpected)
988
989
        # really it ought to reduce this to 
990
        # ['aaa', 'xxx', 'yyy', 'bbb']
991
992
993
    def testClashReplace(self):
994
        self.doMerge(['aaa'],
995
                     ['xxx'],
996
                     ['yyy', 'zzz'],
997
                     ['<<<<<<< ', 'xxx', '=======', 'yyy', 'zzz', 
998
                      '>>>>>>> '])
999
1000
    def testNonClashInsert1(self):
1001
        self.doMerge(['aaa'],
1002
                     ['xxx', 'aaa'],
1003
                     ['yyy', 'zzz'],
1004
                     ['<<<<<<< ', 'xxx', 'aaa', '=======', 'yyy', 'zzz', 
1005
                      '>>>>>>> '])
1006
1007
    def testNonClashInsert2(self):
1008
        self.doMerge(['aaa'],
1009
                     ['aaa'],
1010
                     ['yyy', 'zzz'],
1011
                     ['yyy', 'zzz'])
1012
1013
1014
    def testDeleteAndModify(self):
1015
        """Clashing delete and modification.
1016
1017
        If one side modifies a region and the other deletes it then
1018
        there should be a conflict with one side blank.
1019
        """
1020
1021
        #######################################
1022
        # skippd, not working yet
1023
        return
1024
        
1025
        self.doMerge(['aaa', 'bbb', 'ccc'],
1026
                     ['aaa', 'ddd', 'ccc'],
1027
                     ['aaa', 'ccc'],
1028
                     ['<<<<<<<< ', 'aaa', '=======', '>>>>>>> ', 'ccc'])
1029
1030
    def _test_merge_from_strings(self, base, a, b, expected):
1031
        w = self.get_file()
1032
        w.add_lines('text0', [], base.splitlines(True))
1033
        w.add_lines('text1', ['text0'], a.splitlines(True))
1034
        w.add_lines('text2', ['text0'], b.splitlines(True))
1035
        self.log('merge plan:')
1036
        p = list(w.plan_merge('text1', 'text2'))
1037
        for state, line in p:
1038
            if line:
1039
                self.log('%12s | %s' % (state, line[:-1]))
1040
        self.log('merge result:')
1041
        result_text = ''.join(w.weave_merge(p))
1042
        self.log(result_text)
1043
        self.assertEqualDiff(result_text, expected)
1044
1045
    def test_weave_merge_conflicts(self):
1046
        # does weave merge properly handle plans that end with unchanged?
1047
        result = ''.join(self.get_file().weave_merge([('new-a', 'hello\n')]))
1048
        self.assertEqual(result, 'hello\n')
1049
1050
    def test_deletion_extended(self):
1051
        """One side deletes, the other deletes more.
1052
        """
1053
        base = """\
1054
            line 1
1055
            line 2
1056
            line 3
1057
            """
1058
        a = """\
1059
            line 1
1060
            line 2
1061
            """
1062
        b = """\
1063
            line 1
1064
            """
1065
        result = """\
1066
            line 1
1067
            """
1068
        self._test_merge_from_strings(base, a, b, result)
1069
1070
    def test_deletion_overlap(self):
1071
        """Delete overlapping regions with no other conflict.
1072
1073
        Arguably it'd be better to treat these as agreement, rather than 
1074
        conflict, but for now conflict is safer.
1075
        """
1076
        base = """\
1077
            start context
1078
            int a() {}
1079
            int b() {}
1080
            int c() {}
1081
            end context
1082
            """
1083
        a = """\
1084
            start context
1085
            int a() {}
1086
            end context
1087
            """
1088
        b = """\
1089
            start context
1090
            int c() {}
1091
            end context
1092
            """
1093
        result = """\
1094
            start context
1095
<<<<<<< 
1096
            int a() {}
1097
=======
1098
            int c() {}
1099
>>>>>>> 
1100
            end context
1101
            """
1102
        self._test_merge_from_strings(base, a, b, result)
1103
1104
    def test_agreement_deletion(self):
1105
        """Agree to delete some lines, without conflicts."""
1106
        base = """\
1107
            start context
1108
            base line 1
1109
            base line 2
1110
            end context
1111
            """
1112
        a = """\
1113
            start context
1114
            base line 1
1115
            end context
1116
            """
1117
        b = """\
1118
            start context
1119
            base line 1
1120
            end context
1121
            """
1122
        result = """\
1123
            start context
1124
            base line 1
1125
            end context
1126
            """
1127
        self._test_merge_from_strings(base, a, b, result)
1128
1129
    def test_sync_on_deletion(self):
1130
        """Specific case of merge where we can synchronize incorrectly.
1131
        
1132
        A previous version of the weave merge concluded that the two versions
1133
        agreed on deleting line 2, and this could be a synchronization point.
1134
        Line 1 was then considered in isolation, and thought to be deleted on 
1135
        both sides.
1136
1137
        It's better to consider the whole thing as a disagreement region.
1138
        """
1139
        base = """\
1140
            start context
1141
            base line 1
1142
            base line 2
1143
            end context
1144
            """
1145
        a = """\
1146
            start context
1147
            base line 1
1148
            a's replacement line 2
1149
            end context
1150
            """
1151
        b = """\
1152
            start context
1153
            b replaces
1154
            both lines
1155
            end context
1156
            """
1157
        result = """\
1158
            start context
1159
<<<<<<< 
1160
            base line 1
1161
            a's replacement line 2
1162
=======
1163
            b replaces
1164
            both lines
1165
>>>>>>> 
1166
            end context
1167
            """
1168
        self._test_merge_from_strings(base, a, b, result)
1169
1170
1171
class TestKnitMerge(TestCaseWithTransport, MergeCasesMixin):
1172
1173
    def get_file(self, name='foo'):
1174
        return KnitVersionedFile(name, get_transport(self.get_url('.')),
1175
                                 delta=True, create=True)
1176
1177
    def log_contents(self, w):
1178
        pass
1179
1180
1181
class TestWeaveMerge(TestCaseWithTransport, MergeCasesMixin):
1182
1183
    def get_file(self, name='foo'):
1184
        return WeaveFile(name, get_transport(self.get_url('.')), create=True)
1185
1186
    def log_contents(self, w):
1187
        self.log('weave is:')
1188
        tmpf = StringIO()
1189
        write_weave(w, tmpf)
1190
        self.log(tmpf.getvalue())
1191
1192
    overlappedInsertExpected = ['aaa', '<<<<<<< ', 'xxx', 'yyy', '=======', 
1193
                                'xxx', '>>>>>>> ', 'bbb']