100
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)
103
106
class TestContainsWhitespace(tests.TestCase):
105
108
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'))
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'))
113
116
# \xa0 is "Non-breaking-space" which on some python locales thinks it
114
117
# 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'))
118
self.failIf(osutils.contains_whitespace(u''))
119
self.failIf(osutils.contains_whitespace(u'hellothere'))
120
self.failIf(osutils.contains_whitespace(u'hello\xa0there'))
120
123
class TestRename(tests.TestCaseInTempDir):
231
234
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
237
class TestRmTree(tests.TestCaseInTempDir):
255
239
def test_rmtree(self):
483
465
class TestCanonicalRelPath(tests.TestCaseInTempDir):
485
_test_needs_features = [features.CaseInsCasePresFilenameFeature]
467
_test_needs_features = [tests.CaseInsCasePresFilenameFeature]
487
469
def test_canonical_relpath_simple(self):
488
470
f = file('MixedCaseName', 'w')
490
472
actual = osutils.canonical_relpath(self.test_base_dir, 'mixedcasename')
491
self.assertEqual('work/MixedCaseName', actual)
473
self.failUnlessEqual('work/MixedCaseName', actual)
493
475
def test_canonical_relpath_missing_tail(self):
494
476
os.mkdir('MixedCaseParent')
495
477
actual = osutils.canonical_relpath(self.test_base_dir,
496
478
'mixedcaseparent/nochild')
497
self.assertEqual('work/MixedCaseParent/nochild', actual)
479
self.failUnlessEqual('work/MixedCaseParent/nochild', actual)
500
482
class Test_CICPCanonicalRelpath(tests.TestCaseWithTransport):
1099
1080
# Ensure the message contains the file name
1100
1081
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)
1138
1083
def test__walkdirs_utf8(self):
1207
1152
self._save_platform_info()
1208
1153
win32utils.winver = None # Avoid the win32 detection code
1209
1154
osutils._fs_enc = 'UTF-8'
1210
self.assertDirReaderIs(
1211
UTF8DirReaderFeature.module.UTF8DirReader)
1155
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1213
1157
def test_force_walkdirs_utf8_fs_ascii(self):
1214
1158
self.requireFeature(UTF8DirReaderFeature)
1215
1159
self._save_platform_info()
1216
1160
win32utils.winver = None # Avoid the win32 detection code
1217
1161
osutils._fs_enc = 'US-ASCII'
1218
self.assertDirReaderIs(
1219
UTF8DirReaderFeature.module.UTF8DirReader)
1162
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1221
1164
def test_force_walkdirs_utf8_fs_ANSI(self):
1222
1165
self.requireFeature(UTF8DirReaderFeature)
1223
1166
self._save_platform_info()
1224
1167
win32utils.winver = None # Avoid the win32 detection code
1225
1168
osutils._fs_enc = 'ANSI_X3.4-1968'
1226
self.assertDirReaderIs(
1227
UTF8DirReaderFeature.module.UTF8DirReader)
1169
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1229
1171
def test_force_walkdirs_utf8_fs_latin1(self):
1230
1172
self._save_platform_info()
1727
1669
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)
1733
1671
def test_re_compile_checked(self):
1734
r = self._deprecated_re_compile_checked(r'A*', re.IGNORECASE)
1672
r = osutils.re_compile_checked(r'A*', re.IGNORECASE)
1735
1673
self.assertTrue(r.match('aaaa'))
1736
1674
self.assertTrue(r.match('aAaA'))
1738
1676
def test_re_compile_checked_error(self):
1739
1677
# 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()
1744
1678
err = self.assertRaises(
1745
1679
errors.BzrCommandError,
1746
self._deprecated_re_compile_checked, '*', re.IGNORECASE, 'test case')
1680
osutils.re_compile_checked, '*', re.IGNORECASE, 'test case')
1747
1681
self.assertEqual(
1748
'Invalid regular expression in test case: '
1749
'"*" nothing to repeat',
1682
"Invalid regular expression in test case: '*': "
1683
"nothing to repeat",
1753
1687
class TestDirReader(tests.TestCaseInTempDir):
1755
scenarios = dir_reader_scenarios()
1757
1689
# Set by load_tests
1758
1690
_dir_reader_class = None
1759
1691
_native_to_unicode = None
1918
1853
self.assertIsInstance(concurrency, int)
1920
1855
def test_local_concurrency_environment_variable(self):
1921
self.overrideEnv('BZR_CONCURRENCY', '2')
1856
os.environ['BZR_CONCURRENCY'] = '2'
1922
1857
self.assertEqual(2, osutils.local_concurrency(use_cache=False))
1923
self.overrideEnv('BZR_CONCURRENCY', '3')
1858
os.environ['BZR_CONCURRENCY'] = '3'
1924
1859
self.assertEqual(3, osutils.local_concurrency(use_cache=False))
1925
self.overrideEnv('BZR_CONCURRENCY', 'foo')
1860
os.environ['BZR_CONCURRENCY'] = 'foo'
1926
1861
self.assertEqual(1, osutils.local_concurrency(use_cache=False))
1928
1863
def test_option_concurrency(self):
1929
self.overrideEnv('BZR_CONCURRENCY', '1')
1864
os.environ['BZR_CONCURRENCY'] = '1'
1930
1865
self.run_bzr('rocks --concurrency 42')
1931
# Command line overrides environment variable
1866
# Command line overrides envrionment variable
1932
1867
self.assertEquals('42', os.environ['BZR_CONCURRENCY'])
1933
1868
self.assertEquals(42, osutils.local_concurrency(use_cache=False))
2004
1927
def test_defaults_to_BZR_COLUMNS(self):
2005
1928
# BZR_COLUMNS is set by the test framework
2006
1929
self.assertNotEqual('12', os.environ['BZR_COLUMNS'])
2007
self.overrideEnv('BZR_COLUMNS', '12')
1930
os.environ['BZR_COLUMNS'] = '12'
2008
1931
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())
2014
1933
def test_falls_back_to_COLUMNS(self):
2015
self.overrideEnv('BZR_COLUMNS', None)
1934
del os.environ['BZR_COLUMNS']
2016
1935
self.assertNotEqual('42', os.environ['COLUMNS'])
2017
1936
self.set_fake_tty()
2018
self.overrideEnv('COLUMNS', '42')
1937
os.environ['COLUMNS'] = '42'
2019
1938
self.assertEqual(42, osutils.terminal_width())
2021
1940
def test_tty_default_without_columns(self):
2022
self.overrideEnv('BZR_COLUMNS', None)
2023
self.overrideEnv('COLUMNS', None)
1941
del os.environ['BZR_COLUMNS']
1942
del os.environ['COLUMNS']
2025
1944
def terminal_size(w, h):
2051
1970
self.overrideAttr(termios, 'TIOCGWINSZ')
2052
1971
del termios.TIOCGWINSZ
2053
self.overrideEnv('BZR_COLUMNS', None)
2054
self.overrideEnv('COLUMNS', None)
1972
del os.environ['BZR_COLUMNS']
1973
del os.environ['COLUMNS']
2055
1974
# Whatever the result is, if we don't raise an exception, it's ok.
2056
1975
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)