ยปCore Development>Code coverage>Lib/test/test_unicode_file.py

Python code coverage for Lib/test/test_unicode_file.py

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