Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions crates/forma-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -189,15 +189,15 @@ 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" => {
if i + 1 >= args.len() {
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" => {
Expand Down Expand Up @@ -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<PathBuf>, 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));
}
31 changes: 31 additions & 0 deletions crates/forma-cli/tests/cli_exit_codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<&str>> = 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);
}
Loading