chore: move testing utils out of utils
This commit is contained in:
parent
55ec6323bb
commit
9abfabde00
7 changed files with 9 additions and 9 deletions
|
|
@ -1,14 +0,0 @@
|
|||
import os
|
||||
from pathlib import Path
|
||||
from typing import Tuple
|
||||
|
||||
|
||||
def get_current_test() -> Tuple[Path, str]:
|
||||
current_test_env = os.getenv("PYTEST_CURRENT_TEST")
|
||||
if current_test_env is None:
|
||||
raise RuntimeError("PYTEST_CURRENT_TEST not set. Must be run under pytest.")
|
||||
|
||||
suite_path, case_name = current_test_env.split('::', 1)
|
||||
case_name = case_name.split(' ', 1)[0]
|
||||
return Path(suite_path).resolve(), case_name
|
||||
|
||||
|
|
@ -1,47 +0,0 @@
|
|||
from functools import wraps
|
||||
from pathlib import Path
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
from byteb4rb1e.utils.testing.pytest import get_current_test
|
||||
|
||||
|
||||
def run_in_subprocess_once():
|
||||
"""
|
||||
A decorator that reruns th test in a subprocess if not already inside one.
|
||||
Requires pytest to be installed and test to be run by pytest.
|
||||
|
||||
For what? Anything that can't be done in a thread-safe manner, e.g. modifying PYTHON_PATH
|
||||
"""
|
||||
def decorator(test_func):
|
||||
@wraps(test_func)
|
||||
def wrapper(*args, **kwargs):
|
||||
if os.environ.get("XPYTEST_INSIDE_SUBPROCESS") == "1":
|
||||
return test_func(*args, **kwargs)
|
||||
|
||||
suite_path, case_name = get_current_test()
|
||||
|
||||
cmd = [
|
||||
sys.executable,
|
||||
"-m", "pytest",
|
||||
f"{suite_path}::{case_name}",
|
||||
]
|
||||
|
||||
result = subprocess.run(
|
||||
cmd,
|
||||
env={**os.environ, "XPYTEST_INSIDE_SUBPROCESS": "1"},
|
||||
capture_output=True,
|
||||
text=True,
|
||||
)
|
||||
|
||||
if result.returncode != 0:
|
||||
print(' '.join(cmd))
|
||||
print("==== Subprocess stdout ====")
|
||||
print(result.stdout)
|
||||
print("==== Subprocess stderr ====")
|
||||
print(result.stderr)
|
||||
raise AssertionError(f"Subprocess test failed with exit code {result.returncode}")
|
||||
return wrapper
|
||||
return decorator
|
||||
|
||||
|
|
@ -1,40 +0,0 @@
|
|||
import os
|
||||
from pathlib import Path
|
||||
import sys
|
||||
from typing import Dict, Tuple, Union
|
||||
|
||||
import pytest
|
||||
|
||||
from byteb4rb1e.utils.testing.pytest import get_current_test
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
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