/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) 2006 Canonical Ltd
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.
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
"""Tests for join between versioned files."""
18
19
1563.2.13 by Robert Collins
InterVersionedFile implemented.
20
import bzrlib.errors as errors
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.
21
from bzrlib.tests import TestCaseWithTransport
22
from bzrlib.transport import get_transport
1563.2.13 by Robert Collins
InterVersionedFile implemented.
23
import bzrlib.versionedfile as versionedfile
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.
24
25
26
class TestJoin(TestCaseWithTransport):
27
    #Tests have self.versionedfile_factory and self.versionedfile_factory_to
28
    #available to create source and target versioned files respectively.
29
30
    def get_source(self, name='source'):
31
        """Get a versioned file we will be joining from."""
32
        return self.versionedfile_factory(name,
1563.2.25 by Robert Collins
Merge in upstream.
33
                                          get_transport(self.get_url()),
34
                                          create=True)
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.
35
1692.2.1 by Robert Collins
Fix knit based push to only perform 2 appends to the target, rather that 2*new-versions.
36
    def get_target(self, name='target', create=True):
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.
37
        """"Get an empty versioned file to join into."""
38
        return self.versionedfile_factory_to(name,
1563.2.25 by Robert Collins
Merge in upstream.
39
                                             get_transport(self.get_url()),
1692.2.1 by Robert Collins
Fix knit based push to only perform 2 appends to the target, rather that 2*new-versions.
40
                                             create=create)
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.
41
42
    def test_join(self):
43
        f1 = self.get_source()
44
        f1.add_lines('r0', [], ['a\n', 'b\n'])
45
        f1.add_lines('r1', ['r0'], ['c\n', 'b\n'])
46
        f2 = self.get_target()
47
        f2.join(f1, None)
48
        def verify_file(f):
49
            self.assertTrue(f.has_version('r0'))
50
            self.assertTrue(f.has_version('r1'))
51
        verify_file(f2)
52
        verify_file(self.get_target())
53
1563.2.13 by Robert Collins
InterVersionedFile implemented.
54
        self.assertRaises(errors.RevisionNotPresent,
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.
55
            f2.join, f1, version_ids=['r3'])
56
1563.2.13 by Robert Collins
InterVersionedFile implemented.
57
    def test_gets_expected_inter_worker(self):
58
        source = self.get_source()
59
        target = self.get_target()
60
        inter = versionedfile.InterVersionedFile.get(source, target)
61
        self.assertTrue(isinstance(inter, self.interversionedfile_class))
1563.2.40 by Robert Collins
Extra test for joining of version-limited sets.
62
63
    def test_join_versions_joins_ancestors_not_siblings(self):
64
        # joining with a version list should bring in ancestors of the
65
        # named versions but not siblings thereof.
66
        target = self.get_target()
67
        target.add_lines('base', [], [])
68
        source = self.get_source()
69
        source.add_lines('base', [], [])
70
        source.add_lines('sibling', ['base'], [])
71
        source.add_lines('ancestorleft', ['base'], [])
72
        source.add_lines('ancestorright', ['base'], [])
73
        source.add_lines('namedleft', ['ancestorleft'], [])
74
        source.add_lines('namedright', ['ancestorright'], [])
75
        target.join(source, version_ids=['namedleft', 'namedright'])
76
        self.assertFalse(target.has_version('sibling'))
77
        self.assertTrue(target.has_version('ancestorleft'))
78
        self.assertTrue(target.has_version('ancestorright'))
79
        self.assertTrue(target.has_version('namedleft'))
80
        self.assertTrue(target.has_version('namedright'))
81
2825.4.1 by Robert Collins
* ``pull``, ``merge`` and ``push`` will no longer silently correct some
82
    def test_join_different_parents_existing_version(self):
83
        """This may either ignore or error."""
1563.2.13 by Robert Collins
InterVersionedFile implemented.
84
        w1 = self.get_target('w1')
85
        w2 = self.get_source('w2')
86
        w1.add_lines('v-1', [], ['line 1\n'])
87
        w2.add_lines('v-2', [], ['line 2\n'])
88
        w1.add_lines('v-3', ['v-1'], ['line 1\n'])
89
        w2.add_lines('v-3', ['v-2'], ['line 1\n'])
2825.4.1 by Robert Collins
* ``pull``, ``merge`` and ``push`` will no longer silently correct some
90
        try:
91
            w1.join(w2)
92
        except errors.WeaveParentMismatch:
93
            # Acceptable behaviour:
94
            return
1563.2.13 by Robert Collins
InterVersionedFile implemented.
95
        self.assertEqual(sorted(w1.versions()),
96
                         'v-1 v-2 v-3'.split())
97
        self.assertEqualDiff(w1.get_text('v-3'),
98
                'line 1\n')
3287.5.2 by Robert Collins
Deprecate VersionedFile.get_parents, breaking pulling from a ghost containing knit or pack repository to weaves, which improves correctness and allows simplification of core code.
99
        self.assertEqual({'v-3':('v-1',)}, w1.get_parent_map(['v-3']))
1563.2.13 by Robert Collins
InterVersionedFile implemented.
100
        ann = list(w1.annotate('v-3'))
101
        self.assertEqual(len(ann), 1)
102
        self.assertEqual(ann[0][0], 'v-1')
103
        self.assertEqual(ann[0][1], 'line 1\n')
104
        
105
    def build_weave1(self):
106
        weave1 = self.get_source()
107
        self.lines1 = ['hello\n']
108
        self.lines3 = ['hello\n', 'cruel\n', 'world\n']
109
        weave1.add_lines('v1', [], self.lines1)
110
        weave1.add_lines('v2', ['v1'], ['hello\n', 'world\n'])
111
        weave1.add_lines('v3', ['v2'], self.lines3)
112
        return weave1
113
        
114
    def test_join_with_empty(self):
115
        """Reweave adding empty weave"""
116
        wb = self.get_target()
117
        w1 = self.build_weave1()
118
        w1.join(wb)
119
        self.verify_weave1(w1)
120
121
    def verify_weave1(self, w1):
122
        self.assertEqual(sorted(w1.versions()), ['v1', 'v2', 'v3'])
123
        self.assertEqual(w1.get_lines('v1'), ['hello\n'])
3287.5.2 by Robert Collins
Deprecate VersionedFile.get_parents, breaking pulling from a ghost containing knit or pack repository to weaves, which improves correctness and allows simplification of core code.
124
        self.assertEqual({'v1':()}, w1.get_parent_map(['v1']))
1563.2.13 by Robert Collins
InterVersionedFile implemented.
125
        self.assertEqual(w1.get_lines('v2'), ['hello\n', 'world\n'])
3287.5.2 by Robert Collins
Deprecate VersionedFile.get_parents, breaking pulling from a ghost containing knit or pack repository to weaves, which improves correctness and allows simplification of core code.
126
        self.assertEqual({'v2':('v1',)}, w1.get_parent_map(['v2']))
1563.2.13 by Robert Collins
InterVersionedFile implemented.
127
        self.assertEqual(w1.get_lines('v3'), ['hello\n', 'cruel\n', 'world\n'])
3287.5.2 by Robert Collins
Deprecate VersionedFile.get_parents, breaking pulling from a ghost containing knit or pack repository to weaves, which improves correctness and allows simplification of core code.
128
        self.assertEqual({'v3':('v2',)}, w1.get_parent_map(['v3']))
1563.2.13 by Robert Collins
InterVersionedFile implemented.
129
1607.1.1 by Robert Collins
Unbreak weave joins when the source weave has less parent information than the target.
130
    def test_join_source_has_less_parents_preserves_parents(self):
131
        # when the target has a text with more parent info, join 
132
        # preserves that.
133
        s = self.get_source()
134
        s.add_lines('base', [], [])
135
        s.add_lines('text', [], [])
136
        t = self.get_target()
137
        t.add_lines('base', [], [])
138
        t.add_lines('text', ['base'], [])
139
        t.join(s)
3287.5.2 by Robert Collins
Deprecate VersionedFile.get_parents, breaking pulling from a ghost containing knit or pack repository to weaves, which improves correctness and allows simplification of core code.
140
        self.assertEqual({'text':('base',)}, t.get_parent_map(['text']))
1607.1.1 by Robert Collins
Unbreak weave joins when the source weave has less parent information than the target.
141
1563.2.13 by Robert Collins
InterVersionedFile implemented.
142
    def test_join_with_ghosts(self):
143
        """Join that inserts parents of an existing revision.
144
2825.4.1 by Robert Collins
* ``pull``, ``merge`` and ``push`` will no longer silently correct some
145
        This can happen when merging from another branch who knows about
146
        revisions the destination does not, and the destinations index is
147
        incorrect because it was or is using a ghost-unaware format to
148
        represent the index. In this test the second weave knows of an
149
        additional parent of v2.
150
        
151
        However v2 must not be changed because we consider indexes immutable:
152
        instead a check or reconcile operation locally should pickup that v2 is
153
        wrong and regenerate the index at a later time. So either this errors,
154
        or leaves v2 unaltered.
1563.2.13 by Robert Collins
InterVersionedFile implemented.
155
        """
156
        w1 = self.build_weave1()
157
        wb = self.get_target()
158
        wb.add_lines('x1', [], ['line from x1\n'])
159
        wb.add_lines('v1', [], ['hello\n'])
160
        wb.add_lines('v2', ['v1', 'x1'], ['hello\n', 'world\n'])
2825.4.1 by Robert Collins
* ``pull``, ``merge`` and ``push`` will no longer silently correct some
161
        try:
162
            w1.join(wb)
163
        except errors.WeaveParentMismatch:
164
            # Acceptable behaviour:
165
            return
166
        self.assertEqual(['v1', 'v2', 'v3', 'x1',], sorted(w1.versions()))
167
        self.assertEqual('line from x1\n', w1.get_text('x1'))
168
        self.assertEqual(['hello\n', 'world\n'], w1.get_lines('v2'))
3287.5.2 by Robert Collins
Deprecate VersionedFile.get_parents, breaking pulling from a ghost containing knit or pack repository to weaves, which improves correctness and allows simplification of core code.
169
        self.assertEqual({'v2':('v1',)}, w1.get_parent_map(['v2']))
1563.2.13 by Robert Collins
InterVersionedFile implemented.
170
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
171
    def test_join_with_ignore_missing_versions(self):
172
        # test that ignore_missing=True makes a listed but absent version id
173
        # be ignored, and that unlisted version_ids are not integrated.
174
        w1 = self.build_weave1()
175
        wb = self.get_target()
176
        wb.add_lines('x1', [], ['line from x1\n'])
177
        wb.add_lines('v1', [], ['hello\n'])
178
        wb.add_lines('v2', ['v1', 'x1'], ['hello\n', 'world\n'])
179
        w1.join(wb, version_ids=['x1', 'z1'], ignore_missing=True)
180
        eq = self.assertEquals
181
        eq(sorted(w1.versions()), ['v1', 'v2', 'v3', 'x1'])
182
        eq(w1.get_text('x1'), 'line from x1\n')
183
        eq(w1.get_lines('v2'), ['hello\n', 'world\n'])
3287.5.2 by Robert Collins
Deprecate VersionedFile.get_parents, breaking pulling from a ghost containing knit or pack repository to weaves, which improves correctness and allows simplification of core code.
184
        self.assertEqual({'v2':('v1',)}, w1.get_parent_map(['v2']))
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
185
    
1563.2.13 by Robert Collins
InterVersionedFile implemented.
186
    def build_source_weave(self, name, *pattern):
187
        w = self.get_source(name)
188
        for version, parents in pattern:
189
            w.add_lines(version, parents, [])
190
        return w
191
192
    def build_target_weave(self, name, *pattern):
193
        w = self.get_target(name)
194
        for version, parents in pattern:
195
            w.add_lines(version, parents, [])
196
        return w
1594.2.8 by Robert Collins
add ghost aware apis to knits.
197
        
198
    def test_joining_ghosts(self):
199
        # some versioned file formats allow lines to be added with parent
200
        # information that is > than that in the format. Formats that do
201
        # not support this need to raise NotImplementedError on the
202
        # add_lines_with_ghosts api.
203
        # files with ghost information when joined into a file which
204
        # supports that must preserve it, when joined into a file which
205
        # does not must discard it, and when filling a ghost for a listed
206
        # ghost must reconcile it
207
        source = self.get_source()
208
        try:
209
            source.has_ghost('a')
210
            source_ghosts = True
211
        except NotImplementedError:
212
            source_ghosts = False
213
        target = self.get_target()
214
        try:
215
            target.has_ghost('a')
216
            target_ghosts = True
217
        except NotImplementedError:
218
            target_ghosts = False
219
220
        if not source_ghosts and not target_ghosts:
221
            # nothing to do
222
            return
223
        if source_ghosts and not target_ghosts:
224
            # switch source and target so source is ghostless
225
            t = source
226
            source = target
1684.3.3 by Robert Collins
Add a special cased weaves to knit converter.
227
            target = t
1594.2.8 by Robert Collins
add ghost aware apis to knits.
228
            source_ghosts = False
229
            target_ghosts = True
230
        # now target always supports ghosts.
231
232
        # try filling target with ghosts and filling in reverse -  
233
        target.add_lines_with_ghosts('notbase', ['base'], [])
3287.5.2 by Robert Collins
Deprecate VersionedFile.get_parents, breaking pulling from a ghost containing knit or pack repository to weaves, which improves correctness and allows simplification of core code.
234
        try:
235
            source.join(target)
236
        except errors.RevisionNotPresent:
237
            # can't join a ghost containing target onto a non-ghost supporting
238
            # source.
239
            self.assertFalse(source_ghosts)
240
            return
241
        else:
242
            self.assertTrue(source_ghosts)
1594.2.8 by Robert Collins
add ghost aware apis to knits.
243
        # legacy apis should behave
244
        self.assertEqual(['notbase'], source.get_ancestry(['notbase']))
2592.3.43 by Robert Collins
A knit iter_parents API.
245
        self.assertEqual({'notbase':()}, source.get_graph())
1594.2.8 by Robert Collins
add ghost aware apis to knits.
246
        self.assertFalse(source.has_version('base'))
3287.5.2 by Robert Collins
Deprecate VersionedFile.get_parents, breaking pulling from a ghost containing knit or pack repository to weaves, which improves correctness and allows simplification of core code.
247
        # ghost data should have been preserved
248
        self.assertEqual(['base', 'notbase'], source.get_ancestry_with_ghosts(['notbase']))
249
        self.assertEqual(['base'], source.get_parents_with_ghosts('notbase'))
250
        self.assertEqual({'notbase':('base',)}, source.get_parent_map(['notbase']))
3287.5.5 by Robert Collins
Refactor internals of knit implementations to implement get_parents_with_ghosts in terms of get_parent_map.
251
        self.assertEqual({'notbase':('base',)}, source.get_graph_with_ghosts())
3287.5.2 by Robert Collins
Deprecate VersionedFile.get_parents, breaking pulling from a ghost containing knit or pack repository to weaves, which improves correctness and allows simplification of core code.
252
        self.assertTrue(source.has_ghost('base'))
1594.2.8 by Robert Collins
add ghost aware apis to knits.
253
254
        # if we add something that is fills out what is a ghost, then 
255
        # when joining into a ghost aware join it should flesh out the ghosts.
256
        source.add_lines('base', [], [])
257
        target.join(source, version_ids=['base']) 
258
        self.assertEqual(['base', 'notbase'], target.get_ancestry(['notbase']))
3287.5.2 by Robert Collins
Deprecate VersionedFile.get_parents, breaking pulling from a ghost containing knit or pack repository to weaves, which improves correctness and allows simplification of core code.
259
        self.assertEqual({'notbase':('base',)}, target.get_parent_map(['notbase']))
2592.3.43 by Robert Collins
A knit iter_parents API.
260
        self.assertEqual({'base':(),
261
                          'notbase':('base', ),
1594.2.8 by Robert Collins
add ghost aware apis to knits.
262
                          },
263
                         target.get_graph())
264
        self.assertTrue(target.has_version('base'))
265
        # we have _with_ghost apis to give us ghost information.
266
        self.assertEqual(['base', 'notbase'], target.get_ancestry_with_ghosts(['notbase']))
267
        self.assertEqual(['base'], target.get_parents_with_ghosts('notbase'))
3287.5.5 by Robert Collins
Refactor internals of knit implementations to implement get_parents_with_ghosts in terms of get_parent_map.
268
        self.assertEqual({'base':(),
269
                          'notbase':('base',),
1594.2.8 by Robert Collins
add ghost aware apis to knits.
270
                          },
271
                         target.get_graph_with_ghosts())
272
        self.assertFalse(target.has_ghost('base'))
1684.3.1 by Robert Collins
Fix versioned file joins with empty targets.
273
274
    def test_restricted_join_into_empty(self):
275
        # joining into an empty versioned file with a version_ids list
276
        # should only grab the selected versions.
277
        source = self.get_source()
278
        source.add_lines('skip_me', [], ['a\n'])
279
        source.add_lines('inherit_me', [], ['b\n'])
280
        source.add_lines('select_me', ['inherit_me'], ['b\n'])
281
        target = self.get_target()
282
        target.join(source, version_ids=['select_me'])
283
        self.assertEqual(['inherit_me', 'select_me'], target.versions())
1692.2.1 by Robert Collins
Fix knit based push to only perform 2 appends to the target, rather that 2*new-versions.
284
285
    def test_join_odd_records(self):
286
        # this tests that joining the 1st, 3rd and 5th records and not the
287
        # 2nd and 4th works, particularly after reopening the file.
288
        # this test is designed to test versioned files that perform
289
        # optimisations on the join. Grabbing partial data and reopening the
290
        # file make it likely to trigger a fault.
291
        source = self.get_source()
292
        source.add_lines('1', [], ['1st\n'])
293
        source.add_lines('2', [], ['2nd\n'])
294
        source.add_lines('3', ['1'], ['1st\n', '2nd\n'])
295
        source.add_lines('4', ['2'], ['1st\n'])
296
        source.add_lines('5', ['3'], ['1st\n', '2nd\n', '3rd\n'])
297
        target = self.get_target()
298
        target.join(source, version_ids=['1', '3', '5'])
299
        target = self.get_target(create=False)
300
        self.assertEqual(set(['1', '3', '5']), set(target.versions()))
301
        self.assertEqual(3, len(target.versions()))
302
        self.assertEqual(['1st\n'], target.get_lines('1'))
303
        self.assertEqual(['1st\n', '2nd\n'], target.get_lines('3'))
304
        self.assertEqual(['1st\n', '2nd\n', '3rd\n'], target.get_lines('5'))