From ba7fff057091787f31f5691ac245b96d66538a27 Mon Sep 17 00:00:00 2001 From: wuyangfan <1102042793@qq.com> Date: Sun, 17 May 2026 11:00:51 +0800 Subject: [PATCH] fix: fail --tty when /dev/tty cannot be opened; satisfy clippy Return an error if --tty is set but /dev/tty is unavailable (e.g. macOS permissions). Add unit tests for terminal_io. Co-authored-by: Cursor --- src/terminal_io.rs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/terminal_io.rs b/src/terminal_io.rs index 48b87f5a..6beab5a4 100644 --- a/src/terminal_io.rs +++ b/src/terminal_io.rs @@ -24,8 +24,10 @@ impl TerminalWriter { { let use_tty = force_tty || !io::stdout().is_terminal(); if use_tty { - if let Ok(file) = std::fs::File::open("/dev/tty") { - return Ok(Self::Tty(file)); + match std::fs::File::open("/dev/tty") { + Ok(file) => return Ok(Self::Tty(file)), + Err(err) if force_tty => return Err(err), + Err(_) => {} } } } @@ -102,3 +104,21 @@ pub fn execute(cmd: C) -> io::Result<()> { Ok(()) }) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn open_stdout_when_not_forcing_tty() { + let writer = TerminalWriter::open(false).unwrap(); + assert!(matches!(writer, TerminalWriter::Stdout(_))); + } + + #[test] + #[cfg(not(unix))] + fn open_tty_errors_on_non_unix() { + let err = TerminalWriter::open(true).unwrap_err(); + assert_eq!(err.kind(), io::ErrorKind::Unsupported); + } +}