35
from bzrlib.symbol_versioning import deprecated_in
36
from bzrlib.tests import features
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):
144
151
self.check_patch(lines)
146
153
def test_external_diff_binary_lang_c(self):
148
154
for lang in ('LANG', 'LC_ALL', 'LANGUAGE'):
149
old_env[lang] = osutils.set_or_unset_env(lang, 'C')
151
lines = external_udiff_lines(['\x00foobar\n'], ['foo\x00bar\n'])
152
# Older versions of diffutils say "Binary files", newer
153
# versions just say "Files".
154
self.assertContainsRe(lines[0],
155
'(Binary f|F)iles old and new differ\n')
156
self.assertEquals(lines[1:], ['\n'])
158
for lang, old_val in old_env.iteritems():
159
osutils.set_or_unset_env(lang, old_val)
155
self.overrideEnv(lang, 'C')
156
lines = external_udiff_lines(['\x00foobar\n'], ['foo\x00bar\n'])
157
# Older versions of diffutils say "Binary files", newer
158
# versions just say "Files".
159
self.assertContainsRe(lines[0], '(Binary f|F)iles old and new differ\n')
160
self.assertEquals(lines[1:], ['\n'])
161
162
def test_no_external_diff(self):
162
163
"""Check that NoDiff is raised when diff is not available"""
163
# Use os.environ['PATH'] to make sure no 'diff' command is available
164
orig_path = os.environ['PATH']
166
os.environ['PATH'] = ''
167
self.assertRaises(errors.NoDiff, diff.external_diff,
168
'old', ['boo\n'], 'new', ['goo\n'],
169
StringIO(), diff_opts=['-u'])
171
os.environ['PATH'] = orig_path
164
# Make sure no 'diff' command is available
165
# XXX: Weird, using None instead of '' breaks the test -- vila 20101216
166
self.overrideEnv('PATH', '')
167
self.assertRaises(errors.NoDiff, diff.external_diff,
168
'old', ['boo\n'], 'new', ['goo\n'],
169
StringIO(), diff_opts=['-u'])
173
171
def test_internal_diff_default(self):
174
172
# Default internal diff encoding is utf8
235
233
output = StringIO.StringIO()
236
234
diff.internal_diff(u'old_\xb5', ['old_text\n'],
237
235
u'new_\xe5', ['new_text\n'], output)
238
self.failUnless(isinstance(output.getvalue(), str),
236
self.assertIsInstance(output.getvalue(), str,
239
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',
242
303
class TestDiffFiles(tests.TestCaseInTempDir):
247
308
lines = external_udiff_lines(['\x00foobar\n'], ['foo\x00bar\n'])
249
310
cmd = ['diff', '-u', '--binary', 'old', 'new']
250
open('old', 'wb').write('\x00foobar\n')
251
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')
252
313
pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE,
253
314
stdin=subprocess.PIPE)
254
315
out, err = pipe.communicate()
258
319
self.assertEqual(out.splitlines(True) + ['\n'], lines)
261
class TestShowDiffTreesHelper(tests.TestCaseWithTransport):
262
"""Has a helper for running show_diff_trees"""
264
def get_diff(self, tree1, tree2, specific_files=None, working_tree=None):
266
if working_tree is not None:
267
extra_trees = (working_tree,)
270
diff.show_diff_trees(tree1, tree2, output,
271
specific_files=specific_files,
272
extra_trees=extra_trees, old_label='old/',
274
return output.getvalue()
277
class TestDiffDates(TestShowDiffTreesHelper):
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):
280
338
super(TestDiffDates, self).setUp()
315
373
os.utime('file1', (1144195200, 1144195200)) # 2006-04-05 00:00:00 UTC
317
375
def test_diff_rev_tree_working_tree(self):
318
output = self.get_diff(self.wt.basis_tree(), self.wt)
376
output = get_diff_as_string(self.wt.basis_tree(), self.wt)
319
377
# note that the date for old/file1 is from rev 2 rather than from
320
378
# the basis revision (rev 4)
321
379
self.assertEqualDiff(output, '''\
331
389
def test_diff_rev_tree_rev_tree(self):
332
390
tree1 = self.b.repository.revision_tree('rev-2')
333
391
tree2 = self.b.repository.revision_tree('rev-3')
334
output = self.get_diff(tree1, tree2)
392
output = get_diff_as_string(tree1, tree2)
335
393
self.assertEqualDiff(output, '''\
336
394
=== modified file 'file2'
337
395
--- old/file2\t2006-04-01 00:00:00 +0000
345
403
def test_diff_add_files(self):
346
404
tree1 = self.b.repository.revision_tree(_mod_revision.NULL_REVISION)
347
405
tree2 = self.b.repository.revision_tree('rev-1')
348
output = self.get_diff(tree1, tree2)
406
output = get_diff_as_string(tree1, tree2)
349
407
# the files have the epoch time stamp for the tree in which
350
408
# they don't exist.
351
409
self.assertEqualDiff(output, '''\
366
424
def test_diff_remove_files(self):
367
425
tree1 = self.b.repository.revision_tree('rev-3')
368
426
tree2 = self.b.repository.revision_tree('rev-4')
369
output = self.get_diff(tree1, tree2)
427
output = get_diff_as_string(tree1, tree2)
370
428
# the file has the epoch time stamp for the tree in which
371
429
# it doesn't exist.
372
430
self.assertEqualDiff(output, '''\
383
441
self.wt.rename_one('file1', 'file1b')
384
442
old_tree = self.b.repository.revision_tree('rev-1')
385
443
new_tree = self.b.repository.revision_tree('rev-4')
386
out = self.get_diff(old_tree, new_tree, specific_files=['file1b'],
444
out = get_diff_as_string(old_tree, new_tree, specific_files=['file1b'],
387
445
working_tree=self.wt)
388
446
self.assertContainsRe(out, 'file1\t')
395
453
self.wt.rename_one('file1', 'dir1/file1')
396
454
old_tree = self.b.repository.revision_tree('rev-1')
397
455
new_tree = self.b.repository.revision_tree('rev-4')
398
out = self.get_diff(old_tree, new_tree, specific_files=['dir1'],
456
out = get_diff_as_string(old_tree, new_tree, specific_files=['dir1'],
399
457
working_tree=self.wt)
400
458
self.assertContainsRe(out, 'file1\t')
401
out = self.get_diff(old_tree, new_tree, specific_files=['dir2'],
459
out = get_diff_as_string(old_tree, new_tree, specific_files=['dir2'],
402
460
working_tree=self.wt)
403
461
self.assertNotContainsRe(out, 'file1\t')
407
class TestShowDiffTrees(TestShowDiffTreesHelper):
464
class TestShowDiffTrees(tests.TestCaseWithTransport):
408
465
"""Direct tests for show_diff_trees"""
410
467
def test_modified_file(self):
415
472
tree.commit('one', rev_id='rev-1')
417
474
self.build_tree_contents([('tree/file', 'new contents\n')])
418
d = self.get_diff(tree.basis_tree(), tree)
475
d = get_diff_as_string(tree.basis_tree(), tree)
419
476
self.assertContainsRe(d, "=== modified file 'file'\n")
420
477
self.assertContainsRe(d, '--- old/file\t')
421
478
self.assertContainsRe(d, '\\+\\+\\+ new/file\t')
433
490
tree.rename_one('dir', 'other')
434
491
self.build_tree_contents([('tree/other/file', 'new contents\n')])
435
d = self.get_diff(tree.basis_tree(), tree)
492
d = get_diff_as_string(tree.basis_tree(), tree)
436
493
self.assertContainsRe(d, "=== renamed directory 'dir' => 'other'\n")
437
494
self.assertContainsRe(d, "=== modified file 'other/file'\n")
438
495
# XXX: This is technically incorrect, because it used to be at another
451
508
tree.commit('one', rev_id='rev-1')
453
510
tree.rename_one('dir', 'newdir')
454
d = self.get_diff(tree.basis_tree(), tree)
511
d = get_diff_as_string(tree.basis_tree(), tree)
455
512
# Renaming a directory should be a single "you renamed this dir" even
456
513
# when there are files inside.
457
514
self.assertEqual(d, "=== renamed directory 'dir' => 'newdir'\n")
464
521
tree.commit('one', rev_id='rev-1')
466
523
tree.rename_one('file', 'newname')
467
d = self.get_diff(tree.basis_tree(), tree)
524
d = get_diff_as_string(tree.basis_tree(), tree)
468
525
self.assertContainsRe(d, "=== renamed file 'file' => 'newname'\n")
469
526
# We shouldn't have a --- or +++ line, because there is no content
480
537
tree.rename_one('file', 'newname')
481
538
self.build_tree_contents([('tree/newname', 'new contents\n')])
482
d = self.get_diff(tree.basis_tree(), tree)
539
d = get_diff_as_string(tree.basis_tree(), tree)
483
540
self.assertContainsRe(d, "=== renamed file 'file' => 'newname'\n")
484
541
self.assertContainsRe(d, '--- old/file\t')
485
542
self.assertContainsRe(d, '\\+\\+\\+ new/newname\t')
509
566
tree.rename_one('c', 'new-c')
510
567
tree.rename_one('d', 'new-d')
512
d = self.get_diff(tree.basis_tree(), tree)
569
d = get_diff_as_string(tree.basis_tree(), tree)
514
571
self.assertContainsRe(d, r"file 'a'.*\(properties changed:"
515
572
".*\+x to -x.*\)")
553
610
def test_unicode_filename(self):
554
611
"""Test when the filename are unicode."""
555
self.requireFeature(tests.UnicodeFilenameFeature)
612
self.requireFeature(features.UnicodeFilenameFeature)
557
614
alpha, omega = u'\u03b1', u'\u03c9'
558
615
autf8, outf8 = alpha.encode('utf8'), omega.encode('utf8')
573
630
tree.add(['add_'+alpha], ['file-id'])
574
631
self.build_tree_contents([('tree/mod_'+alpha, 'contents_mod\n')])
576
d = self.get_diff(tree.basis_tree(), tree)
633
d = get_diff_as_string(tree.basis_tree(), tree)
577
634
self.assertContainsRe(d,
578
635
"=== renamed file 'ren_%s' => 'ren_%s'\n"%(autf8, outf8))
579
636
self.assertContainsRe(d, "=== added file 'add_%s'"%autf8)
721
778
' \@\@\n-old\n\+new\n\n')
723
780
def test_diff_kind_change(self):
724
self.requireFeature(tests.SymlinkFeature)
781
self.requireFeature(features.SymlinkFeature)
725
782
self.build_tree_contents([('old-tree/olddir/',),
726
783
('old-tree/olddir/oldfile', 'old\n')])
727
784
self.old_tree.add('olddir')
1222
1279
'how are you today?\n']
1223
1280
txt_b = ['hello there\n',
1224
1281
'how are you today?\n']
1225
open('a1', 'wb').writelines(txt_a)
1226
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)
1228
1285
unified_diff_files = patiencediff.unified_diff_files
1229
1286
psm = self._PatienceSequenceMatcher
1240
1297
txt_a = map(lambda x: x+'\n', 'abcdefghijklmnop')
1241
1298
txt_b = map(lambda x: x+'\n', 'abcdefxydefghijklmnop')
1242
open('a2', 'wb').writelines(txt_a)
1243
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)
1245
1302
# This is the result with LongestCommonSubstring matching
1246
1303
self.assertEquals(['--- a2\n',
1293
1350
class TestUsingCompiledIfAvailable(tests.TestCase):
1295
1352
def test_PatienceSequenceMatcher(self):
1296
if compiled_patiencediff_feature.available():
1353
if features.compiled_patiencediff_feature.available():
1297
1354
from bzrlib._patiencediff_c import PatienceSequenceMatcher_c
1298
1355
self.assertIs(PatienceSequenceMatcher_c,
1299
1356
patiencediff.PatienceSequenceMatcher)
1359
1416
diff_obj._execute('old', 'new')
1360
1417
self.assertEqual(output.getvalue().rstrip(), 'old new')
1362
def test_excute_missing(self):
1419
def test_execute_missing(self):
1363
1420
diff_obj = diff.DiffFromTool(['a-tool-which-is-unlikely-to-exist'],
1364
1421
None, None, None)
1365
1422
self.addCleanup(diff_obj.finish)
1434
1491
diff_obj._prepare_files('file2-id', 'oldname2', 'newname2')
1494
class TestDiffFromToolEncodedFilename(tests.TestCaseWithTransport):
1496
def test_encodable_filename(self):
1497
# Just checks file path for external diff tool.
1498
# We cannot change CPython's internal encoding used by os.exec*.
1499
diffobj = diff.DiffFromTool(['dummy', '@old_path', '@new_path'],
1501
for _, scenario in EncodingAdapter.encoding_scenarios:
1502
encoding = scenario['encoding']
1503
dirname = scenario['info']['directory']
1504
filename = scenario['info']['filename']
1506
self.overrideAttr(diffobj, '_fenc', lambda: encoding)
1507
relpath = dirname + u'/' + filename
1508
fullpath = diffobj._safe_filename('safe', relpath)
1511
fullpath.encode(encoding).decode(encoding)
1513
self.assert_(fullpath.startswith(diffobj._root + '/safe'))
1515
def test_unencodable_filename(self):
1516
diffobj = diff.DiffFromTool(['dummy', '@old_path', '@new_path'],
1518
for _, scenario in EncodingAdapter.encoding_scenarios:
1519
encoding = scenario['encoding']
1520
dirname = scenario['info']['directory']
1521
filename = scenario['info']['filename']
1523
if encoding == 'iso-8859-1':
1524
encoding = 'iso-8859-2'
1526
encoding = 'iso-8859-1'
1528
self.overrideAttr(diffobj, '_fenc', lambda: encoding)
1529
relpath = dirname + u'/' + filename
1530
fullpath = diffobj._safe_filename('safe', relpath)
1533
fullpath.encode(encoding).decode(encoding)
1535
self.assert_(fullpath.startswith(diffobj._root + '/safe'))
1437
1538
class TestGetTreesAndBranchesToDiffLocked(tests.TestCaseWithTransport):
1439
1540
def call_gtabtd(self, path_list, revision_specs, old_url, new_url):
1440
"""Call get_trees_and_branches_to_diff_locked. Overridden by
1441
TestGetTreesAndBranchesToDiff.
1541
"""Call get_trees_and_branches_to_diff_locked."""
1443
1542
return diff.get_trees_and_branches_to_diff_locked(
1444
1543
path_list, revision_specs, old_url, new_url, self.addCleanup)
1482
1581
self.assertEqual(tree.branch.base, new_branch.base)
1483
1582
self.assertIs(None, specific_files)
1484
1583
self.assertEqual(tree.basedir, extra_trees[0].basedir)
1487
class TestGetTreesAndBranchesToDiff(TestGetTreesAndBranchesToDiffLocked):
1488
"""Apply the tests for get_trees_and_branches_to_diff_locked to the
1489
deprecated get_trees_and_branches_to_diff function.
1492
def call_gtabtd(self, path_list, revision_specs, old_url, new_url):
1493
return self.applyDeprecated(
1494
deprecated_in((2, 2, 0)), diff.get_trees_and_branches_to_diff,
1495
path_list, revision_specs, old_url, new_url)