97
def load_tests(basic_tests, module, loader):
98
suite = loader.suiteClass()
99
dir_reader_tests, remaining_tests = tests.split_suite_by_condition(
100
basic_tests, tests.condition_isinstance(TestDirReader))
101
tests.multiply_tests(dir_reader_tests, dir_reader_scenarios(), suite)
102
suite.addTest(remaining_tests)
101
load_tests = load_tests_apply_scenarios
106
104
class TestContainsWhitespace(tests.TestCase):
108
106
def test_contains_whitespace(self):
109
self.failUnless(osutils.contains_whitespace(u' '))
110
self.failUnless(osutils.contains_whitespace(u'hello there'))
111
self.failUnless(osutils.contains_whitespace(u'hellothere\n'))
112
self.failUnless(osutils.contains_whitespace(u'hello\nthere'))
113
self.failUnless(osutils.contains_whitespace(u'hello\rthere'))
114
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'))
116
114
# \xa0 is "Non-breaking-space" which on some python locales thinks it
117
115
# is whitespace, but we do not.
118
self.failIf(osutils.contains_whitespace(u''))
119
self.failIf(osutils.contains_whitespace(u'hellothere'))
120
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'))
123
121
class TestRename(tests.TestCaseInTempDir):
1082
1110
# Ensure the message contains the file name
1083
1111
self.assertContainsRe(str(e), "\./test-unreadable")
1114
def test_walkdirs_encoding_error(self):
1115
# <https://bugs.launchpad.net/bzr/+bug/488519>
1116
# walkdirs didn't raise a useful message when the filenames
1117
# are not using the filesystem's encoding
1119
# require a bytestring based filesystem
1120
self.requireFeature(features.ByteStringNamedFilesystem)
1131
self.build_tree(tree)
1133
# rename the 1file to a latin-1 filename
1134
os.rename("./1file", "\xe8file")
1135
if "\xe8file" not in os.listdir("."):
1136
self.skip("Lack filesystem that preserves arbitrary bytes")
1138
self._save_platform_info()
1139
win32utils.winver = None # Avoid the win32 detection code
1140
osutils._fs_enc = 'UTF-8'
1142
# this should raise on error
1144
for dirdetail, dirblock in osutils.walkdirs('.'):
1147
self.assertRaises(errors.BadFilenameEncoding, attempt)
1085
1149
def test__walkdirs_utf8(self):
1153
1217
self.requireFeature(UTF8DirReaderFeature)
1154
1218
self._save_platform_info()
1155
1219
win32utils.winver = None # Avoid the win32 detection code
1156
osutils._fs_enc = 'UTF-8'
1157
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1220
osutils._fs_enc = 'utf-8'
1221
self.assertDirReaderIs(
1222
UTF8DirReaderFeature.module.UTF8DirReader)
1159
1224
def test_force_walkdirs_utf8_fs_ascii(self):
1160
1225
self.requireFeature(UTF8DirReaderFeature)
1161
1226
self._save_platform_info()
1162
1227
win32utils.winver = None # Avoid the win32 detection code
1163
osutils._fs_enc = 'US-ASCII'
1164
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1166
def test_force_walkdirs_utf8_fs_ANSI(self):
1167
self.requireFeature(UTF8DirReaderFeature)
1168
self._save_platform_info()
1169
win32utils.winver = None # Avoid the win32 detection code
1170
osutils._fs_enc = 'ANSI_X3.4-1968'
1171
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1228
osutils._fs_enc = 'ascii'
1229
self.assertDirReaderIs(
1230
UTF8DirReaderFeature.module.UTF8DirReader)
1173
1232
def test_force_walkdirs_utf8_fs_latin1(self):
1174
1233
self._save_platform_info()
1175
1234
win32utils.winver = None # Avoid the win32 detection code
1176
osutils._fs_enc = 'latin1'
1235
osutils._fs_enc = 'iso-8859-1'
1177
1236
self.assertDirReaderIs(osutils.UnicodeDirReader)
1179
1238
def test_force_walkdirs_utf8_nt(self):
1671
1730
class TestReCompile(tests.TestCase):
1732
def _deprecated_re_compile_checked(self, *args, **kwargs):
1733
return self.applyDeprecated(symbol_versioning.deprecated_in((2, 2, 0)),
1734
osutils.re_compile_checked, *args, **kwargs)
1673
1736
def test_re_compile_checked(self):
1674
r = osutils.re_compile_checked(r'A*', re.IGNORECASE)
1737
r = self._deprecated_re_compile_checked(r'A*', re.IGNORECASE)
1675
1738
self.assertTrue(r.match('aaaa'))
1676
1739
self.assertTrue(r.match('aAaA'))
1678
1741
def test_re_compile_checked_error(self):
1679
1742
# like https://bugs.launchpad.net/bzr/+bug/251352
1744
# Due to possible test isolation error, re.compile is not lazy at
1745
# this point. We re-install lazy compile.
1746
lazy_regex.install_lazy_compile()
1680
1747
err = self.assertRaises(
1681
1748
errors.BzrCommandError,
1682
osutils.re_compile_checked, '*', re.IGNORECASE, 'test case')
1749
self._deprecated_re_compile_checked, '*', re.IGNORECASE, 'test case')
1683
1750
self.assertEqual(
1684
"Invalid regular expression in test case: '*': "
1685
"nothing to repeat",
1751
'Invalid regular expression in test case: '
1752
'"*" nothing to repeat',
1689
1756
class TestDirReader(tests.TestCaseInTempDir):
1758
scenarios = dir_reader_scenarios()
1691
1760
# Set by load_tests
1692
1761
_dir_reader_class = None
1693
1762
_native_to_unicode = None
1855
1921
self.assertIsInstance(concurrency, int)
1857
1923
def test_local_concurrency_environment_variable(self):
1858
os.environ['BZR_CONCURRENCY'] = '2'
1924
self.overrideEnv('BZR_CONCURRENCY', '2')
1859
1925
self.assertEqual(2, osutils.local_concurrency(use_cache=False))
1860
os.environ['BZR_CONCURRENCY'] = '3'
1926
self.overrideEnv('BZR_CONCURRENCY', '3')
1861
1927
self.assertEqual(3, osutils.local_concurrency(use_cache=False))
1862
os.environ['BZR_CONCURRENCY'] = 'foo'
1928
self.overrideEnv('BZR_CONCURRENCY', 'foo')
1863
1929
self.assertEqual(1, osutils.local_concurrency(use_cache=False))
1865
1931
def test_option_concurrency(self):
1866
os.environ['BZR_CONCURRENCY'] = '1'
1932
self.overrideEnv('BZR_CONCURRENCY', '1')
1867
1933
self.run_bzr('rocks --concurrency 42')
1868
# Command line overrides envrionment variable
1934
# Command line overrides environment variable
1869
1935
self.assertEquals('42', os.environ['BZR_CONCURRENCY'])
1870
1936
self.assertEquals(42, osutils.local_concurrency(use_cache=False))
1929
2007
def test_defaults_to_BZR_COLUMNS(self):
1930
2008
# BZR_COLUMNS is set by the test framework
1931
2009
self.assertNotEqual('12', os.environ['BZR_COLUMNS'])
1932
os.environ['BZR_COLUMNS'] = '12'
2010
self.overrideEnv('BZR_COLUMNS', '12')
1933
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())
1935
2017
def test_falls_back_to_COLUMNS(self):
1936
del os.environ['BZR_COLUMNS']
2018
self.overrideEnv('BZR_COLUMNS', None)
1937
2019
self.assertNotEqual('42', os.environ['COLUMNS'])
1938
2020
self.set_fake_tty()
1939
os.environ['COLUMNS'] = '42'
2021
self.overrideEnv('COLUMNS', '42')
1940
2022
self.assertEqual(42, osutils.terminal_width())
1942
2024
def test_tty_default_without_columns(self):
1943
del os.environ['BZR_COLUMNS']
1944
del os.environ['COLUMNS']
2025
self.overrideEnv('BZR_COLUMNS', None)
2026
self.overrideEnv('COLUMNS', None)
1946
2028
def terminal_size(w, h):
1972
2054
self.overrideAttr(termios, 'TIOCGWINSZ')
1973
2055
del termios.TIOCGWINSZ
1974
del os.environ['BZR_COLUMNS']
1975
del os.environ['COLUMNS']
2056
self.overrideEnv('BZR_COLUMNS', None)
2057
self.overrideEnv('COLUMNS', None)
1976
2058
# Whatever the result is, if we don't raise an exception, it's ok.
1977
2059
osutils.terminal_width()
2062
class TestCreationOps(tests.TestCaseInTempDir):
2063
_test_needs_features = [features.chown_feature]
2066
tests.TestCaseInTempDir.setUp(self)
2067
self.overrideAttr(os, 'chown', self._dummy_chown)
2069
# params set by call to _dummy_chown
2070
self.path = self.uid = self.gid = None
2072
def _dummy_chown(self, path, uid, gid):
2073
self.path, self.uid, self.gid = path, uid, gid
2075
def test_copy_ownership_from_path(self):
2076
"""copy_ownership_from_path test with specified src."""
2078
f = open('test_file', 'wt')
2079
osutils.copy_ownership_from_path('test_file', ownsrc)
2082
self.assertEquals(self.path, 'test_file')
2083
self.assertEquals(self.uid, s.st_uid)
2084
self.assertEquals(self.gid, s.st_gid)
2086
def test_copy_ownership_nonesrc(self):
2087
"""copy_ownership_from_path test with src=None."""
2088
f = open('test_file', 'wt')
2089
# should use parent dir for permissions
2090
osutils.copy_ownership_from_path('test_file')
2093
self.assertEquals(self.path, 'test_file')
2094
self.assertEquals(self.uid, s.st_uid)
2095
self.assertEquals(self.gid, s.st_gid)
2098
class TestGetuserUnicode(tests.TestCase):
2100
def test_ascii_user(self):
2101
self.overrideEnv('LOGNAME', 'jrandom')
2102
self.assertEqual(u'jrandom', osutils.getuser_unicode())
2104
def test_unicode_user(self):
2105
ue = osutils.get_user_encoding()
2106
uni_val, env_val = tests.probe_unicode_in_user_encoding()
2108
raise tests.TestSkipped(
2109
'Cannot find a unicode character that works in encoding %s'
2110
% (osutils.get_user_encoding(),))
2111
uni_username = u'jrandom' + uni_val
2112
encoded_username = uni_username.encode(ue)
2113
self.overrideEnv('LOGNAME', encoded_username)
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")))