1
# Copyright (C) 2005, 2006 Canonical Ltd
1
# Copyright (C) 2005-2010 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
63
63
AttribFeature = _AttribFeature()
66
class _CompiledPatienceDiffFeature(Feature):
70
import bzrlib._patiencediff_c
75
def feature_name(self):
76
return 'bzrlib._patiencediff_c'
78
CompiledPatienceDiffFeature = _CompiledPatienceDiffFeature()
66
compiled_patiencediff_feature = tests.ModuleAvailableFeature(
67
'bzrlib._patiencediff_c')
81
70
def udiff_lines(old, new, allow_binary=False):
1157
1146
class TestPatienceDiffLib_c(TestPatienceDiffLib):
1159
_test_needs_features = [CompiledPatienceDiffFeature]
1148
_test_needs_features = [compiled_patiencediff_feature]
1161
1150
def setUp(self):
1162
1151
super(TestPatienceDiffLib_c, self).setUp()
1253
1242
class TestPatienceDiffLibFiles_c(TestPatienceDiffLibFiles):
1255
_test_needs_features = [CompiledPatienceDiffFeature]
1244
_test_needs_features = [compiled_patiencediff_feature]
1257
1246
def setUp(self):
1258
1247
super(TestPatienceDiffLibFiles_c, self).setUp()
1264
1253
class TestUsingCompiledIfAvailable(TestCase):
1266
1255
def test_PatienceSequenceMatcher(self):
1267
if CompiledPatienceDiffFeature.available():
1256
if compiled_patiencediff_feature.available():
1268
1257
from bzrlib._patiencediff_c import PatienceSequenceMatcher_c
1269
1258
self.assertIs(PatienceSequenceMatcher_c,
1270
1259
bzrlib.patiencediff.PatienceSequenceMatcher)
1274
1263
bzrlib.patiencediff.PatienceSequenceMatcher)
1276
1265
def test_unique_lcs(self):
1277
if CompiledPatienceDiffFeature.available():
1266
if compiled_patiencediff_feature.available():
1278
1267
from bzrlib._patiencediff_c import unique_lcs_c
1279
1268
self.assertIs(unique_lcs_c,
1280
1269
bzrlib.patiencediff.unique_lcs)
1284
1273
bzrlib.patiencediff.unique_lcs)
1286
1275
def test_recurse_matches(self):
1287
if CompiledPatienceDiffFeature.available():
1276
if compiled_patiencediff_feature.available():
1288
1277
from bzrlib._patiencediff_c import recurse_matches_c
1289
1278
self.assertIs(recurse_matches_c,
1290
1279
bzrlib.patiencediff.recurse_matches)
1299
1288
def test_from_string(self):
1300
1289
diff_obj = DiffFromTool.from_string('diff', None, None, None)
1301
1290
self.addCleanup(diff_obj.finish)
1302
self.assertEqual(['diff', '%(old_path)s', '%(new_path)s'],
1291
self.assertEqual(['diff', '@old_path', '@new_path'],
1303
1292
diff_obj.command_template)
1305
1294
def test_from_string_u5(self):
1306
1295
diff_obj = DiffFromTool.from_string('diff -u\\ 5', None, None, None)
1307
1296
self.addCleanup(diff_obj.finish)
1308
self.assertEqual(['diff', '-u 5', '%(old_path)s', '%(new_path)s'],
1297
self.assertEqual(['diff', '-u 5', '@old_path', '@new_path'],
1309
1298
diff_obj.command_template)
1310
1299
self.assertEqual(['diff', '-u 5', 'old-path', 'new-path'],
1311
1300
diff_obj._get_command('old-path', 'new-path'))
1313
1302
def test_execute(self):
1314
1303
output = StringIO()
1315
1304
diff_obj = DiffFromTool(['python', '-c',
1316
'print "%(old_path)s %(new_path)s"'],
1305
'print "@old_path @new_path"'],
1317
1306
None, None, output)
1318
1307
self.addCleanup(diff_obj.finish)
1319
1308
diff_obj._execute('old', 'new')
1337
1326
tree.commit('old tree')
1338
1327
tree.lock_read()
1339
1328
self.addCleanup(tree.unlock)
1329
basis_tree = tree.basis_tree()
1330
basis_tree.lock_read()
1331
self.addCleanup(basis_tree.unlock)
1340
1332
diff_obj = DiffFromTool(['python', '-c',
1341
'print "%(old_path)s %(new_path)s"'],
1333
'print "@old_path @new_path"'],
1334
basis_tree, tree, output)
1343
1335
diff_obj._prepare_files('file-id', 'file', 'file')
1344
self.assertReadableByAttrib(diff_obj._root, 'old\\file', r'old\\file')
1345
self.assertReadableByAttrib(diff_obj._root, 'new\\file', r'new\\file')
1336
# The old content should be readonly
1337
self.assertReadableByAttrib(diff_obj._root, 'old\\file',
1339
# The new content should use the tree object, not a 'new' file anymore
1340
self.assertEndsWith(tree.basedir, 'work/tree')
1341
self.assertReadableByAttrib(tree.basedir, 'file', r'work\\tree\\file$')
1347
1343
def assertReadableByAttrib(self, cwd, relpath, regex):
1348
1344
proc = subprocess.Popen(['attrib', relpath],
1349
1345
stdout=subprocess.PIPE,
1352
result = proc.stdout.read()
1353
self.assertContainsRe(result, regex)
1347
(result, err) = proc.communicate()
1348
self.assertContainsRe(result.replace('\r\n', '\n'), regex)
1355
1350
def test_prepare_files(self):
1356
1351
output = StringIO()
1370
1365
tree.lock_read()
1371
1366
self.addCleanup(tree.unlock)
1372
1367
diff_obj = DiffFromTool(['python', '-c',
1373
'print "%(old_path)s %(new_path)s"'],
1368
'print "@old_path @new_path"'],
1374
1369
old_tree, tree, output)
1375
1370
self.addCleanup(diff_obj.finish)
1376
1371
self.assertContainsRe(diff_obj._root, 'bzr-diff-[^/]*')
1379
1374
self.assertContainsRe(old_path, 'old/oldname$')
1380
1375
self.assertEqual(0, os.stat(old_path).st_mtime)
1381
self.assertContainsRe(new_path, 'new/newname$')
1376
self.assertContainsRe(new_path, 'tree/newname$')
1382
1377
self.assertFileEqual('oldcontent', old_path)
1383
1378
self.assertFileEqual('newcontent', new_path)
1384
1379
if osutils.host_os_dereferences_symlinks():