~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils.py

(gz) Fix deprecations of win32utils path function unicode wrappers (Martin
 Packman)

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
import errno
21
21
import os
22
22
import re
 
23
import select
23
24
import socket
24
25
import sys
25
26
import time
41
42
from bzrlib.tests.scenarios import load_tests_apply_scenarios
42
43
 
43
44
 
44
 
class _UTF8DirReaderFeature(tests.Feature):
 
45
class _UTF8DirReaderFeature(features.Feature):
45
46
 
46
47
    def _probe(self):
47
48
        try:
54
55
    def feature_name(self):
55
56
        return 'bzrlib._readdir_pyx'
56
57
 
57
 
UTF8DirReaderFeature = _UTF8DirReaderFeature()
 
58
UTF8DirReaderFeature = features.ModuleAvailableFeature('bzrlib._readdir_pyx')
58
59
 
59
 
term_ios_feature = tests.ModuleAvailableFeature('termios')
 
60
term_ios_feature = features.ModuleAvailableFeature('termios')
60
61
 
61
62
 
62
63
def _already_unicode(s):
429
430
class TestLinks(tests.TestCaseInTempDir):
430
431
 
431
432
    def test_dereference_path(self):
432
 
        self.requireFeature(tests.SymlinkFeature)
 
433
        self.requireFeature(features.SymlinkFeature)
433
434
        cwd = osutils.realpath('.')
434
435
        os.mkdir('bar')
435
436
        bar_path = osutils.pathjoin(cwd, 'bar')
482
483
 
483
484
class TestCanonicalRelPath(tests.TestCaseInTempDir):
484
485
 
485
 
    _test_needs_features = [tests.CaseInsCasePresFilenameFeature]
 
486
    _test_needs_features = [features.CaseInsCasePresFilenameFeature]
486
487
 
487
488
    def test_canonical_relpath_simple(self):
488
489
        f = file('MixedCaseName', 'w')
818
819
        self.assertEqual(None, osutils.safe_file_id(None))
819
820
 
820
821
 
 
822
class TestPosixFuncs(tests.TestCase):
 
823
    """Test that the posix version of normpath returns an appropriate path
 
824
       when used with 2 leading slashes."""
 
825
 
 
826
    def test_normpath(self):
 
827
        self.assertEqual('/etc/shadow', osutils._posix_normpath('/etc/shadow'))
 
828
        self.assertEqual('/etc/shadow', osutils._posix_normpath('//etc/shadow'))
 
829
        self.assertEqual('/etc/shadow', osutils._posix_normpath('///etc/shadow'))
 
830
 
 
831
 
821
832
class TestWin32Funcs(tests.TestCase):
822
833
    """Test that _win32 versions of os utilities return appropriate paths."""
823
834
 
888
899
    """Test win32 functions that create files."""
889
900
 
890
901
    def test_getcwd(self):
891
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
902
        self.requireFeature(features.UnicodeFilenameFeature)
892
903
        os.mkdir(u'mu-\xb5')
893
904
        os.chdir(u'mu-\xb5')
894
905
        # TODO: jam 20060427 This will probably fail on Mac OSX because
984
995
    """Test mac special functions that require directories."""
985
996
 
986
997
    def test_getcwd(self):
987
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
998
        self.requireFeature(features.UnicodeFilenameFeature)
988
999
        os.mkdir(u'B\xe5gfors')
989
1000
        os.chdir(u'B\xe5gfors')
990
1001
        self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
991
1002
 
992
1003
    def test_getcwd_nonnorm(self):
993
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1004
        self.requireFeature(features.UnicodeFilenameFeature)
994
1005
        # Test that _mac_getcwd() will normalize this path
995
1006
        os.mkdir(u'Ba\u030agfors')
996
1007
        os.chdir(u'Ba\u030agfors')
1106
1117
        # are not using the filesystem's encoding
1107
1118
 
1108
1119
        # require a bytestring based filesystem
1109
 
        self.requireFeature(tests.ByteStringNamedFilesystem)
 
1120
        self.requireFeature(features.ByteStringNamedFilesystem)
1110
1121
 
1111
1122
        tree = [
1112
1123
            '.bzr',
1206
1217
        self.requireFeature(UTF8DirReaderFeature)
1207
1218
        self._save_platform_info()
1208
1219
        win32utils.winver = None # Avoid the win32 detection code
1209
 
        osutils._fs_enc = 'UTF-8'
1210
 
        self.assertDirReaderIs(UTF8DirReaderFeature.reader)
 
1220
        osutils._fs_enc = 'utf-8'
 
1221
        self.assertDirReaderIs(
 
1222
            UTF8DirReaderFeature.module.UTF8DirReader)
1211
1223
 
1212
1224
    def test_force_walkdirs_utf8_fs_ascii(self):
1213
1225
        self.requireFeature(UTF8DirReaderFeature)
1214
1226
        self._save_platform_info()
1215
1227
        win32utils.winver = None # Avoid the win32 detection code
1216
 
        osutils._fs_enc = 'US-ASCII'
1217
 
        self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1218
 
 
1219
 
    def test_force_walkdirs_utf8_fs_ANSI(self):
1220
 
        self.requireFeature(UTF8DirReaderFeature)
1221
 
        self._save_platform_info()
1222
 
        win32utils.winver = None # Avoid the win32 detection code
1223
 
        osutils._fs_enc = 'ANSI_X3.4-1968'
1224
 
        self.assertDirReaderIs(UTF8DirReaderFeature.reader)
 
1228
        osutils._fs_enc = 'ascii'
 
1229
        self.assertDirReaderIs(
 
1230
            UTF8DirReaderFeature.module.UTF8DirReader)
1225
1231
 
1226
1232
    def test_force_walkdirs_utf8_fs_latin1(self):
1227
1233
        self._save_platform_info()
1228
1234
        win32utils.winver = None # Avoid the win32 detection code
1229
 
        osutils._fs_enc = 'latin1'
 
1235
        osutils._fs_enc = 'iso-8859-1'
1230
1236
        self.assertDirReaderIs(osutils.UnicodeDirReader)
1231
1237
 
1232
1238
    def test_force_walkdirs_utf8_nt(self):
1245
1251
 
1246
1252
    def test_unicode_walkdirs(self):
1247
1253
        """Walkdirs should always return unicode paths."""
1248
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1254
        self.requireFeature(features.UnicodeFilenameFeature)
1249
1255
        name0 = u'0file-\xb6'
1250
1256
        name1 = u'1dir-\u062c\u0648'
1251
1257
        name2 = u'2file-\u0633'
1288
1294
 
1289
1295
        The abspath portion might be in unicode or utf-8
1290
1296
        """
1291
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1297
        self.requireFeature(features.UnicodeFilenameFeature)
1292
1298
        name0 = u'0file-\xb6'
1293
1299
        name1 = u'1dir-\u062c\u0648'
1294
1300
        name2 = u'2file-\u0633'
1349
1355
 
1350
1356
        The abspath portion should be in unicode
1351
1357
        """
1352
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1358
        self.requireFeature(features.UnicodeFilenameFeature)
1353
1359
        # Use the unicode reader. TODO: split into driver-and-driven unit
1354
1360
        # tests.
1355
1361
        self._save_platform_info()
1396
1402
 
1397
1403
    def test__walkdirs_utf8_win32readdir(self):
1398
1404
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
1399
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1405
        self.requireFeature(features.UnicodeFilenameFeature)
1400
1406
        from bzrlib._walkdirs_win32 import Win32ReadDir
1401
1407
        self._save_platform_info()
1402
1408
        osutils._selected_dir_reader = Win32ReadDir()
1453
1459
    def test__walkdirs_utf_win32_find_file_stat_file(self):
1454
1460
        """make sure our Stat values are valid"""
1455
1461
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
1456
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1462
        self.requireFeature(features.UnicodeFilenameFeature)
1457
1463
        from bzrlib._walkdirs_win32 import Win32ReadDir
1458
1464
        name0u = u'0file-\xb6'
1459
1465
        name0 = name0u.encode('utf8')
1477
1483
    def test__walkdirs_utf_win32_find_file_stat_directory(self):
1478
1484
        """make sure our Stat values are valid"""
1479
1485
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
1480
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1486
        self.requireFeature(features.UnicodeFilenameFeature)
1481
1487
        from bzrlib._walkdirs_win32 import Win32ReadDir
1482
1488
        name0u = u'0dir-\u062c\u0648'
1483
1489
        name0 = name0u.encode('utf8')
1583
1589
        self.assertEqual(['c'], os.listdir('target/b'))
1584
1590
 
1585
1591
    def test_copy_tree_symlinks(self):
1586
 
        self.requireFeature(tests.SymlinkFeature)
 
1592
        self.requireFeature(features.SymlinkFeature)
1587
1593
        self.build_tree(['source/'])
1588
1594
        os.symlink('a/generic/path', 'source/lnk')
1589
1595
        osutils.copy_tree('source', 'target')
1856
1862
        return filtered_dirblocks
1857
1863
 
1858
1864
    def test_walk_unicode_tree(self):
1859
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1865
        self.requireFeature(features.UnicodeFilenameFeature)
1860
1866
        tree, expected_dirblocks = self._get_unicode_tree()
1861
1867
        self.build_tree(tree)
1862
1868
        result = list(osutils._walkdirs_utf8('.'))
1863
1869
        self.assertEqual(expected_dirblocks, self._filter_out(result))
1864
1870
 
1865
1871
    def test_symlink(self):
1866
 
        self.requireFeature(tests.SymlinkFeature)
1867
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1872
        self.requireFeature(features.SymlinkFeature)
 
1873
        self.requireFeature(features.UnicodeFilenameFeature)
1868
1874
        target = u'target\N{Euro Sign}'
1869
1875
        link_name = u'l\N{Euro Sign}nk'
1870
1876
        os.symlink(target, link_name)
1888
1894
    But prior python versions failed to properly encode the passed unicode
1889
1895
    string.
1890
1896
    """
1891
 
    _test_needs_features = [tests.SymlinkFeature, tests.UnicodeFilenameFeature]
 
1897
    _test_needs_features = [features.SymlinkFeature, features.UnicodeFilenameFeature]
1892
1898
 
1893
1899
    def setUp(self):
1894
1900
        super(tests.TestCaseInTempDir, self).setUp()
2109
2115
        self.overrideEnv('LOGNAME', u'jrandom\xb6'.encode(ue))
2110
2116
        self.assertEqual(u'jrandom\xb6', osutils.getuser_unicode())
2111
2117
 
2112
 
    def test_no_username_bug_660174(self):
2113
 
        self.requireFeature(features.win32_feature)
2114
 
        for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
2115
 
            self.overrideEnv(name, None)
2116
 
        self.assertEqual(u'UNKNOWN', osutils.getuser_unicode())
2117
 
 
2118
2118
 
2119
2119
class TestBackupNames(tests.TestCase):
2120
2120
 
2166
2166
        self.assertTrue(osutils.find_executable_on_path('sh') is not None)
2167
2167
        self.assertTrue(
2168
2168
            osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
 
2169
 
 
2170
 
 
2171
class TestEnvironmentErrors(tests.TestCase):
 
2172
    """Test handling of environmental errors"""
 
2173
 
 
2174
    def test_is_oserror(self):
 
2175
        self.assertTrue(osutils.is_environment_error(
 
2176
            OSError(errno.EINVAL, "Invalid parameter")))
 
2177
 
 
2178
    def test_is_ioerror(self):
 
2179
        self.assertTrue(osutils.is_environment_error(
 
2180
            IOError(errno.EINVAL, "Invalid parameter")))
 
2181
 
 
2182
    def test_is_socket_error(self):
 
2183
        self.assertTrue(osutils.is_environment_error(
 
2184
            socket.error(errno.EINVAL, "Invalid parameter")))
 
2185
 
 
2186
    def test_is_select_error(self):
 
2187
        self.assertTrue(osutils.is_environment_error(
 
2188
            select.error(errno.EINVAL, "Invalid parameter")))
 
2189
 
 
2190
    def test_is_pywintypes_error(self):
 
2191
        self.requireFeature(features.pywintypes)
 
2192
        import pywintypes
 
2193
        self.assertTrue(osutils.is_environment_error(
 
2194
            pywintypes.error(errno.EINVAL, "Invalid parameter", "Caller")))