12 # |
12 # |
13 # You should have received a copy of the GNU General Public License |
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 |
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 |
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
16 |
16 |
17 |
17 import os |
18 from bzrlib.tests import TestCaseInTempDir, TestCase |
18 import unittest |
19 from bzrlib.merge3 import Merge3 |
19 from unittest import TestCase |
20 from bzrlib.errors import CantReprocessAndShowBase, BinaryFile |
20 import imp |
|
21 import shutil |
|
22 from mercurial import util |
|
23 |
|
24 # copy simplemerge to the cwd to avoid creating a .pyc file in the source tree |
|
25 shutil.copyfile(os.path.join(os.environ['TESTDIR'], os.path.pardir, |
|
26 'contrib', 'simplemerge'), |
|
27 'simplemerge.py') |
|
28 simplemerge = imp.load_source('simplemerge', 'simplemerge.py') |
|
29 Merge3 = simplemerge.Merge3 |
|
30 CantReprocessAndShowBase = simplemerge.CantReprocessAndShowBase |
21 |
31 |
22 def split_lines(t): |
32 def split_lines(t): |
23 from cStringIO import StringIO |
33 from cStringIO import StringIO |
24 return StringIO(t).readlines() |
34 return StringIO(t).readlines() |
25 |
35 |
308 this_text = ("a\n"*10+"b\n" * 10).splitlines(True) |
320 this_text = ("a\n"*10+"b\n" * 10).splitlines(True) |
309 other_text = ("a\n"*10+"c\n"+"b\n" * 8 + "c\n").splitlines(True) |
321 other_text = ("a\n"*10+"c\n"+"b\n" * 8 + "c\n").splitlines(True) |
310 m3 = Merge3(base_text, other_text, this_text) |
322 m3 = Merge3(base_text, other_text, this_text) |
311 m_lines = m3.merge_lines('OTHER', 'THIS', reprocess=True) |
323 m_lines = m3.merge_lines('OTHER', 'THIS', reprocess=True) |
312 merged_text = "".join(list(m_lines)) |
324 merged_text = "".join(list(m_lines)) |
313 optimal_text = ("a\n" * 10 + "<<<<<<< OTHER\nc\n" |
325 optimal_text = ("a\n" * 10 + "<<<<<<< OTHER\nc\n=======\n" |
314 + 8* "b\n" + "c\n=======\n" |
326 + ">>>>>>> THIS\n" |
315 + 10*"b\n" + ">>>>>>> THIS\n") |
327 + 8* "b\n" + "<<<<<<< OTHER\nc\n=======\n" |
316 self.assertEqualDiff(optimal_text, merged_text) |
328 + 2* "b\n" + ">>>>>>> THIS\n") |
|
329 self.assertEquals(optimal_text, merged_text) |
317 |
330 |
318 def test_minimal_conflicts_unique(self): |
331 def test_minimal_conflicts_unique(self): |
319 def add_newline(s): |
332 def add_newline(s): |
320 """Add a newline to each entry in the string""" |
333 """Add a newline to each entry in the string""" |
321 return [(x+'\n') for x in s] |
334 return [(x+'\n') for x in s] |
329 optimal_text = ''.join(add_newline("abcdefghijklm") |
342 optimal_text = ''.join(add_newline("abcdefghijklm") |
330 + ["<<<<<<< OTHER\n1\n=======\nN\n>>>>>>> THIS\n"] |
343 + ["<<<<<<< OTHER\n1\n=======\nN\n>>>>>>> THIS\n"] |
331 + add_newline('OPQRSTUVWXY') |
344 + add_newline('OPQRSTUVWXY') |
332 + ["<<<<<<< OTHER\n2\n=======\nZ\n>>>>>>> THIS\n"] |
345 + ["<<<<<<< OTHER\n2\n=======\nZ\n>>>>>>> THIS\n"] |
333 ) |
346 ) |
334 self.assertEqualDiff(optimal_text, merged_text) |
347 self.assertEquals(optimal_text, merged_text) |
335 |
348 |
336 def test_minimal_conflicts_nonunique(self): |
349 def test_minimal_conflicts_nonunique(self): |
337 def add_newline(s): |
350 def add_newline(s): |
338 """Add a newline to each entry in the string""" |
351 """Add a newline to each entry in the string""" |
339 return [(x+'\n') for x in s] |
352 return [(x+'\n') for x in s] |
347 optimal_text = ''.join(add_newline("abacddefgghijk") |
360 optimal_text = ''.join(add_newline("abacddefgghijk") |
348 + ["<<<<<<< OTHER\nn\n=======\na\n>>>>>>> THIS\n"] |
361 + ["<<<<<<< OTHER\nn\n=======\na\n>>>>>>> THIS\n"] |
349 + add_newline('lmontfpr') |
362 + add_newline('lmontfpr') |
350 + ["<<<<<<< OTHER\nd\n=======\nz\n>>>>>>> THIS\n"] |
363 + ["<<<<<<< OTHER\nd\n=======\nz\n>>>>>>> THIS\n"] |
351 ) |
364 ) |
352 self.assertEqualDiff(optimal_text, merged_text) |
365 self.assertEquals(optimal_text, merged_text) |
353 |
366 |
354 def test_reprocess_and_base(self): |
367 def test_reprocess_and_base(self): |
355 """Reprocessing and showing base breaks correctly""" |
368 """Reprocessing and showing base breaks correctly""" |
356 base_text = ("a\n" * 20).splitlines(True) |
369 base_text = ("a\n" * 20).splitlines(True) |
357 this_text = ("a\n"*10+"b\n" * 10).splitlines(True) |
370 this_text = ("a\n"*10+"b\n" * 10).splitlines(True) |
360 m_lines = m3.merge_lines('OTHER', 'THIS', reprocess=True, |
373 m_lines = m3.merge_lines('OTHER', 'THIS', reprocess=True, |
361 base_marker='|||||||') |
374 base_marker='|||||||') |
362 self.assertRaises(CantReprocessAndShowBase, list, m_lines) |
375 self.assertRaises(CantReprocessAndShowBase, list, m_lines) |
363 |
376 |
364 def test_binary(self): |
377 def test_binary(self): |
365 self.assertRaises(BinaryFile, Merge3, ['\x00'], ['a'], ['b']) |
378 self.assertRaises(util.Abort, Merge3, ['\x00'], ['a'], ['b']) |
366 |
379 |
367 def test_dos_text(self): |
380 def test_dos_text(self): |
368 base_text = 'a\r\n' |
381 base_text = 'a\r\n' |
369 this_text = 'b\r\n' |
382 this_text = 'b\r\n' |
370 other_text = 'c\r\n' |
383 other_text = 'c\r\n' |
381 m3 = Merge3(base_text.splitlines(True), other_text.splitlines(True), |
394 m3 = Merge3(base_text.splitlines(True), other_text.splitlines(True), |
382 this_text.splitlines(True)) |
395 this_text.splitlines(True)) |
383 m_lines = m3.merge_lines('OTHER', 'THIS') |
396 m_lines = m3.merge_lines('OTHER', 'THIS') |
384 self.assertEqual('<<<<<<< OTHER\rc\r=======\rb\r' |
397 self.assertEqual('<<<<<<< OTHER\rc\r=======\rb\r' |
385 '>>>>>>> THIS\r'.splitlines(True), list(m_lines)) |
398 '>>>>>>> THIS\r'.splitlines(True), list(m_lines)) |
|
399 |
|
400 if __name__ == '__main__': |
|
401 # hide the timer |
|
402 import time |
|
403 orig = time.time |
|
404 try: |
|
405 time.time = lambda: 0 |
|
406 unittest.main() |
|
407 finally: |
|
408 time.time = orig |
|
409 |