1# Test some Unicode file name semantics 2# We don't test many operations on files other than 3# that their names can be used with Unicode characters. 4import os, glob, time, shutil 5import sys 6import unicodedata 7 8import unittest 9from test.support.os_helper import (rmtree, change_cwd, TESTFN_UNICODE, 10 TESTFN_UNENCODABLE, create_empty_file) 11 12 13if not os.path.supports_unicode_filenames: 14 try: 15 TESTFN_UNICODE.encode(sys.getfilesystemencoding()) 16 except (UnicodeError, TypeError): 17 # Either the file system encoding is None, or the file name 18 # cannot be encoded in the file system encoding. 19 raise unittest.SkipTest("No Unicode filesystem semantics on this platform.") 20 21def remove_if_exists(filename): 22 if os.path.exists(filename): 23 os.unlink(filename) 24 25class TestUnicodeFiles(unittest.TestCase): 26 # The 'do_' functions are the actual tests. They generally assume the 27 # file already exists etc. 28 29 # Do all the tests we can given only a single filename. The file should 30 # exist. 31 def _do_single(self, filename): 32 self.assertTrue(os.path.exists(filename)) 33 self.assertTrue(os.path.isfile(filename)) 34 self.assertTrue(os.access(filename, os.R_OK)) 35 self.assertTrue(os.path.exists(os.path.abspath(filename))) 36 self.assertTrue(os.path.isfile(os.path.abspath(filename))) 37 self.assertTrue(os.access(os.path.abspath(filename), os.R_OK)) 38 os.chmod(filename, 0o777) 39 os.utime(filename, None) 40 os.utime(filename, (time.time(), time.time())) 41 # Copy/rename etc tests using the same filename 42 self._do_copyish(filename, filename) 43 # Filename should appear in glob output 44 self.assertTrue( 45 os.path.abspath(filename)==os.path.abspath(glob.glob(glob.escape(filename))[0])) 46 # basename should appear in listdir. 47 path, base = os.path.split(os.path.abspath(filename)) 48 file_list = os.listdir(path) 49 # Normalize the unicode strings, as round-tripping the name via the OS 50 # may return a different (but equivalent) value. 51 base = unicodedata.normalize("NFD", base) 52 file_list = [unicodedata.normalize("NFD", f) for f in file_list] 53 54 self.assertIn(base, file_list) 55 56 # Tests that copy, move, etc one file to another. 57 def _do_copyish(self, filename1, filename2): 58 # Should be able to rename the file using either name. 59 self.assertTrue(os.path.isfile(filename1)) # must exist. 60 os.rename(filename1, filename2 + ".new") 61 self.assertFalse(os.path.isfile(filename2)) 62 self.assertTrue(os.path.isfile(filename1 + '.new')) 63 os.rename(filename1 + ".new", filename2) 64 self.assertFalse(os.path.isfile(filename1 + '.new')) 65 self.assertTrue(os.path.isfile(filename2)) 66 67 shutil.copy(filename1, filename2 + ".new") 68 os.unlink(filename1 + ".new") # remove using equiv name. 69 # And a couple of moves, one using each name. 70 shutil.move(filename1, filename2 + ".new") 71 self.assertFalse(os.path.exists(filename2)) 72 self.assertTrue(os.path.exists(filename1 + '.new')) 73 shutil.move(filename1 + ".new", filename2) 74 self.assertFalse(os.path.exists(filename2 + '.new')) 75 self.assertTrue(os.path.exists(filename1)) 76 # Note - due to the implementation of shutil.move, 77 # it tries a rename first. This only fails on Windows when on 78 # different file systems - and this test can't ensure that. 79 # So we test the shutil.copy2 function, which is the thing most 80 # likely to fail. 81 shutil.copy2(filename1, filename2 + ".new") 82 self.assertTrue(os.path.isfile(filename1 + '.new')) 83 os.unlink(filename1 + ".new") 84 self.assertFalse(os.path.exists(filename2 + '.new')) 85 86 def _do_directory(self, make_name, chdir_name): 87 if os.path.isdir(make_name): 88 rmtree(make_name) 89 os.mkdir(make_name) 90 try: 91 with change_cwd(chdir_name): 92 cwd_result = os.getcwd() 93 name_result = make_name 94 95 cwd_result = unicodedata.normalize("NFD", cwd_result) 96 name_result = unicodedata.normalize("NFD", name_result) 97 98 self.assertEqual(os.path.basename(cwd_result),name_result) 99 finally: 100 os.rmdir(make_name) 101 102 # The '_test' functions 'entry points with params' - ie, what the 103 # top-level 'test' functions would be if they could take params 104 def _test_single(self, filename): 105 remove_if_exists(filename) 106 create_empty_file(filename) 107 try: 108 self._do_single(filename) 109 finally: 110 os.unlink(filename) 111 self.assertTrue(not os.path.exists(filename)) 112 # and again with os.open. 113 f = os.open(filename, os.O_CREAT) 114 os.close(f) 115 try: 116 self._do_single(filename) 117 finally: 118 os.unlink(filename) 119 120 # The 'test' functions are unittest entry points, and simply call our 121 # _test functions with each of the filename combinations we wish to test 122 def test_single_files(self): 123 self._test_single(TESTFN_UNICODE) 124 if TESTFN_UNENCODABLE is not None: 125 self._test_single(TESTFN_UNENCODABLE) 126 127 def test_directories(self): 128 # For all 'equivalent' combinations: 129 # Make dir with encoded, chdir with unicode, checkdir with encoded 130 # (or unicode/encoded/unicode, etc 131 ext = ".dir" 132 self._do_directory(TESTFN_UNICODE+ext, TESTFN_UNICODE+ext) 133 # Our directory name that can't use a non-unicode name. 134 if TESTFN_UNENCODABLE is not None: 135 self._do_directory(TESTFN_UNENCODABLE+ext, 136 TESTFN_UNENCODABLE+ext) 137 138 139if __name__ == "__main__": 140 unittest.main() 141