86
83
dict(_dir_reader_class=_readdir_pyx.UTF8DirReader,
87
84
_native_to_unicode=_utf8_to_unicode)))
89
if test__walkdirs_win32.win32_readdir_feature.available():
86
if test__walkdirs_win32.Win32ReadDirFeature.available():
91
88
from bzrlib import _walkdirs_win32
89
# TODO: check on windows, it may be that we need to use/add
90
# safe_unicode instead of _fs_enc_to_unicode
94
93
dict(_dir_reader_class=_walkdirs_win32.Win32ReadDir,
95
_native_to_unicode=_already_unicode)))
94
_native_to_unicode=_fs_enc_to_unicode)))
96
95
except ImportError:
101
load_tests = load_tests_apply_scenarios
100
def load_tests(basic_tests, module, loader):
101
suite = loader.suiteClass()
102
dir_reader_tests, remaining_tests = tests.split_suite_by_condition(
103
basic_tests, tests.condition_isinstance(TestDirReader))
104
tests.multiply_tests(dir_reader_tests, dir_reader_scenarios(), suite)
105
suite.addTest(remaining_tests)
104
109
class TestContainsWhitespace(tests.TestCase):
106
111
def test_contains_whitespace(self):
107
self.assertTrue(osutils.contains_whitespace(u' '))
108
self.assertTrue(osutils.contains_whitespace(u'hello there'))
109
self.assertTrue(osutils.contains_whitespace(u'hellothere\n'))
110
self.assertTrue(osutils.contains_whitespace(u'hello\nthere'))
111
self.assertTrue(osutils.contains_whitespace(u'hello\rthere'))
112
self.assertTrue(osutils.contains_whitespace(u'hello\tthere'))
112
self.failUnless(osutils.contains_whitespace(u' '))
113
self.failUnless(osutils.contains_whitespace(u'hello there'))
114
self.failUnless(osutils.contains_whitespace(u'hellothere\n'))
115
self.failUnless(osutils.contains_whitespace(u'hello\nthere'))
116
self.failUnless(osutils.contains_whitespace(u'hello\rthere'))
117
self.failUnless(osutils.contains_whitespace(u'hello\tthere'))
114
119
# \xa0 is "Non-breaking-space" which on some python locales thinks it
115
120
# is whitespace, but we do not.
116
self.assertFalse(osutils.contains_whitespace(u''))
117
self.assertFalse(osutils.contains_whitespace(u'hellothere'))
118
self.assertFalse(osutils.contains_whitespace(u'hello\xa0there'))
121
self.failIf(osutils.contains_whitespace(u''))
122
self.failIf(osutils.contains_whitespace(u'hellothere'))
123
self.failIf(osutils.contains_whitespace(u'hello\xa0there'))
121
126
class TestRename(tests.TestCaseInTempDir):
123
def create_file(self, filename, content):
124
f = open(filename, 'wb')
130
def _fancy_rename(self, a, b):
131
osutils.fancy_rename(a, b, rename_func=os.rename,
132
unlink_func=os.unlink)
134
128
def test_fancy_rename(self):
135
129
# This should work everywhere
136
self.create_file('a', 'something in a\n')
137
self._fancy_rename('a', 'b')
138
self.assertPathDoesNotExist('a')
139
self.assertPathExists('b')
131
osutils.fancy_rename(a, b,
132
rename_func=os.rename,
133
unlink_func=os.unlink)
135
open('a', 'wb').write('something in a\n')
137
self.failIfExists('a')
138
self.failUnlessExists('b')
140
139
self.check_file_contents('b', 'something in a\n')
142
self.create_file('a', 'new something in a\n')
143
self._fancy_rename('b', 'a')
141
open('a', 'wb').write('new something in a\n')
145
144
self.check_file_contents('a', 'something in a\n')
147
def test_fancy_rename_fails_source_missing(self):
148
# An exception should be raised, and the target should be left in place
149
self.create_file('target', 'data in target\n')
150
self.assertRaises((IOError, OSError), self._fancy_rename,
151
'missingsource', 'target')
152
self.assertPathExists('target')
153
self.check_file_contents('target', 'data in target\n')
155
def test_fancy_rename_fails_if_source_and_target_missing(self):
156
self.assertRaises((IOError, OSError), self._fancy_rename,
157
'missingsource', 'missingtarget')
159
146
def test_rename(self):
160
147
# Rename should be semi-atomic on all platforms
161
self.create_file('a', 'something in a\n')
148
open('a', 'wb').write('something in a\n')
162
149
osutils.rename('a', 'b')
163
self.assertPathDoesNotExist('a')
164
self.assertPathExists('b')
150
self.failIfExists('a')
151
self.failUnlessExists('b')
165
152
self.check_file_contents('b', 'something in a\n')
167
self.create_file('a', 'new something in a\n')
154
open('a', 'wb').write('new something in a\n')
168
155
osutils.rename('b', 'a')
170
157
self.check_file_contents('a', 'something in a\n')
493
442
class TestCanonicalRelPath(tests.TestCaseInTempDir):
495
_test_needs_features = [features.CaseInsCasePresFilenameFeature]
444
_test_needs_features = [tests.CaseInsCasePresFilenameFeature]
497
446
def test_canonical_relpath_simple(self):
498
447
f = file('MixedCaseName', 'w')
500
actual = osutils.canonical_relpath(self.test_base_dir, 'mixedcasename')
501
self.assertEqual('work/MixedCaseName', actual)
449
# Watch out for tricky test dir (on OSX /tmp -> /private/tmp)
450
real_base_dir = osutils.realpath(self.test_base_dir)
451
actual = osutils.canonical_relpath(real_base_dir, 'mixedcasename')
452
self.failUnlessEqual('work/MixedCaseName', actual)
503
454
def test_canonical_relpath_missing_tail(self):
504
455
os.mkdir('MixedCaseParent')
505
actual = osutils.canonical_relpath(self.test_base_dir,
456
# Watch out for tricky test dir (on OSX /tmp -> /private/tmp)
457
real_base_dir = osutils.realpath(self.test_base_dir)
458
actual = osutils.canonical_relpath(real_base_dir,
506
459
'mixedcaseparent/nochild')
507
self.assertEqual('work/MixedCaseParent/nochild', actual)
510
class Test_CICPCanonicalRelpath(tests.TestCaseWithTransport):
512
def assertRelpath(self, expected, base, path):
513
actual = osutils._cicp_canonical_relpath(base, path)
514
self.assertEqual(expected, actual)
516
def test_simple(self):
517
self.build_tree(['MixedCaseName'])
518
base = osutils.realpath(self.get_transport('.').local_abspath('.'))
519
self.assertRelpath('MixedCaseName', base, 'mixedcAsename')
521
def test_subdir_missing_tail(self):
522
self.build_tree(['MixedCaseParent/', 'MixedCaseParent/a_child'])
523
base = osutils.realpath(self.get_transport('.').local_abspath('.'))
524
self.assertRelpath('MixedCaseParent/a_child', base,
525
'MixedCaseParent/a_child')
526
self.assertRelpath('MixedCaseParent/a_child', base,
527
'MixedCaseParent/A_Child')
528
self.assertRelpath('MixedCaseParent/not_child', base,
529
'MixedCaseParent/not_child')
531
def test_at_root_slash(self):
532
# We can't test this on Windows, because it has a 'MIN_ABS_PATHLENGTH'
534
if osutils.MIN_ABS_PATHLENGTH > 1:
535
raise tests.TestSkipped('relpath requires %d chars'
536
% osutils.MIN_ABS_PATHLENGTH)
537
self.assertRelpath('foo', '/', '/foo')
539
def test_at_root_drive(self):
540
if sys.platform != 'win32':
541
raise tests.TestNotApplicable('we can only test drive-letter relative'
542
' paths on Windows where we have drive'
545
# The specific issue is that when at the root of a drive, 'abspath'
546
# returns "C:/" or just "/". However, the code assumes that abspath
547
# always returns something like "C:/foo" or "/foo" (no trailing slash).
548
self.assertRelpath('foo', 'C:/', 'C:/foo')
549
self.assertRelpath('foo', 'X:/', 'X:/foo')
550
self.assertRelpath('foo', 'X:/', 'X://foo')
460
self.failUnlessEqual('work/MixedCaseParent/nochild', actual)
553
463
class TestPumpFile(tests.TestCase):
1922
1777
class TestConcurrency(tests.TestCase):
1925
super(TestConcurrency, self).setUp()
1926
self.overrideAttr(osutils, '_cached_local_concurrency')
1928
1779
def test_local_concurrency(self):
1929
1780
concurrency = osutils.local_concurrency()
1930
1781
self.assertIsInstance(concurrency, int)
1932
def test_local_concurrency_environment_variable(self):
1933
self.overrideEnv('BZR_CONCURRENCY', '2')
1934
self.assertEqual(2, osutils.local_concurrency(use_cache=False))
1935
self.overrideEnv('BZR_CONCURRENCY', '3')
1936
self.assertEqual(3, osutils.local_concurrency(use_cache=False))
1937
self.overrideEnv('BZR_CONCURRENCY', 'foo')
1938
self.assertEqual(1, osutils.local_concurrency(use_cache=False))
1940
def test_option_concurrency(self):
1941
self.overrideEnv('BZR_CONCURRENCY', '1')
1942
self.run_bzr('rocks --concurrency 42')
1943
# Command line overrides environment variable
1944
self.assertEquals('42', os.environ['BZR_CONCURRENCY'])
1945
self.assertEquals(42, osutils.local_concurrency(use_cache=False))
1948
class TestFailedToLoadExtension(tests.TestCase):
1950
def _try_loading(self):
1952
import bzrlib._fictional_extension_py
1953
except ImportError, e:
1954
osutils.failed_to_load_extension(e)
1958
super(TestFailedToLoadExtension, self).setUp()
1959
self.overrideAttr(osutils, '_extension_load_failures', [])
1961
def test_failure_to_load(self):
1963
self.assertLength(1, osutils._extension_load_failures)
1964
self.assertEquals(osutils._extension_load_failures[0],
1965
"No module named _fictional_extension_py")
1967
def test_report_extension_load_failures_no_warning(self):
1968
self.assertTrue(self._try_loading())
1969
warnings, result = self.callCatchWarnings(osutils.report_extension_load_failures)
1970
# it used to give a Python warning; it no longer does
1971
self.assertLength(0, warnings)
1973
def test_report_extension_load_failures_message(self):
1975
trace.push_log_file(log)
1976
self.assertTrue(self._try_loading())
1977
osutils.report_extension_load_failures()
1978
self.assertContainsRe(
1980
r"bzr: warning: some compiled extensions could not be loaded; "
1981
"see <https://answers\.launchpad\.net/bzr/\+faq/703>\n"
1985
class TestTerminalWidth(tests.TestCase):
1988
tests.TestCase.setUp(self)
1989
self._orig_terminal_size_state = osutils._terminal_size_state
1990
self._orig_first_terminal_size = osutils._first_terminal_size
1991
self.addCleanup(self.restore_osutils_globals)
1992
osutils._terminal_size_state = 'no_data'
1993
osutils._first_terminal_size = None
1995
def restore_osutils_globals(self):
1996
osutils._terminal_size_state = self._orig_terminal_size_state
1997
osutils._first_terminal_size = self._orig_first_terminal_size
1999
def replace_stdout(self, new):
2000
self.overrideAttr(sys, 'stdout', new)
2002
def replace__terminal_size(self, new):
2003
self.overrideAttr(osutils, '_terminal_size', new)
2005
def set_fake_tty(self):
2007
class I_am_a_tty(object):
2011
self.replace_stdout(I_am_a_tty())
2013
def test_default_values(self):
2014
self.assertEqual(80, osutils.default_terminal_width)
2016
def test_defaults_to_BZR_COLUMNS(self):
2017
# BZR_COLUMNS is set by the test framework
2018
self.assertNotEqual('12', os.environ['BZR_COLUMNS'])
2019
self.overrideEnv('BZR_COLUMNS', '12')
2020
self.assertEqual(12, osutils.terminal_width())
2022
def test_BZR_COLUMNS_0_no_limit(self):
2023
self.overrideEnv('BZR_COLUMNS', '0')
2024
self.assertEqual(None, osutils.terminal_width())
2026
def test_falls_back_to_COLUMNS(self):
2027
self.overrideEnv('BZR_COLUMNS', None)
2028
self.assertNotEqual('42', os.environ['COLUMNS'])
2030
self.overrideEnv('COLUMNS', '42')
2031
self.assertEqual(42, osutils.terminal_width())
2033
def test_tty_default_without_columns(self):
2034
self.overrideEnv('BZR_COLUMNS', None)
2035
self.overrideEnv('COLUMNS', None)
2037
def terminal_size(w, h):
2041
# We need to override the osutils definition as it depends on the
2042
# running environment that we can't control (PQM running without a
2043
# controlling terminal is one example).
2044
self.replace__terminal_size(terminal_size)
2045
self.assertEqual(42, osutils.terminal_width())
2047
def test_non_tty_default_without_columns(self):
2048
self.overrideEnv('BZR_COLUMNS', None)
2049
self.overrideEnv('COLUMNS', None)
2050
self.replace_stdout(None)
2051
self.assertEqual(None, osutils.terminal_width())
2053
def test_no_TIOCGWINSZ(self):
2054
self.requireFeature(term_ios_feature)
2055
termios = term_ios_feature.module
2056
# bug 63539 is about a termios without TIOCGWINSZ attribute
2058
orig = termios.TIOCGWINSZ
2059
except AttributeError:
2060
# We won't remove TIOCGWINSZ, because it doesn't exist anyway :)
2063
self.overrideAttr(termios, 'TIOCGWINSZ')
2064
del termios.TIOCGWINSZ
2065
self.overrideEnv('BZR_COLUMNS', None)
2066
self.overrideEnv('COLUMNS', None)
2067
# Whatever the result is, if we don't raise an exception, it's ok.
2068
osutils.terminal_width()
2071
class TestCreationOps(tests.TestCaseInTempDir):
2072
_test_needs_features = [features.chown_feature]
2075
tests.TestCaseInTempDir.setUp(self)
2076
self.overrideAttr(os, 'chown', self._dummy_chown)
2078
# params set by call to _dummy_chown
2079
self.path = self.uid = self.gid = None
2081
def _dummy_chown(self, path, uid, gid):
2082
self.path, self.uid, self.gid = path, uid, gid
2084
def test_copy_ownership_from_path(self):
2085
"""copy_ownership_from_path test with specified src."""
2087
f = open('test_file', 'wt')
2088
osutils.copy_ownership_from_path('test_file', ownsrc)
2091
self.assertEquals(self.path, 'test_file')
2092
self.assertEquals(self.uid, s.st_uid)
2093
self.assertEquals(self.gid, s.st_gid)
2095
def test_copy_ownership_nonesrc(self):
2096
"""copy_ownership_from_path test with src=None."""
2097
f = open('test_file', 'wt')
2098
# should use parent dir for permissions
2099
osutils.copy_ownership_from_path('test_file')
2102
self.assertEquals(self.path, 'test_file')
2103
self.assertEquals(self.uid, s.st_uid)
2104
self.assertEquals(self.gid, s.st_gid)
2107
class TestPathFromEnviron(tests.TestCase):
2109
def test_is_unicode(self):
2110
self.overrideEnv('BZR_TEST_PATH', './anywhere at all/')
2111
path = osutils.path_from_environ('BZR_TEST_PATH')
2112
self.assertIsInstance(path, unicode)
2113
self.assertEqual(u'./anywhere at all/', path)
2115
def test_posix_path_env_ascii(self):
2116
self.overrideEnv('BZR_TEST_PATH', '/tmp')
2117
home = osutils._posix_path_from_environ('BZR_TEST_PATH')
2118
self.assertIsInstance(home, unicode)
2119
self.assertEqual(u'/tmp', home)
2121
def test_posix_path_env_unicode(self):
2122
self.requireFeature(features.ByteStringNamedFilesystem)
2123
self.overrideEnv('BZR_TEST_PATH', '/home/\xa7test')
2124
self.overrideAttr(osutils, "_fs_enc", "iso8859-1")
2125
self.assertEqual(u'/home/\xa7test',
2126
osutils._posix_path_from_environ('BZR_TEST_PATH'))
2127
osutils._fs_enc = "iso8859-5"
2128
self.assertEqual(u'/home/\u0407test',
2129
osutils._posix_path_from_environ('BZR_TEST_PATH'))
2130
osutils._fs_enc = "utf-8"
2131
self.assertRaises(errors.BadFilenameEncoding,
2132
osutils._posix_path_from_environ, 'BZR_TEST_PATH')
2135
class TestGetHomeDir(tests.TestCase):
2137
def test_is_unicode(self):
2138
home = osutils._get_home_dir()
2139
self.assertIsInstance(home, unicode)
2141
def test_posix_homeless(self):
2142
self.overrideEnv('HOME', None)
2143
home = osutils._get_home_dir()
2144
self.assertIsInstance(home, unicode)
2146
def test_posix_home_ascii(self):
2147
self.overrideEnv('HOME', '/home/test')
2148
home = osutils._posix_get_home_dir()
2149
self.assertIsInstance(home, unicode)
2150
self.assertEqual(u'/home/test', home)
2152
def test_posix_home_unicode(self):
2153
self.requireFeature(features.ByteStringNamedFilesystem)
2154
self.overrideEnv('HOME', '/home/\xa7test')
2155
self.overrideAttr(osutils, "_fs_enc", "iso8859-1")
2156
self.assertEqual(u'/home/\xa7test', osutils._posix_get_home_dir())
2157
osutils._fs_enc = "iso8859-5"
2158
self.assertEqual(u'/home/\u0407test', osutils._posix_get_home_dir())
2159
osutils._fs_enc = "utf-8"
2160
self.assertRaises(errors.BadFilenameEncoding,
2161
osutils._posix_get_home_dir)
2164
class TestGetuserUnicode(tests.TestCase):
2166
def test_is_unicode(self):
2167
user = osutils.getuser_unicode()
2168
self.assertIsInstance(user, unicode)
2170
def envvar_to_override(self):
2171
if sys.platform == "win32":
2172
# Disable use of platform calls on windows so envvar is used
2173
self.overrideAttr(win32utils, 'has_ctypes', False)
2174
return 'USERNAME' # only variable used on windows
2175
return 'LOGNAME' # first variable checked by getpass.getuser()
2177
def test_ascii_user(self):
2178
self.overrideEnv(self.envvar_to_override(), 'jrandom')
2179
self.assertEqual(u'jrandom', osutils.getuser_unicode())
2181
def test_unicode_user(self):
2182
ue = osutils.get_user_encoding()
2183
uni_val, env_val = tests.probe_unicode_in_user_encoding()
2185
raise tests.TestSkipped(
2186
'Cannot find a unicode character that works in encoding %s'
2187
% (osutils.get_user_encoding(),))
2188
uni_username = u'jrandom' + uni_val
2189
encoded_username = uni_username.encode(ue)
2190
self.overrideEnv(self.envvar_to_override(), encoded_username)
2191
self.assertEqual(uni_username, osutils.getuser_unicode())
2194
class TestBackupNames(tests.TestCase):
2197
super(TestBackupNames, self).setUp()
2200
def backup_exists(self, name):
2201
return name in self.backups
2203
def available_backup_name(self, name):
2204
backup_name = osutils.available_backup_name(name, self.backup_exists)
2205
self.backups.append(backup_name)
2208
def assertBackupName(self, expected, name):
2209
self.assertEqual(expected, self.available_backup_name(name))
2211
def test_empty(self):
2212
self.assertBackupName('file.~1~', 'file')
2214
def test_existing(self):
2215
self.available_backup_name('file')
2216
self.available_backup_name('file')
2217
self.assertBackupName('file.~3~', 'file')
2218
# Empty slots are found, this is not a strict requirement and may be
2219
# revisited if we test against all implementations.
2220
self.backups.remove('file.~2~')
2221
self.assertBackupName('file.~2~', 'file')
2224
class TestFindExecutableInPath(tests.TestCase):
2226
def test_windows(self):
2227
if sys.platform != 'win32':
2228
raise tests.TestSkipped('test requires win32')
2229
self.assertTrue(osutils.find_executable_on_path('explorer') is not None)
2231
osutils.find_executable_on_path('explorer.exe') is not None)
2233
osutils.find_executable_on_path('EXPLORER.EXE') is not None)
2235
osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
2236
self.assertTrue(osutils.find_executable_on_path('file.txt') is None)
2238
def test_windows_app_path(self):
2239
if sys.platform != 'win32':
2240
raise tests.TestSkipped('test requires win32')
2241
# Override PATH env var so that exe can only be found on App Path
2242
self.overrideEnv('PATH', '')
2243
# Internt Explorer is always registered in the App Path
2244
self.assertTrue(osutils.find_executable_on_path('iexplore') is not None)
2246
def test_other(self):
2247
if sys.platform == 'win32':
2248
raise tests.TestSkipped('test requires non-win32')
2249
self.assertTrue(osutils.find_executable_on_path('sh') is not None)
2251
osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
2254
class TestEnvironmentErrors(tests.TestCase):
2255
"""Test handling of environmental errors"""
2257
def test_is_oserror(self):
2258
self.assertTrue(osutils.is_environment_error(
2259
OSError(errno.EINVAL, "Invalid parameter")))
2261
def test_is_ioerror(self):
2262
self.assertTrue(osutils.is_environment_error(
2263
IOError(errno.EINVAL, "Invalid parameter")))
2265
def test_is_socket_error(self):
2266
self.assertTrue(osutils.is_environment_error(
2267
socket.error(errno.EINVAL, "Invalid parameter")))
2269
def test_is_select_error(self):
2270
self.assertTrue(osutils.is_environment_error(
2271
select.error(errno.EINVAL, "Invalid parameter")))
2273
def test_is_pywintypes_error(self):
2274
self.requireFeature(features.pywintypes)
2276
self.assertTrue(osutils.is_environment_error(
2277
pywintypes.error(errno.EINVAL, "Invalid parameter", "Caller")))