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
1157
self._save_platform_info()
1208
1158
win32utils.winver = None # Avoid the win32 detection code
1209
1159
osutils._fs_enc = 'UTF-8'
1210
self.assertDirReaderIs(
1211
UTF8DirReaderFeature.module.UTF8DirReader)
1160
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1213
1162
def test_force_walkdirs_utf8_fs_ascii(self):
1214
1163
self.requireFeature(UTF8DirReaderFeature)
1215
1164
self._save_platform_info()
1216
1165
win32utils.winver = None # Avoid the win32 detection code
1217
1166
osutils._fs_enc = 'US-ASCII'
1218
self.assertDirReaderIs(
1219
UTF8DirReaderFeature.module.UTF8DirReader)
1167
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1221
1169
def test_force_walkdirs_utf8_fs_ANSI(self):
1222
1170
self.requireFeature(UTF8DirReaderFeature)
1223
1171
self._save_platform_info()
1224
1172
win32utils.winver = None # Avoid the win32 detection code
1225
1173
osutils._fs_enc = 'ANSI_X3.4-1968'
1226
self.assertDirReaderIs(
1227
UTF8DirReaderFeature.module.UTF8DirReader)
1174
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1229
1176
def test_force_walkdirs_utf8_fs_latin1(self):
1230
1177
self._save_platform_info()
1727
1675
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
1677
def test_re_compile_checked(self):
1734
r = self._deprecated_re_compile_checked(r'A*', re.IGNORECASE)
1678
r = osutils.re_compile_checked(r'A*', re.IGNORECASE)
1735
1679
self.assertTrue(r.match('aaaa'))
1736
1680
self.assertTrue(r.match('aAaA'))
1738
1682
def test_re_compile_checked_error(self):
1739
1683
# 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
1684
err = self.assertRaises(
1745
1685
errors.BzrCommandError,
1746
self._deprecated_re_compile_checked, '*', re.IGNORECASE, 'test case')
1686
osutils.re_compile_checked, '*', re.IGNORECASE, 'test case')
1747
1687
self.assertEqual(
1748
'Invalid regular expression in test case: '
1749
'"*" nothing to repeat',
1688
"Invalid regular expression in test case: '*': "
1689
"nothing to repeat",
1753
1693
class TestDirReader(tests.TestCaseInTempDir):
1755
scenarios = dir_reader_scenarios()
1757
1695
# Set by load_tests
1758
1696
_dir_reader_class = None
1759
1697
_native_to_unicode = None
1761
1699
def setUp(self):
1762
1700
tests.TestCaseInTempDir.setUp(self)
1763
self.overrideAttr(osutils,
1764
'_selected_dir_reader', self._dir_reader_class())
1702
# Save platform specific info and reset it
1703
cur_dir_reader = osutils._selected_dir_reader
1706
osutils._selected_dir_reader = cur_dir_reader
1707
self.addCleanup(restore)
1709
osutils._selected_dir_reader = self._dir_reader_class()
1766
1711
def _get_ascii_tree(self):
1912
1860
def setUp(self):
1913
1861
super(TestConcurrency, self).setUp()
1914
self.overrideAttr(osutils, '_cached_local_concurrency')
1862
orig = osutils._cached_local_concurrency
1864
osutils._cached_local_concurrency = orig
1865
self.addCleanup(restore)
1916
1867
def test_local_concurrency(self):
1917
1868
concurrency = osutils.local_concurrency()
1918
1869
self.assertIsInstance(concurrency, int)
1920
1871
def test_local_concurrency_environment_variable(self):
1921
self.overrideEnv('BZR_CONCURRENCY', '2')
1872
os.environ['BZR_CONCURRENCY'] = '2'
1922
1873
self.assertEqual(2, osutils.local_concurrency(use_cache=False))
1923
self.overrideEnv('BZR_CONCURRENCY', '3')
1874
os.environ['BZR_CONCURRENCY'] = '3'
1924
1875
self.assertEqual(3, osutils.local_concurrency(use_cache=False))
1925
self.overrideEnv('BZR_CONCURRENCY', 'foo')
1876
os.environ['BZR_CONCURRENCY'] = 'foo'
1926
1877
self.assertEqual(1, osutils.local_concurrency(use_cache=False))
1928
1879
def test_option_concurrency(self):
1929
self.overrideEnv('BZR_CONCURRENCY', '1')
1880
os.environ['BZR_CONCURRENCY'] = '1'
1930
1881
self.run_bzr('rocks --concurrency 42')
1931
# Command line overrides environment variable
1882
# Command line overrides envrionment variable
1932
1883
self.assertEquals('42', os.environ['BZR_CONCURRENCY'])
1933
1884
self.assertEquals(42, osutils.local_concurrency(use_cache=False))
1973
1929
class TestTerminalWidth(tests.TestCase):
1976
tests.TestCase.setUp(self)
1977
self._orig_terminal_size_state = osutils._terminal_size_state
1978
self._orig_first_terminal_size = osutils._first_terminal_size
1979
self.addCleanup(self.restore_osutils_globals)
1980
osutils._terminal_size_state = 'no_data'
1981
osutils._first_terminal_size = None
1983
def restore_osutils_globals(self):
1984
osutils._terminal_size_state = self._orig_terminal_size_state
1985
osutils._first_terminal_size = self._orig_first_terminal_size
1987
1931
def replace_stdout(self, new):
1988
self.overrideAttr(sys, 'stdout', new)
1932
orig_stdout = sys.stdout
1934
sys.stdout = orig_stdout
1935
self.addCleanup(restore)
1990
1938
def replace__terminal_size(self, new):
1991
self.overrideAttr(osutils, '_terminal_size', new)
1939
orig__terminal_size = osutils._terminal_size
1941
osutils._terminal_size = orig__terminal_size
1942
self.addCleanup(restore)
1943
osutils._terminal_size = new
1993
1945
def set_fake_tty(self):
2004
1956
def test_defaults_to_BZR_COLUMNS(self):
2005
1957
# BZR_COLUMNS is set by the test framework
2006
1958
self.assertNotEqual('12', os.environ['BZR_COLUMNS'])
2007
self.overrideEnv('BZR_COLUMNS', '12')
1959
os.environ['BZR_COLUMNS'] = '12'
2008
1960
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
1962
def test_falls_back_to_COLUMNS(self):
2015
self.overrideEnv('BZR_COLUMNS', None)
1963
del os.environ['BZR_COLUMNS']
2016
1964
self.assertNotEqual('42', os.environ['COLUMNS'])
2017
1965
self.set_fake_tty()
2018
self.overrideEnv('COLUMNS', '42')
1966
os.environ['COLUMNS'] = '42'
2019
1967
self.assertEqual(42, osutils.terminal_width())
2021
1969
def test_tty_default_without_columns(self):
2022
self.overrideEnv('BZR_COLUMNS', None)
2023
self.overrideEnv('COLUMNS', None)
1970
del os.environ['BZR_COLUMNS']
1971
del os.environ['COLUMNS']
2025
1973
def terminal_size(w, h):
2048
1996
# We won't remove TIOCGWINSZ, because it doesn't exist anyway :)
2051
self.overrideAttr(termios, 'TIOCGWINSZ')
2000
termios.TIOCGWINSZ = orig
2001
self.addCleanup(restore)
2052
2002
del termios.TIOCGWINSZ
2053
self.overrideEnv('BZR_COLUMNS', None)
2054
self.overrideEnv('COLUMNS', None)
2003
del os.environ['BZR_COLUMNS']
2004
del os.environ['COLUMNS']
2055
2005
# Whatever the result is, if we don't raise an exception, it's ok.
2056
2006
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)