Files
SPHLite/tools/testing/test_cli.py
T
2026-07-02 19:55:48 +08:00

64 lines
2.1 KiB
Python

from __future__ import annotations
import argparse
import subprocess
from pathlib import Path
def run_command(args: list[str], expect_ok: bool = True) -> subprocess.CompletedProcess[str]:
result = subprocess.run(args, text=True, capture_output=True)
if expect_ok and result.returncode != 0:
raise AssertionError(result.stderr or result.stdout)
if not expect_ok and result.returncode == 0:
raise AssertionError("command unexpectedly succeeded")
return result
def require_field(output: str, field: str) -> None:
if f"{field}:" not in output:
raise AssertionError(f"missing CLI field {field}")
def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument("--exe", required=True)
parser.add_argument("--input", required=True)
parser.add_argument("--bad-input", required=True)
parser.add_argument("--output-dir", required=True)
args = parser.parse_args()
exe = str(Path(args.exe))
model = str(Path(args.input))
bad_model = str(Path(args.bad_input))
output_dir = str(Path(args.output_dir))
version = run_command([exe, "--version"])
require_field(version.stdout, "version")
if "0.2.0" not in version.stdout:
raise AssertionError("CLI version mismatch")
inspect = run_command([exe, model])
for field in ("version", "input", "mode", "particles", "warnings", "time", "step", "status"):
require_field(inspect.stdout, field)
if "mode: inspect" not in inspect.stdout:
raise AssertionError("inspect mode not reported")
check = run_command([exe, model, "--check"])
if "mode: check" not in check.stdout:
raise AssertionError("check mode not reported")
run = run_command([exe, model, "--run", "--output", output_dir])
if "mode: run" not in run.stdout:
raise AssertionError("run mode not reported")
if "step: 1" not in run.stdout:
raise AssertionError("run mode did not advance")
bad = run_command([exe, bad_model, "--check"], expect_ok=False)
require_field(bad.stderr, "error_code")
require_field(bad.stderr, "error_message")
return 0
if __name__ == "__main__":
raise SystemExit(main())