fix interactive command input
This commit is contained in:
parent
0c8a4009dc
commit
4d14b9747f
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Reference in New Issue