818
828
self.assertEqual(None, osutils.safe_file_id(None))
831
class TestSendAll(tests.TestCase):
833
def test_send_with_disconnected_socket(self):
834
class DisconnectedSocket(object):
835
def __init__(self, err):
837
def send(self, content):
841
# All of these should be treated as ConnectionReset
843
for err_cls in (IOError, socket.error):
844
for errnum in osutils._end_of_stream_errors:
845
errs.append(err_cls(errnum))
847
sock = DisconnectedSocket(err)
848
self.assertRaises(errors.ConnectionReset,
849
osutils.send_all, sock, 'some more content')
851
def test_send_with_no_progress(self):
852
# See https://bugs.launchpad.net/bzr/+bug/1047309
853
# It seems that paramiko can get into a state where it doesn't error,
854
# but it returns 0 bytes sent for requests over and over again.
855
class NoSendingSocket(object):
858
def send(self, bytes):
860
if self.call_count > 100:
861
# Prevent the test suite from hanging
862
raise RuntimeError('too many calls')
864
sock = NoSendingSocket()
865
self.assertRaises(errors.ConnectionReset,
866
osutils.send_all, sock, 'content')
867
self.assertEqual(1, sock.call_count)
870
class TestPosixFuncs(tests.TestCase):
871
"""Test that the posix version of normpath returns an appropriate path
872
when used with 2 leading slashes."""
874
def test_normpath(self):
875
self.assertEqual('/etc/shadow', osutils._posix_normpath('/etc/shadow'))
876
self.assertEqual('/etc/shadow', osutils._posix_normpath('//etc/shadow'))
877
self.assertEqual('/etc/shadow', osutils._posix_normpath('///etc/shadow'))
821
880
class TestWin32Funcs(tests.TestCase):
822
881
"""Test that _win32 versions of os utilities return appropriate paths."""
1206
1265
self.requireFeature(UTF8DirReaderFeature)
1207
1266
self._save_platform_info()
1208
1267
win32utils.winver = None # Avoid the win32 detection code
1209
osutils._fs_enc = 'UTF-8'
1210
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1268
osutils._fs_enc = 'utf-8'
1269
self.assertDirReaderIs(
1270
UTF8DirReaderFeature.module.UTF8DirReader)
1212
1272
def test_force_walkdirs_utf8_fs_ascii(self):
1213
1273
self.requireFeature(UTF8DirReaderFeature)
1214
1274
self._save_platform_info()
1215
1275
win32utils.winver = None # Avoid the win32 detection code
1216
osutils._fs_enc = 'US-ASCII'
1217
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
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)
1276
osutils._fs_enc = 'ascii'
1277
self.assertDirReaderIs(
1278
UTF8DirReaderFeature.module.UTF8DirReader)
1226
1280
def test_force_walkdirs_utf8_fs_latin1(self):
1227
1281
self._save_platform_info()
1228
1282
win32utils.winver = None # Avoid the win32 detection code
1229
osutils._fs_enc = 'latin1'
1283
osutils._fs_enc = 'iso-8859-1'
1230
1284
self.assertDirReaderIs(osutils.UnicodeDirReader)
1232
1286
def test_force_walkdirs_utf8_nt(self):
1856
1910
return filtered_dirblocks
1858
1912
def test_walk_unicode_tree(self):
1859
self.requireFeature(tests.UnicodeFilenameFeature)
1913
self.requireFeature(features.UnicodeFilenameFeature)
1860
1914
tree, expected_dirblocks = self._get_unicode_tree()
1861
1915
self.build_tree(tree)
1862
1916
result = list(osutils._walkdirs_utf8('.'))
1863
1917
self.assertEqual(expected_dirblocks, self._filter_out(result))
1865
1919
def test_symlink(self):
1866
self.requireFeature(tests.SymlinkFeature)
1867
self.requireFeature(tests.UnicodeFilenameFeature)
1920
self.requireFeature(features.SymlinkFeature)
1921
self.requireFeature(features.UnicodeFilenameFeature)
1868
1922
target = u'target\N{Euro Sign}'
1869
1923
link_name = u'l\N{Euro Sign}nk'
1870
1924
os.symlink(target, link_name)
2089
2143
self.assertEquals(self.gid, s.st_gid)
2146
class TestPathFromEnviron(tests.TestCase):
2148
def test_is_unicode(self):
2149
self.overrideEnv('BZR_TEST_PATH', './anywhere at all/')
2150
path = osutils.path_from_environ('BZR_TEST_PATH')
2151
self.assertIsInstance(path, unicode)
2152
self.assertEqual(u'./anywhere at all/', path)
2154
def test_posix_path_env_ascii(self):
2155
self.overrideEnv('BZR_TEST_PATH', '/tmp')
2156
home = osutils._posix_path_from_environ('BZR_TEST_PATH')
2157
self.assertIsInstance(home, unicode)
2158
self.assertEqual(u'/tmp', home)
2160
def test_posix_path_env_unicode(self):
2161
self.requireFeature(features.ByteStringNamedFilesystem)
2162
self.overrideEnv('BZR_TEST_PATH', '/home/\xa7test')
2163
self.overrideAttr(osutils, "_fs_enc", "iso8859-1")
2164
self.assertEqual(u'/home/\xa7test',
2165
osutils._posix_path_from_environ('BZR_TEST_PATH'))
2166
osutils._fs_enc = "iso8859-5"
2167
self.assertEqual(u'/home/\u0407test',
2168
osutils._posix_path_from_environ('BZR_TEST_PATH'))
2169
osutils._fs_enc = "utf-8"
2170
self.assertRaises(errors.BadFilenameEncoding,
2171
osutils._posix_path_from_environ, 'BZR_TEST_PATH')
2174
class TestGetHomeDir(tests.TestCase):
2176
def test_is_unicode(self):
2177
home = osutils._get_home_dir()
2178
self.assertIsInstance(home, unicode)
2180
def test_posix_homeless(self):
2181
self.overrideEnv('HOME', None)
2182
home = osutils._get_home_dir()
2183
self.assertIsInstance(home, unicode)
2185
def test_posix_home_ascii(self):
2186
self.overrideEnv('HOME', '/home/test')
2187
home = osutils._posix_get_home_dir()
2188
self.assertIsInstance(home, unicode)
2189
self.assertEqual(u'/home/test', home)
2191
def test_posix_home_unicode(self):
2192
self.requireFeature(features.ByteStringNamedFilesystem)
2193
self.overrideEnv('HOME', '/home/\xa7test')
2194
self.overrideAttr(osutils, "_fs_enc", "iso8859-1")
2195
self.assertEqual(u'/home/\xa7test', osutils._posix_get_home_dir())
2196
osutils._fs_enc = "iso8859-5"
2197
self.assertEqual(u'/home/\u0407test', osutils._posix_get_home_dir())
2198
osutils._fs_enc = "utf-8"
2199
self.assertRaises(errors.BadFilenameEncoding,
2200
osutils._posix_get_home_dir)
2092
2203
class TestGetuserUnicode(tests.TestCase):
2205
def test_is_unicode(self):
2206
user = osutils.getuser_unicode()
2207
self.assertIsInstance(user, unicode)
2209
def envvar_to_override(self):
2210
if sys.platform == "win32":
2211
# Disable use of platform calls on windows so envvar is used
2212
self.overrideAttr(win32utils, 'has_ctypes', False)
2213
return 'USERNAME' # only variable used on windows
2214
return 'LOGNAME' # first variable checked by getpass.getuser()
2094
2216
def test_ascii_user(self):
2095
self.overrideEnv('LOGNAME', 'jrandom')
2217
self.overrideEnv(self.envvar_to_override(), 'jrandom')
2096
2218
self.assertEqual(u'jrandom', osutils.getuser_unicode())
2098
2220
def test_unicode_user(self):
2104
2226
% (osutils.get_user_encoding(),))
2105
2227
uni_username = u'jrandom' + uni_val
2106
2228
encoded_username = uni_username.encode(ue)
2107
self.overrideEnv('LOGNAME', encoded_username)
2229
self.overrideEnv(self.envvar_to_override(), encoded_username)
2108
2230
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())
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())
2119
2233
class TestBackupNames(tests.TestCase):
2166
2288
self.assertTrue(osutils.find_executable_on_path('sh') is not None)
2167
2289
self.assertTrue(
2168
2290
osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
2293
class TestEnvironmentErrors(tests.TestCase):
2294
"""Test handling of environmental errors"""
2296
def test_is_oserror(self):
2297
self.assertTrue(osutils.is_environment_error(
2298
OSError(errno.EINVAL, "Invalid parameter")))
2300
def test_is_ioerror(self):
2301
self.assertTrue(osutils.is_environment_error(
2302
IOError(errno.EINVAL, "Invalid parameter")))
2304
def test_is_socket_error(self):
2305
self.assertTrue(osutils.is_environment_error(
2306
socket.error(errno.EINVAL, "Invalid parameter")))
2308
def test_is_select_error(self):
2309
self.assertTrue(osutils.is_environment_error(
2310
select.error(errno.EINVAL, "Invalid parameter")))
2312
def test_is_pywintypes_error(self):
2313
self.requireFeature(features.pywintypes)
2315
self.assertTrue(osutils.is_environment_error(
2316
pywintypes.error(errno.EINVAL, "Invalid parameter", "Caller")))