34
from bzrlib.tests import (
35
from bzrlib.symbol_versioning import deprecated_in
36
from bzrlib.tests import features
38
37
from bzrlib.tests.blackbox.test_diff import subst_dates
39
from bzrlib.tests.scenarios import load_tests_apply_scenarios
42
load_tests = load_tests_apply_scenarios
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')
45
61
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)
91
84
class TestDiff(tests.TestCase):
93
86
def test_add_nl(self):
94
87
"""diff generates a valid diff for patches that add a newline"""
95
88
lines = udiff_lines(['boo'], ['boo\n'])
96
89
self.check_patch(lines)
97
self.assertEqual(lines[4], '\\ No newline at end of file\n')
90
self.assertEquals(lines[4], '\\ No newline at end of file\n')
98
91
## "expected no-nl, got %r" % lines[4]
100
93
def test_add_nl_2(self):
113
106
lines = udiff_lines(['boo\n'], ['boo'])
114
107
self.check_patch(lines)
115
self.assertEqual(lines[5], '\\ No newline at end of file\n')
108
self.assertEquals(lines[5], '\\ No newline at end of file\n')
116
109
## "expected no-nl, got %r" % lines[5]
118
111
def check_patch(self, lines):
119
self.assertTrue(len(lines) > 1)
112
self.assert_(len(lines) > 1)
120
113
## "Not enough lines for a file header for patch:\n%s" % "".join(lines)
121
self.assertTrue(lines[0].startswith ('---'))
114
self.assert_(lines[0].startswith ('---'))
122
115
## 'No orig line for patch:\n%s' % "".join(lines)
123
self.assertTrue(lines[1].startswith ('+++'))
116
self.assert_(lines[1].startswith ('+++'))
124
117
## 'No mod line for patch:\n%s' % "".join(lines)
125
self.assertTrue(len(lines) > 2)
118
self.assert_(len(lines) > 2)
126
119
## "No hunks for patch:\n%s" % "".join(lines)
127
self.assertTrue(lines[2].startswith('@@'))
120
self.assert_(lines[2].startswith('@@'))
128
121
## "No hunk header for patch:\n%s" % "".join(lines)
129
self.assertTrue('@@' in lines[2][2:])
122
self.assert_('@@' in lines[2][2:])
130
123
## "Unterminated hunk header for patch:\n%s" % "".join(lines)
132
125
def test_binary_lines(self):
236
229
self.assertIsInstance(output.getvalue(), str,
237
230
'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',
303
233
class TestDiffFiles(tests.TestCaseInTempDir):
308
238
lines = external_udiff_lines(['\x00foobar\n'], ['foo\x00bar\n'])
310
240
cmd = ['diff', '-u', '--binary', 'old', 'new']
311
with open('old', 'wb') as f: f.write('\x00foobar\n')
312
with open('new', 'wb') as f: f.write('foo\x00bar\n')
241
open('old', 'wb').write('\x00foobar\n')
242
open('new', 'wb').write('foo\x00bar\n')
313
243
pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE,
314
244
stdin=subprocess.PIPE)
315
245
out, err = pipe.communicate()
246
# Diff returns '2' on Binary files.
247
self.assertEqual(2, pipe.returncode)
316
248
# We should output whatever diff tells us, plus a trailing newline
317
249
self.assertEqual(out.splitlines(True) + ['\n'], lines)
862
794
b = ''.join([unichr(i) for i in range(4300, 4800, 2)])
863
795
sm = self._PatienceSequenceMatcher(None, a, b)
864
796
mb = sm.get_matching_blocks()
865
self.assertEqual(35, len(mb))
797
self.assertEquals(35, len(mb))
867
799
def test_unique_lcs(self):
868
800
unique_lcs = self._unique_lcs
869
self.assertEqual(unique_lcs('', ''), [])
870
self.assertEqual(unique_lcs('', 'a'), [])
871
self.assertEqual(unique_lcs('a', ''), [])
872
self.assertEqual(unique_lcs('a', 'a'), [(0,0)])
873
self.assertEqual(unique_lcs('a', 'b'), [])
874
self.assertEqual(unique_lcs('ab', 'ab'), [(0,0), (1,1)])
875
self.assertEqual(unique_lcs('abcde', 'cdeab'), [(2,0), (3,1), (4,2)])
876
self.assertEqual(unique_lcs('cdeab', 'abcde'), [(0,2), (1,3), (2,4)])
877
self.assertEqual(unique_lcs('abXde', 'abYde'), [(0,0), (1,1),
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),
879
self.assertEqual(unique_lcs('acbac', 'abc'), [(2,1)])
811
self.assertEquals(unique_lcs('acbac', 'abc'), [(2,1)])
881
813
def test_recurse_matches(self):
882
814
def test_one(a, b, matches):
883
815
test_matches = []
884
816
self._recurse_matches(
885
817
a, b, 0, 0, len(a), len(b), test_matches, 10)
886
self.assertEqual(test_matches, matches)
818
self.assertEquals(test_matches, matches)
888
820
test_one(['a', '', 'b', '', 'c'], ['a', 'a', 'b', 'c', 'c'],
889
821
[(0, 0), (2, 2), (4, 4)])
1315
1247
, list(unified_diff_files('a2', 'b2')))
1317
1249
# And the patience diff
1318
self.assertEqual(['--- a2\n',
1320
'@@ -4,6 +4,11 @@\n',
1332
list(unified_diff_files('a2', 'b2',
1333
sequencematcher=psm)))
1250
self.assertEquals(['--- a2\n',
1252
'@@ -4,6 +4,11 @@\n',
1265
, list(unified_diff_files('a2', 'b2',
1266
sequencematcher=psm)))
1336
1269
class TestPatienceDiffLibFiles_c(TestPatienceDiffLibFiles):
1338
_test_needs_features = [features.compiled_patiencediff_feature]
1271
_test_needs_features = [compiled_patiencediff_feature]
1340
1273
def setUp(self):
1341
1274
super(TestPatienceDiffLibFiles_c, self).setUp()
1488
1421
diff_obj._prepare_files('file2-id', 'oldname2', 'newname2')
1491
class TestDiffFromToolEncodedFilename(tests.TestCaseWithTransport):
1493
def test_encodable_filename(self):
1494
# Just checks file path for external diff tool.
1495
# We cannot change CPython's internal encoding used by os.exec*.
1496
diffobj = diff.DiffFromTool(['dummy', '@old_path', '@new_path'],
1498
for _, scenario in EncodingAdapter.encoding_scenarios:
1499
encoding = scenario['encoding']
1500
dirname = scenario['info']['directory']
1501
filename = scenario['info']['filename']
1503
self.overrideAttr(diffobj, '_fenc', lambda: encoding)
1504
relpath = dirname + u'/' + filename
1505
fullpath = diffobj._safe_filename('safe', relpath)
1506
self.assertEqual(fullpath,
1507
fullpath.encode(encoding).decode(encoding))
1508
self.assertTrue(fullpath.startswith(diffobj._root + '/safe'))
1510
def test_unencodable_filename(self):
1511
diffobj = diff.DiffFromTool(['dummy', '@old_path', '@new_path'],
1513
for _, scenario in EncodingAdapter.encoding_scenarios:
1514
encoding = scenario['encoding']
1515
dirname = scenario['info']['directory']
1516
filename = scenario['info']['filename']
1518
if encoding == 'iso-8859-1':
1519
encoding = 'iso-8859-2'
1521
encoding = 'iso-8859-1'
1523
self.overrideAttr(diffobj, '_fenc', lambda: encoding)
1524
relpath = dirname + u'/' + filename
1525
fullpath = diffobj._safe_filename('safe', relpath)
1526
self.assertEqual(fullpath,
1527
fullpath.encode(encoding).decode(encoding))
1528
self.assertTrue(fullpath.startswith(diffobj._root + '/safe'))
1531
1424
class TestGetTreesAndBranchesToDiffLocked(tests.TestCaseWithTransport):
1533
1426
def call_gtabtd(self, path_list, revision_specs, old_url, new_url):
1534
"""Call get_trees_and_branches_to_diff_locked."""
1427
"""Call get_trees_and_branches_to_diff_locked. Overridden by
1428
TestGetTreesAndBranchesToDiff.
1535
1430
return diff.get_trees_and_branches_to_diff_locked(
1536
1431
path_list, revision_specs, old_url, new_url, self.addCleanup)
1574
1469
self.assertEqual(tree.branch.base, new_branch.base)
1575
1470
self.assertIs(None, specific_files)
1576
1471
self.assertEqual(tree.basedir, extra_trees[0].basedir)
1474
class TestGetTreesAndBranchesToDiff(TestGetTreesAndBranchesToDiffLocked):
1475
"""Apply the tests for get_trees_and_branches_to_diff_locked to the
1476
deprecated get_trees_and_branches_to_diff function.
1479
def call_gtabtd(self, path_list, revision_specs, old_url, new_url):
1480
return self.applyDeprecated(
1481
deprecated_in((2, 2, 0)), diff.get_trees_and_branches_to_diff,
1482
path_list, revision_specs, old_url, new_url)