0001 # Test some Unicode file name semantics 0002 # We dont test many operations on files other than 0003 # that their names can be used with Unicode characters. 0004 import os, glob, time, shutil 0005 import unicodedata 0006 0007 import unittest 0008 from test.test_support import run_suite, TestSkipped, TESTFN_UNICODE 0009 from test.test_support import TESTFN_ENCODING, TESTFN_UNICODE_UNENCODEABLE 0010 try: 0011 TESTFN_ENCODED = TESTFN_UNICODE.encode(TESTFN_ENCODING) 0012 except (UnicodeError, TypeError): 0013 # Either the file system encoding is None, or the file name 0014 # cannot be encoded in the file system encoding. 0015 raise TestSkipped("No Unicode filesystem semantics on this platform.") 0016 0017 def remove_if_exists(filename): 0018 if os.path.exists(filename): 0019 os.unlink(filename) 0020 0021 class TestUnicodeFiles(unittest.TestCase): 0022 # The 'do_' functions are the actual tests. They generally assume the 0023 # file already exists etc. 0024 0025 # Do all the tests we can given only a single filename. The file should 0026 # exist. 0027 def _do_single(self, filename): 0028 self.failUnless(os.path.exists(filename)) 0029 self.failUnless(os.path.isfile(filename)) 0030 self.failUnless(os.path.exists(os.path.abspath(filename))) 0031 self.failUnless(os.path.isfile(os.path.abspath(filename))) 0032 os.chmod(filename, 0777) 0033 os.utime(filename, None) 0034 os.utime(filename, (time.time(), time.time())) 0035 # Copy/rename etc tests using the same filename 0036 self._do_copyish(filename, filename) 0037 # Filename should appear in glob output 0038 self.failUnless( 0039 os.path.abspath(filename)==os.path.abspath(glob.glob(filename)[0])) 0040 # basename should appear in listdir. 0041 path, base = os.path.split(os.path.abspath(filename)) 0042 if isinstance(base, str): 0043 base = base.decode(TESTFN_ENCODING) 0044 file_list = os.listdir(path) 0045 # listdir() with a unicode arg may or may not return Unicode 0046 # objects, depending on the platform. 0047 if file_list and isinstance(file_list[0], str): 0048 file_list = [f.decode(TESTFN_ENCODING) for f in file_list] 0049 0050 # Normalize the unicode strings, as round-tripping the name via the OS 0051 # may return a different (but equivalent) value. 0052 base = unicodedata.normalize("NFD", base) 0053 file_list = [unicodedata.normalize("NFD", f) for f in file_list] 0054 0055 self.failUnless(base in file_list) 0056 0057 # Do as many "equivalancy' tests as we can - ie, check that although we 0058 # have different types for the filename, they refer to the same file. 0059 def _do_equivilent(self, filename1, filename2): 0060 # Note we only check "filename1 against filename2" - we don't bother 0061 # checking "filename2 against 1", as we assume we are called again with 0062 # the args reversed. 0063 self.failUnless(type(filename1)!=type(filename2), 0064 "No point checking equivalent filenames of the same type") 0065 # stat and lstat should return the same results. 0066 self.failUnlessEqual(os.stat(filename1), 0067 os.stat(filename2)) 0068 self.failUnlessEqual(os.lstat(filename1), 0069 os.lstat(filename2)) 0070 # Copy/rename etc tests using equivalent filename 0071 self._do_copyish(filename1, filename2) 0072 0073 # Tests that copy, move, etc one file to another. 0074 def _do_copyish(self, filename1, filename2): 0075 # Should be able to rename the file using either name. 0076 self.failUnless(os.path.isfile(filename1)) # must exist. 0077 os.rename(filename1, filename2 + ".new") 0078 self.failUnless(os.path.isfile(filename1+".new")) 0079 os.rename(filename1 + ".new", filename2) 0080 self.failUnless(os.path.isfile(filename2)) 0081 0082 # Try using shutil on the filenames. 0083 try: 0084 filename1==filename2 0085 except UnicodeDecodeError: 0086 # these filenames can't be compared - shutil.copy tries to do 0087 # just that. This is really a bug in 'shutil' - if one of shutil's 0088 # 2 params are Unicode and the other isn't, it should coerce the 0089 # string to Unicode with the filesystem encoding before comparison. 0090 pass 0091 else: 0092 # filenames can be compared. 0093 shutil.copy(filename1, filename2 + ".new") 0094 os.unlink(filename1 + ".new") # remove using equiv name. 0095 # And a couple of moves, one using each name. 0096 shutil.move(filename1, filename2 + ".new") 0097 self.failUnless(not os.path.exists(filename2)) 0098 shutil.move(filename1 + ".new", filename2) 0099 self.failUnless(os.path.exists(filename1)) 0100 # Note - due to the implementation of shutil.move, 0101 # it tries a rename first. This only fails on Windows when on 0102 # different file systems - and this test can't ensure that. 0103 # So we test the shutil.copy2 function, which is the thing most 0104 # likely to fail. 0105 shutil.copy2(filename1, filename2 + ".new") 0106 os.unlink(filename1 + ".new") 0107 0108 def _do_directory(self, make_name, chdir_name, encoded): 0109 cwd = os.getcwd() 0110 if os.path.isdir(make_name): 0111 os.rmdir(make_name) 0112 os.mkdir(make_name) 0113 try: 0114 os.chdir(chdir_name) 0115 try: 0116 if not encoded: 0117 cwd_result = os.getcwdu() 0118 name_result = make_name 0119 else: 0120 cwd_result = os.getcwd().decode(TESTFN_ENCODING) 0121 name_result = make_name.decode(TESTFN_ENCODING) 0122 0123 cwd_result = unicodedata.normalize("NFD", cwd_result) 0124 name_result = unicodedata.normalize("NFD", name_result) 0125 0126 self.failUnlessEqual(os.path.basename(cwd_result),name_result) 0127 finally: 0128 os.chdir(cwd) 0129 finally: 0130 os.rmdir(make_name) 0131 0132 # The '_test' functions 'entry points with params' - ie, what the 0133 # top-level 'test' functions would be if they could take params 0134 def _test_single(self, filename): 0135 remove_if_exists(filename) 0136 f = file(filename, "w") 0137 f.close() 0138 try: 0139 self._do_single(filename) 0140 finally: 0141 os.unlink(filename) 0142 self.failUnless(not os.path.exists(filename)) 0143 # and again with os.open. 0144 f = os.open(filename, os.O_CREAT) 0145 os.close(f) 0146 try: 0147 self._do_single(filename) 0148 finally: 0149 os.unlink(filename) 0150 0151 def _test_equivalent(self, filename1, filename2): 0152 remove_if_exists(filename1) 0153 self.failUnless(not os.path.exists(filename2)) 0154 f = file(filename1, "w") 0155 f.close() 0156 try: 0157 self._do_equivilent(filename1, filename2) 0158 finally: 0159 os.unlink(filename1) 0160 0161 # The 'test' functions are unittest entry points, and simply call our 0162 # _test functions with each of the filename combinations we wish to test 0163 def test_single_files(self): 0164 self._test_single(TESTFN_ENCODED) 0165 self._test_single(TESTFN_UNICODE) 0166 if TESTFN_UNICODE_UNENCODEABLE is not None: 0167 self._test_single(TESTFN_UNICODE_UNENCODEABLE) 0168 0169 def test_equivalent_files(self): 0170 self._test_equivalent(TESTFN_ENCODED, TESTFN_UNICODE) 0171 self._test_equivalent(TESTFN_UNICODE, TESTFN_ENCODED) 0172 0173 def test_directories(self): 0174 # For all 'equivilent' combinations: 0175 # Make dir with encoded, chdir with unicode, checkdir with encoded 0176 # (or unicode/encoded/unicode, etc 0177 ext = ".dir" 0178 self._do_directory(TESTFN_ENCODED+ext, TESTFN_ENCODED+ext, True) 0179 self._do_directory(TESTFN_ENCODED+ext, TESTFN_UNICODE+ext, True) 0180 self._do_directory(TESTFN_UNICODE+ext, TESTFN_ENCODED+ext, False) 0181 self._do_directory(TESTFN_UNICODE+ext, TESTFN_UNICODE+ext, False) 0182 # Our directory name that can't use a non-unicode name. 0183 if TESTFN_UNICODE_UNENCODEABLE is not None: 0184 self._do_directory(TESTFN_UNICODE_UNENCODEABLE+ext, 0185 TESTFN_UNICODE_UNENCODEABLE+ext, 0186 False) 0187 0188 def test_main(): 0189 suite = unittest.TestSuite() 0190 suite.addTest(unittest.makeSuite(TestUnicodeFiles)) 0191 run_suite(suite) 0192 0193 if __name__ == "__main__": 0194 test_main() 0195
Generated by PyXR 0.9.4