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):
411
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)
414
483
class TestLinks(tests.TestCaseInTempDir):
416
485
def test_dereference_path(self):
417
self.requireFeature(tests.SymlinkFeature)
486
self.requireFeature(features.SymlinkFeature)
418
487
cwd = osutils.realpath('.')
420
489
bar_path = osutils.pathjoin(cwd, 'bar')
803
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'))
806
924
class TestWin32Funcs(tests.TestCase):
807
925
"""Test that _win32 versions of os utilities return appropriate paths."""
809
927
def test_abspath(self):
928
self.requireFeature(features.win32_feature)
810
929
self.assertEqual('C:/foo', osutils._win32_abspath('C:\\foo'))
811
930
self.assertEqual('C:/foo', osutils._win32_abspath('C:/foo'))
812
931
self.assertEqual('//HOST/path', osutils._win32_abspath(r'\\HOST\path'))
1990
2172
def _dummy_chown(self, path, uid, gid):
1991
2173
self.path, self.uid, self.gid = path, uid, gid
1993
def test_mkdir_with_ownership_chown(self):
1994
"""Ensure that osutils.mkdir_with_ownership chowns correctly with ownership_src.
1997
osutils.mkdir_with_ownership('foo', ownsrc)
2000
self.assertEquals(self.path, 'foo')
2001
self.assertEquals(self.uid, s.st_uid)
2002
self.assertEquals(self.gid, s.st_gid)
2004
def test_open_with_ownership_chown(self):
2005
"""Ensure that osutils.open_with_ownership chowns correctly with ownership_src.
2008
f = osutils.open_with_ownership('foo', 'w', ownership_src=ownsrc)
2010
# do a test write and close
2015
self.assertEquals(self.path, 'foo')
2016
self.assertEquals(self.uid, s.st_uid)
2017
self.assertEquals(self.gid, s.st_gid)
2175
def test_copy_ownership_from_path(self):
2176
"""copy_ownership_from_path test with specified src."""
2178
f = open('test_file', 'wt')
2179
osutils.copy_ownership_from_path('test_file', ownsrc)
2182
self.assertEqual(self.path, 'test_file')
2183
self.assertEqual(self.uid, s.st_uid)
2184
self.assertEqual(self.gid, s.st_gid)
2186
def test_copy_ownership_nonesrc(self):
2187
"""copy_ownership_from_path test with src=None."""
2188
f = open('test_file', 'wt')
2189
# should use parent dir for permissions
2190
osutils.copy_ownership_from_path('test_file')
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)
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()
2268
def test_ascii_user(self):
2269
self.overrideEnv(self.envvar_to_override(), 'jrandom')
2270
self.assertEqual(u'jrandom', osutils.getuser_unicode())
2272
def test_unicode_user(self):
2273
ue = osutils.get_user_encoding()
2274
uni_val, env_val = tests.probe_unicode_in_user_encoding()
2276
raise tests.TestSkipped(
2277
'Cannot find a unicode character that works in encoding %s'
2278
% (osutils.get_user_encoding(),))
2279
uni_username = u'jrandom' + uni_val
2280
encoded_username = uni_username.encode(ue)
2281
self.overrideEnv(self.envvar_to_override(), encoded_username)
2282
self.assertEqual(uni_username, osutils.getuser_unicode())
2285
class TestBackupNames(tests.TestCase):
2288
super(TestBackupNames, self).setUp()
2291
def backup_exists(self, name):
2292
return name in self.backups
2294
def available_backup_name(self, name):
2295
backup_name = osutils.available_backup_name(name, self.backup_exists)
2296
self.backups.append(backup_name)
2299
def assertBackupName(self, expected, name):
2300
self.assertEqual(expected, self.available_backup_name(name))
2302
def test_empty(self):
2303
self.assertBackupName('file.~1~', 'file')
2305
def test_existing(self):
2306
self.available_backup_name('file')
2307
self.available_backup_name('file')
2308
self.assertBackupName('file.~3~', 'file')
2309
# Empty slots are found, this is not a strict requirement and may be
2310
# revisited if we test against all implementations.
2311
self.backups.remove('file.~2~')
2312
self.assertBackupName('file.~2~', 'file')
2315
class TestFindExecutableInPath(tests.TestCase):
2317
def test_windows(self):
2318
if sys.platform != 'win32':
2319
raise tests.TestSkipped('test requires win32')
2320
self.assertTrue(osutils.find_executable_on_path('explorer') is not None)
2322
osutils.find_executable_on_path('explorer.exe') is not None)
2324
osutils.find_executable_on_path('EXPLORER.EXE') is not None)
2326
osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
2327
self.assertTrue(osutils.find_executable_on_path('file.txt') is None)
2329
def test_windows_app_path(self):
2330
if sys.platform != 'win32':
2331
raise tests.TestSkipped('test requires win32')
2332
# Override PATH env var so that exe can only be found on App Path
2333
self.overrideEnv('PATH', '')
2334
# Internt Explorer is always registered in the App Path
2335
self.assertTrue(osutils.find_executable_on_path('iexplore') is not None)
2337
def test_other(self):
2338
if sys.platform == 'win32':
2339
raise tests.TestSkipped('test requires non-win32')
2340
self.assertTrue(osutils.find_executable_on_path('sh') is not None)
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")))