151
169
self.check_patch(lines)
153
171
def test_external_diff_binary_lang_c(self):
154
173
for lang in ('LANG', 'LC_ALL', 'LANGUAGE'):
155
self.overrideEnv(lang, 'C')
156
lines = external_udiff_lines(['\x00foobar\n'], ['foo\x00bar\n'])
157
# Older versions of diffutils say "Binary files", newer
158
# versions just say "Files".
159
self.assertContainsRe(lines[0], '(Binary f|F)iles old and new differ\n')
160
self.assertEqual(lines[1:], ['\n'])
174
old_env[lang] = osutils.set_or_unset_env(lang, 'C')
176
lines = external_udiff_lines(['\x00foobar\n'], ['foo\x00bar\n'])
177
# Older versions of diffutils say "Binary files", newer
178
# versions just say "Files".
179
self.assertContainsRe(lines[0],
180
'(Binary f|F)iles old and new differ\n')
181
self.assertEquals(lines[1:], ['\n'])
183
for lang, old_val in old_env.iteritems():
184
osutils.set_or_unset_env(lang, old_val)
162
186
def test_no_external_diff(self):
163
187
"""Check that NoDiff is raised when diff is not available"""
164
# Make sure no 'diff' command is available
165
# XXX: Weird, using None instead of '' breaks the test -- vila 20101216
166
self.overrideEnv('PATH', '')
167
self.assertRaises(errors.NoDiff, diff.external_diff,
168
'old', ['boo\n'], 'new', ['goo\n'],
169
StringIO(), diff_opts=['-u'])
188
# Use os.environ['PATH'] to make sure no 'diff' command is available
189
orig_path = os.environ['PATH']
191
os.environ['PATH'] = ''
192
self.assertRaises(NoDiff, external_diff,
193
'old', ['boo\n'], 'new', ['goo\n'],
194
StringIO(), diff_opts=['-u'])
196
os.environ['PATH'] = orig_path
171
198
def test_internal_diff_default(self):
172
199
# Default internal diff encoding is utf8
173
200
output = StringIO()
174
diff.internal_diff(u'old_\xb5', ['old_text\n'],
175
u'new_\xe5', ['new_text\n'], output)
201
internal_diff(u'old_\xb5', ['old_text\n'],
202
u'new_\xe5', ['new_text\n'], output)
176
203
lines = output.getvalue().splitlines(True)
177
204
self.check_patch(lines)
178
self.assertEqual(['--- old_\xc2\xb5\n',
205
self.assertEquals(['--- old_\xc2\xb5\n',
179
206
'+++ new_\xc3\xa5\n',
180
207
'@@ -1,1 +1,1 @@\n',
219
246
def test_internal_diff_no_content(self):
220
247
output = StringIO()
221
diff.internal_diff(u'old', [], u'new', [], output)
248
internal_diff(u'old', [], u'new', [], output)
222
249
self.assertEqual('', output.getvalue())
224
251
def test_internal_diff_no_changes(self):
225
252
output = StringIO()
226
diff.internal_diff(u'old', ['text\n', 'contents\n'],
227
u'new', ['text\n', 'contents\n'],
253
internal_diff(u'old', ['text\n', 'contents\n'],
254
u'new', ['text\n', 'contents\n'],
229
256
self.assertEqual('', output.getvalue())
231
258
def test_internal_diff_returns_bytes(self):
233
260
output = StringIO.StringIO()
234
diff.internal_diff(u'old_\xb5', ['old_text\n'],
235
u'new_\xe5', ['new_text\n'], output)
236
self.assertIsInstance(output.getvalue(), str,
261
internal_diff(u'old_\xb5', ['old_text\n'],
262
u'new_\xe5', ['new_text\n'], output)
263
self.failUnless(isinstance(output.getvalue(), str),
237
264
'internal_diff should return bytestrings')
239
def test_internal_diff_default_context(self):
241
diff.internal_diff('old', ['same_text\n','same_text\n','same_text\n',
242
'same_text\n','same_text\n','old_text\n'],
243
'new', ['same_text\n','same_text\n','same_text\n',
244
'same_text\n','same_text\n','new_text\n'], output)
245
lines = output.getvalue().splitlines(True)
246
self.check_patch(lines)
247
self.assertEqual(['--- old\n',
259
def test_internal_diff_no_context(self):
261
diff.internal_diff('old', ['same_text\n','same_text\n','same_text\n',
262
'same_text\n','same_text\n','old_text\n'],
263
'new', ['same_text\n','same_text\n','same_text\n',
264
'same_text\n','same_text\n','new_text\n'], output,
266
lines = output.getvalue().splitlines(True)
267
self.check_patch(lines)
268
self.assertEqual(['--- old\n',
277
def test_internal_diff_more_context(self):
279
diff.internal_diff('old', ['same_text\n','same_text\n','same_text\n',
280
'same_text\n','same_text\n','old_text\n'],
281
'new', ['same_text\n','same_text\n','same_text\n',
282
'same_text\n','same_text\n','new_text\n'], output,
284
lines = output.getvalue().splitlines(True)
285
self.check_patch(lines)
286
self.assertEqual(['--- old\n',
303
class TestDiffFiles(tests.TestCaseInTempDir):
267
class TestDiffFiles(TestCaseInTempDir):
305
269
def test_external_diff_binary(self):
306
270
"""The output when using external diff should use diff's i18n error"""
850
771
'.*a-file(.|\n)*b-file')
853
class TestPatienceDiffLib(tests.TestCase):
774
class TestPatienceDiffLib(TestCase):
856
777
super(TestPatienceDiffLib, self).setUp()
857
self._unique_lcs = _patiencediff_py.unique_lcs_py
858
self._recurse_matches = _patiencediff_py.recurse_matches_py
778
self._unique_lcs = bzrlib._patiencediff_py.unique_lcs_py
779
self._recurse_matches = bzrlib._patiencediff_py.recurse_matches_py
859
780
self._PatienceSequenceMatcher = \
860
_patiencediff_py.PatienceSequenceMatcher_py
862
def test_diff_unicode_string(self):
863
a = ''.join([unichr(i) for i in range(4000, 4500, 3)])
864
b = ''.join([unichr(i) for i in range(4300, 4800, 2)])
865
sm = self._PatienceSequenceMatcher(None, a, b)
866
mb = sm.get_matching_blocks()
867
self.assertEqual(35, len(mb))
781
bzrlib._patiencediff_py.PatienceSequenceMatcher_py
869
783
def test_unique_lcs(self):
870
784
unique_lcs = self._unique_lcs
871
self.assertEqual(unique_lcs('', ''), [])
872
self.assertEqual(unique_lcs('', 'a'), [])
873
self.assertEqual(unique_lcs('a', ''), [])
874
self.assertEqual(unique_lcs('a', 'a'), [(0,0)])
875
self.assertEqual(unique_lcs('a', 'b'), [])
876
self.assertEqual(unique_lcs('ab', 'ab'), [(0,0), (1,1)])
877
self.assertEqual(unique_lcs('abcde', 'cdeab'), [(2,0), (3,1), (4,2)])
878
self.assertEqual(unique_lcs('cdeab', 'abcde'), [(0,2), (1,3), (2,4)])
879
self.assertEqual(unique_lcs('abXde', 'abYde'), [(0,0), (1,1),
785
self.assertEquals(unique_lcs('', ''), [])
786
self.assertEquals(unique_lcs('', 'a'), [])
787
self.assertEquals(unique_lcs('a', ''), [])
788
self.assertEquals(unique_lcs('a', 'a'), [(0,0)])
789
self.assertEquals(unique_lcs('a', 'b'), [])
790
self.assertEquals(unique_lcs('ab', 'ab'), [(0,0), (1,1)])
791
self.assertEquals(unique_lcs('abcde', 'cdeab'), [(2,0), (3,1), (4,2)])
792
self.assertEquals(unique_lcs('cdeab', 'abcde'), [(0,2), (1,3), (2,4)])
793
self.assertEquals(unique_lcs('abXde', 'abYde'), [(0,0), (1,1),
881
self.assertEqual(unique_lcs('acbac', 'abc'), [(2,1)])
795
self.assertEquals(unique_lcs('acbac', 'abc'), [(2,1)])
883
797
def test_recurse_matches(self):
884
798
def test_one(a, b, matches):
885
799
test_matches = []
886
800
self._recurse_matches(
887
801
a, b, 0, 0, len(a), len(b), test_matches, 10)
888
self.assertEqual(test_matches, matches)
802
self.assertEquals(test_matches, matches)
890
804
test_one(['a', '', 'b', '', 'c'], ['a', 'a', 'b', 'c', 'c'],
891
805
[(0, 0), (2, 2), (4, 4)])
1317
1210
, list(unified_diff_files('a2', 'b2')))
1319
1212
# And the patience diff
1320
self.assertEqual(['--- a2\n',
1322
'@@ -4,6 +4,11 @@\n',
1334
list(unified_diff_files('a2', 'b2',
1335
sequencematcher=psm)))
1213
self.assertEquals(['--- a2 \n',
1215
'@@ -4,6 +4,11 @@\n',
1228
, list(unified_diff_files('a2', 'b2',
1229
sequencematcher=psm)))
1338
1232
class TestPatienceDiffLibFiles_c(TestPatienceDiffLibFiles):
1340
_test_needs_features = [features.compiled_patiencediff_feature]
1234
_test_needs_features = [CompiledPatienceDiffFeature]
1342
1236
def setUp(self):
1343
1237
super(TestPatienceDiffLibFiles_c, self).setUp()
1344
from bzrlib import _patiencediff_c
1238
import bzrlib._patiencediff_c
1345
1239
self._PatienceSequenceMatcher = \
1346
_patiencediff_c.PatienceSequenceMatcher_c
1349
class TestUsingCompiledIfAvailable(tests.TestCase):
1240
bzrlib._patiencediff_c.PatienceSequenceMatcher_c
1243
class TestUsingCompiledIfAvailable(TestCase):
1351
1245
def test_PatienceSequenceMatcher(self):
1352
if features.compiled_patiencediff_feature.available():
1246
if CompiledPatienceDiffFeature.available():
1353
1247
from bzrlib._patiencediff_c import PatienceSequenceMatcher_c
1354
1248
self.assertIs(PatienceSequenceMatcher_c,
1355
patiencediff.PatienceSequenceMatcher)
1249
bzrlib.patiencediff.PatienceSequenceMatcher)
1357
1251
from bzrlib._patiencediff_py import PatienceSequenceMatcher_py
1358
1252
self.assertIs(PatienceSequenceMatcher_py,
1359
patiencediff.PatienceSequenceMatcher)
1253
bzrlib.patiencediff.PatienceSequenceMatcher)
1361
1255
def test_unique_lcs(self):
1362
if features.compiled_patiencediff_feature.available():
1256
if CompiledPatienceDiffFeature.available():
1363
1257
from bzrlib._patiencediff_c import unique_lcs_c
1364
1258
self.assertIs(unique_lcs_c,
1365
patiencediff.unique_lcs)
1259
bzrlib.patiencediff.unique_lcs)
1367
1261
from bzrlib._patiencediff_py import unique_lcs_py
1368
1262
self.assertIs(unique_lcs_py,
1369
patiencediff.unique_lcs)
1263
bzrlib.patiencediff.unique_lcs)
1371
1265
def test_recurse_matches(self):
1372
if features.compiled_patiencediff_feature.available():
1266
if CompiledPatienceDiffFeature.available():
1373
1267
from bzrlib._patiencediff_c import recurse_matches_c
1374
1268
self.assertIs(recurse_matches_c,
1375
patiencediff.recurse_matches)
1269
bzrlib.patiencediff.recurse_matches)
1377
1271
from bzrlib._patiencediff_py import recurse_matches_py
1378
1272
self.assertIs(recurse_matches_py,
1379
patiencediff.recurse_matches)
1382
class TestDiffFromTool(tests.TestCaseWithTransport):
1273
bzrlib.patiencediff.recurse_matches)
1276
class TestDiffFromTool(TestCaseWithTransport):
1384
1278
def test_from_string(self):
1385
diff_obj = diff.DiffFromTool.from_string('diff', None, None, None)
1279
diff_obj = DiffFromTool.from_string('diff', None, None, None)
1386
1280
self.addCleanup(diff_obj.finish)
1387
self.assertEqual(['diff', '@old_path', '@new_path'],
1281
self.assertEqual(['diff', '%(old_path)s', '%(new_path)s'],
1388
1282
diff_obj.command_template)
1390
1284
def test_from_string_u5(self):
1391
diff_obj = diff.DiffFromTool.from_string('diff "-u 5"',
1285
diff_obj = DiffFromTool.from_string('diff -u\\ 5', None, None, None)
1393
1286
self.addCleanup(diff_obj.finish)
1394
self.assertEqual(['diff', '-u 5', '@old_path', '@new_path'],
1287
self.assertEqual(['diff', '-u 5', '%(old_path)s', '%(new_path)s'],
1395
1288
diff_obj.command_template)
1396
1289
self.assertEqual(['diff', '-u 5', 'old-path', 'new-path'],
1397
1290
diff_obj._get_command('old-path', 'new-path'))
1399
def test_from_string_path_with_backslashes(self):
1400
self.requireFeature(features.backslashdir_feature)
1401
tool = 'C:\\Tools\\Diff.exe'
1402
diff_obj = diff.DiffFromTool.from_string(tool, None, None, None)
1403
self.addCleanup(diff_obj.finish)
1404
self.assertEqual(['C:\\Tools\\Diff.exe', '@old_path', '@new_path'],
1405
diff_obj.command_template)
1406
self.assertEqual(['C:\\Tools\\Diff.exe', 'old-path', 'new-path'],
1407
diff_obj._get_command('old-path', 'new-path'))
1409
1292
def test_execute(self):
1410
1293
output = StringIO()
1411
diff_obj = diff.DiffFromTool(['python', '-c',
1412
'print "@old_path @new_path"'],
1294
diff_obj = DiffFromTool(['python', '-c',
1295
'print "%(old_path)s %(new_path)s"'],
1414
1297
self.addCleanup(diff_obj.finish)
1415
1298
diff_obj._execute('old', 'new')
1416
1299
self.assertEqual(output.getvalue().rstrip(), 'old new')
1418
def test_execute_missing(self):
1419
diff_obj = diff.DiffFromTool(['a-tool-which-is-unlikely-to-exist'],
1301
def test_excute_missing(self):
1302
diff_obj = DiffFromTool(['a-tool-which-is-unlikely-to-exist'],
1421
1304
self.addCleanup(diff_obj.finish)
1422
e = self.assertRaises(errors.ExecutableMissing, diff_obj._execute,
1305
e = self.assertRaises(ExecutableMissing, diff_obj._execute, 'old',
1424
1307
self.assertEqual('a-tool-which-is-unlikely-to-exist could not be found'
1425
1308
' on this machine', str(e))
1427
def test_prepare_files_creates_paths_readable_by_windows_tool(self):
1428
self.requireFeature(features.AttribFeature)
1430
tree = self.make_branch_and_tree('tree')
1431
self.build_tree_contents([('tree/file', 'content')])
1432
tree.add('file', 'file-id')
1433
tree.commit('old tree')
1435
self.addCleanup(tree.unlock)
1436
basis_tree = tree.basis_tree()
1437
basis_tree.lock_read()
1438
self.addCleanup(basis_tree.unlock)
1439
diff_obj = diff.DiffFromTool(['python', '-c',
1440
'print "@old_path @new_path"'],
1441
basis_tree, tree, output)
1442
diff_obj._prepare_files('file-id', 'file', 'file')
1443
# The old content should be readonly
1444
self.assertReadableByAttrib(diff_obj._root, 'old\\file',
1446
# The new content should use the tree object, not a 'new' file anymore
1447
self.assertEndsWith(tree.basedir, 'work/tree')
1448
self.assertReadableByAttrib(tree.basedir, 'file', r'work\\tree\\file$')
1450
def assertReadableByAttrib(self, cwd, relpath, regex):
1451
proc = subprocess.Popen(['attrib', relpath],
1452
stdout=subprocess.PIPE,
1454
(result, err) = proc.communicate()
1455
self.assertContainsRe(result.replace('\r\n', '\n'), regex)
1457
1310
def test_prepare_files(self):
1458
1311
output = StringIO()
1459
1312
tree = self.make_branch_and_tree('tree')
1460
1313
self.build_tree_contents([('tree/oldname', 'oldcontent')])
1461
self.build_tree_contents([('tree/oldname2', 'oldcontent2')])
1462
1314
tree.add('oldname', 'file-id')
1463
tree.add('oldname2', 'file2-id')
1464
# Earliest allowable date on FAT32 filesystems is 1980-01-01
1465
tree.commit('old tree', timestamp=315532800)
1315
tree.commit('old tree', timestamp=0)
1466
1316
tree.rename_one('oldname', 'newname')
1467
tree.rename_one('oldname2', 'newname2')
1468
1317
self.build_tree_contents([('tree/newname', 'newcontent')])
1469
self.build_tree_contents([('tree/newname2', 'newcontent2')])
1470
1318
old_tree = tree.basis_tree()
1471
1319
old_tree.lock_read()
1472
1320
self.addCleanup(old_tree.unlock)
1473
1321
tree.lock_read()
1474
1322
self.addCleanup(tree.unlock)
1475
diff_obj = diff.DiffFromTool(['python', '-c',
1476
'print "@old_path @new_path"'],
1477
old_tree, tree, output)
1323
diff_obj = DiffFromTool(['python', '-c',
1324
'print "%(old_path)s %(new_path)s"'],
1325
old_tree, tree, output)
1478
1326
self.addCleanup(diff_obj.finish)
1479
1327
self.assertContainsRe(diff_obj._root, 'bzr-diff-[^/]*')
1480
1328
old_path, new_path = diff_obj._prepare_files('file-id', 'oldname',
1482
1330
self.assertContainsRe(old_path, 'old/oldname$')
1483
self.assertEqual(315532800, os.stat(old_path).st_mtime)
1484
self.assertContainsRe(new_path, 'tree/newname$')
1331
self.assertEqual(0, os.stat(old_path).st_mtime)
1332
self.assertContainsRe(new_path, 'new/newname$')
1485
1333
self.assertFileEqual('oldcontent', old_path)
1486
1334
self.assertFileEqual('newcontent', new_path)
1487
if osutils.host_os_dereferences_symlinks():
1335
if osutils.has_symlinks():
1488
1336
self.assertTrue(os.path.samefile('tree/newname', new_path))
1489
1337
# make sure we can create files with the same parent directories
1490
diff_obj._prepare_files('file2-id', 'oldname2', 'newname2')
1493
class TestDiffFromToolEncodedFilename(tests.TestCaseWithTransport):
1495
def test_encodable_filename(self):
1496
# Just checks file path for external diff tool.
1497
# We cannot change CPython's internal encoding used by os.exec*.
1498
diffobj = diff.DiffFromTool(['dummy', '@old_path', '@new_path'],
1500
for _, scenario in EncodingAdapter.encoding_scenarios:
1501
encoding = scenario['encoding']
1502
dirname = scenario['info']['directory']
1503
filename = scenario['info']['filename']
1505
self.overrideAttr(diffobj, '_fenc', lambda: encoding)
1506
relpath = dirname + u'/' + filename
1507
fullpath = diffobj._safe_filename('safe', relpath)
1508
self.assertEqual(fullpath,
1509
fullpath.encode(encoding).decode(encoding))
1510
self.assertTrue(fullpath.startswith(diffobj._root + '/safe'))
1512
def test_unencodable_filename(self):
1513
diffobj = diff.DiffFromTool(['dummy', '@old_path', '@new_path'],
1515
for _, scenario in EncodingAdapter.encoding_scenarios:
1516
encoding = scenario['encoding']
1517
dirname = scenario['info']['directory']
1518
filename = scenario['info']['filename']
1520
if encoding == 'iso-8859-1':
1521
encoding = 'iso-8859-2'
1523
encoding = 'iso-8859-1'
1525
self.overrideAttr(diffobj, '_fenc', lambda: encoding)
1526
relpath = dirname + u'/' + filename
1527
fullpath = diffobj._safe_filename('safe', relpath)
1528
self.assertEqual(fullpath,
1529
fullpath.encode(encoding).decode(encoding))
1530
self.assertTrue(fullpath.startswith(diffobj._root + '/safe'))
1533
class TestGetTreesAndBranchesToDiffLocked(tests.TestCaseWithTransport):
1535
def call_gtabtd(self, path_list, revision_specs, old_url, new_url):
1536
"""Call get_trees_and_branches_to_diff_locked."""
1537
return diff.get_trees_and_branches_to_diff_locked(
1538
path_list, revision_specs, old_url, new_url, self.addCleanup)
1540
def test_basic(self):
1541
tree = self.make_branch_and_tree('tree')
1542
(old_tree, new_tree,
1543
old_branch, new_branch,
1544
specific_files, extra_trees) = self.call_gtabtd(
1545
['tree'], None, None, None)
1547
self.assertIsInstance(old_tree, revisiontree.RevisionTree)
1548
self.assertEqual(_mod_revision.NULL_REVISION,
1549
old_tree.get_revision_id())
1550
self.assertEqual(tree.basedir, new_tree.basedir)
1551
self.assertEqual(tree.branch.base, old_branch.base)
1552
self.assertEqual(tree.branch.base, new_branch.base)
1553
self.assertIs(None, specific_files)
1554
self.assertIs(None, extra_trees)
1556
def test_with_rev_specs(self):
1557
tree = self.make_branch_and_tree('tree')
1558
self.build_tree_contents([('tree/file', 'oldcontent')])
1559
tree.add('file', 'file-id')
1560
tree.commit('old tree', timestamp=0, rev_id="old-id")
1561
self.build_tree_contents([('tree/file', 'newcontent')])
1562
tree.commit('new tree', timestamp=0, rev_id="new-id")
1564
revisions = [revisionspec.RevisionSpec.from_string('1'),
1565
revisionspec.RevisionSpec.from_string('2')]
1566
(old_tree, new_tree,
1567
old_branch, new_branch,
1568
specific_files, extra_trees) = self.call_gtabtd(
1569
['tree'], revisions, None, None)
1571
self.assertIsInstance(old_tree, revisiontree.RevisionTree)
1572
self.assertEqual("old-id", old_tree.get_revision_id())
1573
self.assertIsInstance(new_tree, revisiontree.RevisionTree)
1574
self.assertEqual("new-id", new_tree.get_revision_id())
1575
self.assertEqual(tree.branch.base, old_branch.base)
1576
self.assertEqual(tree.branch.base, new_branch.base)
1577
self.assertIs(None, specific_files)
1578
self.assertEqual(tree.basedir, extra_trees[0].basedir)
1338
diff_obj._prepare_files('file-id', 'oldname2', 'newname2')