diff --git a/src/uu/seq/src/seq.rs b/src/uu/seq/src/seq.rs index 6741356606d..7b56c26f574 100644 --- a/src/uu/seq/src/seq.rs +++ b/src/uu/seq/src/seq.rs @@ -4,7 +4,7 @@ // file that was distributed with this source code. // spell-checker:ignore (ToDO) bigdecimal extendedbigdecimal numberparse hexadecimalfloat biguint use std::ffi::{OsStr, OsString}; -use std::io::{BufWriter, ErrorKind, Write, stdout}; +use std::io::{BufWriter, Write, stdout}; use clap::{Arg, ArgAction, Command}; use num_bigint::BigUint; @@ -211,7 +211,12 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { match result { Ok(()) => Ok(()), - Err(err) if err.kind() == ErrorKind::BrokenPipe => Ok(()), + Err(err) if err.kind() == std::io::ErrorKind::BrokenPipe => { + // GNU seq prints the Broken pipe message but still exits with status 0 + let err = err.map_err_context(|| "write error".into()); + uucore::show_error!("{err}"); + Ok(()) + } Err(err) => Err(err.map_err_context(|| "write error".into())), } } diff --git a/tests/by-util/test_seq.rs b/tests/by-util/test_seq.rs index f82a6228fe1..de0ad10d9fb 100644 --- a/tests/by-util/test_seq.rs +++ b/tests/by-util/test_seq.rs @@ -10,6 +10,25 @@ fn test_invalid_arg() { new_ucmd!().arg("--definitely-invalid").fails_with_code(1); } +#[test] +#[cfg(unix)] +fn test_broken_pipe_still_exits_success() { + use std::process::Stdio; + + let mut child = new_ucmd!() + .args(&["1", "5"]) + .set_stdout(Stdio::piped()) + .run_no_wait(); + + // Trigger a Broken pipe by writing to a pipe whose reader closed first. + child.close_stdout(); + let result = child.wait().unwrap(); + + result + .code_is(0) + .stderr_contains("write error: Broken pipe"); +} + #[test] fn test_no_args() { new_ucmd!()