54
54
from bzrlib.tests.file_utils import (
57
from bzrlib.tests.test__walkdirs_win32 import WalkdirsWin32Feature
60
def load_tests(standard_tests, module, loader):
61
"""Parameterize readdir tests."""
62
to_adapt, result = split_suite_by_re(standard_tests, "readdir")
63
adapter = TestScenarioApplier()
64
from bzrlib import _readdir_py
65
adapter.scenarios = [('python', {'read_dir': _readdir_py.read_dir})]
66
if ReadDirFeature.available():
67
adapter.scenarios.append(('pyrex',
68
{'read_dir': ReadDirFeature.read_dir}))
69
adapt_tests(to_adapt, adapter, result)
73
class _ReadDirFeature(Feature):
57
from bzrlib.tests.test__walkdirs_win32 import Win32ReadDirFeature
60
class _UTF8DirReaderFeature(Feature):
77
64
from bzrlib import _readdir_pyx
78
self.read_dir = _readdir_pyx.read_dir
65
self.reader = _readdir_pyx.UTF8DirReader
80
67
except ImportError:
780
767
class TestWalkDirs(TestCaseInTempDir):
782
def test_readdir(self):
791
self.build_tree(tree)
792
expected_names = ['.bzr', '0file', '1dir', '2file']
793
# read_dir returns pairs, which form a table with either None in all
794
# the first columns, or a sort key to get best on-disk-read order,
795
# and the disk path name in utf-8 encoding in the second column.
796
read_result = self.read_dir('.')
797
# The second column is always the names, and every name except "." and
798
# ".." should be present.
799
names = sorted([row[1] for row in read_result])
800
self.assertEqual(expected_names, names)
801
expected_sort_key = None
802
if read_result[0][0] is None:
803
# No sort key returned - all keys must None
804
operator = self.assertEqual
806
# A sort key in the first row implies sort keys in the other rows.
807
operator = self.assertNotEqual
808
for row in read_result:
809
operator(None, row[0])
811
def test_compiled_extension_exists(self):
812
self.requireFeature(ReadDirFeature)
814
769
def test_walkdirs(self):
912
867
new_dirblock.append((info[0], info[1], info[2], info[4]))
913
868
dirblock[:] = new_dirblock
915
def test__walkdirs_utf8_selection(self):
916
# Just trigger the function once, to make sure it has selected a real
918
list(osutils._walkdirs_utf8('.'))
919
if WalkdirsWin32Feature.available():
920
# If the compiled form is available, make sure it is used
921
from bzrlib._walkdirs_win32 import _walkdirs_utf8_win32_find_file
922
self.assertIs(_walkdirs_utf8_win32_find_file,
923
osutils._real_walkdirs_utf8)
924
elif sys.platform == 'win32':
925
self.assertIs(osutils._walkdirs_unicode_to_utf8,
926
osutils._real_walkdirs_utf8)
927
elif osutils._fs_enc.upper() in ('UTF-8', 'ASCII', 'ANSI_X3.4-1968'): # ascii
928
self.assertIs(osutils._walkdirs_fs_utf8,
929
osutils._real_walkdirs_utf8)
931
self.assertIs(osutils._walkdirs_unicode_to_utf8,
932
osutils._real_walkdirs_utf8)
934
870
def _save_platform_info(self):
935
871
cur_winver = win32utils.winver
936
872
cur_fs_enc = osutils._fs_enc
937
cur_real_walkdirs_utf8 = osutils._real_walkdirs_utf8
873
cur_dir_reader = osutils._selected_dir_reader
939
875
win32utils.winver = cur_winver
940
876
osutils._fs_enc = cur_fs_enc
941
osutils._real_walkdirs_utf8 = cur_real_walkdirs_utf8
877
osutils._selected_dir_reader = cur_dir_reader
942
878
self.addCleanup(restore)
944
def assertWalkdirsUtf8Is(self, expected):
880
def assertReadFSDirIs(self, expected):
945
881
"""Assert the right implementation for _walkdirs_utf8 is chosen."""
946
882
# Force it to redetect
947
osutils._real_walkdirs_utf8 = None
883
osutils._selected_dir_reader = None
948
884
# Nothing to list, but should still trigger the selection logic
949
885
self.assertEqual([(('', '.'), [])], list(osutils._walkdirs_utf8('.')))
950
self.assertIs(expected, osutils._real_walkdirs_utf8)
886
self.assertIsInstance(osutils._selected_dir_reader, expected)
952
888
def test_force_walkdirs_utf8_fs_utf8(self):
889
self.requireFeature(UTF8DirReaderFeature)
953
890
self._save_platform_info()
954
891
win32utils.winver = None # Avoid the win32 detection code
955
892
osutils._fs_enc = 'UTF-8'
956
self.assertWalkdirsUtf8Is(osutils._walkdirs_fs_utf8)
893
self.assertReadFSDirIs(UTF8DirReaderFeature.reader)
958
895
def test_force_walkdirs_utf8_fs_ascii(self):
896
self.requireFeature(UTF8DirReaderFeature)
959
897
self._save_platform_info()
960
898
win32utils.winver = None # Avoid the win32 detection code
961
899
osutils._fs_enc = 'US-ASCII'
962
self.assertWalkdirsUtf8Is(osutils._walkdirs_fs_utf8)
900
self.assertReadFSDirIs(UTF8DirReaderFeature.reader)
964
902
def test_force_walkdirs_utf8_fs_ANSI(self):
903
self.requireFeature(UTF8DirReaderFeature)
965
904
self._save_platform_info()
966
905
win32utils.winver = None # Avoid the win32 detection code
967
906
osutils._fs_enc = 'ANSI_X3.4-1968'
968
self.assertWalkdirsUtf8Is(osutils._walkdirs_fs_utf8)
907
self.assertReadFSDirIs(UTF8DirReaderFeature.reader)
970
909
def test_force_walkdirs_utf8_fs_latin1(self):
971
910
self._save_platform_info()
972
911
win32utils.winver = None # Avoid the win32 detection code
973
912
osutils._fs_enc = 'latin1'
974
self.assertWalkdirsUtf8Is(osutils._walkdirs_unicode_to_utf8)
913
self.assertReadFSDirIs(osutils.UnicodeDirReader)
976
915
def test_force_walkdirs_utf8_nt(self):
977
self.requireFeature(WalkdirsWin32Feature)
916
# Disabled because the thunk of the whole walkdirs api is disabled.
917
self.requireFeature(Win32ReadDirFeature)
978
918
self._save_platform_info()
979
919
win32utils.winver = 'Windows NT'
980
from bzrlib._walkdirs_win32 import _walkdirs_utf8_win32_find_file
981
self.assertWalkdirsUtf8Is(_walkdirs_utf8_win32_find_file)
920
from bzrlib._walkdirs_win32 import Win32ReadDir
921
self.assertReadFSDirIs(Win32ReadDir)
983
def test_force_walkdirs_utf8_nt(self):
984
self.requireFeature(WalkdirsWin32Feature)
923
def test_force_walkdirs_utf8_98(self):
924
self.requireFeature(Win32ReadDirFeature)
985
925
self._save_platform_info()
986
926
win32utils.winver = 'Windows 98'
987
self.assertWalkdirsUtf8Is(osutils._walkdirs_unicode_to_utf8)
927
self.assertReadFSDirIs(osutils.UnicodeDirReader)
989
929
def test_unicode_walkdirs(self):
990
930
"""Walkdirs should always return unicode paths."""
1223
1169
def test__walkdirs_utf_win32_find_file_stat_directory(self):
1224
1170
"""make sure our Stat values are valid"""
1225
self.requireFeature(WalkdirsWin32Feature)
1171
self.requireFeature(Win32ReadDirFeature)
1226
1172
self.requireFeature(tests.UnicodeFilenameFeature)
1227
from bzrlib._walkdirs_win32 import _walkdirs_utf8_win32_find_file
1173
from bzrlib._walkdirs_win32 import Win32ReadDir
1228
1174
name0u = u'0dir-\u062c\u0648'
1229
1175
name0 = name0u.encode('utf8')
1230
1176
self.build_tree([name0u + '/'])
1232
result = list(_walkdirs_utf8_win32_find_file(u'.'))
1233
entry = result[0][1][0]
1178
result = Win32ReadDir().read_dir('', u'.')
1234
1180
self.assertEqual((name0, name0, 'directory'), entry[:3])
1235
1181
self.assertEqual(u'./' + name0u, entry[4])
1236
1182
self.assertStatIsCorrect(entry[4], entry[3])