144
143
self.check_patch(lines)
146
145
def test_external_diff_binary_lang_c(self):
147
147
for lang in ('LANG', 'LC_ALL', 'LANGUAGE'):
148
self.overrideEnv(lang, 'C')
149
lines = external_udiff_lines(['\x00foobar\n'], ['foo\x00bar\n'])
150
# Older versions of diffutils say "Binary files", newer
151
# versions just say "Files".
152
self.assertContainsRe(lines[0], '(Binary f|F)iles old and new differ\n')
153
self.assertEquals(lines[1:], ['\n'])
148
old_env[lang] = osutils.set_or_unset_env(lang, 'C')
150
lines = external_udiff_lines(['\x00foobar\n'], ['foo\x00bar\n'])
151
# Older versions of diffutils say "Binary files", newer
152
# versions just say "Files".
153
self.assertContainsRe(lines[0],
154
'(Binary f|F)iles old and new differ\n')
155
self.assertEquals(lines[1:], ['\n'])
157
for lang, old_val in old_env.iteritems():
158
osutils.set_or_unset_env(lang, old_val)
155
160
def test_no_external_diff(self):
156
161
"""Check that NoDiff is raised when diff is not available"""
157
# Make sure no 'diff' command is available
158
# XXX: Weird, using None instead of '' breaks the test -- vila 20101216
159
self.overrideEnv('PATH', '')
160
self.assertRaises(errors.NoDiff, diff.external_diff,
161
'old', ['boo\n'], 'new', ['goo\n'],
162
StringIO(), diff_opts=['-u'])
162
# Use os.environ['PATH'] to make sure no 'diff' command is available
163
orig_path = os.environ['PATH']
165
os.environ['PATH'] = ''
166
self.assertRaises(errors.NoDiff, diff.external_diff,
167
'old', ['boo\n'], 'new', ['goo\n'],
168
StringIO(), diff_opts=['-u'])
170
os.environ['PATH'] = orig_path
164
172
def test_internal_diff_default(self):
165
173
# Default internal diff encoding is utf8
226
234
output = StringIO.StringIO()
227
235
diff.internal_diff(u'old_\xb5', ['old_text\n'],
228
236
u'new_\xe5', ['new_text\n'], output)
229
self.assertIsInstance(output.getvalue(), str,
237
self.failUnless(isinstance(output.getvalue(), str),
230
238
'internal_diff should return bytestrings')
249
257
self.assertEqual(out.splitlines(True) + ['\n'], lines)
252
def get_diff_as_string(tree1, tree2, specific_files=None, working_tree=None):
254
if working_tree is not None:
255
extra_trees = (working_tree,)
258
diff.show_diff_trees(tree1, tree2, output,
259
specific_files=specific_files,
260
extra_trees=extra_trees, old_label='old/',
262
return output.getvalue()
265
class TestDiffDates(tests.TestCaseWithTransport):
260
class TestShowDiffTreesHelper(tests.TestCaseWithTransport):
261
"""Has a helper for running show_diff_trees"""
263
def get_diff(self, tree1, tree2, specific_files=None, working_tree=None):
265
if working_tree is not None:
266
extra_trees = (working_tree,)
269
diff.show_diff_trees(tree1, tree2, output,
270
specific_files=specific_files,
271
extra_trees=extra_trees, old_label='old/',
273
return output.getvalue()
276
class TestDiffDates(TestShowDiffTreesHelper):
268
279
super(TestDiffDates, self).setUp()
303
314
os.utime('file1', (1144195200, 1144195200)) # 2006-04-05 00:00:00 UTC
305
316
def test_diff_rev_tree_working_tree(self):
306
output = get_diff_as_string(self.wt.basis_tree(), self.wt)
317
output = self.get_diff(self.wt.basis_tree(), self.wt)
307
318
# note that the date for old/file1 is from rev 2 rather than from
308
319
# the basis revision (rev 4)
309
320
self.assertEqualDiff(output, '''\
319
330
def test_diff_rev_tree_rev_tree(self):
320
331
tree1 = self.b.repository.revision_tree('rev-2')
321
332
tree2 = self.b.repository.revision_tree('rev-3')
322
output = get_diff_as_string(tree1, tree2)
333
output = self.get_diff(tree1, tree2)
323
334
self.assertEqualDiff(output, '''\
324
335
=== modified file 'file2'
325
336
--- old/file2\t2006-04-01 00:00:00 +0000
333
344
def test_diff_add_files(self):
334
345
tree1 = self.b.repository.revision_tree(_mod_revision.NULL_REVISION)
335
346
tree2 = self.b.repository.revision_tree('rev-1')
336
output = get_diff_as_string(tree1, tree2)
347
output = self.get_diff(tree1, tree2)
337
348
# the files have the epoch time stamp for the tree in which
338
349
# they don't exist.
339
350
self.assertEqualDiff(output, '''\
354
365
def test_diff_remove_files(self):
355
366
tree1 = self.b.repository.revision_tree('rev-3')
356
367
tree2 = self.b.repository.revision_tree('rev-4')
357
output = get_diff_as_string(tree1, tree2)
368
output = self.get_diff(tree1, tree2)
358
369
# the file has the epoch time stamp for the tree in which
359
370
# it doesn't exist.
360
371
self.assertEqualDiff(output, '''\
371
382
self.wt.rename_one('file1', 'file1b')
372
383
old_tree = self.b.repository.revision_tree('rev-1')
373
384
new_tree = self.b.repository.revision_tree('rev-4')
374
out = get_diff_as_string(old_tree, new_tree, specific_files=['file1b'],
385
out = self.get_diff(old_tree, new_tree, specific_files=['file1b'],
375
386
working_tree=self.wt)
376
387
self.assertContainsRe(out, 'file1\t')
383
394
self.wt.rename_one('file1', 'dir1/file1')
384
395
old_tree = self.b.repository.revision_tree('rev-1')
385
396
new_tree = self.b.repository.revision_tree('rev-4')
386
out = get_diff_as_string(old_tree, new_tree, specific_files=['dir1'],
397
out = self.get_diff(old_tree, new_tree, specific_files=['dir1'],
387
398
working_tree=self.wt)
388
399
self.assertContainsRe(out, 'file1\t')
389
out = get_diff_as_string(old_tree, new_tree, specific_files=['dir2'],
400
out = self.get_diff(old_tree, new_tree, specific_files=['dir2'],
390
401
working_tree=self.wt)
391
402
self.assertNotContainsRe(out, 'file1\t')
394
class TestShowDiffTrees(tests.TestCaseWithTransport):
406
class TestShowDiffTrees(TestShowDiffTreesHelper):
395
407
"""Direct tests for show_diff_trees"""
397
409
def test_modified_file(self):
402
414
tree.commit('one', rev_id='rev-1')
404
416
self.build_tree_contents([('tree/file', 'new contents\n')])
405
d = get_diff_as_string(tree.basis_tree(), tree)
417
d = self.get_diff(tree.basis_tree(), tree)
406
418
self.assertContainsRe(d, "=== modified file 'file'\n")
407
419
self.assertContainsRe(d, '--- old/file\t')
408
420
self.assertContainsRe(d, '\\+\\+\\+ new/file\t')
420
432
tree.rename_one('dir', 'other')
421
433
self.build_tree_contents([('tree/other/file', 'new contents\n')])
422
d = get_diff_as_string(tree.basis_tree(), tree)
434
d = self.get_diff(tree.basis_tree(), tree)
423
435
self.assertContainsRe(d, "=== renamed directory 'dir' => 'other'\n")
424
436
self.assertContainsRe(d, "=== modified file 'other/file'\n")
425
437
# XXX: This is technically incorrect, because it used to be at another
438
450
tree.commit('one', rev_id='rev-1')
440
452
tree.rename_one('dir', 'newdir')
441
d = get_diff_as_string(tree.basis_tree(), tree)
453
d = self.get_diff(tree.basis_tree(), tree)
442
454
# Renaming a directory should be a single "you renamed this dir" even
443
455
# when there are files inside.
444
456
self.assertEqual(d, "=== renamed directory 'dir' => 'newdir'\n")
451
463
tree.commit('one', rev_id='rev-1')
453
465
tree.rename_one('file', 'newname')
454
d = get_diff_as_string(tree.basis_tree(), tree)
466
d = self.get_diff(tree.basis_tree(), tree)
455
467
self.assertContainsRe(d, "=== renamed file 'file' => 'newname'\n")
456
468
# We shouldn't have a --- or +++ line, because there is no content
467
479
tree.rename_one('file', 'newname')
468
480
self.build_tree_contents([('tree/newname', 'new contents\n')])
469
d = get_diff_as_string(tree.basis_tree(), tree)
481
d = self.get_diff(tree.basis_tree(), tree)
470
482
self.assertContainsRe(d, "=== renamed file 'file' => 'newname'\n")
471
483
self.assertContainsRe(d, '--- old/file\t')
472
484
self.assertContainsRe(d, '\\+\\+\\+ new/newname\t')
496
508
tree.rename_one('c', 'new-c')
497
509
tree.rename_one('d', 'new-d')
499
d = get_diff_as_string(tree.basis_tree(), tree)
511
d = self.get_diff(tree.basis_tree(), tree)
501
513
self.assertContainsRe(d, r"file 'a'.*\(properties changed:"
502
514
".*\+x to -x.*\)")
509
521
self.assertNotContainsRe(d, r"file 'e'")
510
522
self.assertNotContainsRe(d, r"file 'f'")
512
525
def test_binary_unicode_filenames(self):
513
526
"""Test that contents of files are *not* encoded in UTF-8 when there
514
527
is a binary file in the diff.
560
573
tree.add(['add_'+alpha], ['file-id'])
561
574
self.build_tree_contents([('tree/mod_'+alpha, 'contents_mod\n')])
563
d = get_diff_as_string(tree.basis_tree(), tree)
576
d = self.get_diff(tree.basis_tree(), tree)
564
577
self.assertContainsRe(d,
565
578
"=== renamed file 'ren_%s' => 'ren_%s'\n"%(autf8, outf8))
566
579
self.assertContainsRe(d, "=== added file 'add_%s'"%autf8)
567
580
self.assertContainsRe(d, "=== modified file 'mod_%s'"%autf8)
568
581
self.assertContainsRe(d, "=== removed file 'del_%s'"%autf8)
570
def test_unicode_filename_path_encoding(self):
571
"""Test for bug #382699: unicode filenames on Windows should be shown
574
self.requireFeature(tests.UnicodeFilenameFeature)
575
# The word 'test' in Russian
576
_russian_test = u'\u0422\u0435\u0441\u0442'
577
directory = _russian_test + u'/'
578
test_txt = _russian_test + u'.txt'
579
u1234 = u'\u1234.txt'
581
tree = self.make_branch_and_tree('.')
582
self.build_tree_contents([
587
tree.add([test_txt, u1234, directory])
590
diff.show_diff_trees(tree.basis_tree(), tree, sio,
591
path_encoding='cp1251')
593
output = subst_dates(sio.getvalue())
595
=== added directory '%(directory)s'
596
=== added file '%(test_txt)s'
597
--- a/%(test_txt)s\tYYYY-MM-DD HH:MM:SS +ZZZZ
598
+++ b/%(test_txt)s\tYYYY-MM-DD HH:MM:SS +ZZZZ
602
=== added file '?.txt'
603
--- a/?.txt\tYYYY-MM-DD HH:MM:SS +ZZZZ
604
+++ b/?.txt\tYYYY-MM-DD HH:MM:SS +ZZZZ
608
''' % {'directory': _russian_test.encode('cp1251'),
609
'test_txt': test_txt.encode('cp1251'),
611
self.assertEqualDiff(output, shouldbe)
614
584
class DiffWasIs(diff.DiffPath):
1421
1391
diff_obj._prepare_files('file2-id', 'oldname2', 'newname2')
1424
class TestDiffFromToolEncodedFilename(tests.TestCaseWithTransport):
1426
def test_encodable_filename(self):
1427
# Just checks file path for external diff tool.
1428
# We cannot change CPython's internal encoding used by os.exec*.
1430
diffobj = diff.DiffFromTool(['dummy', '@old_path', '@new_path'],
1432
for _, scenario in EncodingAdapter.encoding_scenarios:
1433
encoding = scenario['encoding']
1434
dirname = scenario['info']['directory']
1435
filename = scenario['info']['filename']
1437
self.overrideAttr(diffobj, '_fenc', lambda: encoding)
1438
relpath = dirname + u'/' + filename
1439
fullpath = diffobj._safe_filename('safe', relpath)
1442
fullpath.encode(encoding).decode(encoding)
1444
self.assert_(fullpath.startswith(diffobj._root + '/safe'))
1446
def test_unencodable_filename(self):
1448
diffobj = diff.DiffFromTool(['dummy', '@old_path', '@new_path'],
1450
for _, scenario in EncodingAdapter.encoding_scenarios:
1451
encoding = scenario['encoding']
1452
dirname = scenario['info']['directory']
1453
filename = scenario['info']['filename']
1455
if encoding == 'iso-8859-1':
1456
encoding = 'iso-8859-2'
1458
encoding = 'iso-8859-1'
1460
self.overrideAttr(diffobj, '_fenc', lambda: encoding)
1461
relpath = dirname + u'/' + filename
1462
fullpath = diffobj._safe_filename('safe', relpath)
1465
fullpath.encode(encoding).decode(encoding)
1467
self.assert_(fullpath.startswith(diffobj._root + '/safe'))
1470
1394
class TestGetTreesAndBranchesToDiffLocked(tests.TestCaseWithTransport):
1472
1396
def call_gtabtd(self, path_list, revision_specs, old_url, new_url):
1526
1450
return self.applyDeprecated(
1527
1451
deprecated_in((2, 2, 0)), diff.get_trees_and_branches_to_diff,
1528
1452
path_list, revision_specs, old_url, new_url)