101
load_tests = load_tests_apply_scenarios
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)
104
106
class TestContainsWhitespace(tests.TestCase):
106
108
def test_contains_whitespace(self):
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'))
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'))
114
116
# \xa0 is "Non-breaking-space" which on some python locales thinks it
115
117
# is whitespace, but we do not.
116
self.assertFalse(osutils.contains_whitespace(u''))
117
self.assertFalse(osutils.contains_whitespace(u'hellothere'))
118
self.assertFalse(osutils.contains_whitespace(u'hello\xa0there'))
118
self.failIf(osutils.contains_whitespace(u''))
119
self.failIf(osutils.contains_whitespace(u'hellothere'))
120
self.failIf(osutils.contains_whitespace(u'hello\xa0there'))
121
123
class TestRename(tests.TestCaseInTempDir):
1110
1080
# Ensure the message contains the file name
1111
1081
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)
1149
1083
def test__walkdirs_utf8(self):
1218
1152
self._save_platform_info()
1219
1153
win32utils.winver = None # Avoid the win32 detection code
1220
1154
osutils._fs_enc = 'UTF-8'
1221
self.assertDirReaderIs(
1222
UTF8DirReaderFeature.module.UTF8DirReader)
1155
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1224
1157
def test_force_walkdirs_utf8_fs_ascii(self):
1225
1158
self.requireFeature(UTF8DirReaderFeature)
1226
1159
self._save_platform_info()
1227
1160
win32utils.winver = None # Avoid the win32 detection code
1228
1161
osutils._fs_enc = 'US-ASCII'
1229
self.assertDirReaderIs(
1230
UTF8DirReaderFeature.module.UTF8DirReader)
1162
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1232
1164
def test_force_walkdirs_utf8_fs_ANSI(self):
1233
1165
self.requireFeature(UTF8DirReaderFeature)
1234
1166
self._save_platform_info()
1235
1167
win32utils.winver = None # Avoid the win32 detection code
1236
1168
osutils._fs_enc = 'ANSI_X3.4-1968'
1237
self.assertDirReaderIs(
1238
UTF8DirReaderFeature.module.UTF8DirReader)
1169
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1240
1171
def test_force_walkdirs_utf8_fs_latin1(self):
1241
1172
self._save_platform_info()
1738
1669
class TestReCompile(tests.TestCase):
1740
def _deprecated_re_compile_checked(self, *args, **kwargs):
1741
return self.applyDeprecated(symbol_versioning.deprecated_in((2, 2, 0)),
1742
osutils.re_compile_checked, *args, **kwargs)
1744
1671
def test_re_compile_checked(self):
1745
r = self._deprecated_re_compile_checked(r'A*', re.IGNORECASE)
1672
r = osutils.re_compile_checked(r'A*', re.IGNORECASE)
1746
1673
self.assertTrue(r.match('aaaa'))
1747
1674
self.assertTrue(r.match('aAaA'))
1749
1676
def test_re_compile_checked_error(self):
1750
1677
# like https://bugs.launchpad.net/bzr/+bug/251352
1752
# Due to possible test isolation error, re.compile is not lazy at
1753
# this point. We re-install lazy compile.
1754
lazy_regex.install_lazy_compile()
1755
1678
err = self.assertRaises(
1756
1679
errors.BzrCommandError,
1757
self._deprecated_re_compile_checked, '*', re.IGNORECASE, 'test case')
1680
osutils.re_compile_checked, '*', re.IGNORECASE, 'test case')
1758
1681
self.assertEqual(
1759
'Invalid regular expression in test case: '
1760
'"*" nothing to repeat',
1682
"Invalid regular expression in test case: '*': "
1683
"nothing to repeat",
1764
1687
class TestDirReader(tests.TestCaseInTempDir):
1766
scenarios = dir_reader_scenarios()
1768
1689
# Set by load_tests
1769
1690
_dir_reader_class = None
1770
1691
_native_to_unicode = None
1929
1853
self.assertIsInstance(concurrency, int)
1931
1855
def test_local_concurrency_environment_variable(self):
1932
self.overrideEnv('BZR_CONCURRENCY', '2')
1856
os.environ['BZR_CONCURRENCY'] = '2'
1933
1857
self.assertEqual(2, osutils.local_concurrency(use_cache=False))
1934
self.overrideEnv('BZR_CONCURRENCY', '3')
1858
os.environ['BZR_CONCURRENCY'] = '3'
1935
1859
self.assertEqual(3, osutils.local_concurrency(use_cache=False))
1936
self.overrideEnv('BZR_CONCURRENCY', 'foo')
1860
os.environ['BZR_CONCURRENCY'] = 'foo'
1937
1861
self.assertEqual(1, osutils.local_concurrency(use_cache=False))
1939
1863
def test_option_concurrency(self):
1940
self.overrideEnv('BZR_CONCURRENCY', '1')
1864
os.environ['BZR_CONCURRENCY'] = '1'
1941
1865
self.run_bzr('rocks --concurrency 42')
1942
# Command line overrides environment variable
1866
# Command line overrides envrionment variable
1943
1867
self.assertEquals('42', os.environ['BZR_CONCURRENCY'])
1944
1868
self.assertEquals(42, osutils.local_concurrency(use_cache=False))
2015
1927
def test_defaults_to_BZR_COLUMNS(self):
2016
1928
# BZR_COLUMNS is set by the test framework
2017
1929
self.assertNotEqual('12', os.environ['BZR_COLUMNS'])
2018
self.overrideEnv('BZR_COLUMNS', '12')
1930
os.environ['BZR_COLUMNS'] = '12'
2019
1931
self.assertEqual(12, osutils.terminal_width())
2021
def test_BZR_COLUMNS_0_no_limit(self):
2022
self.overrideEnv('BZR_COLUMNS', '0')
2023
self.assertEqual(None, osutils.terminal_width())
2025
1933
def test_falls_back_to_COLUMNS(self):
2026
self.overrideEnv('BZR_COLUMNS', None)
1934
del os.environ['BZR_COLUMNS']
2027
1935
self.assertNotEqual('42', os.environ['COLUMNS'])
2028
1936
self.set_fake_tty()
2029
self.overrideEnv('COLUMNS', '42')
1937
os.environ['COLUMNS'] = '42'
2030
1938
self.assertEqual(42, osutils.terminal_width())
2032
1940
def test_tty_default_without_columns(self):
2033
self.overrideEnv('BZR_COLUMNS', None)
2034
self.overrideEnv('COLUMNS', None)
1941
del os.environ['BZR_COLUMNS']
1942
del os.environ['COLUMNS']
2036
1944
def terminal_size(w, h):
2062
1970
self.overrideAttr(termios, 'TIOCGWINSZ')
2063
1971
del termios.TIOCGWINSZ
2064
self.overrideEnv('BZR_COLUMNS', None)
2065
self.overrideEnv('COLUMNS', None)
1972
del os.environ['BZR_COLUMNS']
1973
del os.environ['COLUMNS']
2066
1974
# Whatever the result is, if we don't raise an exception, it's ok.
2067
1975
osutils.terminal_width()
2070
class TestCreationOps(tests.TestCaseInTempDir):
2071
_test_needs_features = [features.chown_feature]
2074
tests.TestCaseInTempDir.setUp(self)
2075
self.overrideAttr(os, 'chown', self._dummy_chown)
2077
# params set by call to _dummy_chown
2078
self.path = self.uid = self.gid = None
2080
def _dummy_chown(self, path, uid, gid):
2081
self.path, self.uid, self.gid = path, uid, gid
2083
def test_copy_ownership_from_path(self):
2084
"""copy_ownership_from_path test with specified src."""
2086
f = open('test_file', 'wt')
2087
osutils.copy_ownership_from_path('test_file', ownsrc)
2090
self.assertEquals(self.path, 'test_file')
2091
self.assertEquals(self.uid, s.st_uid)
2092
self.assertEquals(self.gid, s.st_gid)
2094
def test_copy_ownership_nonesrc(self):
2095
"""copy_ownership_from_path test with src=None."""
2096
f = open('test_file', 'wt')
2097
# should use parent dir for permissions
2098
osutils.copy_ownership_from_path('test_file')
2101
self.assertEquals(self.path, 'test_file')
2102
self.assertEquals(self.uid, s.st_uid)
2103
self.assertEquals(self.gid, s.st_gid)
2106
class TestGetuserUnicode(tests.TestCase):
2108
def test_ascii_user(self):
2109
self.overrideEnv('LOGNAME', 'jrandom')
2110
self.assertEqual(u'jrandom', osutils.getuser_unicode())
2112
def test_unicode_user(self):
2113
ue = osutils.get_user_encoding()
2114
uni_val, env_val = tests.probe_unicode_in_user_encoding()
2116
raise tests.TestSkipped(
2117
'Cannot find a unicode character that works in encoding %s'
2118
% (osutils.get_user_encoding(),))
2119
uni_username = u'jrandom' + uni_val
2120
encoded_username = uni_username.encode(ue)
2121
self.overrideEnv('LOGNAME', encoded_username)
2122
self.assertEqual(uni_username, osutils.getuser_unicode())
2123
self.overrideEnv('LOGNAME', u'jrandom\xb6'.encode(ue))
2124
self.assertEqual(u'jrandom\xb6', osutils.getuser_unicode())
2126
def test_no_username_bug_660174(self):
2127
self.requireFeature(features.win32_feature)
2128
for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
2129
self.overrideEnv(name, None)
2130
self.assertEqual(u'UNKNOWN', osutils.getuser_unicode())
2133
class TestBackupNames(tests.TestCase):
2136
super(TestBackupNames, self).setUp()
2139
def backup_exists(self, name):
2140
return name in self.backups
2142
def available_backup_name(self, name):
2143
backup_name = osutils.available_backup_name(name, self.backup_exists)
2144
self.backups.append(backup_name)
2147
def assertBackupName(self, expected, name):
2148
self.assertEqual(expected, self.available_backup_name(name))
2150
def test_empty(self):
2151
self.assertBackupName('file.~1~', 'file')
2153
def test_existing(self):
2154
self.available_backup_name('file')
2155
self.available_backup_name('file')
2156
self.assertBackupName('file.~3~', 'file')
2157
# Empty slots are found, this is not a strict requirement and may be
2158
# revisited if we test against all implementations.
2159
self.backups.remove('file.~2~')
2160
self.assertBackupName('file.~2~', 'file')
2163
class TestFindExecutableInPath(tests.TestCase):
2165
def test_windows(self):
2166
if sys.platform != 'win32':
2167
raise tests.TestSkipped('test requires win32')
2168
self.assertTrue(osutils.find_executable_on_path('explorer') is not None)
2170
osutils.find_executable_on_path('explorer.exe') is not None)
2172
osutils.find_executable_on_path('EXPLORER.EXE') is not None)
2174
osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
2175
self.assertTrue(osutils.find_executable_on_path('file.txt') is None)
2177
def test_other(self):
2178
if sys.platform == 'win32':
2179
raise tests.TestSkipped('test requires non-win32')
2180
self.assertTrue(osutils.find_executable_on_path('sh') is not None)
2182
osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
2185
class TestEnvironmentErrors(tests.TestCase):
2186
"""Test handling of environmental errors"""
2188
def test_is_oserror(self):
2189
self.assertTrue(osutils.is_environment_error(
2190
OSError(errno.EINVAL, "Invalid parameter")))
2192
def test_is_ioerror(self):
2193
self.assertTrue(osutils.is_environment_error(
2194
IOError(errno.EINVAL, "Invalid parameter")))
2196
def test_is_socket_error(self):
2197
self.assertTrue(osutils.is_environment_error(
2198
socket.error(errno.EINVAL, "Invalid parameter")))
2200
def test_is_select_error(self):
2201
self.assertTrue(osutils.is_environment_error(
2202
select.error(errno.EINVAL, "Invalid parameter")))
2204
def test_is_pywintypes_error(self):
2205
self.requireFeature(features.pywintypes)
2207
self.assertTrue(osutils.is_environment_error(
2208
pywintypes.error(errno.EINVAL, "Invalid parameter", "Caller")))