2024-10-04 01:51:34 +00:00
|
|
|
import shutil
|
|
|
|
from _warnings import warn
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
def tmp_output_dir(request: pytest.FixtureRequest) -> Path:
|
|
|
|
path = Path(__file__).parents[1].resolve() / "__tmp__"
|
|
|
|
if path.exists():
|
|
|
|
shutil.rmtree(str(path))
|
2024-10-19 03:11:58 +00:00
|
|
|
path.mkdir(parents=True, exist_ok=True)
|
2024-10-04 01:51:34 +00:00
|
|
|
|
|
|
|
yield path
|
|
|
|
|
|
|
|
if not request.config.getvalue("--with-output"):
|
|
|
|
try:
|
|
|
|
shutil.rmtree(str(path))
|
|
|
|
except PermissionError as e:
|
|
|
|
# sporadic error on windows machines...
|
|
|
|
warn(
|
2024-10-04 02:57:54 +00:00
|
|
|
"Temporary directory could not be removed due to a permissions error: "
|
|
|
|
f"\n{str(e)}"
|
2024-10-04 01:51:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
2024-10-19 03:11:58 +00:00
|
|
|
def tmp_output_dir_func(
|
|
|
|
tmp_output_dir, testrun_uid, request: pytest.FixtureRequest
|
|
|
|
) -> Path:
|
2024-10-04 01:51:34 +00:00
|
|
|
"""
|
|
|
|
tmp output dir that gets cleared between every function
|
|
|
|
cleans at the start rather than at cleanup in case the output is to be inspected
|
|
|
|
"""
|
|
|
|
subpath = tmp_output_dir / f"__tmpfunc_{request.node.name}__"
|
2024-10-19 03:11:58 +00:00
|
|
|
if testrun_uid:
|
|
|
|
subpath = subpath / testrun_uid
|
2024-10-04 01:51:34 +00:00
|
|
|
if subpath.exists():
|
|
|
|
shutil.rmtree(str(subpath))
|
2024-10-19 03:11:58 +00:00
|
|
|
subpath.mkdir(parents=True)
|
2024-10-04 01:51:34 +00:00
|
|
|
return subpath
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
def tmp_output_dir_mod(tmp_output_dir, request: pytest.FixtureRequest) -> Path:
|
|
|
|
"""
|
|
|
|
tmp output dir that gets cleared between every function
|
|
|
|
cleans at the start rather than at cleanup in case the output is to be inspected
|
|
|
|
"""
|
|
|
|
subpath = tmp_output_dir / f"__tmpmod_{request.module}__"
|
|
|
|
if subpath.exists():
|
|
|
|
shutil.rmtree(str(subpath))
|
2024-10-19 03:11:58 +00:00
|
|
|
subpath.mkdir(parents=True)
|
2024-10-04 01:51:34 +00:00
|
|
|
return subpath
|