Move python implementation under python/ in preparation for the upcoming Go rewrite
This commit is contained in:
117
python/tests/test_new_launcher.py
Normal file
117
python/tests/test_new_launcher.py
Normal file
@@ -0,0 +1,117 @@
|
||||
import importlib.machinery
|
||||
import importlib.util
|
||||
import io
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
LAUNCHER = ROOT / "audita"
|
||||
|
||||
|
||||
def _load_launcher_module():
|
||||
loader = importlib.machinery.SourceFileLoader("audita_launcher", str(LAUNCHER))
|
||||
spec = importlib.util.spec_from_file_location("audita_launcher", LAUNCHER, loader=loader)
|
||||
assert spec is not None and spec.loader is not None
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(module)
|
||||
return module
|
||||
|
||||
|
||||
def test_root_launcher_exists_and_is_executable():
|
||||
assert LAUNCHER.is_file()
|
||||
assert os.access(LAUNCHER, os.X_OK)
|
||||
|
||||
|
||||
def test_root_launcher_help_smoke():
|
||||
if shutil.which("uv") is None:
|
||||
pytest.skip("uv is not installed")
|
||||
|
||||
result = subprocess.run(
|
||||
[str(LAUNCHER), "--help"],
|
||||
cwd=ROOT,
|
||||
text=True,
|
||||
capture_output=True,
|
||||
check=False,
|
||||
)
|
||||
|
||||
assert result.returncode == 0
|
||||
assert result.stdout.startswith("usage: audita ")
|
||||
|
||||
|
||||
def test_root_launcher_writes_error_log_when_uv_is_missing(tmp_path):
|
||||
work_dir = tmp_path / "work"
|
||||
result = subprocess.run(
|
||||
[sys.executable, str(LAUNCHER), "process", "transcript.json", "--glossary", "glossary.yaml", "--work-dir", str(work_dir)],
|
||||
cwd=ROOT,
|
||||
text=True,
|
||||
capture_output=True,
|
||||
env={**os.environ, "PATH": ""},
|
||||
check=False,
|
||||
)
|
||||
|
||||
assert result.returncode == 1
|
||||
assert result.stdout == ""
|
||||
assert "audita: error: uv is required to run this launcher" in result.stderr
|
||||
run_dir = next(work_dir.iterdir())
|
||||
assert f"audita: run directory: {run_dir}" in result.stderr
|
||||
assert (run_dir / "error.log").exists()
|
||||
|
||||
|
||||
def test_root_launcher_preserves_child_exit_code_and_writes_fallback_error_log(tmp_path):
|
||||
work_dir = tmp_path / "work"
|
||||
fake_bin = tmp_path / "bin"
|
||||
fake_bin.mkdir()
|
||||
fake_uv = fake_bin / "uv"
|
||||
fake_uv.write_text("#!/bin/sh\nexit 120\n", encoding="utf-8")
|
||||
fake_uv.chmod(0o755)
|
||||
|
||||
result = subprocess.run(
|
||||
[sys.executable, str(LAUNCHER), "process", "transcript.json", "--glossary", "glossary.yaml", "--work-dir", str(work_dir)],
|
||||
cwd=ROOT,
|
||||
text=True,
|
||||
capture_output=True,
|
||||
env={**os.environ, "PATH": str(fake_bin)},
|
||||
check=False,
|
||||
)
|
||||
|
||||
assert result.returncode == 120
|
||||
assert result.stdout == ""
|
||||
assert "audita: subprocess exited with status 120" in result.stderr
|
||||
run_dir = next(work_dir.iterdir())
|
||||
assert (run_dir / "error.log").exists()
|
||||
|
||||
|
||||
class _BrokenWriter:
|
||||
def write(self, _message):
|
||||
raise OSError(9, "Bad file descriptor")
|
||||
|
||||
def flush(self):
|
||||
raise OSError(9, "Bad file descriptor")
|
||||
|
||||
|
||||
def test_root_launcher_falls_back_to_stdout_when_stderr_is_invalid(monkeypatch, tmp_path):
|
||||
launcher = _load_launcher_module()
|
||||
work_dir = tmp_path / "work"
|
||||
stdout_capture = io.StringIO()
|
||||
|
||||
monkeypatch.setattr(launcher.shutil, "which", lambda _name: None)
|
||||
monkeypatch.setattr(
|
||||
launcher.sys,
|
||||
"argv",
|
||||
["audita", "process", "transcript.json", "--glossary", "glossary.yaml", "--work-dir", str(work_dir)],
|
||||
)
|
||||
monkeypatch.setattr(launcher.sys, "stderr", _BrokenWriter())
|
||||
monkeypatch.setattr(launcher.sys, "stdout", stdout_capture)
|
||||
|
||||
exit_code = launcher.main()
|
||||
|
||||
assert exit_code == 1
|
||||
assert "audita: error: uv is required to run this launcher" in stdout_capture.getvalue()
|
||||
run_dir = next(work_dir.iterdir())
|
||||
assert (run_dir / "error.log").exists()
|
||||
Reference in New Issue
Block a user