Files
SPHLite/tools/testing/test_cli.py
T

181 lines
7.1 KiB
Python
Raw Normal View History

2026-07-02 19:50:44 +08:00
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}")
2026-07-02 21:26:32 +08:00
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 read_summary(path: Path) -> dict[str, str]:
metrics: dict[str, str] = {}
for line in path.read_text(encoding="utf-8").splitlines()[1:]:
if "," in line:
name, value = line.split(",", 1)
metrics[name.strip()] = value.strip()
return metrics
2026-07-02 19:50:44 +08:00
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")
2026-07-03 11:50:07 +08:00
if "0.4.0" not in version.stdout:
2026-07-02 19:50:44 +08:00
raise AssertionError("CLI version mismatch")
inspect = run_command([exe, model])
2026-07-02 21:26:32 +08:00
for field in (
"version",
"input",
"mode",
"particles",
"warnings",
"time",
"step",
"water_particles",
"water_mass",
"max_pressure",
"dtcrit",
2026-07-03 09:55:48 +08:00
"gas_particles",
"gas_volume",
"gas_mean_pressure",
"gas_max_pressure",
2026-07-03 11:04:59 +08:00
"gas_mass",
"gas_momentum_x",
"gas_min_density",
"gas_max_density",
2026-07-03 09:55:48 +08:00
"gas_equivalent_radius",
2026-07-03 11:04:59 +08:00
"gas_dt_cfl",
"gas_dt_accel",
2026-07-03 09:55:48 +08:00
"gas_dtcrit",
2026-07-03 11:04:59 +08:00
"gas_source_mass",
"gas_source_energy",
2026-07-02 21:26:32 +08:00
"status",
):
2026-07-02 19:50:44 +08:00
require_field(inspect.stdout, field)
2026-07-02 21:26:32 +08:00
inspect_fields = read_fields(inspect.stdout)
2026-07-03 11:50:07 +08:00
if inspect_fields["version"] != "0.4.0":
2026-07-02 23:53:17 +08:00
raise AssertionError("inspect version field mismatch")
if Path(inspect_fields["input"]) != Path(model):
raise AssertionError("inspect input field mismatch")
2026-07-02 21:26:32 +08:00
if inspect_fields["mode"] != "inspect":
2026-07-02 19:50:44 +08:00
raise AssertionError("inspect mode not reported")
2026-07-02 23:53:17 +08:00
if int(inspect_fields["particles"]) != 4:
raise AssertionError("CLI particle count mismatch")
if int(inspect_fields["warnings"]) != 0:
raise AssertionError("CLI warning count mismatch")
if abs(float(inspect_fields["time"])) > 1.0e-15:
raise AssertionError("inspect time should be zero")
if int(inspect_fields["step"]) != 0:
raise AssertionError("inspect step should be zero")
2026-07-02 21:26:32 +08:00
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")
2026-07-02 23:53:17 +08:00
if float(inspect_fields["max_pressure"]) < 0.0:
raise AssertionError("CLI max pressure mismatch")
2026-07-02 21:26:32 +08:00
if float(inspect_fields["dtcrit"]) <= 0.0:
raise AssertionError("CLI dtcrit mismatch")
2026-07-03 09:55:48 +08:00
if int(inspect_fields["gas_particles"]) != 0:
raise AssertionError("CLI water inspect gas particle count mismatch")
2026-07-02 23:53:17 +08:00
if inspect_fields["status"] != "ok":
raise AssertionError("CLI status field mismatch")
2026-07-02 19:50:44 +08:00
2026-07-03 09:55:48 +08:00
gas_model = str(Path(args.input).parent / "gas_ideal_minimal_030.inp")
gas_inspect = run_command([exe, gas_model])
gas_fields = read_fields(gas_inspect.stdout)
if int(gas_fields["gas_particles"]) != 4:
raise AssertionError("CLI gas particle count mismatch")
if float(gas_fields["gas_volume"]) <= 0.0:
raise AssertionError("CLI gas volume mismatch")
if float(gas_fields["gas_max_pressure"]) <= 0.0:
raise AssertionError("CLI gas pressure mismatch")
if float(gas_fields["gas_equivalent_radius"]) <= 0.0:
raise AssertionError("CLI gas radius mismatch")
if float(gas_fields["gas_dtcrit"]) <= 0.0:
raise AssertionError("CLI gas dt mismatch")
2026-07-03 11:04:59 +08:00
if float(gas_fields["gas_mass"]) <= 0.0:
raise AssertionError("CLI gas extended mass mismatch")
if float(gas_fields["gas_source_mass"]) != 0.0:
raise AssertionError("CLI gas source default mismatch")
2026-07-03 09:55:48 +08:00
2026-07-02 19:50:44 +08:00
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])
2026-07-02 21:26:32 +08:00
run_fields = read_fields(run.stdout)
if run_fields["mode"] != "run":
2026-07-02 19:50:44 +08:00
raise AssertionError("run mode not reported")
2026-07-02 23:53:17 +08:00
if float(run_fields["time"]) <= 0.0:
raise AssertionError("run mode time did not advance")
2026-07-02 21:26:32 +08:00
if int(run_fields["step"]) <= 0:
2026-07-02 19:50:44 +08:00
raise AssertionError("run mode did not advance")
2026-07-02 21:26:32 +08:00
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")
summary = read_summary(summary_file)
if int(summary["step"]) != output_step:
raise AssertionError("CLI summary step does not match actual step")
if abs(float(summary["time"]) - float(run_fields["time"])) > 1.0e-12:
raise AssertionError("CLI summary time does not match actual time")
2026-07-02 19:50:44 +08:00
bad = run_command([exe, bad_model, "--check"], expect_ok=False)
require_field(bad.stderr, "error_code")
require_field(bad.stderr, "error_message")
2026-07-02 23:53:17 +08:00
missing_arg = run_command([exe], expect_ok=False)
if "usage:" not in missing_arg.stdout:
raise AssertionError("missing input did not print usage")
missing_file = run_command([exe, str(Path(output_dir) / "missing.inp")], expect_ok=False)
require_field(missing_file.stderr, "error_code")
conflict = run_command([exe, model, "--run", "--check"], expect_ok=False)
if "usage:" not in conflict.stdout:
raise AssertionError("conflicting modes did not print usage")
missing_output = run_command([exe, model, "--run", "--output"], expect_ok=False)
if "usage:" not in missing_output.stdout:
raise AssertionError("missing output path did not print usage")
illegal_output = run_command(
[exe, model, "--run", "--output", str(Path(output_dir) / "missing-dir" / "nested")],
expect_ok=False,
)
require_field(illegal_output.stderr, "error_code")
2026-07-02 19:50:44 +08:00
return 0
if __name__ == "__main__":
raise SystemExit(main())