ยปCore Development>Code coverage>Lib/importlib/test/import_/test_path.py

Python code coverage for Lib/importlib/test/import_/test_path.py

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