14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Implementation tests for breezy.merge.Merger."""
17
"""Implementation tests for bzrlib.merge.Merger."""
21
from ..conflicts import TextConflict
21
from bzrlib.conflicts import TextConflict
24
24
merge as _mod_merge,
26
from bzrlib.tests import (
28
28
TestCaseWithTransport,
30
from .test_merge_core import MergeBuilder
31
from ..transform import TreeTransform
35
def load_tests(loader, standard_tests, pattern):
30
from bzrlib.tests.test_merge_core import MergeBuilder
31
from bzrlib.transform import TreeTransform
35
def load_tests(standard_tests, module, loader):
36
36
"""Multiply tests for tranport implementations."""
37
37
result = loader.suiteClass()
44
44
class TestMergeImplementation(TestCaseWithTransport):
46
46
def do_merge(self, target_tree, source_tree, **kwargs):
47
merger = _mod_merge.Merger.from_revision_ids(
47
merger = _mod_merge.Merger.from_revision_ids(None,
48
48
target_tree, source_tree.last_revision(),
49
49
other_branch=source_tree.branch)
50
50
merger.merge_type=self.merge_type
57
57
this_tree.lock_write()
58
58
self.addCleanup(this_tree.unlock)
59
59
self.build_tree_contents([
60
('this/file1', b'a\nb\n'),
61
('this/file2', b'a\nb\n')
60
('this/file1', 'a\nb\n'),
61
('this/file2', 'a\nb\n')
63
63
this_tree.add(['file1', 'file2'])
64
64
this_tree.commit('Added files')
65
other_tree = this_tree.controldir.sprout('other').open_workingtree()
65
other_tree = this_tree.bzrdir.sprout('other').open_workingtree()
66
66
self.build_tree_contents([
67
('other/file1', b'a\nb\nc\n'),
68
('other/file2', b'a\nb\nc\n')
67
('other/file1', 'a\nb\nc\n'),
68
('other/file2', 'a\nb\nc\n')
70
70
other_tree.commit('modified both')
71
71
self.build_tree_contents([
72
('this/file1', b'd\na\nb\n'),
73
('this/file2', b'd\na\nb\n')
72
('this/file1', 'd\na\nb\n'),
73
('this/file2', 'd\na\nb\n')
75
75
this_tree.commit('modified both')
76
76
self.do_merge(this_tree, other_tree, interesting_files=['file1'])
77
self.assertFileEqual(b'd\na\nb\nc\n', 'this/file1')
78
self.assertFileEqual(b'd\na\nb\n', 'this/file2')
77
self.assertFileEqual('d\na\nb\nc\n', 'this/file1')
78
self.assertFileEqual('d\na\nb\n', 'this/file2')
80
80
def test_merge_move_and_change(self):
81
81
this_tree = self.make_branch_and_tree('this')
82
82
this_tree.lock_write()
83
83
self.addCleanup(this_tree.unlock)
84
84
self.build_tree_contents([
85
('this/file1', b'line 1\nline 2\nline 3\nline 4\n'),
85
('this/file1', 'line 1\nline 2\nline 3\nline 4\n'),
87
87
this_tree.add('file1',)
88
88
this_tree.commit('Added file')
89
other_tree = this_tree.controldir.sprout('other').open_workingtree()
89
other_tree = this_tree.bzrdir.sprout('other').open_workingtree()
90
90
self.build_tree_contents([
91
('other/file1', b'line 1\nline 2 to 2.1\nline 3\nline 4\n'),
91
('other/file1', 'line 1\nline 2 to 2.1\nline 3\nline 4\n'),
93
93
other_tree.commit('Changed 2 to 2.1')
94
94
self.build_tree_contents([
95
('this/file1', b'line 1\nline 3\nline 2\nline 4\n'),
95
('this/file1', 'line 1\nline 3\nline 2\nline 4\n'),
97
97
this_tree.commit('Swapped 2 & 3')
98
98
self.do_merge(this_tree, other_tree)
100
100
self.expectFailure(
101
101
"lca merge doesn't conflict for move and change",
102
102
self.assertFileEqual,
110
b'>>>>>>> MERGE-SOURCE\n'
111
b'line 4\n', 'this/file1')
110
'>>>>>>> MERGE-SOURCE\n'
111
'line 4\n', 'this/file1')
113
self.assertFileEqual(b'line 1\n'
120
b'>>>>>>> MERGE-SOURCE\n'
121
b'line 4\n', 'this/file1')
113
self.assertFileEqual('line 1\n'
120
'>>>>>>> MERGE-SOURCE\n'
121
'line 4\n', 'this/file1')
123
123
def test_modify_conflicts_with_delete(self):
124
124
# If one side deletes a line, and the other modifies that line, then
125
125
# the modification should be considered a conflict
126
126
builder = self.make_branch_builder('test')
127
127
builder.start_series()
128
builder.build_snapshot(None,
128
builder.build_snapshot('BASE-id', None,
129
129
[('add', ('', None, 'directory', None)),
130
('add', ('foo', b'foo-id', 'file', b'a\nb\nc\nd\ne\n')),
131
], revision_id=b'BASE-id')
130
('add', ('foo', 'foo-id', 'file', 'a\nb\nc\nd\ne\n')),
133
builder.build_snapshot([b'BASE-id'],
134
[('modify', ('foo', b'a\nc\nd\ne\n'))],
135
revision_id=b'OTHER-id')
133
builder.build_snapshot('OTHER-id', ['BASE-id'],
134
[('modify', ('foo-id', 'a\nc\nd\ne\n'))])
136
135
# Modify 'b\n', add 'X\n'
137
builder.build_snapshot([b'BASE-id'],
138
[('modify', ('foo', b'a\nb2\nc\nd\nX\ne\n'))],
139
revision_id=b'THIS-id')
136
builder.build_snapshot('THIS-id', ['BASE-id'],
137
[('modify', ('foo-id', 'a\nb2\nc\nd\nX\ne\n'))])
140
138
builder.finish_series()
141
139
branch = builder.get_branch()
142
this_tree = branch.controldir.create_workingtree()
140
this_tree = branch.bzrdir.create_workingtree()
143
141
this_tree.lock_write()
144
142
self.addCleanup(this_tree.unlock)
145
other_tree = this_tree.controldir.sprout('other', b'OTHER-id').open_workingtree()
143
other_tree = this_tree.bzrdir.sprout('other',
144
'OTHER-id').open_workingtree()
146
145
self.do_merge(this_tree, other_tree)
147
146
if self.merge_type is _mod_merge.LCAMerger:
148
147
self.expectFailure("lca merge doesn't track deleted lines",
149
148
self.assertFileEqual,
154
b'>>>>>>> MERGE-SOURCE\n'
153
'>>>>>>> MERGE-SOURCE\n'
160
159
self.assertFileEqual(
165
b'>>>>>>> MERGE-SOURCE\n'
164
'>>>>>>> MERGE-SOURCE\n'
171
170
def get_limbodir_deletiondir(self, wt):
172
171
transform = TreeTransform(wt)
301
300
def create_file_needing_contents_merge(self, builder, file_id):
302
builder.add_file(file_id, builder.tree_root, "name1", b"text1", True)
301
builder.add_file(file_id, builder.tree_root, "name1", "text1", True)
303
302
builder.change_contents(file_id, other="text4", this="text3")
305
304
def test_change_vs_change(self):
306
305
"""Hook is used for (changed, changed)"""
307
306
self.install_hook_success()
308
307
builder = self.make_merge_builder()
309
builder.add_file("1", builder.tree_root, "name1", b"text1", True)
308
builder.add_file("1", builder.tree_root, "name1", "text1", True)
310
309
builder.change_contents("1", other="text4", this="text3")
311
310
conflicts = builder.merge(self.merge_type)
312
311
self.assertEqual(conflicts, [])
313
312
self.assertEqual(
314
builder.this.get_file('name1').read(), 'text-merged-by-hook')
313
builder.this.get_file('1').read(), 'text-merged-by-hook')
316
315
def test_change_vs_deleted(self):
317
316
"""Hook is used for (changed, deleted)"""
318
317
self.install_hook_success()
319
318
builder = self.make_merge_builder()
320
builder.add_file("1", builder.tree_root, "name1", b"text1", True)
319
builder.add_file("1", builder.tree_root, "name1", "text1", True)
321
320
builder.change_contents("1", this="text2")
322
321
builder.remove_file("1", other=True)
323
322
conflicts = builder.merge(self.merge_type)
324
323
self.assertEqual(conflicts, [])
325
324
self.assertEqual(
326
builder.this.get_file('name1').read(), 'text-merged-by-hook')
325
builder.this.get_file('1').read(), 'text-merged-by-hook')
328
327
def test_result_can_be_delete(self):
329
328
"""A hook's result can be the deletion of a file."""
355
354
self.install_hook_log_lines()
356
355
builder = self.make_merge_builder()
357
builder.add_file("1", builder.tree_root, "name1", b"text1", True)
356
builder.add_file("1", builder.tree_root, "name1", "text1", True)
358
357
builder.change_contents("1", this="text2", other="text3")
359
358
conflicts = builder.merge(self.merge_type)
360
359
self.assertEqual(
369
368
conflicts = builder.merge(self.merge_type)
370
369
self.assertEqual(conflicts, [])
371
370
self.assertEqual(
372
builder.this.get_file('name1').read(), 'text-merged-by-hook')
371
builder.this.get_file('1').read(), 'text-merged-by-hook')
373
372
self.assertEqual([('inactive',), ('success',)], self.hook_log)
375
374
def test_chain_when_not_applicable(self):
383
382
conflicts = builder.merge(self.merge_type)
384
383
self.assertEqual(conflicts, [])
385
384
self.assertEqual(
386
builder.this.get_file('name1').read(), 'text-merged-by-hook')
385
builder.this.get_file('1').read(), 'text-merged-by-hook')
387
386
self.assertEqual([('no-op',), ('success',)], self.hook_log)
389
388
def test_chain_stops_after_success(self):