| 1 | n/a | from importlib import _bootstrap |
|---|
| 2 | n/a | from importlib import machinery |
|---|
| 3 | n/a | from .. import util |
|---|
| 4 | n/a | from . import util as import_util |
|---|
| 5 | n/a | import imp |
|---|
| 6 | n/a | import os |
|---|
| 7 | n/a | import sys |
|---|
| 8 | n/a | import tempfile |
|---|
| 9 | n/a | from test import support |
|---|
| 10 | n/a | from types import MethodType |
|---|
| 11 | n/a | import unittest |
|---|
| 12 | n/a | import warnings |
|---|
| 13 | n/a | |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | class FinderTests(unittest.TestCase): |
|---|
| 16 | n/a | |
|---|
| 17 | n/a | """Tests for PathFinder.""" |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | def test_failure(self): |
|---|
| 20 | n/a | # Test None returned upon not finding a suitable finder. |
|---|
| 21 | n/a | module = '<test module>' |
|---|
| 22 | n/a | with util.import_state(): |
|---|
| 23 | n/a | self.assertIsNone(machinery.PathFinder.find_module(module)) |
|---|
| 24 | n/a | |
|---|
| 25 | n/a | def test_sys_path(self): |
|---|
| 26 | n/a | # Test that sys.path is used when 'path' is None. |
|---|
| 27 | n/a | # Implicitly tests that sys.path_importer_cache is used. |
|---|
| 28 | n/a | module = '<test module>' |
|---|
| 29 | n/a | path = '<test path>' |
|---|
| 30 | n/a | importer = util.mock_modules(module) |
|---|
| 31 | n/a | with util.import_state(path_importer_cache={path: importer}, |
|---|
| 32 | n/a | path=[path]): |
|---|
| 33 | n/a | loader = machinery.PathFinder.find_module(module) |
|---|
| 34 | n/a | self.assertIs(loader, importer) |
|---|
| 35 | n/a | |
|---|
| 36 | n/a | def test_path(self): |
|---|
| 37 | n/a | # Test that 'path' is used when set. |
|---|
| 38 | n/a | # Implicitly tests that sys.path_importer_cache is used. |
|---|
| 39 | n/a | module = '<test module>' |
|---|
| 40 | n/a | path = '<test path>' |
|---|
| 41 | n/a | importer = util.mock_modules(module) |
|---|
| 42 | n/a | with util.import_state(path_importer_cache={path: importer}): |
|---|
| 43 | n/a | loader = machinery.PathFinder.find_module(module, [path]) |
|---|
| 44 | n/a | self.assertIs(loader, importer) |
|---|
| 45 | n/a | |
|---|
| 46 | n/a | def test_empty_list(self): |
|---|
| 47 | n/a | # An empty list should not count as asking for sys.path. |
|---|
| 48 | n/a | module = 'module' |
|---|
| 49 | n/a | path = '<test path>' |
|---|
| 50 | n/a | importer = util.mock_modules(module) |
|---|
| 51 | n/a | with util.import_state(path_importer_cache={path: importer}, |
|---|
| 52 | n/a | path=[path]): |
|---|
| 53 | n/a | self.assertIsNone(machinery.PathFinder.find_module('module', [])) |
|---|
| 54 | n/a | |
|---|
| 55 | n/a | def test_path_hooks(self): |
|---|
| 56 | n/a | # Test that sys.path_hooks is used. |
|---|
| 57 | n/a | # Test that sys.path_importer_cache is set. |
|---|
| 58 | n/a | module = '<test module>' |
|---|
| 59 | n/a | path = '<test path>' |
|---|
| 60 | n/a | importer = util.mock_modules(module) |
|---|
| 61 | n/a | hook = import_util.mock_path_hook(path, importer=importer) |
|---|
| 62 | n/a | with util.import_state(path_hooks=[hook]): |
|---|
| 63 | n/a | loader = machinery.PathFinder.find_module(module, [path]) |
|---|
| 64 | n/a | self.assertIs(loader, importer) |
|---|
| 65 | n/a | self.assertIn(path, sys.path_importer_cache) |
|---|
| 66 | n/a | self.assertIs(sys.path_importer_cache[path], importer) |
|---|
| 67 | n/a | |
|---|
| 68 | n/a | def test_empty_path_hooks(self): |
|---|
| 69 | n/a | # Test that if sys.path_hooks is empty a warning is raised, |
|---|
| 70 | n/a | # sys.path_importer_cache gets None set, and PathFinder returns None. |
|---|
| 71 | n/a | path_entry = 'bogus_path' |
|---|
| 72 | n/a | with util.import_state(path_importer_cache={}, path_hooks=[], |
|---|
| 73 | n/a | path=[path_entry]): |
|---|
| 74 | n/a | with warnings.catch_warnings(record=True) as w: |
|---|
| 75 | n/a | warnings.simplefilter('always') |
|---|
| 76 | n/a | self.assertIsNone(machinery.PathFinder.find_module('os')) |
|---|
| 77 | n/a | self.assertIsNone(sys.path_importer_cache[path_entry]) |
|---|
| 78 | n/a | self.assertEqual(len(w), 1) |
|---|
| 79 | n/a | self.assertTrue(issubclass(w[-1].category, ImportWarning)) |
|---|
| 80 | n/a | |
|---|
| 81 | n/a | def test_path_importer_cache_empty_string(self): |
|---|
| 82 | n/a | # The empty string should create a finder using the cwd. |
|---|
| 83 | n/a | path = '' |
|---|
| 84 | n/a | module = '<test module>' |
|---|
| 85 | n/a | importer = util.mock_modules(module) |
|---|
| 86 | n/a | hook = import_util.mock_path_hook(os.curdir, importer=importer) |
|---|
| 87 | n/a | with util.import_state(path=[path], path_hooks=[hook]): |
|---|
| 88 | n/a | loader = machinery.PathFinder.find_module(module) |
|---|
| 89 | n/a | self.assertIs(loader, importer) |
|---|
| 90 | n/a | self.assertIn(os.curdir, sys.path_importer_cache) |
|---|
| 91 | n/a | |
|---|
| 92 | n/a | |
|---|
| 93 | n/a | def test_main(): |
|---|
| 94 | n/a | from test.support import run_unittest |
|---|
| 95 | n/a | run_unittest(FinderTests) |
|---|
| 96 | n/a | |
|---|
| 97 | n/a | if __name__ == '__main__': |
|---|
| 98 | n/a | test_main() |
|---|