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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
18
from cStringIO import StringIO
22
from tempfile import TemporaryFile
24
from bzrlib import tests
25
from bzrlib.diff import (
29
revision as _mod_revision,
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):
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):
43
if (sys.platform not in ('cygwin', 'win32')):
47
import bzrlib._patiencediff_c
46
proc = subprocess.Popen(['attrib', '.'], stdout=subprocess.PIPE)
49
return (0 == proc.wait())
52
51
def feature_name(self):
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()
52
return 'attrib Windows command-line tool'
54
AttribFeature = _AttribFeature()
57
compiled_patiencediff_feature = tests.ModuleAvailableFeature(
58
'bzrlib._patiencediff_c')
87
61
def udiff_lines(old, new, allow_binary=False):
88
62
output = StringIO()
89
internal_diff('old', old, 'new', new, output, allow_binary)
63
diff.internal_diff('old', old, 'new', new, output, allow_binary)
91
65
return output.readlines()
503
480
tree.rename_one('file', 'newname')
504
481
self.build_tree_contents([('tree/newname', 'new contents\n')])
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'
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'")
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.
516
529
# See https://bugs.launchpad.net/bugs/110092.
517
self.requireFeature(UnicodeFilename)
530
self.requireFeature(tests.UnicodeFilenameFeature)
519
532
# This bug isn't triggered with cStringIO.
520
533
from StringIO import StringIO
1198
1282
class TestPatienceDiffLibFiles_c(TestPatienceDiffLibFiles):
1200
_test_needs_features = [CompiledPatienceDiffFeature]
1284
_test_needs_features = [compiled_patiencediff_feature]
1202
1286
def setUp(self):
1203
1287
super(TestPatienceDiffLibFiles_c, self).setUp()
1204
import bzrlib._patiencediff_c
1288
from bzrlib import _patiencediff_c
1205
1289
self._PatienceSequenceMatcher = \
1206
bzrlib._patiencediff_c.PatienceSequenceMatcher_c
1209
class TestUsingCompiledIfAvailable(TestCase):
1290
_patiencediff_c.PatienceSequenceMatcher_c
1293
class TestUsingCompiledIfAvailable(tests.TestCase):
1211
1295
def test_PatienceSequenceMatcher(self):
1212
if CompiledPatienceDiffFeature.available():
1296
if compiled_patiencediff_feature.available():
1213
1297
from bzrlib._patiencediff_c import PatienceSequenceMatcher_c
1214
1298
self.assertIs(PatienceSequenceMatcher_c,
1215
bzrlib.patiencediff.PatienceSequenceMatcher)
1299
patiencediff.PatienceSequenceMatcher)
1217
1301
from bzrlib._patiencediff_py import PatienceSequenceMatcher_py
1218
1302
self.assertIs(PatienceSequenceMatcher_py,
1219
bzrlib.patiencediff.PatienceSequenceMatcher)
1303
patiencediff.PatienceSequenceMatcher)
1221
1305
def test_unique_lcs(self):
1222
if CompiledPatienceDiffFeature.available():
1306
if compiled_patiencediff_feature.available():
1223
1307
from bzrlib._patiencediff_c import unique_lcs_c
1224
1308
self.assertIs(unique_lcs_c,
1225
bzrlib.patiencediff.unique_lcs)
1309
patiencediff.unique_lcs)
1227
1311
from bzrlib._patiencediff_py import unique_lcs_py
1228
1312
self.assertIs(unique_lcs_py,
1229
bzrlib.patiencediff.unique_lcs)
1313
patiencediff.unique_lcs)
1231
1315
def test_recurse_matches(self):
1232
if CompiledPatienceDiffFeature.available():
1316
if compiled_patiencediff_feature.available():
1233
1317
from bzrlib._patiencediff_c import recurse_matches_c
1234
1318
self.assertIs(recurse_matches_c,
1235
bzrlib.patiencediff.recurse_matches)
1319
patiencediff.recurse_matches)
1237
1321
from bzrlib._patiencediff_py import recurse_matches_py
1238
1322
self.assertIs(recurse_matches_py,
1239
bzrlib.patiencediff.recurse_matches)
1242
class TestDiffFromTool(TestCaseWithTransport):
1323
patiencediff.recurse_matches)
1326
class TestDiffFromTool(tests.TestCaseWithTransport):
1244
1328
def test_from_string(self):
1245
diff_obj = DiffFromTool.from_string('diff', None, None, None)
1329
diff_obj = diff.DiffFromTool.from_string('diff', None, None, None)
1246
1330
self.addCleanup(diff_obj.finish)
1247
self.assertEqual(['diff', '%(old_path)s', '%(new_path)s'],
1331
self.assertEqual(['diff', '@old_path', '@new_path'],
1248
1332
diff_obj.command_template)
1250
1334
def test_from_string_u5(self):
1251
diff_obj = DiffFromTool.from_string('diff -u\\ 5', None, None, None)
1335
diff_obj = diff.DiffFromTool.from_string('diff "-u 5"',
1252
1337
self.addCleanup(diff_obj.finish)
1253
self.assertEqual(['diff', '-u 5', '%(old_path)s', '%(new_path)s'],
1338
self.assertEqual(['diff', '-u 5', '@old_path', '@new_path'],
1254
1339
diff_obj.command_template)
1255
1340
self.assertEqual(['diff', '-u 5', 'old-path', 'new-path'],
1256
1341
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'))
1258
1353
def test_execute(self):
1259
1354
output = StringIO()
1260
diff_obj = DiffFromTool(['python', '-c',
1261
'print "%(old_path)s %(new_path)s"'],
1355
diff_obj = diff.DiffFromTool(['python', '-c',
1356
'print "@old_path @new_path"'],
1263
1358
self.addCleanup(diff_obj.finish)
1264
1359
diff_obj._execute('old', 'new')
1265
1360
self.assertEqual(output.getvalue().rstrip(), 'old new')
1267
1362
def test_excute_missing(self):
1268
diff_obj = DiffFromTool(['a-tool-which-is-unlikely-to-exist'],
1363
diff_obj = diff.DiffFromTool(['a-tool-which-is-unlikely-to-exist'],
1270
1365
self.addCleanup(diff_obj.finish)
1271
e = self.assertRaises(ExecutableMissing, diff_obj._execute, 'old',
1366
e = self.assertRaises(errors.ExecutableMissing, diff_obj._execute,
1273
1368
self.assertEqual('a-tool-which-is-unlikely-to-exist could not be found'
1274
1369
' 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)
1276
1401
def test_prepare_files(self):
1277
1402
output = StringIO()
1278
1403
tree = self.make_branch_and_tree('tree')
1279
1404
self.build_tree_contents([('tree/oldname', 'oldcontent')])
1405
self.build_tree_contents([('tree/oldname2', 'oldcontent2')])
1280
1406
tree.add('oldname', 'file-id')
1281
tree.commit('old tree', timestamp=0)
1407
tree.add('oldname2', 'file2-id')
1408
# Earliest allowable date on FAT32 filesystems is 1980-01-01
1409
tree.commit('old tree', timestamp=315532800)
1282
1410
tree.rename_one('oldname', 'newname')
1411
tree.rename_one('oldname2', 'newname2')
1283
1412
self.build_tree_contents([('tree/newname', 'newcontent')])
1413
self.build_tree_contents([('tree/newname2', 'newcontent2')])
1284
1414
old_tree = tree.basis_tree()
1285
1415
old_tree.lock_read()
1286
1416
self.addCleanup(old_tree.unlock)
1287
1417
tree.lock_read()
1288
1418
self.addCleanup(tree.unlock)
1289
diff_obj = DiffFromTool(['python', '-c',
1290
'print "%(old_path)s %(new_path)s"'],
1291
old_tree, tree, output)
1419
diff_obj = diff.DiffFromTool(['python', '-c',
1420
'print "@old_path @new_path"'],
1421
old_tree, tree, output)
1292
1422
self.addCleanup(diff_obj.finish)
1293
1423
self.assertContainsRe(diff_obj._root, 'bzr-diff-[^/]*')
1294
1424
old_path, new_path = diff_obj._prepare_files('file-id', 'oldname',
1296
1426
self.assertContainsRe(old_path, 'old/oldname$')
1297
self.assertEqual(0, os.stat(old_path).st_mtime)
1298
self.assertContainsRe(new_path, 'new/newname$')
1427
self.assertEqual(315532800, os.stat(old_path).st_mtime)
1428
self.assertContainsRe(new_path, 'tree/newname$')
1299
1429
self.assertFileEqual('oldcontent', old_path)
1300
1430
self.assertFileEqual('newcontent', new_path)
1301
if osutils.has_symlinks():
1431
if osutils.host_os_dereferences_symlinks():
1302
1432
self.assertTrue(os.path.samefile('tree/newname', new_path))
1303
1433
# make sure we can create files with the same parent directories
1304
diff_obj._prepare_files('file-id', 'oldname2', 'newname2')
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)