Merge pull request #5059 from blueyed/pytester-popen-run-stdin
pytester: allow passing in stdin to run/popen
This commit is contained in:
		
						commit
						10fa66e5b5
					
				| 
						 | 
					@ -0,0 +1 @@
 | 
				
			||||||
 | 
					Standard input (stdin) can be given to pytester's ``Testdir.run()`` and ``Testdir.popen()``.
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1 @@
 | 
				
			||||||
 | 
					pytester's ``Testdir.popen()`` uses ``stdout`` and ``stderr`` via keyword arguments with defaults now (``subprocess.PIPE``).
 | 
				
			||||||
| 
						 | 
					@ -473,6 +473,8 @@ class Testdir(object):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    CLOSE_STDIN = object
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    class TimeoutExpired(Exception):
 | 
					    class TimeoutExpired(Exception):
 | 
				
			||||||
        pass
 | 
					        pass
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1032,7 +1034,14 @@ class Testdir(object):
 | 
				
			||||||
            if colitem.name == name:
 | 
					            if colitem.name == name:
 | 
				
			||||||
                return colitem
 | 
					                return colitem
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def popen(self, cmdargs, stdout, stderr, **kw):
 | 
					    def popen(
 | 
				
			||||||
 | 
					        self,
 | 
				
			||||||
 | 
					        cmdargs,
 | 
				
			||||||
 | 
					        stdout=subprocess.PIPE,
 | 
				
			||||||
 | 
					        stderr=subprocess.PIPE,
 | 
				
			||||||
 | 
					        stdin=CLOSE_STDIN,
 | 
				
			||||||
 | 
					        **kw
 | 
				
			||||||
 | 
					    ):
 | 
				
			||||||
        """Invoke subprocess.Popen.
 | 
					        """Invoke subprocess.Popen.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        This calls subprocess.Popen making sure the current working directory
 | 
					        This calls subprocess.Popen making sure the current working directory
 | 
				
			||||||
| 
						 | 
					@ -1050,10 +1059,18 @@ class Testdir(object):
 | 
				
			||||||
        env["USERPROFILE"] = env["HOME"]
 | 
					        env["USERPROFILE"] = env["HOME"]
 | 
				
			||||||
        kw["env"] = env
 | 
					        kw["env"] = env
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        popen = subprocess.Popen(
 | 
					        if stdin is Testdir.CLOSE_STDIN:
 | 
				
			||||||
            cmdargs, stdin=subprocess.PIPE, stdout=stdout, stderr=stderr, **kw
 | 
					            kw["stdin"] = subprocess.PIPE
 | 
				
			||||||
        )
 | 
					        elif isinstance(stdin, bytes):
 | 
				
			||||||
 | 
					            kw["stdin"] = subprocess.PIPE
 | 
				
			||||||
 | 
					        else:
 | 
				
			||||||
 | 
					            kw["stdin"] = stdin
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        popen = subprocess.Popen(cmdargs, stdout=stdout, stderr=stderr, **kw)
 | 
				
			||||||
 | 
					        if stdin is Testdir.CLOSE_STDIN:
 | 
				
			||||||
            popen.stdin.close()
 | 
					            popen.stdin.close()
 | 
				
			||||||
 | 
					        elif isinstance(stdin, bytes):
 | 
				
			||||||
 | 
					            popen.stdin.write(stdin)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return popen
 | 
					        return popen
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1065,6 +1082,10 @@ class Testdir(object):
 | 
				
			||||||
        :param args: the sequence of arguments to pass to `subprocess.Popen()`
 | 
					        :param args: the sequence of arguments to pass to `subprocess.Popen()`
 | 
				
			||||||
        :param timeout: the period in seconds after which to timeout and raise
 | 
					        :param timeout: the period in seconds after which to timeout and raise
 | 
				
			||||||
            :py:class:`Testdir.TimeoutExpired`
 | 
					            :py:class:`Testdir.TimeoutExpired`
 | 
				
			||||||
 | 
					        :param stdin: optional standard input.  Bytes are being send, closing
 | 
				
			||||||
 | 
					            the pipe, otherwise it is passed through to ``popen``.
 | 
				
			||||||
 | 
					            Defaults to ``CLOSE_STDIN``, which translates to using a pipe
 | 
				
			||||||
 | 
					            (``subprocess.PIPE``) that gets closed.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        Returns a :py:class:`RunResult`.
 | 
					        Returns a :py:class:`RunResult`.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1072,6 +1093,7 @@ class Testdir(object):
 | 
				
			||||||
        __tracebackhide__ = True
 | 
					        __tracebackhide__ = True
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        timeout = kwargs.pop("timeout", None)
 | 
					        timeout = kwargs.pop("timeout", None)
 | 
				
			||||||
 | 
					        stdin = kwargs.pop("stdin", Testdir.CLOSE_STDIN)
 | 
				
			||||||
        raise_on_kwargs(kwargs)
 | 
					        raise_on_kwargs(kwargs)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        cmdargs = [
 | 
					        cmdargs = [
 | 
				
			||||||
| 
						 | 
					@ -1086,8 +1108,14 @@ class Testdir(object):
 | 
				
			||||||
        try:
 | 
					        try:
 | 
				
			||||||
            now = time.time()
 | 
					            now = time.time()
 | 
				
			||||||
            popen = self.popen(
 | 
					            popen = self.popen(
 | 
				
			||||||
                cmdargs, stdout=f1, stderr=f2, close_fds=(sys.platform != "win32")
 | 
					                cmdargs,
 | 
				
			||||||
 | 
					                stdin=stdin,
 | 
				
			||||||
 | 
					                stdout=f1,
 | 
				
			||||||
 | 
					                stderr=f2,
 | 
				
			||||||
 | 
					                close_fds=(sys.platform != "win32"),
 | 
				
			||||||
            )
 | 
					            )
 | 
				
			||||||
 | 
					            if isinstance(stdin, bytes):
 | 
				
			||||||
 | 
					                popen.stdin.close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            def handle_timeout():
 | 
					            def handle_timeout():
 | 
				
			||||||
                __tracebackhide__ = True
 | 
					                __tracebackhide__ = True
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -4,6 +4,7 @@ from __future__ import division
 | 
				
			||||||
from __future__ import print_function
 | 
					from __future__ import print_function
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import os
 | 
					import os
 | 
				
			||||||
 | 
					import subprocess
 | 
				
			||||||
import sys
 | 
					import sys
 | 
				
			||||||
import time
 | 
					import time
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -482,3 +483,79 @@ def test_pytester_addopts(request, monkeypatch):
 | 
				
			||||||
        testdir.finalize()
 | 
					        testdir.finalize()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    assert os.environ["PYTEST_ADDOPTS"] == "--orig-unused"
 | 
					    assert os.environ["PYTEST_ADDOPTS"] == "--orig-unused"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_run_stdin(testdir):
 | 
				
			||||||
 | 
					    with pytest.raises(testdir.TimeoutExpired):
 | 
				
			||||||
 | 
					        testdir.run(
 | 
				
			||||||
 | 
					            sys.executable,
 | 
				
			||||||
 | 
					            "-c",
 | 
				
			||||||
 | 
					            "import sys, time; time.sleep(1); print(sys.stdin.read())",
 | 
				
			||||||
 | 
					            stdin=subprocess.PIPE,
 | 
				
			||||||
 | 
					            timeout=0.1,
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    with pytest.raises(testdir.TimeoutExpired):
 | 
				
			||||||
 | 
					        result = testdir.run(
 | 
				
			||||||
 | 
					            sys.executable,
 | 
				
			||||||
 | 
					            "-c",
 | 
				
			||||||
 | 
					            "import sys, time; time.sleep(1); print(sys.stdin.read())",
 | 
				
			||||||
 | 
					            stdin=b"input\n2ndline",
 | 
				
			||||||
 | 
					            timeout=0.1,
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    result = testdir.run(
 | 
				
			||||||
 | 
					        sys.executable,
 | 
				
			||||||
 | 
					        "-c",
 | 
				
			||||||
 | 
					        "import sys; print(sys.stdin.read())",
 | 
				
			||||||
 | 
					        stdin=b"input\n2ndline",
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
 | 
					    assert result.stdout.lines == ["input", "2ndline"]
 | 
				
			||||||
 | 
					    assert result.stderr.str() == ""
 | 
				
			||||||
 | 
					    assert result.ret == 0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_popen_stdin_pipe(testdir):
 | 
				
			||||||
 | 
					    proc = testdir.popen(
 | 
				
			||||||
 | 
					        [sys.executable, "-c", "import sys; print(sys.stdin.read())"],
 | 
				
			||||||
 | 
					        stdout=subprocess.PIPE,
 | 
				
			||||||
 | 
					        stderr=subprocess.PIPE,
 | 
				
			||||||
 | 
					        stdin=subprocess.PIPE,
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
 | 
					    stdin = b"input\n2ndline"
 | 
				
			||||||
 | 
					    stdout, stderr = proc.communicate(input=stdin)
 | 
				
			||||||
 | 
					    assert stdout.decode("utf8").splitlines() == ["input", "2ndline"]
 | 
				
			||||||
 | 
					    assert stderr == b""
 | 
				
			||||||
 | 
					    assert proc.returncode == 0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_popen_stdin_bytes(testdir):
 | 
				
			||||||
 | 
					    proc = testdir.popen(
 | 
				
			||||||
 | 
					        [sys.executable, "-c", "import sys; print(sys.stdin.read())"],
 | 
				
			||||||
 | 
					        stdout=subprocess.PIPE,
 | 
				
			||||||
 | 
					        stderr=subprocess.PIPE,
 | 
				
			||||||
 | 
					        stdin=b"input\n2ndline",
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
 | 
					    stdout, stderr = proc.communicate()
 | 
				
			||||||
 | 
					    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
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue