diff --git a/mypy/test/helpers.py b/mypy/test/helpers.py index daffa8344dc0..267f99e5586b 100644 --- a/mypy/test/helpers.py +++ b/mypy/test/helpers.py @@ -1,11 +1,13 @@ import os import re +import subprocess import sys import time from typing import List, Dict, Tuple, Callable, Any, Optional from mypy import defaults +from mypy.test.config import test_temp_dir import pytest # type: ignore # no pytest in typeshed @@ -327,3 +329,30 @@ def parse_options(program_text: str, testcase: DataDrivenTestCase, options.python_version = testcase_pyversion(testcase.file, testcase.name) return options + + +def split_lines(*streams: bytes) -> List[str]: + """Returns a single list of string lines from the byte streams in args.""" + return [ + s + for stream in streams + for s in stream.decode('utf8').splitlines() + ] + + +def run_command(cmdline: List[str], *, env: Optional[Dict[str, str]] = None, + timeout: int = 300, cwd: str = test_temp_dir) -> Tuple[int, List[str]]: + """A poor man's subprocess.run() for 3.4 compatibility.""" + process = subprocess.Popen( + cmdline, + env=env, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + cwd=cwd, + ) + try: + out, err = process.communicate(timeout=timeout) + except subprocess.TimeoutExpired: + out = err = b'' + process.kill() + return process.returncode, split_lines(out, err) diff --git a/mypy/test/testpythoneval.py b/mypy/test/testpythoneval.py index 222fa6ff32c2..85cffdff6478 100644 --- a/mypy/test/testpythoneval.py +++ b/mypy/test/testpythoneval.py @@ -13,15 +13,15 @@ import os import os.path import re -import subprocess import sys import pytest # type: ignore # no pytest in typeshed -from typing import Dict, List, Tuple, Optional + +from typing import List from mypy.test.config import test_temp_dir from mypy.test.data import DataDrivenTestCase, DataSuite -from mypy.test.helpers import assert_string_arrays_equal +from mypy.test.helpers import assert_string_arrays_equal, run_command from mypy.util import try_find_python2_interpreter from mypy import api @@ -79,7 +79,7 @@ def test_python_evaluation(testcase: DataDrivenTestCase) -> None: output.append(line.rstrip("\r\n")) if returncode == 0: # Execute the program. - returncode, interp_out = run([interpreter, program]) + returncode, interp_out = run_command([interpreter, program]) output.extend(interp_out) # Remove temp file. os.remove(program_path) @@ -88,35 +88,7 @@ def test_python_evaluation(testcase: DataDrivenTestCase) -> None: testcase.file, testcase.line)) -def split_lines(*streams: bytes) -> List[str]: - """Returns a single list of string lines from the byte streams in args.""" - return [ - s.rstrip('\n\r') - for stream in streams - for s in str(stream, 'utf8').splitlines() - ] - - def adapt_output(testcase: DataDrivenTestCase) -> List[str]: """Translates the generic _program.py into the actual filename.""" program = '_' + testcase.name + '.py' return [program_re.sub(program, line) for line in testcase.output] - - -def run( - cmdline: List[str], *, env: Optional[Dict[str, str]] = None, timeout: int = 300 -) -> Tuple[int, List[str]]: - """A poor man's subprocess.run() for 3.3 and 3.4 compatibility.""" - process = subprocess.Popen( - cmdline, - env=env, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - cwd=test_temp_dir, - ) - try: - out, err = process.communicate(timeout=timeout) - except subprocess.TimeoutExpired: - out = err = b'' - process.kill() - return process.returncode, split_lines(out, err)