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); + } +}