100
load_tests = load_tests_apply_scenarios
95
def load_tests(basic_tests, module, loader):
96
suite = loader.suiteClass()
97
dir_reader_tests, remaining_tests = tests.split_suite_by_condition(
98
basic_tests, tests.condition_isinstance(TestDirReader))
99
tests.multiply_tests(dir_reader_tests, dir_reader_scenarios(), suite)
100
suite.addTest(remaining_tests)
103
104
class TestContainsWhitespace(tests.TestCase):
105
106
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'))
107
self.failUnless(osutils.contains_whitespace(u' '))
108
self.failUnless(osutils.contains_whitespace(u'hello there'))
109
self.failUnless(osutils.contains_whitespace(u'hellothere\n'))
110
self.failUnless(osutils.contains_whitespace(u'hello\nthere'))
111
self.failUnless(osutils.contains_whitespace(u'hello\rthere'))
112
self.failUnless(osutils.contains_whitespace(u'hello\tthere'))
113
114
# \xa0 is "Non-breaking-space" which on some python locales thinks it
114
115
# 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'))
116
self.failIf(osutils.contains_whitespace(u''))
117
self.failIf(osutils.contains_whitespace(u'hellothere'))
118
self.failIf(osutils.contains_whitespace(u'hello\xa0there'))
120
121
class TestRename(tests.TestCaseInTempDir):
1724
1665
class TestReCompile(tests.TestCase):
1726
def _deprecated_re_compile_checked(self, *args, **kwargs):
1727
return self.applyDeprecated(symbol_versioning.deprecated_in((2, 2, 0)),
1728
osutils.re_compile_checked, *args, **kwargs)
1730
1667
def test_re_compile_checked(self):
1731
r = self._deprecated_re_compile_checked(r'A*', re.IGNORECASE)
1668
r = osutils.re_compile_checked(r'A*', re.IGNORECASE)
1732
1669
self.assertTrue(r.match('aaaa'))
1733
1670
self.assertTrue(r.match('aAaA'))
1735
1672
def test_re_compile_checked_error(self):
1736
1673
# like https://bugs.launchpad.net/bzr/+bug/251352
1738
# Due to possible test isolation error, re.compile is not lazy at
1739
# this point. We re-install lazy compile.
1740
lazy_regex.install_lazy_compile()
1741
1674
err = self.assertRaises(
1742
1675
errors.BzrCommandError,
1743
self._deprecated_re_compile_checked, '*', re.IGNORECASE, 'test case')
1676
osutils.re_compile_checked, '*', re.IGNORECASE, 'test case')
1744
1677
self.assertEqual(
1745
'Invalid regular expression in test case: '
1746
'"*" nothing to repeat',
1678
"Invalid regular expression in test case: '*': "
1679
"nothing to repeat",
1750
1683
class TestDirReader(tests.TestCaseInTempDir):
1752
scenarios = dir_reader_scenarios()
1754
1685
# Set by load_tests
1755
1686
_dir_reader_class = None
1756
1687
_native_to_unicode = None
1758
1689
def setUp(self):
1759
1690
tests.TestCaseInTempDir.setUp(self)
1760
self.overrideAttr(osutils,
1761
'_selected_dir_reader', self._dir_reader_class())
1692
# Save platform specific info and reset it
1693
cur_dir_reader = osutils._selected_dir_reader
1696
osutils._selected_dir_reader = cur_dir_reader
1697
self.addCleanup(restore)
1699
osutils._selected_dir_reader = self._dir_reader_class()
1763
1701
def _get_ascii_tree(self):
1965
1892
r"bzr: warning: some compiled extensions could not be loaded; "
1966
1893
"see <https://answers\.launchpad\.net/bzr/\+faq/703>\n"
1970
class TestTerminalWidth(tests.TestCase):
1973
tests.TestCase.setUp(self)
1974
self._orig_terminal_size_state = osutils._terminal_size_state
1975
self._orig_first_terminal_size = osutils._first_terminal_size
1976
self.addCleanup(self.restore_osutils_globals)
1977
osutils._terminal_size_state = 'no_data'
1978
osutils._first_terminal_size = None
1980
def restore_osutils_globals(self):
1981
osutils._terminal_size_state = self._orig_terminal_size_state
1982
osutils._first_terminal_size = self._orig_first_terminal_size
1984
def replace_stdout(self, new):
1985
self.overrideAttr(sys, 'stdout', new)
1987
def replace__terminal_size(self, new):
1988
self.overrideAttr(osutils, '_terminal_size', new)
1990
def set_fake_tty(self):
1992
class I_am_a_tty(object):
1996
self.replace_stdout(I_am_a_tty())
1998
def test_default_values(self):
1999
self.assertEqual(80, osutils.default_terminal_width)
2001
def test_defaults_to_BZR_COLUMNS(self):
2002
# BZR_COLUMNS is set by the test framework
2003
self.assertNotEqual('12', os.environ['BZR_COLUMNS'])
2004
self.overrideEnv('BZR_COLUMNS', '12')
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())
2011
def test_falls_back_to_COLUMNS(self):
2012
self.overrideEnv('BZR_COLUMNS', None)
2013
self.assertNotEqual('42', os.environ['COLUMNS'])
2015
self.overrideEnv('COLUMNS', '42')
2016
self.assertEqual(42, osutils.terminal_width())
2018
def test_tty_default_without_columns(self):
2019
self.overrideEnv('BZR_COLUMNS', None)
2020
self.overrideEnv('COLUMNS', None)
2022
def terminal_size(w, h):
2026
# We need to override the osutils definition as it depends on the
2027
# running environment that we can't control (PQM running without a
2028
# controlling terminal is one example).
2029
self.replace__terminal_size(terminal_size)
2030
self.assertEqual(42, osutils.terminal_width())
2032
def test_non_tty_default_without_columns(self):
2033
self.overrideEnv('BZR_COLUMNS', None)
2034
self.overrideEnv('COLUMNS', None)
2035
self.replace_stdout(None)
2036
self.assertEqual(None, osutils.terminal_width())
2038
def test_no_TIOCGWINSZ(self):
2039
self.requireFeature(term_ios_feature)
2040
termios = term_ios_feature.module
2041
# bug 63539 is about a termios without TIOCGWINSZ attribute
2043
orig = termios.TIOCGWINSZ
2044
except AttributeError:
2045
# We won't remove TIOCGWINSZ, because it doesn't exist anyway :)
2048
self.overrideAttr(termios, 'TIOCGWINSZ')
2049
del termios.TIOCGWINSZ
2050
self.overrideEnv('BZR_COLUMNS', None)
2051
self.overrideEnv('COLUMNS', None)
2052
# Whatever the result is, if we don't raise an exception, it's ok.
2053
osutils.terminal_width()
2056
class TestCreationOps(tests.TestCaseInTempDir):
2057
_test_needs_features = [features.chown_feature]
2060
tests.TestCaseInTempDir.setUp(self)
2061
self.overrideAttr(os, 'chown', self._dummy_chown)
2063
# params set by call to _dummy_chown
2064
self.path = self.uid = self.gid = None
2066
def _dummy_chown(self, path, uid, gid):
2067
self.path, self.uid, self.gid = path, uid, gid
2069
def test_copy_ownership_from_path(self):
2070
"""copy_ownership_from_path test with specified src."""
2072
f = open('test_file', 'wt')
2073
osutils.copy_ownership_from_path('test_file', ownsrc)
2076
self.assertEquals(self.path, 'test_file')
2077
self.assertEquals(self.uid, s.st_uid)
2078
self.assertEquals(self.gid, s.st_gid)
2080
def test_copy_ownership_nonesrc(self):
2081
"""copy_ownership_from_path test with src=None."""
2082
f = open('test_file', 'wt')
2083
# should use parent dir for permissions
2084
osutils.copy_ownership_from_path('test_file')
2087
self.assertEquals(self.path, 'test_file')
2088
self.assertEquals(self.uid, s.st_uid)
2089
self.assertEquals(self.gid, s.st_gid)
2092
class TestGetuserUnicode(tests.TestCase):
2094
def test_ascii_user(self):
2095
self.overrideEnv('LOGNAME', 'jrandom')
2096
self.assertEqual(u'jrandom', osutils.getuser_unicode())
2098
def test_unicode_user(self):
2099
ue = osutils.get_user_encoding()
2100
uni_val, env_val = tests.probe_unicode_in_user_encoding()
2102
raise tests.TestSkipped(
2103
'Cannot find a unicode character that works in encoding %s'
2104
% (osutils.get_user_encoding(),))
2105
uni_username = u'jrandom' + uni_val
2106
encoded_username = uni_username.encode(ue)
2107
self.overrideEnv('LOGNAME', encoded_username)
2108
self.assertEqual(uni_username, osutils.getuser_unicode())
2109
self.overrideEnv('LOGNAME', u'jrandom\xb6'.encode(ue))
2110
self.assertEqual(u'jrandom\xb6', osutils.getuser_unicode())
2112
def test_no_username_bug_660174(self):
2113
self.requireFeature(features.win32_feature)
2114
for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
2115
self.overrideEnv(name, None)
2116
self.assertEqual(u'UNKNOWN', osutils.getuser_unicode())
2119
class TestBackupNames(tests.TestCase):
2122
super(TestBackupNames, self).setUp()
2125
def backup_exists(self, name):
2126
return name in self.backups
2128
def available_backup_name(self, name):
2129
backup_name = osutils.available_backup_name(name, self.backup_exists)
2130
self.backups.append(backup_name)
2133
def assertBackupName(self, expected, name):
2134
self.assertEqual(expected, self.available_backup_name(name))
2136
def test_empty(self):
2137
self.assertBackupName('file.~1~', 'file')
2139
def test_existing(self):
2140
self.available_backup_name('file')
2141
self.available_backup_name('file')
2142
self.assertBackupName('file.~3~', 'file')
2143
# Empty slots are found, this is not a strict requirement and may be
2144
# revisited if we test against all implementations.
2145
self.backups.remove('file.~2~')
2146
self.assertBackupName('file.~2~', 'file')
2149
class TestFindExecutableInPath(tests.TestCase):
2151
def test_windows(self):
2152
if sys.platform != 'win32':
2153
raise tests.TestSkipped('test requires win32')
2154
self.assertTrue(osutils.find_executable_on_path('explorer') is not None)
2156
osutils.find_executable_on_path('explorer.exe') is not None)
2158
osutils.find_executable_on_path('EXPLORER.EXE') is not None)
2160
osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)
2161
self.assertTrue(osutils.find_executable_on_path('file.txt') is None)
2163
def test_other(self):
2164
if sys.platform == 'win32':
2165
raise tests.TestSkipped('test requires non-win32')
2166
self.assertTrue(osutils.find_executable_on_path('sh') is not None)
2168
osutils.find_executable_on_path('THIS SHOULD NOT EXIST') is None)