"""Check that the shell processes a pipe before its writer closes it.""" import select import subprocess import sys from pathlib import Path TIMEOUT = 4 shell = str(Path(sys.argv[1] if len(sys.argv) > 1 else "rc2").resolve()) process = subprocess.Popen([shell, "-I"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) try: process.stdin.write(b"echo first\n") process.stdin.flush() ready, _, _ = select.select([process.stdout], [], [], TIMEOUT) assert ready, "The shell waited for EOF before it processed a complete command" assert process.stdout.readline() == b"first\n" process.stdin.write(b"fn f {\necho second\n}\nf") process.stdin.close() assert process.wait(timeout=TIMEOUT) == 0 assert process.stdout.read() == b"second\n" assert process.stderr.read() == b"" print("Stream tests passed") finally: if process.poll() is None: process.kill() process.wait() result = subprocess.run([shell, "-I"], input=b"echo 'a\x00b'\n", capture_output=True, timeout=TIMEOUT) assert result.returncode == 1 and not result.stdout and result.stderr, "The shell accepted a NUL source byte"