~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils.py

  • Committer: Martin Packman
  • Date: 2012-09-05 20:22:17 UTC
  • mfrom: (6437.63.6 2.5)
  • mto: This revision was merged to the branch mainline in revision 6558.
  • Revision ID: martin.packman@canonical.com-20120905202217-79io6livc1q0p66u
Merge 2.5 into bzr.dev

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):
180
181
        shape = sorted(os.listdir('.'))
181
182
        self.assertEquals(['A', 'B'], shape)
182
183
 
 
184
    def test_rename_exception(self):
 
185
        try:
 
186
            osutils.rename('nonexistent_path', 'different_nonexistent_path')
 
187
        except OSError, e:
 
188
            self.assertEqual(e.old_filename, 'nonexistent_path')
 
189
            self.assertEqual(e.new_filename, 'different_nonexistent_path')
 
190
            self.assertTrue('nonexistent_path' in e.strerror)
 
191
            self.assertTrue('different_nonexistent_path' in e.strerror)
 
192
 
183
193
 
184
194
class TestRandChars(tests.TestCase):
185
195
 
429
439
class TestLinks(tests.TestCaseInTempDir):
430
440
 
431
441
    def test_dereference_path(self):
432
 
        self.requireFeature(tests.SymlinkFeature)
 
442
        self.requireFeature(features.SymlinkFeature)
433
443
        cwd = osutils.realpath('.')
434
444
        os.mkdir('bar')
435
445
        bar_path = osutils.pathjoin(cwd, 'bar')
482
492
 
483
493
class TestCanonicalRelPath(tests.TestCaseInTempDir):
484
494
 
485
 
    _test_needs_features = [tests.CaseInsCasePresFilenameFeature]
 
495
    _test_needs_features = [features.CaseInsCasePresFilenameFeature]
486
496
 
487
497
    def test_canonical_relpath_simple(self):
488
498
        f = file('MixedCaseName', 'w')
818
828
        self.assertEqual(None, osutils.safe_file_id(None))
819
829
 
820
830
 
 
831
class TestPosixFuncs(tests.TestCase):
 
832
    """Test that the posix version of normpath returns an appropriate path
 
833
       when used with 2 leading slashes."""
 
834
 
 
835
    def test_normpath(self):
 
836
        self.assertEqual('/etc/shadow', osutils._posix_normpath('/etc/shadow'))
 
837
        self.assertEqual('/etc/shadow', osutils._posix_normpath('//etc/shadow'))
 
838
        self.assertEqual('/etc/shadow', osutils._posix_normpath('///etc/shadow'))
 
839
 
 
840
 
821
841
class TestWin32Funcs(tests.TestCase):
822
842
    """Test that _win32 versions of os utilities return appropriate paths."""
823
843
 
888
908
    """Test win32 functions that create files."""
889
909
 
890
910
    def test_getcwd(self):
891
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
911
        self.requireFeature(features.UnicodeFilenameFeature)
892
912
        os.mkdir(u'mu-\xb5')
893
913
        os.chdir(u'mu-\xb5')
894
914
        # TODO: jam 20060427 This will probably fail on Mac OSX because
984
1004
    """Test mac special functions that require directories."""
985
1005
 
986
1006
    def test_getcwd(self):
987
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1007
        self.requireFeature(features.UnicodeFilenameFeature)
988
1008
        os.mkdir(u'B\xe5gfors')
989
1009
        os.chdir(u'B\xe5gfors')
990
1010
        self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
991
1011
 
992
1012
    def test_getcwd_nonnorm(self):
993
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1013
        self.requireFeature(features.UnicodeFilenameFeature)
994
1014
        # Test that _mac_getcwd() will normalize this path
995
1015
        os.mkdir(u'Ba\u030agfors')
996
1016
        os.chdir(u'Ba\u030agfors')
1106
1126
        # are not using the filesystem's encoding
1107
1127
 
1108
1128
        # require a bytestring based filesystem
1109
 
        self.requireFeature(tests.ByteStringNamedFilesystem)
 
1129
        self.requireFeature(features.ByteStringNamedFilesystem)
1110
1130
 
1111
1131
        tree = [
1112
1132
            '.bzr',
1206
1226
        self.requireFeature(UTF8DirReaderFeature)
1207
1227
        self._save_platform_info()
1208
1228
        win32utils.winver = None # Avoid the win32 detection code
1209
 
        osutils._fs_enc = 'UTF-8'
1210
 
        self.assertDirReaderIs(UTF8DirReaderFeature.reader)
 
1229
        osutils._fs_enc = 'utf-8'
 
1230
        self.assertDirReaderIs(
 
1231
            UTF8DirReaderFeature.module.UTF8DirReader)
1211
1232
 
1212
1233
    def test_force_walkdirs_utf8_fs_ascii(self):
1213
1234
        self.requireFeature(UTF8DirReaderFeature)
1214
1235
        self._save_platform_info()
1215
1236
        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)
 
1237
        osutils._fs_enc = 'ascii'
 
1238
        self.assertDirReaderIs(
 
1239
            UTF8DirReaderFeature.module.UTF8DirReader)
1225
1240
 
1226
1241
    def test_force_walkdirs_utf8_fs_latin1(self):
1227
1242
        self._save_platform_info()
1228
1243
        win32utils.winver = None # Avoid the win32 detection code
1229
 
        osutils._fs_enc = 'latin1'
 
1244
        osutils._fs_enc = 'iso-8859-1'
1230
1245
        self.assertDirReaderIs(osutils.UnicodeDirReader)
1231
1246
 
1232
1247
    def test_force_walkdirs_utf8_nt(self):
1245
1260
 
1246
1261
    def test_unicode_walkdirs(self):
1247
1262
        """Walkdirs should always return unicode paths."""
1248
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1263
        self.requireFeature(features.UnicodeFilenameFeature)
1249
1264
        name0 = u'0file-\xb6'
1250
1265
        name1 = u'1dir-\u062c\u0648'
1251
1266
        name2 = u'2file-\u0633'
1288
1303
 
1289
1304
        The abspath portion might be in unicode or utf-8
1290
1305
        """
1291
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1306
        self.requireFeature(features.UnicodeFilenameFeature)
1292
1307
        name0 = u'0file-\xb6'
1293
1308
        name1 = u'1dir-\u062c\u0648'
1294
1309
        name2 = u'2file-\u0633'
1349
1364
 
1350
1365
        The abspath portion should be in unicode
1351
1366
        """
1352
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1367
        self.requireFeature(features.UnicodeFilenameFeature)
1353
1368
        # Use the unicode reader. TODO: split into driver-and-driven unit
1354
1369
        # tests.
1355
1370
        self._save_platform_info()
1396
1411
 
1397
1412
    def test__walkdirs_utf8_win32readdir(self):
1398
1413
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
1399
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1414
        self.requireFeature(features.UnicodeFilenameFeature)
1400
1415
        from bzrlib._walkdirs_win32 import Win32ReadDir
1401
1416
        self._save_platform_info()
1402
1417
        osutils._selected_dir_reader = Win32ReadDir()
1453
1468
    def test__walkdirs_utf_win32_find_file_stat_file(self):
1454
1469
        """make sure our Stat values are valid"""
1455
1470
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
1456
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1471
        self.requireFeature(features.UnicodeFilenameFeature)
1457
1472
        from bzrlib._walkdirs_win32 import Win32ReadDir
1458
1473
        name0u = u'0file-\xb6'
1459
1474
        name0 = name0u.encode('utf8')
1477
1492
    def test__walkdirs_utf_win32_find_file_stat_directory(self):
1478
1493
        """make sure our Stat values are valid"""
1479
1494
        self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
1480
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1495
        self.requireFeature(features.UnicodeFilenameFeature)
1481
1496
        from bzrlib._walkdirs_win32 import Win32ReadDir
1482
1497
        name0u = u'0dir-\u062c\u0648'
1483
1498
        name0 = name0u.encode('utf8')
1583
1598
        self.assertEqual(['c'], os.listdir('target/b'))
1584
1599
 
1585
1600
    def test_copy_tree_symlinks(self):
1586
 
        self.requireFeature(tests.SymlinkFeature)
 
1601
        self.requireFeature(features.SymlinkFeature)
1587
1602
        self.build_tree(['source/'])
1588
1603
        os.symlink('a/generic/path', 'source/lnk')
1589
1604
        osutils.copy_tree('source', 'target')
1856
1871
        return filtered_dirblocks
1857
1872
 
1858
1873
    def test_walk_unicode_tree(self):
1859
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1874
        self.requireFeature(features.UnicodeFilenameFeature)
1860
1875
        tree, expected_dirblocks = self._get_unicode_tree()
1861
1876
        self.build_tree(tree)
1862
1877
        result = list(osutils._walkdirs_utf8('.'))
1863
1878
        self.assertEqual(expected_dirblocks, self._filter_out(result))
1864
1879
 
1865
1880
    def test_symlink(self):
1866
 
        self.requireFeature(tests.SymlinkFeature)
1867
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1881
        self.requireFeature(features.SymlinkFeature)
 
1882
        self.requireFeature(features.UnicodeFilenameFeature)
1868
1883
        target = u'target\N{Euro Sign}'
1869
1884
        link_name = u'l\N{Euro Sign}nk'
1870
1885
        os.symlink(target, link_name)
1888
1903
    But prior python versions failed to properly encode the passed unicode
1889
1904
    string.
1890
1905
    """
1891
 
    _test_needs_features = [tests.SymlinkFeature, tests.UnicodeFilenameFeature]
 
1906
    _test_needs_features = [features.SymlinkFeature, features.UnicodeFilenameFeature]
1892
1907
 
1893
1908
    def setUp(self):
1894
1909
        super(tests.TestCaseInTempDir, self).setUp()
2089
2104
        self.assertEquals(self.gid, s.st_gid)
2090
2105
 
2091
2106
 
 
2107
class TestPathFromEnviron(tests.TestCase):
 
2108
 
 
2109
    def test_is_unicode(self):
 
2110
        self.overrideEnv('BZR_TEST_PATH', './anywhere at all/')
 
2111
        path = osutils.path_from_environ('BZR_TEST_PATH')
 
2112
        self.assertIsInstance(path, unicode)
 
2113
        self.assertEqual(u'./anywhere at all/', path)
 
2114
 
 
2115
    def test_posix_path_env_ascii(self):
 
2116
        self.overrideEnv('BZR_TEST_PATH', '/tmp')
 
2117
        home = osutils._posix_path_from_environ('BZR_TEST_PATH')
 
2118
        self.assertIsInstance(home, unicode)
 
2119
        self.assertEqual(u'/tmp', home)
 
2120
 
 
2121
    def test_posix_path_env_unicode(self):
 
2122
        self.requireFeature(features.ByteStringNamedFilesystem)
 
2123
        self.overrideEnv('BZR_TEST_PATH', '/home/\xa7test')
 
2124
        self.overrideAttr(osutils, "_fs_enc", "iso8859-1")
 
2125
        self.assertEqual(u'/home/\xa7test',
 
2126
            osutils._posix_path_from_environ('BZR_TEST_PATH'))
 
2127
        osutils._fs_enc = "iso8859-5"
 
2128
        self.assertEqual(u'/home/\u0407test',
 
2129
            osutils._posix_path_from_environ('BZR_TEST_PATH'))
 
2130
        osutils._fs_enc = "utf-8"
 
2131
        self.assertRaises(errors.BadFilenameEncoding,
 
2132
            osutils._posix_path_from_environ, 'BZR_TEST_PATH')
 
2133
 
 
2134
 
 
2135
class TestGetHomeDir(tests.TestCase):
 
2136
 
 
2137
    def test_is_unicode(self):
 
2138
        home = osutils._get_home_dir()
 
2139
        self.assertIsInstance(home, unicode)
 
2140
 
 
2141
    def test_posix_homeless(self):
 
2142
        self.overrideEnv('HOME', None)
 
2143
        home = osutils._get_home_dir()
 
2144
        self.assertIsInstance(home, unicode)
 
2145
 
 
2146
    def test_posix_home_ascii(self):
 
2147
        self.overrideEnv('HOME', '/home/test')
 
2148
        home = osutils._posix_get_home_dir()
 
2149
        self.assertIsInstance(home, unicode)
 
2150
        self.assertEqual(u'/home/test', home)
 
2151
 
 
2152
    def test_posix_home_unicode(self):
 
2153
        self.requireFeature(features.ByteStringNamedFilesystem)
 
2154
        self.overrideEnv('HOME', '/home/\xa7test')
 
2155
        self.overrideAttr(osutils, "_fs_enc", "iso8859-1")
 
2156
        self.assertEqual(u'/home/\xa7test', osutils._posix_get_home_dir())
 
2157
        osutils._fs_enc = "iso8859-5"
 
2158
        self.assertEqual(u'/home/\u0407test', osutils._posix_get_home_dir())
 
2159
        osutils._fs_enc = "utf-8"
 
2160
        self.assertRaises(errors.BadFilenameEncoding,
 
2161
            osutils._posix_get_home_dir)
 
2162
 
 
2163
 
2092
2164
class TestGetuserUnicode(tests.TestCase):
2093
2165
 
 
2166
    def test_is_unicode(self):
 
2167
        user = osutils.getuser_unicode()
 
2168
        self.assertIsInstance(user, unicode)
 
2169
 
 
2170
    def envvar_to_override(self):
 
2171
        if sys.platform == "win32":
 
2172
            # Disable use of platform calls on windows so envvar is used
 
2173
            self.overrideAttr(win32utils, 'has_ctypes', False)
 
2174
            return 'USERNAME' # only variable used on windows
 
2175
        return 'LOGNAME' # first variable checked by getpass.getuser()
 
2176
 
2094
2177
    def test_ascii_user(self):
2095
 
        self.overrideEnv('LOGNAME', 'jrandom')
 
2178
        self.overrideEnv(self.envvar_to_override(), 'jrandom')
2096
2179
        self.assertEqual(u'jrandom', osutils.getuser_unicode())
2097
2180
 
2098
2181
    def test_unicode_user(self):
2104
2187
                % (osutils.get_user_encoding(),))
2105
2188
        uni_username = u'jrandom' + uni_val
2106
2189
        encoded_username = uni_username.encode(ue)
2107
 
        self.overrideEnv('LOGNAME', encoded_username)
 
2190
        self.overrideEnv(self.envvar_to_override(), encoded_username)
2108
2191
        self.assertEqual(uni_username, osutils.getuser_unicode())
2109
 
        self.overrideEnv('LOGNAME', u'jrandom\xb6'.encode(ue))
2110
 
        self.assertEqual(u'jrandom\xb6', osutils.getuser_unicode())
2111
 
 
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
2192
 
2118
2193
 
2119
2194
class TestBackupNames(tests.TestCase):
2159
2234
        self.assertTrue(
2160
2235
            osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
2161
2236
        self.assertTrue(osutils.find_executable_on_path('file.txt') is None)
 
2237
        
 
2238
    def test_windows_app_path(self):
 
2239
        if sys.platform != 'win32':
 
2240
            raise tests.TestSkipped('test requires win32')
 
2241
        # Override PATH env var so that exe can only be found on App Path
 
2242
        self.overrideEnv('PATH', '')
 
2243
        # Internt Explorer is always registered in the App Path
 
2244
        self.assertTrue(osutils.find_executable_on_path('iexplore') is not None)
2162
2245
 
2163
2246
    def test_other(self):
2164
2247
        if sys.platform == 'win32':
2166
2249
        self.assertTrue(osutils.find_executable_on_path('sh') is not None)
2167
2250
        self.assertTrue(
2168
2251
            osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
 
2252
 
 
2253
 
 
2254
class TestEnvironmentErrors(tests.TestCase):
 
2255
    """Test handling of environmental errors"""
 
2256
 
 
2257
    def test_is_oserror(self):
 
2258
        self.assertTrue(osutils.is_environment_error(
 
2259
            OSError(errno.EINVAL, "Invalid parameter")))
 
2260
 
 
2261
    def test_is_ioerror(self):
 
2262
        self.assertTrue(osutils.is_environment_error(
 
2263
            IOError(errno.EINVAL, "Invalid parameter")))
 
2264
 
 
2265
    def test_is_socket_error(self):
 
2266
        self.assertTrue(osutils.is_environment_error(
 
2267
            socket.error(errno.EINVAL, "Invalid parameter")))
 
2268
 
 
2269
    def test_is_select_error(self):
 
2270
        self.assertTrue(osutils.is_environment_error(
 
2271
            select.error(errno.EINVAL, "Invalid parameter")))
 
2272
 
 
2273
    def test_is_pywintypes_error(self):
 
2274
        self.requireFeature(features.pywintypes)
 
2275
        import pywintypes
 
2276
        self.assertTrue(osutils.is_environment_error(
 
2277
            pywintypes.error(errno.EINVAL, "Invalid parameter", "Caller")))