1
# Copyright (C) 2005-2010 Canonical Ltd
1
# Copyright (C) 2004, 2005 by Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
11
# GNU General Public License for more details.
13
13
# You should have received a copy of the GNU General Public License
14
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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
from bzrlib.selftest import TestCaseInTempDir, TestCase
23
19
from bzrlib.merge3 import Merge3
24
from bzrlib.errors import CantReprocessAndShowBase, BinaryFile
26
21
def split_lines(t):
27
22
from cStringIO import StringIO
172
167
('unchanged', 1, 2),])
174
169
def test_append_a(self):
175
m3 = merge3.Merge3(['aaa\n', 'bbb\n'],
176
['aaa\n', 'bbb\n', '222\n'],
170
m3 = Merge3(['aaa\n', 'bbb\n'],
171
['aaa\n', 'bbb\n', '222\n'],
179
174
self.assertEquals(''.join(m3.merge_lines()),
180
175
'aaa\nbbb\n222\n')
182
177
def test_append_b(self):
183
m3 = merge3.Merge3(['aaa\n', 'bbb\n'],
185
['aaa\n', 'bbb\n', '222\n'])
178
m3 = Merge3(['aaa\n', 'bbb\n'],
180
['aaa\n', 'bbb\n', '222\n'])
187
182
self.assertEquals(''.join(m3.merge_lines()),
188
183
'aaa\nbbb\n222\n')
190
185
def test_append_agreement(self):
191
m3 = merge3.Merge3(['aaa\n', 'bbb\n'],
192
['aaa\n', 'bbb\n', '222\n'],
193
['aaa\n', 'bbb\n', '222\n'])
186
m3 = Merge3(['aaa\n', 'bbb\n'],
187
['aaa\n', 'bbb\n', '222\n'],
188
['aaa\n', 'bbb\n', '222\n'])
195
190
self.assertEquals(''.join(m3.merge_lines()),
196
191
'aaa\nbbb\n222\n')
198
193
def test_append_clash(self):
199
m3 = merge3.Merge3(['aaa\n', 'bbb\n'],
200
['aaa\n', 'bbb\n', '222\n'],
201
['aaa\n', 'bbb\n', '333\n'])
194
m3 = Merge3(['aaa\n', 'bbb\n'],
195
['aaa\n', 'bbb\n', '222\n'],
196
['aaa\n', 'bbb\n', '333\n'])
203
198
ml = m3.merge_lines(name_a='a',
301
296
def test_merge_poem(self):
302
297
"""Test case from diff3 manual"""
303
m3 = merge3.Merge3(TZU, LAO, TAO)
298
m3 = Merge3(TZU, LAO, TAO)
304
299
ml = list(m3.merge_lines('LAO', 'TAO'))
305
300
self.log('merge result:')
306
301
self.log(''.join(ml))
307
302
self.assertEquals(ml, MERGED_RESULT)
309
def test_minimal_conflicts_common(self):
311
base_text = ("a\n" * 20).splitlines(True)
312
this_text = ("a\n"*10+"b\n" * 10).splitlines(True)
313
other_text = ("a\n"*10+"c\n"+"b\n" * 8 + "c\n").splitlines(True)
314
m3 = merge3.Merge3(base_text, other_text, this_text)
315
m_lines = m3.merge_lines('OTHER', 'THIS', reprocess=True)
316
merged_text = "".join(list(m_lines))
317
optimal_text = ("a\n" * 10 + "<<<<<<< OTHER\nc\n"
318
+ 8* "b\n" + "c\n=======\n"
319
+ 10*"b\n" + ">>>>>>> THIS\n")
320
self.assertEqualDiff(optimal_text, merged_text)
322
def test_minimal_conflicts_unique(self):
324
"""Add a newline to each entry in the string"""
325
return [(x+'\n') for x in s]
327
base_text = add_newline("abcdefghijklm")
328
this_text = add_newline("abcdefghijklmNOPQRSTUVWXYZ")
329
other_text = add_newline("abcdefghijklm1OPQRSTUVWXY2")
330
m3 = merge3.Merge3(base_text, other_text, this_text)
331
m_lines = m3.merge_lines('OTHER', 'THIS', reprocess=True)
332
merged_text = "".join(list(m_lines))
333
optimal_text = ''.join(add_newline("abcdefghijklm")
334
+ ["<<<<<<< OTHER\n1\n=======\nN\n>>>>>>> THIS\n"]
335
+ add_newline('OPQRSTUVWXY')
336
+ ["<<<<<<< OTHER\n2\n=======\nZ\n>>>>>>> THIS\n"]
338
self.assertEqualDiff(optimal_text, merged_text)
340
def test_minimal_conflicts_nonunique(self):
342
"""Add a newline to each entry in the string"""
343
return [(x+'\n') for x in s]
345
base_text = add_newline("abacddefgghij")
346
this_text = add_newline("abacddefgghijkalmontfprz")
347
other_text = add_newline("abacddefgghijknlmontfprd")
348
m3 = merge3.Merge3(base_text, other_text, this_text)
349
m_lines = m3.merge_lines('OTHER', 'THIS', reprocess=True)
350
merged_text = "".join(list(m_lines))
351
optimal_text = ''.join(add_newline("abacddefgghijk")
352
+ ["<<<<<<< OTHER\nn\n=======\na\n>>>>>>> THIS\n"]
353
+ add_newline('lmontfpr')
354
+ ["<<<<<<< OTHER\nd\n=======\nz\n>>>>>>> THIS\n"]
356
self.assertEqualDiff(optimal_text, merged_text)
358
def test_reprocess_and_base(self):
359
"""Reprocessing and showing base breaks correctly"""
360
base_text = ("a\n" * 20).splitlines(True)
361
this_text = ("a\n"*10+"b\n" * 10).splitlines(True)
362
other_text = ("a\n"*10+"c\n"+"b\n" * 8 + "c\n").splitlines(True)
363
m3 = merge3.Merge3(base_text, other_text, this_text)
364
m_lines = m3.merge_lines('OTHER', 'THIS', reprocess=True,
365
base_marker='|||||||')
366
self.assertRaises(CantReprocessAndShowBase, list, m_lines)
368
def test_binary(self):
369
self.assertRaises(BinaryFile, merge3.Merge3, ['\x00'], ['a'], ['b'])
371
def test_dos_text(self):
375
m3 = merge3.Merge3(base_text.splitlines(True),
376
other_text.splitlines(True),
377
this_text.splitlines(True))
378
m_lines = m3.merge_lines('OTHER', 'THIS')
379
self.assertEqual('<<<<<<< OTHER\r\nc\r\n=======\r\nb\r\n'
380
'>>>>>>> THIS\r\n'.splitlines(True), list(m_lines))
382
def test_mac_text(self):
386
m3 = merge3.Merge3(base_text.splitlines(True),
387
other_text.splitlines(True),
388
this_text.splitlines(True))
389
m_lines = m3.merge_lines('OTHER', 'THIS')
390
self.assertEqual('<<<<<<< OTHER\rc\r=======\rb\r'
391
'>>>>>>> THIS\r'.splitlines(True), list(m_lines))
393
def test_merge3_cherrypick(self):
396
other_text = "a\nb\nc\n"
397
# When cherrypicking, lines in base are not part of the conflict
398
m3 = merge3.Merge3(base_text.splitlines(True),
399
this_text.splitlines(True),
400
other_text.splitlines(True), is_cherrypick=True)
401
m_lines = m3.merge_lines()
402
self.assertEqualDiff('a\n<<<<<<<\n=======\nc\n>>>>>>>\n',
405
# This is not symmetric
406
m3 = merge3.Merge3(base_text.splitlines(True),
407
other_text.splitlines(True),
408
this_text.splitlines(True), is_cherrypick=True)
409
m_lines = m3.merge_lines()
410
self.assertEqualDiff('a\n<<<<<<<\nb\nc\n=======\n>>>>>>>\n',
413
def test_merge3_cherrypick_w_mixed(self):
414
base_text = 'a\nb\nc\nd\ne\n'
415
this_text = 'a\nb\nq\n'
416
other_text = 'a\nb\nc\nd\nf\ne\ng\n'
417
# When cherrypicking, lines in base are not part of the conflict
418
m3 = merge3.Merge3(base_text.splitlines(True),
419
this_text.splitlines(True),
420
other_text.splitlines(True), is_cherrypick=True)
421
m_lines = m3.merge_lines()
422
self.assertEqualDiff('a\n'
435
def test_allow_objects(self):
436
"""Objects other than strs may be used with Merge3 when
439
merge_groups and merge_regions work with non-str input. Methods that
440
return lines like merge_lines fail.
442
base = [(x,x) for x in 'abcde']
443
a = [(x,x) for x in 'abcdef']
444
b = [(x,x) for x in 'Zabcde']
445
m3 = merge3.Merge3(base, a, b, allow_objects=True)
450
list(m3.merge_regions()))
452
[('b', [('Z', 'Z')]),
453
('unchanged', [(x,x) for x in 'abcde']),
454
('a', [('f', 'f')])],
455
list(m3.merge_groups()))