~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_diff.py

  • Committer: Joe Julian
  • Date: 2010-01-10 02:25:31 UTC
  • mto: (4634.119.7 2.0)
  • mto: This revision was merged to the branch mainline in revision 4959.
  • Revision ID: joe@julianfamily.org-20100110022531-wqk61rsagz8xsiga
Added MANIFEST.in to allow bdist_rpm to have all the required include files and tools. bdist_rpm will still fail to build correctly on some distributions due to a disttools bug http://bugs.python.org/issue644744

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005, 2006 Canonical Ltd
2
2
#
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
32
32
    external_diff,
33
33
    internal_diff,
34
34
    show_diff_trees,
35
 
    get_trees_and_branches_to_diff,
36
35
    )
37
36
from bzrlib.errors import BinaryFile, NoDiff, ExecutableMissing
38
37
import bzrlib.osutils as osutils
42
41
import bzrlib._patiencediff_py
43
42
from bzrlib.tests import (Feature, TestCase, TestCaseWithTransport,
44
43
                          TestCaseInTempDir, TestSkipped)
45
 
from bzrlib.revisiontree import RevisionTree
46
 
from bzrlib.revisionspec import RevisionSpec
47
 
 
48
 
from bzrlib.tests.test_win32utils import BackslashDirSeparatorFeature
49
44
 
50
45
 
51
46
class _AttribFeature(Feature):
65
60
AttribFeature = _AttribFeature()
66
61
 
67
62
 
68
 
compiled_patiencediff_feature = tests.ModuleAvailableFeature(
69
 
                                    'bzrlib._patiencediff_c')
 
63
class _CompiledPatienceDiffFeature(Feature):
 
64
 
 
65
    def _probe(self):
 
66
        try:
 
67
            import bzrlib._patiencediff_c
 
68
        except ImportError:
 
69
            return False
 
70
        return True
 
71
 
 
72
    def feature_name(self):
 
73
        return 'bzrlib._patiencediff_c'
 
74
 
 
75
CompiledPatienceDiffFeature = _CompiledPatienceDiffFeature()
70
76
 
71
77
 
72
78
def udiff_lines(old, new, allow_binary=False):
1147
1153
 
1148
1154
class TestPatienceDiffLib_c(TestPatienceDiffLib):
1149
1155
 
1150
 
    _test_needs_features = [compiled_patiencediff_feature]
 
1156
    _test_needs_features = [CompiledPatienceDiffFeature]
1151
1157
 
1152
1158
    def setUp(self):
1153
1159
        super(TestPatienceDiffLib_c, self).setUp()
1243
1249
 
1244
1250
class TestPatienceDiffLibFiles_c(TestPatienceDiffLibFiles):
1245
1251
 
1246
 
    _test_needs_features = [compiled_patiencediff_feature]
 
1252
    _test_needs_features = [CompiledPatienceDiffFeature]
1247
1253
 
1248
1254
    def setUp(self):
1249
1255
        super(TestPatienceDiffLibFiles_c, self).setUp()
1255
1261
class TestUsingCompiledIfAvailable(TestCase):
1256
1262
 
1257
1263
    def test_PatienceSequenceMatcher(self):
1258
 
        if compiled_patiencediff_feature.available():
 
1264
        if CompiledPatienceDiffFeature.available():
1259
1265
            from bzrlib._patiencediff_c import PatienceSequenceMatcher_c
1260
1266
            self.assertIs(PatienceSequenceMatcher_c,
1261
1267
                          bzrlib.patiencediff.PatienceSequenceMatcher)
1265
1271
                          bzrlib.patiencediff.PatienceSequenceMatcher)
1266
1272
 
1267
1273
    def test_unique_lcs(self):
1268
 
        if compiled_patiencediff_feature.available():
 
1274
        if CompiledPatienceDiffFeature.available():
1269
1275
            from bzrlib._patiencediff_c import unique_lcs_c
1270
1276
            self.assertIs(unique_lcs_c,
1271
1277
                          bzrlib.patiencediff.unique_lcs)
1275
1281
                          bzrlib.patiencediff.unique_lcs)
1276
1282
 
1277
1283
    def test_recurse_matches(self):
1278
 
        if compiled_patiencediff_feature.available():
 
1284
        if CompiledPatienceDiffFeature.available():
1279
1285
            from bzrlib._patiencediff_c import recurse_matches_c
1280
1286
            self.assertIs(recurse_matches_c,
1281
1287
                          bzrlib.patiencediff.recurse_matches)
1290
1296
    def test_from_string(self):
1291
1297
        diff_obj = DiffFromTool.from_string('diff', None, None, None)
1292
1298
        self.addCleanup(diff_obj.finish)
1293
 
        self.assertEqual(['diff', '@old_path', '@new_path'],
 
1299
        self.assertEqual(['diff', '%(old_path)s', '%(new_path)s'],
1294
1300
            diff_obj.command_template)
1295
1301
 
1296
1302
    def test_from_string_u5(self):
1297
 
        diff_obj = DiffFromTool.from_string('diff "-u 5"', None, None, None)
 
1303
        diff_obj = DiffFromTool.from_string('diff -u\\ 5', None, None, None)
1298
1304
        self.addCleanup(diff_obj.finish)
1299
 
        self.assertEqual(['diff', '-u 5', '@old_path', '@new_path'],
 
1305
        self.assertEqual(['diff', '-u 5', '%(old_path)s', '%(new_path)s'],
1300
1306
                         diff_obj.command_template)
1301
1307
        self.assertEqual(['diff', '-u 5', 'old-path', 'new-path'],
1302
1308
                         diff_obj._get_command('old-path', 'new-path'))
1303
 
        
1304
 
    def test_from_string_path_with_backslashes(self):
1305
 
        self.requireFeature(BackslashDirSeparatorFeature)
1306
 
        tool = 'C:\\Tools\\Diff.exe'
1307
 
        diff_obj = DiffFromTool.from_string(tool, None, None, None)
1308
 
        self.addCleanup(diff_obj.finish)
1309
 
        self.assertEqual(['C:\\Tools\\Diff.exe', '@old_path', '@new_path'],
1310
 
                         diff_obj.command_template)
1311
 
        self.assertEqual(['C:\\Tools\\Diff.exe', 'old-path', 'new-path'],
1312
 
                         diff_obj._get_command('old-path', 'new-path'))
1313
1309
 
1314
1310
    def test_execute(self):
1315
1311
        output = StringIO()
1316
1312
        diff_obj = DiffFromTool(['python', '-c',
1317
 
                                 'print "@old_path @new_path"'],
 
1313
                                 'print "%(old_path)s %(new_path)s"'],
1318
1314
                                None, None, output)
1319
1315
        self.addCleanup(diff_obj.finish)
1320
1316
        diff_obj._execute('old', 'new')
1338
1334
        tree.commit('old tree')
1339
1335
        tree.lock_read()
1340
1336
        self.addCleanup(tree.unlock)
1341
 
        basis_tree = tree.basis_tree()
1342
 
        basis_tree.lock_read()
1343
 
        self.addCleanup(basis_tree.unlock)
1344
1337
        diff_obj = DiffFromTool(['python', '-c',
1345
 
                                 'print "@old_path @new_path"'],
1346
 
                                basis_tree, tree, output)
 
1338
                                 'print "%(old_path)s %(new_path)s"'],
 
1339
                                tree, tree, output)
1347
1340
        diff_obj._prepare_files('file-id', 'file', 'file')
1348
 
        # The old content should be readonly
1349
 
        self.assertReadableByAttrib(diff_obj._root, 'old\\file',
1350
 
                                    r'R.*old\\file$')
1351
 
        # The new content should use the tree object, not a 'new' file anymore
1352
 
        self.assertEndsWith(tree.basedir, 'work/tree')
1353
 
        self.assertReadableByAttrib(tree.basedir, 'file', r'work\\tree\\file$')
 
1341
        self.assertReadableByAttrib(diff_obj._root, 'old\\file', r'old\\file')
 
1342
        self.assertReadableByAttrib(diff_obj._root, 'new\\file', r'new\\file')
1354
1343
 
1355
1344
    def assertReadableByAttrib(self, cwd, relpath, regex):
1356
1345
        proc = subprocess.Popen(['attrib', relpath],
1357
1346
                                stdout=subprocess.PIPE,
1358
1347
                                cwd=cwd)
1359
 
        (result, err) = proc.communicate()
1360
 
        self.assertContainsRe(result.replace('\r\n', '\n'), regex)
 
1348
        proc.wait()
 
1349
        result = proc.stdout.read()
 
1350
        self.assertContainsRe(result, regex)
1361
1351
 
1362
1352
    def test_prepare_files(self):
1363
1353
        output = StringIO()
1377
1367
        tree.lock_read()
1378
1368
        self.addCleanup(tree.unlock)
1379
1369
        diff_obj = DiffFromTool(['python', '-c',
1380
 
                                 'print "@old_path @new_path"'],
 
1370
                                 'print "%(old_path)s %(new_path)s"'],
1381
1371
                                old_tree, tree, output)
1382
1372
        self.addCleanup(diff_obj.finish)
1383
1373
        self.assertContainsRe(diff_obj._root, 'bzr-diff-[^/]*')
1385
1375
                                                     'newname')
1386
1376
        self.assertContainsRe(old_path, 'old/oldname$')
1387
1377
        self.assertEqual(0, os.stat(old_path).st_mtime)
1388
 
        self.assertContainsRe(new_path, 'tree/newname$')
 
1378
        self.assertContainsRe(new_path, 'new/newname$')
1389
1379
        self.assertFileEqual('oldcontent', old_path)
1390
1380
        self.assertFileEqual('newcontent', new_path)
1391
1381
        if osutils.host_os_dereferences_symlinks():
1392
1382
            self.assertTrue(os.path.samefile('tree/newname', new_path))
1393
1383
        # make sure we can create files with the same parent directories
1394
1384
        diff_obj._prepare_files('file2-id', 'oldname2', 'newname2')
1395
 
 
1396
 
 
1397
 
class TestGetTreesAndBranchesToDiff(TestCaseWithTransport):
1398
 
 
1399
 
    def test_basic(self):
1400
 
        tree = self.make_branch_and_tree('tree')
1401
 
        (old_tree, new_tree,
1402
 
         old_branch, new_branch,
1403
 
         specific_files, extra_trees) = \
1404
 
            get_trees_and_branches_to_diff(['tree'], None, None, None)
1405
 
 
1406
 
        self.assertIsInstance(old_tree, RevisionTree)
1407
 
        #print dir (old_tree)
1408
 
        self.assertEqual(_mod_revision.NULL_REVISION, old_tree.get_revision_id())
1409
 
        self.assertEqual(tree.basedir, new_tree.basedir)
1410
 
        self.assertEqual(tree.branch.base, old_branch.base)
1411
 
        self.assertEqual(tree.branch.base, new_branch.base)
1412
 
        self.assertIs(None, specific_files)
1413
 
        self.assertIs(None, extra_trees)
1414
 
 
1415
 
    def test_with_rev_specs(self):
1416
 
        tree = self.make_branch_and_tree('tree')
1417
 
        self.build_tree_contents([('tree/file', 'oldcontent')])
1418
 
        tree.add('file', 'file-id')
1419
 
        tree.commit('old tree', timestamp=0, rev_id="old-id")
1420
 
        self.build_tree_contents([('tree/file', 'newcontent')])
1421
 
        tree.commit('new tree', timestamp=0, rev_id="new-id")
1422
 
 
1423
 
        revisions = [RevisionSpec.from_string('1'),
1424
 
                     RevisionSpec.from_string('2')]
1425
 
        (old_tree, new_tree,
1426
 
         old_branch, new_branch,
1427
 
         specific_files, extra_trees) = \
1428
 
            get_trees_and_branches_to_diff(['tree'], revisions, None, None)
1429
 
 
1430
 
        self.assertIsInstance(old_tree, RevisionTree)
1431
 
        self.assertEqual("old-id", old_tree.get_revision_id())
1432
 
        self.assertIsInstance(new_tree, RevisionTree)
1433
 
        self.assertEqual("new-id", new_tree.get_revision_id())
1434
 
        self.assertEqual(tree.branch.base, old_branch.base)
1435
 
        self.assertEqual(tree.branch.base, new_branch.base)
1436
 
        self.assertIs(None, specific_files)
1437
 
        self.assertEqual(tree.basedir, extra_trees[0].basedir)