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