cli: report failures after keep-going scripts (#2836)

* cli: report failures after keep-going scripts

Keep executing requested steps while retaining the first failure in Report, so failed scripts return nonzero before save/export.

* docs: describe first script failure in Report

Describe the first failing step without assuming all callers stop immediately.

* test: cover keep-going failure reporting and output guards

Exercise failures before and after a successful step, preserve the first failure, and require both output paths to remain absent.
This commit is contained in:
Mex authored and GitHub committed 2026-11-15 14:59:06 +00:00
1 parent ec1cc4d37a
commit ca463bab52
3 files changed
+47 -5

No files matched your search

+7 -3
View File
@@ -692,11 +692,15 @@ fn script(args: &[String]) -> Result<(), String> {
let report = if keep_going {
// Run every step; failures become {"error": …} results.
let mut results: Vec<Value> = Vec::new();
for st in &steps {
let mut failed = None;
for (i, st) in steps.iter().enumerate() {
let r = designcraft_engine::script::resolve(&st.params, &results).and_then(|p| exec(b, &st.command, p));
results.push(r.unwrap_or_else(|e| json!({"error": e, "command": st.command})));
results.push(r.unwrap_or_else(|e| {
failed.get_or_insert_with(|| (i, st.command.clone(), e.clone()));
json!({"error": e, "command": st.command})
}));
}
designcraft_engine::script::Report { results, failed: None }
designcraft_engine::script::Report { results, failed }
} else {
designcraft_engine::script::run(&steps, |id, p| exec(b, id, p))
};
+39 -1
View File
@@ -151,11 +151,49 @@ fn script_export_of_the_document_in_the_running_app_saves_a_copy() {
#[test]
fn failing_script_exits_nonzero_with_the_step() {
let mut child = cli().args(["script", "-"]).stdin(Stdio::piped()).stdout(Stdio::piped()).stderr(Stdio::piped()).spawn().unwrap();
child.stdin.take().unwrap().write_all(b"document.inspect\nno.such.command\n").unwrap();
child.stdin.take().unwrap().write_all(b"document.inspect\nno.such.command\ndocument.inspect\n").unwrap();
let out = child.wait_with_output().unwrap();
assert!(!out.status.success());
let v: serde_json::Value = serde_json::from_slice(&out.stdout).unwrap();
assert_eq!(v["failedIndex"], 1);
assert_eq!(v["completed"], 1);
}
#[test]
fn keep_going_reports_the_first_failure_and_does_not_write_outputs() {
struct Outputs([std::path::PathBuf; 2]);
impl Drop for Outputs {
fn drop(&mut self) {
for path in &self.0 {
let _ = std::fs::remove_file(path);
}
}
}
let root = std::env::temp_dir();
let name = format!("dc-cli-keep-going-{}", std::process::id());
let paths = [root.join(format!("{name}.designcraft")), root.join(format!("{name}.png"))];
assert!(paths.iter().all(|path| !path.exists()));
let outputs = Outputs(paths);
let mut child = cli()
.args(["script", "-", "--keep-going", "--save", outputs.0[0].to_str().unwrap(), "--export", outputs.0[1].to_str().unwrap()])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.unwrap();
child.stdin.take().unwrap().write_all(b"no.such.command\ndocument.inspect\nno.other.command\n").unwrap();
let out = child.wait_with_output().unwrap();
assert!(!out.status.success());
let v: serde_json::Value = serde_json::from_slice(&out.stdout).unwrap();
assert_eq!(v["completed"], 3);
assert_eq!(v["failedIndex"], 0);
assert_eq!(v["failedCommand"], "no.such.command");
assert!(v["results"][0]["error"].is_string());
assert!(v["results"][1].is_object() && v["results"][1]["error"].is_null());
assert!(v["results"][2]["error"].is_string());
assert!(outputs.0.iter().all(|path| !path.exists()), "--save and --export require a successful script");
}
#[test]
+1 -1
View File
@@ -209,7 +209,7 @@ fn resolve_in(params: &Value, results: &[Value], max_ref: usize, budget: &mut us
#[derive(Clone, Debug, PartialEq)]
pub struct Report {
pub results: Vec<Value>,
/// The failing step (index, command, message); later steps didn't run.
/// The first failing step (index, command, message), if any.
pub failed: Option<(usize, String, String)>,
}