Merge branch 'feature/12' into dev
ID: 12
Type: feature
Title: simplify testing.fixtures.mock_pkg
Status: done
Priority: high
Created: 2025-06-21
Description: Only bootstrap a package mock with the minimum requirements for a
Python module and let the consumer handle the directory layout.
This commit is contained in:
commit
aad29bc76a
3 changed files with 29 additions and 20 deletions
2
TODO
2
TODO
|
|
@ -178,7 +178,7 @@ Description: to shorten the namespace and also indicate that testing utilities
|
|||
ID: 12
|
||||
Type: feature
|
||||
Title: simplify testing.fixtures.mock_pkg
|
||||
Status: in-progress
|
||||
Status: done
|
||||
Priority: high
|
||||
Created: 2025-06-21
|
||||
Description: Only bootstrap a package mock with the minimum requirements for a
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue