• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 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.
4import os, glob, time, shutil
5import unicodedata
6
7import unittest
8from test.test_support import run_unittest, change_cwd, TESTFN_UNICODE
9from test.test_support import TESTFN_ENCODING, TESTFN_UNENCODABLE
10try:
11    TESTFN_ENCODED = TESTFN_UNICODE.encode(TESTFN_ENCODING)
12except (UnicodeError, TypeError):
13    # Either the file system encoding is None, or the file name
14    # cannot be encoded in the file system encoding.
15    raise unittest.SkipTest("No Unicode filesystem semantics on this platform.")
16
17if TESTFN_ENCODED.decode(TESTFN_ENCODING) != TESTFN_UNICODE:
18    # The file system encoding does not support Latin-1
19    # (which test_support assumes), so try the file system
20    # encoding instead.
21    import sys
22    try:
23        TESTFN_UNICODE = unicode("@test-\xe0\xf2", sys.getfilesystemencoding())
24        TESTFN_ENCODED = TESTFN_UNICODE.encode(TESTFN_ENCODING)
25        if '?' in TESTFN_ENCODED:
26            # MBCS will not report the error properly
27            raise UnicodeError, "mbcs encoding problem"
28    except (UnicodeError, TypeError):
29        raise unittest.SkipTest("Cannot find a suiteable filename.")
30
31if TESTFN_ENCODED.decode(TESTFN_ENCODING) != TESTFN_UNICODE:
32    raise unittest.SkipTest("Cannot find a suitable filename.")
33
34def remove_if_exists(filename):
35    if os.path.exists(filename):
36        os.unlink(filename)
37
38class TestUnicodeFiles(unittest.TestCase):
39    # The 'do_' functions are the actual tests.  They generally assume the
40    # file already exists etc.
41
42    # Do all the tests we can given only a single filename.  The file should
43    # exist.
44    def _do_single(self, filename):
45        self.assertTrue(os.path.exists(filename))
46        self.assertTrue(os.path.isfile(filename))
47        self.assertTrue(os.access(filename, os.R_OK))
48        self.assertTrue(os.path.exists(os.path.abspath(filename)))
49        self.assertTrue(os.path.isfile(os.path.abspath(filename)))
50        self.assertTrue(os.access(os.path.abspath(filename), os.R_OK))
51        os.chmod(filename, 0777)
52        os.utime(filename, None)
53        os.utime(filename, (time.time(), time.time()))
54        # Copy/rename etc tests using the same filename
55        self._do_copyish(filename, filename)
56        # Filename should appear in glob output
57        self.assertTrue(
58            os.path.abspath(filename)==os.path.abspath(glob.glob(filename)[0]))
59        # basename should appear in listdir.
60        path, base = os.path.split(os.path.abspath(filename))
61        if isinstance(base, str):
62            base = base.decode(TESTFN_ENCODING)
63        file_list = os.listdir(path)
64        # listdir() with a unicode arg may or may not return Unicode
65        # objects, depending on the platform.
66        if file_list and isinstance(file_list[0], str):
67            file_list = [f.decode(TESTFN_ENCODING) for f in file_list]
68
69        # Normalize the unicode strings, as round-tripping the name via the OS
70        # may return a different (but equivalent) value.
71        base = unicodedata.normalize("NFD", base)
72        file_list = [unicodedata.normalize("NFD", f) for f in file_list]
73
74        self.assertIn(base, file_list)
75
76    # Do as many "equivalancy' tests as we can - ie, check that although we
77    # have different types for the filename, they refer to the same file.
78    def _do_equivalent(self, filename1, filename2):
79        # Note we only check "filename1 against filename2" - we don't bother
80        # checking "filename2 against 1", as we assume we are called again with
81        # the args reversed.
82        self.assertTrue(type(filename1)!=type(filename2),
83                    "No point checking equivalent filenames of the same type")
84        # stat and lstat should return the same results.
85        self.assertEqual(os.stat(filename1),
86                             os.stat(filename2))
87        self.assertEqual(os.lstat(filename1),
88                             os.lstat(filename2))
89        # Copy/rename etc tests using equivalent filename
90        self._do_copyish(filename1, filename2)
91
92    # Tests that copy, move, etc one file to another.
93    def _do_copyish(self, filename1, filename2):
94        # Should be able to rename the file using either name.
95        self.assertTrue(os.path.isfile(filename1)) # must exist.
96        os.rename(filename1, filename2 + ".new")
97        self.assertTrue(os.path.isfile(filename1+".new"))
98        os.rename(filename1 + ".new", filename2)
99        self.assertTrue(os.path.isfile(filename2))
100
101        shutil.copy(filename1, filename2 + ".new")
102        os.unlink(filename1 + ".new") # remove using equiv name.
103        # And a couple of moves, one using each name.
104        shutil.move(filename1, filename2 + ".new")
105        self.assertTrue(not os.path.exists(filename2))
106        shutil.move(filename1 + ".new", filename2)
107        self.assertTrue(os.path.exists(filename1))
108        # Note - due to the implementation of shutil.move,
109        # it tries a rename first.  This only fails on Windows when on
110        # different file systems - and this test can't ensure that.
111        # So we test the shutil.copy2 function, which is the thing most
112        # likely to fail.
113        shutil.copy2(filename1, filename2 + ".new")
114        os.unlink(filename1 + ".new")
115
116    def _do_directory(self, make_name, chdir_name, encoded):
117        if os.path.isdir(make_name):
118            os.rmdir(make_name)
119        os.mkdir(make_name)
120        try:
121            with change_cwd(chdir_name):
122                if not encoded:
123                    cwd_result = os.getcwdu()
124                    name_result = make_name
125                else:
126                    cwd_result = os.getcwd().decode(TESTFN_ENCODING)
127                    name_result = make_name.decode(TESTFN_ENCODING)
128
129                cwd_result = unicodedata.normalize("NFD", cwd_result)
130                name_result = unicodedata.normalize("NFD", name_result)
131
132                self.assertEqual(os.path.basename(cwd_result),name_result)
133        finally:
134            os.rmdir(make_name)
135
136    # The '_test' functions 'entry points with params' - ie, what the
137    # top-level 'test' functions would be if they could take params
138    def _test_single(self, filename):
139        remove_if_exists(filename)
140        f = file(filename, "w")
141        f.close()
142        try:
143            self._do_single(filename)
144        finally:
145            os.unlink(filename)
146        self.assertTrue(not os.path.exists(filename))
147        # and again with os.open.
148        f = os.open(filename, os.O_CREAT)
149        os.close(f)
150        try:
151            self._do_single(filename)
152        finally:
153            os.unlink(filename)
154
155    def _test_equivalent(self, filename1, filename2):
156        remove_if_exists(filename1)
157        self.assertTrue(not os.path.exists(filename2))
158        f = file(filename1, "w")
159        f.close()
160        try:
161            self._do_equivalent(filename1, filename2)
162        finally:
163            os.unlink(filename1)
164
165    # The 'test' functions are unittest entry points, and simply call our
166    # _test functions with each of the filename combinations we wish to test
167    def test_single_files(self):
168        self._test_single(TESTFN_ENCODED)
169        self._test_single(TESTFN_UNICODE)
170        if TESTFN_UNENCODABLE is not None:
171            self._test_single(TESTFN_UNENCODABLE)
172
173    def test_equivalent_files(self):
174        self._test_equivalent(TESTFN_ENCODED, TESTFN_UNICODE)
175        self._test_equivalent(TESTFN_UNICODE, TESTFN_ENCODED)
176
177    def test_directories(self):
178        # For all 'equivalent' combinations:
179        #  Make dir with encoded, chdir with unicode, checkdir with encoded
180        #  (or unicode/encoded/unicode, etc
181        ext = ".dir"
182        self._do_directory(TESTFN_ENCODED+ext, TESTFN_ENCODED+ext, True)
183        self._do_directory(TESTFN_ENCODED+ext, TESTFN_UNICODE+ext, True)
184        self._do_directory(TESTFN_UNICODE+ext, TESTFN_ENCODED+ext, False)
185        self._do_directory(TESTFN_UNICODE+ext, TESTFN_UNICODE+ext, False)
186        # Our directory name that can't use a non-unicode name.
187        if TESTFN_UNENCODABLE is not None:
188            self._do_directory(TESTFN_UNENCODABLE+ext,
189                               TESTFN_UNENCODABLE+ext,
190                               False)
191
192def test_main():
193    run_unittest(__name__)
194
195if __name__ == "__main__":
196    test_main()
197