diff --git a/apps/solidcraft-cli/src/main.rs b/apps/solidcraft-cli/src/main.rs index 26314b661..0558aeefe 100644 --- a/apps/solidcraft-cli/src/main.rs +++ b/apps/solidcraft-cli/src/main.rs @@ -2,7 +2,7 @@ //! //! ```text //! solidcraft-cli run [--in design|part.step|mesh.3mf] [--out FILE]... [--save FILE] [--quiet] [--results] [--timings] [--profile] [--json-out FILE] [--allow-errors] -//! solidcraft-cli eval [--json-out FILE] (alias: inspect) measurements as JSON +//! solidcraft-cli eval [--in design.solidcraft|part.step] [--json-out FILE] (alias: inspect) measurements as JSON //! solidcraft-cli script [--out script.json] the design's history as a command script //! solidcraft-cli bench-open [--parse-only] [--save OUT.solidcraft] [--display LEVEL [--edit NAME EXPR]] [--ops script.json] loading timings as JSON lines //! solidcraft-cli bench-agent [--scale F] [--save F] [--script-out F] a ~1,300-body design built command by command, timed @@ -58,7 +58,7 @@ struct Command { /// `run`'s lines in the usage. const USAGE_RUN: &str = " solidcraft-cli run [--in design.solidcraft|part.step] [--out FILE]... [--save FILE] [--quiet] [--results] [--timings] [--profile] [--json-out FILE] [--allow-errors]\n solidcraft-cli run --in [--out FILE]...\n (a timeline feature that fails fails the run and nothing is written; --allow-errors writes anyway)\n (--timings prints {timings: [{index, command, ms}], total_ms} for a script, with --results its results too;\n --profile adds each command's profile: {boolean_ms, tessellate_ms, other_ms}, the kernel's share)\n"; /// `eval`'s lines in the usage. -const USAGE_EVAL: &str = " solidcraft-cli eval [--json-out FILE] (alias: inspect; or --in FILE)\n (--json-out FILE writes the JSON that run (with --results, else the measurement) and eval print to FILE instead of stdout,\n as plain UTF-8: a shell's > can re-encode it, e.g. Windows PowerShell adds a byte-order mark)\n"; +const USAGE_EVAL: &str = " solidcraft-cli eval [--in design.solidcraft|part.step] [--json-out FILE] (alias: inspect)\n solidcraft-cli eval --in [--json-out FILE]\n (with both inputs, runs the positional script on --in before measuring; does not automatically save the modified design)\n (--json-out FILE writes the JSON that run (with --results, else the measurement) and eval print to FILE instead of stdout,\n as plain UTF-8: a shell's > can re-encode it, e.g. Windows PowerShell adds a byte-order mark)\n"; /// `script`'s lines in the usage. const USAGE_SCRIPT: &str = " solidcraft-cli script [--out script.json]\n (the design's history as a command script that eval and run replay)\n"; /// `bench-open`'s lines in the usage. @@ -392,7 +392,7 @@ fn cmd_run(args: &[String]) -> Result<(), String> { Ok(()) } -/// The session `run` and `snapshot` start from: `--in` (a design or STEP file) with the +/// The session `run`, `eval` and `snapshot` start from: `--in` (a design or STEP file) with the /// positional script run on top of it, or either one alone. fn open(base: Option<&str>, path: Option<&String>) -> Result { match (base, path) { @@ -445,8 +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 mut s = open(flag(args, "--in"), positional(args))?; let m = s.execute("inspect.measure", &json!({})).map_err(|e| e.to_string())?; let d = s.execute("document.inspect", &json!({})).map_err(|e| e.to_string())?; let out = json!({ diff --git a/apps/solidcraft-cli/tests/json_out.rs b/apps/solidcraft-cli/tests/json_out.rs index 7be69744a..a56f6e5e0 100644 --- a/apps/solidcraft-cli/tests/json_out.rs +++ b/apps/solidcraft-cli/tests/json_out.rs @@ -139,11 +139,13 @@ fn eval_json_out_holds_what_stdout_would() { fn json_out_needs_a_file_and_fails_when_it_cannot_write() { let dir = Scratch::new(); let script = dir.script(); - for cmd in ["run", "eval"] { - let output = cli().arg(cmd).arg(&script).arg("--json-out").output().unwrap(); - assert_eq!(output.status.code(), Some(1), "{cmd}"); - assert!(String::from_utf8_lossy(&output.stderr).contains("--json-out needs a FILE"), "{cmd}"); - assert!(output.stdout.is_empty(), "{cmd}"); + for cmd in ["run", "eval", "inspect"] { + for trailing in [&[][..], &["--in"][..]] { + let output = cli().arg(cmd).arg(&script).arg("--json-out").args(trailing).output().unwrap(); + assert_eq!(output.status.code(), Some(1), "{cmd}"); + assert!(String::from_utf8_lossy(&output.stderr).contains("--json-out needs a FILE"), "{cmd}"); + assert!(output.stdout.is_empty(), "{cmd}"); + } let missing = dir.0.join("missing").join("out.json"); let output = cli().arg(cmd).arg(&script).arg("--json-out").arg(&missing).output().unwrap(); diff --git a/apps/solidcraft-cli/tests/run_results.rs b/apps/solidcraft-cli/tests/run_results.rs index 978a7b86c..386aada99 100644 --- a/apps/solidcraft-cli/tests/run_results.rs +++ b/apps/solidcraft-cli/tests/run_results.rs @@ -267,6 +267,110 @@ fn results_with_base_design_and_quiet_still_report_and_save() { assert_eq!(body_count(&base), 1); } +/// `eval` and its alias must measure the script applied to the input design, rather than +/// silently reporting the input alone (#6861). +#[test] +fn eval_runs_its_script_on_the_in_design() { + let dir = Scratch::new(); + let base = dir.0.join("base.solidcraft"); + let script = dir.0.join("add.json"); + json_output(cli().arg("run").arg(dir.script()).arg("--save").arg(&base).output().unwrap()); + let original = std::fs::read(&base).unwrap(); + std::fs::write( + &script, + json!({"commands": [{"command": "solid.box", "params": {"length": 5, "width": 3, "height": 4, "corner": [10, 0, 0]}}]}).to_string(), + ) + .unwrap(); + for command in ["eval", "inspect"] { + // Both existing one-input forms still measure just the original body. + for input in [&base, &dir.script()] { + for named_input in [false, true] { + let mut c = cli(); + c.arg(command); + if named_input { + c.arg("--in"); + } + let measured = json_output(c.arg(input).output().unwrap()); + assert_eq!(measured["body_count"], 1); + assert!((measured["total"]["volume_mm3"].as_f64().unwrap() - 24.0).abs() < 1e-6); + } + } + for input_first in [false, true] { + for to_file in [false, true] { + let mut c = cli(); + c.arg(command); + if input_first { + c.arg("--in").arg(&base).arg(&script); + } else { + c.arg(&script).arg("--in").arg(&base); + } + let report = dir.0.join("measurement.json"); + if to_file { + c.arg("--json-out").arg(&report); + } + let output = c.output().unwrap(); + let measured = if to_file { + assert!(output.status.success(), "{}", String::from_utf8_lossy(&output.stderr)); + assert!(output.stdout.is_empty()); + serde_json::from_slice(&std::fs::read(&report).unwrap()).unwrap() + } else { + json_output(output) + }; + assert_eq!(measured["body_count"], 2, "{command}, input_first={input_first}, to_file={to_file}"); + assert!((measured["total"]["volume_mm3"].as_f64().unwrap() - 84.0).abs() < 1e-6); + assert_eq!(measured["timeline"].as_array().unwrap().len(), 2, "the script runs exactly once"); + assert_eq!(std::fs::read(&base).unwrap(), original, "evaluation never saves its changes to the input"); + } + } + } +} + +#[test] +fn eval_propagates_script_errors_before_writing_a_report() { + let dir = Scratch::new(); + let base = dir.script(); + let script = dir.0.join("bad.json"); + for command in ["eval", "inspect"] { + for contents in ["{", r#"{"commands":[{"command":"missing.command","params":{}}]}"#] { + std::fs::write(&script, contents).unwrap(); + let report = dir.0.join("report.json"); + let output = cli().arg(command).arg(&script).arg("--in").arg(&base).arg("--json-out").arg(&report).output().unwrap(); + assert_eq!(output.status.code(), Some(1)); + assert!(!output.stderr.is_empty()); + assert!(output.stdout.is_empty()); + assert!(!report.exists()); + } + } +} + +#[test] +fn eval_rejects_unknown_options_and_missing_input_values_before_writing_a_report() { + let dir = Scratch::new(); + let script = dir.script(); + let report = dir.0.join("report.json"); + for command in ["eval", "inspect"] { + for (options, message) in [(&["--frobnicate", "1"][..], "doesn't take --frobnicate"), (&["--in"][..], "--in needs a value")] { + for options_first in [false, true] { + std::fs::write(&report, b"previous report").unwrap(); + let mut c = cli(); + c.arg(command); + if options_first { + c.args(options).arg("--json-out").arg(&report).arg(&script); + } else { + c.arg(&script).args(options).arg("--json-out").arg(&report); + } + // A following option also proves that --in cannot consume a flag as its value. + let output = c.output().unwrap(); + assert_eq!(output.status.code(), Some(1), "{command}, {options:?}"); + let error = String::from_utf8_lossy(&output.stderr); + assert!(error.contains(message), "{command}: {error}"); + assert!(output.stdout.is_empty()); + assert_eq!(std::fs::read(&report).unwrap(), b"previous report"); + } + } + } +} + #[test] fn results_propagate_output_errors() { let dir = Scratch::new();