diff --git a/src/util.rs b/src/util.rs index 1c34949..980b5bb 100644 --- a/src/util.rs +++ b/src/util.rs @@ -95,6 +95,30 @@ pub fn get_git_user() -> Option { } }) } +/// The candidate list for an ambiguous search, as an error rather than a print +/// followed by `exit(1)`. +/// +/// An error reaches the user on stderr and leaves the exit code non-zero, which +/// is the only combination the opencode wrapper can relay: it throws on the exit +/// code and keeps stderr, so the list printed to stdout was thrown away and the +/// agent saw nothing but a bare exit code. +fn ambiguous(search: &str, matches: &[(String, String, String, String)]) -> anyhow::Error { + let mut message = format!("Multiple tickets found matching '{}':\n", search); + + for (i, (status, title, _, id)) in matches.iter().enumerate() { + message.push_str(&format!( + " {}. ({}) {} [{}]\n", + i + 1, + color_text(short_id(id), "red"), + title, + color_text(status, "orange"), + )); + } + + message.push_str("\nUse the displayed ID or a more specific search term to select."); + anyhow!(message) +} + pub fn resolve_ticket(search: &str, worklane_dir: &Path) -> Result<(String, String, String)> { let statuses = status_dirs(); let mut matches: Vec<(String, String, String, String)> = Vec::new(); @@ -146,18 +170,7 @@ pub fn resolve_ticket(search: &str, worklane_dir: &Path) -> Result<(String, Stri } if matches.len() > 1 { - println!("Multiple tickets found matching '{}':", search); - for (i, (status, title, _, id)) in matches.iter().enumerate() { - println!( - " {}. ({}) {} [{}]", - i + 1, - color_text(short_id(id), "red"), - title, - color_text(status, "orange"), - ); - } - println!("\nUse the displayed ID or a more specific search term to select."); - std::process::exit(1); + return Err(ambiguous(search, &matches)); } let (status, title, content, _short_id) = matches.remove(0); diff --git a/tests/cmd_show.rs b/tests/cmd_show.rs index 6d3cc69..231efbc 100644 --- a/tests/cmd_show.rs +++ b/tests/cmd_show.rs @@ -64,6 +64,11 @@ fn get_stdout(cmd: &mut Command) -> String { String::from_utf8(assert.get_output().stdout.clone()).unwrap() } +fn get_stderr(cmd: &mut Command) -> String { + let assert = cmd.assert(); + String::from_utf8(assert.get_output().stderr.clone()).unwrap() +} + #[test] fn show_displays_ticket_by_full_uuid() { let temp = TempDir::new().unwrap(); @@ -256,13 +261,57 @@ fn show_asks_for_disambiguation_when_multiple_matches() { let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(&temp.path()).arg("show").arg("est"); + let stderr = get_stderr(&mut cmd); + assert!(stderr.contains("Multiple tickets found")); + assert!(stderr.contains("Test Ticket A")); + assert!(stderr.contains("Test Ticket B")); + assert!(stderr.contains("abc123d")); + assert!(stderr.contains("fedcba0")); + assert!(stderr.contains("Use the displayed ID")); +} + +/// The candidate list has to travel the same way every other error does, or the +/// opencode wrapper throws on the exit code and discards it. +#[test] +fn show_reports_ambiguity_on_stderr_with_a_failing_exit() { + let temp = TempDir::new().unwrap(); + let worklane = setup_worklane(&temp); + + create_ticket( + &worklane, + "backlog", + "Ambiguous One", + "First.", + "abc123def456789012345678901234567890", + "alice", + "2026-12-31", + "test", + ); + create_ticket( + &worklane, + "backlog", + "Ambiguous Two", + "Second.", + "fedcba0987654321fedcba09876543210987654321", + "bob", + "2026-10-15", + "test", + ); + + let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); + cmd.current_dir(&temp.path()) + .arg("show") + .arg("ambiguous") + .assert() + .failure(); + + let stderr = get_stderr(&mut cmd); + assert!(stderr.contains("Ambiguous One")); + assert!(stderr.contains("Ambiguous Two")); + + // Nothing on stdout: that is where it used to go, and where it was lost let stdout = get_stdout(&mut cmd); - assert!(stdout.contains("Multiple tickets found")); - assert!(stdout.contains("Test Ticket A")); - assert!(stdout.contains("Test Ticket B")); - assert!(stdout.contains("abc123d")); - assert!(stdout.contains("fedcba0")); - assert!(stdout.contains("Use the displayed ID")); + assert!(stdout.is_empty(), "stdout was {stdout:?}"); } /// Filenames shorter than the displayed id width used to panic while printing @@ -286,10 +335,10 @@ fn show_disambiguates_tickets_with_short_filenames() { let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(&temp.path()).arg("show").arg("short"); - let stdout = get_stdout(&mut cmd); - assert!(stdout.contains("Multiple tickets found")); - assert!(stdout.contains("Short One")); - assert!(stdout.contains("Short Two")); + let stderr = get_stderr(&mut cmd); + assert!(stderr.contains("Multiple tickets found")); + assert!(stderr.contains("Short One")); + assert!(stderr.contains("Short Two")); } /// A multibyte character straddling the cut used to panic. @@ -309,10 +358,10 @@ fn show_disambiguates_tickets_with_non_ascii_filenames() { let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); cmd.current_dir(&temp.path()).arg("show").arg("accented"); - let stdout = get_stdout(&mut cmd); - assert!(stdout.contains("Multiple tickets found")); - assert!(stdout.contains("Accented One")); - assert!(stdout.contains("Accented Two")); + let stderr = get_stderr(&mut cmd); + assert!(stderr.contains("Multiple tickets found")); + assert!(stderr.contains("Accented One")); + assert!(stderr.contains("Accented Two")); } #[test]