blob: cc670db92210a7a598ffc6880d64c2e54fdf0a47 [file] [log] [blame]
Mark Hammondef8b6542001-05-13 08:04:26 +00001# Test some Unicode file name semantics
2# We dont test many operations on files other than
3# that their names can be used with Unicode characters.
Mark Hammond6d459722003-12-03 01:29:56 +00004import os, glob, time, shutil
Nicholas Bastin66803412004-03-21 20:55:47 +00005import unicodedata
Mark Hammondef8b6542001-05-13 08:04:26 +00006
Mark Hammond6d459722003-12-03 01:29:56 +00007import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00008from test.support import run_unittest, TestSkipped, TESTFN_UNICODE
9from test.support import TESTFN_ENCODING, TESTFN_UNICODE_UNENCODEABLE
Mark Hammondef8b6542001-05-13 08:04:26 +000010try:
Brett Cannon96d7e832007-07-30 01:34:07 +000011 TESTFN_UNICODE.encode(TESTFN_ENCODING)
Martin v. Löwisc2ca32d2003-03-17 18:30:15 +000012except (UnicodeError, TypeError):
Martin v. Löwisc49435c2003-03-08 10:25:31 +000013 # Either the file system encoding is None, or the file name
14 # cannot be encoded in the file system encoding.
Guido van Rossum19b4a272007-08-26 23:30:31 +000015 raise TestSkipped("No Unicode filesystem semantics on this platform")
Mark Hammondef8b6542001-05-13 08:04:26 +000016
Mark Hammond6d459722003-12-03 01:29:56 +000017def remove_if_exists(filename):
18 if os.path.exists(filename):
19 os.unlink(filename)
Mark Hammondef8b6542001-05-13 08:04:26 +000020
Mark Hammond6d459722003-12-03 01:29:56 +000021class TestUnicodeFiles(unittest.TestCase):
22 # The 'do_' functions are the actual tests. They generally assume the
23 # file already exists etc.
Tim Peters58eb11c2004-01-18 20:29:55 +000024
Mark Hammond6d459722003-12-03 01:29:56 +000025 # Do all the tests we can given only a single filename. The file should
26 # exist.
27 def _do_single(self, filename):
28 self.failUnless(os.path.exists(filename))
29 self.failUnless(os.path.isfile(filename))
Martin v. Löwisb60ae992005-03-08 09:10:29 +000030 self.failUnless(os.access(filename, os.R_OK))
Mark Hammond6d459722003-12-03 01:29:56 +000031 self.failUnless(os.path.exists(os.path.abspath(filename)))
32 self.failUnless(os.path.isfile(os.path.abspath(filename)))
Martin v. Löwisb60ae992005-03-08 09:10:29 +000033 self.failUnless(os.access(os.path.abspath(filename), os.R_OK))
Guido van Rossumcd16bf62007-06-13 18:07:49 +000034 os.chmod(filename, 0o777)
Mark Hammond6d459722003-12-03 01:29:56 +000035 os.utime(filename, None)
36 os.utime(filename, (time.time(), time.time()))
37 # Copy/rename etc tests using the same filename
38 self._do_copyish(filename, filename)
39 # Filename should appear in glob output
40 self.failUnless(
41 os.path.abspath(filename)==os.path.abspath(glob.glob(filename)[0]))
42 # basename should appear in listdir.
43 path, base = os.path.split(os.path.abspath(filename))
Raymond Hettinger3b04ce82004-06-28 06:57:19 +000044 file_list = os.listdir(path)
Raymond Hettinger3b04ce82004-06-28 06:57:19 +000045 # Normalize the unicode strings, as round-tripping the name via the OS
46 # may return a different (but equivalent) value.
47 base = unicodedata.normalize("NFD", base)
Nicholas Bastin66803412004-03-21 20:55:47 +000048 file_list = [unicodedata.normalize("NFD", f) for f in file_list]
49
Raymond Hettinger3b04ce82004-06-28 06:57:19 +000050 self.failUnless(base in file_list)
Tim Peters58eb11c2004-01-18 20:29:55 +000051
Christian Heimese25f35e2008-03-20 10:49:03 +000052 # Do as many "equivalancy' tests as we can - ie, check that although we
53 # have different types for the filename, they refer to the same file.
54 def _do_equivalent(self, filename1, filename2):
55 # Note we only check "filename1 against filename2" - we don't bother
56 # checking "filename2 against 1", as we assume we are called again with
57 # the args reversed.
58 self.failUnless(type(filename1)!=type(filename2),
59 "No point checking equivalent filenames of the same type")
60 # stat and lstat should return the same results.
61 self.failUnlessEqual(os.stat(filename1),
62 os.stat(filename2))
63 self.failUnlessEqual(os.lstat(filename1),
64 os.lstat(filename2))
65 # Copy/rename etc tests using equivalent filename
66 self._do_copyish(filename1, filename2)
67
Mark Hammond6d459722003-12-03 01:29:56 +000068 # Tests that copy, move, etc one file to another.
69 def _do_copyish(self, filename1, filename2):
70 # Should be able to rename the file using either name.
71 self.failUnless(os.path.isfile(filename1)) # must exist.
72 os.rename(filename1, filename2 + ".new")
73 self.failUnless(os.path.isfile(filename1+".new"))
74 os.rename(filename1 + ".new", filename2)
75 self.failUnless(os.path.isfile(filename2))
Mark Hammondef8b6542001-05-13 08:04:26 +000076
Christian Heimese25f35e2008-03-20 10:49:03 +000077 shutil.copy(filename1, filename2 + ".new")
78 os.unlink(filename1 + ".new") # remove using equiv name.
79 # And a couple of moves, one using each name.
80 shutil.move(filename1, filename2 + ".new")
81 self.failUnless(not os.path.exists(filename2))
82 shutil.move(filename1 + ".new", filename2)
83 self.failUnless(os.path.exists(filename1))
84 # Note - due to the implementation of shutil.move,
85 # it tries a rename first. This only fails on Windows when on
86 # different file systems - and this test can't ensure that.
87 # So we test the shutil.copy2 function, which is the thing most
88 # likely to fail.
89 shutil.copy2(filename1, filename2 + ".new")
90 os.unlink(filename1 + ".new")
Mark Hammondef8b6542001-05-13 08:04:26 +000091
Nicholas Bastin66803412004-03-21 20:55:47 +000092 def _do_directory(self, make_name, chdir_name, encoded):
Mark Hammond6d459722003-12-03 01:29:56 +000093 cwd = os.getcwd()
94 if os.path.isdir(make_name):
95 os.rmdir(make_name)
96 os.mkdir(make_name)
97 try:
98 os.chdir(chdir_name)
99 try:
Nicholas Bastin66803412004-03-21 20:55:47 +0000100 if not encoded:
101 cwd_result = os.getcwdu()
102 name_result = make_name
103 else:
104 cwd_result = os.getcwd().decode(TESTFN_ENCODING)
105 name_result = make_name.decode(TESTFN_ENCODING)
106
107 cwd_result = unicodedata.normalize("NFD", cwd_result)
108 name_result = unicodedata.normalize("NFD", name_result)
109
110 self.failUnlessEqual(os.path.basename(cwd_result),name_result)
Mark Hammond6d459722003-12-03 01:29:56 +0000111 finally:
112 os.chdir(cwd)
113 finally:
114 os.rmdir(make_name)
Mark Hammondef8b6542001-05-13 08:04:26 +0000115
Mark Hammond6d459722003-12-03 01:29:56 +0000116 # The '_test' functions 'entry points with params' - ie, what the
117 # top-level 'test' functions would be if they could take params
118 def _test_single(self, filename):
119 remove_if_exists(filename)
Alex Martelli01c77c62006-08-24 02:58:11 +0000120 f = open(filename, "w")
Mark Hammond6d459722003-12-03 01:29:56 +0000121 f.close()
122 try:
123 self._do_single(filename)
124 finally:
125 os.unlink(filename)
126 self.failUnless(not os.path.exists(filename))
127 # and again with os.open.
128 f = os.open(filename, os.O_CREAT)
129 os.close(f)
130 try:
131 self._do_single(filename)
132 finally:
133 os.unlink(filename)
Tim Peters58eb11c2004-01-18 20:29:55 +0000134
Christian Heimese25f35e2008-03-20 10:49:03 +0000135 def _test_equivalent(self, filename1, filename2):
136 remove_if_exists(filename1)
137 self.failUnless(not os.path.exists(filename2))
138 f = file(filename1, "w")
139 f.close()
140 try:
141 self._do_equivalent(filename1, filename2)
142 finally:
143 os.unlink(filename1)
144
Mark Hammond6d459722003-12-03 01:29:56 +0000145 # The 'test' functions are unittest entry points, and simply call our
146 # _test functions with each of the filename combinations we wish to test
147 def test_single_files(self):
Mark Hammond6d459722003-12-03 01:29:56 +0000148 self._test_single(TESTFN_UNICODE)
Mark Hammond2e8624c2003-12-03 22:16:47 +0000149 if TESTFN_UNICODE_UNENCODEABLE is not None:
150 self._test_single(TESTFN_UNICODE_UNENCODEABLE)
Mark Hammondef8b6542001-05-13 08:04:26 +0000151
Mark Hammond6d459722003-12-03 01:29:56 +0000152 def test_directories(self):
Christian Heimese25f35e2008-03-20 10:49:03 +0000153 # For all 'equivalent' combinations:
154 # Make dir with encoded, chdir with unicode, checkdir with encoded
155 # (or unicode/encoded/unicode, etc
Martin v. Löwisa79f1252007-08-30 10:08:57 +0000156 ext = ".dir"
Nicholas Bastin66803412004-03-21 20:55:47 +0000157 self._do_directory(TESTFN_UNICODE+ext, TESTFN_UNICODE+ext, False)
Mark Hammond6d459722003-12-03 01:29:56 +0000158 # Our directory name that can't use a non-unicode name.
Mark Hammond2e8624c2003-12-03 22:16:47 +0000159 if TESTFN_UNICODE_UNENCODEABLE is not None:
160 self._do_directory(TESTFN_UNICODE_UNENCODEABLE+ext,
161 TESTFN_UNICODE_UNENCODEABLE+ext,
Nicholas Bastin66803412004-03-21 20:55:47 +0000162 False)
Mark Hammondef8b6542001-05-13 08:04:26 +0000163
Mark Hammond6d459722003-12-03 01:29:56 +0000164def test_main():
Guido van Rossumd8faa362007-04-27 19:54:29 +0000165 run_unittest(__name__)
Mark Hammondef8b6542001-05-13 08:04:26 +0000166
Mark Hammond6d459722003-12-03 01:29:56 +0000167if __name__ == "__main__":
168 test_main()