Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

fix(test): treat Linux PTY EIO-on-close as EOF in read helpers (#3398)

Worktrunk Bot1d46c0083b66
diff --git a/tests/common/pty.rs b/tests/common/pty.rs
index b20640cce..993852113 100644
--- a/tests/common/pty.rs
+++ b/tests/common/pty.rs
@@ -19,6 +19,55 @@ use std::path::Path;
use std::sync::mpsc;
use std::sync::{Arc, Mutex};
+/// Read a PTY master to end-of-stream, returning its output lossily as UTF-8.
+///
+/// On Linux a `read` on the master returns `EIO` once the child has exited and
+/// closed the slave side, instead of the clean 0-byte EOF macOS returns. Both
+/// mean "the child is gone, nothing more to read", so `EIO` is treated as
+/// end-of-stream here. `read_to_string().unwrap()` instead panicked
+/// intermittently depending on whether the read raced ahead of or behind the
+/// child's exit — #3144 was the same fragile read timing out on macOS, this is
+/// its Linux face. Lossy UTF-8 (like the Windows branch of `read_pty_output`)
+/// so a multibyte sequence truncated at `EIO` doesn't panic the test.
+#[cfg(unix)]
+pub fn read_pty_master_to_string<R: Read + ?Sized>(reader: &mut R) -> String {
+ use std::io::ErrorKind;
+ // POSIX `EIO` is 5 on every platform these tests run on (Linux, macOS);
+ // avoids a `libc` dev-dependency just for the constant.
+ const EIO: i32 = 5;
+ let mut bytes = Vec::new();
+ let mut chunk = [0u8; 4096];
+ loop {
+ match reader.read(&mut chunk) {
+ Ok(0) => break, // clean EOF (the macOS behavior)
+ Ok(n) => bytes.extend_from_slice(&chunk[..n]),
+ Err(e) if e.raw_os_error() == Some(EIO) => break,
+ // A transient `WouldBlock` (EAGAIN) means "no data yet", not EOF —
+ // retry after a short pause, mirroring the Windows branch of
+ // `read_pty_output`. The child will eventually produce data, close
+ // (EOF), or exit (EIO), so this can't spin forever. `read_to_string`
+ // didn't retry EAGAIN either, so it panicked here just the same;
+ // this is the more robust replacement.
+ Err(e) if e.kind() == ErrorKind::WouldBlock => {
+ std::thread::sleep(std::time::Duration::from_millis(10));
+ continue;
+ }
+ Err(e) if e.kind() == ErrorKind::Interrupted => continue,
+ Err(e) => panic!("PTY master read failed: {e}"),
+ }
+ }
+ String::from_utf8_lossy(&bytes).into_owned()
+}
+
+/// Read a PTY master to end-of-stream on Windows (ConPTY), where a blocking
+/// `read_to_string` reaches EOF cleanly.
+#[cfg(not(unix))]
+pub fn read_pty_master_to_string<R: Read + ?Sized>(reader: &mut R) -> String {
+ let mut buf = String::new();
+ reader.read_to_string(&mut buf).unwrap();
+ buf
+}
+
/// Read output from PTY and wait for child exit.
///
/// On Unix, this simply reads to EOF then waits for child.
@@ -40,8 +89,7 @@ pub fn read_pty_output(
// Drop writer to signal EOF to child's stdin (important for Unix PTYs)
drop(writer);
let mut reader = reader;
- let mut buf = String::new();
- reader.read_to_string(&mut buf).unwrap();
+ let buf = read_pty_master_to_string(&mut reader);
let exit_status = child.wait().unwrap();
(buf, exit_status.exit_code() as i32)
}
diff --git a/tests/common/shell.rs b/tests/common/shell.rs
index 9104a6aa1..f2829ad5a 100644
--- a/tests/common/shell.rs
+++ b/tests/common/shell.rs
@@ -18,7 +18,6 @@ pub fn shell_binary(shell: &str) -> &str {
/// Works on both Unix (bash/zsh/fish) and Windows (PowerShell, Git Bash).
pub fn execute_shell_script(repo: &TestRepo, shell: &str, script: &str) -> String {
use portable_pty::CommandBuilder;
- use std::io::Read;
let pair = super::open_pty();
@@ -102,10 +101,11 @@ pub fn execute_shell_script(repo: &TestRepo, shell: &str, script: &str) -> Strin
let mut child = pair.slave.spawn_command(cmd).unwrap();
drop(pair.slave); // Close slave in parent
- // Read everything the "terminal" would display
+ // Read everything the "terminal" would display. Blocks until child exits &
+ // PTY closes; treats Linux's EIO-on-slave-close as a clean EOF (see
+ // read_pty_master_to_string).
let mut reader = pair.master.try_clone_reader().unwrap();
- let mut buf = String::new();
- reader.read_to_string(&mut buf).unwrap(); // Blocks until child exits & PTY closes
+ let buf = super::pty::read_pty_master_to_string(&mut reader);
let status = child.wait().unwrap();
diff --git a/tests/integration_tests/shell_wrapper.rs b/tests/integration_tests/shell_wrapper.rs
index c601fb83f..ab30651ea 100644
--- a/tests/integration_tests/shell_wrapper.rs
+++ b/tests/integration_tests/shell_wrapper.rs
@@ -2723,7 +2723,7 @@ fi
#[rstest]
fn test_readme_example_approval_prompt(repo: TestRepo) {
use portable_pty::CommandBuilder;
- use std::io::{Read, Write};
+ use std::io::Write;
// Remove origin so worktrunk uses directory name as project identifier
repo.run_git(&["remote", "remove", "origin"]);
@@ -2777,8 +2777,7 @@ fi
drop(writer);
// Read all output
- let mut buf = String::new();
- reader.read_to_string(&mut buf).unwrap();
+ let buf = crate::common::pty::read_pty_master_to_string(&mut reader);
child.wait().unwrap();
// Normalize: strip ANSI codes and control characters
@@ -2835,8 +2834,6 @@ fi
/// - Completion output being executed as commands (the COMPLETE mode bug)
#[rstest]
fn test_bash_completion_produces_correct_output(repo: TestRepo) {
- use std::io::Read;
-
let wt_bin = wt_bin();
let wt_bin_dir = wt_bin.parent().unwrap();
@@ -2922,8 +2919,7 @@ fi
drop(pair.slave);
let mut reader = pair.master.try_clone_reader().unwrap();
- let mut buf = String::new();
- reader.read_to_string(&mut buf).unwrap();
+ let buf = crate::common::pty::read_pty_master_to_string(&mut reader);
let status = child.wait().unwrap();
let output = buf.replace("\r\n", "\n");
@@ -2971,8 +2967,6 @@ fi
/// wt command with COMPLETE=zsh produces completion candidates.
#[rstest]
fn test_zsh_completion_produces_correct_output(repo: TestRepo) {
- use std::io::Read;
-
let wt_bin = wt_bin();
let wt_bin_dir = wt_bin.parent().unwrap();
@@ -3047,8 +3041,7 @@ fi
drop(pair.slave);
let mut reader = pair.master.try_clone_reader().unwrap();
- let mut buf = String::new();
- reader.read_to_string(&mut buf).unwrap();
+ let buf = crate::common::pty::read_pty_master_to_string(&mut reader);
let status = child.wait().unwrap();
let output = buf.replace("\r\n", "\n");
@@ -3368,8 +3361,6 @@ for c in "${{COMPREPLY[@]}}"; do echo "${{c%% *}}"; done
#[case("zsh")]
#[case("fish")]
fn test_wrapper_help_redirect_captures_all_output(#[case] shell: &str, repo: TestRepo) {
- use std::io::Read;
-
let wt_bin = wt_bin();
let wt_bin_dir = wt_bin.parent().unwrap();
@@ -3458,8 +3449,7 @@ echo "SCRIPT_COMPLETED"
drop(pair.slave);
let mut reader = pair.master.try_clone_reader().unwrap();
- let mut buf = String::new();
- reader.read_to_string(&mut buf).unwrap();
+ let buf = crate::common::pty::read_pty_master_to_string(&mut reader);
let _status = child.wait().unwrap();
let terminal_output = buf.replace("\r\n", "\n");
@@ -3519,8 +3509,6 @@ echo "SCRIPT_COMPLETED"
#[case("zsh")]
#[case("fish")]
fn test_wrapper_help_interactive_uses_pager(#[case] shell: &str, repo: TestRepo) {
- use std::io::Read;
-
let wt_bin = wt_bin();
let wt_bin_dir = wt_bin.parent().unwrap();
@@ -3625,8 +3613,7 @@ echo "SCRIPT_COMPLETED"
drop(pair.slave);
let mut reader = pair.master.try_clone_reader().unwrap();
- let mut buf = String::new();
- reader.read_to_string(&mut buf).unwrap();
+ let buf = crate::common::pty::read_pty_master_to_string(&mut reader);
let _status = child.wait().unwrap();
let terminal_output = buf.replace("\r\n", "\n");