~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_osutils.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2008-09-24 01:43:25 UTC
  • mfrom: (3696.3.12 integration)
  • Revision ID: pqm@pqm.ubuntu.com-20080924014325-ucivgbdmsbuthnqw
(robertc) Accelerate _walkdirs_utf8 on unix platforms. (Robert
        Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
54
54
from bzrlib.tests.file_utils import (
55
55
    FakeReadFile,
56
56
    )
57
 
from bzrlib.tests.test__walkdirs_win32 import WalkdirsWin32Feature
58
 
 
59
 
 
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)
70
 
    return result
71
 
 
72
 
 
73
 
class _ReadDirFeature(Feature):
 
57
from bzrlib.tests.test__walkdirs_win32 import Win32ReadDirFeature
 
58
 
 
59
 
 
60
class _UTF8DirReaderFeature(Feature):
74
61
 
75
62
    def _probe(self):
76
63
        try:
77
64
            from bzrlib import _readdir_pyx
78
 
            self.read_dir = _readdir_pyx.read_dir
 
65
            self.reader = _readdir_pyx.UTF8DirReader
79
66
            return True
80
67
        except ImportError:
81
68
            return False
83
70
    def feature_name(self):
84
71
        return 'bzrlib._readdir_pyx'
85
72
 
86
 
ReadDirFeature = _ReadDirFeature()
 
73
UTF8DirReaderFeature = _UTF8DirReaderFeature()
87
74
 
88
75
 
89
76
class TestOSUtils(TestCaseInTempDir):
779
766
 
780
767
class TestWalkDirs(TestCaseInTempDir):
781
768
 
782
 
    def test_readdir(self):
783
 
        tree = [
784
 
            '.bzr/',
785
 
            '0file',
786
 
            '1dir/',
787
 
            '1dir/0file',
788
 
            '1dir/1dir/',
789
 
            '2file'
790
 
            ]
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
805
 
        else:
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])
810
 
 
811
 
    def test_compiled_extension_exists(self):
812
 
        self.requireFeature(ReadDirFeature)
813
 
        
814
769
    def test_walkdirs(self):
815
770
        tree = [
816
771
            '.bzr',
912
867
                new_dirblock.append((info[0], info[1], info[2], info[4]))
913
868
            dirblock[:] = new_dirblock
914
869
 
915
 
    def test__walkdirs_utf8_selection(self):
916
 
        # Just trigger the function once, to make sure it has selected a real
917
 
        # implementation.
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)
930
 
        else:
931
 
            self.assertIs(osutils._walkdirs_unicode_to_utf8,
932
 
                          osutils._real_walkdirs_utf8)
933
 
 
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
938
874
        def restore():
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)
943
879
 
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)
951
887
 
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)
957
894
 
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)
963
901
 
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)
969
908
 
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)
975
914
 
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)
982
922
 
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)
988
928
 
989
929
    def test_unicode_walkdirs(self):
990
930
        """Walkdirs should always return unicode paths."""
1093
1033
            result.append((dirdetail, new_dirblock))
1094
1034
        self.assertEqual(expected_dirblocks, result)
1095
1035
 
1096
 
    def test_unicode__walkdirs_unicode_to_utf8(self):
1097
 
        """walkdirs_unicode_to_utf8 should be a safe fallback everywhere
 
1036
    def test__walkdirs_utf8_with_unicode_fs(self):
 
1037
        """UnicodeDirReader should be a safe fallback everywhere
1098
1038
 
1099
1039
        The abspath portion should be in unicode
1100
1040
        """
 
1041
        # Use the unicode reader. TODO: split into driver-and-driven unit
 
1042
        # tests.
 
1043
        self._save_platform_info()
 
1044
        osutils._selected_dir_reader = osutils.UnicodeDirReader()
1101
1045
        name0u = u'0file-\xb6'
1102
1046
        name1u = u'1dir-\u062c\u0648'
1103
1047
        name2u = u'2file-\u0633'
1138
1082
                 ]
1139
1083
                ),
1140
1084
            ]
1141
 
        result = list(osutils._walkdirs_unicode_to_utf8('.'))
 
1085
        result = list(osutils._walkdirs_utf8('.'))
1142
1086
        self._filter_out_stat(result)
1143
1087
        self.assertEqual(expected_dirblocks, result)
1144
1088
 
1145
 
    def test__walkdirs_utf_win32_find_file(self):
1146
 
        self.requireFeature(WalkdirsWin32Feature)
 
1089
    def test__walkdirs_utf8_win32readdir(self):
 
1090
        self.requireFeature(Win32ReadDirFeature)
1147
1091
        self.requireFeature(tests.UnicodeFilenameFeature)
1148
 
        from bzrlib._walkdirs_win32 import _walkdirs_utf8_win32_find_file
 
1092
        from bzrlib._walkdirs_win32 import Win32ReadDir
 
1093
        self._save_platform_info()
 
1094
        osutils._selected_dir_reader = Win32ReadDir()
1149
1095
        name0u = u'0file-\xb6'
1150
1096
        name1u = u'1dir-\u062c\u0648'
1151
1097
        name2u = u'2file-\u0633'
1182
1128
                 ]
1183
1129
                ),
1184
1130
            ]
1185
 
        result = list(_walkdirs_utf8_win32_find_file(u'.'))
 
1131
        result = list(osutils._walkdirs_utf8(u'.'))
1186
1132
        self._filter_out_stat(result)
1187
1133
        self.assertEqual(expected_dirblocks, result)
1188
1134
 
1198
1144
 
1199
1145
    def test__walkdirs_utf_win32_find_file_stat_file(self):
1200
1146
        """make sure our Stat values are valid"""
1201
 
        self.requireFeature(WalkdirsWin32Feature)
 
1147
        self.requireFeature(Win32ReadDirFeature)
1202
1148
        self.requireFeature(tests.UnicodeFilenameFeature)
1203
 
        from bzrlib._walkdirs_win32 import _walkdirs_utf8_win32_find_file
 
1149
        from bzrlib._walkdirs_win32 import Win32ReadDir
1204
1150
        name0u = u'0file-\xb6'
1205
1151
        name0 = name0u.encode('utf8')
1206
1152
        self.build_tree([name0u])
1213
1159
        finally:
1214
1160
            f.close()
1215
1161
 
1216
 
        result = list(_walkdirs_utf8_win32_find_file(u'.'))
1217
 
        entry = result[0][1][0]
 
1162
        result = Win32ReadDir().read_dir('', u'.')
 
1163
        entry = result[0]
1218
1164
        self.assertEqual((name0, name0, 'file'), entry[:3])
1219
1165
        self.assertEqual(u'./' + name0u, entry[4])
1220
1166
        self.assertStatIsCorrect(entry[4], entry[3])
1222
1168
 
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 + '/'])
1231
1177
 
1232
 
        result = list(_walkdirs_utf8_win32_find_file(u'.'))
1233
 
        entry = result[0][1][0]
 
1178
        result = Win32ReadDir().read_dir('', u'.')
 
1179
        entry = result[0]
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])