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
1842
1897
os.symlink(self.target, self.link)
1844
1899
def test_os_readlink_link_encoding(self):
1845
if sys.version_info < (2, 6):
1846
self.assertRaises(UnicodeEncodeError, os.readlink, self.link)
1848
self.assertEquals(self.target, os.readlink(self.link))
1900
self.assertEquals(self.target, os.readlink(self.link))
1850
1902
def test_os_readlink_link_decoding(self):
1851
1903
self.assertEquals(self.target.encode(osutils._fs_enc),
1863
1915
self.assertIsInstance(concurrency, int)
1865
1917
def test_local_concurrency_environment_variable(self):
1866
os.environ['BZR_CONCURRENCY'] = '2'
1918
self.overrideEnv('BZR_CONCURRENCY', '2')
1867
1919
self.assertEqual(2, osutils.local_concurrency(use_cache=False))
1868
os.environ['BZR_CONCURRENCY'] = '3'
1920
self.overrideEnv('BZR_CONCURRENCY', '3')
1869
1921
self.assertEqual(3, osutils.local_concurrency(use_cache=False))
1870
os.environ['BZR_CONCURRENCY'] = 'foo'
1922
self.overrideEnv('BZR_CONCURRENCY', 'foo')
1871
1923
self.assertEqual(1, osutils.local_concurrency(use_cache=False))
1873
1925
def test_option_concurrency(self):
1874
os.environ['BZR_CONCURRENCY'] = '1'
1926
self.overrideEnv('BZR_CONCURRENCY', '1')
1875
1927
self.run_bzr('rocks --concurrency 42')
1876
# Command line overrides envrionment variable
1928
# Command line overrides environment variable
1877
1929
self.assertEquals('42', os.environ['BZR_CONCURRENCY'])
1878
1930
self.assertEquals(42, osutils.local_concurrency(use_cache=False))
1918
1970
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
1920
1984
def replace_stdout(self, new):
1921
1985
self.overrideAttr(sys, 'stdout', new)
1937
2001
def test_defaults_to_BZR_COLUMNS(self):
1938
2002
# BZR_COLUMNS is set by the test framework
1939
2003
self.assertNotEqual('12', os.environ['BZR_COLUMNS'])
1940
os.environ['BZR_COLUMNS'] = '12'
2004
self.overrideEnv('BZR_COLUMNS', '12')
1941
2005
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())
1943
2011
def test_falls_back_to_COLUMNS(self):
1944
del os.environ['BZR_COLUMNS']
2012
self.overrideEnv('BZR_COLUMNS', None)
1945
2013
self.assertNotEqual('42', os.environ['COLUMNS'])
1946
2014
self.set_fake_tty()
1947
os.environ['COLUMNS'] = '42'
2015
self.overrideEnv('COLUMNS', '42')
1948
2016
self.assertEqual(42, osutils.terminal_width())
1950
2018
def test_tty_default_without_columns(self):
1951
del os.environ['BZR_COLUMNS']
1952
del os.environ['COLUMNS']
2019
self.overrideEnv('BZR_COLUMNS', None)
2020
self.overrideEnv('COLUMNS', None)
1954
2022
def terminal_size(w, h):
1962
2030
self.assertEqual(42, osutils.terminal_width())
1964
2032
def test_non_tty_default_without_columns(self):
1965
del os.environ['BZR_COLUMNS']
1966
del os.environ['COLUMNS']
2033
self.overrideEnv('BZR_COLUMNS', None)
2034
self.overrideEnv('COLUMNS', None)
1967
2035
self.replace_stdout(None)
1968
2036
self.assertEqual(None, osutils.terminal_width())
2018
2086
self.assertEquals(self.path, 'test_file')
2019
2087
self.assertEquals(self.uid, s.st_uid)
2020
2088
self.assertEquals(self.gid, s.st_gid)
2090
class TestGetuserUnicode(tests.TestCase):
2092
def test_ascii_user(self):
2093
self.overrideEnv('LOGNAME', 'jrandom')
2094
self.assertEqual(u'jrandom', osutils.getuser_unicode())
2096
def test_unicode_user(self):
2097
ue = osutils.get_user_encoding()
2098
uni_val, env_val = tests.probe_unicode_in_user_encoding()
2100
raise tests.TestSkipped(
2101
'Cannot find a unicode character that works in encoding %s'
2102
% (osutils.get_user_encoding(),))
2103
uni_username = u'jrandom' + uni_val
2104
encoded_username = uni_username.encode(ue)
2105
self.overrideEnv('LOGNAME', encoded_username)
2106
self.assertEqual(uni_username, osutils.getuser_unicode())
2107
self.overrideEnv('LOGNAME', u'jrandom\xb6'.encode(ue))
2108
self.assertEqual(u'jrandom\xb6', osutils.getuser_unicode())
2110
class TestBackupNames(tests.TestCase):
2113
super(TestBackupNames, self).setUp()
2116
def backup_exists(self, name):
2117
return name in self.backups
2119
def available_backup_name(self, name):
2120
backup_name = osutils.available_backup_name(name, self.backup_exists)
2121
self.backups.append(backup_name)
2124
def assertBackupName(self, expected, name):
2125
self.assertEqual(expected, self.available_backup_name(name))
2127
def test_empty(self):
2128
self.assertBackupName('file.~1~', 'file')
2130
def test_existing(self):
2131
self.available_backup_name('file')
2132
self.available_backup_name('file')
2133
self.assertBackupName('file.~3~', 'file')
2134
# Empty slots are found, this is not a strict requirement and may be
2135
# revisited if we test against all implementations.
2136
self.backups.remove('file.~2~')
2137
self.assertBackupName('file.~2~', 'file')
2140
class TestFindExecutableInPath(tests.TestCase):
2142
def test_windows(self):
2143
if sys.platform != 'win32':
2144
raise tests.TestSkipped('test requires win32')
2145
self.assertTrue(osutils.find_executable_on_path('explorer') is not None)
2147
osutils.find_executable_on_path('explorer.exe') is not None)
2149
osutils.find_executable_on_path('EXPLORER.EXE') is not None)
2151
osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
2152
self.assertTrue(osutils.find_executable_on_path('file.txt') is None)
2154
def test_other(self):
2155
if sys.platform == 'win32':
2156
raise tests.TestSkipped('test requires non-win32')
2157
self.assertTrue(osutils.find_executable_on_path('sh') is not None)
2159
osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)