ยปCore Development>Code coverage>Lib/test/test_importlib/source/test_case_sensitivity.py

Python code coverage for Lib/test/test_importlib/source/test_case_sensitivity.py

#countcontent
1n/a"""Test case-sensitivity (PEP 235)."""
2n/afrom .. import util
3n/a
4n/aimportlib = util.import_importlib('importlib')
5n/amachinery = util.import_importlib('importlib.machinery')
6n/a
7n/aimport os
8n/afrom test import support as test_support
9n/aimport unittest
10n/a
11n/a
12n/a@util.case_insensitive_tests
13n/aclass CaseSensitivityTest(util.CASEOKTestBase):
14n/a
15n/a """PEP 235 dictates that on case-preserving, case-insensitive file systems
16n/a that imports are case-sensitive unless the PYTHONCASEOK environment
17n/a variable is set."""
18n/a
19n/a name = 'MoDuLe'
20n/a assert name != name.lower()
21n/a
22n/a def finder(self, path):
23n/a return self.machinery.FileFinder(path,
24n/a (self.machinery.SourceFileLoader,
25n/a self.machinery.SOURCE_SUFFIXES),
26n/a (self.machinery.SourcelessFileLoader,
27n/a self.machinery.BYTECODE_SUFFIXES))
28n/a
29n/a def sensitivity_test(self):
30n/a """Look for a module with matching and non-matching sensitivity."""
31n/a sensitive_pkg = 'sensitive.{0}'.format(self.name)
32n/a insensitive_pkg = 'insensitive.{0}'.format(self.name.lower())
33n/a context = util.create_modules(insensitive_pkg, sensitive_pkg)
34n/a with context as mapping:
35n/a sensitive_path = os.path.join(mapping['.root'], 'sensitive')
36n/a insensitive_path = os.path.join(mapping['.root'], 'insensitive')
37n/a sensitive_finder = self.finder(sensitive_path)
38n/a insensitive_finder = self.finder(insensitive_path)
39n/a return self.find(sensitive_finder), self.find(insensitive_finder)
40n/a
41n/a def test_sensitive(self):
42n/a with test_support.EnvironmentVarGuard() as env:
43n/a env.unset('PYTHONCASEOK')
44n/a self.caseok_env_changed(should_exist=False)
45n/a sensitive, insensitive = self.sensitivity_test()
46n/a self.assertIsNotNone(sensitive)
47n/a self.assertIn(self.name, sensitive.get_filename(self.name))
48n/a self.assertIsNone(insensitive)
49n/a
50n/a def test_insensitive(self):
51n/a with test_support.EnvironmentVarGuard() as env:
52n/a env.set('PYTHONCASEOK', '1')
53n/a self.caseok_env_changed(should_exist=True)
54n/a sensitive, insensitive = self.sensitivity_test()
55n/a self.assertIsNotNone(sensitive)
56n/a self.assertIn(self.name, sensitive.get_filename(self.name))
57n/a self.assertIsNotNone(insensitive)
58n/a self.assertIn(self.name, insensitive.get_filename(self.name))
59n/a
60n/a
61n/aclass CaseSensitivityTestPEP302(CaseSensitivityTest):
62n/a def find(self, finder):
63n/a return finder.find_module(self.name)
64n/a
65n/a
66n/a(Frozen_CaseSensitivityTestPEP302,
67n/a Source_CaseSensitivityTestPEP302
68n/a ) = util.test_both(CaseSensitivityTestPEP302, importlib=importlib,
69n/a machinery=machinery)
70n/a
71n/a
72n/aclass CaseSensitivityTestPEP451(CaseSensitivityTest):
73n/a def find(self, finder):
74n/a found = finder.find_spec(self.name)
75n/a return found.loader if found is not None else found
76n/a
77n/a
78n/a(Frozen_CaseSensitivityTestPEP451,
79n/a Source_CaseSensitivityTestPEP451
80n/a ) = util.test_both(CaseSensitivityTestPEP451, importlib=importlib,
81n/a machinery=machinery)
82n/a
83n/a
84n/aif __name__ == '__main__':
85n/a unittest.main()