/brz/remove-bazaar

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

« back to all changes in this revision

Viewing changes to bzrlib/tests/per_merger.py

  • Committer: Ian Clatworthy
  • Date: 2010-01-20 23:28:35 UTC
  • mto: (4977.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 4978.
  • Revision ID: ian.clatworthy@canonical.com-20100120232835-pyzvwp2pau0nnh0d
Hide most storage formats

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2009 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""Implementation tests for bzrlib.merge.Merger."""
 
18
 
 
19
import os
 
20
 
 
21
from bzrlib.conflicts import TextConflict
 
22
from bzrlib import (
 
23
    errors,
 
24
    merge as _mod_merge,
 
25
    option,
 
26
    progress,
 
27
    )
 
28
from bzrlib.tests import (
 
29
    multiply_tests,
 
30
    TestCaseWithTransport,
 
31
    )
 
32
from bzrlib.tests.test_merge_core import MergeBuilder
 
33
from bzrlib.transform import TreeTransform
 
34
 
 
35
 
 
36
 
 
37
def load_tests(standard_tests, module, loader):
 
38
    """Multiply tests for tranport implementations."""
 
39
    result = loader.suiteClass()
 
40
    scenarios = [
 
41
        (name, {'merge_type': merger})
 
42
        for name, merger in option._merge_type_registry.items()]
 
43
    return multiply_tests(standard_tests, scenarios, result)
 
44
 
 
45
 
 
46
class TestMergeImplementation(TestCaseWithTransport):
 
47
 
 
48
    def do_merge(self, target_tree, source_tree, **kwargs):
 
49
        merger = _mod_merge.Merger.from_revision_ids(progress.DummyProgress(),
 
50
            target_tree, source_tree.last_revision(),
 
51
            other_branch=source_tree.branch)
 
52
        merger.merge_type=self.merge_type
 
53
        for name, value in kwargs.items():
 
54
            setattr(merger, name, value)
 
55
        merger.do_merge()
 
56
 
 
57
    def test_merge_specific_file(self):
 
58
        this_tree = self.make_branch_and_tree('this')
 
59
        this_tree.lock_write()
 
60
        self.addCleanup(this_tree.unlock)
 
61
        self.build_tree_contents([
 
62
            ('this/file1', 'a\nb\n'),
 
63
            ('this/file2', 'a\nb\n')
 
64
        ])
 
65
        this_tree.add(['file1', 'file2'])
 
66
        this_tree.commit('Added files')
 
67
        other_tree = this_tree.bzrdir.sprout('other').open_workingtree()
 
68
        self.build_tree_contents([
 
69
            ('other/file1', 'a\nb\nc\n'),
 
70
            ('other/file2', 'a\nb\nc\n')
 
71
        ])
 
72
        other_tree.commit('modified both')
 
73
        self.build_tree_contents([
 
74
            ('this/file1', 'd\na\nb\n'),
 
75
            ('this/file2', 'd\na\nb\n')
 
76
        ])
 
77
        this_tree.commit('modified both')
 
78
        self.do_merge(this_tree, other_tree, interesting_files=['file1'])
 
79
        self.assertFileEqual('d\na\nb\nc\n', 'this/file1')
 
80
        self.assertFileEqual('d\na\nb\n', 'this/file2')
 
81
 
 
82
    def test_merge_move_and_change(self):
 
83
        this_tree = self.make_branch_and_tree('this')
 
84
        this_tree.lock_write()
 
85
        self.addCleanup(this_tree.unlock)
 
86
        self.build_tree_contents([
 
87
            ('this/file1', 'line 1\nline 2\nline 3\nline 4\n'),
 
88
        ])
 
89
        this_tree.add('file1',)
 
90
        this_tree.commit('Added file')
 
91
        other_tree = this_tree.bzrdir.sprout('other').open_workingtree()
 
92
        self.build_tree_contents([
 
93
            ('other/file1', 'line 1\nline 2 to 2.1\nline 3\nline 4\n'),
 
94
        ])
 
95
        other_tree.commit('Changed 2 to 2.1')
 
96
        self.build_tree_contents([
 
97
            ('this/file1', 'line 1\nline 3\nline 2\nline 4\n'),
 
98
        ])
 
99
        this_tree.commit('Swapped 2 & 3')
 
100
        self.do_merge(this_tree, other_tree)
 
101
        if self.merge_type is _mod_merge.LCAMerger:
 
102
            self.expectFailure(
 
103
                "lca merge doesn't conflict for move and change",
 
104
                self.assertFileEqual,
 
105
                'line 1\n'
 
106
                '<<<<<<< TREE\n'
 
107
                'line 3\n'
 
108
                'line 2\n'
 
109
                '=======\n'
 
110
                'line 2 to 2.1\n'
 
111
                'line 3\n'
 
112
                '>>>>>>> MERGE-SOURCE\n'
 
113
                'line 4\n', 'this/file1')
 
114
        else:
 
115
            self.assertFileEqual('line 1\n'
 
116
                '<<<<<<< TREE\n'
 
117
                'line 3\n'
 
118
                'line 2\n'
 
119
                '=======\n'
 
120
                'line 2 to 2.1\n'
 
121
                'line 3\n'
 
122
                '>>>>>>> MERGE-SOURCE\n'
 
123
                'line 4\n', 'this/file1')
 
124
 
 
125
    def test_modify_conflicts_with_delete(self):
 
126
        # If one side deletes a line, and the other modifies that line, then
 
127
        # the modification should be considered a conflict
 
128
        builder = self.make_branch_builder('test')
 
129
        builder.start_series()
 
130
        builder.build_snapshot('BASE-id', None,
 
131
            [('add', ('', None, 'directory', None)),
 
132
             ('add', ('foo', 'foo-id', 'file', 'a\nb\nc\nd\ne\n')),
 
133
            ])
 
134
        # Delete 'b\n'
 
135
        builder.build_snapshot('OTHER-id', ['BASE-id'],
 
136
            [('modify', ('foo-id', 'a\nc\nd\ne\n'))])
 
137
        # Modify 'b\n', add 'X\n'
 
138
        builder.build_snapshot('THIS-id', ['BASE-id'],
 
139
            [('modify', ('foo-id', 'a\nb2\nc\nd\nX\ne\n'))])
 
140
        builder.finish_series()
 
141
        branch = builder.get_branch()
 
142
        this_tree = branch.bzrdir.create_workingtree()
 
143
        this_tree.lock_write()
 
144
        self.addCleanup(this_tree.unlock)
 
145
        other_tree = this_tree.bzrdir.sprout('other',
 
146
                                             'OTHER-id').open_workingtree()
 
147
        self.do_merge(this_tree, other_tree)
 
148
        if self.merge_type is _mod_merge.LCAMerger:
 
149
            self.expectFailure("lca merge doesn't track deleted lines",
 
150
                self.assertFileEqual,
 
151
                    'a\n'
 
152
                    '<<<<<<< TREE\n'
 
153
                    'b2\n'
 
154
                    '=======\n'
 
155
                    '>>>>>>> MERGE-SOURCE\n'
 
156
                    'c\n'
 
157
                    'd\n'
 
158
                    'X\n'
 
159
                    'e\n', 'test/foo')
 
160
        else:
 
161
            self.assertFileEqual(
 
162
                'a\n'
 
163
                '<<<<<<< TREE\n'
 
164
                'b2\n'
 
165
                '=======\n'
 
166
                '>>>>>>> MERGE-SOURCE\n'
 
167
                'c\n'
 
168
                'd\n'
 
169
                'X\n'
 
170
                'e\n', 'test/foo')
 
171
 
 
172
    def get_limbodir_deletiondir(self, wt):
 
173
        transform = TreeTransform(wt)
 
174
        limbodir = transform._limbodir
 
175
        deletiondir = transform._deletiondir
 
176
        transform.finalize()
 
177
        return (limbodir, deletiondir)
 
178
 
 
179
    def test_merge_with_existing_limbo(self):
 
180
        wt = self.make_branch_and_tree('this')
 
181
        (limbodir, deletiondir) =  self.get_limbodir_deletiondir(wt)
 
182
        os.mkdir(limbodir)
 
183
        self.assertRaises(errors.ExistingLimbo, self.do_merge, wt, wt)
 
184
        self.assertRaises(errors.LockError, wt.unlock)
 
185
 
 
186
    def test_merge_with_pending_deletion(self):
 
187
        wt = self.make_branch_and_tree('this')
 
188
        (limbodir, deletiondir) =  self.get_limbodir_deletiondir(wt)
 
189
        os.mkdir(deletiondir)
 
190
        self.assertRaises(errors.ExistingPendingDeletion, self.do_merge, wt, wt)
 
191
        self.assertRaises(errors.LockError, wt.unlock)
 
192
 
 
193
 
 
194
class TestHookMergeFileContent(TestCaseWithTransport):
 
195
    """Tests that the 'merge_file_content' hook is invoked."""
 
196
 
 
197
    def setUp(self):
 
198
        TestCaseWithTransport.setUp(self)
 
199
        self.hook_log = []
 
200
 
 
201
    def install_hook_noop(self):
 
202
        def hook_na(merge_params):
 
203
            # This hook unconditionally does nothing.
 
204
            self.hook_log.append(('no-op',))
 
205
            return 'not_applicable', None
 
206
        _mod_merge.Merger.hooks.install_named_hook(
 
207
            'merge_file_content', hook_na, 'test hook (no-op)')
 
208
 
 
209
    def install_hook_success(self):
 
210
        def hook_success(merge_params):
 
211
            self.hook_log.append(('success',))
 
212
            if merge_params.file_id == '1':
 
213
                return 'success', ['text-merged-by-hook']
 
214
            return 'not_applicable', None
 
215
        _mod_merge.Merger.hooks.install_named_hook(
 
216
            'merge_file_content', hook_success, 'test hook (success)')
 
217
 
 
218
    def install_hook_conflict(self):
 
219
        def hook_conflict(merge_params):
 
220
            self.hook_log.append(('conflict',))
 
221
            if merge_params.file_id == '1':
 
222
                return 'conflicted', ['text-with-conflict-markers-from-hook']
 
223
            return 'not_applicable', None
 
224
        _mod_merge.Merger.hooks.install_named_hook(
 
225
            'merge_file_content', hook_conflict, 'test hook (delete)')
 
226
 
 
227
    def install_hook_delete(self):
 
228
        def hook_delete(merge_params):
 
229
            self.hook_log.append(('delete',))
 
230
            if merge_params.file_id == '1':
 
231
                return 'delete', None
 
232
            return 'not_applicable', None
 
233
        _mod_merge.Merger.hooks.install_named_hook(
 
234
            'merge_file_content', hook_delete, 'test hook (delete)')
 
235
 
 
236
    def install_hook_log_lines(self):
 
237
        """Install a hook that saves the get_lines for the this, base and other
 
238
        versions of the file.
 
239
        """
 
240
        def hook_log_lines(merge_params):
 
241
            self.hook_log.append((
 
242
                'log_lines',
 
243
                merge_params.this_lines,
 
244
                merge_params.other_lines,
 
245
                merge_params.base_lines,
 
246
                ))
 
247
            return 'not_applicable', None
 
248
        _mod_merge.Merger.hooks.install_named_hook(
 
249
            'merge_file_content', hook_log_lines, 'test hook (log_lines)')
 
250
 
 
251
    def make_merge_builder(self):
 
252
        builder = MergeBuilder(self.test_base_dir)
 
253
        self.addCleanup(builder.cleanup)
 
254
        return builder
 
255
 
 
256
    def create_file_needing_contents_merge(self, builder, file_id):
 
257
        builder.add_file(file_id, builder.tree_root, "name1", "text1", True)
 
258
        builder.change_contents(file_id, other="text4", this="text3")
 
259
 
 
260
    def test_change_vs_change(self):
 
261
        """Hook is used for (changed, changed)"""
 
262
        self.install_hook_success()
 
263
        builder = self.make_merge_builder()
 
264
        builder.add_file("1", builder.tree_root, "name1", "text1", True)
 
265
        builder.change_contents("1", other="text4", this="text3")
 
266
        conflicts = builder.merge(self.merge_type)
 
267
        self.assertEqual(conflicts, [])
 
268
        self.assertEqual(
 
269
            builder.this.get_file('1').read(), 'text-merged-by-hook')
 
270
 
 
271
    def test_change_vs_deleted(self):
 
272
        """Hook is used for (changed, deleted)"""
 
273
        self.install_hook_success()
 
274
        builder = self.make_merge_builder()
 
275
        builder.add_file("1", builder.tree_root, "name1", "text1", True)
 
276
        builder.change_contents("1", this="text2")
 
277
        builder.remove_file("1", other=True)
 
278
        conflicts = builder.merge(self.merge_type)
 
279
        self.assertEqual(conflicts, [])
 
280
        self.assertEqual(
 
281
            builder.this.get_file('1').read(), 'text-merged-by-hook')
 
282
 
 
283
    def test_result_can_be_delete(self):
 
284
        """A hook's result can be the deletion of a file."""
 
285
        self.install_hook_delete()
 
286
        builder = self.make_merge_builder()
 
287
        self.create_file_needing_contents_merge(builder, "1")
 
288
        conflicts = builder.merge(self.merge_type)
 
289
        self.assertEqual(conflicts, [])
 
290
        self.assertRaises(errors.NoSuchId, builder.this.id2path, '1')
 
291
        self.assertEqual([], list(builder.this.list_files()))
 
292
 
 
293
    def test_result_can_be_conflict(self):
 
294
        """A hook's result can be a conflict."""
 
295
        self.install_hook_conflict()
 
296
        builder = self.make_merge_builder()
 
297
        self.create_file_needing_contents_merge(builder, "1")
 
298
        conflicts = builder.merge(self.merge_type)
 
299
        self.assertEqual(conflicts, [TextConflict('name1', file_id='1')])
 
300
        # The hook still gets to set the file contents in this case, so that it
 
301
        # can insert custom conflict markers.
 
302
        self.assertEqual(
 
303
            builder.this.get_file('1').read(),
 
304
            'text-with-conflict-markers-from-hook')
 
305
 
 
306
    def test_can_access_this_other_and_base_versions(self):
 
307
        """The hook function can call params.merger.get_lines to access the
 
308
        THIS/OTHER/BASE versions of the file.
 
309
        """
 
310
        self.install_hook_log_lines()
 
311
        builder = self.make_merge_builder()
 
312
        builder.add_file("1", builder.tree_root, "name1", "text1", True)
 
313
        builder.change_contents("1", this="text2", other="text3")
 
314
        conflicts = builder.merge(self.merge_type)
 
315
        self.assertEqual(
 
316
            [('log_lines', ['text2'], ['text3'], ['text1'])], self.hook_log)
 
317
 
 
318
    def test_chain_when_not_applicable(self):
 
319
        """When a hook function returns not_applicable, the next function is
 
320
        tried (when one exists).
 
321
        """
 
322
        self.install_hook_noop()
 
323
        self.install_hook_success()
 
324
        builder = self.make_merge_builder()
 
325
        self.create_file_needing_contents_merge(builder, "1")
 
326
        conflicts = builder.merge(self.merge_type)
 
327
        self.assertEqual(conflicts, [])
 
328
        self.assertEqual(
 
329
            builder.this.get_file('1').read(), 'text-merged-by-hook')
 
330
        self.assertEqual([('no-op',), ('success',)], self.hook_log)
 
331
 
 
332
    def test_chain_stops_after_success(self):
 
333
        """When a hook function returns success, no later functions are tried.
 
334
        """
 
335
        self.install_hook_success()
 
336
        self.install_hook_noop()
 
337
        builder = self.make_merge_builder()
 
338
        self.create_file_needing_contents_merge(builder, "1")
 
339
        conflicts = builder.merge(self.merge_type)
 
340
        self.assertEqual([('success',)], self.hook_log)
 
341
 
 
342
    def test_chain_stops_after_conflict(self):
 
343
        """When a hook function returns conflict, no later functions are tried.
 
344
        """
 
345
        self.install_hook_conflict()
 
346
        self.install_hook_noop()
 
347
        builder = self.make_merge_builder()
 
348
        self.create_file_needing_contents_merge(builder, "1")
 
349
        conflicts = builder.merge(self.merge_type)
 
350
        self.assertEqual([('conflict',)], self.hook_log)
 
351
 
 
352
    def test_chain_stops_after_delete(self):
 
353
        """When a hook function returns delete, no later functions are tried.
 
354
        """
 
355
        self.install_hook_delete()
 
356
        self.install_hook_noop()
 
357
        builder = self.make_merge_builder()
 
358
        self.create_file_needing_contents_merge(builder, "1")
 
359
        conflicts = builder.merge(self.merge_type)
 
360
        self.assertEqual([('delete',)], self.hook_log)
 
361