71
53
UTF8DirReaderFeature = _UTF8DirReaderFeature()
74
class TestOSUtils(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):
76
111
def test_contains_whitespace(self):
77
112
self.failUnless(osutils.contains_whitespace(u' '))
297
353
# Instead blackbox.test_locale should check for localized
298
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):
300
378
def test_dereference_path(self):
301
self.requireFeature(SymlinkFeature)
379
self.requireFeature(tests.SymlinkFeature)
302
380
cwd = osutils.realpath('.')
304
382
bar_path = osutils.pathjoin(cwd, 'bar')
345
423
osutils.make_readonly('dangling')
346
424
osutils.make_writable('dangling')
348
def test_kind_marker(self):
349
self.assertEqual("", osutils.kind_marker("file"))
350
self.assertEqual("/", osutils.kind_marker(osutils._directory_kind))
351
self.assertEqual("@", osutils.kind_marker("symlink"))
352
self.assertRaises(errors.BzrError, osutils.kind_marker, "unknown")
354
426
def test_host_os_dereferences_symlinks(self):
355
427
osutils.host_os_dereferences_symlinks()
358
class TestCanonicalRelPath(TestCaseInTempDir):
430
class TestCanonicalRelPath(tests.TestCaseInTempDir):
360
_test_needs_features = [CaseInsCasePresFilenameFeature]
432
_test_needs_features = [tests.CaseInsCasePresFilenameFeature]
362
434
def test_canonical_relpath_simple(self):
363
435
f = file('MixedCaseName', 'w')
365
self.failUnlessEqual(
366
canonical_relpath(self.test_base_dir, 'mixedcasename'),
367
'work/MixedCaseName')
437
# Watch out for tricky test dir (on OSX /tmp -> /private/tmp)
438
real_base_dir = osutils.realpath(self.test_base_dir)
439
actual = osutils.canonical_relpath(real_base_dir, 'mixedcasename')
440
self.failUnlessEqual('work/MixedCaseName', actual)
369
442
def test_canonical_relpath_missing_tail(self):
370
443
os.mkdir('MixedCaseParent')
371
self.failUnlessEqual(
372
canonical_relpath(self.test_base_dir, 'mixedcaseparent/nochild'),
373
'work/MixedCaseParent/nochild')
376
class TestPumpFile(TestCase):
444
# Watch out for tricky test dir (on OSX /tmp -> /private/tmp)
445
real_base_dir = osutils.realpath(self.test_base_dir)
446
actual = osutils.canonical_relpath(real_base_dir,
447
'mixedcaseparent/nochild')
448
self.failUnlessEqual('work/MixedCaseParent/nochild', actual)
451
class TestPumpFile(tests.TestCase):
377
452
"""Test pumpfile method."""
455
tests.TestCase.setUp(self)
379
456
# create a test datablock
380
457
self.block_size = 512
381
458
pattern = '0123456789ABCDEF'
388
465
# make sure test data is larger than max read size
389
466
self.assertTrue(self.test_data_len > self.block_size)
391
from_file = FakeReadFile(self.test_data)
468
from_file = file_utils.FakeReadFile(self.test_data)
392
469
to_file = StringIO()
394
471
# read (max / 2) bytes and verify read size wasn't affected
395
472
num_bytes_to_read = self.block_size / 2
396
pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
473
osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
397
474
self.assertEqual(from_file.get_max_read_size(), num_bytes_to_read)
398
475
self.assertEqual(from_file.get_read_count(), 1)
400
477
# read (max) bytes and verify read size wasn't affected
401
478
num_bytes_to_read = self.block_size
402
479
from_file.reset_read_count()
403
pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
480
osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
404
481
self.assertEqual(from_file.get_max_read_size(), num_bytes_to_read)
405
482
self.assertEqual(from_file.get_read_count(), 1)
407
484
# read (max + 1) bytes and verify read size was limited
408
485
num_bytes_to_read = self.block_size + 1
409
486
from_file.reset_read_count()
410
pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
487
osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
411
488
self.assertEqual(from_file.get_max_read_size(), self.block_size)
412
489
self.assertEqual(from_file.get_read_count(), 2)
414
491
# finish reading the rest of the data
415
492
num_bytes_to_read = self.test_data_len - to_file.tell()
416
pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
493
osutils.pumpfile(from_file, to_file, num_bytes_to_read, self.block_size)
418
495
# report error if the data wasn't equal (we only report the size due
419
496
# to the length of the data)
489
567
activity.append((length, direction))
490
568
from_file = StringIO(self.test_data)
491
569
to_file = StringIO()
492
pumpfile(from_file, to_file, buff_size=500,
493
report_activity=log_activity, direction='read')
570
osutils.pumpfile(from_file, to_file, buff_size=500,
571
report_activity=log_activity, direction='read')
494
572
self.assertEqual([(500, 'read'), (500, 'read'), (500, 'read'),
495
573
(36, 'read')], activity)
497
575
from_file = StringIO(self.test_data)
498
576
to_file = StringIO()
500
pumpfile(from_file, to_file, buff_size=500,
501
report_activity=log_activity, direction='write')
578
osutils.pumpfile(from_file, to_file, buff_size=500,
579
report_activity=log_activity, direction='write')
502
580
self.assertEqual([(500, 'write'), (500, 'write'), (500, 'write'),
503
581
(36, 'write')], activity)
506
584
from_file = StringIO(self.test_data)
507
585
to_file = StringIO()
509
pumpfile(from_file, to_file, buff_size=500, read_length=1028,
510
report_activity=log_activity, direction='read')
587
osutils.pumpfile(from_file, to_file, buff_size=500, read_length=1028,
588
report_activity=log_activity, direction='read')
511
589
self.assertEqual([(500, 'read'), (500, 'read'), (28, 'read')], activity)
515
class TestPumpStringFile(TestCase):
593
class TestPumpStringFile(tests.TestCase):
517
595
def test_empty(self):
518
596
output = StringIO()
519
pump_string_file("", output)
597
osutils.pump_string_file("", output)
520
598
self.assertEqual("", output.getvalue())
522
600
def test_more_than_segment_size(self):
523
601
output = StringIO()
524
pump_string_file("123456789", output, 2)
602
osutils.pump_string_file("123456789", output, 2)
525
603
self.assertEqual("123456789", output.getvalue())
527
605
def test_segment_size(self):
528
606
output = StringIO()
529
pump_string_file("12", output, 2)
607
osutils.pump_string_file("12", output, 2)
530
608
self.assertEqual("12", output.getvalue())
532
610
def test_segment_size_multiple(self):
533
611
output = StringIO()
534
pump_string_file("1234", output, 2)
612
osutils.pump_string_file("1234", output, 2)
535
613
self.assertEqual("1234", output.getvalue())
538
class TestSafeUnicode(TestCase):
616
class TestSafeUnicode(tests.TestCase):
540
618
def test_from_ascii_string(self):
541
619
self.assertEqual(u'foobar', osutils.safe_unicode('foobar'))
643
721
self.assertEqual('C:/foo', osutils._win32_realpath('C:/foo'))
645
723
def test_pathjoin(self):
646
self.assertEqual('path/to/foo', osutils._win32_pathjoin('path', 'to', 'foo'))
647
self.assertEqual('C:/foo', osutils._win32_pathjoin('path\\to', 'C:\\foo'))
648
self.assertEqual('C:/foo', osutils._win32_pathjoin('path/to', 'C:/foo'))
649
self.assertEqual('path/to/foo', osutils._win32_pathjoin('path/to/', 'foo'))
650
self.assertEqual('/foo', osutils._win32_pathjoin('C:/path/to/', '/foo'))
651
self.assertEqual('/foo', osutils._win32_pathjoin('C:\\path\\to\\', '\\foo'))
724
self.assertEqual('path/to/foo',
725
osutils._win32_pathjoin('path', 'to', 'foo'))
726
self.assertEqual('C:/foo',
727
osutils._win32_pathjoin('path\\to', 'C:\\foo'))
728
self.assertEqual('C:/foo',
729
osutils._win32_pathjoin('path/to', 'C:/foo'))
730
self.assertEqual('path/to/foo',
731
osutils._win32_pathjoin('path/to/', 'foo'))
732
self.assertEqual('/foo',
733
osutils._win32_pathjoin('C:/path/to/', '/foo'))
734
self.assertEqual('/foo',
735
osutils._win32_pathjoin('C:\\path\\to\\', '\\foo'))
653
737
def test_normpath(self):
654
self.assertEqual('path/to/foo', osutils._win32_normpath(r'path\\from\..\to\.\foo'))
655
self.assertEqual('path/to/foo', osutils._win32_normpath('path//from/../to/./foo'))
738
self.assertEqual('path/to/foo',
739
osutils._win32_normpath(r'path\\from\..\to\.\foo'))
740
self.assertEqual('path/to/foo',
741
osutils._win32_normpath('path//from/../to/./foo'))
657
743
def test_getcwd(self):
658
744
cwd = osutils._win32_getcwd()
776
860
self.assertRaises(errors.BzrError, osutils.splitpath, 'a/../b')
779
class TestMacFuncsDirs(TestCaseInTempDir):
863
class TestMacFuncsDirs(tests.TestCaseInTempDir):
780
864
"""Test mac special functions that require directories."""
782
866
def test_getcwd(self):
783
# On Mac, this will actually create Ba\u030agfors
784
# but chdir will still work, because it accepts both paths
786
os.mkdir(u'B\xe5gfors')
788
raise TestSkipped("Unable to create Unicode filename")
867
self.requireFeature(tests.UnicodeFilenameFeature)
868
os.mkdir(u'B\xe5gfors')
790
869
os.chdir(u'B\xe5gfors')
791
870
self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
793
872
def test_getcwd_nonnorm(self):
873
self.requireFeature(tests.UnicodeFilenameFeature)
794
874
# Test that _mac_getcwd() will normalize this path
796
os.mkdir(u'Ba\u030agfors')
798
raise TestSkipped("Unable to create Unicode filename")
875
os.mkdir(u'Ba\u030agfors')
800
876
os.chdir(u'Ba\u030agfors')
801
877
self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
804
class TestChunksToLines(TestCase):
880
class TestChunksToLines(tests.TestCase):
806
882
def test_smoketest(self):
807
883
self.assertEqual(['foo\n', 'bar\n', 'baz\n'],
889
968
os.mkdir("test-unreadable")
890
969
os.chmod("test-unreadable", 0000)
891
970
# must chmod it back so that it can be removed
892
self.addCleanup(lambda: os.chmod("test-unreadable", 0700))
971
self.addCleanup(os.chmod, "test-unreadable", 0700)
893
972
# The error is not raised until the generator is actually evaluated.
894
973
# (It would be ok if it happened earlier but at the moment it
896
e = self.assertRaises(OSError, list,
897
osutils._walkdirs_utf8("."))
898
self.assertEquals(e.filename, './test-unreadable')
899
self.assertEquals(str(e),
900
"[Errno 13] chdir: Permission denied: './test-unreadable'")
975
e = self.assertRaises(OSError, list, osutils._walkdirs_utf8("."))
976
self.assertEquals('./test-unreadable', e.filename)
977
self.assertEquals(errno.EACCES, e.errno)
978
# Ensure the message contains the file name
979
self.assertContainsRe(str(e), "\./test-unreadable")
902
981
def test__walkdirs_utf8(self):
977
1055
self._save_platform_info()
978
1056
win32utils.winver = None # Avoid the win32 detection code
979
1057
osutils._fs_enc = 'UTF-8'
980
self.assertReadFSDirIs(UTF8DirReaderFeature.reader)
1058
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
982
1060
def test_force_walkdirs_utf8_fs_ascii(self):
983
1061
self.requireFeature(UTF8DirReaderFeature)
984
1062
self._save_platform_info()
985
1063
win32utils.winver = None # Avoid the win32 detection code
986
1064
osutils._fs_enc = 'US-ASCII'
987
self.assertReadFSDirIs(UTF8DirReaderFeature.reader)
1065
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
989
1067
def test_force_walkdirs_utf8_fs_ANSI(self):
990
1068
self.requireFeature(UTF8DirReaderFeature)
991
1069
self._save_platform_info()
992
1070
win32utils.winver = None # Avoid the win32 detection code
993
1071
osutils._fs_enc = 'ANSI_X3.4-1968'
994
self.assertReadFSDirIs(UTF8DirReaderFeature.reader)
1072
self.assertDirReaderIs(UTF8DirReaderFeature.reader)
996
1074
def test_force_walkdirs_utf8_fs_latin1(self):
997
1075
self._save_platform_info()
998
1076
win32utils.winver = None # Avoid the win32 detection code
999
1077
osutils._fs_enc = 'latin1'
1000
self.assertReadFSDirIs(osutils.UnicodeDirReader)
1078
self.assertDirReaderIs(osutils.UnicodeDirReader)
1002
1080
def test_force_walkdirs_utf8_nt(self):
1003
1081
# Disabled because the thunk of the whole walkdirs api is disabled.
1004
self.requireFeature(Win32ReadDirFeature)
1082
self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
1005
1083
self._save_platform_info()
1006
1084
win32utils.winver = 'Windows NT'
1007
1085
from bzrlib._walkdirs_win32 import Win32ReadDir
1008
self.assertReadFSDirIs(Win32ReadDir)
1086
self.assertDirReaderIs(Win32ReadDir)
1010
1088
def test_force_walkdirs_utf8_98(self):
1011
self.requireFeature(Win32ReadDirFeature)
1089
self.requireFeature(test__walkdirs_win32.Win32ReadDirFeature)
1012
1090
self._save_platform_info()
1013
1091
win32utils.winver = 'Windows 98'
1014
self.assertReadFSDirIs(osutils.UnicodeDirReader)
1092
self.assertDirReaderIs(osutils.UnicodeDirReader)
1016
1094
def test_unicode_walkdirs(self):
1017
1095
"""Walkdirs should always return unicode paths."""
1096
self.requireFeature(tests.UnicodeFilenameFeature)
1018
1097
name0 = u'0file-\xb6'
1019
1098
name1 = u'1dir-\u062c\u0648'
1020
1099
name2 = u'2file-\u0633'
1452
1518
self.failIf('BZR_TEST_ENV_VAR' in os.environ)
1455
class TestLocalTimeOffset(TestCase):
1457
def test_local_time_offset(self):
1458
"""Test that local_time_offset() returns a sane value."""
1459
offset = osutils.local_time_offset()
1460
self.assertTrue(isinstance(offset, int))
1461
# Test that the offset is no more than a eighteen hours in
1463
# Time zone handling is system specific, so it is difficult to
1464
# do more specific tests, but a value outside of this range is
1466
eighteen_hours = 18 * 3600
1467
self.assertTrue(-eighteen_hours < offset < eighteen_hours)
1469
def test_local_time_offset_with_timestamp(self):
1470
"""Test that local_time_offset() works with a timestamp."""
1471
offset = osutils.local_time_offset(1000000000.1234567)
1472
self.assertTrue(isinstance(offset, int))
1473
eighteen_hours = 18 * 3600
1474
self.assertTrue(-eighteen_hours < offset < eighteen_hours)
1477
class TestShaFileByName(TestCaseInTempDir):
1479
def test_sha_empty(self):
1480
self.build_tree_contents([('foo', '')])
1481
expected_sha = osutils.sha_string('')
1482
self.assertEqual(expected_sha, osutils.sha_file_by_name('foo'))
1484
def test_sha_mixed_endings(self):
1485
text = 'test\r\nwith\nall\rpossible line endings\r\n'
1486
self.build_tree_contents([('foo', text)])
1487
expected_sha = osutils.sha_string(text)
1488
self.assertEqual(expected_sha, osutils.sha_file_by_name('foo'))
1491
class TestResourceLoading(TestCaseInTempDir):
1521
class TestSizeShaFile(tests.TestCaseInTempDir):
1523
def test_sha_empty(self):
1524
self.build_tree_contents([('foo', '')])
1525
expected_sha = osutils.sha_string('')
1527
self.addCleanup(f.close)
1528
size, sha = osutils.size_sha_file(f)
1529
self.assertEqual(0, size)
1530
self.assertEqual(expected_sha, sha)
1532
def test_sha_mixed_endings(self):
1533
text = 'test\r\nwith\nall\rpossible line endings\r\n'
1534
self.build_tree_contents([('foo', text)])
1535
expected_sha = osutils.sha_string(text)
1537
self.addCleanup(f.close)
1538
size, sha = osutils.size_sha_file(f)
1539
self.assertEqual(38, size)
1540
self.assertEqual(expected_sha, sha)
1543
class TestShaFileByName(tests.TestCaseInTempDir):
1545
def test_sha_empty(self):
1546
self.build_tree_contents([('foo', '')])
1547
expected_sha = osutils.sha_string('')
1548
self.assertEqual(expected_sha, osutils.sha_file_by_name('foo'))
1550
def test_sha_mixed_endings(self):
1551
text = 'test\r\nwith\nall\rpossible line endings\r\n'
1552
self.build_tree_contents([('foo', text)])
1553
expected_sha = osutils.sha_string(text)
1554
self.assertEqual(expected_sha, osutils.sha_file_by_name('foo'))
1557
class TestResourceLoading(tests.TestCaseInTempDir):
1493
1559
def test_resource_string(self):
1494
1560
# test resource in bzrlib
1503
1569
# test unknown resource
1504
1570
self.assertRaises(IOError, osutils.resource_string, 'bzrlib', 'yyy.xx')
1573
class TestReCompile(tests.TestCase):
1575
def test_re_compile_checked(self):
1576
r = osutils.re_compile_checked(r'A*', re.IGNORECASE)
1577
self.assertTrue(r.match('aaaa'))
1578
self.assertTrue(r.match('aAaA'))
1580
def test_re_compile_checked_error(self):
1581
# like https://bugs.launchpad.net/bzr/+bug/251352
1582
err = self.assertRaises(
1583
errors.BzrCommandError,
1584
osutils.re_compile_checked, '*', re.IGNORECASE, 'test case')
1586
"Invalid regular expression in test case: '*': "
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)))