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('.')
957
1051
self._save_platform_info()
958
1052
win32utils.winver = None # Avoid the win32 detection code
959
1053
osutils._fs_enc = 'UTF-8'
960
self.assertReadFSDirIs(UTF8DirReaderFeature.reader)
1054
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
962
1056
def test_force_walkdirs_utf8_fs_ascii(self):
963
1057
self.requireFeature(UTF8DirReaderFeature)
964
1058
self._save_platform_info()
965
1059
win32utils.winver = None # Avoid the win32 detection code
966
1060
osutils._fs_enc = 'US-ASCII'
967
self.assertReadFSDirIs(UTF8DirReaderFeature.reader)
1061
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
969
1063
def test_force_walkdirs_utf8_fs_ANSI(self):
970
1064
self.requireFeature(UTF8DirReaderFeature)
971
1065
self._save_platform_info()
972
1066
win32utils.winver = None # Avoid the win32 detection code
973
1067
osutils._fs_enc = 'ANSI_X3.4-1968'
974
self.assertReadFSDirIs(UTF8DirReaderFeature.reader)
1068
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
976
1070
def test_force_walkdirs_utf8_fs_latin1(self):
977
1071
self._save_platform_info()
978
1072
win32utils.winver = None # Avoid the win32 detection code
979
1073
osutils._fs_enc = 'latin1'
980
self.assertReadFSDirIs(osutils.UnicodeDirReader)
1074
self.assertDirReaderIs(osutils.UnicodeDirReader)
982
1076
def test_force_walkdirs_utf8_nt(self):
983
1077
# Disabled because the thunk of the whole walkdirs api is disabled.
1420
1514
self.failIf('BZR_TEST_ENV_VAR' in os.environ)
1423
class TestLocalTimeOffset(tests.TestCase):
1425
def test_local_time_offset(self):
1426
"""Test that local_time_offset() returns a sane value."""
1427
offset = osutils.local_time_offset()
1428
self.assertTrue(isinstance(offset, int))
1429
# Test that the offset is no more than a eighteen hours in
1431
# Time zone handling is system specific, so it is difficult to
1432
# do more specific tests, but a value outside of this range is
1434
eighteen_hours = 18 * 3600
1435
self.assertTrue(-eighteen_hours < offset < eighteen_hours)
1437
def test_local_time_offset_with_timestamp(self):
1438
"""Test that local_time_offset() works with a timestamp."""
1439
offset = osutils.local_time_offset(1000000000.1234567)
1440
self.assertTrue(isinstance(offset, int))
1441
eighteen_hours = 18 * 3600
1442
self.assertTrue(-eighteen_hours < offset < eighteen_hours)
1445
1517
class TestSizeShaFile(tests.TestCaseInTempDir):
1447
1519
def test_sha_empty(self):
1510
1582
"Invalid regular expression in test case: '*': "
1511
1583
"nothing to repeat",
1587
class TestDirReader(tests.TestCaseInTempDir):
1590
_dir_reader_class = None
1591
_native_to_unicode = None
1594
tests.TestCaseInTempDir.setUp(self)
1596
# Save platform specific info and reset it
1597
cur_dir_reader = osutils._selected_dir_reader
1600
osutils._selected_dir_reader = cur_dir_reader
1601
self.addCleanup(restore)
1603
osutils._selected_dir_reader = self._dir_reader_class()
1605
def _get_ascii_tree(self):
1613
expected_dirblocks = [
1615
[('0file', '0file', 'file'),
1616
('1dir', '1dir', 'directory'),
1617
('2file', '2file', 'file'),
1620
(('1dir', './1dir'),
1621
[('1dir/0file', '0file', 'file'),
1622
('1dir/1dir', '1dir', 'directory'),
1625
(('1dir/1dir', './1dir/1dir'),
1630
return tree, expected_dirblocks
1632
def test_walk_cur_dir(self):
1633
tree, expected_dirblocks = self._get_ascii_tree()
1634
self.build_tree(tree)
1635
result = list(osutils._walkdirs_utf8('.'))
1636
# Filter out stat and abspath
1637
self.assertEqual(expected_dirblocks,
1638
[(dirinfo, [line[0:3] for line in block])
1639
for dirinfo, block in result])
1641
def test_walk_sub_dir(self):
1642
tree, expected_dirblocks = self._get_ascii_tree()
1643
self.build_tree(tree)
1644
# you can search a subdir only, with a supplied prefix.
1645
result = list(osutils._walkdirs_utf8('./1dir', '1dir'))
1646
# Filter out stat and abspath
1647
self.assertEqual(expected_dirblocks[1:],
1648
[(dirinfo, [line[0:3] for line in block])
1649
for dirinfo, block in result])
1651
def _get_unicode_tree(self):
1652
name0u = u'0file-\xb6'
1653
name1u = u'1dir-\u062c\u0648'
1654
name2u = u'2file-\u0633'
1658
name1u + '/' + name0u,
1659
name1u + '/' + name1u + '/',
1662
name0 = name0u.encode('UTF-8')
1663
name1 = name1u.encode('UTF-8')
1664
name2 = name2u.encode('UTF-8')
1665
expected_dirblocks = [
1667
[(name0, name0, 'file', './' + name0u),
1668
(name1, name1, 'directory', './' + name1u),
1669
(name2, name2, 'file', './' + name2u),
1672
((name1, './' + name1u),
1673
[(name1 + '/' + name0, name0, 'file', './' + name1u
1675
(name1 + '/' + name1, name1, 'directory', './' + name1u
1679
((name1 + '/' + name1, './' + name1u + '/' + name1u),
1684
return tree, expected_dirblocks
1686
def _filter_out(self, raw_dirblocks):
1687
"""Filter out a walkdirs_utf8 result.
1689
stat field is removed, all native paths are converted to unicode
1691
filtered_dirblocks = []
1692
for dirinfo, block in raw_dirblocks:
1693
dirinfo = (dirinfo[0], self._native_to_unicode(dirinfo[1]))
1696
details.append(line[0:3] + (self._native_to_unicode(line[4]), ))
1697
filtered_dirblocks.append((dirinfo, details))
1698
return filtered_dirblocks
1700
def test_walk_unicode_tree(self):
1701
self.requireFeature(tests.UnicodeFilenameFeature)
1702
tree, expected_dirblocks = self._get_unicode_tree()
1703
self.build_tree(tree)
1704
result = list(osutils._walkdirs_utf8('.'))
1705
self.assertEqual(expected_dirblocks, self._filter_out(result))
1707
def test_symlink(self):
1708
self.requireFeature(tests.SymlinkFeature)
1709
self.requireFeature(tests.UnicodeFilenameFeature)
1710
target = u'target\N{Euro Sign}'
1711
link_name = u'l\N{Euro Sign}nk'
1712
os.symlink(target, link_name)
1713
target_utf8 = target.encode('UTF-8')
1714
link_name_utf8 = link_name.encode('UTF-8')
1715
expected_dirblocks = [
1717
[(link_name_utf8, link_name_utf8,
1718
'symlink', './' + link_name),],
1720
result = list(osutils._walkdirs_utf8('.'))
1721
self.assertEqual(expected_dirblocks, self._filter_out(result))
1724
class TestReadLink(tests.TestCaseInTempDir):
1725
"""Exposes os.readlink() problems and the osutils solution.
1727
The only guarantee offered by os.readlink(), starting with 2.6, is that a
1728
unicode string will be returned if a unicode string is passed.
1730
But prior python versions failed to properly encode the passed unicode
1733
_test_needs_features = [tests.SymlinkFeature, tests.UnicodeFilenameFeature]
1736
super(tests.TestCaseInTempDir, self).setUp()
1737
self.link = u'l\N{Euro Sign}ink'
1738
self.target = u'targe\N{Euro Sign}t'
1739
os.symlink(self.target, self.link)
1741
def test_os_readlink_link_encoding(self):
1742
if sys.version_info < (2, 6):
1743
self.assertRaises(UnicodeEncodeError, os.readlink, self.link)
1745
self.assertEquals(self.target, os.readlink(self.link))
1747
def test_os_readlink_link_decoding(self):
1748
self.assertEquals(self.target.encode(osutils._fs_enc),
1749
os.readlink(self.link.encode(osutils._fs_enc)))