diff --git a/Cargo.lock b/Cargo.lock index d3506f2..e98ca8f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1503,7 +1503,7 @@ dependencies = [ [[package]] name = "worklane" -version = "0.17.1" +version = "0.17.2" dependencies = [ "anyhow", "assert_cmd", diff --git a/Cargo.toml b/Cargo.toml index c9f2b3c..e4d6759 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "worklane" -version = "0.17.1" +version = "0.17.2" edition = "2024" [dependencies] diff --git a/src/handlers.rs b/src/handlers.rs index 0ce011a..0f6f276 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -256,10 +256,6 @@ pub fn cmd_add(args: &AddArgs, start: &Path) -> Result<()> { let filename = format!("{}_{}.md", id, slug); let filepath = backlog_dir.join(&filename); - if filepath.exists() { - return Err(anyhow!("A ticket with this title already exists.")); - } - // Joined with a single newline, so an empty value is how a caller spells a // paragraph break and an all-blank description is no description at all. let description = trim_blank_ends(description); @@ -327,7 +323,21 @@ pub fn cmd_add(args: &AddArgs, start: &Path) -> Result<()> { ); body.push('\n'); - fs::write(&filepath, body)?; + // No existence check ahead of this. The name carries a uuid generated a few lines + // up, so a collision is not something that happens, and the branch that used to + // guard the write claimed the *title* was taken, which is the opposite of what the + // 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 + ) + })?; println!("Created ticket: {}", filename); Ok(()) diff --git a/tests/cmd_add.rs b/tests/cmd_add.rs index d2bbc04..30731cd 100644 --- a/tests/cmd_add.rs +++ b/tests/cmd_add.rs @@ -561,3 +561,39 @@ fn read_only_ticket(dir: &Path) -> String { fs::read_to_string(file.path()).unwrap() } + +fn chmod(path: &Path, mode: u32) { + use std::os::unix::fs::PermissionsExt; + + let mut perms = fs::metadata(path).unwrap().permissions(); + perms.set_mode(mode); + fs::set_permissions(path, perms).unwrap(); +} + +/// A duplicate title is not a failure, so the only thing left to refuse is the write +/// 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. +#[test] +fn add_names_the_ticket_and_the_path_when_the_write_fails() { + let temp = TempDir::new().unwrap(); + let worklane = setup_worklane(&temp); + let backlog = worklane.join("backlog"); + + chmod(&backlog, 0o555); + + let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); + let assert = cmd + .current_dir(temp.path()) + .args(["add", "--title", "Unwritable"]) + .assert() + .failure(); + + let stderr = String::from_utf8(assert.get_output().stderr.clone()).unwrap(); + assert!(stderr.contains("Unwritable"), "{stderr}"); + assert!(stderr.contains("unwritable.md"), "{stderr}"); + assert!(!stderr.contains("already exists"), "{stderr}"); + + chmod(&backlog, 0o755); + assert_eq!(fs::read_dir(&backlog).unwrap().count(), 0); +}