From 4d14b9747f477445a723066a49ea305dd06f5eb1 Mon Sep 17 00:00:00 2001 From: AI Christianson Date: Wed, 12 Feb 2025 17:08:37 -0500 Subject: [PATCH] fix interactive command input --- ra_aid/proc/interactive.py | 13 +++++++++++-- tests/ra_aid/proc/test_interactive.py | 25 +++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/ra_aid/proc/interactive.py b/ra_aid/proc/interactive.py index e4917ba..8d2d1bd 100644 --- a/ra_aid/proc/interactive.py +++ b/ra_aid/proc/interactive.py @@ -12,6 +12,8 @@ import os import shlex import shutil import errno +import sys +import io import subprocess from typing import List, Tuple @@ -62,12 +64,19 @@ def run_interactive_command(cmd: List[str]) -> Tuple[bytes, int]: # Open a new pseudo-tty. master_fd, slave_fd = os.openpty() - # Spawn the subprocess with its stdio attached to the slave end. + try: + # Try to use real TTY stdin if available + stdin_fd = sys.stdin.fileno() + except (AttributeError, io.UnsupportedOperation): + # Fallback to pseudo-TTY for tests + stdin_fd = slave_fd + proc = subprocess.Popen( cmd, - stdin=slave_fd, + stdin=stdin_fd, stdout=slave_fd, stderr=slave_fd, + bufsize=0, close_fds=True ) os.close(slave_fd) # Close slave in the parent. diff --git a/tests/ra_aid/proc/test_interactive.py b/tests/ra_aid/proc/test_interactive.py index 318fc7a..15afeb2 100644 --- a/tests/ra_aid/proc/test_interactive.py +++ b/tests/ra_aid/proc/test_interactive.py @@ -1,6 +1,7 @@ """Tests for the interactive subprocess module.""" import os +import sys import tempfile import pytest @@ -168,3 +169,27 @@ def test_tty_available(): b"/dev/pts/" in output_cleaned or b"/dev/ttys" in output_cleaned ), f"Unexpected TTY output: {output_cleaned}" assert retcode == 0 + + +def test_interactive_input(): + """Test that interactive input works properly with cat command.""" + # Create a temporary file to store expected input + with tempfile.NamedTemporaryFile(mode='w', delete=False) as f: + f.write("test input\n") + temp_path = f.name + + try: + # Redirect the temp file as stdin and run cat + with open(temp_path, 'rb') as stdin_file: + old_stdin = sys.stdin + sys.stdin = stdin_file + try: + output, retcode = run_interactive_command(["cat"]) + finally: + sys.stdin = old_stdin + + # Verify the output matches input + assert b"test input" in output + assert retcode == 0 + finally: + os.unlink(temp_path)