98
def load_tests(basic_tests, module, loader):
99
suite = loader.suiteClass()
100
dir_reader_tests, remaining_tests = tests.split_suite_by_condition(
101
basic_tests, tests.condition_isinstance(TestDirReader))
102
tests.multiply_tests(dir_reader_tests, dir_reader_scenarios(), suite)
103
suite.addTest(remaining_tests)
102
load_tests = load_tests_apply_scenarios
107
105
class TestContainsWhitespace(tests.TestCase):
109
107
def test_contains_whitespace(self):
110
self.failUnless(osutils.contains_whitespace(u' '))
111
self.failUnless(osutils.contains_whitespace(u'hello there'))
112
self.failUnless(osutils.contains_whitespace(u'hellothere\n'))
113
self.failUnless(osutils.contains_whitespace(u'hello\nthere'))
114
self.failUnless(osutils.contains_whitespace(u'hello\rthere'))
115
self.failUnless(osutils.contains_whitespace(u'hello\tthere'))
108
self.assertTrue(osutils.contains_whitespace(u' '))
109
self.assertTrue(osutils.contains_whitespace(u'hello there'))
110
self.assertTrue(osutils.contains_whitespace(u'hellothere\n'))
111
self.assertTrue(osutils.contains_whitespace(u'hello\nthere'))
112
self.assertTrue(osutils.contains_whitespace(u'hello\rthere'))
113
self.assertTrue(osutils.contains_whitespace(u'hello\tthere'))
117
115
# \xa0 is "Non-breaking-space" which on some python locales thinks it
118
116
# is whitespace, but we do not.
119
self.failIf(osutils.contains_whitespace(u''))
120
self.failIf(osutils.contains_whitespace(u'hellothere'))
121
self.failIf(osutils.contains_whitespace(u'hello\xa0there'))
117
self.assertFalse(osutils.contains_whitespace(u''))
118
self.assertFalse(osutils.contains_whitespace(u'hellothere'))
119
self.assertFalse(osutils.contains_whitespace(u'hello\xa0there'))
124
122
class TestRename(tests.TestCaseInTempDir):
418
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)
421
483
class TestLinks(tests.TestCaseInTempDir):
423
485
def test_dereference_path(self):
424
self.requireFeature(tests.SymlinkFeature)
486
self.requireFeature(features.SymlinkFeature)
425
487
cwd = osutils.realpath('.')
427
489
bar_path = osutils.pathjoin(cwd, 'bar')
810
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'))
813
924
class TestWin32Funcs(tests.TestCase):
814
925
"""Test that _win32 versions of os utilities return appropriate paths."""
1161
1315
self.requireFeature(UTF8DirReaderFeature)
1162
1316
self._save_platform_info()
1163
1317
win32utils.winver = None # Avoid the win32 detection code
1164
osutils._fs_enc = 'UTF-8'
1165
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1318
osutils._fs_enc = 'utf-8'
1319
self.assertDirReaderIs(
1320
UTF8DirReaderFeature.module.UTF8DirReader)
1167
1322
def test_force_walkdirs_utf8_fs_ascii(self):
1168
1323
self.requireFeature(UTF8DirReaderFeature)
1169
1324
self._save_platform_info()
1170
1325
win32utils.winver = None # Avoid the win32 detection code
1171
osutils._fs_enc = 'US-ASCII'
1172
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1174
def test_force_walkdirs_utf8_fs_ANSI(self):
1175
self.requireFeature(UTF8DirReaderFeature)
1176
self._save_platform_info()
1177
win32utils.winver = None # Avoid the win32 detection code
1178
osutils._fs_enc = 'ANSI_X3.4-1968'
1179
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1326
osutils._fs_enc = 'ascii'
1327
self.assertDirReaderIs(
1328
UTF8DirReaderFeature.module.UTF8DirReader)
1181
1330
def test_force_walkdirs_utf8_fs_latin1(self):
1182
1331
self._save_platform_info()
1183
1332
win32utils.winver = None # Avoid the win32 detection code
1184
osutils._fs_enc = 'latin1'
1333
osutils._fs_enc = 'iso-8859-1'
1185
1334
self.assertDirReaderIs(osutils.UnicodeDirReader)
1187
1336
def test_force_walkdirs_utf8_nt(self):
1679
1828
class TestReCompile(tests.TestCase):
1830
def _deprecated_re_compile_checked(self, *args, **kwargs):
1831
return self.applyDeprecated(symbol_versioning.deprecated_in((2, 2, 0)),
1832
osutils.re_compile_checked, *args, **kwargs)
1681
1834
def test_re_compile_checked(self):
1682
r = osutils.re_compile_checked(r'A*', re.IGNORECASE)
1835
r = self._deprecated_re_compile_checked(r'A*', re.IGNORECASE)
1683
1836
self.assertTrue(r.match('aaaa'))
1684
1837
self.assertTrue(r.match('aAaA'))
1686
1839
def test_re_compile_checked_error(self):
1687
1840
# like https://bugs.launchpad.net/bzr/+bug/251352
1842
# Due to possible test isolation error, re.compile is not lazy at
1843
# this point. We re-install lazy compile.
1844
lazy_regex.install_lazy_compile()
1688
1845
err = self.assertRaises(
1689
1846
errors.BzrCommandError,
1690
osutils.re_compile_checked, '*', re.IGNORECASE, 'test case')
1847
self._deprecated_re_compile_checked, '*', re.IGNORECASE, 'test case')
1691
1848
self.assertEqual(
1692
"Invalid regular expression in test case: '*': "
1693
"nothing to repeat",
1849
'Invalid regular expression in test case: '
1850
'"*" nothing to repeat',
1697
1854
class TestDirReader(tests.TestCaseInTempDir):
1856
scenarios = dir_reader_scenarios()
1699
1858
# Set by load_tests
1700
1859
_dir_reader_class = None
1701
1860
_native_to_unicode = None
1703
1862
def setUp(self):
1704
tests.TestCaseInTempDir.setUp(self)
1863
super(TestDirReader, self).setUp()
1705
1864
self.overrideAttr(osutils,
1706
1865
'_selected_dir_reader', self._dir_reader_class())
2018
2191
self.assertEquals(self.path, 'test_file')
2019
2192
self.assertEquals(self.uid, s.st_uid)
2020
2193
self.assertEquals(self.gid, s.st_gid)
2196
class TestPathFromEnviron(tests.TestCase):
2198
def test_is_unicode(self):
2199
self.overrideEnv('BZR_TEST_PATH', './anywhere at all/')
2200
path = osutils.path_from_environ('BZR_TEST_PATH')
2201
self.assertIsInstance(path, unicode)
2202
self.assertEqual(u'./anywhere at all/', path)
2204
def test_posix_path_env_ascii(self):
2205
self.overrideEnv('BZR_TEST_PATH', '/tmp')
2206
home = osutils._posix_path_from_environ('BZR_TEST_PATH')
2207
self.assertIsInstance(home, unicode)
2208
self.assertEqual(u'/tmp', home)
2210
def test_posix_path_env_unicode(self):
2211
self.requireFeature(features.ByteStringNamedFilesystem)
2212
self.overrideEnv('BZR_TEST_PATH', '/home/\xa7test')
2213
self.overrideAttr(osutils, "_fs_enc", "iso8859-1")
2214
self.assertEqual(u'/home/\xa7test',
2215
osutils._posix_path_from_environ('BZR_TEST_PATH'))
2216
osutils._fs_enc = "iso8859-5"
2217
self.assertEqual(u'/home/\u0407test',
2218
osutils._posix_path_from_environ('BZR_TEST_PATH'))
2219
osutils._fs_enc = "utf-8"
2220
self.assertRaises(errors.BadFilenameEncoding,
2221
osutils._posix_path_from_environ, 'BZR_TEST_PATH')
2224
class TestGetHomeDir(tests.TestCase):
2226
def test_is_unicode(self):
2227
home = osutils._get_home_dir()
2228
self.assertIsInstance(home, unicode)
2230
def test_posix_homeless(self):
2231
self.overrideEnv('HOME', None)
2232
home = osutils._get_home_dir()
2233
self.assertIsInstance(home, unicode)
2235
def test_posix_home_ascii(self):
2236
self.overrideEnv('HOME', '/home/test')
2237
home = osutils._posix_get_home_dir()
2238
self.assertIsInstance(home, unicode)
2239
self.assertEqual(u'/home/test', home)
2241
def test_posix_home_unicode(self):
2242
self.requireFeature(features.ByteStringNamedFilesystem)
2243
self.overrideEnv('HOME', '/home/\xa7test')
2244
self.overrideAttr(osutils, "_fs_enc", "iso8859-1")
2245
self.assertEqual(u'/home/\xa7test', osutils._posix_get_home_dir())
2246
osutils._fs_enc = "iso8859-5"
2247
self.assertEqual(u'/home/\u0407test', osutils._posix_get_home_dir())
2248
osutils._fs_enc = "utf-8"
2249
self.assertRaises(errors.BadFilenameEncoding,
2250
osutils._posix_get_home_dir)
2253
class TestGetuserUnicode(tests.TestCase):
2255
def test_is_unicode(self):
2256
user = osutils.getuser_unicode()
2257
self.assertIsInstance(user, unicode)
2259
def envvar_to_override(self):
2260
if sys.platform == "win32":
2261
# Disable use of platform calls on windows so envvar is used
2262
self.overrideAttr(win32utils, 'has_ctypes', False)
2263
return 'USERNAME' # only variable used on windows
2264
return 'LOGNAME' # first variable checked by getpass.getuser()
2266
def test_ascii_user(self):
2267
self.overrideEnv(self.envvar_to_override(), 'jrandom')
2268
self.assertEqual(u'jrandom', osutils.getuser_unicode())
2270
def test_unicode_user(self):
2271
ue = osutils.get_user_encoding()
2272
uni_val, env_val = tests.probe_unicode_in_user_encoding()
2274
raise tests.TestSkipped(
2275
'Cannot find a unicode character that works in encoding %s'
2276
% (osutils.get_user_encoding(),))
2277
uni_username = u'jrandom' + uni_val
2278
encoded_username = uni_username.encode(ue)
2279
self.overrideEnv(self.envvar_to_override(), encoded_username)
2280
self.assertEqual(uni_username, osutils.getuser_unicode())
2283
class TestBackupNames(tests.TestCase):
2286
super(TestBackupNames, self).setUp()
2289
def backup_exists(self, name):
2290
return name in self.backups
2292
def available_backup_name(self, name):
2293
backup_name = osutils.available_backup_name(name, self.backup_exists)
2294
self.backups.append(backup_name)
2297
def assertBackupName(self, expected, name):
2298
self.assertEqual(expected, self.available_backup_name(name))
2300
def test_empty(self):
2301
self.assertBackupName('file.~1~', 'file')
2303
def test_existing(self):
2304
self.available_backup_name('file')
2305
self.available_backup_name('file')
2306
self.assertBackupName('file.~3~', 'file')
2307
# Empty slots are found, this is not a strict requirement and may be
2308
# revisited if we test against all implementations.
2309
self.backups.remove('file.~2~')
2310
self.assertBackupName('file.~2~', 'file')
2313
class TestFindExecutableInPath(tests.TestCase):
2315
def test_windows(self):
2316
if sys.platform != 'win32':
2317
raise tests.TestSkipped('test requires win32')
2318
self.assertTrue(osutils.find_executable_on_path('explorer') is not None)
2320
osutils.find_executable_on_path('explorer.exe') is not None)
2322
osutils.find_executable_on_path('EXPLORER.EXE') is not None)
2324
osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
2325
self.assertTrue(osutils.find_executable_on_path('file.txt') is None)
2327
def test_windows_app_path(self):
2328
if sys.platform != 'win32':
2329
raise tests.TestSkipped('test requires win32')
2330
# Override PATH env var so that exe can only be found on App Path
2331
self.overrideEnv('PATH', '')
2332
# Internt Explorer is always registered in the App Path
2333
self.assertTrue(osutils.find_executable_on_path('iexplore') is not None)
2335
def test_other(self):
2336
if sys.platform == 'win32':
2337
raise tests.TestSkipped('test requires non-win32')
2338
self.assertTrue(osutils.find_executable_on_path('sh') is not None)
2340
osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
2343
class TestEnvironmentErrors(tests.TestCase):
2344
"""Test handling of environmental errors"""
2346
def test_is_oserror(self):
2347
self.assertTrue(osutils.is_environment_error(
2348
OSError(errno.EINVAL, "Invalid parameter")))
2350
def test_is_ioerror(self):
2351
self.assertTrue(osutils.is_environment_error(
2352
IOError(errno.EINVAL, "Invalid parameter")))
2354
def test_is_socket_error(self):
2355
self.assertTrue(osutils.is_environment_error(
2356
socket.error(errno.EINVAL, "Invalid parameter")))
2358
def test_is_select_error(self):
2359
self.assertTrue(osutils.is_environment_error(
2360
select.error(errno.EINVAL, "Invalid parameter")))
2362
def test_is_pywintypes_error(self):
2363
self.requireFeature(features.pywintypes)
2365
self.assertTrue(osutils.is_environment_error(
2366
pywintypes.error(errno.EINVAL, "Invalid parameter", "Caller")))