35
35
from bzrlib.symbol_versioning import deprecated_in
36
from bzrlib.tests import features, EncodingAdapter
36
from bzrlib.tests import features
37
37
from bzrlib.tests.blackbox.test_diff import subst_dates
38
from bzrlib.tests import (
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')
43
61
def udiff_lines(old, new, allow_binary=False):
126
144
self.check_patch(lines)
128
146
def test_external_diff_binary_lang_c(self):
129
148
for lang in ('LANG', 'LC_ALL', 'LANGUAGE'):
130
self.overrideEnv(lang, 'C')
131
lines = external_udiff_lines(['\x00foobar\n'], ['foo\x00bar\n'])
132
# Older versions of diffutils say "Binary files", newer
133
# versions just say "Files".
134
self.assertContainsRe(lines[0], '(Binary f|F)iles old and new differ\n')
135
self.assertEquals(lines[1:], ['\n'])
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)
137
161
def test_no_external_diff(self):
138
162
"""Check that NoDiff is raised when diff is not available"""
139
# Make sure no 'diff' command is available
140
# XXX: Weird, using None instead of '' breaks the test -- vila 20101216
141
self.overrideEnv('PATH', '')
142
self.assertRaises(errors.NoDiff, diff.external_diff,
143
'old', ['boo\n'], 'new', ['goo\n'],
144
StringIO(), diff_opts=['-u'])
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
146
173
def test_internal_diff_default(self):
147
174
# Default internal diff encoding is utf8
208
235
output = StringIO.StringIO()
209
236
diff.internal_diff(u'old_\xb5', ['old_text\n'],
210
237
u'new_\xe5', ['new_text\n'], output)
211
self.assertIsInstance(output.getvalue(), str,
238
self.failUnless(isinstance(output.getvalue(), str),
212
239
'internal_diff should return bytestrings')
214
def test_internal_diff_default_context(self):
216
diff.internal_diff('old', ['same_text\n','same_text\n','same_text\n',
217
'same_text\n','same_text\n','old_text\n'],
218
'new', ['same_text\n','same_text\n','same_text\n',
219
'same_text\n','same_text\n','new_text\n'], output)
220
lines = output.getvalue().splitlines(True)
221
self.check_patch(lines)
222
self.assertEquals(['--- old\n',
234
def test_internal_diff_no_context(self):
236
diff.internal_diff('old', ['same_text\n','same_text\n','same_text\n',
237
'same_text\n','same_text\n','old_text\n'],
238
'new', ['same_text\n','same_text\n','same_text\n',
239
'same_text\n','same_text\n','new_text\n'], output,
241
lines = output.getvalue().splitlines(True)
242
self.check_patch(lines)
243
self.assertEquals(['--- old\n',
252
def test_internal_diff_more_context(self):
254
diff.internal_diff('old', ['same_text\n','same_text\n','same_text\n',
255
'same_text\n','same_text\n','old_text\n'],
256
'new', ['same_text\n','same_text\n','same_text\n',
257
'same_text\n','same_text\n','new_text\n'], output,
259
lines = output.getvalue().splitlines(True)
260
self.check_patch(lines)
261
self.assertEquals(['--- old\n',
278
242
class TestDiffFiles(tests.TestCaseInTempDir):
283
247
lines = external_udiff_lines(['\x00foobar\n'], ['foo\x00bar\n'])
285
249
cmd = ['diff', '-u', '--binary', 'old', 'new']
286
with open('old', 'wb') as f: f.write('\x00foobar\n')
287
with open('new', 'wb') as f: f.write('foo\x00bar\n')
250
open('old', 'wb').write('\x00foobar\n')
251
open('new', 'wb').write('foo\x00bar\n')
288
252
pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE,
289
253
stdin=subprocess.PIPE)
290
254
out, err = pipe.communicate()
294
258
self.assertEqual(out.splitlines(True) + ['\n'], lines)
297
def get_diff_as_string(tree1, tree2, specific_files=None, working_tree=None):
299
if working_tree is not None:
300
extra_trees = (working_tree,)
303
diff.show_diff_trees(tree1, tree2, output,
304
specific_files=specific_files,
305
extra_trees=extra_trees, old_label='old/',
307
return output.getvalue()
310
class TestDiffDates(tests.TestCaseWithTransport):
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):
313
280
super(TestDiffDates, self).setUp()
348
315
os.utime('file1', (1144195200, 1144195200)) # 2006-04-05 00:00:00 UTC
350
317
def test_diff_rev_tree_working_tree(self):
351
output = get_diff_as_string(self.wt.basis_tree(), self.wt)
318
output = self.get_diff(self.wt.basis_tree(), self.wt)
352
319
# note that the date for old/file1 is from rev 2 rather than from
353
320
# the basis revision (rev 4)
354
321
self.assertEqualDiff(output, '''\
364
331
def test_diff_rev_tree_rev_tree(self):
365
332
tree1 = self.b.repository.revision_tree('rev-2')
366
333
tree2 = self.b.repository.revision_tree('rev-3')
367
output = get_diff_as_string(tree1, tree2)
334
output = self.get_diff(tree1, tree2)
368
335
self.assertEqualDiff(output, '''\
369
336
=== modified file 'file2'
370
337
--- old/file2\t2006-04-01 00:00:00 +0000
378
345
def test_diff_add_files(self):
379
346
tree1 = self.b.repository.revision_tree(_mod_revision.NULL_REVISION)
380
347
tree2 = self.b.repository.revision_tree('rev-1')
381
output = get_diff_as_string(tree1, tree2)
348
output = self.get_diff(tree1, tree2)
382
349
# the files have the epoch time stamp for the tree in which
383
350
# they don't exist.
384
351
self.assertEqualDiff(output, '''\
399
366
def test_diff_remove_files(self):
400
367
tree1 = self.b.repository.revision_tree('rev-3')
401
368
tree2 = self.b.repository.revision_tree('rev-4')
402
output = get_diff_as_string(tree1, tree2)
369
output = self.get_diff(tree1, tree2)
403
370
# the file has the epoch time stamp for the tree in which
404
371
# it doesn't exist.
405
372
self.assertEqualDiff(output, '''\
416
383
self.wt.rename_one('file1', 'file1b')
417
384
old_tree = self.b.repository.revision_tree('rev-1')
418
385
new_tree = self.b.repository.revision_tree('rev-4')
419
out = get_diff_as_string(old_tree, new_tree, specific_files=['file1b'],
386
out = self.get_diff(old_tree, new_tree, specific_files=['file1b'],
420
387
working_tree=self.wt)
421
388
self.assertContainsRe(out, 'file1\t')
428
395
self.wt.rename_one('file1', 'dir1/file1')
429
396
old_tree = self.b.repository.revision_tree('rev-1')
430
397
new_tree = self.b.repository.revision_tree('rev-4')
431
out = get_diff_as_string(old_tree, new_tree, specific_files=['dir1'],
398
out = self.get_diff(old_tree, new_tree, specific_files=['dir1'],
432
399
working_tree=self.wt)
433
400
self.assertContainsRe(out, 'file1\t')
434
out = get_diff_as_string(old_tree, new_tree, specific_files=['dir2'],
401
out = self.get_diff(old_tree, new_tree, specific_files=['dir2'],
435
402
working_tree=self.wt)
436
403
self.assertNotContainsRe(out, 'file1\t')
439
class TestShowDiffTrees(tests.TestCaseWithTransport):
407
class TestShowDiffTrees(TestShowDiffTreesHelper):
440
408
"""Direct tests for show_diff_trees"""
442
410
def test_modified_file(self):
447
415
tree.commit('one', rev_id='rev-1')
449
417
self.build_tree_contents([('tree/file', 'new contents\n')])
450
d = get_diff_as_string(tree.basis_tree(), tree)
418
d = self.get_diff(tree.basis_tree(), tree)
451
419
self.assertContainsRe(d, "=== modified file 'file'\n")
452
420
self.assertContainsRe(d, '--- old/file\t')
453
421
self.assertContainsRe(d, '\\+\\+\\+ new/file\t')
465
433
tree.rename_one('dir', 'other')
466
434
self.build_tree_contents([('tree/other/file', 'new contents\n')])
467
d = get_diff_as_string(tree.basis_tree(), tree)
435
d = self.get_diff(tree.basis_tree(), tree)
468
436
self.assertContainsRe(d, "=== renamed directory 'dir' => 'other'\n")
469
437
self.assertContainsRe(d, "=== modified file 'other/file'\n")
470
438
# XXX: This is technically incorrect, because it used to be at another
483
451
tree.commit('one', rev_id='rev-1')
485
453
tree.rename_one('dir', 'newdir')
486
d = get_diff_as_string(tree.basis_tree(), tree)
454
d = self.get_diff(tree.basis_tree(), tree)
487
455
# Renaming a directory should be a single "you renamed this dir" even
488
456
# when there are files inside.
489
457
self.assertEqual(d, "=== renamed directory 'dir' => 'newdir'\n")
496
464
tree.commit('one', rev_id='rev-1')
498
466
tree.rename_one('file', 'newname')
499
d = get_diff_as_string(tree.basis_tree(), tree)
467
d = self.get_diff(tree.basis_tree(), tree)
500
468
self.assertContainsRe(d, "=== renamed file 'file' => 'newname'\n")
501
469
# We shouldn't have a --- or +++ line, because there is no content
512
480
tree.rename_one('file', 'newname')
513
481
self.build_tree_contents([('tree/newname', 'new contents\n')])
514
d = get_diff_as_string(tree.basis_tree(), tree)
482
d = self.get_diff(tree.basis_tree(), tree)
515
483
self.assertContainsRe(d, "=== renamed file 'file' => 'newname'\n")
516
484
self.assertContainsRe(d, '--- old/file\t')
517
485
self.assertContainsRe(d, '\\+\\+\\+ new/newname\t')
541
509
tree.rename_one('c', 'new-c')
542
510
tree.rename_one('d', 'new-d')
544
d = get_diff_as_string(tree.basis_tree(), tree)
512
d = self.get_diff(tree.basis_tree(), tree)
546
514
self.assertContainsRe(d, r"file 'a'.*\(properties changed:"
547
515
".*\+x to -x.*\)")
585
553
def test_unicode_filename(self):
586
554
"""Test when the filename are unicode."""
587
self.requireFeature(features.UnicodeFilenameFeature)
555
self.requireFeature(tests.UnicodeFilenameFeature)
589
557
alpha, omega = u'\u03b1', u'\u03c9'
590
558
autf8, outf8 = alpha.encode('utf8'), omega.encode('utf8')
605
573
tree.add(['add_'+alpha], ['file-id'])
606
574
self.build_tree_contents([('tree/mod_'+alpha, 'contents_mod\n')])
608
d = get_diff_as_string(tree.basis_tree(), tree)
576
d = self.get_diff(tree.basis_tree(), tree)
609
577
self.assertContainsRe(d,
610
578
"=== renamed file 'ren_%s' => 'ren_%s'\n"%(autf8, outf8))
611
579
self.assertContainsRe(d, "=== added file 'add_%s'"%autf8)
753
721
' \@\@\n-old\n\+new\n\n')
755
723
def test_diff_kind_change(self):
756
self.requireFeature(features.SymlinkFeature)
724
self.requireFeature(tests.SymlinkFeature)
757
725
self.build_tree_contents([('old-tree/olddir/',),
758
726
('old-tree/olddir/oldfile', 'old\n')])
759
727
self.old_tree.add('olddir')
1254
1222
'how are you today?\n']
1255
1223
txt_b = ['hello there\n',
1256
1224
'how are you today?\n']
1257
with open('a1', 'wb') as f: f.writelines(txt_a)
1258
with open('b1', 'wb') as f: f.writelines(txt_b)
1225
open('a1', 'wb').writelines(txt_a)
1226
open('b1', 'wb').writelines(txt_b)
1260
1228
unified_diff_files = patiencediff.unified_diff_files
1261
1229
psm = self._PatienceSequenceMatcher
1272
1240
txt_a = map(lambda x: x+'\n', 'abcdefghijklmnop')
1273
1241
txt_b = map(lambda x: x+'\n', 'abcdefxydefghijklmnop')
1274
with open('a2', 'wb') as f: f.writelines(txt_a)
1275
with open('b2', 'wb') as f: f.writelines(txt_b)
1242
open('a2', 'wb').writelines(txt_a)
1243
open('b2', 'wb').writelines(txt_b)
1277
1245
# This is the result with LongestCommonSubstring matching
1278
1246
self.assertEquals(['--- a2\n',
1325
1293
class TestUsingCompiledIfAvailable(tests.TestCase):
1327
1295
def test_PatienceSequenceMatcher(self):
1328
if features.compiled_patiencediff_feature.available():
1296
if compiled_patiencediff_feature.available():
1329
1297
from bzrlib._patiencediff_c import PatienceSequenceMatcher_c
1330
1298
self.assertIs(PatienceSequenceMatcher_c,
1331
1299
patiencediff.PatienceSequenceMatcher)
1466
1434
diff_obj._prepare_files('file2-id', 'oldname2', 'newname2')
1469
class TestDiffFromToolEncodedFilename(tests.TestCaseWithTransport):
1471
def test_encodable_filename(self):
1472
# Just checks file path for external diff tool.
1473
# We cannot change CPython's internal encoding used by os.exec*.
1475
diffobj = diff.DiffFromTool(['dummy', '@old_path', '@new_path'],
1477
for _, scenario in EncodingAdapter.encoding_scenarios:
1478
encoding = scenario['encoding']
1479
dirname = scenario['info']['directory']
1480
filename = scenario['info']['filename']
1482
self.overrideAttr(diffobj, '_fenc', lambda: encoding)
1483
relpath = dirname + u'/' + filename
1484
fullpath = diffobj._safe_filename('safe', relpath)
1487
fullpath.encode(encoding).decode(encoding)
1489
self.assert_(fullpath.startswith(diffobj._root + '/safe'))
1491
def test_unencodable_filename(self):
1493
diffobj = diff.DiffFromTool(['dummy', '@old_path', '@new_path'],
1495
for _, scenario in EncodingAdapter.encoding_scenarios:
1496
encoding = scenario['encoding']
1497
dirname = scenario['info']['directory']
1498
filename = scenario['info']['filename']
1500
if encoding == 'iso-8859-1':
1501
encoding = 'iso-8859-2'
1503
encoding = 'iso-8859-1'
1505
self.overrideAttr(diffobj, '_fenc', lambda: encoding)
1506
relpath = dirname + u'/' + filename
1507
fullpath = diffobj._safe_filename('safe', relpath)
1510
fullpath.encode(encoding).decode(encoding)
1512
self.assert_(fullpath.startswith(diffobj._root + '/safe'))
1515
1437
class TestGetTreesAndBranchesToDiffLocked(tests.TestCaseWithTransport):
1517
1439
def call_gtabtd(self, path_list, revision_specs, old_url, new_url):
1518
"""Call get_trees_and_branches_to_diff_locked."""
1440
"""Call get_trees_and_branches_to_diff_locked. Overridden by
1441
TestGetTreesAndBranchesToDiff.
1519
1443
return diff.get_trees_and_branches_to_diff_locked(
1520
1444
path_list, revision_specs, old_url, new_url, self.addCleanup)
1558
1482
self.assertEqual(tree.branch.base, new_branch.base)
1559
1483
self.assertIs(None, specific_files)
1560
1484
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)