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)
100
load_tests = load_tests_apply_scenarios
109
103
class TestContainsWhitespace(tests.TestCase):
111
105
def test_contains_whitespace(self):
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'))
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'))
119
113
# \xa0 is "Non-breaking-space" which on some python locales thinks it
120
114
# is whitespace, but we do not.
121
self.failIf(osutils.contains_whitespace(u''))
122
self.failIf(osutils.contains_whitespace(u'hellothere'))
123
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'))
126
120
class TestRename(tests.TestCaseInTempDir):
140
134
# This should work everywhere
141
135
self.create_file('a', 'something in a\n')
142
136
self._fancy_rename('a', 'b')
143
self.failIfExists('a')
144
self.failUnlessExists('b')
137
self.assertPathDoesNotExist('a')
138
self.assertPathExists('b')
145
139
self.check_file_contents('b', 'something in a\n')
147
141
self.create_file('a', 'new something in a\n')
154
148
self.create_file('target', 'data in target\n')
155
149
self.assertRaises((IOError, OSError), self._fancy_rename,
156
150
'missingsource', 'target')
157
self.failUnlessExists('target')
151
self.assertPathExists('target')
158
152
self.check_file_contents('target', 'data in target\n')
160
154
def test_fancy_rename_fails_if_source_and_target_missing(self):
165
159
# Rename should be semi-atomic on all platforms
166
160
self.create_file('a', 'something in a\n')
167
161
osutils.rename('a', 'b')
168
self.failIfExists('a')
169
self.failUnlessExists('b')
162
self.assertPathDoesNotExist('a')
163
self.assertPathExists('b')
170
164
self.check_file_contents('b', 'something in a\n')
172
166
self.create_file('a', 'new something in a\n')
237
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'))
240
253
class TestRmTree(tests.TestCaseInTempDir):
242
255
def test_rmtree(self):
475
488
f = file('MixedCaseName', 'w')
477
490
actual = osutils.canonical_relpath(self.test_base_dir, 'mixedcasename')
478
self.failUnlessEqual('work/MixedCaseName', actual)
491
self.assertEqual('work/MixedCaseName', actual)
480
493
def test_canonical_relpath_missing_tail(self):
481
494
os.mkdir('MixedCaseParent')
482
495
actual = osutils.canonical_relpath(self.test_base_dir,
483
496
'mixedcaseparent/nochild')
484
self.failUnlessEqual('work/MixedCaseParent/nochild', actual)
497
self.assertEqual('work/MixedCaseParent/nochild', actual)
487
500
class Test_CICPCanonicalRelpath(tests.TestCaseWithTransport):
1598
1614
('d', 'source/b', 'target/b'),
1599
1615
('f', 'source/b/c', 'target/b/c'),
1600
1616
], processed_files)
1601
self.failIfExists('target')
1617
self.assertPathDoesNotExist('target')
1602
1618
if osutils.has_symlinks():
1603
1619
self.assertEqual([('source/lnk', 'target/lnk')], processed_links)
1879
1897
os.symlink(self.target, self.link)
1881
1899
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))
1900
self.assertEquals(self.target, os.readlink(self.link))
1887
1902
def test_os_readlink_link_decoding(self):
1888
1903
self.assertEquals(self.target.encode(osutils._fs_enc),
1900
1915
self.assertIsInstance(concurrency, int)
1902
1917
def test_local_concurrency_environment_variable(self):
1903
os.environ['BZR_CONCURRENCY'] = '2'
1918
self.overrideEnv('BZR_CONCURRENCY', '2')
1904
1919
self.assertEqual(2, osutils.local_concurrency(use_cache=False))
1905
os.environ['BZR_CONCURRENCY'] = '3'
1920
self.overrideEnv('BZR_CONCURRENCY', '3')
1906
1921
self.assertEqual(3, osutils.local_concurrency(use_cache=False))
1907
os.environ['BZR_CONCURRENCY'] = 'foo'
1922
self.overrideEnv('BZR_CONCURRENCY', 'foo')
1908
1923
self.assertEqual(1, osutils.local_concurrency(use_cache=False))
1910
1925
def test_option_concurrency(self):
1911
os.environ['BZR_CONCURRENCY'] = '1'
1926
self.overrideEnv('BZR_CONCURRENCY', '1')
1912
1927
self.run_bzr('rocks --concurrency 42')
1913
# Command line overrides envrionment variable
1928
# Command line overrides environment variable
1914
1929
self.assertEquals('42', os.environ['BZR_CONCURRENCY'])
1915
1930
self.assertEquals(42, osutils.local_concurrency(use_cache=False))
1986
2001
def test_defaults_to_BZR_COLUMNS(self):
1987
2002
# BZR_COLUMNS is set by the test framework
1988
2003
self.assertNotEqual('12', os.environ['BZR_COLUMNS'])
1989
os.environ['BZR_COLUMNS'] = '12'
2004
self.overrideEnv('BZR_COLUMNS', '12')
1990
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())
1992
2011
def test_falls_back_to_COLUMNS(self):
1993
del os.environ['BZR_COLUMNS']
2012
self.overrideEnv('BZR_COLUMNS', None)
1994
2013
self.assertNotEqual('42', os.environ['COLUMNS'])
1995
2014
self.set_fake_tty()
1996
os.environ['COLUMNS'] = '42'
2015
self.overrideEnv('COLUMNS', '42')
1997
2016
self.assertEqual(42, osutils.terminal_width())
1999
2018
def test_tty_default_without_columns(self):
2000
del os.environ['BZR_COLUMNS']
2001
del os.environ['COLUMNS']
2019
self.overrideEnv('BZR_COLUMNS', None)
2020
self.overrideEnv('COLUMNS', None)
2003
2022
def terminal_size(w, h):
2011
2030
self.assertEqual(42, osutils.terminal_width())
2013
2032
def test_non_tty_default_without_columns(self):
2014
del os.environ['BZR_COLUMNS']
2015
del os.environ['COLUMNS']
2033
self.overrideEnv('BZR_COLUMNS', None)
2034
self.overrideEnv('COLUMNS', None)
2016
2035
self.replace_stdout(None)
2017
2036
self.assertEqual(None, osutils.terminal_width())
2029
2048
self.overrideAttr(termios, 'TIOCGWINSZ')
2030
2049
del termios.TIOCGWINSZ
2031
del os.environ['BZR_COLUMNS']
2032
del os.environ['COLUMNS']
2050
self.overrideEnv('BZR_COLUMNS', None)
2051
self.overrideEnv('COLUMNS', None)
2033
2052
# Whatever the result is, if we don't raise an exception, it's ok.
2034
2053
osutils.terminal_width()
2071
2090
class TestGetuserUnicode(tests.TestCase):
2073
2092
def test_ascii_user(self):
2074
osutils.set_or_unset_env('LOGNAME', 'jrandom')
2093
self.overrideEnv('LOGNAME', 'jrandom')
2075
2094
self.assertEqual(u'jrandom', osutils.getuser_unicode())
2077
2096
def test_unicode_user(self):
2078
2097
ue = osutils.get_user_encoding()
2079
osutils.set_or_unset_env('LOGNAME', u'jrandom\xb6'.encode(ue))
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))
2080
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)