diff --git a/Cargo.lock b/Cargo.lock index eebb598..58b5a5c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1503,7 +1503,7 @@ dependencies = [ [[package]] name = "worklane" -version = "0.32.10" +version = "0.32.11" dependencies = [ "anyhow", "assert_cmd", diff --git a/Cargo.toml b/Cargo.toml index 3b66233..3617a61 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "worklane" -version = "0.32.10" +version = "0.32.11" edition = "2024" [dependencies] diff --git a/src/handlers.rs b/src/handlers.rs index ddf0197..eefc7fc 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -344,10 +344,10 @@ pub fn cmd_add(args: &AddArgs, start: &Path) -> Result<()> { // format allows: duplicate titles are supported and `add_allows_duplicate_titles` // pins it. `cmd_campaign_add` deliberately does not carry it either. What is left is // the write's own failure, named with the ticket and the path the way the other - // writes in the tree name them. - fs::write(&filepath, body).map_err(|e| { - anyhow!("Could not write ticket '{}' at {}: {}", title, filepath.display(), e) - })?; + // writes in the tree name them. Staged like them too: there is no existing content + // to corrupt, but a partial file in backlog is a ticket the listing would read, so + // the same writer as every edit is the one that makes the new file appear whole. + write_then_replace(&worklane_dir, &filepath, &filename, &body, TICKET, title)?; println!("Created ticket: {}", filename); Ok(()) diff --git a/tests/cmd_add.rs b/tests/cmd_add.rs index 965747e..1106cea 100644 --- a/tests/cmd_add.rs +++ b/tests/cmd_add.rs @@ -574,6 +574,12 @@ fn chmod(path: &Path, mode: u32) { /// itself, and it is named with the ticket and the path rather than raised as a bare io /// error. The branch this replaces could not fire at all: the filename carries a fresh /// uuid, and its message claimed the title was taken. +/// +/// The add stages through `write_then_replace` like every other write, so the temp +/// lives under `.worklane/`, outside the directory the chmod keeps closed, and the +/// rename is the step that fails here. Both halves have to be gone afterwards: no half +/// ticket in backlog, and no `.wl-*.tmp` beside it, which is what `stray_files` checks +/// for a move. #[test] fn add_names_the_ticket_and_the_path_when_the_write_fails() { let temp = TempDir::new().unwrap(); @@ -596,6 +602,14 @@ fn add_names_the_ticket_and_the_path_when_the_write_fails() { chmod(&backlog, 0o755); assert_eq!(fs::read_dir(&backlog).unwrap().count(), 0); + + let strays: Vec = fs::read_dir(&worklane) + .unwrap() + .filter_map(|e| e.ok()) + .map(|e| e.file_name().into_string().unwrap()) + .filter(|name| name.starts_with(".wl-")) + .collect(); + assert_eq!(strays, Vec::::new(), "the staging file was left behind"); } /// The board above the project is real and empty, and stays empty: a dangling