99
def load_tests(basic_tests, module, loader):
100
suite = loader.suiteClass()
101
dir_reader_tests, remaining_tests = tests.split_suite_by_condition(
102
basic_tests, tests.condition_isinstance(TestDirReader))
103
tests.multiply_tests(dir_reader_tests, dir_reader_scenarios(), suite)
104
suite.addTest(remaining_tests)
100
load_tests = load_tests_apply_scenarios
108
103
class TestContainsWhitespace(tests.TestCase):
110
105
def test_contains_whitespace(self):
111
self.failUnless(osutils.contains_whitespace(u' '))
112
self.failUnless(osutils.contains_whitespace(u'hello there'))
113
self.failUnless(osutils.contains_whitespace(u'hellothere\n'))
114
self.failUnless(osutils.contains_whitespace(u'hello\nthere'))
115
self.failUnless(osutils.contains_whitespace(u'hello\rthere'))
116
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'))
118
113
# \xa0 is "Non-breaking-space" which on some python locales thinks it
119
114
# is whitespace, but we do not.
120
self.failIf(osutils.contains_whitespace(u''))
121
self.failIf(osutils.contains_whitespace(u'hellothere'))
122
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'))
125
120
class TestRename(tests.TestCaseInTempDir):
139
134
# This should work everywhere
140
135
self.create_file('a', 'something in a\n')
141
136
self._fancy_rename('a', 'b')
142
self.failIfExists('a')
143
self.failUnlessExists('b')
137
self.assertPathDoesNotExist('a')
138
self.assertPathExists('b')
144
139
self.check_file_contents('b', 'something in a\n')
146
141
self.create_file('a', 'new something in a\n')
153
148
self.create_file('target', 'data in target\n')
154
149
self.assertRaises((IOError, OSError), self._fancy_rename,
155
150
'missingsource', 'target')
156
self.failUnlessExists('target')
151
self.assertPathExists('target')
157
152
self.check_file_contents('target', 'data in target\n')
159
154
def test_fancy_rename_fails_if_source_and_target_missing(self):
164
159
# Rename should be semi-atomic on all platforms
165
160
self.create_file('a', 'something in a\n')
166
161
osutils.rename('a', 'b')
167
self.failIfExists('a')
168
self.failUnlessExists('b')
162
self.assertPathDoesNotExist('a')
163
self.assertPathExists('b')
169
164
self.check_file_contents('b', 'something in a\n')
171
166
self.create_file('a', 'new something in a\n')
236
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'))
239
253
class TestRmTree(tests.TestCaseInTempDir):
241
255
def test_rmtree(self):
469
483
class TestCanonicalRelPath(tests.TestCaseInTempDir):
471
_test_needs_features = [tests.CaseInsCasePresFilenameFeature]
485
_test_needs_features = [features.CaseInsCasePresFilenameFeature]
473
487
def test_canonical_relpath_simple(self):
474
488
f = file('MixedCaseName', 'w')
476
490
actual = osutils.canonical_relpath(self.test_base_dir, 'mixedcasename')
477
self.failUnlessEqual('work/MixedCaseName', actual)
491
self.assertEqual('work/MixedCaseName', actual)
479
493
def test_canonical_relpath_missing_tail(self):
480
494
os.mkdir('MixedCaseParent')
481
495
actual = osutils.canonical_relpath(self.test_base_dir,
482
496
'mixedcaseparent/nochild')
483
self.failUnlessEqual('work/MixedCaseParent/nochild', actual)
497
self.assertEqual('work/MixedCaseParent/nochild', actual)
486
500
class Test_CICPCanonicalRelpath(tests.TestCaseWithTransport):
970
984
"""Test mac special functions that require directories."""
972
986
def test_getcwd(self):
973
self.requireFeature(tests.UnicodeFilenameFeature)
987
self.requireFeature(features.UnicodeFilenameFeature)
974
988
os.mkdir(u'B\xe5gfors')
975
989
os.chdir(u'B\xe5gfors')
976
990
self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
978
992
def test_getcwd_nonnorm(self):
979
self.requireFeature(tests.UnicodeFilenameFeature)
993
self.requireFeature(features.UnicodeFilenameFeature)
980
994
# Test that _mac_getcwd() will normalize this path
981
995
os.mkdir(u'Ba\u030agfors')
982
996
os.chdir(u'Ba\u030agfors')
1191
1207
self._save_platform_info()
1192
1208
win32utils.winver = None # Avoid the win32 detection code
1193
1209
osutils._fs_enc = 'UTF-8'
1194
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1210
self.assertDirReaderIs(
1211
UTF8DirReaderFeature.module.UTF8DirReader)
1196
1213
def test_force_walkdirs_utf8_fs_ascii(self):
1197
1214
self.requireFeature(UTF8DirReaderFeature)
1198
1215
self._save_platform_info()
1199
1216
win32utils.winver = None # Avoid the win32 detection code
1200
1217
osutils._fs_enc = 'US-ASCII'
1201
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1218
self.assertDirReaderIs(
1219
UTF8DirReaderFeature.module.UTF8DirReader)
1203
1221
def test_force_walkdirs_utf8_fs_ANSI(self):
1204
1222
self.requireFeature(UTF8DirReaderFeature)
1205
1223
self._save_platform_info()
1206
1224
win32utils.winver = None # Avoid the win32 detection code
1207
1225
osutils._fs_enc = 'ANSI_X3.4-1968'
1208
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
1226
self.assertDirReaderIs(
1227
UTF8DirReaderFeature.module.UTF8DirReader)
1210
1229
def test_force_walkdirs_utf8_fs_latin1(self):
1211
1230
self._save_platform_info()
1381
1400
def test__walkdirs_utf8_win32readdir(self):
1382
1401
self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
1383
self.requireFeature(tests.UnicodeFilenameFeature)
1402
self.requireFeature(features.UnicodeFilenameFeature)
1384
1403
from bzrlib._walkdirs_win32 import Win32ReadDir
1385
1404
self._save_platform_info()
1386
1405
osutils._selected_dir_reader = Win32ReadDir()
1437
1456
def test__walkdirs_utf_win32_find_file_stat_file(self):
1438
1457
"""make sure our Stat values are valid"""
1439
1458
self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
1440
self.requireFeature(tests.UnicodeFilenameFeature)
1459
self.requireFeature(features.UnicodeFilenameFeature)
1441
1460
from bzrlib._walkdirs_win32 import Win32ReadDir
1442
1461
name0u = u'0file-\xb6'
1443
1462
name0 = name0u.encode('utf8')
1461
1480
def test__walkdirs_utf_win32_find_file_stat_directory(self):
1462
1481
"""make sure our Stat values are valid"""
1463
1482
self.requireFeature(test__walkdirs_win32.win32_readdir_feature)
1464
self.requireFeature(tests.UnicodeFilenameFeature)
1483
self.requireFeature(features.UnicodeFilenameFeature)
1465
1484
from bzrlib._walkdirs_win32 import Win32ReadDir
1466
1485
name0u = u'0dir-\u062c\u0648'
1467
1486
name0 = name0u.encode('utf8')
1567
1586
self.assertEqual(['c'], os.listdir('target/b'))
1569
1588
def test_copy_tree_symlinks(self):
1570
self.requireFeature(tests.SymlinkFeature)
1589
self.requireFeature(features.SymlinkFeature)
1571
1590
self.build_tree(['source/'])
1572
1591
os.symlink('a/generic/path', 'source/lnk')
1573
1592
osutils.copy_tree('source', 'target')
1598
1617
('d', 'source/b', 'target/b'),
1599
1618
('f', 'source/b/c', 'target/b/c'),
1600
1619
], processed_files)
1601
self.failIfExists('target')
1620
self.assertPathDoesNotExist('target')
1602
1621
if osutils.has_symlinks():
1603
1622
self.assertEqual([('source/lnk', 'target/lnk')], processed_links)
1650
1669
old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', None)
1651
1670
self.assertEqual('foo', old)
1652
1671
self.assertEqual(None, os.environ.get('BZR_TEST_ENV_VAR'))
1653
self.failIf('BZR_TEST_ENV_VAR' in os.environ)
1672
self.assertFalse('BZR_TEST_ENV_VAR' in os.environ)
1656
1675
class TestSizeShaFile(tests.TestCaseInTempDir):
1838
1859
return filtered_dirblocks
1840
1861
def test_walk_unicode_tree(self):
1841
self.requireFeature(tests.UnicodeFilenameFeature)
1862
self.requireFeature(features.UnicodeFilenameFeature)
1842
1863
tree, expected_dirblocks = self._get_unicode_tree()
1843
1864
self.build_tree(tree)
1844
1865
result = list(osutils._walkdirs_utf8('.'))
1845
1866
self.assertEqual(expected_dirblocks, self._filter_out(result))
1847
1868
def test_symlink(self):
1848
self.requireFeature(tests.SymlinkFeature)
1849
self.requireFeature(tests.UnicodeFilenameFeature)
1869
self.requireFeature(features.SymlinkFeature)
1870
self.requireFeature(features.UnicodeFilenameFeature)
1850
1871
target = u'target\N{Euro Sign}'
1851
1872
link_name = u'l\N{Euro Sign}nk'
1852
1873
os.symlink(target, link_name)
1879
1900
os.symlink(self.target, self.link)
1881
1902
def test_os_readlink_link_encoding(self):
1882
if sys.version_info < (2, 6):
1883
self.assertRaises(UnicodeEncodeError, os.readlink, self.link)
1885
self.assertEquals(self.target, os.readlink(self.link))
1903
self.assertEquals(self.target, os.readlink(self.link))
1887
1905
def test_os_readlink_link_decoding(self):
1888
1906
self.assertEquals(self.target.encode(osutils._fs_enc),
1900
1918
self.assertIsInstance(concurrency, int)
1902
1920
def test_local_concurrency_environment_variable(self):
1903
os.environ['BZR_CONCURRENCY'] = '2'
1921
self.overrideEnv('BZR_CONCURRENCY', '2')
1904
1922
self.assertEqual(2, osutils.local_concurrency(use_cache=False))
1905
os.environ['BZR_CONCURRENCY'] = '3'
1923
self.overrideEnv('BZR_CONCURRENCY', '3')
1906
1924
self.assertEqual(3, osutils.local_concurrency(use_cache=False))
1907
os.environ['BZR_CONCURRENCY'] = 'foo'
1925
self.overrideEnv('BZR_CONCURRENCY', 'foo')
1908
1926
self.assertEqual(1, osutils.local_concurrency(use_cache=False))
1910
1928
def test_option_concurrency(self):
1911
os.environ['BZR_CONCURRENCY'] = '1'
1929
self.overrideEnv('BZR_CONCURRENCY', '1')
1912
1930
self.run_bzr('rocks --concurrency 42')
1913
# Command line overrides envrionment variable
1931
# Command line overrides environment variable
1914
1932
self.assertEquals('42', os.environ['BZR_CONCURRENCY'])
1915
1933
self.assertEquals(42, osutils.local_concurrency(use_cache=False))
1986
2004
def test_defaults_to_BZR_COLUMNS(self):
1987
2005
# BZR_COLUMNS is set by the test framework
1988
2006
self.assertNotEqual('12', os.environ['BZR_COLUMNS'])
1989
os.environ['BZR_COLUMNS'] = '12'
2007
self.overrideEnv('BZR_COLUMNS', '12')
1990
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())
1992
2014
def test_falls_back_to_COLUMNS(self):
1993
del os.environ['BZR_COLUMNS']
2015
self.overrideEnv('BZR_COLUMNS', None)
1994
2016
self.assertNotEqual('42', os.environ['COLUMNS'])
1995
2017
self.set_fake_tty()
1996
os.environ['COLUMNS'] = '42'
2018
self.overrideEnv('COLUMNS', '42')
1997
2019
self.assertEqual(42, osutils.terminal_width())
1999
2021
def test_tty_default_without_columns(self):
2000
del os.environ['BZR_COLUMNS']
2001
del os.environ['COLUMNS']
2022
self.overrideEnv('BZR_COLUMNS', None)
2023
self.overrideEnv('COLUMNS', None)
2003
2025
def terminal_size(w, h):
2011
2033
self.assertEqual(42, osutils.terminal_width())
2013
2035
def test_non_tty_default_without_columns(self):
2014
del os.environ['BZR_COLUMNS']
2015
del os.environ['COLUMNS']
2036
self.overrideEnv('BZR_COLUMNS', None)
2037
self.overrideEnv('COLUMNS', None)
2016
2038
self.replace_stdout(None)
2017
2039
self.assertEqual(None, osutils.terminal_width())
2029
2051
self.overrideAttr(termios, 'TIOCGWINSZ')
2030
2052
del termios.TIOCGWINSZ
2031
del os.environ['BZR_COLUMNS']
2032
del os.environ['COLUMNS']
2053
self.overrideEnv('BZR_COLUMNS', None)
2054
self.overrideEnv('COLUMNS', None)
2033
2055
# Whatever the result is, if we don't raise an exception, it's ok.
2034
2056
osutils.terminal_width()
2036
2059
class TestCreationOps(tests.TestCaseInTempDir):
2037
2060
_test_needs_features = [features.chown_feature]
2068
2091
self.assertEquals(self.uid, s.st_uid)
2069
2092
self.assertEquals(self.gid, s.st_gid)
2071
2095
class TestGetuserUnicode(tests.TestCase):
2073
2097
def test_ascii_user(self):
2074
osutils.set_or_unset_env('LOGNAME', 'jrandom')
2098
self.overrideEnv('LOGNAME', 'jrandom')
2075
2099
self.assertEqual(u'jrandom', osutils.getuser_unicode())
2077
2101
def test_unicode_user(self):
2083
2107
% (osutils.get_user_encoding(),))
2084
2108
uni_username = u'jrandom' + uni_val
2085
2109
encoded_username = uni_username.encode(ue)
2086
osutils.set_or_unset_env('LOGNAME', encoded_username)
2110
self.overrideEnv('LOGNAME', encoded_username)
2087
2111
self.assertEqual(uni_username, osutils.getuser_unicode())
2112
self.overrideEnv('LOGNAME', u'jrandom\xb6'.encode(ue))
2113
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)