~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_diff.py

  • Committer: Ian Clatworthy
  • Date: 2009-09-09 00:49:50 UTC
  • mto: (4634.37.2 prepare-2.0)
  • mto: This revision was merged to the branch mainline in revision 4689.
  • Revision ID: ian.clatworthy@canonical.com-20090909004950-43z4zdicb5u91iet
tweak quick reference naming to make it consistent with other PDFs

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
44
 
48
45
 
49
46
class _AttribFeature(Feature):
63
60
AttribFeature = _AttribFeature()
64
61
 
65
62
 
66
 
compiled_patiencediff_feature = tests.ModuleAvailableFeature(
67
 
                                    '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()
68
76
 
69
77
 
70
78
def udiff_lines(old, new, allow_binary=False):
1145
1153
 
1146
1154
class TestPatienceDiffLib_c(TestPatienceDiffLib):
1147
1155
 
1148
 
    _test_needs_features = [compiled_patiencediff_feature]
 
1156
    _test_needs_features = [CompiledPatienceDiffFeature]
1149
1157
 
1150
1158
    def setUp(self):
1151
1159
        super(TestPatienceDiffLib_c, self).setUp()
1241
1249
 
1242
1250
class TestPatienceDiffLibFiles_c(TestPatienceDiffLibFiles):
1243
1251
 
1244
 
    _test_needs_features = [compiled_patiencediff_feature]
 
1252
    _test_needs_features = [CompiledPatienceDiffFeature]
1245
1253
 
1246
1254
    def setUp(self):
1247
1255
        super(TestPatienceDiffLibFiles_c, self).setUp()
1253
1261
class TestUsingCompiledIfAvailable(TestCase):
1254
1262
 
1255
1263
    def test_PatienceSequenceMatcher(self):
1256
 
        if compiled_patiencediff_feature.available():
 
1264
        if CompiledPatienceDiffFeature.available():
1257
1265
            from bzrlib._patiencediff_c import PatienceSequenceMatcher_c
1258
1266
            self.assertIs(PatienceSequenceMatcher_c,
1259
1267
                          bzrlib.patiencediff.PatienceSequenceMatcher)
1263
1271
                          bzrlib.patiencediff.PatienceSequenceMatcher)
1264
1272
 
1265
1273
    def test_unique_lcs(self):
1266
 
        if compiled_patiencediff_feature.available():
 
1274
        if CompiledPatienceDiffFeature.available():
1267
1275
            from bzrlib._patiencediff_c import unique_lcs_c
1268
1276
            self.assertIs(unique_lcs_c,
1269
1277
                          bzrlib.patiencediff.unique_lcs)
1273
1281
                          bzrlib.patiencediff.unique_lcs)
1274
1282
 
1275
1283
    def test_recurse_matches(self):
1276
 
        if compiled_patiencediff_feature.available():
 
1284
        if CompiledPatienceDiffFeature.available():
1277
1285
            from bzrlib._patiencediff_c import recurse_matches_c
1278
1286
            self.assertIs(recurse_matches_c,
1279
1287
                          bzrlib.patiencediff.recurse_matches)
1288
1296
    def test_from_string(self):
1289
1297
        diff_obj = DiffFromTool.from_string('diff', None, None, None)
1290
1298
        self.addCleanup(diff_obj.finish)
1291
 
        self.assertEqual(['diff', '@old_path', '@new_path'],
 
1299
        self.assertEqual(['diff', '%(old_path)s', '%(new_path)s'],
1292
1300
            diff_obj.command_template)
1293
1301
 
1294
1302
    def test_from_string_u5(self):
1295
1303
        diff_obj = DiffFromTool.from_string('diff -u\\ 5', None, None, None)
1296
1304
        self.addCleanup(diff_obj.finish)
1297
 
        self.assertEqual(['diff', '-u 5', '@old_path', '@new_path'],
 
1305
        self.assertEqual(['diff', '-u 5', '%(old_path)s', '%(new_path)s'],
1298
1306
                         diff_obj.command_template)
1299
1307
        self.assertEqual(['diff', '-u 5', 'old-path', 'new-path'],
1300
1308
                         diff_obj._get_command('old-path', 'new-path'))
1302
1310
    def test_execute(self):
1303
1311
        output = StringIO()
1304
1312
        diff_obj = DiffFromTool(['python', '-c',
1305
 
                                 'print "@old_path @new_path"'],
 
1313
                                 'print "%(old_path)s %(new_path)s"'],
1306
1314
                                None, None, output)
1307
1315
        self.addCleanup(diff_obj.finish)
1308
1316
        diff_obj._execute('old', 'new')
1326
1334
        tree.commit('old tree')
1327
1335
        tree.lock_read()
1328
1336
        self.addCleanup(tree.unlock)
1329
 
        basis_tree = tree.basis_tree()
1330
 
        basis_tree.lock_read()
1331
 
        self.addCleanup(basis_tree.unlock)
1332
1337
        diff_obj = DiffFromTool(['python', '-c',
1333
 
                                 'print "@old_path @new_path"'],
1334
 
                                basis_tree, tree, output)
 
1338
                                 'print "%(old_path)s %(new_path)s"'],
 
1339
                                tree, tree, output)
1335
1340
        diff_obj._prepare_files('file-id', 'file', 'file')
1336
 
        # The old content should be readonly
1337
 
        self.assertReadableByAttrib(diff_obj._root, 'old\\file',
1338
 
                                    r'R.*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$')
 
1341
        self.assertReadableByAttrib(diff_obj._root, 'old\\file', r'old\\file')
 
1342
        self.assertReadableByAttrib(diff_obj._root, 'new\\file', r'new\\file')
1342
1343
 
1343
1344
    def assertReadableByAttrib(self, cwd, relpath, regex):
1344
1345
        proc = subprocess.Popen(['attrib', relpath],
1345
1346
                                stdout=subprocess.PIPE,
1346
1347
                                cwd=cwd)
1347
 
        (result, err) = proc.communicate()
1348
 
        self.assertContainsRe(result.replace('\r\n', '\n'), regex)
 
1348
        proc.wait()
 
1349
        result = proc.stdout.read()
 
1350
        self.assertContainsRe(result, regex)
1349
1351
 
1350
1352
    def test_prepare_files(self):
1351
1353
        output = StringIO()
1365
1367
        tree.lock_read()
1366
1368
        self.addCleanup(tree.unlock)
1367
1369
        diff_obj = DiffFromTool(['python', '-c',
1368
 
                                 'print "@old_path @new_path"'],
 
1370
                                 'print "%(old_path)s %(new_path)s"'],
1369
1371
                                old_tree, tree, output)
1370
1372
        self.addCleanup(diff_obj.finish)
1371
1373
        self.assertContainsRe(diff_obj._root, 'bzr-diff-[^/]*')
1373
1375
                                                     'newname')
1374
1376
        self.assertContainsRe(old_path, 'old/oldname$')
1375
1377
        self.assertEqual(0, os.stat(old_path).st_mtime)
1376
 
        self.assertContainsRe(new_path, 'tree/newname$')
 
1378
        self.assertContainsRe(new_path, 'new/newname$')
1377
1379
        self.assertFileEqual('oldcontent', old_path)
1378
1380
        self.assertFileEqual('newcontent', new_path)
1379
1381
        if osutils.host_os_dereferences_symlinks():
1380
1382
            self.assertTrue(os.path.samefile('tree/newname', new_path))
1381
1383
        # make sure we can create files with the same parent directories
1382
1384
        diff_obj._prepare_files('file2-id', 'oldname2', 'newname2')
1383
 
 
1384
 
 
1385
 
class TestGetTreesAndBranchesToDiff(TestCaseWithTransport):
1386
 
 
1387
 
    def test_basic(self):
1388
 
        tree = self.make_branch_and_tree('tree')
1389
 
        (old_tree, new_tree,
1390
 
         old_branch, new_branch,
1391
 
         specific_files, extra_trees) = \
1392
 
            get_trees_and_branches_to_diff(['tree'], None, None, None)
1393
 
 
1394
 
        self.assertIsInstance(old_tree, RevisionTree)
1395
 
        #print dir (old_tree)
1396
 
        self.assertEqual(_mod_revision.NULL_REVISION, old_tree.get_revision_id())
1397
 
        self.assertEqual(tree.basedir, new_tree.basedir)
1398
 
        self.assertEqual(tree.branch.base, old_branch.base)
1399
 
        self.assertEqual(tree.branch.base, new_branch.base)
1400
 
        self.assertIs(None, specific_files)
1401
 
        self.assertIs(None, extra_trees)
1402
 
 
1403
 
    def test_with_rev_specs(self):
1404
 
        tree = self.make_branch_and_tree('tree')
1405
 
        self.build_tree_contents([('tree/file', 'oldcontent')])
1406
 
        tree.add('file', 'file-id')
1407
 
        tree.commit('old tree', timestamp=0, rev_id="old-id")
1408
 
        self.build_tree_contents([('tree/file', 'newcontent')])
1409
 
        tree.commit('new tree', timestamp=0, rev_id="new-id")
1410
 
 
1411
 
        revisions = [RevisionSpec.from_string('1'),
1412
 
                     RevisionSpec.from_string('2')]
1413
 
        (old_tree, new_tree,
1414
 
         old_branch, new_branch,
1415
 
         specific_files, extra_trees) = \
1416
 
            get_trees_and_branches_to_diff(['tree'], revisions, None, None)
1417
 
 
1418
 
        self.assertIsInstance(old_tree, RevisionTree)
1419
 
        self.assertEqual("old-id", old_tree.get_revision_id())
1420
 
        self.assertIsInstance(new_tree, RevisionTree)
1421
 
        self.assertEqual("new-id", new_tree.get_revision_id())
1422
 
        self.assertEqual(tree.branch.base, old_branch.base)
1423
 
        self.assertEqual(tree.branch.base, new_branch.base)
1424
 
        self.assertIs(None, specific_files)
1425
 
        self.assertEqual(tree.basedir, extra_trees[0].basedir)