100
load_tests = load_tests_apply_scenarios
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)
103
108
class TestContainsWhitespace(tests.TestCase):
105
110
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'))
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'))
113
118
# \xa0 is "Non-breaking-space" which on some python locales thinks it
114
119
# 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'))
120
self.failIf(osutils.contains_whitespace(u''))
121
self.failIf(osutils.contains_whitespace(u'hellothere'))
122
self.failIf(osutils.contains_whitespace(u'hello\xa0there'))
120
125
class TestRename(tests.TestCaseInTempDir):
134
139
# This should work everywhere
135
140
self.create_file('a', 'something in a\n')
136
141
self._fancy_rename('a', 'b')
137
self.assertPathDoesNotExist('a')
138
self.assertPathExists('b')
142
self.failIfExists('a')
143
self.failUnlessExists('b')
139
144
self.check_file_contents('b', 'something in a\n')
141
146
self.create_file('a', 'new something in a\n')
148
153
self.create_file('target', 'data in target\n')
149
154
self.assertRaises((IOError, OSError), self._fancy_rename,
150
155
'missingsource', 'target')
151
self.assertPathExists('target')
156
self.failUnlessExists('target')
152
157
self.check_file_contents('target', 'data in target\n')
154
159
def test_fancy_rename_fails_if_source_and_target_missing(self):
159
164
# Rename should be semi-atomic on all platforms
160
165
self.create_file('a', 'something in a\n')
161
166
osutils.rename('a', 'b')
162
self.assertPathDoesNotExist('a')
163
self.assertPathExists('b')
167
self.failIfExists('a')
168
self.failUnlessExists('b')
164
169
self.check_file_contents('b', 'something in a\n')
166
171
self.create_file('a', 'new something in a\n')
231
236
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
239
class TestRmTree(tests.TestCaseInTempDir):
255
241
def test_rmtree(self):
488
474
f = file('MixedCaseName', 'w')
490
476
actual = osutils.canonical_relpath(self.test_base_dir, 'mixedcasename')
491
self.assertEqual('work/MixedCaseName', actual)
477
self.failUnlessEqual('work/MixedCaseName', actual)
493
479
def test_canonical_relpath_missing_tail(self):
494
480
os.mkdir('MixedCaseParent')
495
481
actual = osutils.canonical_relpath(self.test_base_dir,
496
482
'mixedcaseparent/nochild')
497
self.assertEqual('work/MixedCaseParent/nochild', actual)
483
self.failUnlessEqual('work/MixedCaseParent/nochild', actual)
500
486
class Test_CICPCanonicalRelpath(tests.TestCaseWithTransport):
926
912
osutils._win32_rename('b', 'a')
927
self.assertPathExists('a')
928
self.assertPathDoesNotExist('b')
913
self.failUnlessExists('a')
914
self.failIfExists('b')
929
915
self.assertFileEqual('baz\n', 'a')
931
917
def test_rename_missing_file(self):
1122
1108
# rename the 1file to a latin-1 filename
1123
1109
os.rename("./1file", "\xe8file")
1124
if "\xe8file" not in os.listdir("."):
1125
self.skip("Lack filesystem that preserves arbitrary bytes")
1127
1111
self._save_platform_info()
1128
1112
win32utils.winver = None # Avoid the win32 detection code
1614
1598
('d', 'source/b', 'target/b'),
1615
1599
('f', 'source/b/c', 'target/b/c'),
1616
1600
], processed_files)
1617
self.assertPathDoesNotExist('target')
1601
self.failIfExists('target')
1618
1602
if osutils.has_symlinks():
1619
1603
self.assertEqual([('source/lnk', 'target/lnk')], processed_links)
1666
1650
old = osutils.set_or_unset_env('BZR_TEST_ENV_VAR', None)
1667
1651
self.assertEqual('foo', old)
1668
1652
self.assertEqual(None, os.environ.get('BZR_TEST_ENV_VAR'))
1669
self.assertFalse('BZR_TEST_ENV_VAR' in os.environ)
1653
self.failIf('BZR_TEST_ENV_VAR' in os.environ)
1672
1656
class TestSizeShaFile(tests.TestCaseInTempDir):
1918
1900
self.assertIsInstance(concurrency, int)
1920
1902
def test_local_concurrency_environment_variable(self):
1921
self.overrideEnv('BZR_CONCURRENCY', '2')
1903
os.environ['BZR_CONCURRENCY'] = '2'
1922
1904
self.assertEqual(2, osutils.local_concurrency(use_cache=False))
1923
self.overrideEnv('BZR_CONCURRENCY', '3')
1905
os.environ['BZR_CONCURRENCY'] = '3'
1924
1906
self.assertEqual(3, osutils.local_concurrency(use_cache=False))
1925
self.overrideEnv('BZR_CONCURRENCY', 'foo')
1907
os.environ['BZR_CONCURRENCY'] = 'foo'
1926
1908
self.assertEqual(1, osutils.local_concurrency(use_cache=False))
1928
1910
def test_option_concurrency(self):
1929
self.overrideEnv('BZR_CONCURRENCY', '1')
1911
os.environ['BZR_CONCURRENCY'] = '1'
1930
1912
self.run_bzr('rocks --concurrency 42')
1931
# Command line overrides environment variable
1913
# Command line overrides envrionment variable
1932
1914
self.assertEquals('42', os.environ['BZR_CONCURRENCY'])
1933
1915
self.assertEquals(42, osutils.local_concurrency(use_cache=False))
2004
1986
def test_defaults_to_BZR_COLUMNS(self):
2005
1987
# BZR_COLUMNS is set by the test framework
2006
1988
self.assertNotEqual('12', os.environ['BZR_COLUMNS'])
2007
self.overrideEnv('BZR_COLUMNS', '12')
1989
os.environ['BZR_COLUMNS'] = '12'
2008
1990
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
1992
def test_falls_back_to_COLUMNS(self):
2015
self.overrideEnv('BZR_COLUMNS', None)
1993
del os.environ['BZR_COLUMNS']
2016
1994
self.assertNotEqual('42', os.environ['COLUMNS'])
2017
1995
self.set_fake_tty()
2018
self.overrideEnv('COLUMNS', '42')
1996
os.environ['COLUMNS'] = '42'
2019
1997
self.assertEqual(42, osutils.terminal_width())
2021
1999
def test_tty_default_without_columns(self):
2022
self.overrideEnv('BZR_COLUMNS', None)
2023
self.overrideEnv('COLUMNS', None)
2000
del os.environ['BZR_COLUMNS']
2001
del os.environ['COLUMNS']
2025
2003
def terminal_size(w, h):
2033
2011
self.assertEqual(42, osutils.terminal_width())
2035
2013
def test_non_tty_default_without_columns(self):
2036
self.overrideEnv('BZR_COLUMNS', None)
2037
self.overrideEnv('COLUMNS', None)
2014
del os.environ['BZR_COLUMNS']
2015
del os.environ['COLUMNS']
2038
2016
self.replace_stdout(None)
2039
2017
self.assertEqual(None, osutils.terminal_width())
2051
2029
self.overrideAttr(termios, 'TIOCGWINSZ')
2052
2030
del termios.TIOCGWINSZ
2053
self.overrideEnv('BZR_COLUMNS', None)
2054
self.overrideEnv('COLUMNS', None)
2031
del os.environ['BZR_COLUMNS']
2032
del os.environ['COLUMNS']
2055
2033
# Whatever the result is, if we don't raise an exception, it's ok.
2056
2034
osutils.terminal_width()
2093
2071
class TestGetuserUnicode(tests.TestCase):
2095
2073
def test_ascii_user(self):
2096
self.overrideEnv('LOGNAME', 'jrandom')
2074
osutils.set_or_unset_env('LOGNAME', 'jrandom')
2097
2075
self.assertEqual(u'jrandom', osutils.getuser_unicode())
2099
2077
def test_unicode_user(self):
2105
2083
% (osutils.get_user_encoding(),))
2106
2084
uni_username = u'jrandom' + uni_val
2107
2085
encoded_username = uni_username.encode(ue)
2108
self.overrideEnv('LOGNAME', encoded_username)
2086
osutils.set_or_unset_env('LOGNAME', encoded_username)
2109
2087
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)