| 1 | n/a | from .. import abc |
|---|
| 2 | n/a | from .. import util |
|---|
| 3 | n/a | |
|---|
| 4 | n/a | machinery = util.import_importlib('importlib.machinery') |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | import sys |
|---|
| 7 | n/a | import unittest |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | |
|---|
| 10 | n/a | @unittest.skipIf(util.BUILTINS.good_name is None, 'no reasonable builtin module') |
|---|
| 11 | n/a | class FindSpecTests(abc.FinderTests): |
|---|
| 12 | n/a | |
|---|
| 13 | n/a | """Test find_spec() for built-in modules.""" |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | def test_module(self): |
|---|
| 16 | n/a | # Common case. |
|---|
| 17 | n/a | with util.uncache(util.BUILTINS.good_name): |
|---|
| 18 | n/a | found = self.machinery.BuiltinImporter.find_spec(util.BUILTINS.good_name) |
|---|
| 19 | n/a | self.assertTrue(found) |
|---|
| 20 | n/a | self.assertEqual(found.origin, 'built-in') |
|---|
| 21 | n/a | |
|---|
| 22 | n/a | # Built-in modules cannot be a package. |
|---|
| 23 | n/a | test_package = None |
|---|
| 24 | n/a | |
|---|
| 25 | n/a | # Built-in modules cannobt be in a package. |
|---|
| 26 | n/a | test_module_in_package = None |
|---|
| 27 | n/a | |
|---|
| 28 | n/a | # Built-in modules cannot be a package. |
|---|
| 29 | n/a | test_package_in_package = None |
|---|
| 30 | n/a | |
|---|
| 31 | n/a | # Built-in modules cannot be a package. |
|---|
| 32 | n/a | test_package_over_module = None |
|---|
| 33 | n/a | |
|---|
| 34 | n/a | def test_failure(self): |
|---|
| 35 | n/a | name = 'importlib' |
|---|
| 36 | n/a | assert name not in sys.builtin_module_names |
|---|
| 37 | n/a | spec = self.machinery.BuiltinImporter.find_spec(name) |
|---|
| 38 | n/a | self.assertIsNone(spec) |
|---|
| 39 | n/a | |
|---|
| 40 | n/a | def test_ignore_path(self): |
|---|
| 41 | n/a | # The value for 'path' should always trigger a failed import. |
|---|
| 42 | n/a | with util.uncache(util.BUILTINS.good_name): |
|---|
| 43 | n/a | spec = self.machinery.BuiltinImporter.find_spec(util.BUILTINS.good_name, |
|---|
| 44 | n/a | ['pkg']) |
|---|
| 45 | n/a | self.assertIsNone(spec) |
|---|
| 46 | n/a | |
|---|
| 47 | n/a | |
|---|
| 48 | n/a | (Frozen_FindSpecTests, |
|---|
| 49 | n/a | Source_FindSpecTests |
|---|
| 50 | n/a | ) = util.test_both(FindSpecTests, machinery=machinery) |
|---|
| 51 | n/a | |
|---|
| 52 | n/a | |
|---|
| 53 | n/a | @unittest.skipIf(util.BUILTINS.good_name is None, 'no reasonable builtin module') |
|---|
| 54 | n/a | class FinderTests(abc.FinderTests): |
|---|
| 55 | n/a | |
|---|
| 56 | n/a | """Test find_module() for built-in modules.""" |
|---|
| 57 | n/a | |
|---|
| 58 | n/a | def test_module(self): |
|---|
| 59 | n/a | # Common case. |
|---|
| 60 | n/a | with util.uncache(util.BUILTINS.good_name): |
|---|
| 61 | n/a | found = self.machinery.BuiltinImporter.find_module(util.BUILTINS.good_name) |
|---|
| 62 | n/a | self.assertTrue(found) |
|---|
| 63 | n/a | self.assertTrue(hasattr(found, 'load_module')) |
|---|
| 64 | n/a | |
|---|
| 65 | n/a | # Built-in modules cannot be a package. |
|---|
| 66 | n/a | test_package = test_package_in_package = test_package_over_module = None |
|---|
| 67 | n/a | |
|---|
| 68 | n/a | # Built-in modules cannot be in a package. |
|---|
| 69 | n/a | test_module_in_package = None |
|---|
| 70 | n/a | |
|---|
| 71 | n/a | def test_failure(self): |
|---|
| 72 | n/a | assert 'importlib' not in sys.builtin_module_names |
|---|
| 73 | n/a | loader = self.machinery.BuiltinImporter.find_module('importlib') |
|---|
| 74 | n/a | self.assertIsNone(loader) |
|---|
| 75 | n/a | |
|---|
| 76 | n/a | def test_ignore_path(self): |
|---|
| 77 | n/a | # The value for 'path' should always trigger a failed import. |
|---|
| 78 | n/a | with util.uncache(util.BUILTINS.good_name): |
|---|
| 79 | n/a | loader = self.machinery.BuiltinImporter.find_module(util.BUILTINS.good_name, |
|---|
| 80 | n/a | ['pkg']) |
|---|
| 81 | n/a | self.assertIsNone(loader) |
|---|
| 82 | n/a | |
|---|
| 83 | n/a | |
|---|
| 84 | n/a | (Frozen_FinderTests, |
|---|
| 85 | n/a | Source_FinderTests |
|---|
| 86 | n/a | ) = util.test_both(FinderTests, machinery=machinery) |
|---|
| 87 | n/a | |
|---|
| 88 | n/a | |
|---|
| 89 | n/a | if __name__ == '__main__': |
|---|
| 90 | n/a | unittest.main() |
|---|