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

Python code coverage for Lib/test/test_unicode_file_functions.py

#countcontent
1n/a# Test the Unicode versions of normal file functions
2n/a# open, os.open, os.stat. os.listdir, os.rename, os.remove, os.mkdir, os.chdir, os.rmdir
3n/aimport os
4n/aimport sys
5n/aimport unittest
6n/aimport warnings
7n/afrom unicodedata import normalize
8n/afrom test import support
9n/a
10n/afilenames = [
11n/a '1_abc',
12n/a '2_ascii',
13n/a '3_Gr\xfc\xdf-Gott',
14n/a '4_\u0393\u03b5\u03b9\u03ac-\u03c3\u03b1\u03c2',
15n/a '5_\u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435',
16n/a '6_\u306b\u307d\u3093',
17n/a '7_\u05d4\u05e9\u05e7\u05e6\u05e5\u05e1',
18n/a '8_\u66e8\u66e9\u66eb',
19n/a '9_\u66e8\u05e9\u3093\u0434\u0393\xdf',
20n/a # Specific code points: fn, NFC(fn) and NFKC(fn) all differents
21n/a '10_\u1fee\u1ffd',
22n/a ]
23n/a
24n/a# Mac OS X decomposes Unicode names, using Normal Form D.
25n/a# http://developer.apple.com/mac/library/qa/qa2001/qa1173.html
26n/a# "However, most volume formats do not follow the exact specification for
27n/a# these normal forms. For example, HFS Plus uses a variant of Normal Form D
28n/a# in which U+2000 through U+2FFF, U+F900 through U+FAFF, and U+2F800 through
29n/a# U+2FAFF are not decomposed."
30n/aif sys.platform != 'darwin':
31n/a filenames.extend([
32n/a # Specific code points: NFC(fn), NFD(fn), NFKC(fn) and NFKD(fn) all differents
33n/a '11_\u0385\u03d3\u03d4',
34n/a '12_\u00a8\u0301\u03d2\u0301\u03d2\u0308', # == NFD('\u0385\u03d3\u03d4')
35n/a '13_\u0020\u0308\u0301\u038e\u03ab', # == NFKC('\u0385\u03d3\u03d4')
36n/a '14_\u1e9b\u1fc1\u1fcd\u1fce\u1fcf\u1fdd\u1fde\u1fdf\u1fed',
37n/a
38n/a # Specific code points: fn, NFC(fn) and NFKC(fn) all differents
39n/a '15_\u1fee\u1ffd\ufad1',
40n/a '16_\u2000\u2000\u2000A',
41n/a '17_\u2001\u2001\u2001A',
42n/a '18_\u2003\u2003\u2003A', # == NFC('\u2001\u2001\u2001A')
43n/a '19_\u0020\u0020\u0020A', # '\u0020' == ' ' == NFKC('\u2000') ==
44n/a # NFKC('\u2001') == NFKC('\u2003')
45n/a ])
46n/a
47n/a
48n/a# Is it Unicode-friendly?
49n/aif not os.path.supports_unicode_filenames:
50n/a fsencoding = sys.getfilesystemencoding()
51n/a try:
52n/a for name in filenames:
53n/a name.encode(fsencoding)
54n/a except UnicodeEncodeError:
55n/a raise unittest.SkipTest("only NT+ and systems with "
56n/a "Unicode-friendly filesystem encoding")
57n/a
58n/a
59n/aclass UnicodeFileTests(unittest.TestCase):
60n/a files = set(filenames)
61n/a normal_form = None
62n/a
63n/a def setUp(self):
64n/a try:
65n/a os.mkdir(support.TESTFN)
66n/a except FileExistsError:
67n/a pass
68n/a self.addCleanup(support.rmtree, support.TESTFN)
69n/a
70n/a files = set()
71n/a for name in self.files:
72n/a name = os.path.join(support.TESTFN, self.norm(name))
73n/a with open(name, 'wb') as f:
74n/a f.write((name+'\n').encode("utf-8"))
75n/a os.stat(name)
76n/a files.add(name)
77n/a self.files = files
78n/a
79n/a def norm(self, s):
80n/a if self.normal_form:
81n/a return normalize(self.normal_form, s)
82n/a return s
83n/a
84n/a def _apply_failure(self, fn, filename,
85n/a expected_exception=FileNotFoundError,
86n/a check_filename=True):
87n/a with self.assertRaises(expected_exception) as c:
88n/a fn(filename)
89n/a exc_filename = c.exception.filename
90n/a if check_filename:
91n/a self.assertEqual(exc_filename, filename, "Function '%s(%a) failed "
92n/a "with bad filename in the exception: %a" %
93n/a (fn.__name__, filename, exc_filename))
94n/a
95n/a def test_failures(self):
96n/a # Pass non-existing Unicode filenames all over the place.
97n/a for name in self.files:
98n/a name = "not_" + name
99n/a self._apply_failure(open, name)
100n/a self._apply_failure(os.stat, name)
101n/a self._apply_failure(os.chdir, name)
102n/a self._apply_failure(os.rmdir, name)
103n/a self._apply_failure(os.remove, name)
104n/a self._apply_failure(os.listdir, name)
105n/a
106n/a if sys.platform == 'win32':
107n/a # Windows is lunatic. Issue #13366.
108n/a _listdir_failure = NotADirectoryError, FileNotFoundError
109n/a else:
110n/a _listdir_failure = NotADirectoryError
111n/a
112n/a def test_open(self):
113n/a for name in self.files:
114n/a f = open(name, 'wb')
115n/a f.write((name+'\n').encode("utf-8"))
116n/a f.close()
117n/a os.stat(name)
118n/a self._apply_failure(os.listdir, name, self._listdir_failure)
119n/a
120n/a # Skip the test on darwin, because darwin does normalize the filename to
121n/a # NFD (a variant of Unicode NFD form). Normalize the filename to NFC, NFKC,
122n/a # NFKD in Python is useless, because darwin will normalize it later and so
123n/a # open(), os.stat(), etc. don't raise any exception.
124n/a @unittest.skipIf(sys.platform == 'darwin', 'irrelevant test on Mac OS X')
125n/a def test_normalize(self):
126n/a files = set(self.files)
127n/a others = set()
128n/a for nf in set(['NFC', 'NFD', 'NFKC', 'NFKD']):
129n/a others |= set(normalize(nf, file) for file in files)
130n/a others -= files
131n/a for name in others:
132n/a self._apply_failure(open, name)
133n/a self._apply_failure(os.stat, name)
134n/a self._apply_failure(os.chdir, name)
135n/a self._apply_failure(os.rmdir, name)
136n/a self._apply_failure(os.remove, name)
137n/a self._apply_failure(os.listdir, name)
138n/a
139n/a # Skip the test on darwin, because darwin uses a normalization different
140n/a # than Python NFD normalization: filenames are different even if we use
141n/a # Python NFD normalization.
142n/a @unittest.skipIf(sys.platform == 'darwin', 'irrelevant test on Mac OS X')
143n/a def test_listdir(self):
144n/a sf0 = set(self.files)
145n/a with warnings.catch_warnings():
146n/a warnings.simplefilter("ignore", DeprecationWarning)
147n/a f1 = os.listdir(support.TESTFN.encode(sys.getfilesystemencoding()))
148n/a f2 = os.listdir(support.TESTFN)
149n/a sf2 = set(os.path.join(support.TESTFN, f) for f in f2)
150n/a self.assertEqual(sf0, sf2, "%a != %a" % (sf0, sf2))
151n/a self.assertEqual(len(f1), len(f2))
152n/a
153n/a def test_rename(self):
154n/a for name in self.files:
155n/a os.rename(name, "tmp")
156n/a os.rename("tmp", name)
157n/a
158n/a def test_directory(self):
159n/a dirname = os.path.join(support.TESTFN, 'Gr\xfc\xdf-\u66e8\u66e9\u66eb')
160n/a filename = '\xdf-\u66e8\u66e9\u66eb'
161n/a with support.temp_cwd(dirname):
162n/a with open(filename, 'wb') as f:
163n/a f.write((filename + '\n').encode("utf-8"))
164n/a os.access(filename,os.R_OK)
165n/a os.remove(filename)
166n/a
167n/a
168n/aclass UnicodeNFCFileTests(UnicodeFileTests):
169n/a normal_form = 'NFC'
170n/a
171n/a
172n/aclass UnicodeNFDFileTests(UnicodeFileTests):
173n/a normal_form = 'NFD'
174n/a
175n/a
176n/aclass UnicodeNFKCFileTests(UnicodeFileTests):
177n/a normal_form = 'NFKC'
178n/a
179n/a
180n/aclass UnicodeNFKDFileTests(UnicodeFileTests):
181n/a normal_form = 'NFKD'
182n/a
183n/a
184n/adef test_main():
185n/a support.run_unittest(
186n/a UnicodeFileTests,
187n/a UnicodeNFCFileTests,
188n/a UnicodeNFDFileTests,
189n/a UnicodeNFKCFileTests,
190n/a UnicodeNFKDFileTests,
191n/a )
192n/a
193n/a
194n/aif __name__ == "__main__":
195n/a test_main()