53
53
UTF8DirReaderFeature = _UTF8DirReaderFeature()
56
class TestOSUtils(tests.TestCaseInTempDir):
56
def _already_unicode(s):
60
def _fs_enc_to_unicode(s):
61
return s.decode(osutils._fs_enc)
64
def _utf8_to_unicode(s):
65
return s.decode('UTF-8')
68
def dir_reader_scenarios():
69
# For each dir reader we define:
71
# - native_to_unicode: a function converting the native_abspath as returned
72
# by DirReader.read_dir to its unicode representation
74
# UnicodeDirReader is the fallback, it should be tested on all platforms.
75
scenarios = [('unicode',
76
dict(_dir_reader_class=osutils.UnicodeDirReader,
77
_native_to_unicode=_already_unicode))]
78
# Some DirReaders are platform specific and even there they may not be
80
if UTF8DirReaderFeature.available():
81
from bzrlib import _readdir_pyx
82
scenarios.append(('utf8',
83
dict(_dir_reader_class=_readdir_pyx.UTF8DirReader,
84
_native_to_unicode=_utf8_to_unicode)))
86
if test__walkdirs_win32.Win32ReadDirFeature.available():
88
from bzrlib import _walkdirs_win32
89
# TODO: check on windows, it may be that we need to use/add
90
# safe_unicode instead of _fs_enc_to_unicode
93
dict(_dir_reader_class=_walkdirs_win32.Win32ReadDir,
94
_native_to_unicode=_fs_enc_to_unicode)))
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)
109
class TestContainsWhitespace(tests.TestCase):
58
111
def test_contains_whitespace(self):
59
112
self.failUnless(osutils.contains_whitespace(u' '))
277
353
# Instead blackbox.test_locale should check for localized
278
354
# dates once they do occur in output strings.
356
def test_local_time_offset(self):
357
"""Test that local_time_offset() returns a sane value."""
358
offset = osutils.local_time_offset()
359
self.assertTrue(isinstance(offset, int))
360
# Test that the offset is no more than a eighteen hours in
362
# Time zone handling is system specific, so it is difficult to
363
# do more specific tests, but a value outside of this range is
365
eighteen_hours = 18 * 3600
366
self.assertTrue(-eighteen_hours < offset < eighteen_hours)
368
def test_local_time_offset_with_timestamp(self):
369
"""Test that local_time_offset() works with a timestamp."""
370
offset = osutils.local_time_offset(1000000000.1234567)
371
self.assertTrue(isinstance(offset, int))
372
eighteen_hours = 18 * 3600
373
self.assertTrue(-eighteen_hours < offset < eighteen_hours)
376
class TestLinks(tests.TestCaseInTempDir):
280
378
def test_dereference_path(self):
281
379
self.requireFeature(tests.SymlinkFeature)
282
380
cwd = osutils.realpath('.')
959
1053
self._save_platform_info()
960
1054
win32utils.winver = None # Avoid the win32 detection code
961
1055
osutils._fs_enc = 'UTF-8'
962
self.assertReadFSDirIs(UTF8DirReaderFeature.reader)
1056
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
964
1058
def test_force_walkdirs_utf8_fs_ascii(self):
965
1059
self.requireFeature(UTF8DirReaderFeature)
966
1060
self._save_platform_info()
967
1061
win32utils.winver = None # Avoid the win32 detection code
968
1062
osutils._fs_enc = 'US-ASCII'
969
self.assertReadFSDirIs(UTF8DirReaderFeature.reader)
1063
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
971
1065
def test_force_walkdirs_utf8_fs_ANSI(self):
972
1066
self.requireFeature(UTF8DirReaderFeature)
973
1067
self._save_platform_info()
974
1068
win32utils.winver = None # Avoid the win32 detection code
975
1069
osutils._fs_enc = 'ANSI_X3.4-1968'
976
self.assertReadFSDirIs(UTF8DirReaderFeature.reader)
1070
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
978
1072
def test_force_walkdirs_utf8_fs_latin1(self):
979
1073
self._save_platform_info()
980
1074
win32utils.winver = None # Avoid the win32 detection code
981
1075
osutils._fs_enc = 'latin1'
982
self.assertReadFSDirIs(osutils.UnicodeDirReader)
1076
self.assertDirReaderIs(osutils.UnicodeDirReader)
984
1078
def test_force_walkdirs_utf8_nt(self):
985
1079
# Disabled because the thunk of the whole walkdirs api is disabled.
1422
1516
self.failIf('BZR_TEST_ENV_VAR' in os.environ)
1425
class TestLocalTimeOffset(tests.TestCase):
1427
def test_local_time_offset(self):
1428
"""Test that local_time_offset() returns a sane value."""
1429
offset = osutils.local_time_offset()
1430
self.assertTrue(isinstance(offset, int))
1431
# Test that the offset is no more than a eighteen hours in
1433
# Time zone handling is system specific, so it is difficult to
1434
# do more specific tests, but a value outside of this range is
1436
eighteen_hours = 18 * 3600
1437
self.assertTrue(-eighteen_hours < offset < eighteen_hours)
1439
def test_local_time_offset_with_timestamp(self):
1440
"""Test that local_time_offset() works with a timestamp."""
1441
offset = osutils.local_time_offset(1000000000.1234567)
1442
self.assertTrue(isinstance(offset, int))
1443
eighteen_hours = 18 * 3600
1444
self.assertTrue(-eighteen_hours < offset < eighteen_hours)
1447
1519
class TestSizeShaFile(tests.TestCaseInTempDir):
1449
1521
def test_sha_empty(self):
1512
1584
"Invalid regular expression in test case: '*': "
1513
1585
"nothing to repeat",
1589
class TestDirReader(tests.TestCaseInTempDir):
1592
_dir_reader_class = None
1593
_native_to_unicode = None
1596
tests.TestCaseInTempDir.setUp(self)
1598
# Save platform specific info and reset it
1599
cur_dir_reader = osutils._selected_dir_reader
1602
osutils._selected_dir_reader = cur_dir_reader
1603
self.addCleanup(restore)
1605
osutils._selected_dir_reader = self._dir_reader_class()
1607
def _get_ascii_tree(self):
1615
expected_dirblocks = [
1617
[('0file', '0file', 'file'),
1618
('1dir', '1dir', 'directory'),
1619
('2file', '2file', 'file'),
1622
(('1dir', './1dir'),
1623
[('1dir/0file', '0file', 'file'),
1624
('1dir/1dir', '1dir', 'directory'),
1627
(('1dir/1dir', './1dir/1dir'),
1632
return tree, expected_dirblocks
1634
def test_walk_cur_dir(self):
1635
tree, expected_dirblocks = self._get_ascii_tree()
1636
self.build_tree(tree)
1637
result = list(osutils._walkdirs_utf8('.'))
1638
# Filter out stat and abspath
1639
self.assertEqual(expected_dirblocks,
1640
[(dirinfo, [line[0:3] for line in block])
1641
for dirinfo, block in result])
1643
def test_walk_sub_dir(self):
1644
tree, expected_dirblocks = self._get_ascii_tree()
1645
self.build_tree(tree)
1646
# you can search a subdir only, with a supplied prefix.
1647
result = list(osutils._walkdirs_utf8('./1dir', '1dir'))
1648
# Filter out stat and abspath
1649
self.assertEqual(expected_dirblocks[1:],
1650
[(dirinfo, [line[0:3] for line in block])
1651
for dirinfo, block in result])
1653
def _get_unicode_tree(self):
1654
name0u = u'0file-\xb6'
1655
name1u = u'1dir-\u062c\u0648'
1656
name2u = u'2file-\u0633'
1660
name1u + '/' + name0u,
1661
name1u + '/' + name1u + '/',
1664
name0 = name0u.encode('UTF-8')
1665
name1 = name1u.encode('UTF-8')
1666
name2 = name2u.encode('UTF-8')
1667
expected_dirblocks = [
1669
[(name0, name0, 'file', './' + name0u),
1670
(name1, name1, 'directory', './' + name1u),
1671
(name2, name2, 'file', './' + name2u),
1674
((name1, './' + name1u),
1675
[(name1 + '/' + name0, name0, 'file', './' + name1u
1677
(name1 + '/' + name1, name1, 'directory', './' + name1u
1681
((name1 + '/' + name1, './' + name1u + '/' + name1u),
1686
return tree, expected_dirblocks
1688
def _filter_out(self, raw_dirblocks):
1689
"""Filter out a walkdirs_utf8 result.
1691
stat field is removed, all native paths are converted to unicode
1693
filtered_dirblocks = []
1694
for dirinfo, block in raw_dirblocks:
1695
dirinfo = (dirinfo[0], self._native_to_unicode(dirinfo[1]))
1698
details.append(line[0:3] + (self._native_to_unicode(line[4]), ))
1699
filtered_dirblocks.append((dirinfo, details))
1700
return filtered_dirblocks
1702
def test_walk_unicode_tree(self):
1703
self.requireFeature(tests.UnicodeFilenameFeature)
1704
tree, expected_dirblocks = self._get_unicode_tree()
1705
self.build_tree(tree)
1706
result = list(osutils._walkdirs_utf8('.'))
1707
self.assertEqual(expected_dirblocks, self._filter_out(result))
1709
def test_symlink(self):
1710
self.requireFeature(tests.SymlinkFeature)
1711
self.requireFeature(tests.UnicodeFilenameFeature)
1712
target = u'target\N{Euro Sign}'
1713
link_name = u'l\N{Euro Sign}nk'
1714
os.symlink(target, link_name)
1715
target_utf8 = target.encode('UTF-8')
1716
link_name_utf8 = link_name.encode('UTF-8')
1717
expected_dirblocks = [
1719
[(link_name_utf8, link_name_utf8,
1720
'symlink', './' + link_name),],
1722
result = list(osutils._walkdirs_utf8('.'))
1723
self.assertEqual(expected_dirblocks, self._filter_out(result))
1726
class TestReadLink(tests.TestCaseInTempDir):
1727
"""Exposes os.readlink() problems and the osutils solution.
1729
The only guarantee offered by os.readlink(), starting with 2.6, is that a
1730
unicode string will be returned if a unicode string is passed.
1732
But prior python versions failed to properly encode the passed unicode
1735
_test_needs_features = [tests.SymlinkFeature, tests.UnicodeFilenameFeature]
1738
super(tests.TestCaseInTempDir, self).setUp()
1739
self.link = u'l\N{Euro Sign}ink'
1740
self.target = u'targe\N{Euro Sign}t'
1741
os.symlink(self.target, self.link)
1743
def test_os_readlink_link_encoding(self):
1744
if sys.version_info < (2, 6):
1745
self.assertRaises(UnicodeEncodeError, os.readlink, self.link)
1747
self.assertEquals(self.target, os.readlink(self.link))
1749
def test_os_readlink_link_decoding(self):
1750
self.assertEquals(self.target.encode(osutils._fs_enc),
1751
os.readlink(self.link.encode(osutils._fs_enc)))