2009-01-17 20:24:28 -04:00
|
|
|
"""Test case-sensitivity (PEP 235)."""
|
2009-02-01 00:00:05 -04:00
|
|
|
from .. import util
|
2013-06-15 19:39:21 -03:00
|
|
|
|
2013-11-08 15:25:37 -04:00
|
|
|
importlib = util.import_importlib('importlib')
|
|
|
|
machinery = util.import_importlib('importlib.machinery')
|
|
|
|
|
2009-01-17 20:24:28 -04:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
from test import support as test_support
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
|
2009-02-01 00:00:05 -04:00
|
|
|
@util.case_insensitive_tests
|
2013-11-08 15:25:37 -04:00
|
|
|
class CaseSensitivityTest:
|
2009-01-17 20:24:28 -04:00
|
|
|
|
|
|
|
"""PEP 235 dictates that on case-preserving, case-insensitive file systems
|
|
|
|
that imports are case-sensitive unless the PYTHONCASEOK environment
|
|
|
|
variable is set."""
|
|
|
|
|
|
|
|
name = 'MoDuLe'
|
|
|
|
assert name != name.lower()
|
|
|
|
|
2013-12-06 15:25:01 -04:00
|
|
|
def finder(self, path):
|
|
|
|
return self.machinery.FileFinder(path,
|
2013-11-08 15:25:37 -04:00
|
|
|
(self.machinery.SourceFileLoader,
|
|
|
|
self.machinery.SOURCE_SUFFIXES),
|
|
|
|
(self.machinery.SourcelessFileLoader,
|
|
|
|
self.machinery.BYTECODE_SUFFIXES))
|
2009-01-17 20:24:28 -04:00
|
|
|
|
|
|
|
def sensitivity_test(self):
|
|
|
|
"""Look for a module with matching and non-matching sensitivity."""
|
|
|
|
sensitive_pkg = 'sensitive.{0}'.format(self.name)
|
|
|
|
insensitive_pkg = 'insensitive.{0}'.format(self.name.lower())
|
2014-05-09 15:32:57 -03:00
|
|
|
context = util.create_modules(insensitive_pkg, sensitive_pkg)
|
2010-07-03 18:48:25 -03:00
|
|
|
with context as mapping:
|
2009-01-17 20:24:28 -04:00
|
|
|
sensitive_path = os.path.join(mapping['.root'], 'sensitive')
|
|
|
|
insensitive_path = os.path.join(mapping['.root'], 'insensitive')
|
2013-12-06 15:25:01 -04:00
|
|
|
sensitive_finder = self.finder(sensitive_path)
|
|
|
|
insensitive_finder = self.finder(insensitive_path)
|
|
|
|
return self.find(sensitive_finder), self.find(insensitive_finder)
|
2009-01-17 20:24:28 -04:00
|
|
|
|
|
|
|
def test_sensitive(self):
|
|
|
|
with test_support.EnvironmentVarGuard() as env:
|
|
|
|
env.unset('PYTHONCASEOK')
|
2013-11-08 15:25:37 -04:00
|
|
|
if b'PYTHONCASEOK' in self.importlib._bootstrap._os.environ:
|
2012-01-30 13:48:16 -04:00
|
|
|
self.skipTest('os.environ changes not reflected in '
|
|
|
|
'_os.environ')
|
2009-01-17 20:24:28 -04:00
|
|
|
sensitive, insensitive = self.sensitivity_test()
|
2013-12-06 15:25:01 -04:00
|
|
|
self.assertIsNotNone(sensitive)
|
2010-07-03 18:48:25 -03:00
|
|
|
self.assertIn(self.name, sensitive.get_filename(self.name))
|
2009-05-10 22:47:11 -03:00
|
|
|
self.assertIsNone(insensitive)
|
2009-01-17 20:24:28 -04:00
|
|
|
|
|
|
|
def test_insensitive(self):
|
2013-09-03 21:43:49 -03:00
|
|
|
with test_support.EnvironmentVarGuard() as env:
|
|
|
|
env.set('PYTHONCASEOK', '1')
|
2013-11-08 15:25:37 -04:00
|
|
|
if b'PYTHONCASEOK' not in self.importlib._bootstrap._os.environ:
|
2013-09-03 21:43:49 -03:00
|
|
|
self.skipTest('os.environ changes not reflected in '
|
|
|
|
'_os.environ')
|
|
|
|
sensitive, insensitive = self.sensitivity_test()
|
2013-12-06 15:25:01 -04:00
|
|
|
self.assertIsNotNone(sensitive)
|
2013-09-03 21:43:49 -03:00
|
|
|
self.assertIn(self.name, sensitive.get_filename(self.name))
|
2013-12-06 15:25:01 -04:00
|
|
|
self.assertIsNotNone(insensitive)
|
2013-09-03 21:43:49 -03:00
|
|
|
self.assertIn(self.name, insensitive.get_filename(self.name))
|
2009-01-17 20:24:28 -04:00
|
|
|
|
2014-05-16 14:40:40 -03:00
|
|
|
|
2013-12-06 15:25:01 -04:00
|
|
|
class CaseSensitivityTestPEP302(CaseSensitivityTest):
|
|
|
|
def find(self, finder):
|
|
|
|
return finder.find_module(self.name)
|
|
|
|
|
2014-05-16 14:40:40 -03:00
|
|
|
|
|
|
|
(Frozen_CaseSensitivityTestPEP302,
|
|
|
|
Source_CaseSensitivityTestPEP302
|
|
|
|
) = util.test_both(CaseSensitivityTestPEP302, importlib=importlib,
|
|
|
|
machinery=machinery)
|
|
|
|
|
2013-12-06 15:25:01 -04:00
|
|
|
|
|
|
|
class CaseSensitivityTestPEP451(CaseSensitivityTest):
|
|
|
|
def find(self, finder):
|
|
|
|
found = finder.find_spec(self.name)
|
|
|
|
return found.loader if found is not None else found
|
|
|
|
|
2014-05-16 14:40:40 -03:00
|
|
|
|
|
|
|
(Frozen_CaseSensitivityTestPEP451,
|
|
|
|
Source_CaseSensitivityTestPEP451
|
|
|
|
) = util.test_both(CaseSensitivityTestPEP451, importlib=importlib,
|
|
|
|
machinery=machinery)
|
2009-01-17 20:24:28 -04:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2013-11-08 15:25:37 -04:00
|
|
|
unittest.main()
|