99
def load_tests(basic_tests, module, loader):
100
suite = loader.suiteClass()
101
dir_reader_tests, remaining_tests = tests.split_suite_by_condition(
102
basic_tests, tests.condition_isinstance(TestDirReader))
103
tests.multiply_tests(dir_reader_tests, dir_reader_scenarios(), suite)
104
suite.addTest(remaining_tests)
102
load_tests = load_tests_apply_scenarios
108
105
class TestContainsWhitespace(tests.TestCase):
110
107
def test_contains_whitespace(self):
111
self.failUnless(osutils.contains_whitespace(u' '))
112
self.failUnless(osutils.contains_whitespace(u'hello there'))
113
self.failUnless(osutils.contains_whitespace(u'hellothere\n'))
114
self.failUnless(osutils.contains_whitespace(u'hello\nthere'))
115
self.failUnless(osutils.contains_whitespace(u'hello\rthere'))
116
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'))
118
115
# \xa0 is "Non-breaking-space" which on some python locales thinks it
119
116
# is whitespace, but we do not.
120
self.failIf(osutils.contains_whitespace(u''))
121
self.failIf(osutils.contains_whitespace(u'hellothere'))
122
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'))
125
122
class TestRename(tests.TestCaseInTempDir):
236
242
self.assertFalse(osutils.is_inside_or_parent_of_any(dirs, fn))
245
class TestLstat(tests.TestCaseInTempDir):
247
def test_lstat_matches_fstat(self):
248
# On Windows, lstat and fstat don't always agree, primarily in the
249
# 'st_ino' and 'st_dev' fields. So we force them to be '0' in our
250
# custom implementation.
251
if sys.platform == 'win32':
252
# We only have special lstat/fstat if we have the extension.
253
# Without it, we may end up re-reading content when we don't have
254
# to, but otherwise it doesn't effect correctness.
255
self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
256
f = open('test-file.txt', 'wb')
257
self.addCleanup(f.close)
258
f.write('some content\n')
260
self.assertEqualStat(osutils.fstat(f.fileno()),
261
osutils.lstat('test-file.txt'))
239
264
class TestRmTree(tests.TestCaseInTempDir):
241
266
def test_rmtree(self):
412
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)
415
483
class TestLinks(tests.TestCaseInTempDir):
417
485
def test_dereference_path(self):
418
self.requireFeature(tests.SymlinkFeature)
486
self.requireFeature(features.SymlinkFeature)
419
487
cwd = osutils.realpath('.')
421
489
bar_path = osutils.pathjoin(cwd, 'bar')
469
537
class TestCanonicalRelPath(tests.TestCaseInTempDir):
471
_test_needs_features = [tests.CaseInsCasePresFilenameFeature]
539
_test_needs_features = [features.CaseInsCasePresFilenameFeature]
473
541
def test_canonical_relpath_simple(self):
474
542
f = file('MixedCaseName', 'w')
476
544
actual = osutils.canonical_relpath(self.test_base_dir, 'mixedcasename')
477
self.failUnlessEqual('work/MixedCaseName', actual)
545
self.assertEqual('work/MixedCaseName', actual)
479
547
def test_canonical_relpath_missing_tail(self):
480
548
os.mkdir('MixedCaseParent')
481
549
actual = osutils.canonical_relpath(self.test_base_dir,
482
550
'mixedcaseparent/nochild')
483
self.failUnlessEqual('work/MixedCaseParent/nochild', actual)
551
self.assertEqual('work/MixedCaseParent/nochild', actual)
486
554
class Test_CICPCanonicalRelpath(tests.TestCaseWithTransport):
804
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'))
807
924
class TestWin32Funcs(tests.TestCase):
808
925
"""Test that _win32 versions of os utilities return appropriate paths."""
826
943
osutils._win32_pathjoin('path/to', 'C:/foo'))
827
944
self.assertEqual('path/to/foo',
828
945
osutils._win32_pathjoin('path/to/', 'foo'))
829
self.assertEqual('/foo',
947
def test_pathjoin_late_bugfix(self):
948
if sys.version_info < (2, 7, 6):
952
self.assertEqual(expected,
830
953
osutils._win32_pathjoin('C:/path/to/', '/foo'))
831
self.assertEqual('/foo',
954
self.assertEqual(expected,
832
955
osutils._win32_pathjoin('C:\\path\\to\\', '\\foo'))
834
957
def test_normpath(self):
1190
1315
self.requireFeature(UTF8DirReaderFeature)
1191
1316
self._save_platform_info()
1192
1317
win32utils.winver = None # Avoid the win32 detection code
1193
osutils._fs_enc = 'UTF-8'
1194
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1318
osutils._fs_enc = 'utf-8'
1319
self.assertDirReaderIs(
1320
UTF8DirReaderFeature.module.UTF8DirReader)
1196
1322
def test_force_walkdirs_utf8_fs_ascii(self):
1197
1323
self.requireFeature(UTF8DirReaderFeature)
1198
1324
self._save_platform_info()
1199
1325
win32utils.winver = None # Avoid the win32 detection code
1200
osutils._fs_enc = 'US-ASCII'
1201
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1203
def test_force_walkdirs_utf8_fs_ANSI(self):
1204
self.requireFeature(UTF8DirReaderFeature)
1205
self._save_platform_info()
1206
win32utils.winver = None # Avoid the win32 detection code
1207
osutils._fs_enc = 'ANSI_X3.4-1968'
1208
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1326
osutils._fs_enc = 'ascii'
1327
self.assertDirReaderIs(
1328
UTF8DirReaderFeature.module.UTF8DirReader)
1210
1330
def test_force_walkdirs_utf8_fs_latin1(self):
1211
1331
self._save_platform_info()
1212
1332
win32utils.winver = None # Avoid the win32 detection code
1213
osutils._fs_enc = 'latin1'
1333
osutils._fs_enc = 'iso-8859-1'
1214
1334
self.assertDirReaderIs(osutils.UnicodeDirReader)
1216
1336
def test_force_walkdirs_utf8_nt(self):
1838
1960
return filtered_dirblocks
1840
1962
def test_walk_unicode_tree(self):
1841
self.requireFeature(tests.UnicodeFilenameFeature)
1963
self.requireFeature(features.UnicodeFilenameFeature)
1842
1964
tree, expected_dirblocks = self._get_unicode_tree()
1843
1965
self.build_tree(tree)
1844
1966
result = list(osutils._walkdirs_utf8('.'))
1845
1967
self.assertEqual(expected_dirblocks, self._filter_out(result))
1847
1969
def test_symlink(self):
1848
self.requireFeature(tests.SymlinkFeature)
1849
self.requireFeature(tests.UnicodeFilenameFeature)
1970
self.requireFeature(features.SymlinkFeature)
1971
self.requireFeature(features.UnicodeFilenameFeature)
1850
1972
target = u'target\N{Euro Sign}'
1851
1973
link_name = u'l\N{Euro Sign}nk'
1852
1974
os.symlink(target, link_name)
1900
2019
self.assertIsInstance(concurrency, int)
1902
2021
def test_local_concurrency_environment_variable(self):
1903
os.environ['BZR_CONCURRENCY'] = '2'
2022
self.overrideEnv('BZR_CONCURRENCY', '2')
1904
2023
self.assertEqual(2, osutils.local_concurrency(use_cache=False))
1905
os.environ['BZR_CONCURRENCY'] = '3'
2024
self.overrideEnv('BZR_CONCURRENCY', '3')
1906
2025
self.assertEqual(3, osutils.local_concurrency(use_cache=False))
1907
os.environ['BZR_CONCURRENCY'] = 'foo'
2026
self.overrideEnv('BZR_CONCURRENCY', 'foo')
1908
2027
self.assertEqual(1, osutils.local_concurrency(use_cache=False))
1910
2029
def test_option_concurrency(self):
1911
os.environ['BZR_CONCURRENCY'] = '1'
2030
self.overrideEnv('BZR_CONCURRENCY', '1')
1912
2031
self.run_bzr('rocks --concurrency 42')
1913
# Command line overrides envrionment variable
2032
# Command line overrides environment variable
1914
2033
self.assertEquals('42', os.environ['BZR_CONCURRENCY'])
1915
2034
self.assertEquals(42, osutils.local_concurrency(use_cache=False))
1986
2105
def test_defaults_to_BZR_COLUMNS(self):
1987
2106
# BZR_COLUMNS is set by the test framework
1988
2107
self.assertNotEqual('12', os.environ['BZR_COLUMNS'])
1989
os.environ['BZR_COLUMNS'] = '12'
2108
self.overrideEnv('BZR_COLUMNS', '12')
1990
2109
self.assertEqual(12, osutils.terminal_width())
2111
def test_BZR_COLUMNS_0_no_limit(self):
2112
self.overrideEnv('BZR_COLUMNS', '0')
2113
self.assertEqual(None, osutils.terminal_width())
1992
2115
def test_falls_back_to_COLUMNS(self):
1993
del os.environ['BZR_COLUMNS']
2116
self.overrideEnv('BZR_COLUMNS', None)
1994
2117
self.assertNotEqual('42', os.environ['COLUMNS'])
1995
2118
self.set_fake_tty()
1996
os.environ['COLUMNS'] = '42'
2119
self.overrideEnv('COLUMNS', '42')
1997
2120
self.assertEqual(42, osutils.terminal_width())
1999
2122
def test_tty_default_without_columns(self):
2000
del os.environ['BZR_COLUMNS']
2001
del os.environ['COLUMNS']
2123
self.overrideEnv('BZR_COLUMNS', None)
2124
self.overrideEnv('COLUMNS', None)
2003
2126
def terminal_size(w, h):
2029
2152
self.overrideAttr(termios, 'TIOCGWINSZ')
2030
2153
del termios.TIOCGWINSZ
2031
del os.environ['BZR_COLUMNS']
2032
del os.environ['COLUMNS']
2154
self.overrideEnv('BZR_COLUMNS', None)
2155
self.overrideEnv('COLUMNS', None)
2033
2156
# Whatever the result is, if we don't raise an exception, it's ok.
2034
2157
osutils.terminal_width()
2036
2160
class TestCreationOps(tests.TestCaseInTempDir):
2037
2161
_test_needs_features = [features.chown_feature]
2039
2163
def setUp(self):
2040
tests.TestCaseInTempDir.setUp(self)
2164
super(TestCreationOps, self).setUp()
2041
2165
self.overrideAttr(os, 'chown', self._dummy_chown)
2043
2167
# params set by call to _dummy_chown
2068
2192
self.assertEquals(self.uid, s.st_uid)
2069
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)
2071
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()
2073
2266
def test_ascii_user(self):
2074
osutils.set_or_unset_env('LOGNAME', 'jrandom')
2267
self.overrideEnv(self.envvar_to_override(), 'jrandom')
2075
2268
self.assertEqual(u'jrandom', osutils.getuser_unicode())
2077
2270
def test_unicode_user(self):
2083
2276
% (osutils.get_user_encoding(),))
2084
2277
uni_username = u'jrandom' + uni_val
2085
2278
encoded_username = uni_username.encode(ue)
2086
osutils.set_or_unset_env('LOGNAME', encoded_username)
2279
self.overrideEnv(self.envvar_to_override(), encoded_username)
2087
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")))