184
185
shape = sorted(os.listdir('.'))
185
186
self.assertEquals(['A', 'B'], shape)
187
def test_rename_error(self):
188
# We wrap os.rename to make it give an error including the filenames
189
# https://bugs.launchpad.net/bzr/+bug/491763
190
err = self.assertRaises(OSError, osutils.rename,
191
'nonexistent', 'target')
192
self.assertContainsString(str(err), 'nonexistent')
195
189
class TestRandChars(tests.TestCase):
868
862
self.assertEqual('//HOST/path', osutils._win98_abspath('//HOST/path'))
870
864
cwd = osutils.getcwd().rstrip('/')
871
drive = osutils._nt_splitdrive(cwd)[0]
865
drive = osutils.ntpath.splitdrive(cwd)[0]
872
866
self.assertEqual(cwd+'/path', osutils._win98_abspath('path'))
873
867
self.assertEqual(drive+'/path', osutils._win98_abspath('/path'))
1071
1065
self.assertExpectedBlocks(expected_dirblocks[1:], result)
1073
1067
def test_walkdirs_os_error(self):
1074
# <https://bugs.edge.launchpad.net/bzr/+bug/338653>
1068
# <https://bugs.launchpad.net/bzr/+bug/338653>
1075
1069
# Pyrex readdir didn't raise useful messages if it had an error
1076
1070
# reading the directory
1077
1071
if sys.platform == 'win32':
1078
1072
raise tests.TestNotApplicable(
1079
1073
"readdir IOError not tested on win32")
1074
self.requireFeature(features.not_running_as_root)
1080
1075
os.mkdir("test-unreadable")
1081
1076
os.chmod("test-unreadable", 0000)
1082
1077
# must chmod it back so that it can be removed
1090
1085
# Ensure the message contains the file name
1091
1086
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")
1110
if "\xe8file" not in os.listdir("."):
1111
self.skip("Lack filesystem that preserves arbitrary bytes")
1113
self._save_platform_info()
1114
win32utils.winver = None # Avoid the win32 detection code
1115
osutils._fs_enc = 'UTF-8'
1117
# this should raise on error
1119
for dirdetail, dirblock in osutils.walkdirs('.'):
1122
self.assertRaises(errors.BadFilenameEncoding, attempt)
1093
1124
def test__walkdirs_utf8(self):
1679
1710
class TestReCompile(tests.TestCase):
1712
def _deprecated_re_compile_checked(self, *args, **kwargs):
1713
return self.applyDeprecated(symbol_versioning.deprecated_in((2, 2, 0)),
1714
osutils.re_compile_checked, *args, **kwargs)
1681
1716
def test_re_compile_checked(self):
1682
r = osutils.re_compile_checked(r'A*', re.IGNORECASE)
1717
r = self._deprecated_re_compile_checked(r'A*', re.IGNORECASE)
1683
1718
self.assertTrue(r.match('aaaa'))
1684
1719
self.assertTrue(r.match('aAaA'))
1686
1721
def test_re_compile_checked_error(self):
1687
1722
# like https://bugs.launchpad.net/bzr/+bug/251352
1724
# Due to possible test isolation error, re.compile is not lazy at
1725
# this point. We re-install lazy compile.
1726
lazy_regex.install_lazy_compile()
1688
1727
err = self.assertRaises(
1689
1728
errors.BzrCommandError,
1690
osutils.re_compile_checked, '*', re.IGNORECASE, 'test case')
1729
self._deprecated_re_compile_checked, '*', re.IGNORECASE, 'test case')
1691
1730
self.assertEqual(
1692
"Invalid regular expression in test case: '*': "
1693
"nothing to repeat",
1731
'Invalid regular expression in test case: '
1732
'"*" nothing to repeat',
1918
1957
class TestTerminalWidth(tests.TestCase):
1960
tests.TestCase.setUp(self)
1961
self._orig_terminal_size_state = osutils._terminal_size_state
1962
self._orig_first_terminal_size = osutils._first_terminal_size
1963
self.addCleanup(self.restore_osutils_globals)
1964
osutils._terminal_size_state = 'no_data'
1965
osutils._first_terminal_size = None
1967
def restore_osutils_globals(self):
1968
osutils._terminal_size_state = self._orig_terminal_size_state
1969
osutils._first_terminal_size = self._orig_first_terminal_size
1920
1971
def replace_stdout(self, new):
1921
1972
self.overrideAttr(sys, 'stdout', new)
2018
2069
self.assertEquals(self.path, 'test_file')
2019
2070
self.assertEquals(self.uid, s.st_uid)
2020
2071
self.assertEquals(self.gid, s.st_gid)
2073
class TestGetuserUnicode(tests.TestCase):
2075
def test_ascii_user(self):
2076
osutils.set_or_unset_env('LOGNAME', 'jrandom')
2077
self.assertEqual(u'jrandom', osutils.getuser_unicode())
2079
def test_unicode_user(self):
2080
ue = osutils.get_user_encoding()
2081
uni_val, env_val = tests.probe_unicode_in_user_encoding()
2083
raise tests.TestSkipped(
2084
'Cannot find a unicode character that works in encoding %s'
2085
% (osutils.get_user_encoding(),))
2086
uni_username = u'jrandom' + uni_val
2087
encoded_username = uni_username.encode(ue)
2088
osutils.set_or_unset_env('LOGNAME', encoded_username)
2089
self.assertEqual(uni_username, osutils.getuser_unicode())
2090
osutils.set_or_unset_env('LOGNAME', u'jrandom\xb6'.encode(ue))
2091
self.assertEqual(u'jrandom\xb6', osutils.getuser_unicode())
2093
class TestBackupNames(tests.TestCase):
2096
super(TestBackupNames, self).setUp()
2099
def backup_exists(self, name):
2100
return name in self.backups
2102
def available_backup_name(self, name):
2103
backup_name = osutils.available_backup_name(name, self.backup_exists)
2104
self.backups.append(backup_name)
2107
def assertBackupName(self, expected, name):
2108
self.assertEqual(expected, self.available_backup_name(name))
2110
def test_empty(self):
2111
self.assertBackupName('file.~1~', 'file')
2113
def test_existing(self):
2114
self.available_backup_name('file')
2115
self.available_backup_name('file')
2116
self.assertBackupName('file.~3~', 'file')
2117
# Empty slots are found, this is not a strict requirement and may be
2118
# revisited if we test against all implementations.
2119
self.backups.remove('file.~2~')
2120
self.assertBackupName('file.~2~', 'file')