refactor(testing.pytest): change mock_pkg interface

being more explicit about what the fixture provides as an output, instead of
solely describing the site effects.

Also the consumer is now responsible for the module directory layout.
This commit is contained in:
Tiara Rodney 2025-06-21 18:14:52 +02:00
parent e6bf657919
commit aa2540cf3f
No known key found for this signature in database
GPG key ID: 5F43FAB4FBE5B5EB
2 changed files with 28 additions and 19 deletions

View file

@ -7,6 +7,8 @@ import pytest
from byteb4rb1e.testing.pytest import get_current_test from byteb4rb1e.testing.pytest import get_current_test
_SITE_PACKAGE_COUNTER: Dict[str, int] = {}
@pytest.fixture @pytest.fixture
def current_test() -> Tuple[Path, str]: def current_test() -> Tuple[Path, str]:
@ -16,25 +18,27 @@ def current_test() -> Tuple[Path, str]:
@pytest.fixture @pytest.fixture
def mock_pkg(tmp_path): def mock_system_site_package_dir(tmp_path):
def _create(name: str, files: Dict[str, Union[str, bytes]]): global _SITE_PACKAGE_COUNTER
pkg_path = tmp_path / name.replace('.', os.path.sep)
package_id = _SITE_PACKAGE_COUNTER.setdefault(tmp_path, 0)
_SITE_PACKAGE_COUNTER[tmp_path] += 1
sys_path = tmp_path / str(package_id)
def _create(name: str) -> Path:
pkg_path = sys_path / name.replace('.', os.path.sep)
pkg_path.mkdir(parents=True) pkg_path.mkdir(parents=True)
(pkg_path / "__init__.py").touch() (pkg_path / "__init__.py").touch()
for fname, content in files.items(): sys.path.insert(0, str(sys_path))
fpath = (pkg_path / fname)
fpath.parent.mkdir(parents=True, exist_ok=True)
if isinstance(content, str):
fpath.write_text(content)
else:
fpath.write_bytes(content)
sys.path.insert(0, str(tmp_path)) return pkg_path
return name, pkg_path
yield _create yield _create
# cleanup sys.path after test # cleanup sys.path after test
if str(tmp_path) in sys.path: if str(sys_path) in sys.path:
sys.path.remove(str(tmp_path)) sys.path.remove(str(sys_path))

View file

@ -6,7 +6,10 @@ import pytest
pytestmark = pytest.mark.pytest pytestmark = pytest.mark.pytest
from byteb4rb1e.testing.pytest.decorators import run_in_subprocess_once from byteb4rb1e.testing.pytest.decorators import run_in_subprocess_once
from byteb4rb1e.testing.pytest.fixtures import current_test, mock_pkg from byteb4rb1e.testing.pytest.fixtures import (
current_test,
mock_system_site_package_dir
)
def test_current_test(current_test): def test_current_test(current_test):
@ -19,14 +22,16 @@ def test_current_test(current_test):
@run_in_subprocess_once() @run_in_subprocess_once()
def test_mock_pkg(mock_pkg): def test_mock_system_site_package_dir(mock_system_site_package_dir):
""" """
""" """
dummy_data = 'Hello' dummy_data = 'Hello'
mock_pkg('foobarpkg', { pkgdir = mock_system_site_package_dir('foobarpkg')
'data.txt': dummy_data
}) (pkgdir / 'data.txt').write_text(dummy_data)
assert (pkgdir / '__init__.py').exists()
result = next(importlib.resources.files('foobarpkg').glob('data.txt')).read_text() result = next(importlib.resources.files('foobarpkg').glob('data.txt')).read_text()