From: Brett Cannon Date: Thu, 5 Feb 2009 02:53:23 +0000 (+0000) Subject: Begin writing tests for importlib.machinery.PathFinder. X-Git-Tag: v3.1a1~287 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=939ea76b3ce0cdc9a878f9499b40091fc6fff3d1;p=python Begin writing tests for importlib.machinery.PathFinder. --- diff --git a/Lib/importlib/test/import_/test_path.py b/Lib/importlib/test/import_/test_path.py index b008b0959b..3eb22c98a3 100644 --- a/Lib/importlib/test/import_/test_path.py +++ b/Lib/importlib/test/import_/test_path.py @@ -1,3 +1,4 @@ +from importlib import machinery from .. import util from . import util as import_util from contextlib import nested @@ -149,9 +150,64 @@ class __path__Tests(BaseTests): self.path_argument_test(name) +class FinderTests(unittest.TestCase): + + """Tests for SysPathImporter.""" + + def test_failure(self): + # Test None returned upon not finding a suitable finder. + def mock_implicit_hooks(): + return [] + # XXX Not blackbox. + original_hooks = machinery.PathFinder._implicit_hooks + machinery.PathFinder._implicit_hooks = staticmethod(mock_implicit_hooks) + try: + with util.import_state(): + self.assert_(machinery.PathFinder.find_module('XXX') is None) + finally: + machinery.PathFinder._implicit_hooks = original_hooks + + def test_sys_path(self): + # Test that sys.path is used when 'path' is None. + # Implicitly tests that sys.path_importer_cache is used. + module = '' + path = '' + importer = util.mock_modules(module) + with util.import_state(path_importer_cache={path: importer}, + path=[path]): + loader = machinery.PathFinder.find_module(module) + self.assert_(loader is importer) + + def test_path(self): + # Test that 'path' is used when set. + # Implicitly tests that sys.path_importer_cache is used. + module = '' + path = '' + importer = util.mock_modules(module) + with util.import_state(path_importer_cache={path: importer}): + loader = machinery.PathFinder.find_module(module, [path]) + self.assert_(loader is importer) + + def test_path_importer_cache_has_None(self): + # Test that the default hook is used when sys.path_importer_cache + # contains None for a path. + # TODO(brett.cannon) implement + pass + + def test_path_hooks(self): + # Test that sys.path_hooks is used. + # TODO(brett.cannon) implement + pass + + def test_implicit_hooks(self): + # Test that the implicit path hooks are used. + # TODO(brett.cannon) implement + pass + + def test_main(): from test.support import run_unittest - run_unittest(PathTests, __path__Tests) + run_unittest(PathTests, __path__Tests, FinderTests) if __name__ == '__main__': test_main()