100
load_tests = load_tests_apply_scenarios
98
def load_tests(basic_tests, module, loader):
99
suite = loader.suiteClass()
100
dir_reader_tests, remaining_tests = tests.split_suite_by_condition(
101
basic_tests, tests.condition_isinstance(TestDirReader))
102
tests.multiply_tests(dir_reader_tests, dir_reader_scenarios(), suite)
103
suite.addTest(remaining_tests)
103
107
class TestContainsWhitespace(tests.TestCase):
105
109
def test_contains_whitespace(self):
106
self.assertTrue(osutils.contains_whitespace(u' '))
107
self.assertTrue(osutils.contains_whitespace(u'hello there'))
108
self.assertTrue(osutils.contains_whitespace(u'hellothere\n'))
109
self.assertTrue(osutils.contains_whitespace(u'hello\nthere'))
110
self.assertTrue(osutils.contains_whitespace(u'hello\rthere'))
111
self.assertTrue(osutils.contains_whitespace(u'hello\tthere'))
110
self.failUnless(osutils.contains_whitespace(u' '))
111
self.failUnless(osutils.contains_whitespace(u'hello there'))
112
self.failUnless(osutils.contains_whitespace(u'hellothere\n'))
113
self.failUnless(osutils.contains_whitespace(u'hello\nthere'))
114
self.failUnless(osutils.contains_whitespace(u'hello\rthere'))
115
self.failUnless(osutils.contains_whitespace(u'hello\tthere'))
113
117
# \xa0 is "Non-breaking-space" which on some python locales thinks it
114
118
# is whitespace, but we do not.
115
self.assertFalse(osutils.contains_whitespace(u''))
116
self.assertFalse(osutils.contains_whitespace(u'hellothere'))
117
self.assertFalse(osutils.contains_whitespace(u'hello\xa0there'))
119
self.failIf(osutils.contains_whitespace(u''))
120
self.failIf(osutils.contains_whitespace(u'hellothere'))
121
self.failIf(osutils.contains_whitespace(u'hello\xa0there'))
120
124
class TestRename(tests.TestCaseInTempDir):
231
235
self.assertFalse(osutils.is_inside_or_parent_of_any(dirs, fn))
234
class TestLstat(tests.TestCaseInTempDir):
236
def test_lstat_matches_fstat(self):
237
# On Windows, lstat and fstat don't always agree, primarily in the
238
# 'st_ino' and 'st_dev' fields. So we force them to be '0' in our
239
# custom implementation.
240
if sys.platform == 'win32':
241
# We only have special lstat/fstat if we have the extension.
242
# Without it, we may end up re-reading content when we don't have
243
# to, but otherwise it doesn't effect correctness.
244
self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
245
f = open('test-file.txt', 'wb')
246
self.addCleanup(f.close)
247
f.write('some content\n')
249
self.assertEqualStat(osutils.fstat(f.fileno()),
250
osutils.lstat('test-file.txt'))
253
238
class TestRmTree(tests.TestCaseInTempDir):
255
240
def test_rmtree(self):
1109
1083
# Ensure the message contains the file name
1110
1084
self.assertContainsRe(str(e), "\./test-unreadable")
1113
def test_walkdirs_encoding_error(self):
1114
# <https://bugs.launchpad.net/bzr/+bug/488519>
1115
# walkdirs didn't raise a useful message when the filenames
1116
# are not using the filesystem's encoding
1118
# require a bytestring based filesystem
1119
self.requireFeature(tests.ByteStringNamedFilesystem)
1130
self.build_tree(tree)
1132
# rename the 1file to a latin-1 filename
1133
os.rename("./1file", "\xe8file")
1134
if "\xe8file" not in os.listdir("."):
1135
self.skip("Lack filesystem that preserves arbitrary bytes")
1137
self._save_platform_info()
1138
win32utils.winver = None # Avoid the win32 detection code
1139
osutils._fs_enc = 'UTF-8'
1141
# this should raise on error
1143
for dirdetail, dirblock in osutils.walkdirs('.'):
1146
self.assertRaises(errors.BadFilenameEncoding, attempt)
1148
1086
def test__walkdirs_utf8(self):
1734
1672
class TestReCompile(tests.TestCase):
1736
def _deprecated_re_compile_checked(self, *args, **kwargs):
1737
return self.applyDeprecated(symbol_versioning.deprecated_in((2, 2, 0)),
1738
osutils.re_compile_checked, *args, **kwargs)
1740
1674
def test_re_compile_checked(self):
1741
r = self._deprecated_re_compile_checked(r'A*', re.IGNORECASE)
1675
r = osutils.re_compile_checked(r'A*', re.IGNORECASE)
1742
1676
self.assertTrue(r.match('aaaa'))
1743
1677
self.assertTrue(r.match('aAaA'))
1745
1679
def test_re_compile_checked_error(self):
1746
1680
# like https://bugs.launchpad.net/bzr/+bug/251352
1748
# Due to possible test isolation error, re.compile is not lazy at
1749
# this point. We re-install lazy compile.
1750
lazy_regex.install_lazy_compile()
1751
1681
err = self.assertRaises(
1752
1682
errors.BzrCommandError,
1753
self._deprecated_re_compile_checked, '*', re.IGNORECASE, 'test case')
1683
osutils.re_compile_checked, '*', re.IGNORECASE, 'test case')
1754
1684
self.assertEqual(
1755
'Invalid regular expression in test case: '
1756
'"*" nothing to repeat',
1685
"Invalid regular expression in test case: '*': "
1686
"nothing to repeat",
1760
1690
class TestDirReader(tests.TestCaseInTempDir):
1762
scenarios = dir_reader_scenarios()
1764
1692
# Set by load_tests
1765
1693
_dir_reader_class = None
1766
1694
_native_to_unicode = None
1925
1856
self.assertIsInstance(concurrency, int)
1927
1858
def test_local_concurrency_environment_variable(self):
1928
self.overrideEnv('BZR_CONCURRENCY', '2')
1859
os.environ['BZR_CONCURRENCY'] = '2'
1929
1860
self.assertEqual(2, osutils.local_concurrency(use_cache=False))
1930
self.overrideEnv('BZR_CONCURRENCY', '3')
1861
os.environ['BZR_CONCURRENCY'] = '3'
1931
1862
self.assertEqual(3, osutils.local_concurrency(use_cache=False))
1932
self.overrideEnv('BZR_CONCURRENCY', 'foo')
1863
os.environ['BZR_CONCURRENCY'] = 'foo'
1933
1864
self.assertEqual(1, osutils.local_concurrency(use_cache=False))
1935
1866
def test_option_concurrency(self):
1936
self.overrideEnv('BZR_CONCURRENCY', '1')
1867
os.environ['BZR_CONCURRENCY'] = '1'
1937
1868
self.run_bzr('rocks --concurrency 42')
1938
# Command line overrides environment variable
1869
# Command line overrides envrionment variable
1939
1870
self.assertEquals('42', os.environ['BZR_CONCURRENCY'])
1940
1871
self.assertEquals(42, osutils.local_concurrency(use_cache=False))
2011
1930
def test_defaults_to_BZR_COLUMNS(self):
2012
1931
# BZR_COLUMNS is set by the test framework
2013
1932
self.assertNotEqual('12', os.environ['BZR_COLUMNS'])
2014
self.overrideEnv('BZR_COLUMNS', '12')
1933
os.environ['BZR_COLUMNS'] = '12'
2015
1934
self.assertEqual(12, osutils.terminal_width())
2017
def test_BZR_COLUMNS_0_no_limit(self):
2018
self.overrideEnv('BZR_COLUMNS', '0')
2019
self.assertEqual(None, osutils.terminal_width())
2021
1936
def test_falls_back_to_COLUMNS(self):
2022
self.overrideEnv('BZR_COLUMNS', None)
1937
del os.environ['BZR_COLUMNS']
2023
1938
self.assertNotEqual('42', os.environ['COLUMNS'])
2024
1939
self.set_fake_tty()
2025
self.overrideEnv('COLUMNS', '42')
1940
os.environ['COLUMNS'] = '42'
2026
1941
self.assertEqual(42, osutils.terminal_width())
2028
1943
def test_tty_default_without_columns(self):
2029
self.overrideEnv('BZR_COLUMNS', None)
2030
self.overrideEnv('COLUMNS', None)
1944
del os.environ['BZR_COLUMNS']
1945
del os.environ['COLUMNS']
2032
1947
def terminal_size(w, h):
2076
1990
def _dummy_chown(self, path, uid, gid):
2077
1991
self.path, self.uid, self.gid = path, uid, gid
2079
def test_copy_ownership_from_path(self):
2080
"""copy_ownership_from_path test with specified src."""
2082
f = open('test_file', 'wt')
2083
osutils.copy_ownership_from_path('test_file', ownsrc)
2086
self.assertEquals(self.path, 'test_file')
2087
self.assertEquals(self.uid, s.st_uid)
2088
self.assertEquals(self.gid, s.st_gid)
2090
def test_copy_ownership_nonesrc(self):
2091
"""copy_ownership_from_path test with src=None."""
2092
f = open('test_file', 'wt')
2093
# should use parent dir for permissions
2094
osutils.copy_ownership_from_path('test_file')
2097
self.assertEquals(self.path, 'test_file')
2098
self.assertEquals(self.uid, s.st_uid)
2099
self.assertEquals(self.gid, s.st_gid)
2102
class TestGetuserUnicode(tests.TestCase):
2104
def test_ascii_user(self):
2105
self.overrideEnv('LOGNAME', 'jrandom')
2106
self.assertEqual(u'jrandom', osutils.getuser_unicode())
2108
def test_unicode_user(self):
2109
ue = osutils.get_user_encoding()
2110
uni_val, env_val = tests.probe_unicode_in_user_encoding()
2112
raise tests.TestSkipped(
2113
'Cannot find a unicode character that works in encoding %s'
2114
% (osutils.get_user_encoding(),))
2115
uni_username = u'jrandom' + uni_val
2116
encoded_username = uni_username.encode(ue)
2117
self.overrideEnv('LOGNAME', encoded_username)
2118
self.assertEqual(uni_username, osutils.getuser_unicode())
2119
self.overrideEnv('LOGNAME', u'jrandom\xb6'.encode(ue))
2120
self.assertEqual(u'jrandom\xb6', osutils.getuser_unicode())
2122
def test_no_username_bug_660174(self):
2123
self.requireFeature(features.win32_feature)
2124
for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
2125
self.overrideEnv(name, None)
2126
self.assertEqual(u'UNKNOWN', osutils.getuser_unicode())
2129
class TestBackupNames(tests.TestCase):
2132
super(TestBackupNames, self).setUp()
2135
def backup_exists(self, name):
2136
return name in self.backups
2138
def available_backup_name(self, name):
2139
backup_name = osutils.available_backup_name(name, self.backup_exists)
2140
self.backups.append(backup_name)
2143
def assertBackupName(self, expected, name):
2144
self.assertEqual(expected, self.available_backup_name(name))
2146
def test_empty(self):
2147
self.assertBackupName('file.~1~', 'file')
2149
def test_existing(self):
2150
self.available_backup_name('file')
2151
self.available_backup_name('file')
2152
self.assertBackupName('file.~3~', 'file')
2153
# Empty slots are found, this is not a strict requirement and may be
2154
# revisited if we test against all implementations.
2155
self.backups.remove('file.~2~')
2156
self.assertBackupName('file.~2~', 'file')
2159
class TestFindExecutableInPath(tests.TestCase):
2161
def test_windows(self):
2162
if sys.platform != 'win32':
2163
raise tests.TestSkipped('test requires win32')
2164
self.assertTrue(osutils.find_executable_on_path('explorer') is not None)
2166
osutils.find_executable_on_path('explorer.exe') is not None)
2168
osutils.find_executable_on_path('EXPLORER.EXE') is not None)
2170
osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
2171
self.assertTrue(osutils.find_executable_on_path('file.txt') is None)
2173
def test_other(self):
2174
if sys.platform == 'win32':
2175
raise tests.TestSkipped('test requires non-win32')
2176
self.assertTrue(osutils.find_executable_on_path('sh') is not None)
2178
osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
1993
def test_mkdir_with_ownership_chown(self):
1994
"""Ensure that osutils.mkdir_with_ownership chowns correctly with ownership_src.
1997
osutils.mkdir_with_ownership('foo', ownsrc)
2000
self.assertEquals(self.path, 'foo')
2001
self.assertEquals(self.uid, s.st_uid)
2002
self.assertEquals(self.gid, s.st_gid)
2004
def test_open_with_ownership_chown(self):
2005
"""Ensure that osutils.open_with_ownership chowns correctly with ownership_src.
2008
f = osutils.open_with_ownership('foo', 'w', ownership_src=ownsrc)
2010
# do a test write and close
2015
self.assertEquals(self.path, 'foo')
2016
self.assertEquals(self.uid, s.st_uid)
2017
self.assertEquals(self.gid, s.st_gid)