feat(testing.pytest): add pkg mock fixture
This commit is contained in:
parent
43cdf21d4b
commit
24806959bb
2 changed files with 45 additions and 2 deletions
|
|
@ -1,6 +1,7 @@
|
|||
import os
|
||||
from pathlib import Path
|
||||
from typing import Tuple
|
||||
import sys
|
||||
from typing import Dict, Tuple, Union
|
||||
|
||||
import pytest
|
||||
|
||||
|
|
@ -12,3 +13,28 @@ def current_test() -> Tuple[Path, str]:
|
|||
"""
|
||||
"""
|
||||
return get_current_test()
|
||||
|
||||
|
||||
@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)
|
||||
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(tmp_path))
|
||||
return name, pkg_path
|
||||
|
||||
yield _create
|
||||
|
||||
# cleanup sys.path after test
|
||||
if str(tmp_path) in sys.path:
|
||||
sys.path.remove(str(tmp_path))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue