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