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):
231
235
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
238
class TestRmTree(tests.TestCaseInTempDir):
255
240
def test_rmtree(self):
483
468
class TestCanonicalRelPath(tests.TestCaseInTempDir):
485
_test_needs_features = [features.CaseInsCasePresFilenameFeature]
470
_test_needs_features = [tests.CaseInsCasePresFilenameFeature]
487
472
def test_canonical_relpath_simple(self):
488
473
f = file('MixedCaseName', 'w')
490
475
actual = osutils.canonical_relpath(self.test_base_dir, 'mixedcasename')
491
self.assertEqual('work/MixedCaseName', actual)
476
self.failUnlessEqual('work/MixedCaseName', actual)
493
478
def test_canonical_relpath_missing_tail(self):
494
479
os.mkdir('MixedCaseParent')
495
480
actual = osutils.canonical_relpath(self.test_base_dir,
496
481
'mixedcaseparent/nochild')
497
self.assertEqual('work/MixedCaseParent/nochild', actual)
482
self.failUnlessEqual('work/MixedCaseParent/nochild', actual)
500
485
class Test_CICPCanonicalRelpath(tests.TestCaseWithTransport):
984
969
"""Test mac special functions that require directories."""
986
971
def test_getcwd(self):
987
self.requireFeature(features.UnicodeFilenameFeature)
972
self.requireFeature(tests.UnicodeFilenameFeature)
988
973
os.mkdir(u'B\xe5gfors')
989
974
os.chdir(u'B\xe5gfors')
990
975
self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
992
977
def test_getcwd_nonnorm(self):
993
self.requireFeature(features.UnicodeFilenameFeature)
978
self.requireFeature(tests.UnicodeFilenameFeature)
994
979
# Test that _mac_getcwd() will normalize this path
995
980
os.mkdir(u'Ba\u030agfors')
996
981
os.chdir(u'Ba\u030agfors')
1207
1189
self._save_platform_info()
1208
1190
win32utils.winver = None # Avoid the win32 detection code
1209
1191
osutils._fs_enc = 'UTF-8'
1210
self.assertDirReaderIs(
1211
UTF8DirReaderFeature.module.UTF8DirReader)
1192
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1213
1194
def test_force_walkdirs_utf8_fs_ascii(self):
1214
1195
self.requireFeature(UTF8DirReaderFeature)
1215
1196
self._save_platform_info()
1216
1197
win32utils.winver = None # Avoid the win32 detection code
1217
1198
osutils._fs_enc = 'US-ASCII'
1218
self.assertDirReaderIs(
1219
UTF8DirReaderFeature.module.UTF8DirReader)
1199
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1221
1201
def test_force_walkdirs_utf8_fs_ANSI(self):
1222
1202
self.requireFeature(UTF8DirReaderFeature)
1223
1203
self._save_platform_info()
1224
1204
win32utils.winver = None # Avoid the win32 detection code
1225
1205
osutils._fs_enc = 'ANSI_X3.4-1968'
1226
self.assertDirReaderIs(
1227
UTF8DirReaderFeature.module.UTF8DirReader)
1206
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1229
1208
def test_force_walkdirs_utf8_fs_latin1(self):
1230
1209
self._save_platform_info()
1727
1706
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
1708
def test_re_compile_checked(self):
1734
r = self._deprecated_re_compile_checked(r'A*', re.IGNORECASE)
1709
r = osutils.re_compile_checked(r'A*', re.IGNORECASE)
1735
1710
self.assertTrue(r.match('aaaa'))
1736
1711
self.assertTrue(r.match('aAaA'))
1738
1713
def test_re_compile_checked_error(self):
1739
1714
# 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
1715
err = self.assertRaises(
1745
1716
errors.BzrCommandError,
1746
self._deprecated_re_compile_checked, '*', re.IGNORECASE, 'test case')
1717
osutils.re_compile_checked, '*', re.IGNORECASE, 'test case')
1747
1718
self.assertEqual(
1748
'Invalid regular expression in test case: '
1749
'"*" nothing to repeat',
1719
"Invalid regular expression in test case: '*': "
1720
"nothing to repeat",
1753
1724
class TestDirReader(tests.TestCaseInTempDir):
1755
scenarios = dir_reader_scenarios()
1757
1726
# Set by load_tests
1758
1727
_dir_reader_class = None
1759
1728
_native_to_unicode = None
1859
1828
return filtered_dirblocks
1861
1830
def test_walk_unicode_tree(self):
1862
self.requireFeature(features.UnicodeFilenameFeature)
1831
self.requireFeature(tests.UnicodeFilenameFeature)
1863
1832
tree, expected_dirblocks = self._get_unicode_tree()
1864
1833
self.build_tree(tree)
1865
1834
result = list(osutils._walkdirs_utf8('.'))
1866
1835
self.assertEqual(expected_dirblocks, self._filter_out(result))
1868
1837
def test_symlink(self):
1869
self.requireFeature(features.SymlinkFeature)
1870
self.requireFeature(features.UnicodeFilenameFeature)
1838
self.requireFeature(tests.SymlinkFeature)
1839
self.requireFeature(tests.UnicodeFilenameFeature)
1871
1840
target = u'target\N{Euro Sign}'
1872
1841
link_name = u'l\N{Euro Sign}nk'
1873
1842
os.symlink(target, link_name)
1900
1869
os.symlink(self.target, self.link)
1902
1871
def test_os_readlink_link_encoding(self):
1903
self.assertEquals(self.target, os.readlink(self.link))
1872
if sys.version_info < (2, 6):
1873
self.assertRaises(UnicodeEncodeError, os.readlink, self.link)
1875
self.assertEquals(self.target, os.readlink(self.link))
1905
1877
def test_os_readlink_link_decoding(self):
1906
1878
self.assertEquals(self.target.encode(osutils._fs_enc),
1918
1890
self.assertIsInstance(concurrency, int)
1920
1892
def test_local_concurrency_environment_variable(self):
1921
self.overrideEnv('BZR_CONCURRENCY', '2')
1893
os.environ['BZR_CONCURRENCY'] = '2'
1922
1894
self.assertEqual(2, osutils.local_concurrency(use_cache=False))
1923
self.overrideEnv('BZR_CONCURRENCY', '3')
1895
os.environ['BZR_CONCURRENCY'] = '3'
1924
1896
self.assertEqual(3, osutils.local_concurrency(use_cache=False))
1925
self.overrideEnv('BZR_CONCURRENCY', 'foo')
1897
os.environ['BZR_CONCURRENCY'] = 'foo'
1926
1898
self.assertEqual(1, osutils.local_concurrency(use_cache=False))
1928
1900
def test_option_concurrency(self):
1929
self.overrideEnv('BZR_CONCURRENCY', '1')
1901
os.environ['BZR_CONCURRENCY'] = '1'
1930
1902
self.run_bzr('rocks --concurrency 42')
1931
# Command line overrides environment variable
1903
# Command line overrides envrionment variable
1932
1904
self.assertEquals('42', os.environ['BZR_CONCURRENCY'])
1933
1905
self.assertEquals(42, osutils.local_concurrency(use_cache=False))
2004
1976
def test_defaults_to_BZR_COLUMNS(self):
2005
1977
# BZR_COLUMNS is set by the test framework
2006
1978
self.assertNotEqual('12', os.environ['BZR_COLUMNS'])
2007
self.overrideEnv('BZR_COLUMNS', '12')
1979
os.environ['BZR_COLUMNS'] = '12'
2008
1980
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
1982
def test_falls_back_to_COLUMNS(self):
2015
self.overrideEnv('BZR_COLUMNS', None)
1983
del os.environ['BZR_COLUMNS']
2016
1984
self.assertNotEqual('42', os.environ['COLUMNS'])
2017
1985
self.set_fake_tty()
2018
self.overrideEnv('COLUMNS', '42')
1986
os.environ['COLUMNS'] = '42'
2019
1987
self.assertEqual(42, osutils.terminal_width())
2021
1989
def test_tty_default_without_columns(self):
2022
self.overrideEnv('BZR_COLUMNS', None)
2023
self.overrideEnv('COLUMNS', None)
1990
del os.environ['BZR_COLUMNS']
1991
del os.environ['COLUMNS']
2025
1993
def terminal_size(w, h):
2051
2019
self.overrideAttr(termios, 'TIOCGWINSZ')
2052
2020
del termios.TIOCGWINSZ
2053
self.overrideEnv('BZR_COLUMNS', None)
2054
self.overrideEnv('COLUMNS', None)
2021
del os.environ['BZR_COLUMNS']
2022
del os.environ['COLUMNS']
2055
2023
# Whatever the result is, if we don't raise an exception, it's ok.
2056
2024
osutils.terminal_width()
2059
2026
class TestCreationOps(tests.TestCaseInTempDir):
2060
2027
_test_needs_features = [features.chown_feature]
2091
2058
self.assertEquals(self.uid, s.st_uid)
2092
2059
self.assertEquals(self.gid, s.st_gid)
2095
2061
class TestGetuserUnicode(tests.TestCase):
2097
2063
def test_ascii_user(self):
2098
self.overrideEnv('LOGNAME', 'jrandom')
2064
osutils.set_or_unset_env('LOGNAME', 'jrandom')
2099
2065
self.assertEqual(u'jrandom', osutils.getuser_unicode())
2101
2067
def test_unicode_user(self):
2102
2068
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))
2069
osutils.set_or_unset_env('LOGNAME', u'jrandom\xb6'.encode(ue))
2113
2070
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)