import os
import platform
import shlex
import shutil
import subprocess
from pathlib import Path
from tempfile import TemporaryDirectory
import pytest
from file_utils import rm_rf
ROOT_DIR = Path(__file__).parent.parent.absolute()
IS_WINDOWS = platform.system() == "Windows"
BASH_CMD = os.environ.get("BASH_CMD", "bash")
def _run(*args, check=True, **kwargs):
proc = subprocess.run(
*args, capture_output=True, text=True, **kwargs
)
if check and proc.returncode != 0:
pytest.fail(f"""Command {args} failed with exit code {proc.returncode}
STDOUT:
{proc.stdout}
STDERR:
{proc.stderr}
""")
return proc
def run_clyde(*args, check=True):
cmd = shlex.join(["clyde", *args])
return run_in_clyde_home(cmd, check=check)
def run_in_clyde_home(cmd: str, check=True):
CLYDE_HOME = os.environ["CLYDE_HOME"]
script = f". scripts/activate.sh ; {cmd}"
cmd = [BASH_CMD, "-c", script]
return _run(cmd, check=check, cwd=str(CLYDE_HOME))
def get_bin_path(bin_name):
if IS_WINDOWS:
bin_name += ".exe"
return Path(os.environ["CLYDE_HOME"]) / "inst" / "bin" / bin_name
def _build_clyde() -> Path:
cmd = ["cargo", "build", "-r"]
_run(cmd, cwd=str(ROOT_DIR))
exe_name = "clyde.exe" if IS_WINDOWS else "clyde"
path = ROOT_DIR / "target" / "release" / exe_name
assert path.exists()
return path
@pytest.fixture(scope="session")
def _setup_clyde_home():
try:
del os.environ["CLYDE_INST_DIR"]
except KeyError:
pass
clyde_exe = _build_clyde()
with TemporaryDirectory(prefix="clyde-functests") as tmp_dir:
clyde_home = Path(tmp_dir) / "clyde_home"
backup_dir = Path(tmp_dir) / "backup"
os.environ["CLYDE_HOME"] = str(clyde_home)
_run([clyde_exe, "setup"])
shutil.copy(clyde_exe, get_bin_path("clyde"))
shutil.move(clyde_home, backup_dir)
yield clyde_home, backup_dir
@pytest.fixture()
def clyde_home(_setup_clyde_home):
clyde_home, backup_dir = _setup_clyde_home
rm_rf(clyde_home)
shutil.copytree(backup_dir, clyde_home)
yield clyde_home