99 lines
3.3 KiB
Python
99 lines
3.3 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 read_fields(output: str) -> dict[str, str]:
|
|
fields: dict[str, str] = {}
|
|
for line in output.splitlines():
|
|
if ":" in line:
|
|
name, value = line.split(":", 1)
|
|
fields[name.strip()] = value.strip()
|
|
return fields
|
|
|
|
|
|
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",
|
|
"water_particles",
|
|
"water_mass",
|
|
"max_pressure",
|
|
"dtcrit",
|
|
"status",
|
|
):
|
|
require_field(inspect.stdout, field)
|
|
inspect_fields = read_fields(inspect.stdout)
|
|
if inspect_fields["mode"] != "inspect":
|
|
raise AssertionError("inspect mode not reported")
|
|
if int(inspect_fields["water_particles"]) != 4:
|
|
raise AssertionError("CLI water particle count mismatch")
|
|
if float(inspect_fields["water_mass"]) <= 0.0:
|
|
raise AssertionError("CLI water mass mismatch")
|
|
if float(inspect_fields["dtcrit"]) <= 0.0:
|
|
raise AssertionError("CLI dtcrit mismatch")
|
|
|
|
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])
|
|
run_fields = read_fields(run.stdout)
|
|
if run_fields["mode"] != "run":
|
|
raise AssertionError("run mode not reported")
|
|
if int(run_fields["step"]) <= 0:
|
|
raise AssertionError("run mode did not advance")
|
|
output_step = int(run_fields["step"])
|
|
output_file = Path(output_dir) / f"sphlite_{output_step:08d}.csv"
|
|
summary_file = Path(output_dir) / f"sphlite_{output_step:08d}_summary.csv"
|
|
if not output_file.exists() or not summary_file.exists():
|
|
raise AssertionError("CLI output files were not written")
|
|
|
|
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())
|