Skip to content

Commit

Permalink
Adapt changes for new fixtures.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Aug 17, 2024
1 parent 198adec commit cba8dce
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions importlib_resources/tests/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import importlib_resources as resources
from ..abc import Traversable
from . import util
from .compat.py39 import os_helper, import_helper


@contextlib.contextmanager
Expand Down Expand Up @@ -112,6 +113,10 @@ class ImplicitContextFiles:
'submod.py': set_val,
'res.txt': 'resources are the best',
},
'frozenpkg': {
'__init__.py': set_val.replace('importlib_resources', 'c_resources'),
'res.txt': 'resources are the best',
},
}

def test_implicit_files_package(self):
Expand All @@ -126,32 +131,26 @@ def test_implicit_files_submodule(self):
"""
assert importlib.import_module('somepkg.submod').val == 'resources are the best'

def _compile_importlib(self, target_dir):
importlib_dir = pathlib.Path(importlib.__file__).parent
shutil.copytree(importlib_dir, target_dir, ignore=lambda *_: ['__pycache__'])
def _compile_importlib(self):
"""
Make a compiled-only copy of the importlib resources package.
"""
bin_site = self.fixtures.enter_context(os_helper.temp_dir())
c_resources = pathlib.Path(bin_site, 'c_resources')
sources = pathlib.Path(resources.__file__).parent
shutil.copytree(sources, c_resources, ignore=lambda *_: ['__pycache__'])

for dirpath, _, filenames in os.walk(target_dir):
for dirpath, _, filenames in os.walk(c_resources):
for filename in filenames:
source_path = pathlib.Path(dirpath) / filename
cfile = source_path.with_suffix('.pyc')
py_compile.compile(source_path, cfile)
pathlib.Path.unlink(source_path)
self.fixtures.enter_context(import_helper.DirsOnSysPath(bin_site))

def test_implicit_files_with_compiled_importlib(self):
self._compile_importlib(pathlib.Path(self.site_dir) / 'cimportlib')
spec = {
'somepkg': {
'__init__.py': textwrap.dedent(
"""
import cimportlib.resources as res
val = res.files().joinpath('res.txt').read_text(encoding='utf-8')
"""
),
'res.txt': 'resources are the best',
},
}
_path.build(spec, self.site_dir)
assert importlib.import_module('somepkg').val == 'resources are the best'
self._compile_importlib()
assert importlib.import_module('frozenpkg').val == 'resources are the best'


class ImplicitContextFilesDiskTests(
Expand Down

0 comments on commit cba8dce

Please sign in to comment.