~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_versionedfile.py

  • Committer: Robert Collins
  • Date: 2006-03-01 12:09:59 UTC
  • mto: (1594.2.4 integration)
  • mto: This revision was merged to the branch mainline in revision 1596.
  • Revision ID: robertc@robertcollins.net-20060301120959-b58a073b9f7f7a00
Consolidate reweave and join as we have no separate usage, make reweave tests apply to all versionedfile implementations and deprecate the old reweave apis.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 
24
24
import bzrlib
25
25
import bzrlib.errors as errors
26
 
from bzrlib.errors import RevisionNotPresent, \
27
 
     RevisionAlreadyPresent
 
26
from bzrlib.errors import (
 
27
                           RevisionNotPresent, 
 
28
                           RevisionAlreadyPresent,
 
29
                           WeaveParentMismatch
 
30
                           )
28
31
from bzrlib.knit import KnitVersionedFile, \
29
32
     KnitAnnotateFactory
30
33
from bzrlib.tests import TestCaseInTempDir
194
197
        """Open the versioned file from disk again."""
195
198
        raise NotImplementedError(self.reopen_file)
196
199
 
 
200
    def test_join_add_parents(self):
 
201
        """Reweave inserting new parents
 
202
        
 
203
        The new version must have the right parent list and must identify
 
204
        lines originating in another parent.
 
205
        """
 
206
        w1 = self.get_file('w1')
 
207
        w2 = self.get_file('w2')
 
208
        w1.add_lines('v-1', [], ['line 1\n'])
 
209
        w2.add_lines('v-2', [], ['line 2\n'])
 
210
        w1.add_lines('v-3', ['v-1'], ['line 1\n'])
 
211
        w2.add_lines('v-3', ['v-2'], ['line 1\n'])
 
212
        w1.join(w2)
 
213
        self.assertEqual(sorted(w1.names()),
 
214
                         'v-1 v-2 v-3'.split())
 
215
        self.assertEqualDiff(w1.get_text('v-3'),
 
216
                'line 1\n')
 
217
        self.assertEqual(sorted(w1.parent_names('v-3')),
 
218
                ['v-1', 'v-2'])
 
219
        ann = list(w1.annotate('v-3'))
 
220
        self.assertEqual(len(ann), 1)
 
221
        self.assertEqual(ann[0][0], 'v-1')
 
222
        self.assertEqual(ann[0][1], 'line 1\n')
 
223
        
 
224
    def build_weave1(self):
 
225
        weave1 = self.get_file()
 
226
        self.lines1 = ['hello\n']
 
227
        self.lines3 = ['hello\n', 'cruel\n', 'world\n']
 
228
        weave1.add_lines('v1', [], self.lines1)
 
229
        weave1.add_lines('v2', ['v1'], ['hello\n', 'world\n'])
 
230
        weave1.add_lines('v3', ['v2'], self.lines3)
 
231
        return weave1
 
232
        
 
233
    def test_join_with_empty(self):
 
234
        """Reweave adding empty weave"""
 
235
        wb = self.get_file('b')
 
236
        w1 = self.build_weave1()
 
237
        w1.join(wb)
 
238
        self.assertEqual(sorted(w1.iter_names()), ['v1', 'v2', 'v3'])
 
239
        self.assertEqual(w1.get_lines('v1'), ['hello\n'])
 
240
        self.assertEqual([], w1.get_parents('v1'))
 
241
        self.assertEqual(w1.get_lines('v2'), ['hello\n', 'world\n'])
 
242
        self.assertEqual(['v1'], w1.get_parents('v2'))
 
243
        self.assertEqual(w1.get_lines('v3'), ['hello\n', 'cruel\n', 'world\n'])
 
244
        self.assertEqual(['v2'], w1.get_parents('v3'))
 
245
 
 
246
    def test_join_with_ghosts_raises_parent_mismatch(self):
 
247
        """Join combined parent lists"""
 
248
        wa = self.build_weave1()
 
249
        wb = self.get_file('b')
 
250
        wb.add_lines('x1', [], ['line from x1\n'])
 
251
        wb.add_lines('v1', [], ['hello\n'])
 
252
        wb.add_lines('v2', ['v1', 'x1'], ['hello\n', 'world\n'])
 
253
        wa.join(wb)
 
254
        self.assertEqual(['v1','x1'], wa.get_parents('v2'))
 
255
 
 
256
    def test_join_with_ghosts(self):
 
257
        """Join that inserts parents of an existing revision.
 
258
 
 
259
        This can happen when merging from another branch who
 
260
        knows about revisions the destination does not.  In 
 
261
        this test the second weave knows of an additional parent of 
 
262
        v2.  Any revisions which are in common still have to have the 
 
263
        same text.
 
264
        """
 
265
        w1 = self.build_weave1()
 
266
        wb = self.get_file('b')
 
267
        wb.add_lines('x1', [], ['line from x1\n'])
 
268
        wb.add_lines('v1', [], ['hello\n'])
 
269
        wb.add_lines('v2', ['v1', 'x1'], ['hello\n', 'world\n'])
 
270
        w1.join(wb)
 
271
        eq = self.assertEquals
 
272
        eq(sorted(w1.iter_names()), ['v1', 'v2', 'v3', 'x1',])
 
273
        eq(w1.get_text('x1'), 'line from x1\n')
 
274
        eq(w1.get_lines('v2'), ['hello\n', 'world\n'])
 
275
        eq(w1.parent_names('v2'), ['v1', 'x1'])
 
276
 
 
277
    def build_empty_weave(self, name, *pattern):
 
278
        w = self.get_file(name)
 
279
        for version, parents in pattern:
 
280
            w.add_lines(version, parents, [])
 
281
        return w
 
282
 
 
283
    def test_join_reorder(self):
 
284
        """Reweave requiring reordering of versions.
 
285
 
 
286
        Weaves must be stored such that parents come before children.  When
 
287
        reweaving, we may add new parents to some children, but it is required
 
288
        that there must be *some* valid order that can be found, otherwise the
 
289
        ancestries are contradictory.  (For the specific case of inserting
 
290
        ghost revisions there will be no disagreement, only partial knowledge
 
291
        of the history.)
 
292
 
 
293
        Note that the weaves are only partially ordered: when there are two
 
294
        versions where neither is an ancestor of the other the order in which
 
295
        they occur is unconstrained.  When we join those versions into
 
296
        another weave, they may become more constrained and it may be
 
297
        necessary to change their order.
 
298
 
 
299
        One simple case of this is 
 
300
 
 
301
        w1: (c[], a[], b[a])
 
302
        w2: (b[], c[b], a[])
 
303
        
 
304
        We need to recognize that the final weave must show the ordering
 
305
        a[], b[a], c[b].  The version that must be first in the result is 
 
306
        not first in either of the input weaves.
 
307
        """
 
308
        w1 = self.build_empty_weave('1', ('c', []), ('a', []), ('b', ['a']))
 
309
        w2 = self.build_empty_weave('2', ('b', []), ('c', ['b']), ('a', []))
 
310
        w1.join(w2)
 
311
        self.assertEqual([], w1.get_parents('a'))
 
312
        self.assertEqual(['a'], w1.get_parents('b'))
 
313
        self.assertEqual(['b'], w1.get_parents('c'))
197
314
 
198
315
class TestWeave(TestCaseInTempDir, VersionedFileTestMixIn):
199
316