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
232
self.assertFalse(osutils.is_inside_or_parent_of_any(dirs, fn))
235
class TestLstat(tests.TestCaseInTempDir):
237
def test_lstat_matches_fstat(self):
238
# On Windows, lstat and fstat don't always agree, primarily in the
239
# 'st_ino' and 'st_dev' fields. So we force them to be '0' in our
240
# custom implementation.
241
if sys.platform == 'win32':
242
# We only have special lstat/fstat if we have the extension.
243
# Without it, we may end up re-reading content when we don't have
244
# to, but otherwise it doesn't effect correctness.
245
self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
246
f = open('test-file.txt', 'wb')
247
self.addCleanup(f.close)
248
f.write('some content\n')
250
self.assertEqualStat(osutils.fstat(f.fileno()),
251
osutils.lstat('test-file.txt'))
239
254
class TestRmTree(tests.TestCaseInTempDir):
241
256
def test_rmtree(self):
469
484
class TestCanonicalRelPath(tests.TestCaseInTempDir):
471
_test_needs_features = [tests.CaseInsCasePresFilenameFeature]
486
_test_needs_features = [features.CaseInsCasePresFilenameFeature]
473
488
def test_canonical_relpath_simple(self):
474
489
f = file('MixedCaseName', 'w')
476
491
actual = osutils.canonical_relpath(self.test_base_dir, 'mixedcasename')
477
self.failUnlessEqual('work/MixedCaseName', actual)
492
self.assertEqual('work/MixedCaseName', actual)
479
494
def test_canonical_relpath_missing_tail(self):
480
495
os.mkdir('MixedCaseParent')
481
496
actual = osutils.canonical_relpath(self.test_base_dir,
482
497
'mixedcaseparent/nochild')
483
self.failUnlessEqual('work/MixedCaseParent/nochild', actual)
498
self.assertEqual('work/MixedCaseParent/nochild', actual)
486
501
class Test_CICPCanonicalRelpath(tests.TestCaseWithTransport):
804
819
self.assertEqual(None, osutils.safe_file_id(None))
822
class TestPosixFuncs(tests.TestCase):
823
"""Test that the posix version of normpath returns an appropriate path
824
when used with 2 leading slashes."""
826
def test_normpath(self):
827
self.assertEqual('/etc/shadow', osutils._posix_normpath('/etc/shadow'))
828
self.assertEqual('/etc/shadow', osutils._posix_normpath('//etc/shadow'))
829
self.assertEqual('/etc/shadow', osutils._posix_normpath('///etc/shadow'))
807
832
class TestWin32Funcs(tests.TestCase):
808
833
"""Test that _win32 versions of os utilities return appropriate paths."""
970
995
"""Test mac special functions that require directories."""
972
997
def test_getcwd(self):
973
self.requireFeature(tests.UnicodeFilenameFeature)
998
self.requireFeature(features.UnicodeFilenameFeature)
974
999
os.mkdir(u'B\xe5gfors')
975
1000
os.chdir(u'B\xe5gfors')
976
1001
self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
978
1003
def test_getcwd_nonnorm(self):
979
self.requireFeature(tests.UnicodeFilenameFeature)
1004
self.requireFeature(features.UnicodeFilenameFeature)
980
1005
# Test that _mac_getcwd() will normalize this path
981
1006
os.mkdir(u'Ba\u030agfors')
982
1007
os.chdir(u'Ba\u030agfors')
1190
1217
self.requireFeature(UTF8DirReaderFeature)
1191
1218
self._save_platform_info()
1192
1219
win32utils.winver = None # Avoid the win32 detection code
1193
osutils._fs_enc = 'UTF-8'
1194
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1220
osutils._fs_enc = 'utf-8'
1221
self.assertDirReaderIs(
1222
UTF8DirReaderFeature.module.UTF8DirReader)
1196
1224
def test_force_walkdirs_utf8_fs_ascii(self):
1197
1225
self.requireFeature(UTF8DirReaderFeature)
1198
1226
self._save_platform_info()
1199
1227
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)
1228
osutils._fs_enc = 'ascii'
1229
self.assertDirReaderIs(
1230
UTF8DirReaderFeature.module.UTF8DirReader)
1210
1232
def test_force_walkdirs_utf8_fs_latin1(self):
1211
1233
self._save_platform_info()
1212
1234
win32utils.winver = None # Avoid the win32 detection code
1213
osutils._fs_enc = 'latin1'
1235
osutils._fs_enc = 'iso-8859-1'
1214
1236
self.assertDirReaderIs(osutils.UnicodeDirReader)
1216
1238
def test_force_walkdirs_utf8_nt(self):
1838
1862
return filtered_dirblocks
1840
1864
def test_walk_unicode_tree(self):
1841
self.requireFeature(tests.UnicodeFilenameFeature)
1865
self.requireFeature(features.UnicodeFilenameFeature)
1842
1866
tree, expected_dirblocks = self._get_unicode_tree()
1843
1867
self.build_tree(tree)
1844
1868
result = list(osutils._walkdirs_utf8('.'))
1845
1869
self.assertEqual(expected_dirblocks, self._filter_out(result))
1847
1871
def test_symlink(self):
1848
self.requireFeature(tests.SymlinkFeature)
1849
self.requireFeature(tests.UnicodeFilenameFeature)
1872
self.requireFeature(features.SymlinkFeature)
1873
self.requireFeature(features.UnicodeFilenameFeature)
1850
1874
target = u'target\N{Euro Sign}'
1851
1875
link_name = u'l\N{Euro Sign}nk'
1852
1876
os.symlink(target, link_name)
1879
1903
os.symlink(self.target, self.link)
1881
1905
def test_os_readlink_link_encoding(self):
1882
if sys.version_info < (2, 6):
1883
self.assertRaises(UnicodeEncodeError, os.readlink, self.link)
1885
self.assertEquals(self.target, os.readlink(self.link))
1906
self.assertEquals(self.target, os.readlink(self.link))
1887
1908
def test_os_readlink_link_decoding(self):
1888
1909
self.assertEquals(self.target.encode(osutils._fs_enc),
1900
1921
self.assertIsInstance(concurrency, int)
1902
1923
def test_local_concurrency_environment_variable(self):
1903
os.environ['BZR_CONCURRENCY'] = '2'
1924
self.overrideEnv('BZR_CONCURRENCY', '2')
1904
1925
self.assertEqual(2, osutils.local_concurrency(use_cache=False))
1905
os.environ['BZR_CONCURRENCY'] = '3'
1926
self.overrideEnv('BZR_CONCURRENCY', '3')
1906
1927
self.assertEqual(3, osutils.local_concurrency(use_cache=False))
1907
os.environ['BZR_CONCURRENCY'] = 'foo'
1928
self.overrideEnv('BZR_CONCURRENCY', 'foo')
1908
1929
self.assertEqual(1, osutils.local_concurrency(use_cache=False))
1910
1931
def test_option_concurrency(self):
1911
os.environ['BZR_CONCURRENCY'] = '1'
1932
self.overrideEnv('BZR_CONCURRENCY', '1')
1912
1933
self.run_bzr('rocks --concurrency 42')
1913
# Command line overrides envrionment variable
1934
# Command line overrides environment variable
1914
1935
self.assertEquals('42', os.environ['BZR_CONCURRENCY'])
1915
1936
self.assertEquals(42, osutils.local_concurrency(use_cache=False))
1986
2007
def test_defaults_to_BZR_COLUMNS(self):
1987
2008
# BZR_COLUMNS is set by the test framework
1988
2009
self.assertNotEqual('12', os.environ['BZR_COLUMNS'])
1989
os.environ['BZR_COLUMNS'] = '12'
2010
self.overrideEnv('BZR_COLUMNS', '12')
1990
2011
self.assertEqual(12, osutils.terminal_width())
2013
def test_BZR_COLUMNS_0_no_limit(self):
2014
self.overrideEnv('BZR_COLUMNS', '0')
2015
self.assertEqual(None, osutils.terminal_width())
1992
2017
def test_falls_back_to_COLUMNS(self):
1993
del os.environ['BZR_COLUMNS']
2018
self.overrideEnv('BZR_COLUMNS', None)
1994
2019
self.assertNotEqual('42', os.environ['COLUMNS'])
1995
2020
self.set_fake_tty()
1996
os.environ['COLUMNS'] = '42'
2021
self.overrideEnv('COLUMNS', '42')
1997
2022
self.assertEqual(42, osutils.terminal_width())
1999
2024
def test_tty_default_without_columns(self):
2000
del os.environ['BZR_COLUMNS']
2001
del os.environ['COLUMNS']
2025
self.overrideEnv('BZR_COLUMNS', None)
2026
self.overrideEnv('COLUMNS', None)
2003
2028
def terminal_size(w, h):
2029
2054
self.overrideAttr(termios, 'TIOCGWINSZ')
2030
2055
del termios.TIOCGWINSZ
2031
del os.environ['BZR_COLUMNS']
2032
del os.environ['COLUMNS']
2056
self.overrideEnv('BZR_COLUMNS', None)
2057
self.overrideEnv('COLUMNS', None)
2033
2058
# Whatever the result is, if we don't raise an exception, it's ok.
2034
2059
osutils.terminal_width()
2036
2062
class TestCreationOps(tests.TestCaseInTempDir):
2037
2063
_test_needs_features = [features.chown_feature]
2068
2094
self.assertEquals(self.uid, s.st_uid)
2069
2095
self.assertEquals(self.gid, s.st_gid)
2071
2098
class TestGetuserUnicode(tests.TestCase):
2073
2100
def test_ascii_user(self):
2074
osutils.set_or_unset_env('LOGNAME', 'jrandom')
2101
self.overrideEnv('LOGNAME', 'jrandom')
2075
2102
self.assertEqual(u'jrandom', osutils.getuser_unicode())
2077
2104
def test_unicode_user(self):
2083
2110
% (osutils.get_user_encoding(),))
2084
2111
uni_username = u'jrandom' + uni_val
2085
2112
encoded_username = uni_username.encode(ue)
2086
osutils.set_or_unset_env('LOGNAME', encoded_username)
2113
self.overrideEnv('LOGNAME', encoded_username)
2087
2114
self.assertEqual(uni_username, osutils.getuser_unicode())
2115
self.overrideEnv('LOGNAME', u'jrandom\xb6'.encode(ue))
2116
self.assertEqual(u'jrandom\xb6', osutils.getuser_unicode())
2119
class TestBackupNames(tests.TestCase):
2122
super(TestBackupNames, self).setUp()
2125
def backup_exists(self, name):
2126
return name in self.backups
2128
def available_backup_name(self, name):
2129
backup_name = osutils.available_backup_name(name, self.backup_exists)
2130
self.backups.append(backup_name)
2133
def assertBackupName(self, expected, name):
2134
self.assertEqual(expected, self.available_backup_name(name))
2136
def test_empty(self):
2137
self.assertBackupName('file.~1~', 'file')
2139
def test_existing(self):
2140
self.available_backup_name('file')
2141
self.available_backup_name('file')
2142
self.assertBackupName('file.~3~', 'file')
2143
# Empty slots are found, this is not a strict requirement and may be
2144
# revisited if we test against all implementations.
2145
self.backups.remove('file.~2~')
2146
self.assertBackupName('file.~2~', 'file')
2149
class TestFindExecutableInPath(tests.TestCase):
2151
def test_windows(self):
2152
if sys.platform != 'win32':
2153
raise tests.TestSkipped('test requires win32')
2154
self.assertTrue(osutils.find_executable_on_path('explorer') is not None)
2156
osutils.find_executable_on_path('explorer.exe') is not None)
2158
osutils.find_executable_on_path('EXPLORER.EXE') is not None)
2160
osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
2161
self.assertTrue(osutils.find_executable_on_path('file.txt') is None)
2163
def test_other(self):
2164
if sys.platform == 'win32':
2165
raise tests.TestSkipped('test requires non-win32')
2166
self.assertTrue(osutils.find_executable_on_path('sh') is not None)
2168
osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
2171
class TestEnvironmentErrors(tests.TestCase):
2172
"""Test handling of environmental errors"""
2174
def test_is_oserror(self):
2175
self.assertTrue(osutils.is_environment_error(
2176
OSError(errno.EINVAL, "Invalid parameter")))
2178
def test_is_ioerror(self):
2179
self.assertTrue(osutils.is_environment_error(
2180
IOError(errno.EINVAL, "Invalid parameter")))
2182
def test_is_socket_error(self):
2183
self.assertTrue(osutils.is_environment_error(
2184
socket.error(errno.EINVAL, "Invalid parameter")))
2186
def test_is_select_error(self):
2187
self.assertTrue(osutils.is_environment_error(
2188
select.error(errno.EINVAL, "Invalid parameter")))
2190
def test_is_pywintypes_error(self):
2191
self.requireFeature(features.pywintypes)
2193
self.assertTrue(osutils.is_environment_error(
2194
pywintypes.error(errno.EINVAL, "Invalid parameter", "Caller")))