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):
233
226
output = StringIO.StringIO()
234
227
diff.internal_diff(u'old_\xb5', ['old_text\n'],
235
228
u'new_\xe5', ['new_text\n'], output)
236
self.assertIsInstance(output.getvalue(), str,
229
self.failUnless(isinstance(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()
319
249
self.assertEqual(out.splitlines(True) + ['\n'], lines)
322
def get_diff_as_string(tree1, tree2, specific_files=None, working_tree=None):
324
if working_tree is not None:
325
extra_trees = (working_tree,)
328
diff.show_diff_trees(tree1, tree2, output,
329
specific_files=specific_files,
330
extra_trees=extra_trees, old_label='old/',
332
return output.getvalue()
335
class TestDiffDates(tests.TestCaseWithTransport):
252
class TestShowDiffTreesHelper(tests.TestCaseWithTransport):
253
"""Has a helper for running show_diff_trees"""
255
def get_diff(self, tree1, tree2, specific_files=None, working_tree=None):
257
if working_tree is not None:
258
extra_trees = (working_tree,)
261
diff.show_diff_trees(tree1, tree2, output,
262
specific_files=specific_files,
263
extra_trees=extra_trees, old_label='old/',
265
return output.getvalue()
268
class TestDiffDates(TestShowDiffTreesHelper):
338
271
super(TestDiffDates, self).setUp()
453
386
self.wt.rename_one('file1', 'dir1/file1')
454
387
old_tree = self.b.repository.revision_tree('rev-1')
455
388
new_tree = self.b.repository.revision_tree('rev-4')
456
out = get_diff_as_string(old_tree, new_tree, specific_files=['dir1'],
389
out = self.get_diff(old_tree, new_tree, specific_files=['dir1'],
457
390
working_tree=self.wt)
458
391
self.assertContainsRe(out, 'file1\t')
459
out = get_diff_as_string(old_tree, new_tree, specific_files=['dir2'],
392
out = self.get_diff(old_tree, new_tree, specific_files=['dir2'],
460
393
working_tree=self.wt)
461
394
self.assertNotContainsRe(out, 'file1\t')
464
class TestShowDiffTrees(tests.TestCaseWithTransport):
398
class TestShowDiffTrees(TestShowDiffTreesHelper):
465
399
"""Direct tests for show_diff_trees"""
467
401
def test_modified_file(self):
864
798
b = ''.join([unichr(i) for i in range(4300, 4800, 2)])
865
799
sm = self._PatienceSequenceMatcher(None, a, b)
866
800
mb = sm.get_matching_blocks()
867
self.assertEqual(35, len(mb))
801
self.assertEquals(35, len(mb))
869
803
def test_unique_lcs(self):
870
804
unique_lcs = self._unique_lcs
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),
805
self.assertEquals(unique_lcs('', ''), [])
806
self.assertEquals(unique_lcs('', 'a'), [])
807
self.assertEquals(unique_lcs('a', ''), [])
808
self.assertEquals(unique_lcs('a', 'a'), [(0,0)])
809
self.assertEquals(unique_lcs('a', 'b'), [])
810
self.assertEquals(unique_lcs('ab', 'ab'), [(0,0), (1,1)])
811
self.assertEquals(unique_lcs('abcde', 'cdeab'), [(2,0), (3,1), (4,2)])
812
self.assertEquals(unique_lcs('cdeab', 'abcde'), [(0,2), (1,3), (2,4)])
813
self.assertEquals(unique_lcs('abXde', 'abYde'), [(0,0), (1,1),
881
self.assertEqual(unique_lcs('acbac', 'abc'), [(2,1)])
815
self.assertEquals(unique_lcs('acbac', 'abc'), [(2,1)])
883
817
def test_recurse_matches(self):
884
818
def test_one(a, b, matches):
885
819
test_matches = []
886
820
self._recurse_matches(
887
821
a, b, 0, 0, len(a), len(b), test_matches, 10)
888
self.assertEqual(test_matches, matches)
822
self.assertEquals(test_matches, matches)
890
824
test_one(['a', '', 'b', '', 'c'], ['a', 'a', 'b', 'c', 'c'],
891
825
[(0, 0), (2, 2), (4, 4)])
1317
1251
, list(unified_diff_files('a2', 'b2')))
1319
1253
# And the patience diff
1320
self.assertEqual(['--- a2\n',
1322
'@@ -4,6 +4,11 @@\n',
1334
list(unified_diff_files('a2', 'b2',
1335
sequencematcher=psm)))
1254
self.assertEquals(['--- a2\n',
1256
'@@ -4,6 +4,11 @@\n',
1269
, list(unified_diff_files('a2', 'b2',
1270
sequencematcher=psm)))
1338
1273
class TestPatienceDiffLibFiles_c(TestPatienceDiffLibFiles):
1340
_test_needs_features = [features.compiled_patiencediff_feature]
1275
_test_needs_features = [compiled_patiencediff_feature]
1342
1277
def setUp(self):
1343
1278
super(TestPatienceDiffLibFiles_c, self).setUp()
1490
1425
diff_obj._prepare_files('file2-id', 'oldname2', 'newname2')
1493
class TestDiffFromToolEncodedFilename(tests.TestCaseWithTransport):
1495
def test_encodable_filename(self):
1496
# Just checks file path for external diff tool.
1497
# We cannot change CPython's internal encoding used by os.exec*.
1498
diffobj = diff.DiffFromTool(['dummy', '@old_path', '@new_path'],
1500
for _, scenario in EncodingAdapter.encoding_scenarios:
1501
encoding = scenario['encoding']
1502
dirname = scenario['info']['directory']
1503
filename = scenario['info']['filename']
1505
self.overrideAttr(diffobj, '_fenc', lambda: encoding)
1506
relpath = dirname + u'/' + filename
1507
fullpath = diffobj._safe_filename('safe', relpath)
1508
self.assertEqual(fullpath,
1509
fullpath.encode(encoding).decode(encoding))
1510
self.assertTrue(fullpath.startswith(diffobj._root + '/safe'))
1512
def test_unencodable_filename(self):
1513
diffobj = diff.DiffFromTool(['dummy', '@old_path', '@new_path'],
1515
for _, scenario in EncodingAdapter.encoding_scenarios:
1516
encoding = scenario['encoding']
1517
dirname = scenario['info']['directory']
1518
filename = scenario['info']['filename']
1520
if encoding == 'iso-8859-1':
1521
encoding = 'iso-8859-2'
1523
encoding = 'iso-8859-1'
1525
self.overrideAttr(diffobj, '_fenc', lambda: encoding)
1526
relpath = dirname + u'/' + filename
1527
fullpath = diffobj._safe_filename('safe', relpath)
1528
self.assertEqual(fullpath,
1529
fullpath.encode(encoding).decode(encoding))
1530
self.assertTrue(fullpath.startswith(diffobj._root + '/safe'))
1533
1428
class TestGetTreesAndBranchesToDiffLocked(tests.TestCaseWithTransport):
1535
1430
def call_gtabtd(self, path_list, revision_specs, old_url, new_url):
1536
"""Call get_trees_and_branches_to_diff_locked."""
1431
"""Call get_trees_and_branches_to_diff_locked. Overridden by
1432
TestGetTreesAndBranchesToDiff.
1537
1434
return diff.get_trees_and_branches_to_diff_locked(
1538
1435
path_list, revision_specs, old_url, new_url, self.addCleanup)
1576
1473
self.assertEqual(tree.branch.base, new_branch.base)
1577
1474
self.assertIs(None, specific_files)
1578
1475
self.assertEqual(tree.basedir, extra_trees[0].basedir)
1478
class TestGetTreesAndBranchesToDiff(TestGetTreesAndBranchesToDiffLocked):
1479
"""Apply the tests for get_trees_and_branches_to_diff_locked to the
1480
deprecated get_trees_and_branches_to_diff function.
1483
def call_gtabtd(self, path_list, revision_specs, old_url, new_url):
1484
return self.applyDeprecated(
1485
deprecated_in((2, 2, 0)), diff.get_trees_and_branches_to_diff,
1486
path_list, revision_specs, old_url, new_url)