From b3cbbe7bd5926a78d08248ec92e6cd59a528a28d Mon Sep 17 00:00:00 2001 From: Victor Santos Oliveira <56417388+Vittuu@users.noreply.github.com> Date: Mon, 26 Oct 2026 10:38:56 +0000 Subject: [PATCH] CLI: reject incomplete and unsupported run/eval options (#6918) (#6919) * claim: #6918 validate run and eval options * fix(cli): reject invalid run and eval options (#6918) --- apps/solidcraft-cli/src/main.rs | 2 + apps/solidcraft-cli/tests/run_results.rs | 99 ++++++++++++++++++++++++ docs/perf.md | 2 +- 3 files changed, 102 insertions(+), 1 deletion(-) diff --git a/apps/solidcraft-cli/src/main.rs b/apps/solidcraft-cli/src/main.rs index 51ee2bb76..95aae0b84 100644 --- a/apps/solidcraft-cli/src/main.rs +++ b/apps/solidcraft-cli/src/main.rs @@ -340,6 +340,7 @@ fn print_json(v: &Value, file: Option<&str>) -> Result<(), String> { fn cmd_run(args: &[String]) -> Result<(), String> { install_renderer(); let json_out = json_out(args)?; + known_args("run", args, &["--in", "--out", "--save", "--json-out"], &["--quiet", "--results", "--timings", "--profile", "--allow-errors"], true)?; let base = flag(args, "--in"); let path = positional(args); if path.is_none() && base.is_none() { @@ -444,6 +445,7 @@ fn timeline_ok(s: &Session) -> Result<(), String> { fn cmd_eval(args: &[String]) -> Result<(), String> { let json_out = json_out(args)?; + known_args("eval", args, &["--in", "--json-out"], &[], true)?; let path = flag(args, "--in").or_else(|| positional(args).map(String::as_str)).ok_or_else(usage)?; let mut s = load(path)?; let m = s.execute("inspect.measure", &json!({})).map_err(|e| e.to_string())?; diff --git a/apps/solidcraft-cli/tests/run_results.rs b/apps/solidcraft-cli/tests/run_results.rs index c095c5305..978a7b86c 100644 --- a/apps/solidcraft-cli/tests/run_results.rs +++ b/apps/solidcraft-cli/tests/run_results.rs @@ -22,6 +22,19 @@ impl Scratch { std::fs::write(&path, json!({"commands": [{"command": "solid.box", "params": {"length": 2, "width": 3, "height": 4}}]}).to_string()).unwrap(); path } + + fn saving_script(&self) -> (PathBuf, PathBuf) { + let path = self.0.join("saving.json"); + let saved = self.0.join("script-ran.solidcraft"); + std::fs::write( + &path, + json!({"commands": [{"command": "solid.box", "params": {"length": 2, "width": 3, "height": 4}}, + {"command": "file.save_as", "params": {"path": saved}}]}) + .to_string(), + ) + .unwrap(); + (path, saved) + } } impl Drop for Scratch { @@ -43,6 +56,92 @@ fn body_count(path: &Path) -> Value { json_output(cli().arg("eval").arg(path).output().unwrap())["body_count"].clone() } +#[test] +fn run_rejects_invalid_options_before_script_execution_or_output_writes() { + let dir = Scratch::new(); + let (script, script_saved) = dir.saving_script(); + let cases = [ + (&["--out"][..], "--out needs a value"), + (&["--save"][..], "--save needs a value"), + (&["--in"][..], "--in needs a value"), + (&["--out", "--quiet"][..], "--out needs a value"), + (&["--ouut", "part.step"][..], "doesn't take --ouut"), + (&["--unknown"][..], "doesn't take --unknown"), + ]; + for existing in [false, true] { + let saved = dir.0.join(format!("{existing}.solidcraft")); + let exported = dir.0.join(format!("{existing}.step")); + let report = dir.0.join(format!("{existing}.json")); + let outputs = [&saved, &exported, &report]; + if existing { + for path in outputs { + std::fs::write(path, b"existing output").unwrap(); + } + } + for (extra, message) in cases { + let output = cli() + .arg("run") + .arg(&script) + .arg("--save") + .arg(&saved) + .arg("--out") + .arg(&exported) + .arg("--json-out") + .arg(&report) + .args(extra) + .output() + .unwrap(); + assert_eq!(output.status.code(), Some(1), "{extra:?}"); + assert!(String::from_utf8_lossy(&output.stderr).contains(message), "{extra:?}: {}", String::from_utf8_lossy(&output.stderr)); + assert!(output.stdout.is_empty()); + assert!(!script_saved.exists(), "invalid arguments must be rejected before the script runs"); + for path in outputs { + if existing { + assert_eq!(std::fs::read(path).unwrap(), b"existing output"); + } else { + assert!(!path.exists()); + } + } + } + } +} + +#[test] +fn eval_and_inspect_reject_invalid_options_before_script_execution_or_reporting() { + let dir = Scratch::new(); + let (script, script_saved) = dir.saving_script(); + let cases = [ + (&["--in"][..], "--in needs a value"), + (&["--in", "--quiet"][..], "--in needs a value"), + (&["--out", "part.step"][..], "doesn't take --out"), + (&["--unknown"][..], "doesn't take --unknown"), + ]; + for command in ["eval", "inspect"] { + for existing in [false, true] { + let report = dir.0.join(format!("{command}-{existing}.json")); + if existing { + std::fs::write(&report, b"existing report").unwrap(); + } + for (extra, message) in cases { + let output = cli().arg(command).arg(&script).arg("--json-out").arg(&report).args(extra).output().unwrap(); + assert_eq!(output.status.code(), Some(1), "{command} {extra:?}"); + assert!( + String::from_utf8_lossy(&output.stderr).contains(message), + "{command} {extra:?}: {}", + String::from_utf8_lossy(&output.stderr) + ); + assert!(output.stdout.is_empty()); + assert!(!script_saved.exists(), "invalid arguments must be rejected before the script runs"); + if existing { + assert_eq!(std::fs::read(&report).unwrap(), b"existing report"); + } else { + assert!(!report.exists()); + } + } + } + } +} + #[test] fn cli_loads_both_design_encodings_by_contents() { let dir = Scratch::new(); diff --git a/docs/perf.md b/docs/perf.md index cd6f35c9a..6f36e29cb 100644 --- a/docs/perf.md +++ b/docs/perf.md @@ -727,7 +727,7 @@ apart from the per-feature milliseconds. The kernel's test suite passes, including four new tests for the prism route and its identity rule. Reproduce after compiling a release build, without other builds running: time -`target/release/solidcraft-cli eval --quiet` with `/usr/bin/time -v`, alternating the +`target/release/solidcraft-cli eval ` with `/usr/bin/time -v`, alternating the baseline and patched binaries, and compare the middle of the runs. The corpus (20 generated designs: hole plates, box grids, assemblies, many-sketch parts) and the measuring scripts are in https://github.com/sahenjp/solidcraft-perf under `perf/`.