~bzr-pqm/bzr/bzr.dev

1185.16.106 by mbp at sourcefrog
Split reweave tests into new file and add one more.
1
# Copyright (C) 2005 by Canonical Ltd
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
"""Test reweave code.
18
19
Reweave takes two weaves containing a partial view of history and combines
20
them into a single weave containing all the information.  This can include 
21
22
 - versions recorded in only one file
23
24
 - versions with different (but not contradictory) lists of parent 
25
   revisions
26
27
It is an error if either of these conditions occur:
28
29
 - contradictory ancestry graphs, e.g.
30
   - v1 is an ancestor of v2 in one weave, and vice versa in the other
31
   - different text for any version 
32
"""
33
34
import os
35
import sys
36
1185.31.25 by John Arbash Meinel
Renamed all of the tests from selftest/foo.py to tests/test_foo.py
37
from bzrlib.tests import TestCase
1185.16.106 by mbp at sourcefrog
Split reweave tests into new file and add one more.
38
from bzrlib.weave import Weave, reweave
39
from bzrlib.errors import WeaveParentMismatch
40
1185.16.118 by mbp at sourcefrog
More reweave tests
41
class TestReweave(TestCase):
1185.16.106 by mbp at sourcefrog
Split reweave tests into new file and add one more.
42
43
    def test_reweave_add_parents(self):
1185.16.107 by mbp at sourcefrog
Test for correct annotations after simple reweave
44
        """Reweave inserting new parents
45
        
46
        The new version must have the right parent list and must identify
47
        lines originating in another parent.
48
        """
1185.16.106 by mbp at sourcefrog
Split reweave tests into new file and add one more.
49
        w1 = Weave('w1')
50
        w2 = Weave('w2')
1185.16.107 by mbp at sourcefrog
Test for correct annotations after simple reweave
51
        w1.add('v-1', [], ['line 1\n'])
52
        w2.add('v-2', [], ['line 2\n'])
53
        w1.add('v-3', ['v-1'], ['line 1\n'])
54
        w2.add('v-3', ['v-2'], ['line 1\n'])
1185.16.106 by mbp at sourcefrog
Split reweave tests into new file and add one more.
55
        w3 = reweave(w1, w2)
56
        self.assertEqual(sorted(w3.names()),
57
                         'v-1 v-2 v-3'.split())
58
        self.assertEqualDiff(w3.get_text('v-3'),
1185.16.107 by mbp at sourcefrog
Test for correct annotations after simple reweave
59
                'line 1\n')
60
        self.assertEqual(sorted(w3.parent_names('v-3')),
61
                ['v-1', 'v-2'])
62
        ann = list(w3.annotate('v-3'))
63
        self.assertEqual(len(ann), 1)
64
        self.assertEqual(w3.idx_to_name(ann[0][0]), 'v-1')
65
        self.assertEqual(ann[0][1], 'line 1\n')
1185.16.106 by mbp at sourcefrog
Split reweave tests into new file and add one more.
66
        
67
    def build_weave1(self):
68
        weave1 = Weave()
69
        self.lines1 = ['hello\n']
70
        self.lines3 = ['hello\n', 'cruel\n', 'world\n']
71
        weave1.add('v1', [], self.lines1)
72
        weave1.add('v2', [0], ['hello\n', 'world\n'])
73
        weave1.add('v3', [1], self.lines3)
74
        return weave1
75
        
76
    def test_reweave_with_empty(self):
1185.16.107 by mbp at sourcefrog
Test for correct annotations after simple reweave
77
        """Reweave adding empty weave"""
1185.16.106 by mbp at sourcefrog
Split reweave tests into new file and add one more.
78
        wb = Weave()
79
        w1 = self.build_weave1()
80
        wr = reweave(w1, wb)
81
        eq = self.assertEquals
82
        eq(sorted(wr.iter_names()), ['v1', 'v2', 'v3'])
83
        eq(wr.get_lines('v3'), ['hello\n', 'cruel\n', 'world\n'])
84
        self.assertEquals(wr, w1)
85
86
    def test_join_with_ghosts_raises_parent_mismatch(self):
1185.16.107 by mbp at sourcefrog
Test for correct annotations after simple reweave
87
        """Join weave traps parent mismatch"""
1185.16.106 by mbp at sourcefrog
Split reweave tests into new file and add one more.
88
        wa = self.build_weave1()
89
        wb = Weave()
90
        wb.add('x1', [], ['line from x1\n'])
91
        wb.add('v1', [], ['hello\n'])
92
        wb.add('v2', ['v1', 'x1'], ['hello\n', 'world\n'])
93
        self.assertRaises(WeaveParentMismatch, wa.join, wb)
94
95
    def test_reweave_with_ghosts(self):
96
        """Join that inserts parents of an existing revision.
97
98
        This can happen when merging from another branch who
99
        knows about revisions the destination does not.  In 
100
        this test the second weave knows of an additional parent of 
101
        v2.  Any revisions which are in common still have to have the 
1185.16.116 by mbp at sourcefrog
Move weave errors into bzrlib.errors and make consistent with new system.
102
        same text.
103
        """
1185.16.106 by mbp at sourcefrog
Split reweave tests into new file and add one more.
104
        w1 = self.build_weave1()
105
        wa = w1.copy()
106
        wb = Weave()
107
        wb.add('x1', [], ['line from x1\n'])
108
        wb.add('v1', [], ['hello\n'])
109
        wb.add('v2', ['v1', 'x1'], ['hello\n', 'world\n'])
110
        wc = reweave(wa, wb)
111
        eq = self.assertEquals
112
        eq(sorted(wc.iter_names()), ['v1', 'v2', 'v3', 'x1',])
113
        eq(wc.get_text('x1'), 'line from x1\n')
114
        eq(wc.get_lines('v2'), ['hello\n', 'world\n'])
115
        eq(wc.parent_names('v2'), ['v1', 'x1'])
116
        w1.reweave(wb)
117
        self.assertEquals(wc, w1)
1185.16.118 by mbp at sourcefrog
More reweave tests
118
119
    def build_empty_weave(self, *pattern):
120
        w = Weave()
121
        for version, parents in pattern:
122
            w.add(version, parents, [])
123
        return w
124
125
    def test_reweave_reorder(self):
126
        """Reweave requiring reordering of versions.
127
128
        Weaves must be stored such that parents come before children.  When
129
        reweaving, we may add new parents to some children, but it is required
130
        that there must be *some* valid order that can be found, otherwise the
131
        ancestries are contradictory.  (For the specific case of inserting
132
        ghost revisions there will be no disagreement, only partial knowledge
133
        of the history.)
134
135
        Note that the weaves are only partially ordered: when there are two
136
        versions where neither is an ancestor of the other the order in which
137
        they occur is unconstrained.  When we reweave those versions into
138
        another weave, they may become more constrained and it may be
139
        necessary to change their order.
140
141
        One simple case of this is 
142
143
        w1: (c[], a[], b[a])
144
        w2: (b[], c[b], a[])
145
        
146
        We need to recognize that the final weave must show the ordering
147
        a[], b[a], c[b].  The version that must be first in the result is 
148
        not first in either of the input weaves.
149
        """
150
        w1 = self.build_empty_weave(('c', []), ('a', []), ('b', ['a']))
151
        w2 = self.build_empty_weave(('b', []), ('c', ['b']), ('a', []))
152
        w3 = reweave(w1, w2)