311
309
self.assertEqual("/", osutils.kind_marker(osutils._directory_kind))
312
310
self.assertEqual("@", osutils.kind_marker("symlink"))
313
311
self.assertEqual("+", osutils.kind_marker("tree-reference"))
314
self.assertEqual("", osutils.kind_marker("fifo"))
315
self.assertEqual("", osutils.kind_marker("socket"))
316
self.assertEqual("", osutils.kind_marker("unknown"))
312
self.assertRaises(errors.BzrError, osutils.kind_marker, "unknown")
319
315
class TestUmask(tests.TestCaseInTempDir):
862
858
self.assertEqual('//HOST/path', osutils._win98_abspath('//HOST/path'))
864
860
cwd = osutils.getcwd().rstrip('/')
865
drive = osutils.ntpath.splitdrive(cwd)[0]
861
drive = osutils._nt_splitdrive(cwd)[0]
866
862
self.assertEqual(cwd+'/path', osutils._win98_abspath('path'))
867
863
self.assertEqual(drive+'/path', osutils._win98_abspath('/path'))
1065
1061
self.assertExpectedBlocks(expected_dirblocks[1:], result)
1067
1063
def test_walkdirs_os_error(self):
1068
# <https://bugs.launchpad.net/bzr/+bug/338653>
1064
# <https://bugs.edge.launchpad.net/bzr/+bug/338653>
1069
1065
# Pyrex readdir didn't raise useful messages if it had an error
1070
1066
# reading the directory
1071
1067
if sys.platform == 'win32':
1072
1068
raise tests.TestNotApplicable(
1073
1069
"readdir IOError not tested on win32")
1074
self.requireFeature(features.not_running_as_root)
1075
1070
os.mkdir("test-unreadable")
1076
1071
os.chmod("test-unreadable", 0000)
1077
1072
# must chmod it back so that it can be removed
1085
1080
# Ensure the message contains the file name
1086
1081
self.assertContainsRe(str(e), "\./test-unreadable")
1089
def test_walkdirs_encoding_error(self):
1090
# <https://bugs.launchpad.net/bzr/+bug/488519>
1091
# walkdirs didn't raise a useful message when the filenames
1092
# are not using the filesystem's encoding
1094
# require a bytestring based filesystem
1095
self.requireFeature(tests.ByteStringNamedFilesystem)
1106
self.build_tree(tree)
1108
# rename the 1file to a latin-1 filename
1109
os.rename("./1file", "\xe8file")
1111
self._save_platform_info()
1112
win32utils.winver = None # Avoid the win32 detection code
1113
osutils._fs_enc = 'UTF-8'
1115
# this should raise on error
1117
for dirdetail, dirblock in osutils.walkdirs('.'):
1120
self.assertRaises(errors.BadFilenameEncoding, attempt)
1122
1083
def test__walkdirs_utf8(self):
1174
1135
dirblock[:] = new_dirblock
1176
1137
def _save_platform_info(self):
1177
self.overrideAttr(win32utils, 'winver')
1178
self.overrideAttr(osutils, '_fs_enc')
1179
self.overrideAttr(osutils, '_selected_dir_reader')
1138
cur_winver = win32utils.winver
1139
cur_fs_enc = osutils._fs_enc
1140
cur_dir_reader = osutils._selected_dir_reader
1142
win32utils.winver = cur_winver
1143
osutils._fs_enc = cur_fs_enc
1144
osutils._selected_dir_reader = cur_dir_reader
1145
self.addCleanup(restore)
1181
1147
def assertDirReaderIs(self, expected):
1182
1148
"""Assert the right implementation for _walkdirs_utf8 is chosen."""
1708
1675
class TestReCompile(tests.TestCase):
1710
def _deprecated_re_compile_checked(self, *args, **kwargs):
1711
return self.applyDeprecated(symbol_versioning.deprecated_in((2, 2, 0)),
1712
osutils.re_compile_checked, *args, **kwargs)
1714
1677
def test_re_compile_checked(self):
1715
r = self._deprecated_re_compile_checked(r'A*', re.IGNORECASE)
1678
r = osutils.re_compile_checked(r'A*', re.IGNORECASE)
1716
1679
self.assertTrue(r.match('aaaa'))
1717
1680
self.assertTrue(r.match('aAaA'))
1719
1682
def test_re_compile_checked_error(self):
1720
1683
# like https://bugs.launchpad.net/bzr/+bug/251352
1722
# Due to possible test isolation error, re.compile is not lazy at
1723
# this point. We re-install lazy compile.
1724
lazy_regex.install_lazy_compile()
1725
1684
err = self.assertRaises(
1726
1685
errors.BzrCommandError,
1727
self._deprecated_re_compile_checked, '*', re.IGNORECASE, 'test case')
1686
osutils.re_compile_checked, '*', re.IGNORECASE, 'test case')
1728
1687
self.assertEqual(
1729
'Invalid regular expression in test case: '
1730
'"*" nothing to repeat',
1688
"Invalid regular expression in test case: '*': "
1689
"nothing to repeat",
1740
1699
def setUp(self):
1741
1700
tests.TestCaseInTempDir.setUp(self)
1742
self.overrideAttr(osutils,
1743
'_selected_dir_reader', self._dir_reader_class())
1702
# Save platform specific info and reset it
1703
cur_dir_reader = osutils._selected_dir_reader
1706
osutils._selected_dir_reader = cur_dir_reader
1707
self.addCleanup(restore)
1709
osutils._selected_dir_reader = self._dir_reader_class()
1745
1711
def _get_ascii_tree(self):
1927
1896
def setUp(self):
1928
1897
super(TestFailedToLoadExtension, self).setUp()
1929
self.overrideAttr(osutils, '_extension_load_failures', [])
1898
self.saved_failures = osutils._extension_load_failures[:]
1899
del osutils._extension_load_failures[:]
1900
self.addCleanup(self.restore_failures)
1902
def restore_failures(self):
1903
osutils._extension_load_failures = self.saved_failures
1931
1905
def test_failure_to_load(self):
1932
1906
self._try_loading()
1955
1929
class TestTerminalWidth(tests.TestCase):
1958
tests.TestCase.setUp(self)
1959
self._orig_terminal_size_state = osutils._terminal_size_state
1960
self._orig_first_terminal_size = osutils._first_terminal_size
1961
self.addCleanup(self.restore_osutils_globals)
1962
osutils._terminal_size_state = 'no_data'
1963
osutils._first_terminal_size = None
1965
def restore_osutils_globals(self):
1966
osutils._terminal_size_state = self._orig_terminal_size_state
1967
osutils._first_terminal_size = self._orig_first_terminal_size
1969
1931
def replace_stdout(self, new):
1970
self.overrideAttr(sys, 'stdout', new)
1932
orig_stdout = sys.stdout
1934
sys.stdout = orig_stdout
1935
self.addCleanup(restore)
1972
1938
def replace__terminal_size(self, new):
1973
self.overrideAttr(osutils, '_terminal_size', new)
1939
orig__terminal_size = osutils._terminal_size
1941
osutils._terminal_size = orig__terminal_size
1942
self.addCleanup(restore)
1943
osutils._terminal_size = new
1975
1945
def set_fake_tty(self):
2026
1996
# We won't remove TIOCGWINSZ, because it doesn't exist anyway :)
2029
self.overrideAttr(termios, 'TIOCGWINSZ')
2000
termios.TIOCGWINSZ = orig
2001
self.addCleanup(restore)
2030
2002
del termios.TIOCGWINSZ
2031
2003
del os.environ['BZR_COLUMNS']
2032
2004
del os.environ['COLUMNS']
2033
2005
# Whatever the result is, if we don't raise an exception, it's ok.
2034
2006
osutils.terminal_width()
2036
class TestCreationOps(tests.TestCaseInTempDir):
2037
_test_needs_features = [features.chown_feature]
2040
tests.TestCaseInTempDir.setUp(self)
2041
self.overrideAttr(os, 'chown', self._dummy_chown)
2043
# params set by call to _dummy_chown
2044
self.path = self.uid = self.gid = None
2046
def _dummy_chown(self, path, uid, gid):
2047
self.path, self.uid, self.gid = path, uid, gid
2049
def test_copy_ownership_from_path(self):
2050
"""copy_ownership_from_path test with specified src."""
2052
f = open('test_file', 'wt')
2053
osutils.copy_ownership_from_path('test_file', ownsrc)
2056
self.assertEquals(self.path, 'test_file')
2057
self.assertEquals(self.uid, s.st_uid)
2058
self.assertEquals(self.gid, s.st_gid)
2060
def test_copy_ownership_nonesrc(self):
2061
"""copy_ownership_from_path test with src=None."""
2062
f = open('test_file', 'wt')
2063
# should use parent dir for permissions
2064
osutils.copy_ownership_from_path('test_file')
2067
self.assertEquals(self.path, 'test_file')
2068
self.assertEquals(self.uid, s.st_uid)
2069
self.assertEquals(self.gid, s.st_gid)
2071
class TestGetuserUnicode(tests.TestCase):
2073
def test_ascii_user(self):
2074
osutils.set_or_unset_env('LOGNAME', 'jrandom')
2075
self.assertEqual(u'jrandom', osutils.getuser_unicode())
2077
def test_unicode_user(self):
2078
ue = osutils.get_user_encoding()
2079
uni_val, env_val = tests.probe_unicode_in_user_encoding()
2081
raise tests.TestSkipped(
2082
'Cannot find a unicode character that works in encoding %s'
2083
% (osutils.get_user_encoding(),))
2084
uni_username = u'jrandom' + uni_val
2085
encoded_username = uni_username.encode(ue)
2086
osutils.set_or_unset_env('LOGNAME', encoded_username)
2087
self.assertEqual(uni_username, osutils.getuser_unicode())