diff options
author | Brett Cannon <brett@python.org> | 2012-07-20 18:48:53 (GMT) |
---|---|---|
committer | Brett Cannon <brett@python.org> | 2012-07-20 18:48:53 (GMT) |
commit | 45a5e3afe52ed89f298242143c5f7e2bb992ac63 (patch) | |
tree | 2255a23f991f1452335d865939f6c30d7b2d7d48 /Lib/test/test_importlib/extension | |
parent | 4afc1c08d04a9c083b654178ea4f947563510836 (diff) | |
download | cpython-45a5e3afe52ed89f298242143c5f7e2bb992ac63.zip cpython-45a5e3afe52ed89f298242143c5f7e2bb992ac63.tar.gz cpython-45a5e3afe52ed89f298242143c5f7e2bb992ac63.tar.bz2 |
Issue #15168: Move importlb.test to test.test_importlib.
This should make the Linux distros happy as it is now easier to leave
importlib's tests out of their base Python distribution.
Diffstat (limited to 'Lib/test/test_importlib/extension')
-rw-r--r-- | Lib/test/test_importlib/extension/__init__.py | 13 | ||||
-rw-r--r-- | Lib/test/test_importlib/extension/test_case_sensitivity.py | 51 | ||||
-rw-r--r-- | Lib/test/test_importlib/extension/test_finder.py | 50 | ||||
-rw-r--r-- | Lib/test/test_importlib/extension/test_loader.py | 71 | ||||
-rw-r--r-- | Lib/test/test_importlib/extension/test_path_hook.py | 32 | ||||
-rw-r--r-- | Lib/test/test_importlib/extension/util.py | 20 |
6 files changed, 237 insertions, 0 deletions
diff --git a/Lib/test/test_importlib/extension/__init__.py b/Lib/test/test_importlib/extension/__init__.py new file mode 100644 index 0000000..c033923 --- /dev/null +++ b/Lib/test/test_importlib/extension/__init__.py @@ -0,0 +1,13 @@ +from .. import test_suite +import os.path +import unittest + + +def test_suite(): + directory = os.path.dirname(__file__) + return test_suite('importlib.test.extension', directory) + + +if __name__ == '__main__': + from test.support import run_unittest + run_unittest(test_suite()) diff --git a/Lib/test/test_importlib/extension/test_case_sensitivity.py b/Lib/test/test_importlib/extension/test_case_sensitivity.py new file mode 100644 index 0000000..bdc21e7 --- /dev/null +++ b/Lib/test/test_importlib/extension/test_case_sensitivity.py @@ -0,0 +1,51 @@ +import imp +import sys +from test import support +import unittest +from importlib import _bootstrap +from .. import util +from . import util as ext_util + + +@util.case_insensitive_tests +class ExtensionModuleCaseSensitivityTest(unittest.TestCase): + + def find_module(self): + good_name = ext_util.NAME + bad_name = good_name.upper() + assert good_name != bad_name + finder = _bootstrap.FileFinder(ext_util.PATH, + (_bootstrap.ExtensionFileLoader, + imp.extension_suffixes(), + False)) + return finder.find_module(bad_name) + + def test_case_sensitive(self): + with support.EnvironmentVarGuard() as env: + env.unset('PYTHONCASEOK') + if b'PYTHONCASEOK' in _bootstrap._os.environ: + self.skipTest('os.environ changes not reflected in ' + '_os.environ') + loader = self.find_module() + self.assertIsNone(loader) + + def test_case_insensitivity(self): + with support.EnvironmentVarGuard() as env: + env.set('PYTHONCASEOK', '1') + if b'PYTHONCASEOK' not in _bootstrap._os.environ: + self.skipTest('os.environ changes not reflected in ' + '_os.environ') + loader = self.find_module() + self.assertTrue(hasattr(loader, 'load_module')) + + + + +def test_main(): + if ext_util.FILENAME is None: + return + support.run_unittest(ExtensionModuleCaseSensitivityTest) + + +if __name__ == '__main__': + test_main() diff --git a/Lib/test/test_importlib/extension/test_finder.py b/Lib/test/test_importlib/extension/test_finder.py new file mode 100644 index 0000000..1c60292 --- /dev/null +++ b/Lib/test/test_importlib/extension/test_finder.py @@ -0,0 +1,50 @@ +from importlib import _bootstrap +from .. import abc +from . import util + +import imp +import unittest + +class FinderTests(abc.FinderTests): + + """Test the finder for extension modules.""" + + def find_module(self, fullname): + importer = _bootstrap.FileFinder(util.PATH, + (_bootstrap.ExtensionFileLoader, + imp.extension_suffixes(), + False)) + return importer.find_module(fullname) + + def test_module(self): + self.assertTrue(self.find_module(util.NAME)) + + def test_package(self): + # Extension modules cannot be an __init__ for a package. + pass + + def test_module_in_package(self): + # No extension module in a package available for testing. + pass + + def test_package_in_package(self): + # Extension modules cannot be an __init__ for a package. + pass + + def test_package_over_module(self): + # Extension modules cannot be an __init__ for a package. + pass + + def test_failure(self): + self.assertIsNone(self.find_module('asdfjkl;')) + + # XXX Raise an exception if someone tries to use the 'path' argument? + + +def test_main(): + from test.support import run_unittest + run_unittest(FinderTests) + + +if __name__ == '__main__': + test_main() diff --git a/Lib/test/test_importlib/extension/test_loader.py b/Lib/test/test_importlib/extension/test_loader.py new file mode 100644 index 0000000..917843f --- /dev/null +++ b/Lib/test/test_importlib/extension/test_loader.py @@ -0,0 +1,71 @@ +from importlib import machinery +from . import util as ext_util +from .. import abc +from .. import util + +import sys +import unittest + + +class LoaderTests(abc.LoaderTests): + + """Test load_module() for extension modules.""" + + def setUp(self): + self.loader = machinery.ExtensionFileLoader(ext_util.NAME, + ext_util.FILEPATH) + + def load_module(self, fullname): + return self.loader.load_module(fullname) + + def test_load_module_API(self): + # Test the default argument for load_module(). + self.loader.load_module() + self.loader.load_module(None) + with self.assertRaises(ImportError): + self.load_module('XXX') + + + def test_module(self): + with util.uncache(ext_util.NAME): + module = self.load_module(ext_util.NAME) + for attr, value in [('__name__', ext_util.NAME), + ('__file__', ext_util.FILEPATH), + ('__package__', '')]: + self.assertEqual(getattr(module, attr), value) + self.assertIn(ext_util.NAME, sys.modules) + self.assertIsInstance(module.__loader__, + machinery.ExtensionFileLoader) + + def test_package(self): + # Extensions are not found in packages. + pass + + def test_lacking_parent(self): + # Extensions are not found in packages. + pass + + def test_module_reuse(self): + with util.uncache(ext_util.NAME): + module1 = self.load_module(ext_util.NAME) + module2 = self.load_module(ext_util.NAME) + self.assertIs(module1, module2) + + def test_state_after_failure(self): + # No easy way to trigger a failure after a successful import. + pass + + def test_unloadable(self): + name = 'asdfjkl;' + with self.assertRaises(ImportError) as cm: + self.load_module(name) + self.assertEqual(cm.exception.name, name) + + +def test_main(): + from test.support import run_unittest + run_unittest(LoaderTests) + + +if __name__ == '__main__': + test_main() diff --git a/Lib/test/test_importlib/extension/test_path_hook.py b/Lib/test/test_importlib/extension/test_path_hook.py new file mode 100644 index 0000000..129e6e2 --- /dev/null +++ b/Lib/test/test_importlib/extension/test_path_hook.py @@ -0,0 +1,32 @@ +from importlib import _bootstrap +from . import util + +import collections +import imp +import sys +import unittest + + +class PathHookTests(unittest.TestCase): + + """Test the path hook for extension modules.""" + # XXX Should it only succeed for pre-existing directories? + # XXX Should it only work for directories containing an extension module? + + def hook(self, entry): + return _bootstrap.FileFinder.path_hook((_bootstrap.ExtensionFileLoader, + imp.extension_suffixes(), False))(entry) + + def test_success(self): + # Path hook should handle a directory where a known extension module + # exists. + self.assertTrue(hasattr(self.hook(util.PATH), 'find_module')) + + +def test_main(): + from test.support import run_unittest + run_unittest(PathHookTests) + + +if __name__ == '__main__': + test_main() diff --git a/Lib/test/test_importlib/extension/util.py b/Lib/test/test_importlib/extension/util.py new file mode 100644 index 0000000..a266dd9 --- /dev/null +++ b/Lib/test/test_importlib/extension/util.py @@ -0,0 +1,20 @@ +import imp +from importlib import machinery +import os +import sys + +PATH = None +EXT = None +FILENAME = None +NAME = '_testcapi' +try: + for PATH in sys.path: + for EXT in machinery.EXTENSION_SUFFIXES: + FILENAME = NAME + EXT + FILEPATH = os.path.join(PATH, FILENAME) + if os.path.exists(os.path.join(PATH, FILENAME)): + raise StopIteration + else: + PATH = EXT = FILENAME = FILEPATH = None +except StopIteration: + pass |