426
437
self.assertTrue(-eighteen_hours < offset < eighteen_hours)
440
class TestFdatasync(tests.TestCaseInTempDir):
442
def do_fdatasync(self):
443
f = tempfile.NamedTemporaryFile()
444
osutils.fdatasync(f.fileno())
448
def raise_eopnotsupp(*args, **kwargs):
449
raise IOError(errno.EOPNOTSUPP, os.strerror(errno.EOPNOTSUPP))
452
def raise_enotsup(*args, **kwargs):
453
raise IOError(errno.ENOTSUP, os.strerror(errno.ENOTSUP))
455
def test_fdatasync_handles_system_function(self):
456
self.overrideAttr(os, "fdatasync")
459
def test_fdatasync_handles_no_fdatasync_no_fsync(self):
460
self.overrideAttr(os, "fdatasync")
461
self.overrideAttr(os, "fsync")
464
def test_fdatasync_handles_no_EOPNOTSUPP(self):
465
self.overrideAttr(errno, "EOPNOTSUPP")
468
def test_fdatasync_catches_ENOTSUP(self):
469
enotsup = getattr(errno, "ENOTSUP", None)
471
raise tests.TestNotApplicable("No ENOTSUP on this platform")
472
self.overrideAttr(os, "fdatasync", self.raise_enotsup)
475
def test_fdatasync_catches_EOPNOTSUPP(self):
476
enotsup = getattr(errno, "EOPNOTSUPP", None)
478
raise tests.TestNotApplicable("No EOPNOTSUPP on this platform")
479
self.overrideAttr(os, "fdatasync", self.raise_eopnotsupp)
429
483
class TestLinks(tests.TestCaseInTempDir):
431
485
def test_dereference_path(self):
432
self.requireFeature(tests.SymlinkFeature)
486
self.requireFeature(features.SymlinkFeature)
433
487
cwd = osutils.realpath('.')
435
489
bar_path = osutils.pathjoin(cwd, 'bar')
818
872
self.assertEqual(None, osutils.safe_file_id(None))
875
class TestSendAll(tests.TestCase):
877
def test_send_with_disconnected_socket(self):
878
class DisconnectedSocket(object):
879
def __init__(self, err):
881
def send(self, content):
885
# All of these should be treated as ConnectionReset
887
for err_cls in (IOError, socket.error):
888
for errnum in osutils._end_of_stream_errors:
889
errs.append(err_cls(errnum))
891
sock = DisconnectedSocket(err)
892
self.assertRaises(errors.ConnectionReset,
893
osutils.send_all, sock, 'some more content')
895
def test_send_with_no_progress(self):
896
# See https://bugs.launchpad.net/bzr/+bug/1047309
897
# It seems that paramiko can get into a state where it doesn't error,
898
# but it returns 0 bytes sent for requests over and over again.
899
class NoSendingSocket(object):
902
def send(self, bytes):
904
if self.call_count > 100:
905
# Prevent the test suite from hanging
906
raise RuntimeError('too many calls')
908
sock = NoSendingSocket()
909
self.assertRaises(errors.ConnectionReset,
910
osutils.send_all, sock, 'content')
911
self.assertEqual(1, sock.call_count)
914
class TestPosixFuncs(tests.TestCase):
915
"""Test that the posix version of normpath returns an appropriate path
916
when used with 2 leading slashes."""
918
def test_normpath(self):
919
self.assertEqual('/etc/shadow', osutils._posix_normpath('/etc/shadow'))
920
self.assertEqual('/etc/shadow', osutils._posix_normpath('//etc/shadow'))
921
self.assertEqual('/etc/shadow', osutils._posix_normpath('///etc/shadow'))
821
924
class TestWin32Funcs(tests.TestCase):
822
925
"""Test that _win32 versions of os utilities return appropriate paths."""
824
927
def test_abspath(self):
928
self.requireFeature(features.win32_feature)
825
929
self.assertEqual('C:/foo', osutils._win32_abspath('C:\\foo'))
826
930
self.assertEqual('C:/foo', osutils._win32_abspath('C:/foo'))
827
931
self.assertEqual('//HOST/path', osutils._win32_abspath(r'\\HOST\path'))
840
944
osutils._win32_pathjoin('path/to', 'C:/foo'))
841
945
self.assertEqual('path/to/foo',
842
946
osutils._win32_pathjoin('path/to/', 'foo'))
843
self.assertEqual('/foo',
948
def test_pathjoin_late_bugfix(self):
949
if sys.version_info < (2, 7, 6):
953
self.assertEqual(expected,
844
954
osutils._win32_pathjoin('C:/path/to/', '/foo'))
845
self.assertEqual('/foo',
955
self.assertEqual(expected,
846
956
osutils._win32_pathjoin('C:\\path\\to\\', '\\foo'))
848
958
def test_normpath(self):
1206
1317
self.requireFeature(UTF8DirReaderFeature)
1207
1318
self._save_platform_info()
1208
1319
win32utils.winver = None # Avoid the win32 detection code
1209
osutils._fs_enc = 'UTF-8'
1210
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1320
osutils._fs_enc = 'utf-8'
1321
self.assertDirReaderIs(
1322
UTF8DirReaderFeature.module.UTF8DirReader)
1212
1324
def test_force_walkdirs_utf8_fs_ascii(self):
1213
1325
self.requireFeature(UTF8DirReaderFeature)
1214
1326
self._save_platform_info()
1215
1327
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)
1328
osutils._fs_enc = 'ascii'
1329
self.assertDirReaderIs(
1330
UTF8DirReaderFeature.module.UTF8DirReader)
1226
1332
def test_force_walkdirs_utf8_fs_latin1(self):
1227
1333
self._save_platform_info()
1228
1334
win32utils.winver = None # Avoid the win32 detection code
1229
osutils._fs_enc = 'latin1'
1335
osutils._fs_enc = 'iso-8859-1'
1230
1336
self.assertDirReaderIs(osutils.UnicodeDirReader)
1232
1338
def test_force_walkdirs_utf8_nt(self):
1856
1962
return filtered_dirblocks
1858
1964
def test_walk_unicode_tree(self):
1859
self.requireFeature(tests.UnicodeFilenameFeature)
1965
self.requireFeature(features.UnicodeFilenameFeature)
1860
1966
tree, expected_dirblocks = self._get_unicode_tree()
1861
1967
self.build_tree(tree)
1862
1968
result = list(osutils._walkdirs_utf8('.'))
1863
1969
self.assertEqual(expected_dirblocks, self._filter_out(result))
1865
1971
def test_symlink(self):
1866
self.requireFeature(tests.SymlinkFeature)
1867
self.requireFeature(tests.UnicodeFilenameFeature)
1972
self.requireFeature(features.SymlinkFeature)
1973
self.requireFeature(features.UnicodeFilenameFeature)
1868
1974
target = u'target\N{Euro Sign}'
1869
1975
link_name = u'l\N{Euro Sign}nk'
1870
1976
os.symlink(target, link_name)
2090
2194
self.assertEquals(self.uid, s.st_uid)
2091
2195
self.assertEquals(self.gid, s.st_gid)
2198
class TestPathFromEnviron(tests.TestCase):
2200
def test_is_unicode(self):
2201
self.overrideEnv('BZR_TEST_PATH', './anywhere at all/')
2202
path = osutils.path_from_environ('BZR_TEST_PATH')
2203
self.assertIsInstance(path, unicode)
2204
self.assertEqual(u'./anywhere at all/', path)
2206
def test_posix_path_env_ascii(self):
2207
self.overrideEnv('BZR_TEST_PATH', '/tmp')
2208
home = osutils._posix_path_from_environ('BZR_TEST_PATH')
2209
self.assertIsInstance(home, unicode)
2210
self.assertEqual(u'/tmp', home)
2212
def test_posix_path_env_unicode(self):
2213
self.requireFeature(features.ByteStringNamedFilesystem)
2214
self.overrideEnv('BZR_TEST_PATH', '/home/\xa7test')
2215
self.overrideAttr(osutils, "_fs_enc", "iso8859-1")
2216
self.assertEqual(u'/home/\xa7test',
2217
osutils._posix_path_from_environ('BZR_TEST_PATH'))
2218
osutils._fs_enc = "iso8859-5"
2219
self.assertEqual(u'/home/\u0407test',
2220
osutils._posix_path_from_environ('BZR_TEST_PATH'))
2221
osutils._fs_enc = "utf-8"
2222
self.assertRaises(errors.BadFilenameEncoding,
2223
osutils._posix_path_from_environ, 'BZR_TEST_PATH')
2226
class TestGetHomeDir(tests.TestCase):
2228
def test_is_unicode(self):
2229
home = osutils._get_home_dir()
2230
self.assertIsInstance(home, unicode)
2232
def test_posix_homeless(self):
2233
self.overrideEnv('HOME', None)
2234
home = osutils._get_home_dir()
2235
self.assertIsInstance(home, unicode)
2237
def test_posix_home_ascii(self):
2238
self.overrideEnv('HOME', '/home/test')
2239
home = osutils._posix_get_home_dir()
2240
self.assertIsInstance(home, unicode)
2241
self.assertEqual(u'/home/test', home)
2243
def test_posix_home_unicode(self):
2244
self.requireFeature(features.ByteStringNamedFilesystem)
2245
self.overrideEnv('HOME', '/home/\xa7test')
2246
self.overrideAttr(osutils, "_fs_enc", "iso8859-1")
2247
self.assertEqual(u'/home/\xa7test', osutils._posix_get_home_dir())
2248
osutils._fs_enc = "iso8859-5"
2249
self.assertEqual(u'/home/\u0407test', osutils._posix_get_home_dir())
2250
osutils._fs_enc = "utf-8"
2251
self.assertRaises(errors.BadFilenameEncoding,
2252
osutils._posix_get_home_dir)
2093
2255
class TestGetuserUnicode(tests.TestCase):
2257
def test_is_unicode(self):
2258
user = osutils.getuser_unicode()
2259
self.assertIsInstance(user, unicode)
2261
def envvar_to_override(self):
2262
if sys.platform == "win32":
2263
# Disable use of platform calls on windows so envvar is used
2264
self.overrideAttr(win32utils, 'has_ctypes', False)
2265
return 'USERNAME' # only variable used on windows
2266
return 'LOGNAME' # first variable checked by getpass.getuser()
2095
2268
def test_ascii_user(self):
2096
self.overrideEnv('LOGNAME', 'jrandom')
2269
self.overrideEnv(self.envvar_to_override(), 'jrandom')
2097
2270
self.assertEqual(u'jrandom', osutils.getuser_unicode())
2099
2272
def test_unicode_user(self):
2160
2340
self.assertTrue(osutils.find_executable_on_path('sh') is not None)
2161
2341
self.assertTrue(
2162
2342
osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
2345
class TestEnvironmentErrors(tests.TestCase):
2346
"""Test handling of environmental errors"""
2348
def test_is_oserror(self):
2349
self.assertTrue(osutils.is_environment_error(
2350
OSError(errno.EINVAL, "Invalid parameter")))
2352
def test_is_ioerror(self):
2353
self.assertTrue(osutils.is_environment_error(
2354
IOError(errno.EINVAL, "Invalid parameter")))
2356
def test_is_socket_error(self):
2357
self.assertTrue(osutils.is_environment_error(
2358
socket.error(errno.EINVAL, "Invalid parameter")))
2360
def test_is_select_error(self):
2361
self.assertTrue(osutils.is_environment_error(
2362
select.error(errno.EINVAL, "Invalid parameter")))
2364
def test_is_pywintypes_error(self):
2365
self.requireFeature(features.pywintypes)
2367
self.assertTrue(osutils.is_environment_error(
2368
pywintypes.error(errno.EINVAL, "Invalid parameter", "Caller")))