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)
101
load_tests = load_tests_apply_scenarios
108
104
class TestContainsWhitespace(tests.TestCase):
110
106
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'))
107
self.assertTrue(osutils.contains_whitespace(u' '))
108
self.assertTrue(osutils.contains_whitespace(u'hello there'))
109
self.assertTrue(osutils.contains_whitespace(u'hellothere\n'))
110
self.assertTrue(osutils.contains_whitespace(u'hello\nthere'))
111
self.assertTrue(osutils.contains_whitespace(u'hello\rthere'))
112
self.assertTrue(osutils.contains_whitespace(u'hello\tthere'))
118
114
# \xa0 is "Non-breaking-space" which on some python locales thinks it
119
115
# 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'))
116
self.assertFalse(osutils.contains_whitespace(u''))
117
self.assertFalse(osutils.contains_whitespace(u'hellothere'))
118
self.assertFalse(osutils.contains_whitespace(u'hello\xa0there'))
125
121
class TestRename(tests.TestCaseInTempDir):
236
241
self.assertFalse(osutils.is_inside_or_parent_of_any(dirs, fn))
244
class TestLstat(tests.TestCaseInTempDir):
246
def test_lstat_matches_fstat(self):
247
# On Windows, lstat and fstat don't always agree, primarily in the
248
# 'st_ino' and 'st_dev' fields. So we force them to be '0' in our
249
# custom implementation.
250
if sys.platform == 'win32':
251
# We only have special lstat/fstat if we have the extension.
252
# Without it, we may end up re-reading content when we don't have
253
# to, but otherwise it doesn't effect correctness.
254
self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
255
f = open('test-file.txt', 'wb')
256
self.addCleanup(f.close)
257
f.write('some content\n')
259
self.assertEqualStat(osutils.fstat(f.fileno()),
260
osutils.lstat('test-file.txt'))
239
263
class TestRmTree(tests.TestCaseInTempDir):
241
265
def test_rmtree(self):
469
493
class TestCanonicalRelPath(tests.TestCaseInTempDir):
471
_test_needs_features = [tests.CaseInsCasePresFilenameFeature]
495
_test_needs_features = [features.CaseInsCasePresFilenameFeature]
473
497
def test_canonical_relpath_simple(self):
474
498
f = file('MixedCaseName', 'w')
476
500
actual = osutils.canonical_relpath(self.test_base_dir, 'mixedcasename')
477
self.failUnlessEqual('work/MixedCaseName', actual)
501
self.assertEqual('work/MixedCaseName', actual)
479
503
def test_canonical_relpath_missing_tail(self):
480
504
os.mkdir('MixedCaseParent')
481
505
actual = osutils.canonical_relpath(self.test_base_dir,
482
506
'mixedcaseparent/nochild')
483
self.failUnlessEqual('work/MixedCaseParent/nochild', actual)
507
self.assertEqual('work/MixedCaseParent/nochild', actual)
486
510
class Test_CICPCanonicalRelpath(tests.TestCaseWithTransport):
804
828
self.assertEqual(None, osutils.safe_file_id(None))
831
class TestSendAll(tests.TestCase):
833
def test_send_with_disconnected_socket(self):
834
class DisconnectedSocket(object):
835
def __init__(self, err):
837
def send(self, content):
841
# All of these should be treated as ConnectionReset
843
for err_cls in (IOError, socket.error):
844
for errnum in osutils._end_of_stream_errors:
845
errs.append(err_cls(errnum))
847
sock = DisconnectedSocket(err)
848
self.assertRaises(errors.ConnectionReset,
849
osutils.send_all, sock, 'some more content')
851
def test_send_with_no_progress(self):
852
# See https://bugs.launchpad.net/bzr/+bug/1047309
853
# It seems that paramiko can get into a state where it doesn't error,
854
# but it returns 0 bytes sent for requests over and over again.
855
class NoSendingSocket(object):
858
def send(self, bytes):
860
if self.call_count > 100:
861
# Prevent the test suite from hanging
862
raise RuntimeError('too many calls')
864
sock = NoSendingSocket()
865
self.assertRaises(errors.ConnectionReset,
866
osutils.send_all, sock, 'content')
867
self.assertEqual(1, sock.call_count)
870
class TestPosixFuncs(tests.TestCase):
871
"""Test that the posix version of normpath returns an appropriate path
872
when used with 2 leading slashes."""
874
def test_normpath(self):
875
self.assertEqual('/etc/shadow', osutils._posix_normpath('/etc/shadow'))
876
self.assertEqual('/etc/shadow', osutils._posix_normpath('//etc/shadow'))
877
self.assertEqual('/etc/shadow', osutils._posix_normpath('///etc/shadow'))
807
880
class TestWin32Funcs(tests.TestCase):
808
881
"""Test that _win32 versions of os utilities return appropriate paths."""
1190
1265
self.requireFeature(UTF8DirReaderFeature)
1191
1266
self._save_platform_info()
1192
1267
win32utils.winver = None # Avoid the win32 detection code
1193
osutils._fs_enc = 'UTF-8'
1194
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1268
osutils._fs_enc = 'utf-8'
1269
self.assertDirReaderIs(
1270
UTF8DirReaderFeature.module.UTF8DirReader)
1196
1272
def test_force_walkdirs_utf8_fs_ascii(self):
1197
1273
self.requireFeature(UTF8DirReaderFeature)
1198
1274
self._save_platform_info()
1199
1275
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)
1276
osutils._fs_enc = 'ascii'
1277
self.assertDirReaderIs(
1278
UTF8DirReaderFeature.module.UTF8DirReader)
1210
1280
def test_force_walkdirs_utf8_fs_latin1(self):
1211
1281
self._save_platform_info()
1212
1282
win32utils.winver = None # Avoid the win32 detection code
1213
osutils._fs_enc = 'latin1'
1283
osutils._fs_enc = 'iso-8859-1'
1214
1284
self.assertDirReaderIs(osutils.UnicodeDirReader)
1216
1286
def test_force_walkdirs_utf8_nt(self):
1838
1910
return filtered_dirblocks
1840
1912
def test_walk_unicode_tree(self):
1841
self.requireFeature(tests.UnicodeFilenameFeature)
1913
self.requireFeature(features.UnicodeFilenameFeature)
1842
1914
tree, expected_dirblocks = self._get_unicode_tree()
1843
1915
self.build_tree(tree)
1844
1916
result = list(osutils._walkdirs_utf8('.'))
1845
1917
self.assertEqual(expected_dirblocks, self._filter_out(result))
1847
1919
def test_symlink(self):
1848
self.requireFeature(tests.SymlinkFeature)
1849
self.requireFeature(tests.UnicodeFilenameFeature)
1920
self.requireFeature(features.SymlinkFeature)
1921
self.requireFeature(features.UnicodeFilenameFeature)
1850
1922
target = u'target\N{Euro Sign}'
1851
1923
link_name = u'l\N{Euro Sign}nk'
1852
1924
os.symlink(target, link_name)
1900
1969
self.assertIsInstance(concurrency, int)
1902
1971
def test_local_concurrency_environment_variable(self):
1903
os.environ['BZR_CONCURRENCY'] = '2'
1972
self.overrideEnv('BZR_CONCURRENCY', '2')
1904
1973
self.assertEqual(2, osutils.local_concurrency(use_cache=False))
1905
os.environ['BZR_CONCURRENCY'] = '3'
1974
self.overrideEnv('BZR_CONCURRENCY', '3')
1906
1975
self.assertEqual(3, osutils.local_concurrency(use_cache=False))
1907
os.environ['BZR_CONCURRENCY'] = 'foo'
1976
self.overrideEnv('BZR_CONCURRENCY', 'foo')
1908
1977
self.assertEqual(1, osutils.local_concurrency(use_cache=False))
1910
1979
def test_option_concurrency(self):
1911
os.environ['BZR_CONCURRENCY'] = '1'
1980
self.overrideEnv('BZR_CONCURRENCY', '1')
1912
1981
self.run_bzr('rocks --concurrency 42')
1913
# Command line overrides envrionment variable
1982
# Command line overrides environment variable
1914
1983
self.assertEquals('42', os.environ['BZR_CONCURRENCY'])
1915
1984
self.assertEquals(42, osutils.local_concurrency(use_cache=False))
1986
2055
def test_defaults_to_BZR_COLUMNS(self):
1987
2056
# BZR_COLUMNS is set by the test framework
1988
2057
self.assertNotEqual('12', os.environ['BZR_COLUMNS'])
1989
os.environ['BZR_COLUMNS'] = '12'
2058
self.overrideEnv('BZR_COLUMNS', '12')
1990
2059
self.assertEqual(12, osutils.terminal_width())
2061
def test_BZR_COLUMNS_0_no_limit(self):
2062
self.overrideEnv('BZR_COLUMNS', '0')
2063
self.assertEqual(None, osutils.terminal_width())
1992
2065
def test_falls_back_to_COLUMNS(self):
1993
del os.environ['BZR_COLUMNS']
2066
self.overrideEnv('BZR_COLUMNS', None)
1994
2067
self.assertNotEqual('42', os.environ['COLUMNS'])
1995
2068
self.set_fake_tty()
1996
os.environ['COLUMNS'] = '42'
2069
self.overrideEnv('COLUMNS', '42')
1997
2070
self.assertEqual(42, osutils.terminal_width())
1999
2072
def test_tty_default_without_columns(self):
2000
del os.environ['BZR_COLUMNS']
2001
del os.environ['COLUMNS']
2073
self.overrideEnv('BZR_COLUMNS', None)
2074
self.overrideEnv('COLUMNS', None)
2003
2076
def terminal_size(w, h):
2029
2102
self.overrideAttr(termios, 'TIOCGWINSZ')
2030
2103
del termios.TIOCGWINSZ
2031
del os.environ['BZR_COLUMNS']
2032
del os.environ['COLUMNS']
2104
self.overrideEnv('BZR_COLUMNS', None)
2105
self.overrideEnv('COLUMNS', None)
2033
2106
# Whatever the result is, if we don't raise an exception, it's ok.
2034
2107
osutils.terminal_width()
2036
2110
class TestCreationOps(tests.TestCaseInTempDir):
2037
2111
_test_needs_features = [features.chown_feature]
2039
2113
def setUp(self):
2040
tests.TestCaseInTempDir.setUp(self)
2114
super(TestCreationOps, self).setUp()
2041
2115
self.overrideAttr(os, 'chown', self._dummy_chown)
2043
2117
# params set by call to _dummy_chown
2068
2142
self.assertEquals(self.uid, s.st_uid)
2069
2143
self.assertEquals(self.gid, s.st_gid)
2146
class TestPathFromEnviron(tests.TestCase):
2148
def test_is_unicode(self):
2149
self.overrideEnv('BZR_TEST_PATH', './anywhere at all/')
2150
path = osutils.path_from_environ('BZR_TEST_PATH')
2151
self.assertIsInstance(path, unicode)
2152
self.assertEqual(u'./anywhere at all/', path)
2154
def test_posix_path_env_ascii(self):
2155
self.overrideEnv('BZR_TEST_PATH', '/tmp')
2156
home = osutils._posix_path_from_environ('BZR_TEST_PATH')
2157
self.assertIsInstance(home, unicode)
2158
self.assertEqual(u'/tmp', home)
2160
def test_posix_path_env_unicode(self):
2161
self.requireFeature(features.ByteStringNamedFilesystem)
2162
self.overrideEnv('BZR_TEST_PATH', '/home/\xa7test')
2163
self.overrideAttr(osutils, "_fs_enc", "iso8859-1")
2164
self.assertEqual(u'/home/\xa7test',
2165
osutils._posix_path_from_environ('BZR_TEST_PATH'))
2166
osutils._fs_enc = "iso8859-5"
2167
self.assertEqual(u'/home/\u0407test',
2168
osutils._posix_path_from_environ('BZR_TEST_PATH'))
2169
osutils._fs_enc = "utf-8"
2170
self.assertRaises(errors.BadFilenameEncoding,
2171
osutils._posix_path_from_environ, 'BZR_TEST_PATH')
2174
class TestGetHomeDir(tests.TestCase):
2176
def test_is_unicode(self):
2177
home = osutils._get_home_dir()
2178
self.assertIsInstance(home, unicode)
2180
def test_posix_homeless(self):
2181
self.overrideEnv('HOME', None)
2182
home = osutils._get_home_dir()
2183
self.assertIsInstance(home, unicode)
2185
def test_posix_home_ascii(self):
2186
self.overrideEnv('HOME', '/home/test')
2187
home = osutils._posix_get_home_dir()
2188
self.assertIsInstance(home, unicode)
2189
self.assertEqual(u'/home/test', home)
2191
def test_posix_home_unicode(self):
2192
self.requireFeature(features.ByteStringNamedFilesystem)
2193
self.overrideEnv('HOME', '/home/\xa7test')
2194
self.overrideAttr(osutils, "_fs_enc", "iso8859-1")
2195
self.assertEqual(u'/home/\xa7test', osutils._posix_get_home_dir())
2196
osutils._fs_enc = "iso8859-5"
2197
self.assertEqual(u'/home/\u0407test', osutils._posix_get_home_dir())
2198
osutils._fs_enc = "utf-8"
2199
self.assertRaises(errors.BadFilenameEncoding,
2200
osutils._posix_get_home_dir)
2071
2203
class TestGetuserUnicode(tests.TestCase):
2205
def test_is_unicode(self):
2206
user = osutils.getuser_unicode()
2207
self.assertIsInstance(user, unicode)
2209
def envvar_to_override(self):
2210
if sys.platform == "win32":
2211
# Disable use of platform calls on windows so envvar is used
2212
self.overrideAttr(win32utils, 'has_ctypes', False)
2213
return 'USERNAME' # only variable used on windows
2214
return 'LOGNAME' # first variable checked by getpass.getuser()
2073
2216
def test_ascii_user(self):
2074
osutils.set_or_unset_env('LOGNAME', 'jrandom')
2217
self.overrideEnv(self.envvar_to_override(), 'jrandom')
2075
2218
self.assertEqual(u'jrandom', osutils.getuser_unicode())
2077
2220
def test_unicode_user(self):
2083
2226
% (osutils.get_user_encoding(),))
2084
2227
uni_username = u'jrandom' + uni_val
2085
2228
encoded_username = uni_username.encode(ue)
2086
osutils.set_or_unset_env('LOGNAME', encoded_username)
2229
self.overrideEnv(self.envvar_to_override(), encoded_username)
2087
2230
self.assertEqual(uni_username, osutils.getuser_unicode())
2233
class TestBackupNames(tests.TestCase):
2236
super(TestBackupNames, self).setUp()
2239
def backup_exists(self, name):
2240
return name in self.backups
2242
def available_backup_name(self, name):
2243
backup_name = osutils.available_backup_name(name, self.backup_exists)
2244
self.backups.append(backup_name)
2247
def assertBackupName(self, expected, name):
2248
self.assertEqual(expected, self.available_backup_name(name))
2250
def test_empty(self):
2251
self.assertBackupName('file.~1~', 'file')
2253
def test_existing(self):
2254
self.available_backup_name('file')
2255
self.available_backup_name('file')
2256
self.assertBackupName('file.~3~', 'file')
2257
# Empty slots are found, this is not a strict requirement and may be
2258
# revisited if we test against all implementations.
2259
self.backups.remove('file.~2~')
2260
self.assertBackupName('file.~2~', 'file')
2263
class TestFindExecutableInPath(tests.TestCase):
2265
def test_windows(self):
2266
if sys.platform != 'win32':
2267
raise tests.TestSkipped('test requires win32')
2268
self.assertTrue(osutils.find_executable_on_path('explorer') is not None)
2270
osutils.find_executable_on_path('explorer.exe') is not None)
2272
osutils.find_executable_on_path('EXPLORER.EXE') is not None)
2274
osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
2275
self.assertTrue(osutils.find_executable_on_path('file.txt') is None)
2277
def test_windows_app_path(self):
2278
if sys.platform != 'win32':
2279
raise tests.TestSkipped('test requires win32')
2280
# Override PATH env var so that exe can only be found on App Path
2281
self.overrideEnv('PATH', '')
2282
# Internt Explorer is always registered in the App Path
2283
self.assertTrue(osutils.find_executable_on_path('iexplore') is not None)
2285
def test_other(self):
2286
if sys.platform == 'win32':
2287
raise tests.TestSkipped('test requires non-win32')
2288
self.assertTrue(osutils.find_executable_on_path('sh') is not None)
2290
osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
2293
class TestEnvironmentErrors(tests.TestCase):
2294
"""Test handling of environmental errors"""
2296
def test_is_oserror(self):
2297
self.assertTrue(osutils.is_environment_error(
2298
OSError(errno.EINVAL, "Invalid parameter")))
2300
def test_is_ioerror(self):
2301
self.assertTrue(osutils.is_environment_error(
2302
IOError(errno.EINVAL, "Invalid parameter")))
2304
def test_is_socket_error(self):
2305
self.assertTrue(osutils.is_environment_error(
2306
socket.error(errno.EINVAL, "Invalid parameter")))
2308
def test_is_select_error(self):
2309
self.assertTrue(osutils.is_environment_error(
2310
select.error(errno.EINVAL, "Invalid parameter")))
2312
def test_is_pywintypes_error(self):
2313
self.requireFeature(features.pywintypes)
2315
self.assertTrue(osutils.is_environment_error(
2316
pywintypes.error(errno.EINVAL, "Invalid parameter", "Caller")))