From fc2cfaf1b1a06035b2f71b701c0172842766360a Mon Sep 17 00:00:00 2001 From: Faris Basha TM <72191505+farisbasha@users.noreply.github.com> Date: Fri, 4 Dec 2026 12:27:33 +0530 Subject: [PATCH] fix(cli): reject repeated --out/--render/--draw instead of dropping paths A second --out (or --render/--draw) replaced the first one, so `forma eval --out a.stl --out b.glb` wrote only b.glb and said nothing. Exit 2 with both paths instead, like an extra input file already does. --- crates/forma-cli/src/main.rs | 23 ++++++++++++++---- crates/forma-cli/tests/cli_exit_codes.rs | 31 ++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/crates/forma-cli/src/main.rs b/crates/forma-cli/src/main.rs index b6ac599..ccbded6 100644 --- a/crates/forma-cli/src/main.rs +++ b/crates/forma-cli/src/main.rs @@ -176,11 +176,11 @@ fn main() { process::exit(2); } if is_render_cmd { - render_path = Some(PathBuf::from(&args[i + 1])); + set_path_once(&mut render_path, "--out", &args[i + 1]); } else if is_draw_cmd { - draw_path = Some(PathBuf::from(&args[i + 1])); + set_path_once(&mut draw_path, "--out", &args[i + 1]); } else { - out_path = Some(PathBuf::from(&args[i + 1])); + set_path_once(&mut out_path, "--out", &args[i + 1]); } i += 2; } @@ -189,7 +189,7 @@ fn main() { eprintln!("ERR: --render requires PNG path"); process::exit(2); } - render_path = Some(PathBuf::from(&args[i + 1])); + set_path_once(&mut render_path, "--render", &args[i + 1]); i += 2; } "--draw" => { @@ -197,7 +197,7 @@ fn main() { eprintln!("ERR: --draw requires SVG path"); process::exit(2); } - draw_path = Some(PathBuf::from(&args[i + 1])); + set_path_once(&mut draw_path, "--draw", &args[i + 1]); i += 2; } "--size" => { @@ -389,3 +389,16 @@ fn main() { } } } + +/// Store an output path, rejecting a second one: each output is written to a +/// single file, so a repeated flag would otherwise drop the earlier path silently. +fn set_path_once(slot: &mut Option, flag: &str, value: &str) { + if let Some(prev) = slot { + eprintln!( + "ERR: {flag} given more than once ('{}' and '{value}'); each output takes one path", + prev.display() + ); + process::exit(2); + } + *slot = Some(PathBuf::from(value)); +} diff --git a/crates/forma-cli/tests/cli_exit_codes.rs b/crates/forma-cli/tests/cli_exit_codes.rs index fcbe902..fce52c5 100644 --- a/crates/forma-cli/tests/cli_exit_codes.rs +++ b/crates/forma-cli/tests/cli_exit_codes.rs @@ -57,3 +57,34 @@ fn issue_84_strict_invariant_failure_exits_with_code_3() { assert_eq!(code_ok, 0); assert!(stdout_ok.contains("WT=ok")); } + +#[test] +fn repeated_output_flags_are_rejected_instead_of_dropped() { + let dir = std::env::temp_dir().join(format!("forma_repeat_out_{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let p = |name: &str| dir.join(name).to_string_lossy().into_owned(); + let (a, b) = (p("a.stl"), p("b.glb")); + let cases: Vec> = vec![ + vec!["eval", "--out", &a, "--out", &b, "-"], + vec!["eval", "-o", &a, "--out", &b, "-"], + vec!["eval", "--render", &a, "--render", &b, "-"], + vec!["eval", "--draw", &a, "--draw", &b, "-"], + vec!["render", "--out", &a, "--out", &b, "-"], + ]; + for args in cases { + let (code, _, stderr) = run_forma(&args, "a = box(1, 1, 1)\n"); + assert_eq!(code, 2, "{args:?}: stderr={stderr}"); + assert!(stderr.contains("given more than once"), "{args:?}: stderr={stderr}"); + } + // Nothing was written, not even the first path. + assert!(!dir.join("a.stl").exists() && !dir.join("b.glb").exists()); + + // One of each output flag still works together. + let (code, _, stderr) = run_forma( + &["eval", "--out", &p("ok.stl"), "--render", &p("ok.png"), "--draw", &p("ok.svg"), "-"], + "a = box(1, 1, 1)\n", + ); + assert_eq!(code, 0, "stderr={stderr}"); + assert!(dir.join("ok.stl").exists() && dir.join("ok.png").exists() && dir.join("ok.svg").exists()); + let _ = std::fs::remove_dir_all(&dir); +}