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)
100
load_tests = load_tests_apply_scenarios
107
103
class TestContainsWhitespace(tests.TestCase):
109
105
def test_contains_whitespace(self):
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'))
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'))
117
113
# \xa0 is "Non-breaking-space" which on some python locales thinks it
118
114
# is whitespace, but we do not.
119
self.failIf(osutils.contains_whitespace(u''))
120
self.failIf(osutils.contains_whitespace(u'hellothere'))
121
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'))
124
120
class TestRename(tests.TestCaseInTempDir):
138
134
# This should work everywhere
139
135
self.create_file('a', 'something in a\n')
140
136
self._fancy_rename('a', 'b')
141
self.failIfExists('a')
142
self.failUnlessExists('b')
137
self.assertPathDoesNotExist('a')
138
self.assertPathExists('b')
143
139
self.check_file_contents('b', 'something in a\n')
145
141
self.create_file('a', 'new something in a\n')
152
148
self.create_file('target', 'data in target\n')
153
149
self.assertRaises((IOError, OSError), self._fancy_rename,
154
150
'missingsource', 'target')
155
self.failUnlessExists('target')
151
self.assertPathExists('target')
156
152
self.check_file_contents('target', 'data in target\n')
158
154
def test_fancy_rename_fails_if_source_and_target_missing(self):
163
159
# Rename should be semi-atomic on all platforms
164
160
self.create_file('a', 'something in a\n')
165
161
osutils.rename('a', 'b')
166
self.failIfExists('a')
167
self.failUnlessExists('b')
162
self.assertPathDoesNotExist('a')
163
self.assertPathExists('b')
168
164
self.check_file_contents('b', 'something in a\n')
170
166
self.create_file('a', 'new something in a\n')
184
180
shape = sorted(os.listdir('.'))
185
181
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')
195
184
class TestRandChars(tests.TestCase):
242
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'))
245
253
class TestRmTree(tests.TestCaseInTempDir):
247
255
def test_rmtree(self):
480
488
f = file('MixedCaseName', 'w')
482
490
actual = osutils.canonical_relpath(self.test_base_dir, 'mixedcasename')
483
self.failUnlessEqual('work/MixedCaseName', actual)
491
self.assertEqual('work/MixedCaseName', actual)
485
493
def test_canonical_relpath_missing_tail(self):
486
494
os.mkdir('MixedCaseParent')
487
495
actual = osutils.canonical_relpath(self.test_base_dir,
488
496
'mixedcaseparent/nochild')
489
self.failUnlessEqual('work/MixedCaseParent/nochild', actual)
497
self.assertEqual('work/MixedCaseParent/nochild', actual)
492
500
class Test_CICPCanonicalRelpath(tests.TestCaseWithTransport):
1071
1079
self.assertExpectedBlocks(expected_dirblocks[1:], result)
1073
1081
def test_walkdirs_os_error(self):
1074
# <https://bugs.edge.launchpad.net/bzr/+bug/338653>
1082
# <https://bugs.launchpad.net/bzr/+bug/338653>
1075
1083
# Pyrex readdir didn't raise useful messages if it had an error
1076
1084
# reading the directory
1077
1085
if sys.platform == 'win32':
1078
1086
raise tests.TestNotApplicable(
1079
1087
"readdir IOError not tested on win32")
1088
self.requireFeature(features.not_running_as_root)
1080
1089
os.mkdir("test-unreadable")
1081
1090
os.chmod("test-unreadable", 0000)
1082
1091
# must chmod it back so that it can be removed
1090
1099
# Ensure the message contains the file name
1091
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(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)
1093
1138
def test__walkdirs_utf8(self):
1569
1614
('d', 'source/b', 'target/b'),
1570
1615
('f', 'source/b/c', 'target/b/c'),
1571
1616
], processed_files)
1572
self.failIfExists('target')
1617
self.assertPathDoesNotExist('target')
1573
1618
if osutils.has_symlinks():
1574
1619
self.assertEqual([('source/lnk', 'target/lnk')], processed_links)
1679
1724
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)
1681
1730
def test_re_compile_checked(self):
1682
r = osutils.re_compile_checked(r'A*', re.IGNORECASE)
1731
r = self._deprecated_re_compile_checked(r'A*', re.IGNORECASE)
1683
1732
self.assertTrue(r.match('aaaa'))
1684
1733
self.assertTrue(r.match('aAaA'))
1686
1735
def test_re_compile_checked_error(self):
1687
1736
# 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()
1688
1741
err = self.assertRaises(
1689
1742
errors.BzrCommandError,
1690
osutils.re_compile_checked, '*', re.IGNORECASE, 'test case')
1743
self._deprecated_re_compile_checked, '*', re.IGNORECASE, 'test case')
1691
1744
self.assertEqual(
1692
"Invalid regular expression in test case: '*': "
1693
"nothing to repeat",
1745
'Invalid regular expression in test case: '
1746
'"*" nothing to repeat',
1697
1750
class TestDirReader(tests.TestCaseInTempDir):
1752
scenarios = dir_reader_scenarios()
1699
1754
# Set by load_tests
1700
1755
_dir_reader_class = None
1701
1756
_native_to_unicode = None
1863
1918
self.assertIsInstance(concurrency, int)
1865
1920
def test_local_concurrency_environment_variable(self):
1866
os.environ['BZR_CONCURRENCY'] = '2'
1921
self.overrideEnv('BZR_CONCURRENCY', '2')
1867
1922
self.assertEqual(2, osutils.local_concurrency(use_cache=False))
1868
os.environ['BZR_CONCURRENCY'] = '3'
1923
self.overrideEnv('BZR_CONCURRENCY', '3')
1869
1924
self.assertEqual(3, osutils.local_concurrency(use_cache=False))
1870
os.environ['BZR_CONCURRENCY'] = 'foo'
1925
self.overrideEnv('BZR_CONCURRENCY', 'foo')
1871
1926
self.assertEqual(1, osutils.local_concurrency(use_cache=False))
1873
1928
def test_option_concurrency(self):
1874
os.environ['BZR_CONCURRENCY'] = '1'
1929
self.overrideEnv('BZR_CONCURRENCY', '1')
1875
1930
self.run_bzr('rocks --concurrency 42')
1876
# Command line overrides envrionment variable
1931
# Command line overrides environment variable
1877
1932
self.assertEquals('42', os.environ['BZR_CONCURRENCY'])
1878
1933
self.assertEquals(42, osutils.local_concurrency(use_cache=False))
1918
1973
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
1920
1987
def replace_stdout(self, new):
1921
1988
self.overrideAttr(sys, 'stdout', new)
1937
2004
def test_defaults_to_BZR_COLUMNS(self):
1938
2005
# BZR_COLUMNS is set by the test framework
1939
2006
self.assertNotEqual('12', os.environ['BZR_COLUMNS'])
1940
os.environ['BZR_COLUMNS'] = '12'
2007
self.overrideEnv('BZR_COLUMNS', '12')
1941
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())
1943
2014
def test_falls_back_to_COLUMNS(self):
1944
del os.environ['BZR_COLUMNS']
2015
self.overrideEnv('BZR_COLUMNS', None)
1945
2016
self.assertNotEqual('42', os.environ['COLUMNS'])
1946
2017
self.set_fake_tty()
1947
os.environ['COLUMNS'] = '42'
2018
self.overrideEnv('COLUMNS', '42')
1948
2019
self.assertEqual(42, osutils.terminal_width())
1950
2021
def test_tty_default_without_columns(self):
1951
del os.environ['BZR_COLUMNS']
1952
del os.environ['COLUMNS']
2022
self.overrideEnv('BZR_COLUMNS', None)
2023
self.overrideEnv('COLUMNS', None)
1954
2025
def terminal_size(w, h):
1962
2033
self.assertEqual(42, osutils.terminal_width())
1964
2035
def test_non_tty_default_without_columns(self):
1965
del os.environ['BZR_COLUMNS']
1966
del os.environ['COLUMNS']
2036
self.overrideEnv('BZR_COLUMNS', None)
2037
self.overrideEnv('COLUMNS', None)
1967
2038
self.replace_stdout(None)
1968
2039
self.assertEqual(None, osutils.terminal_width())
2018
2089
self.assertEquals(self.path, 'test_file')
2019
2090
self.assertEquals(self.uid, s.st_uid)
2020
2091
self.assertEquals(self.gid, s.st_gid)
2093
class TestGetuserUnicode(tests.TestCase):
2095
def test_ascii_user(self):
2096
self.overrideEnv('LOGNAME', 'jrandom')
2097
self.assertEqual(u'jrandom', osutils.getuser_unicode())
2099
def test_unicode_user(self):
2100
ue = osutils.get_user_encoding()
2101
uni_val, env_val = tests.probe_unicode_in_user_encoding()
2103
raise tests.TestSkipped(
2104
'Cannot find a unicode character that works in encoding %s'
2105
% (osutils.get_user_encoding(),))
2106
uni_username = u'jrandom' + uni_val
2107
encoded_username = uni_username.encode(ue)
2108
self.overrideEnv('LOGNAME', encoded_username)
2109
self.assertEqual(uni_username, osutils.getuser_unicode())
2110
self.overrideEnv('LOGNAME', u'jrandom\xb6'.encode(ue))
2111
self.assertEqual(u'jrandom\xb6', osutils.getuser_unicode())
2113
class TestBackupNames(tests.TestCase):
2116
super(TestBackupNames, self).setUp()
2119
def backup_exists(self, name):
2120
return name in self.backups
2122
def available_backup_name(self, name):
2123
backup_name = osutils.available_backup_name(name, self.backup_exists)
2124
self.backups.append(backup_name)
2127
def assertBackupName(self, expected, name):
2128
self.assertEqual(expected, self.available_backup_name(name))
2130
def test_empty(self):
2131
self.assertBackupName('file.~1~', 'file')
2133
def test_existing(self):
2134
self.available_backup_name('file')
2135
self.available_backup_name('file')
2136
self.assertBackupName('file.~3~', 'file')
2137
# Empty slots are found, this is not a strict requirement and may be
2138
# revisited if we test against all implementations.
2139
self.backups.remove('file.~2~')
2140
self.assertBackupName('file.~2~', 'file')
2143
class TestFindExecutableInPath(tests.TestCase):
2145
def test_windows(self):
2146
if sys.platform != 'win32':
2147
raise tests.TestSkipped('test requires win32')
2148
self.assertTrue(osutils.find_executable_on_path('explorer') is not None)
2150
osutils.find_executable_on_path('explorer.exe') is not None)
2152
osutils.find_executable_on_path('EXPLORER.EXE') is not None)
2154
osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
2155
self.assertTrue(osutils.find_executable_on_path('file.txt') is None)
2157
def test_other(self):
2158
if sys.platform == 'win32':
2159
raise tests.TestSkipped('test requires non-win32')
2160
self.assertTrue(osutils.find_executable_on_path('sh') is not None)
2162
osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)