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('.')
697
789
def test_minimum_path_selection(self):
698
790
self.assertEqual(set(),
699
791
osutils.minimum_path_selection([]))
792
self.assertEqual(set(['a']),
793
osutils.minimum_path_selection(['a']))
700
794
self.assertEqual(set(['a', 'b']),
701
795
osutils.minimum_path_selection(['a', 'b']))
702
796
self.assertEqual(set(['a/', 'b']),
703
797
osutils.minimum_path_selection(['a/', 'b']))
704
798
self.assertEqual(set(['a/', 'b']),
705
799
osutils.minimum_path_selection(['a/c', 'a/', 'b']))
800
self.assertEqual(set(['a-b', 'a', 'a0b']),
801
osutils.minimum_path_selection(['a-b', 'a/b', 'a0b', 'a']))
707
803
def test_mkdtemp(self):
708
804
tmpdir = osutils._win32_mkdtemp(dir='.')
957
1055
self._save_platform_info()
958
1056
win32utils.winver = None # Avoid the win32 detection code
959
1057
osutils._fs_enc = 'UTF-8'
960
self.assertReadFSDirIs(UTF8DirReaderFeature.reader)
1058
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
962
1060
def test_force_walkdirs_utf8_fs_ascii(self):
963
1061
self.requireFeature(UTF8DirReaderFeature)
964
1062
self._save_platform_info()
965
1063
win32utils.winver = None # Avoid the win32 detection code
966
1064
osutils._fs_enc = 'US-ASCII'
967
self.assertReadFSDirIs(UTF8DirReaderFeature.reader)
1065
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
969
1067
def test_force_walkdirs_utf8_fs_ANSI(self):
970
1068
self.requireFeature(UTF8DirReaderFeature)
971
1069
self._save_platform_info()
972
1070
win32utils.winver = None # Avoid the win32 detection code
973
1071
osutils._fs_enc = 'ANSI_X3.4-1968'
974
self.assertReadFSDirIs(UTF8DirReaderFeature.reader)
1072
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
976
1074
def test_force_walkdirs_utf8_fs_latin1(self):
977
1075
self._save_platform_info()
978
1076
win32utils.winver = None # Avoid the win32 detection code
979
1077
osutils._fs_enc = 'latin1'
980
self.assertReadFSDirIs(osutils.UnicodeDirReader)
1078
self.assertDirReaderIs(osutils.UnicodeDirReader)
982
1080
def test_force_walkdirs_utf8_nt(self):
983
1081
# Disabled because the thunk of the whole walkdirs api is disabled.
1420
1518
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
1521
class TestSizeShaFile(tests.TestCaseInTempDir):
1447
1523
def test_sha_empty(self):
1510
1586
"Invalid regular expression in test case: '*': "
1511
1587
"nothing to repeat",
1591
class TestDirReader(tests.TestCaseInTempDir):
1594
_dir_reader_class = None
1595
_native_to_unicode = None
1598
tests.TestCaseInTempDir.setUp(self)
1600
# Save platform specific info and reset it
1601
cur_dir_reader = osutils._selected_dir_reader
1604
osutils._selected_dir_reader = cur_dir_reader
1605
self.addCleanup(restore)
1607
osutils._selected_dir_reader = self._dir_reader_class()
1609
def _get_ascii_tree(self):
1617
expected_dirblocks = [
1619
[('0file', '0file', 'file'),
1620
('1dir', '1dir', 'directory'),
1621
('2file', '2file', 'file'),
1624
(('1dir', './1dir'),
1625
[('1dir/0file', '0file', 'file'),
1626
('1dir/1dir', '1dir', 'directory'),
1629
(('1dir/1dir', './1dir/1dir'),
1634
return tree, expected_dirblocks
1636
def test_walk_cur_dir(self):
1637
tree, expected_dirblocks = self._get_ascii_tree()
1638
self.build_tree(tree)
1639
result = list(osutils._walkdirs_utf8('.'))
1640
# Filter out stat and abspath
1641
self.assertEqual(expected_dirblocks,
1642
[(dirinfo, [line[0:3] for line in block])
1643
for dirinfo, block in result])
1645
def test_walk_sub_dir(self):
1646
tree, expected_dirblocks = self._get_ascii_tree()
1647
self.build_tree(tree)
1648
# you can search a subdir only, with a supplied prefix.
1649
result = list(osutils._walkdirs_utf8('./1dir', '1dir'))
1650
# Filter out stat and abspath
1651
self.assertEqual(expected_dirblocks[1:],
1652
[(dirinfo, [line[0:3] for line in block])
1653
for dirinfo, block in result])
1655
def _get_unicode_tree(self):
1656
name0u = u'0file-\xb6'
1657
name1u = u'1dir-\u062c\u0648'
1658
name2u = u'2file-\u0633'
1662
name1u + '/' + name0u,
1663
name1u + '/' + name1u + '/',
1666
name0 = name0u.encode('UTF-8')
1667
name1 = name1u.encode('UTF-8')
1668
name2 = name2u.encode('UTF-8')
1669
expected_dirblocks = [
1671
[(name0, name0, 'file', './' + name0u),
1672
(name1, name1, 'directory', './' + name1u),
1673
(name2, name2, 'file', './' + name2u),
1676
((name1, './' + name1u),
1677
[(name1 + '/' + name0, name0, 'file', './' + name1u
1679
(name1 + '/' + name1, name1, 'directory', './' + name1u
1683
((name1 + '/' + name1, './' + name1u + '/' + name1u),
1688
return tree, expected_dirblocks
1690
def _filter_out(self, raw_dirblocks):
1691
"""Filter out a walkdirs_utf8 result.
1693
stat field is removed, all native paths are converted to unicode
1695
filtered_dirblocks = []
1696
for dirinfo, block in raw_dirblocks:
1697
dirinfo = (dirinfo[0], self._native_to_unicode(dirinfo[1]))
1700
details.append(line[0:3] + (self._native_to_unicode(line[4]), ))
1701
filtered_dirblocks.append((dirinfo, details))
1702
return filtered_dirblocks
1704
def test_walk_unicode_tree(self):
1705
self.requireFeature(tests.UnicodeFilenameFeature)
1706
tree, expected_dirblocks = self._get_unicode_tree()
1707
self.build_tree(tree)
1708
result = list(osutils._walkdirs_utf8('.'))
1709
self.assertEqual(expected_dirblocks, self._filter_out(result))
1711
def test_symlink(self):
1712
self.requireFeature(tests.SymlinkFeature)
1713
self.requireFeature(tests.UnicodeFilenameFeature)
1714
target = u'target\N{Euro Sign}'
1715
link_name = u'l\N{Euro Sign}nk'
1716
os.symlink(target, link_name)
1717
target_utf8 = target.encode('UTF-8')
1718
link_name_utf8 = link_name.encode('UTF-8')
1719
expected_dirblocks = [
1721
[(link_name_utf8, link_name_utf8,
1722
'symlink', './' + link_name),],
1724
result = list(osutils._walkdirs_utf8('.'))
1725
self.assertEqual(expected_dirblocks, self._filter_out(result))
1728
class TestReadLink(tests.TestCaseInTempDir):
1729
"""Exposes os.readlink() problems and the osutils solution.
1731
The only guarantee offered by os.readlink(), starting with 2.6, is that a
1732
unicode string will be returned if a unicode string is passed.
1734
But prior python versions failed to properly encode the passed unicode
1737
_test_needs_features = [tests.SymlinkFeature, tests.UnicodeFilenameFeature]
1740
super(tests.TestCaseInTempDir, self).setUp()
1741
self.link = u'l\N{Euro Sign}ink'
1742
self.target = u'targe\N{Euro Sign}t'
1743
os.symlink(self.target, self.link)
1745
def test_os_readlink_link_encoding(self):
1746
if sys.version_info < (2, 6):
1747
self.assertRaises(UnicodeEncodeError, os.readlink, self.link)
1749
self.assertEquals(self.target, os.readlink(self.link))
1751
def test_os_readlink_link_decoding(self):
1752
self.assertEquals(self.target.encode(osutils._fs_enc),
1753
os.readlink(self.link.encode(osutils._fs_enc)))