From aa2540cf3f9a141e045e2e1c3b56ff7aa4a39673 Mon Sep 17 00:00:00 2001 From: Tiara Rodney Date: Sat, 21 Jun 2025 18:14:52 +0200 Subject: [PATCH] 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. --- src/byteb4rb1e/testing/pytest/fixtures.py | 32 +++++++++++-------- .../testing/pytest/test_fixtures.py | 15 ++++++--- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/src/byteb4rb1e/testing/pytest/fixtures.py b/src/byteb4rb1e/testing/pytest/fixtures.py index b9ca0cb..7c93041 100644 --- a/src/byteb4rb1e/testing/pytest/fixtures.py +++ b/src/byteb4rb1e/testing/pytest/fixtures.py @@ -7,6 +7,8 @@ import pytest from byteb4rb1e.testing.pytest import get_current_test +_SITE_PACKAGE_COUNTER: Dict[str, int] = {} + @pytest.fixture def current_test() -> Tuple[Path, str]: @@ -16,25 +18,27 @@ def current_test() -> Tuple[Path, str]: @pytest.fixture -def mock_pkg(tmp_path): - def _create(name: str, files: Dict[str, Union[str, bytes]]): - pkg_path = tmp_path / name.replace('.', os.path.sep) +def mock_system_site_package_dir(tmp_path): + global _SITE_PACKAGE_COUNTER + + 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 / "__init__.py").touch() - for fname, content in files.items(): - 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(sys_path)) - sys.path.insert(0, str(tmp_path)) - return name, pkg_path + return pkg_path yield _create # cleanup sys.path after test - if str(tmp_path) in sys.path: - sys.path.remove(str(tmp_path)) + if str(sys_path) in sys.path: + sys.path.remove(str(sys_path)) diff --git a/tests/integration/byteb4rb1e/testing/pytest/test_fixtures.py b/tests/integration/byteb4rb1e/testing/pytest/test_fixtures.py index 0e0c5d9..a08851d 100644 --- a/tests/integration/byteb4rb1e/testing/pytest/test_fixtures.py +++ b/tests/integration/byteb4rb1e/testing/pytest/test_fixtures.py @@ -6,7 +6,10 @@ import pytest pytestmark = pytest.mark.pytest 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): @@ -19,14 +22,16 @@ def test_current_test(current_test): @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' - mock_pkg('foobarpkg', { - 'data.txt': dummy_data - }) + pkgdir = mock_system_site_package_dir('foobarpkg') + + (pkgdir / 'data.txt').write_text(dummy_data) + + assert (pkgdir / '__init__.py').exists() result = next(importlib.resources.files('foobarpkg').glob('data.txt')).read_text()