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):
148
152
self.create_file('target', 'data in target\n')
149
153
self.assertRaises((IOError, OSError), self._fancy_rename,
150
154
'missingsource', 'target')
151
self.assertPathExists('target')
155
self.failUnlessExists('target')
152
156
self.check_file_contents('target', 'data in target\n')
154
158
def test_fancy_rename_fails_if_source_and_target_missing(self):
180
184
shape = sorted(os.listdir('.'))
181
185
self.assertEquals(['A', 'B'], shape)
187
def test_rename_error(self):
188
# We wrap os.rename to make it give an error including the filenames
189
# https://bugs.launchpad.net/bzr/+bug/491763
190
err = self.assertRaises(OSError, osutils.rename,
191
'nonexistent', 'target')
192
self.assertContainsString(str(err), 'nonexistent')
184
195
class TestRandChars(tests.TestCase):
231
242
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
245
class TestRmTree(tests.TestCaseInTempDir):
255
247
def test_rmtree(self):
488
480
f = file('MixedCaseName', 'w')
490
482
actual = osutils.canonical_relpath(self.test_base_dir, 'mixedcasename')
491
self.assertEqual('work/MixedCaseName', actual)
483
self.failUnlessEqual('work/MixedCaseName', actual)
493
485
def test_canonical_relpath_missing_tail(self):
494
486
os.mkdir('MixedCaseParent')
495
487
actual = osutils.canonical_relpath(self.test_base_dir,
496
488
'mixedcaseparent/nochild')
497
self.assertEqual('work/MixedCaseParent/nochild', actual)
489
self.failUnlessEqual('work/MixedCaseParent/nochild', actual)
500
492
class Test_CICPCanonicalRelpath(tests.TestCaseWithTransport):
1079
1071
self.assertExpectedBlocks(expected_dirblocks[1:], result)
1081
1073
def test_walkdirs_os_error(self):
1082
# <https://bugs.launchpad.net/bzr/+bug/338653>
1074
# <https://bugs.edge.launchpad.net/bzr/+bug/338653>
1083
1075
# Pyrex readdir didn't raise useful messages if it had an error
1084
1076
# reading the directory
1085
1077
if sys.platform == 'win32':
1086
1078
raise tests.TestNotApplicable(
1087
1079
"readdir IOError not tested on win32")
1088
self.requireFeature(features.not_running_as_root)
1089
1080
os.mkdir("test-unreadable")
1090
1081
os.chmod("test-unreadable", 0000)
1091
1082
# must chmod it back so that it can be removed
1099
1090
# Ensure the message contains the file name
1100
1091
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(tests.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
1093
def test__walkdirs_utf8(self):
1724
1679
class TestReCompile(tests.TestCase):
1726
def _deprecated_re_compile_checked(self, *args, **kwargs):
1727
return self.applyDeprecated(symbol_versioning.deprecated_in((2, 2, 0)),
1728
osutils.re_compile_checked, *args, **kwargs)
1730
1681
def test_re_compile_checked(self):
1731
r = self._deprecated_re_compile_checked(r'A*', re.IGNORECASE)
1682
r = osutils.re_compile_checked(r'A*', re.IGNORECASE)
1732
1683
self.assertTrue(r.match('aaaa'))
1733
1684
self.assertTrue(r.match('aAaA'))
1735
1686
def test_re_compile_checked_error(self):
1736
1687
# like https://bugs.launchpad.net/bzr/+bug/251352
1738
# Due to possible test isolation error, re.compile is not lazy at
1739
# this point. We re-install lazy compile.
1740
lazy_regex.install_lazy_compile()
1741
1688
err = self.assertRaises(
1742
1689
errors.BzrCommandError,
1743
self._deprecated_re_compile_checked, '*', re.IGNORECASE, 'test case')
1690
osutils.re_compile_checked, '*', re.IGNORECASE, 'test case')
1744
1691
self.assertEqual(
1745
'Invalid regular expression in test case: '
1746
'"*" nothing to repeat',
1692
"Invalid regular expression in test case: '*': "
1693
"nothing to repeat",
1750
1697
class TestDirReader(tests.TestCaseInTempDir):
1752
scenarios = dir_reader_scenarios()
1754
1699
# Set by load_tests
1755
1700
_dir_reader_class = None
1756
1701
_native_to_unicode = None
1897
1842
os.symlink(self.target, self.link)
1899
1844
def test_os_readlink_link_encoding(self):
1900
self.assertEquals(self.target, os.readlink(self.link))
1845
if sys.version_info < (2, 6):
1846
self.assertRaises(UnicodeEncodeError, os.readlink, self.link)
1848
self.assertEquals(self.target, os.readlink(self.link))
1902
1850
def test_os_readlink_link_decoding(self):
1903
1851
self.assertEquals(self.target.encode(osutils._fs_enc),
1915
1863
self.assertIsInstance(concurrency, int)
1917
1865
def test_local_concurrency_environment_variable(self):
1918
self.overrideEnv('BZR_CONCURRENCY', '2')
1866
os.environ['BZR_CONCURRENCY'] = '2'
1919
1867
self.assertEqual(2, osutils.local_concurrency(use_cache=False))
1920
self.overrideEnv('BZR_CONCURRENCY', '3')
1868
os.environ['BZR_CONCURRENCY'] = '3'
1921
1869
self.assertEqual(3, osutils.local_concurrency(use_cache=False))
1922
self.overrideEnv('BZR_CONCURRENCY', 'foo')
1870
os.environ['BZR_CONCURRENCY'] = 'foo'
1923
1871
self.assertEqual(1, osutils.local_concurrency(use_cache=False))
1925
1873
def test_option_concurrency(self):
1926
self.overrideEnv('BZR_CONCURRENCY', '1')
1874
os.environ['BZR_CONCURRENCY'] = '1'
1927
1875
self.run_bzr('rocks --concurrency 42')
1928
# Command line overrides environment variable
1876
# Command line overrides envrionment variable
1929
1877
self.assertEquals('42', os.environ['BZR_CONCURRENCY'])
1930
1878
self.assertEquals(42, osutils.local_concurrency(use_cache=False))
1970
1918
class TestTerminalWidth(tests.TestCase):
1973
tests.TestCase.setUp(self)
1974
self._orig_terminal_size_state = osutils._terminal_size_state
1975
self._orig_first_terminal_size = osutils._first_terminal_size
1976
self.addCleanup(self.restore_osutils_globals)
1977
osutils._terminal_size_state = 'no_data'
1978
osutils._first_terminal_size = None
1980
def restore_osutils_globals(self):
1981
osutils._terminal_size_state = self._orig_terminal_size_state
1982
osutils._first_terminal_size = self._orig_first_terminal_size
1984
1920
def replace_stdout(self, new):
1985
1921
self.overrideAttr(sys, 'stdout', new)
2001
1937
def test_defaults_to_BZR_COLUMNS(self):
2002
1938
# BZR_COLUMNS is set by the test framework
2003
1939
self.assertNotEqual('12', os.environ['BZR_COLUMNS'])
2004
self.overrideEnv('BZR_COLUMNS', '12')
1940
os.environ['BZR_COLUMNS'] = '12'
2005
1941
self.assertEqual(12, osutils.terminal_width())
2007
def test_BZR_COLUMNS_0_no_limit(self):
2008
self.overrideEnv('BZR_COLUMNS', '0')
2009
self.assertEqual(None, osutils.terminal_width())
2011
1943
def test_falls_back_to_COLUMNS(self):
2012
self.overrideEnv('BZR_COLUMNS', None)
1944
del os.environ['BZR_COLUMNS']
2013
1945
self.assertNotEqual('42', os.environ['COLUMNS'])
2014
1946
self.set_fake_tty()
2015
self.overrideEnv('COLUMNS', '42')
1947
os.environ['COLUMNS'] = '42'
2016
1948
self.assertEqual(42, osutils.terminal_width())
2018
1950
def test_tty_default_without_columns(self):
2019
self.overrideEnv('BZR_COLUMNS', None)
2020
self.overrideEnv('COLUMNS', None)
1951
del os.environ['BZR_COLUMNS']
1952
del os.environ['COLUMNS']
2022
1954
def terminal_size(w, h):
2048
1980
self.overrideAttr(termios, 'TIOCGWINSZ')
2049
1981
del termios.TIOCGWINSZ
2050
self.overrideEnv('BZR_COLUMNS', None)
2051
self.overrideEnv('COLUMNS', None)
1982
del os.environ['BZR_COLUMNS']
1983
del os.environ['COLUMNS']
2052
1984
# Whatever the result is, if we don't raise an exception, it's ok.
2053
1985
osutils.terminal_width()
2056
1987
class TestCreationOps(tests.TestCaseInTempDir):
2057
1988
_test_needs_features = [features.chown_feature]
2087
2018
self.assertEquals(self.path, 'test_file')
2088
2019
self.assertEquals(self.uid, s.st_uid)
2089
2020
self.assertEquals(self.gid, s.st_gid)
2092
class TestGetuserUnicode(tests.TestCase):
2094
def test_ascii_user(self):
2095
self.overrideEnv('LOGNAME', 'jrandom')
2096
self.assertEqual(u'jrandom', osutils.getuser_unicode())
2098
def test_unicode_user(self):
2099
ue = osutils.get_user_encoding()
2100
uni_val, env_val = tests.probe_unicode_in_user_encoding()
2102
raise tests.TestSkipped(
2103
'Cannot find a unicode character that works in encoding %s'
2104
% (osutils.get_user_encoding(),))
2105
uni_username = u'jrandom' + uni_val
2106
encoded_username = uni_username.encode(ue)
2107
self.overrideEnv('LOGNAME', encoded_username)
2108
self.assertEqual(uni_username, osutils.getuser_unicode())
2109
self.overrideEnv('LOGNAME', u'jrandom\xb6'.encode(ue))
2110
self.assertEqual(u'jrandom\xb6', osutils.getuser_unicode())
2112
def test_no_username_bug_660174(self):
2113
self.requireFeature(features.win32_feature)
2114
for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
2115
self.overrideEnv(name, None)
2116
self.assertEqual(u'UNKNOWN', osutils.getuser_unicode())
2119
class TestBackupNames(tests.TestCase):
2122
super(TestBackupNames, self).setUp()
2125
def backup_exists(self, name):
2126
return name in self.backups
2128
def available_backup_name(self, name):
2129
backup_name = osutils.available_backup_name(name, self.backup_exists)
2130
self.backups.append(backup_name)
2133
def assertBackupName(self, expected, name):
2134
self.assertEqual(expected, self.available_backup_name(name))
2136
def test_empty(self):
2137
self.assertBackupName('file.~1~', 'file')
2139
def test_existing(self):
2140
self.available_backup_name('file')
2141
self.available_backup_name('file')
2142
self.assertBackupName('file.~3~', 'file')
2143
# Empty slots are found, this is not a strict requirement and may be
2144
# revisited if we test against all implementations.
2145
self.backups.remove('file.~2~')
2146
self.assertBackupName('file.~2~', 'file')
2149
class TestFindExecutableInPath(tests.TestCase):
2151
def test_windows(self):
2152
if sys.platform != 'win32':
2153
raise tests.TestSkipped('test requires win32')
2154
self.assertTrue(osutils.find_executable_on_path('explorer') is not None)
2156
osutils.find_executable_on_path('explorer.exe') is not None)
2158
osutils.find_executable_on_path('EXPLORER.EXE') is not None)
2160
osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
2161
self.assertTrue(osutils.find_executable_on_path('file.txt') is None)
2163
def test_other(self):
2164
if sys.platform == 'win32':
2165
raise tests.TestSkipped('test requires non-win32')
2166
self.assertTrue(osutils.find_executable_on_path('sh') is not None)
2168
osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)