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)
100
load_tests = load_tests_apply_scenarios
106
103
class TestContainsWhitespace(tests.TestCase):
108
105
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'))
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'))
116
113
# \xa0 is "Non-breaking-space" which on some python locales thinks it
117
114
# 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'))
115
self.assertFalse(osutils.contains_whitespace(u''))
116
self.assertFalse(osutils.contains_whitespace(u'hellothere'))
117
self.assertFalse(osutils.contains_whitespace(u'hello\xa0there'))
123
120
class TestRename(tests.TestCaseInTempDir):
234
231
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'))
237
253
class TestRmTree(tests.TestCaseInTempDir):
239
255
def test_rmtree(self):
467
483
class TestCanonicalRelPath(tests.TestCaseInTempDir):
469
_test_needs_features = [tests.CaseInsCasePresFilenameFeature]
485
_test_needs_features = [features.CaseInsCasePresFilenameFeature]
471
487
def test_canonical_relpath_simple(self):
472
488
f = file('MixedCaseName', 'w')
474
490
actual = osutils.canonical_relpath(self.test_base_dir, 'mixedcasename')
475
self.failUnlessEqual('work/MixedCaseName', actual)
491
self.assertEqual('work/MixedCaseName', actual)
477
493
def test_canonical_relpath_missing_tail(self):
478
494
os.mkdir('MixedCaseParent')
479
495
actual = osutils.canonical_relpath(self.test_base_dir,
480
496
'mixedcaseparent/nochild')
481
self.failUnlessEqual('work/MixedCaseParent/nochild', actual)
497
self.assertEqual('work/MixedCaseParent/nochild', actual)
484
500
class Test_CICPCanonicalRelpath(tests.TestCaseWithTransport):
1082
1099
# Ensure the message contains the file name
1083
1100
self.assertContainsRe(str(e), "\./test-unreadable")
1103
def test_walkdirs_encoding_error(self):
1104
# <https://bugs.launchpad.net/bzr/+bug/488519>
1105
# walkdirs didn't raise a useful message when the filenames
1106
# are not using the filesystem's encoding
1108
# require a bytestring based filesystem
1109
self.requireFeature(features.ByteStringNamedFilesystem)
1120
self.build_tree(tree)
1122
# rename the 1file to a latin-1 filename
1123
os.rename("./1file", "\xe8file")
1124
if "\xe8file" not in os.listdir("."):
1125
self.skip("Lack filesystem that preserves arbitrary bytes")
1127
self._save_platform_info()
1128
win32utils.winver = None # Avoid the win32 detection code
1129
osutils._fs_enc = 'UTF-8'
1131
# this should raise on error
1133
for dirdetail, dirblock in osutils.walkdirs('.'):
1136
self.assertRaises(errors.BadFilenameEncoding, attempt)
1085
1138
def test__walkdirs_utf8(self):
1154
1207
self._save_platform_info()
1155
1208
win32utils.winver = None # Avoid the win32 detection code
1156
1209
osutils._fs_enc = 'UTF-8'
1157
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1210
self.assertDirReaderIs(
1211
UTF8DirReaderFeature.module.UTF8DirReader)
1159
1213
def test_force_walkdirs_utf8_fs_ascii(self):
1160
1214
self.requireFeature(UTF8DirReaderFeature)
1161
1215
self._save_platform_info()
1162
1216
win32utils.winver = None # Avoid the win32 detection code
1163
1217
osutils._fs_enc = 'US-ASCII'
1164
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1218
self.assertDirReaderIs(
1219
UTF8DirReaderFeature.module.UTF8DirReader)
1166
1221
def test_force_walkdirs_utf8_fs_ANSI(self):
1167
1222
self.requireFeature(UTF8DirReaderFeature)
1168
1223
self._save_platform_info()
1169
1224
win32utils.winver = None # Avoid the win32 detection code
1170
1225
osutils._fs_enc = 'ANSI_X3.4-1968'
1171
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1226
self.assertDirReaderIs(
1227
UTF8DirReaderFeature.module.UTF8DirReader)
1173
1229
def test_force_walkdirs_utf8_fs_latin1(self):
1174
1230
self._save_platform_info()
1671
1727
class TestReCompile(tests.TestCase):
1729
def _deprecated_re_compile_checked(self, *args, **kwargs):
1730
return self.applyDeprecated(symbol_versioning.deprecated_in((2, 2, 0)),
1731
osutils.re_compile_checked, *args, **kwargs)
1673
1733
def test_re_compile_checked(self):
1674
r = osutils.re_compile_checked(r'A*', re.IGNORECASE)
1734
r = self._deprecated_re_compile_checked(r'A*', re.IGNORECASE)
1675
1735
self.assertTrue(r.match('aaaa'))
1676
1736
self.assertTrue(r.match('aAaA'))
1678
1738
def test_re_compile_checked_error(self):
1679
1739
# like https://bugs.launchpad.net/bzr/+bug/251352
1741
# Due to possible test isolation error, re.compile is not lazy at
1742
# this point. We re-install lazy compile.
1743
lazy_regex.install_lazy_compile()
1680
1744
err = self.assertRaises(
1681
1745
errors.BzrCommandError,
1682
osutils.re_compile_checked, '*', re.IGNORECASE, 'test case')
1746
self._deprecated_re_compile_checked, '*', re.IGNORECASE, 'test case')
1683
1747
self.assertEqual(
1684
"Invalid regular expression in test case: '*': "
1685
"nothing to repeat",
1748
'Invalid regular expression in test case: '
1749
'"*" nothing to repeat',
1689
1753
class TestDirReader(tests.TestCaseInTempDir):
1755
scenarios = dir_reader_scenarios()
1691
1757
# Set by load_tests
1692
1758
_dir_reader_class = None
1693
1759
_native_to_unicode = None
1855
1918
self.assertIsInstance(concurrency, int)
1857
1920
def test_local_concurrency_environment_variable(self):
1858
os.environ['BZR_CONCURRENCY'] = '2'
1921
self.overrideEnv('BZR_CONCURRENCY', '2')
1859
1922
self.assertEqual(2, osutils.local_concurrency(use_cache=False))
1860
os.environ['BZR_CONCURRENCY'] = '3'
1923
self.overrideEnv('BZR_CONCURRENCY', '3')
1861
1924
self.assertEqual(3, osutils.local_concurrency(use_cache=False))
1862
os.environ['BZR_CONCURRENCY'] = 'foo'
1925
self.overrideEnv('BZR_CONCURRENCY', 'foo')
1863
1926
self.assertEqual(1, osutils.local_concurrency(use_cache=False))
1865
1928
def test_option_concurrency(self):
1866
os.environ['BZR_CONCURRENCY'] = '1'
1929
self.overrideEnv('BZR_CONCURRENCY', '1')
1867
1930
self.run_bzr('rocks --concurrency 42')
1868
# Command line overrides envrionment variable
1931
# Command line overrides environment variable
1869
1932
self.assertEquals('42', os.environ['BZR_CONCURRENCY'])
1870
1933
self.assertEquals(42, osutils.local_concurrency(use_cache=False))
1929
2004
def test_defaults_to_BZR_COLUMNS(self):
1930
2005
# BZR_COLUMNS is set by the test framework
1931
2006
self.assertNotEqual('12', os.environ['BZR_COLUMNS'])
1932
os.environ['BZR_COLUMNS'] = '12'
2007
self.overrideEnv('BZR_COLUMNS', '12')
1933
2008
self.assertEqual(12, osutils.terminal_width())
2010
def test_BZR_COLUMNS_0_no_limit(self):
2011
self.overrideEnv('BZR_COLUMNS', '0')
2012
self.assertEqual(None, osutils.terminal_width())
1935
2014
def test_falls_back_to_COLUMNS(self):
1936
del os.environ['BZR_COLUMNS']
2015
self.overrideEnv('BZR_COLUMNS', None)
1937
2016
self.assertNotEqual('42', os.environ['COLUMNS'])
1938
2017
self.set_fake_tty()
1939
os.environ['COLUMNS'] = '42'
2018
self.overrideEnv('COLUMNS', '42')
1940
2019
self.assertEqual(42, osutils.terminal_width())
1942
2021
def test_tty_default_without_columns(self):
1943
del os.environ['BZR_COLUMNS']
1944
del os.environ['COLUMNS']
2022
self.overrideEnv('BZR_COLUMNS', None)
2023
self.overrideEnv('COLUMNS', None)
1946
2025
def terminal_size(w, h):
1972
2051
self.overrideAttr(termios, 'TIOCGWINSZ')
1973
2052
del termios.TIOCGWINSZ
1974
del os.environ['BZR_COLUMNS']
1975
del os.environ['COLUMNS']
2053
self.overrideEnv('BZR_COLUMNS', None)
2054
self.overrideEnv('COLUMNS', None)
1976
2055
# Whatever the result is, if we don't raise an exception, it's ok.
1977
2056
osutils.terminal_width()
2059
class TestCreationOps(tests.TestCaseInTempDir):
2060
_test_needs_features = [features.chown_feature]
2063
tests.TestCaseInTempDir.setUp(self)
2064
self.overrideAttr(os, 'chown', self._dummy_chown)
2066
# params set by call to _dummy_chown
2067
self.path = self.uid = self.gid = None
2069
def _dummy_chown(self, path, uid, gid):
2070
self.path, self.uid, self.gid = path, uid, gid
2072
def test_copy_ownership_from_path(self):
2073
"""copy_ownership_from_path test with specified src."""
2075
f = open('test_file', 'wt')
2076
osutils.copy_ownership_from_path('test_file', ownsrc)
2079
self.assertEquals(self.path, 'test_file')
2080
self.assertEquals(self.uid, s.st_uid)
2081
self.assertEquals(self.gid, s.st_gid)
2083
def test_copy_ownership_nonesrc(self):
2084
"""copy_ownership_from_path test with src=None."""
2085
f = open('test_file', 'wt')
2086
# should use parent dir for permissions
2087
osutils.copy_ownership_from_path('test_file')
2090
self.assertEquals(self.path, 'test_file')
2091
self.assertEquals(self.uid, s.st_uid)
2092
self.assertEquals(self.gid, s.st_gid)
2095
class TestGetuserUnicode(tests.TestCase):
2097
def test_ascii_user(self):
2098
self.overrideEnv('LOGNAME', 'jrandom')
2099
self.assertEqual(u'jrandom', osutils.getuser_unicode())
2101
def test_unicode_user(self):
2102
ue = osutils.get_user_encoding()
2103
uni_val, env_val = tests.probe_unicode_in_user_encoding()
2105
raise tests.TestSkipped(
2106
'Cannot find a unicode character that works in encoding %s'
2107
% (osutils.get_user_encoding(),))
2108
uni_username = u'jrandom' + uni_val
2109
encoded_username = uni_username.encode(ue)
2110
self.overrideEnv('LOGNAME', encoded_username)
2111
self.assertEqual(uni_username, osutils.getuser_unicode())
2112
self.overrideEnv('LOGNAME', u'jrandom\xb6'.encode(ue))
2113
self.assertEqual(u'jrandom\xb6', osutils.getuser_unicode())
2115
def test_no_username_bug_660174(self):
2116
self.requireFeature(features.win32_feature)
2117
for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
2118
self.overrideEnv(name, None)
2119
self.assertEqual(u'UNKNOWN', osutils.getuser_unicode())
2122
class TestBackupNames(tests.TestCase):
2125
super(TestBackupNames, self).setUp()
2128
def backup_exists(self, name):
2129
return name in self.backups
2131
def available_backup_name(self, name):
2132
backup_name = osutils.available_backup_name(name, self.backup_exists)
2133
self.backups.append(backup_name)
2136
def assertBackupName(self, expected, name):
2137
self.assertEqual(expected, self.available_backup_name(name))
2139
def test_empty(self):
2140
self.assertBackupName('file.~1~', 'file')
2142
def test_existing(self):
2143
self.available_backup_name('file')
2144
self.available_backup_name('file')
2145
self.assertBackupName('file.~3~', 'file')
2146
# Empty slots are found, this is not a strict requirement and may be
2147
# revisited if we test against all implementations.
2148
self.backups.remove('file.~2~')
2149
self.assertBackupName('file.~2~', 'file')
2152
class TestFindExecutableInPath(tests.TestCase):
2154
def test_windows(self):
2155
if sys.platform != 'win32':
2156
raise tests.TestSkipped('test requires win32')
2157
self.assertTrue(osutils.find_executable_on_path('explorer') is not None)
2159
osutils.find_executable_on_path('explorer.exe') is not None)
2161
osutils.find_executable_on_path('EXPLORER.EXE') is not None)
2163
osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
2164
self.assertTrue(osutils.find_executable_on_path('file.txt') is None)
2166
def test_other(self):
2167
if sys.platform == 'win32':
2168
raise tests.TestSkipped('test requires non-win32')
2169
self.assertTrue(osutils.find_executable_on_path('sh') is not None)
2171
osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)