35
from bzrlib.symbol_versioning import deprecated_in
36
from bzrlib.tests import features, EncodingAdapter
34
from bzrlib.tests import (
37
38
from bzrlib.tests.blackbox.test_diff import subst_dates
40
class _AttribFeature(tests.Feature):
43
if (sys.platform not in ('cygwin', 'win32')):
46
proc = subprocess.Popen(['attrib', '.'], stdout=subprocess.PIPE)
49
return (0 == proc.wait())
51
def feature_name(self):
52
return 'attrib Windows command-line tool'
54
AttribFeature = _AttribFeature()
57
compiled_patiencediff_feature = tests.ModuleAvailableFeature(
58
'bzrlib._patiencediff_c')
39
from bzrlib.tests.scenarios import load_tests_apply_scenarios
42
load_tests = load_tests_apply_scenarios
61
45
def udiff_lines(old, new, allow_binary=False):
68
class TestDiffOptions(tests.TestCase):
70
def test_unified_added(self):
71
"""Check for default style '-u' only if no other style specified
74
# Verify that style defaults to unified, id est '-u' appended
75
# to option list, in the absence of an alternative style.
76
self.assertEqual(['-a', '-u'], diff.default_style_unified(['-a']))
79
class TestDiffOptionsScenarios(tests.TestCase):
81
scenarios = [(s, dict(style=s)) for s in diff.style_option_list]
82
style = None # Set by load_tests_apply_scenarios from scenarios
84
def test_unified_not_added(self):
85
# Verify that for all valid style options, '-u' is not
86
# appended to option list.
87
ret_opts = diff.default_style_unified(diff_opts=["%s" % (self.style,)])
88
self.assertEqual(["%s" % (self.style,)], ret_opts)
84
91
class TestDiff(tests.TestCase):
86
93
def test_add_nl(self):
87
94
"""diff generates a valid diff for patches that add a newline"""
88
95
lines = udiff_lines(['boo'], ['boo\n'])
89
96
self.check_patch(lines)
90
self.assertEquals(lines[4], '\\ No newline at end of file\n')
97
self.assertEqual(lines[4], '\\ No newline at end of file\n')
91
98
## "expected no-nl, got %r" % lines[4]
93
100
def test_add_nl_2(self):
106
113
lines = udiff_lines(['boo\n'], ['boo'])
107
114
self.check_patch(lines)
108
self.assertEquals(lines[5], '\\ No newline at end of file\n')
115
self.assertEqual(lines[5], '\\ No newline at end of file\n')
109
116
## "expected no-nl, got %r" % lines[5]
111
118
def check_patch(self, lines):
112
self.assert_(len(lines) > 1)
119
self.assertTrue(len(lines) > 1)
113
120
## "Not enough lines for a file header for patch:\n%s" % "".join(lines)
114
self.assert_(lines[0].startswith ('---'))
121
self.assertTrue(lines[0].startswith ('---'))
115
122
## 'No orig line for patch:\n%s' % "".join(lines)
116
self.assert_(lines[1].startswith ('+++'))
123
self.assertTrue(lines[1].startswith ('+++'))
117
124
## 'No mod line for patch:\n%s' % "".join(lines)
118
self.assert_(len(lines) > 2)
125
self.assertTrue(len(lines) > 2)
119
126
## "No hunks for patch:\n%s" % "".join(lines)
120
self.assert_(lines[2].startswith('@@'))
127
self.assertTrue(lines[2].startswith('@@'))
121
128
## "No hunk header for patch:\n%s" % "".join(lines)
122
self.assert_('@@' in lines[2][2:])
129
self.assertTrue('@@' in lines[2][2:])
123
130
## "Unterminated hunk header for patch:\n%s" % "".join(lines)
125
132
def test_binary_lines(self):
229
236
self.assertIsInstance(output.getvalue(), str,
230
237
'internal_diff should return bytestrings')
239
def test_internal_diff_default_context(self):
241
diff.internal_diff('old', ['same_text\n','same_text\n','same_text\n',
242
'same_text\n','same_text\n','old_text\n'],
243
'new', ['same_text\n','same_text\n','same_text\n',
244
'same_text\n','same_text\n','new_text\n'], output)
245
lines = output.getvalue().splitlines(True)
246
self.check_patch(lines)
247
self.assertEqual(['--- old\n',
259
def test_internal_diff_no_context(self):
261
diff.internal_diff('old', ['same_text\n','same_text\n','same_text\n',
262
'same_text\n','same_text\n','old_text\n'],
263
'new', ['same_text\n','same_text\n','same_text\n',
264
'same_text\n','same_text\n','new_text\n'], output,
266
lines = output.getvalue().splitlines(True)
267
self.check_patch(lines)
268
self.assertEqual(['--- old\n',
277
def test_internal_diff_more_context(self):
279
diff.internal_diff('old', ['same_text\n','same_text\n','same_text\n',
280
'same_text\n','same_text\n','old_text\n'],
281
'new', ['same_text\n','same_text\n','same_text\n',
282
'same_text\n','same_text\n','new_text\n'], output,
284
lines = output.getvalue().splitlines(True)
285
self.check_patch(lines)
286
self.assertEqual(['--- old\n',
233
303
class TestDiffFiles(tests.TestCaseInTempDir):
238
308
lines = external_udiff_lines(['\x00foobar\n'], ['foo\x00bar\n'])
240
310
cmd = ['diff', '-u', '--binary', 'old', 'new']
241
open('old', 'wb').write('\x00foobar\n')
242
open('new', 'wb').write('foo\x00bar\n')
311
with open('old', 'wb') as f: f.write('\x00foobar\n')
312
with open('new', 'wb') as f: f.write('foo\x00bar\n')
243
313
pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE,
244
314
stdin=subprocess.PIPE)
245
315
out, err = pipe.communicate()
794
864
b = ''.join([unichr(i) for i in range(4300, 4800, 2)])
795
865
sm = self._PatienceSequenceMatcher(None, a, b)
796
866
mb = sm.get_matching_blocks()
797
self.assertEquals(35, len(mb))
867
self.assertEqual(35, len(mb))
799
869
def test_unique_lcs(self):
800
870
unique_lcs = self._unique_lcs
801
self.assertEquals(unique_lcs('', ''), [])
802
self.assertEquals(unique_lcs('', 'a'), [])
803
self.assertEquals(unique_lcs('a', ''), [])
804
self.assertEquals(unique_lcs('a', 'a'), [(0,0)])
805
self.assertEquals(unique_lcs('a', 'b'), [])
806
self.assertEquals(unique_lcs('ab', 'ab'), [(0,0), (1,1)])
807
self.assertEquals(unique_lcs('abcde', 'cdeab'), [(2,0), (3,1), (4,2)])
808
self.assertEquals(unique_lcs('cdeab', 'abcde'), [(0,2), (1,3), (2,4)])
809
self.assertEquals(unique_lcs('abXde', 'abYde'), [(0,0), (1,1),
871
self.assertEqual(unique_lcs('', ''), [])
872
self.assertEqual(unique_lcs('', 'a'), [])
873
self.assertEqual(unique_lcs('a', ''), [])
874
self.assertEqual(unique_lcs('a', 'a'), [(0,0)])
875
self.assertEqual(unique_lcs('a', 'b'), [])
876
self.assertEqual(unique_lcs('ab', 'ab'), [(0,0), (1,1)])
877
self.assertEqual(unique_lcs('abcde', 'cdeab'), [(2,0), (3,1), (4,2)])
878
self.assertEqual(unique_lcs('cdeab', 'abcde'), [(0,2), (1,3), (2,4)])
879
self.assertEqual(unique_lcs('abXde', 'abYde'), [(0,0), (1,1),
811
self.assertEquals(unique_lcs('acbac', 'abc'), [(2,1)])
881
self.assertEqual(unique_lcs('acbac', 'abc'), [(2,1)])
813
883
def test_recurse_matches(self):
814
884
def test_one(a, b, matches):
815
885
test_matches = []
816
886
self._recurse_matches(
817
887
a, b, 0, 0, len(a), len(b), test_matches, 10)
818
self.assertEquals(test_matches, matches)
888
self.assertEqual(test_matches, matches)
820
890
test_one(['a', '', 'b', '', 'c'], ['a', 'a', 'b', 'c', 'c'],
821
891
[(0, 0), (2, 2), (4, 4)])
1247
1317
, list(unified_diff_files('a2', 'b2')))
1249
1319
# And the patience diff
1250
self.assertEquals(['--- a2\n',
1252
'@@ -4,6 +4,11 @@\n',
1265
, list(unified_diff_files('a2', 'b2',
1266
sequencematcher=psm)))
1320
self.assertEqual(['--- a2\n',
1322
'@@ -4,6 +4,11 @@\n',
1334
list(unified_diff_files('a2', 'b2',
1335
sequencematcher=psm)))
1269
1338
class TestPatienceDiffLibFiles_c(TestPatienceDiffLibFiles):
1271
_test_needs_features = [compiled_patiencediff_feature]
1340
_test_needs_features = [features.compiled_patiencediff_feature]
1273
1342
def setUp(self):
1274
1343
super(TestPatienceDiffLibFiles_c, self).setUp()
1426
1495
def test_encodable_filename(self):
1427
1496
# Just checks file path for external diff tool.
1428
1497
# We cannot change CPython's internal encoding used by os.exec*.
1430
1498
diffobj = diff.DiffFromTool(['dummy', '@old_path', '@new_path'],
1431
1499
None, None, None)
1432
1500
for _, scenario in EncodingAdapter.encoding_scenarios:
1433
1501
encoding = scenario['encoding']
1434
dirname = scenario['info']['directory']
1502
dirname = scenario['info']['directory']
1435
1503
filename = scenario['info']['filename']
1437
1505
self.overrideAttr(diffobj, '_fenc', lambda: encoding)
1438
1506
relpath = dirname + u'/' + filename
1439
1507
fullpath = diffobj._safe_filename('safe', relpath)
1442
fullpath.encode(encoding).decode(encoding)
1444
self.assert_(fullpath.startswith(diffobj._root + '/safe'))
1508
self.assertEqual(fullpath,
1509
fullpath.encode(encoding).decode(encoding))
1510
self.assertTrue(fullpath.startswith(diffobj._root + '/safe'))
1446
1512
def test_unencodable_filename(self):
1448
1513
diffobj = diff.DiffFromTool(['dummy', '@old_path', '@new_path'],
1449
1514
None, None, None)
1450
1515
for _, scenario in EncodingAdapter.encoding_scenarios:
1451
1516
encoding = scenario['encoding']
1452
dirname = scenario['info']['directory']
1517
dirname = scenario['info']['directory']
1453
1518
filename = scenario['info']['filename']
1455
1520
if encoding == 'iso-8859-1':
1460
1525
self.overrideAttr(diffobj, '_fenc', lambda: encoding)
1461
1526
relpath = dirname + u'/' + filename
1462
1527
fullpath = diffobj._safe_filename('safe', relpath)
1465
fullpath.encode(encoding).decode(encoding)
1467
self.assert_(fullpath.startswith(diffobj._root + '/safe'))
1528
self.assertEqual(fullpath,
1529
fullpath.encode(encoding).decode(encoding))
1530
self.assertTrue(fullpath.startswith(diffobj._root + '/safe'))
1470
1533
class TestGetTreesAndBranchesToDiffLocked(tests.TestCaseWithTransport):
1472
1535
def call_gtabtd(self, path_list, revision_specs, old_url, new_url):
1473
"""Call get_trees_and_branches_to_diff_locked. Overridden by
1474
TestGetTreesAndBranchesToDiff.
1536
"""Call get_trees_and_branches_to_diff_locked."""
1476
1537
return diff.get_trees_and_branches_to_diff_locked(
1477
1538
path_list, revision_specs, old_url, new_url, self.addCleanup)
1515
1576
self.assertEqual(tree.branch.base, new_branch.base)
1516
1577
self.assertIs(None, specific_files)
1517
1578
self.assertEqual(tree.basedir, extra_trees[0].basedir)
1520
class TestGetTreesAndBranchesToDiff(TestGetTreesAndBranchesToDiffLocked):
1521
"""Apply the tests for get_trees_and_branches_to_diff_locked to the
1522
deprecated get_trees_and_branches_to_diff function.
1525
def call_gtabtd(self, path_list, revision_specs, old_url, new_url):
1526
return self.applyDeprecated(
1527
deprecated_in((2, 2, 0)), diff.get_trees_and_branches_to_diff,
1528
path_list, revision_specs, old_url, new_url)