178
180
# we can't use failUnlessExists on case-insensitive filesystem
179
181
# so try to check shape of the tree
180
182
shape = sorted(os.listdir('.'))
181
self.assertEquals(['A', 'B'], shape)
183
self.assertEqual(['A', 'B'], shape)
185
def test_rename_exception(self):
187
osutils.rename('nonexistent_path', 'different_nonexistent_path')
189
self.assertEqual(e.old_filename, 'nonexistent_path')
190
self.assertEqual(e.new_filename, 'different_nonexistent_path')
191
self.assertTrue('nonexistent_path' in e.strerror)
192
self.assertTrue('different_nonexistent_path' in e.strerror)
184
195
class TestRandChars(tests.TestCase):
288
299
def test_file_kind(self):
289
300
self.build_tree(['file', 'dir/'])
290
self.assertEquals('file', osutils.file_kind('file'))
291
self.assertEquals('directory', osutils.file_kind('dir/'))
301
self.assertEqual('file', osutils.file_kind('file'))
302
self.assertEqual('directory', osutils.file_kind('dir/'))
292
303
if osutils.has_symlinks():
293
304
os.symlink('symlink', 'symlink')
294
self.assertEquals('symlink', osutils.file_kind('symlink'))
305
self.assertEqual('symlink', osutils.file_kind('symlink'))
296
307
# TODO: jam 20060529 Test a block device
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)
821
914
class TestPosixFuncs(tests.TestCase):
822
915
"""Test that the posix version of normpath returns an appropriate path
823
916
when used with 2 leading slashes."""
1216
1317
self.requireFeature(UTF8DirReaderFeature)
1217
1318
self._save_platform_info()
1218
1319
win32utils.winver = None # Avoid the win32 detection code
1219
osutils._fs_enc = 'UTF-8'
1220
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1320
osutils._fs_enc = 'utf-8'
1321
self.assertDirReaderIs(
1322
UTF8DirReaderFeature.module.UTF8DirReader)
1222
1324
def test_force_walkdirs_utf8_fs_ascii(self):
1223
1325
self.requireFeature(UTF8DirReaderFeature)
1224
1326
self._save_platform_info()
1225
1327
win32utils.winver = None # Avoid the win32 detection code
1226
osutils._fs_enc = 'US-ASCII'
1227
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1229
def test_force_walkdirs_utf8_fs_ANSI(self):
1230
self.requireFeature(UTF8DirReaderFeature)
1231
self._save_platform_info()
1232
win32utils.winver = None # Avoid the win32 detection code
1233
osutils._fs_enc = 'ANSI_X3.4-1968'
1234
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1328
osutils._fs_enc = 'ascii'
1329
self.assertDirReaderIs(
1330
UTF8DirReaderFeature.module.UTF8DirReader)
1236
1332
def test_force_walkdirs_utf8_fs_latin1(self):
1237
1333
self._save_platform_info()
1238
1334
win32utils.winver = None # Avoid the win32 detection code
1239
osutils._fs_enc = 'latin1'
1335
osutils._fs_enc = 'iso-8859-1'
1240
1336
self.assertDirReaderIs(osutils.UnicodeDirReader)
1242
1338
def test_force_walkdirs_utf8_nt(self):
1866
1962
return filtered_dirblocks
1868
1964
def test_walk_unicode_tree(self):
1869
self.requireFeature(tests.UnicodeFilenameFeature)
1965
self.requireFeature(features.UnicodeFilenameFeature)
1870
1966
tree, expected_dirblocks = self._get_unicode_tree()
1871
1967
self.build_tree(tree)
1872
1968
result = list(osutils._walkdirs_utf8('.'))
1873
1969
self.assertEqual(expected_dirblocks, self._filter_out(result))
1875
1971
def test_symlink(self):
1876
self.requireFeature(tests.SymlinkFeature)
1877
self.requireFeature(tests.UnicodeFilenameFeature)
1972
self.requireFeature(features.SymlinkFeature)
1973
self.requireFeature(features.UnicodeFilenameFeature)
1878
1974
target = u'target\N{Euro Sign}'
1879
1975
link_name = u'l\N{Euro Sign}nk'
1880
1976
os.symlink(target, link_name)
2094
2190
osutils.copy_ownership_from_path('test_file')
2096
2192
s = os.stat('..')
2097
self.assertEquals(self.path, 'test_file')
2098
self.assertEquals(self.uid, s.st_uid)
2099
self.assertEquals(self.gid, s.st_gid)
2193
self.assertEqual(self.path, 'test_file')
2194
self.assertEqual(self.uid, s.st_uid)
2195
self.assertEqual(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)
2102
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()
2104
2268
def test_ascii_user(self):
2105
self.overrideEnv('LOGNAME', 'jrandom')
2269
self.overrideEnv(self.envvar_to_override(), 'jrandom')
2106
2270
self.assertEqual(u'jrandom', osutils.getuser_unicode())
2108
2272
def test_unicode_user(self):
2114
2278
% (osutils.get_user_encoding(),))
2115
2279
uni_username = u'jrandom' + uni_val
2116
2280
encoded_username = uni_username.encode(ue)
2117
self.overrideEnv('LOGNAME', encoded_username)
2281
self.overrideEnv(self.envvar_to_override(), encoded_username)
2118
2282
self.assertEqual(uni_username, osutils.getuser_unicode())
2119
self.overrideEnv('LOGNAME', u'jrandom\xb6'.encode(ue))
2120
self.assertEqual(u'jrandom\xb6', osutils.getuser_unicode())
2122
def test_no_username_bug_660174(self):
2123
self.requireFeature(features.win32_feature)
2124
for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
2125
self.overrideEnv(name, None)
2126
self.assertEqual(u'UNKNOWN', osutils.getuser_unicode())
2129
2285
class TestBackupNames(tests.TestCase):
2176
2340
self.assertTrue(osutils.find_executable_on_path('sh') is not None)
2177
2341
self.assertTrue(
2178
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")))