diff --git a/changelog.d/+watch-summary-write-error.bugfix.md b/changelog.d/+watch-summary-write-error.bugfix.md new file mode 100644 index 0000000..2cb6a08 --- /dev/null +++ b/changelog.d/+watch-summary-write-error.bugfix.md @@ -0,0 +1 @@ +Write `--result` when `watch` cannot write the run summary, and exit 1. diff --git a/src/main.rs b/src/main.rs index 0c23bb4..1c1520b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1654,6 +1654,7 @@ fn cmd_watch(w: &WatchArgs) -> Result { ), } } + let mut output_errors = Vec::new(); if let Err(f) = write_summary_with_pending_signal( &s, &mut res, @@ -1662,10 +1663,18 @@ fn cmd_watch(w: &WatchArgs) -> Result { Some(to_sorted(&doc)), &[], ) { - return Err(signal_outranks(res.exit_code, f)); + output_errors.push(f.msg); } if let Some(p) = w.result { - write_json(&to_sorted(&res), Some(p)).map_err(|f| signal_outranks(res.exit_code, f))?; + if let Err(f) = write_json(&to_sorted(&res), Some(p)) { + output_errors.push(f.msg); + } + } + if !output_errors.is_empty() { + return Err(signal_outranks( + res.exit_code, + Fail::new(exit::FAILED, output_errors.join("; ")), + )); } Ok(res.exit_code) } diff --git a/tests/watch.rs b/tests/watch.rs index b5799e9..6157df9 100644 --- a/tests/watch.rs +++ b/tests/watch.rs @@ -7,6 +7,7 @@ mod common; use common::*; use serde_json::Value; +use std::os::unix::fs::PermissionsExt; use std::process::{Command, Output, Stdio}; use std::time::{Duration, Instant}; @@ -214,6 +215,37 @@ fn a_signal_while_watch_waits_for_the_summary_lock_preserves_signal_exit() { assert_eq!(result["exit_code"], 143, "{result}"); } +#[test] +fn an_unwritable_run_summary_still_writes_the_result() { + let t = built(""); + change(&t); + let run_dir = t.run_dir(LATER); + let log_dir = run_dir.join("cpu"); + std::fs::create_dir_all(&log_dir).unwrap(); + // The summary lock is a file this directory already holds. With the + // directory unwritable, the summary file is what cannot be created. + std::fs::write(run_dir.join(".summary.lock"), "").unwrap(); + std::fs::set_permissions(&run_dir, std::fs::Permissions::from_mode(0o555)).unwrap(); + let result = t.root().join("watch-result.json"); + let result_arg = result.display().to_string(); + let vars = eessi_vars(&t, "2023.06"); + let output = watch(&t, LATER, &["--result", &result_arg], &with(&vars)); + std::fs::set_permissions(&run_dir, std::fs::Permissions::from_mode(0o755)).unwrap(); + + let err = stderr(&output); + assert_eq!(code(&output), 1, "{err}"); + assert!(err.contains("cannot write run summary:"), "{err}"); + let summary = run_dir.join("summary.json"); + assert!(err.contains(&summary.display().to_string()), "{err}"); + assert!(!summary.exists()); + let text = + std::fs::read_to_string(&result).unwrap_or_else(|error| panic!("{result:?}: {error}")); + let written: Value = serde_json::from_str(&text).unwrap(); + assert_eq!(written["kind"], "result", "{written}"); + assert_eq!(written["target"], "cpu", "{written}"); + assert_eq!(written["exit_code"], 0, "{written}"); +} + const LATER: &str = "20260102_000000"; const PLAN: &str = "\