35
from bzrlib.symbol_versioning import deprecated_in
36
from bzrlib.tests import features, EncodingAdapter
37
from bzrlib.tests.blackbox.test_diff import subst_dates
38
34
from bzrlib.tests import (
38
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
43
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)
66
91
class TestDiff(tests.TestCase):
68
93
def test_add_nl(self):
211
236
self.assertIsInstance(output.getvalue(), str,
212
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.assertEquals(['--- 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.assertEquals(['--- 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.assertEquals(['--- old\n',
215
303
class TestDiffFiles(tests.TestCaseInTempDir):
220
308
lines = external_udiff_lines(['\x00foobar\n'], ['foo\x00bar\n'])
222
310
cmd = ['diff', '-u', '--binary', 'old', 'new']
223
open('old', 'wb').write('\x00foobar\n')
224
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')
225
313
pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE,
226
314
stdin=subprocess.PIPE)
227
315
out, err = pipe.communicate()
1191
1279
'how are you today?\n']
1192
1280
txt_b = ['hello there\n',
1193
1281
'how are you today?\n']
1194
open('a1', 'wb').writelines(txt_a)
1195
open('b1', 'wb').writelines(txt_b)
1282
with open('a1', 'wb') as f: f.writelines(txt_a)
1283
with open('b1', 'wb') as f: f.writelines(txt_b)
1197
1285
unified_diff_files = patiencediff.unified_diff_files
1198
1286
psm = self._PatienceSequenceMatcher
1209
1297
txt_a = map(lambda x: x+'\n', 'abcdefghijklmnop')
1210
1298
txt_b = map(lambda x: x+'\n', 'abcdefxydefghijklmnop')
1211
open('a2', 'wb').writelines(txt_a)
1212
open('b2', 'wb').writelines(txt_b)
1299
with open('a2', 'wb') as f: f.writelines(txt_a)
1300
with open('b2', 'wb') as f: f.writelines(txt_b)
1214
1302
# This is the result with LongestCommonSubstring matching
1215
1303
self.assertEquals(['--- a2\n',
1328
1416
diff_obj._execute('old', 'new')
1329
1417
self.assertEqual(output.getvalue().rstrip(), 'old new')
1331
def test_excute_missing(self):
1419
def test_execute_missing(self):
1332
1420
diff_obj = diff.DiffFromTool(['a-tool-which-is-unlikely-to-exist'],
1333
1421
None, None, None)
1334
1422
self.addCleanup(diff_obj.finish)
1408
1496
def test_encodable_filename(self):
1409
1497
# Just checks file path for external diff tool.
1410
1498
# We cannot change CPython's internal encoding used by os.exec*.
1412
1499
diffobj = diff.DiffFromTool(['dummy', '@old_path', '@new_path'],
1413
1500
None, None, None)
1414
1501
for _, scenario in EncodingAdapter.encoding_scenarios:
1426
1513
self.assert_(fullpath.startswith(diffobj._root + '/safe'))
1428
1515
def test_unencodable_filename(self):
1430
1516
diffobj = diff.DiffFromTool(['dummy', '@old_path', '@new_path'],
1431
1517
None, None, None)
1432
1518
for _, scenario in EncodingAdapter.encoding_scenarios: