From 4fca86e2afe57ee2ad3dae9ffe4c4debdb565a59 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sat, 6 Apr 2019 12:09:31 +0200 Subject: [PATCH] testdir.popen: use kwargs with defaults for stdout/stderr --- changelog/5059.trivial.rst | 1 + src/_pytest/pytester.py | 9 ++++++++- testing/test_pytester.py | 19 +++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 changelog/5059.trivial.rst diff --git a/changelog/5059.trivial.rst b/changelog/5059.trivial.rst new file mode 100644 index 000000000..bd8035669 --- /dev/null +++ b/changelog/5059.trivial.rst @@ -0,0 +1 @@ +pytester's ``Testdir.popen()`` uses ``stdout`` and ``stderr`` via keyword arguments with defaults now (``subprocess.PIPE``). diff --git a/src/_pytest/pytester.py b/src/_pytest/pytester.py index 45c88bb3f..4a89f5515 100644 --- a/src/_pytest/pytester.py +++ b/src/_pytest/pytester.py @@ -1034,7 +1034,14 @@ class Testdir(object): if colitem.name == name: return colitem - def popen(self, cmdargs, stdout, stderr, stdin=CLOSE_STDIN, **kw): + def popen( + self, + cmdargs, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + stdin=CLOSE_STDIN, + **kw + ): """Invoke subprocess.Popen. This calls subprocess.Popen making sure the current working directory diff --git a/testing/test_pytester.py b/testing/test_pytester.py index 86e160ecb..f3b5f70e2 100644 --- a/testing/test_pytester.py +++ b/testing/test_pytester.py @@ -540,3 +540,22 @@ def test_popen_stdin_bytes(testdir): assert stdout.decode("utf8").splitlines() == ["input", "2ndline"] assert stderr == b"" assert proc.returncode == 0 + + +def test_popen_default_stdin_stderr_and_stdin_None(testdir): + # stdout, stderr default to pipes, + # stdin can be None to not close the pipe, avoiding + # "ValueError: flush of closed file" with `communicate()`. + p1 = testdir.makepyfile( + """ + import sys + print(sys.stdin.read()) # empty + print('stdout') + sys.stderr.write('stderr') + """ + ) + proc = testdir.popen([sys.executable, str(p1)], stdin=None) + stdout, stderr = proc.communicate(b"ignored") + assert stdout.splitlines() == [b"", b"stdout"] + assert stderr.splitlines() == [b"stderr"] + assert proc.returncode == 0