|
| 1 | +import os |
| 2 | +import pathlib |
| 3 | +import py_compile |
| 4 | +import shutil |
1 | 5 | import textwrap
|
2 | 6 | import unittest
|
3 | 7 | import warnings
|
@@ -122,6 +126,33 @@ def test_implicit_files_submodule(self):
|
122 | 126 | """
|
123 | 127 | assert importlib.import_module('somepkg.submod').val == 'resources are the best'
|
124 | 128 |
|
| 129 | + def _compile_importlib(self, target_dir): |
| 130 | + importlib_dir = pathlib.Path(importlib.__file__).parent |
| 131 | + shutil.copytree(importlib_dir, target_dir, ignore=lambda *_: ['__pycache__']) |
| 132 | + |
| 133 | + for dirpath, _, filenames in os.walk(target_dir): |
| 134 | + for filename in filenames: |
| 135 | + source_path = pathlib.Path(dirpath) / filename |
| 136 | + cfile = source_path.with_suffix('.pyc') |
| 137 | + py_compile.compile(source_path, cfile) |
| 138 | + pathlib.Path.unlink(source_path) |
| 139 | + |
| 140 | + def test_implicit_files_with_compiled_importlib(self): |
| 141 | + self._compile_importlib(pathlib.Path(self.site_dir) / 'cimportlib') |
| 142 | + spec = { |
| 143 | + 'somepkg': { |
| 144 | + '__init__.py': textwrap.dedent( |
| 145 | + """ |
| 146 | + import cimportlib.resources as res |
| 147 | + val = res.files().joinpath('res.txt').read_text(encoding='utf-8') |
| 148 | + """ |
| 149 | + ), |
| 150 | + 'res.txt': 'resources are the best', |
| 151 | + }, |
| 152 | + } |
| 153 | + _path.build(spec, self.site_dir) |
| 154 | + assert importlib.import_module('somepkg').val == 'resources are the best' |
| 155 | + |
125 | 156 |
|
126 | 157 | class ImplicitContextFilesDiskTests(
|
127 | 158 | DirectSpec, util.DiskSetup, ImplicitContextFiles, unittest.TestCase
|
|
0 commit comments