~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-01-05 09:50:04 UTC
  • mfrom: (6424 +trunk)
  • mto: This revision was merged to the branch mainline in revision 6426.
  • Revision ID: martin.packman@canonical.com-20120105095004-mia9xb7y0efmto0v
Merge bzr.dev to resolve conflicts in bzrlib.builtins

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
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
 
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'
 
1220
        osutils._fs_enc = 'utf-8'
1210
1221
        self.assertDirReaderIs(
1211
1222
            UTF8DirReaderFeature.module.UTF8DirReader)
1212
1223
 
1214
1225
        self.requireFeature(UTF8DirReaderFeature)
1215
1226
        self._save_platform_info()
1216
1227
        win32utils.winver = None # Avoid the win32 detection code
1217
 
        osutils._fs_enc = 'US-ASCII'
1218
 
        self.assertDirReaderIs(
1219
 
            UTF8DirReaderFeature.module.UTF8DirReader)
1220
 
 
1221
 
    def test_force_walkdirs_utf8_fs_ANSI(self):
1222
 
        self.requireFeature(UTF8DirReaderFeature)
1223
 
        self._save_platform_info()
1224
 
        win32utils.winver = None # Avoid the win32 detection code
1225
 
        osutils._fs_enc = 'ANSI_X3.4-1968'
 
1228
        osutils._fs_enc = 'ascii'
1226
1229
        self.assertDirReaderIs(
1227
1230
            UTF8DirReaderFeature.module.UTF8DirReader)
1228
1231
 
1229
1232
    def test_force_walkdirs_utf8_fs_latin1(self):
1230
1233
        self._save_platform_info()
1231
1234
        win32utils.winver = None # Avoid the win32 detection code
1232
 
        osutils._fs_enc = 'latin1'
 
1235
        osutils._fs_enc = 'iso-8859-1'
1233
1236
        self.assertDirReaderIs(osutils.UnicodeDirReader)
1234
1237
 
1235
1238
    def test_force_walkdirs_utf8_nt(self):
2094
2097
 
2095
2098
class TestGetuserUnicode(tests.TestCase):
2096
2099
 
 
2100
    def test_is_unicode(self):
 
2101
        user = osutils.getuser_unicode()
 
2102
        self.assertIsInstance(user, unicode)
 
2103
 
 
2104
    def envvar_to_override(self):
 
2105
        if sys.platform == "win32":
 
2106
            # Disable use of platform calls on windows so envvar is used
 
2107
            self.overrideAttr(win32utils, 'has_ctypes', False)
 
2108
            return 'USERNAME' # only variable used on windows
 
2109
        return 'LOGNAME' # first variable checked by getpass.getuser()
 
2110
 
2097
2111
    def test_ascii_user(self):
2098
 
        self.overrideEnv('LOGNAME', 'jrandom')
 
2112
        self.overrideEnv(self.envvar_to_override(), 'jrandom')
2099
2113
        self.assertEqual(u'jrandom', osutils.getuser_unicode())
2100
2114
 
2101
2115
    def test_unicode_user(self):
2107
2121
                % (osutils.get_user_encoding(),))
2108
2122
        uni_username = u'jrandom' + uni_val
2109
2123
        encoded_username = uni_username.encode(ue)
2110
 
        self.overrideEnv('LOGNAME', encoded_username)
 
2124
        self.overrideEnv(self.envvar_to_override(), encoded_username)
2111
2125
        self.assertEqual(uni_username, osutils.getuser_unicode())
2112
 
        self.overrideEnv('LOGNAME', u'jrandom\xb6'.encode(ue))
2113
 
        self.assertEqual(u'jrandom\xb6', osutils.getuser_unicode())
2114
 
 
2115
 
    def test_no_username_bug_660174(self):
2116
 
        self.requireFeature(features.win32_feature)
2117
 
        for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
2118
 
            self.overrideEnv(name, None)
2119
 
        self.assertEqual(u'UNKNOWN', osutils.getuser_unicode())
2120
2126
 
2121
2127
 
2122
2128
class TestBackupNames(tests.TestCase):
2169
2175
        self.assertTrue(osutils.find_executable_on_path('sh') is not None)
2170
2176
        self.assertTrue(
2171
2177
            osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
 
2178
 
 
2179
 
 
2180
class TestEnvironmentErrors(tests.TestCase):
 
2181
    """Test handling of environmental errors"""
 
2182
 
 
2183
    def test_is_oserror(self):
 
2184
        self.assertTrue(osutils.is_environment_error(
 
2185
            OSError(errno.EINVAL, "Invalid parameter")))
 
2186
 
 
2187
    def test_is_ioerror(self):
 
2188
        self.assertTrue(osutils.is_environment_error(
 
2189
            IOError(errno.EINVAL, "Invalid parameter")))
 
2190
 
 
2191
    def test_is_socket_error(self):
 
2192
        self.assertTrue(osutils.is_environment_error(
 
2193
            socket.error(errno.EINVAL, "Invalid parameter")))
 
2194
 
 
2195
    def test_is_select_error(self):
 
2196
        self.assertTrue(osutils.is_environment_error(
 
2197
            select.error(errno.EINVAL, "Invalid parameter")))
 
2198
 
 
2199
    def test_is_pywintypes_error(self):
 
2200
        self.requireFeature(features.pywintypes)
 
2201
        import pywintypes
 
2202
        self.assertTrue(osutils.is_environment_error(
 
2203
            pywintypes.error(errno.EINVAL, "Invalid parameter", "Caller")))