13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19
from cStringIO import StringIO
22
from tempfile import TemporaryFile
29
revision as _mod_revision,
24
from bzrlib import tests
25
from bzrlib.diff import (
35
from bzrlib.symbol_versioning import deprecated_in
36
from bzrlib.tests import features
37
from bzrlib.tests.blackbox.test_diff import subst_dates
40
class _AttribFeature(tests.Feature):
35
from bzrlib.errors import BinaryFile, NoDiff, ExecutableMissing
36
import bzrlib.osutils as osutils
37
import bzrlib.patiencediff
38
import bzrlib._patiencediff_py
39
from bzrlib.tests import (Feature, TestCase, TestCaseWithTransport,
40
TestCaseInTempDir, TestSkipped)
43
class _CompiledPatienceDiffFeature(Feature):
43
if (sys.platform not in ('cygwin', 'win32')):
46
proc = subprocess.Popen(['attrib', '.'], stdout=subprocess.PIPE)
47
import bzrlib._patiencediff_c
49
return (0 == proc.wait())
51
52
def feature_name(self):
52
return 'attrib Windows command-line tool'
54
AttribFeature = _AttribFeature()
57
compiled_patiencediff_feature = tests.ModuleAvailableFeature(
58
'bzrlib._patiencediff_c')
53
return 'bzrlib._patiencediff_c'
55
CompiledPatienceDiffFeature = _CompiledPatienceDiffFeature()
58
class _UnicodeFilename(Feature):
59
"""Does the filesystem support Unicode filenames?"""
64
except UnicodeEncodeError:
66
except (IOError, OSError):
67
# The filesystem allows the Unicode filename but the file doesn't
71
# The filesystem allows the Unicode filename and the file exists,
75
UnicodeFilename = _UnicodeFilename()
78
class TestUnicodeFilename(TestCase):
80
def test_probe_passes(self):
81
"""UnicodeFilename._probe passes."""
82
# We can't test much more than that because the behaviour depends
84
UnicodeFilename._probe()
61
87
def udiff_lines(old, new, allow_binary=False):
62
88
output = StringIO()
63
diff.internal_diff('old', old, 'new', new, output, allow_binary)
89
internal_diff('old', old, 'new', new, output, allow_binary)
65
91
return output.readlines()
480
503
tree.rename_one('file', 'newname')
481
504
self.build_tree_contents([('tree/newname', 'new contents\n')])
482
d = self.get_diff(tree.basis_tree(), tree)
483
self.assertContainsRe(d, "=== renamed file 'file' => 'newname'\n")
484
self.assertContainsRe(d, '--- old/file\t')
485
self.assertContainsRe(d, '\\+\\+\\+ new/newname\t')
486
self.assertContainsRe(d, '-contents\n'
490
def test_internal_diff_exec_property(self):
491
tree = self.make_branch_and_tree('tree')
493
tt = transform.TreeTransform(tree)
494
tt.new_file('a', tt.root, 'contents\n', 'a-id', True)
495
tt.new_file('b', tt.root, 'contents\n', 'b-id', False)
496
tt.new_file('c', tt.root, 'contents\n', 'c-id', True)
497
tt.new_file('d', tt.root, 'contents\n', 'd-id', False)
498
tt.new_file('e', tt.root, 'contents\n', 'control-e-id', True)
499
tt.new_file('f', tt.root, 'contents\n', 'control-f-id', False)
501
tree.commit('one', rev_id='rev-1')
503
tt = transform.TreeTransform(tree)
504
tt.set_executability(False, tt.trans_id_file_id('a-id'))
505
tt.set_executability(True, tt.trans_id_file_id('b-id'))
506
tt.set_executability(False, tt.trans_id_file_id('c-id'))
507
tt.set_executability(True, tt.trans_id_file_id('d-id'))
509
tree.rename_one('c', 'new-c')
510
tree.rename_one('d', 'new-d')
512
d = self.get_diff(tree.basis_tree(), tree)
514
self.assertContainsRe(d, r"file 'a'.*\(properties changed:"
516
self.assertContainsRe(d, r"file 'b'.*\(properties changed:"
518
self.assertContainsRe(d, r"file 'c'.*\(properties changed:"
520
self.assertContainsRe(d, r"file 'd'.*\(properties changed:"
522
self.assertNotContainsRe(d, r"file 'e'")
523
self.assertNotContainsRe(d, r"file 'f'")
505
diff = self.get_diff(tree.basis_tree(), tree)
506
self.assertContainsRe(diff, "=== renamed file 'file' => 'newname'\n")
507
self.assertContainsRe(diff, '--- old/file\t')
508
self.assertContainsRe(diff, '\\+\\+\\+ new/newname\t')
509
self.assertContainsRe(diff, '-contents\n'
525
512
def test_binary_unicode_filenames(self):
526
513
"""Test that contents of files are *not* encoded in UTF-8 when there
527
514
is a binary file in the diff.
529
516
# See https://bugs.launchpad.net/bugs/110092.
530
self.requireFeature(tests.UnicodeFilenameFeature)
517
self.requireFeature(UnicodeFilename)
532
519
# This bug isn't triggered with cStringIO.
533
520
from StringIO import StringIO
1282
1198
class TestPatienceDiffLibFiles_c(TestPatienceDiffLibFiles):
1284
_test_needs_features = [compiled_patiencediff_feature]
1200
_test_needs_features = [CompiledPatienceDiffFeature]
1286
1202
def setUp(self):
1287
1203
super(TestPatienceDiffLibFiles_c, self).setUp()
1288
from bzrlib import _patiencediff_c
1204
import bzrlib._patiencediff_c
1289
1205
self._PatienceSequenceMatcher = \
1290
_patiencediff_c.PatienceSequenceMatcher_c
1293
class TestUsingCompiledIfAvailable(tests.TestCase):
1206
bzrlib._patiencediff_c.PatienceSequenceMatcher_c
1209
class TestUsingCompiledIfAvailable(TestCase):
1295
1211
def test_PatienceSequenceMatcher(self):
1296
if compiled_patiencediff_feature.available():
1212
if CompiledPatienceDiffFeature.available():
1297
1213
from bzrlib._patiencediff_c import PatienceSequenceMatcher_c
1298
1214
self.assertIs(PatienceSequenceMatcher_c,
1299
patiencediff.PatienceSequenceMatcher)
1215
bzrlib.patiencediff.PatienceSequenceMatcher)
1301
1217
from bzrlib._patiencediff_py import PatienceSequenceMatcher_py
1302
1218
self.assertIs(PatienceSequenceMatcher_py,
1303
patiencediff.PatienceSequenceMatcher)
1219
bzrlib.patiencediff.PatienceSequenceMatcher)
1305
1221
def test_unique_lcs(self):
1306
if compiled_patiencediff_feature.available():
1222
if CompiledPatienceDiffFeature.available():
1307
1223
from bzrlib._patiencediff_c import unique_lcs_c
1308
1224
self.assertIs(unique_lcs_c,
1309
patiencediff.unique_lcs)
1225
bzrlib.patiencediff.unique_lcs)
1311
1227
from bzrlib._patiencediff_py import unique_lcs_py
1312
1228
self.assertIs(unique_lcs_py,
1313
patiencediff.unique_lcs)
1229
bzrlib.patiencediff.unique_lcs)
1315
1231
def test_recurse_matches(self):
1316
if compiled_patiencediff_feature.available():
1232
if CompiledPatienceDiffFeature.available():
1317
1233
from bzrlib._patiencediff_c import recurse_matches_c
1318
1234
self.assertIs(recurse_matches_c,
1319
patiencediff.recurse_matches)
1235
bzrlib.patiencediff.recurse_matches)
1321
1237
from bzrlib._patiencediff_py import recurse_matches_py
1322
1238
self.assertIs(recurse_matches_py,
1323
patiencediff.recurse_matches)
1326
class TestDiffFromTool(tests.TestCaseWithTransport):
1239
bzrlib.patiencediff.recurse_matches)
1242
class TestDiffFromTool(TestCaseWithTransport):
1328
1244
def test_from_string(self):
1329
diff_obj = diff.DiffFromTool.from_string('diff', None, None, None)
1245
diff_obj = DiffFromTool.from_string('diff', None, None, None)
1330
1246
self.addCleanup(diff_obj.finish)
1331
self.assertEqual(['diff', '@old_path', '@new_path'],
1247
self.assertEqual(['diff', '%(old_path)s', '%(new_path)s'],
1332
1248
diff_obj.command_template)
1334
1250
def test_from_string_u5(self):
1335
diff_obj = diff.DiffFromTool.from_string('diff "-u 5"',
1251
diff_obj = DiffFromTool.from_string('diff -u\\ 5', None, None, None)
1337
1252
self.addCleanup(diff_obj.finish)
1338
self.assertEqual(['diff', '-u 5', '@old_path', '@new_path'],
1253
self.assertEqual(['diff', '-u 5', '%(old_path)s', '%(new_path)s'],
1339
1254
diff_obj.command_template)
1340
1255
self.assertEqual(['diff', '-u 5', 'old-path', 'new-path'],
1341
1256
diff_obj._get_command('old-path', 'new-path'))
1343
def test_from_string_path_with_backslashes(self):
1344
self.requireFeature(features.backslashdir_feature)
1345
tool = 'C:\\Tools\\Diff.exe'
1346
diff_obj = diff.DiffFromTool.from_string(tool, None, None, None)
1347
self.addCleanup(diff_obj.finish)
1348
self.assertEqual(['C:\\Tools\\Diff.exe', '@old_path', '@new_path'],
1349
diff_obj.command_template)
1350
self.assertEqual(['C:\\Tools\\Diff.exe', 'old-path', 'new-path'],
1351
diff_obj._get_command('old-path', 'new-path'))
1353
1258
def test_execute(self):
1354
1259
output = StringIO()
1355
diff_obj = diff.DiffFromTool(['python', '-c',
1356
'print "@old_path @new_path"'],
1260
diff_obj = DiffFromTool(['python', '-c',
1261
'print "%(old_path)s %(new_path)s"'],
1358
1263
self.addCleanup(diff_obj.finish)
1359
1264
diff_obj._execute('old', 'new')
1360
1265
self.assertEqual(output.getvalue().rstrip(), 'old new')
1362
1267
def test_excute_missing(self):
1363
diff_obj = diff.DiffFromTool(['a-tool-which-is-unlikely-to-exist'],
1268
diff_obj = DiffFromTool(['a-tool-which-is-unlikely-to-exist'],
1365
1270
self.addCleanup(diff_obj.finish)
1366
e = self.assertRaises(errors.ExecutableMissing, diff_obj._execute,
1271
e = self.assertRaises(ExecutableMissing, diff_obj._execute, 'old',
1368
1273
self.assertEqual('a-tool-which-is-unlikely-to-exist could not be found'
1369
1274
' on this machine', str(e))
1371
def test_prepare_files_creates_paths_readable_by_windows_tool(self):
1372
self.requireFeature(AttribFeature)
1374
tree = self.make_branch_and_tree('tree')
1375
self.build_tree_contents([('tree/file', 'content')])
1376
tree.add('file', 'file-id')
1377
tree.commit('old tree')
1379
self.addCleanup(tree.unlock)
1380
basis_tree = tree.basis_tree()
1381
basis_tree.lock_read()
1382
self.addCleanup(basis_tree.unlock)
1383
diff_obj = diff.DiffFromTool(['python', '-c',
1384
'print "@old_path @new_path"'],
1385
basis_tree, tree, output)
1386
diff_obj._prepare_files('file-id', 'file', 'file')
1387
# The old content should be readonly
1388
self.assertReadableByAttrib(diff_obj._root, 'old\\file',
1390
# The new content should use the tree object, not a 'new' file anymore
1391
self.assertEndsWith(tree.basedir, 'work/tree')
1392
self.assertReadableByAttrib(tree.basedir, 'file', r'work\\tree\\file$')
1394
def assertReadableByAttrib(self, cwd, relpath, regex):
1395
proc = subprocess.Popen(['attrib', relpath],
1396
stdout=subprocess.PIPE,
1398
(result, err) = proc.communicate()
1399
self.assertContainsRe(result.replace('\r\n', '\n'), regex)
1401
1276
def test_prepare_files(self):
1402
1277
output = StringIO()
1403
1278
tree = self.make_branch_and_tree('tree')
1404
1279
self.build_tree_contents([('tree/oldname', 'oldcontent')])
1405
self.build_tree_contents([('tree/oldname2', 'oldcontent2')])
1406
1280
tree.add('oldname', 'file-id')
1407
tree.add('oldname2', 'file2-id')
1408
# Earliest allowable date on FAT32 filesystems is 1980-01-01
1409
tree.commit('old tree', timestamp=315532800)
1281
tree.commit('old tree', timestamp=0)
1410
1282
tree.rename_one('oldname', 'newname')
1411
tree.rename_one('oldname2', 'newname2')
1412
1283
self.build_tree_contents([('tree/newname', 'newcontent')])
1413
self.build_tree_contents([('tree/newname2', 'newcontent2')])
1414
1284
old_tree = tree.basis_tree()
1415
1285
old_tree.lock_read()
1416
1286
self.addCleanup(old_tree.unlock)
1417
1287
tree.lock_read()
1418
1288
self.addCleanup(tree.unlock)
1419
diff_obj = diff.DiffFromTool(['python', '-c',
1420
'print "@old_path @new_path"'],
1421
old_tree, tree, output)
1289
diff_obj = DiffFromTool(['python', '-c',
1290
'print "%(old_path)s %(new_path)s"'],
1291
old_tree, tree, output)
1422
1292
self.addCleanup(diff_obj.finish)
1423
1293
self.assertContainsRe(diff_obj._root, 'bzr-diff-[^/]*')
1424
1294
old_path, new_path = diff_obj._prepare_files('file-id', 'oldname',
1426
1296
self.assertContainsRe(old_path, 'old/oldname$')
1427
self.assertEqual(315532800, os.stat(old_path).st_mtime)
1428
self.assertContainsRe(new_path, 'tree/newname$')
1297
self.assertEqual(0, os.stat(old_path).st_mtime)
1298
self.assertContainsRe(new_path, 'new/newname$')
1429
1299
self.assertFileEqual('oldcontent', old_path)
1430
1300
self.assertFileEqual('newcontent', new_path)
1431
if osutils.host_os_dereferences_symlinks():
1301
if osutils.has_symlinks():
1432
1302
self.assertTrue(os.path.samefile('tree/newname', new_path))
1433
1303
# make sure we can create files with the same parent directories
1434
diff_obj._prepare_files('file2-id', 'oldname2', 'newname2')
1437
class TestGetTreesAndBranchesToDiffLocked(tests.TestCaseWithTransport):
1439
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.
1443
return diff.get_trees_and_branches_to_diff_locked(
1444
path_list, revision_specs, old_url, new_url, self.addCleanup)
1446
def test_basic(self):
1447
tree = self.make_branch_and_tree('tree')
1448
(old_tree, new_tree,
1449
old_branch, new_branch,
1450
specific_files, extra_trees) = self.call_gtabtd(
1451
['tree'], None, None, None)
1453
self.assertIsInstance(old_tree, revisiontree.RevisionTree)
1454
self.assertEqual(_mod_revision.NULL_REVISION,
1455
old_tree.get_revision_id())
1456
self.assertEqual(tree.basedir, new_tree.basedir)
1457
self.assertEqual(tree.branch.base, old_branch.base)
1458
self.assertEqual(tree.branch.base, new_branch.base)
1459
self.assertIs(None, specific_files)
1460
self.assertIs(None, extra_trees)
1462
def test_with_rev_specs(self):
1463
tree = self.make_branch_and_tree('tree')
1464
self.build_tree_contents([('tree/file', 'oldcontent')])
1465
tree.add('file', 'file-id')
1466
tree.commit('old tree', timestamp=0, rev_id="old-id")
1467
self.build_tree_contents([('tree/file', 'newcontent')])
1468
tree.commit('new tree', timestamp=0, rev_id="new-id")
1470
revisions = [revisionspec.RevisionSpec.from_string('1'),
1471
revisionspec.RevisionSpec.from_string('2')]
1472
(old_tree, new_tree,
1473
old_branch, new_branch,
1474
specific_files, extra_trees) = self.call_gtabtd(
1475
['tree'], revisions, None, None)
1477
self.assertIsInstance(old_tree, revisiontree.RevisionTree)
1478
self.assertEqual("old-id", old_tree.get_revision_id())
1479
self.assertIsInstance(new_tree, revisiontree.RevisionTree)
1480
self.assertEqual("new-id", new_tree.get_revision_id())
1481
self.assertEqual(tree.branch.base, old_branch.base)
1482
self.assertEqual(tree.branch.base, new_branch.base)
1483
self.assertIs(None, specific_files)
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)
1304
diff_obj._prepare_files('file-id', 'oldname2', 'newname2')