import os
import shutil
import tempfile
from collections.abc import Generator
import pytest
@pytest.fixture(autouse=True)
def setup_workspace(request: pytest.FixtureRequest) -> Generator[None]:
old_root = os.environ.get("COREASON_WORKSPACE_ROOT")
old_cla = os.environ.get("COREASON_CLA_ACCEPTED")
os.environ["COREASON_CLA_ACCEPTED"] = "yes"
if "tmp_path" in request.fixturenames:
tmp_path = request.getfixturevalue("tmp_path")
os.environ["COREASON_WORKSPACE_ROOT"] = str(tmp_path)
yield
else:
temp_dir = tempfile.mkdtemp()
os.environ["COREASON_WORKSPACE_ROOT"] = temp_dir
try:
yield
finally:
shutil.rmtree(temp_dir, ignore_errors=True)
if old_root is not None:
os.environ["COREASON_WORKSPACE_ROOT"] = old_root
elif "COREASON_WORKSPACE_ROOT" in os.environ:
del os.environ["COREASON_WORKSPACE_ROOT"]
if old_cla is not None:
os.environ["COREASON_CLA_ACCEPTED"] = old_cla
elif "COREASON_CLA_ACCEPTED" in os.environ:
del os.environ["COREASON_CLA_ACCEPTED"]