Rust sets SIGPIPE to SIG_IGN at startup, so a write to a closed pipe returned EPIPE and println panicked on it. `worklane ls | head -3` printed a panic and a backtrace on stderr instead of three lines, which reads as a real fault and is not what any other tool in a pipeline does. main now restores the default disposition before any output, the same six lines frig carries for the same reason. The cost is that a truncated pipe kills the process with signal 13, so a shell reports 141 rather than 101. That is what head and sort do, and it is now in the README next to the other exit codes. The tests drop the read end rather than racing head for it, over a listing large enough to exceed the 64 KiB a pipe holds, since a smaller one lands entirely in the buffer and proves nothing. A shell pipeline into head is covered too, and the exit codes for a real error and a usage mistake are pinned so the reset cannot quietly change them. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
174 lines
5.7 KiB
Rust
174 lines
5.7 KiB
Rust
use assert_fs::TempDir;
|
|
use assert_fs::fixture::{PathChild, PathCreateDir};
|
|
use std::fs;
|
|
use std::os::unix::process::ExitStatusExt;
|
|
use std::process::{Command, Stdio};
|
|
|
|
const BIN_NAME: &str = env!("CARGO_BIN_EXE_worklane");
|
|
|
|
/// What a truncated pipe costs: the process dies by signal rather than returning a
|
|
/// code, so a shell reports 128 + 13. Asserted here so the README and the code stay
|
|
/// honest about what `pipefail` sees.
|
|
const SIGPIPE: i32 = 13;
|
|
|
|
/// A pipe holds 64 KiB on Linux before a write blocks. Below that the whole listing
|
|
/// lands in the buffer, the writer never notices the reader is gone, and a test
|
|
/// proves nothing. Every ticket here is one `ls` row of roughly a hundred bytes.
|
|
const TICKETS: usize = 1200;
|
|
|
|
fn setup_crowded_board(temp: &TempDir) -> std::path::PathBuf {
|
|
let worklane = temp.child(".worklane");
|
|
worklane.create_dir_all().unwrap();
|
|
|
|
for dir in ["backlog", "committed", "reviewing", "done"] {
|
|
worklane.child(dir).create_dir_all().unwrap();
|
|
}
|
|
|
|
let backlog = worklane.path().join("backlog");
|
|
for i in 0..TICKETS {
|
|
let id = format!("{:08x}-0000-4000-8000-000000000000", i);
|
|
let title = format!("A ticket with a title long enough to fill a row {}", i);
|
|
fs::write(
|
|
backlog.join(format!("{}_ticket-{}.md", id, i)),
|
|
format!(
|
|
"# {}\n\n## Metadata\n\n\
|
|
- **ID**: {}\n\
|
|
- **Owner**: someone with a name\n\
|
|
- **Status**: backlog\n",
|
|
title, id
|
|
),
|
|
)
|
|
.unwrap();
|
|
}
|
|
|
|
temp.path().to_path_buf()
|
|
}
|
|
|
|
fn panic_text(stderr: &str) -> bool {
|
|
stderr.contains("panicked")
|
|
|| stderr.contains("failed printing to stdout")
|
|
|| stderr.contains("Broken pipe")
|
|
}
|
|
|
|
/// The reader closed before the writer finished: dropping the read end here is the
|
|
/// same thing `head` does when it has seen enough, without the timing luck.
|
|
fn run_with_stdout_dropped(dir: &std::path::Path, args: &[&str]) -> std::process::Output {
|
|
let mut child = Command::new(BIN_NAME)
|
|
.args(args)
|
|
.current_dir(dir)
|
|
.stdout(Stdio::piped())
|
|
.stderr(Stdio::piped())
|
|
.spawn()
|
|
.unwrap();
|
|
|
|
drop(child.stdout.take().unwrap());
|
|
child.wait_with_output().unwrap()
|
|
}
|
|
|
|
#[test]
|
|
fn ls_says_nothing_when_the_reader_goes_away() {
|
|
let temp = TempDir::new().unwrap();
|
|
let dir = setup_crowded_board(&temp);
|
|
|
|
let output = run_with_stdout_dropped(&dir, &["ls", "backlog"]);
|
|
let stderr = String::from_utf8_lossy(&output.stderr);
|
|
|
|
assert!(!panic_text(&stderr), "stderr was {stderr:?}");
|
|
assert_ne!(output.status.code(), Some(101), "panicked instead of dying");
|
|
assert_eq!(
|
|
output.status.signal(),
|
|
Some(SIGPIPE),
|
|
"expected death by SIGPIPE, got {:?}",
|
|
output.status
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn show_says_nothing_when_the_reader_goes_away() {
|
|
let temp = TempDir::new().unwrap();
|
|
let dir = setup_crowded_board(&temp);
|
|
|
|
// A ticket carrying more comments than a pipe will hold, since the comment list
|
|
// is the longest thing show prints and the last thing it reaches.
|
|
let comments = temp
|
|
.path()
|
|
.join(".worklane")
|
|
.join("comments")
|
|
.join("00000000-0000-4000-8000-000000000000");
|
|
fs::create_dir_all(&comments).unwrap();
|
|
for i in 0..800 {
|
|
fs::write(
|
|
comments.join(format!("2026082{}_120000_{:06}.md", i % 10, i)),
|
|
format!(
|
|
"**Comment**\nA comment body long enough to be worth buffering, number {}.\n\n\
|
|
**Author**: someone\n**Time**: 2026-08-20 12:00:00\n",
|
|
i
|
|
),
|
|
)
|
|
.unwrap();
|
|
}
|
|
|
|
let output = run_with_stdout_dropped(&dir, &["show", "00000000-0000-4000"]);
|
|
let stderr = String::from_utf8_lossy(&output.stderr);
|
|
|
|
assert!(!panic_text(&stderr), "stderr was {stderr:?}");
|
|
assert_ne!(output.status.code(), Some(101), "panicked instead of dying");
|
|
assert_eq!(
|
|
output.status.signal(),
|
|
Some(SIGPIPE),
|
|
"expected death by SIGPIPE, got {:?}",
|
|
output.status
|
|
);
|
|
}
|
|
|
|
/// The real invocation, through a shell into `head`, which is how this was found.
|
|
/// Whether the writer gets far enough to notice is up to the scheduler, so this
|
|
/// asserts only what must hold either way: `head` gets its lines and nothing
|
|
/// alarming reaches stderr.
|
|
#[test]
|
|
fn a_shell_pipeline_into_head_prints_lines_and_not_a_panic() {
|
|
let temp = TempDir::new().unwrap();
|
|
let dir = setup_crowded_board(&temp);
|
|
|
|
let output = Command::new("sh")
|
|
.arg("-c")
|
|
.arg(format!("{} ls backlog | head -3", BIN_NAME))
|
|
.current_dir(&dir)
|
|
.output()
|
|
.unwrap();
|
|
|
|
let stdout = String::from_utf8_lossy(&output.stdout);
|
|
let stderr = String::from_utf8_lossy(&output.stderr);
|
|
|
|
assert_eq!(stdout.lines().count(), 3, "stdout was {stdout:?}");
|
|
assert!(!panic_text(&stderr), "stderr was {stderr:?}");
|
|
}
|
|
|
|
/// The reset must not touch anything else: a real failure still returns its code.
|
|
#[test]
|
|
fn real_exit_codes_are_unchanged() {
|
|
let temp = TempDir::new().unwrap();
|
|
let dir = setup_crowded_board(&temp);
|
|
|
|
let ok = Command::new(BIN_NAME)
|
|
.args(["ls", "backlog"])
|
|
.current_dir(&dir)
|
|
.output()
|
|
.unwrap();
|
|
assert_eq!(ok.status.code(), Some(0));
|
|
|
|
let missing = Command::new(BIN_NAME)
|
|
.args(["show", "no-such-ticket-anywhere"])
|
|
.current_dir(&dir)
|
|
.output()
|
|
.unwrap();
|
|
assert_eq!(missing.status.code(), Some(1));
|
|
|
|
let misuse = Command::new(BIN_NAME)
|
|
.arg("--bogus")
|
|
.current_dir(&dir)
|
|
.output()
|
|
.unwrap();
|
|
assert_eq!(misuse.status.code(), Some(2));
|
|
}
|