test_unicode_file.py 6.87 KB
Newer Older
1 2 3
# Test some Unicode file name semantics
# We dont test many operations on files other than
# that their names can be used with Unicode characters.
4
import os, glob, time, shutil
5
import unicodedata
6

7
import unittest
Benjamin Peterson's avatar
Benjamin Peterson committed
8
from test.support import run_unittest, TESTFN_UNICODE, rmtree
9
from test.support import TESTFN_ENCODING, TESTFN_UNICODE_UNENCODEABLE
10
try:
11
    TESTFN_UNICODE.encode(TESTFN_ENCODING)
12
except (UnicodeError, TypeError):
13 14
    # Either the file system encoding is None, or the file name
    # cannot be encoded in the file system encoding.
Benjamin Peterson's avatar
Benjamin Peterson committed
15
    raise unittest.SkipTest("No Unicode filesystem semantics on this platform.")
16

17 18 19
def remove_if_exists(filename):
    if os.path.exists(filename):
        os.unlink(filename)
20

21 22 23
class TestUnicodeFiles(unittest.TestCase):
    # The 'do_' functions are the actual tests.  They generally assume the
    # file already exists etc.
Tim Peters's avatar
Tim Peters committed
24

25 26 27
    # Do all the tests we can given only a single filename.  The file should
    # exist.
    def _do_single(self, filename):
28 29 30 31 32 33
        self.assertTrue(os.path.exists(filename))
        self.assertTrue(os.path.isfile(filename))
        self.assertTrue(os.access(filename, os.R_OK))
        self.assertTrue(os.path.exists(os.path.abspath(filename)))
        self.assertTrue(os.path.isfile(os.path.abspath(filename)))
        self.assertTrue(os.access(os.path.abspath(filename), os.R_OK))
34
        os.chmod(filename, 0o777)
35 36 37 38 39
        os.utime(filename, None)
        os.utime(filename, (time.time(), time.time()))
        # Copy/rename etc tests using the same filename
        self._do_copyish(filename, filename)
        # Filename should appear in glob output
40
        self.assertTrue(
41 42 43
            os.path.abspath(filename)==os.path.abspath(glob.glob(filename)[0]))
        # basename should appear in listdir.
        path, base = os.path.split(os.path.abspath(filename))
44 45 46 47
        file_list = os.listdir(path)
        # Normalize the unicode strings, as round-tripping the name via the OS
        # may return a different (but equivalent) value.
        base = unicodedata.normalize("NFD", base)
48 49
        file_list = [unicodedata.normalize("NFD", f) for f in file_list]

50
        self.assertIn(base, file_list)
Tim Peters's avatar
Tim Peters committed
51

52 53 54 55 56 57
    # Do as many "equivalancy' tests as we can - ie, check that although we
    # have different types for the filename, they refer to the same file.
    def _do_equivalent(self, filename1, filename2):
        # Note we only check "filename1 against filename2" - we don't bother
        # checking "filename2 against 1", as we assume we are called again with
        # the args reversed.
58
        self.assertTrue(type(filename1)!=type(filename2),
59 60
                    "No point checking equivalent filenames of the same type")
        # stat and lstat should return the same results.
61
        self.assertEqual(os.stat(filename1),
62
                             os.stat(filename2))
63
        self.assertEqual(os.lstat(filename1),
64 65 66 67
                             os.lstat(filename2))
        # Copy/rename etc tests using equivalent filename
        self._do_copyish(filename1, filename2)

68 69 70
    # Tests that copy, move, etc one file to another.
    def _do_copyish(self, filename1, filename2):
        # Should be able to rename the file using either name.
71
        self.assertTrue(os.path.isfile(filename1)) # must exist.
72
        os.rename(filename1, filename2 + ".new")
73
        self.assertTrue(os.path.isfile(filename1+".new"))
74
        os.rename(filename1 + ".new", filename2)
75
        self.assertTrue(os.path.isfile(filename2))
76

77 78 79 80
        shutil.copy(filename1, filename2 + ".new")
        os.unlink(filename1 + ".new") # remove using equiv name.
        # And a couple of moves, one using each name.
        shutil.move(filename1, filename2 + ".new")
81
        self.assertTrue(not os.path.exists(filename2))
82
        shutil.move(filename1 + ".new", filename2)
83
        self.assertTrue(os.path.exists(filename1))
84 85 86 87 88 89 90
        # Note - due to the implementation of shutil.move,
        # it tries a rename first.  This only fails on Windows when on
        # different file systems - and this test can't ensure that.
        # So we test the shutil.copy2 function, which is the thing most
        # likely to fail.
        shutil.copy2(filename1, filename2 + ".new")
        os.unlink(filename1 + ".new")
91

92
    def _do_directory(self, make_name, chdir_name, encoded):
93
        cwd = os.getcwdb()
94
        if os.path.isdir(make_name):
95
            rmtree(make_name)
96 97 98 99
        os.mkdir(make_name)
        try:
            os.chdir(chdir_name)
            try:
100
                if not encoded:
101
                    cwd_result = os.getcwd()
102 103
                    name_result = make_name
                else:
104
                    cwd_result = os.getcwdb().decode(TESTFN_ENCODING)
105 106 107 108 109
                    name_result = make_name.decode(TESTFN_ENCODING)

                cwd_result = unicodedata.normalize("NFD", cwd_result)
                name_result = unicodedata.normalize("NFD", name_result)

110
                self.assertEqual(os.path.basename(cwd_result),name_result)
111 112 113 114
            finally:
                os.chdir(cwd)
        finally:
            os.rmdir(make_name)
115

116 117 118 119
    # The '_test' functions 'entry points with params' - ie, what the
    # top-level 'test' functions would be if they could take params
    def _test_single(self, filename):
        remove_if_exists(filename)
120
        f = open(filename, "w")
121 122 123 124 125
        f.close()
        try:
            self._do_single(filename)
        finally:
            os.unlink(filename)
126
        self.assertTrue(not os.path.exists(filename))
127 128 129 130 131 132 133
        # and again with os.open.
        f = os.open(filename, os.O_CREAT)
        os.close(f)
        try:
            self._do_single(filename)
        finally:
            os.unlink(filename)
Tim Peters's avatar
Tim Peters committed
134

135 136
    def _test_equivalent(self, filename1, filename2):
        remove_if_exists(filename1)
137
        self.assertTrue(not os.path.exists(filename2))
138 139 140 141 142 143 144
        f = file(filename1, "w")
        f.close()
        try:
            self._do_equivalent(filename1, filename2)
        finally:
            os.unlink(filename1)

145 146 147 148
    # The 'test' functions are unittest entry points, and simply call our
    # _test functions with each of the filename combinations we wish to test
    def test_single_files(self):
        self._test_single(TESTFN_UNICODE)
149 150
        if TESTFN_UNICODE_UNENCODEABLE is not None:
            self._test_single(TESTFN_UNICODE_UNENCODEABLE)
151

152
    def test_directories(self):
153 154 155
        # For all 'equivalent' combinations:
        #  Make dir with encoded, chdir with unicode, checkdir with encoded
        #  (or unicode/encoded/unicode, etc
156
        ext = ".dir"
157
        self._do_directory(TESTFN_UNICODE+ext, TESTFN_UNICODE+ext, False)
158
        # Our directory name that can't use a non-unicode name.
159 160 161
        if TESTFN_UNICODE_UNENCODEABLE is not None:
            self._do_directory(TESTFN_UNICODE_UNENCODEABLE+ext,
                               TESTFN_UNICODE_UNENCODEABLE+ext,
162
                               False)
163

164
def test_main():
165
    run_unittest(__name__)
166

167 168
if __name__ == "__main__":
    test_main()