~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_diff.py

Abbreviate pack_stat struct format to '>6L'

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
    transform,
34
34
    )
35
35
from bzrlib.symbol_versioning import deprecated_in
36
 
from bzrlib.tests import features
 
36
from bzrlib.tests import features, EncodingAdapter
37
37
from bzrlib.tests.blackbox.test_diff import subst_dates
38
 
 
39
 
 
40
 
class _AttribFeature(tests.Feature):
41
 
 
42
 
    def _probe(self):
43
 
        if (sys.platform not in ('cygwin', 'win32')):
44
 
            return False
45
 
        try:
46
 
            proc = subprocess.Popen(['attrib', '.'], stdout=subprocess.PIPE)
47
 
        except OSError, e:
48
 
            return False
49
 
        return (0 == proc.wait())
50
 
 
51
 
    def feature_name(self):
52
 
        return 'attrib Windows command-line tool'
53
 
 
54
 
AttribFeature = _AttribFeature()
55
 
 
56
 
 
57
 
compiled_patiencediff_feature = tests.ModuleAvailableFeature(
58
 
                                    'bzrlib._patiencediff_c')
 
38
from bzrlib.tests import (
 
39
    features,
 
40
    )
59
41
 
60
42
 
61
43
def udiff_lines(old, new, allow_binary=False):
514
496
        is a binary file in the diff.
515
497
        """
516
498
        # See https://bugs.launchpad.net/bugs/110092.
517
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
499
        self.requireFeature(features.UnicodeFilenameFeature)
518
500
 
519
501
        # This bug isn't triggered with cStringIO.
520
502
        from StringIO import StringIO
539
521
 
540
522
    def test_unicode_filename(self):
541
523
        """Test when the filename are unicode."""
542
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
524
        self.requireFeature(features.UnicodeFilenameFeature)
543
525
 
544
526
        alpha, omega = u'\u03b1', u'\u03c9'
545
527
        autf8, outf8 = alpha.encode('utf8'), omega.encode('utf8')
571
553
        """Test for bug #382699: unicode filenames on Windows should be shown
572
554
        in user encoding.
573
555
        """
574
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
556
        self.requireFeature(features.UnicodeFilenameFeature)
575
557
        # The word 'test' in Russian
576
558
        _russian_test = u'\u0422\u0435\u0441\u0442'
577
559
        directory = _russian_test + u'/'
708
690
             ' \@\@\n-old\n\+new\n\n')
709
691
 
710
692
    def test_diff_kind_change(self):
711
 
        self.requireFeature(tests.SymlinkFeature)
 
693
        self.requireFeature(features.SymlinkFeature)
712
694
        self.build_tree_contents([('old-tree/olddir/',),
713
695
                                  ('old-tree/olddir/oldfile', 'old\n')])
714
696
        self.old_tree.add('olddir')
1172
1154
 
1173
1155
class TestPatienceDiffLib_c(TestPatienceDiffLib):
1174
1156
 
1175
 
    _test_needs_features = [compiled_patiencediff_feature]
 
1157
    _test_needs_features = [features.compiled_patiencediff_feature]
1176
1158
 
1177
1159
    def setUp(self):
1178
1160
        super(TestPatienceDiffLib_c, self).setUp()
1268
1250
 
1269
1251
class TestPatienceDiffLibFiles_c(TestPatienceDiffLibFiles):
1270
1252
 
1271
 
    _test_needs_features = [compiled_patiencediff_feature]
 
1253
    _test_needs_features = [features.compiled_patiencediff_feature]
1272
1254
 
1273
1255
    def setUp(self):
1274
1256
        super(TestPatienceDiffLibFiles_c, self).setUp()
1280
1262
class TestUsingCompiledIfAvailable(tests.TestCase):
1281
1263
 
1282
1264
    def test_PatienceSequenceMatcher(self):
1283
 
        if compiled_patiencediff_feature.available():
 
1265
        if features.compiled_patiencediff_feature.available():
1284
1266
            from bzrlib._patiencediff_c import PatienceSequenceMatcher_c
1285
1267
            self.assertIs(PatienceSequenceMatcher_c,
1286
1268
                          patiencediff.PatienceSequenceMatcher)
1290
1272
                          patiencediff.PatienceSequenceMatcher)
1291
1273
 
1292
1274
    def test_unique_lcs(self):
1293
 
        if compiled_patiencediff_feature.available():
 
1275
        if features.compiled_patiencediff_feature.available():
1294
1276
            from bzrlib._patiencediff_c import unique_lcs_c
1295
1277
            self.assertIs(unique_lcs_c,
1296
1278
                          patiencediff.unique_lcs)
1300
1282
                          patiencediff.unique_lcs)
1301
1283
 
1302
1284
    def test_recurse_matches(self):
1303
 
        if compiled_patiencediff_feature.available():
 
1285
        if features.compiled_patiencediff_feature.available():
1304
1286
            from bzrlib._patiencediff_c import recurse_matches_c
1305
1287
            self.assertIs(recurse_matches_c,
1306
1288
                          patiencediff.recurse_matches)
1356
1338
                         ' on this machine', str(e))
1357
1339
 
1358
1340
    def test_prepare_files_creates_paths_readable_by_windows_tool(self):
1359
 
        self.requireFeature(AttribFeature)
 
1341
        self.requireFeature(features.AttribFeature)
1360
1342
        output = StringIO()
1361
1343
        tree = self.make_branch_and_tree('tree')
1362
1344
        self.build_tree_contents([('tree/file', 'content')])
1421
1403
        diff_obj._prepare_files('file2-id', 'oldname2', 'newname2')
1422
1404
 
1423
1405
 
 
1406
class TestDiffFromToolEncodedFilename(tests.TestCaseWithTransport):
 
1407
 
 
1408
    def test_encodable_filename(self):
 
1409
        # Just checks file path for external diff tool.
 
1410
        # We cannot change CPython's internal encoding used by os.exec*.
 
1411
        import sys
 
1412
        diffobj = diff.DiffFromTool(['dummy', '@old_path', '@new_path'],
 
1413
                                    None, None, None)
 
1414
        for _, scenario in EncodingAdapter.encoding_scenarios:
 
1415
            encoding = scenario['encoding']
 
1416
            dirname  = scenario['info']['directory']
 
1417
            filename = scenario['info']['filename']
 
1418
 
 
1419
            self.overrideAttr(diffobj, '_fenc', lambda: encoding)
 
1420
            relpath = dirname + u'/' + filename
 
1421
            fullpath = diffobj._safe_filename('safe', relpath)
 
1422
            self.assertEqual(
 
1423
                    fullpath,
 
1424
                    fullpath.encode(encoding).decode(encoding)
 
1425
                    )
 
1426
            self.assert_(fullpath.startswith(diffobj._root + '/safe'))
 
1427
 
 
1428
    def test_unencodable_filename(self):
 
1429
        import sys
 
1430
        diffobj = diff.DiffFromTool(['dummy', '@old_path', '@new_path'],
 
1431
                                    None, None, None)
 
1432
        for _, scenario in EncodingAdapter.encoding_scenarios:
 
1433
            encoding = scenario['encoding']
 
1434
            dirname  = scenario['info']['directory']
 
1435
            filename = scenario['info']['filename']
 
1436
 
 
1437
            if encoding == 'iso-8859-1':
 
1438
                encoding = 'iso-8859-2'
 
1439
            else:
 
1440
                encoding = 'iso-8859-1'
 
1441
 
 
1442
            self.overrideAttr(diffobj, '_fenc', lambda: encoding)
 
1443
            relpath = dirname + u'/' + filename
 
1444
            fullpath = diffobj._safe_filename('safe', relpath)
 
1445
            self.assertEqual(
 
1446
                    fullpath,
 
1447
                    fullpath.encode(encoding).decode(encoding)
 
1448
                    )
 
1449
            self.assert_(fullpath.startswith(diffobj._root + '/safe'))
 
1450
 
 
1451
 
1424
1452
class TestGetTreesAndBranchesToDiffLocked(tests.TestCaseWithTransport):
1425
1453
 
1426
1454
    def call_gtabtd(self, path_list, revision_specs, old_url, new_url):
1427
 
        """Call get_trees_and_branches_to_diff_locked.  Overridden by
1428
 
        TestGetTreesAndBranchesToDiff.
1429
 
        """
 
1455
        """Call get_trees_and_branches_to_diff_locked."""
1430
1456
        return diff.get_trees_and_branches_to_diff_locked(
1431
1457
            path_list, revision_specs, old_url, new_url, self.addCleanup)
1432
1458
 
1469
1495
        self.assertEqual(tree.branch.base, new_branch.base)
1470
1496
        self.assertIs(None, specific_files)
1471
1497
        self.assertEqual(tree.basedir, extra_trees[0].basedir)
1472
 
 
1473
 
 
1474
 
class TestGetTreesAndBranchesToDiff(TestGetTreesAndBranchesToDiffLocked):
1475
 
    """Apply the tests for get_trees_and_branches_to_diff_locked to the
1476
 
    deprecated get_trees_and_branches_to_diff function.
1477
 
    """
1478
 
 
1479
 
    def call_gtabtd(self, path_list, revision_specs, old_url, new_url):
1480
 
        return self.applyDeprecated(
1481
 
            deprecated_in((2, 2, 0)), diff.get_trees_and_branches_to_diff,
1482
 
            path_list, revision_specs, old_url, new_url)