Skip to content

Commit 34e63a1

Browse files
emmatypinggvanrossum
authored andcommitted
Move run_command into test.helpers (#4683)
Split out of #4403, these are helpers, and will eventually be used by other tests.
1 parent ac90292 commit 34e63a1

File tree

2 files changed

+33
-32
lines changed

2 files changed

+33
-32
lines changed

mypy/test/helpers.py

+29
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import os
22
import re
3+
import subprocess
34
import sys
45
import time
56

67
from typing import List, Dict, Tuple, Callable, Any, Optional
78

89
from mypy import defaults
10+
from mypy.test.config import test_temp_dir
911

1012
import pytest # type: ignore # no pytest in typeshed
1113

@@ -327,3 +329,30 @@ def parse_options(program_text: str, testcase: DataDrivenTestCase,
327329
options.python_version = testcase_pyversion(testcase.file, testcase.name)
328330

329331
return options
332+
333+
334+
def split_lines(*streams: bytes) -> List[str]:
335+
"""Returns a single list of string lines from the byte streams in args."""
336+
return [
337+
s
338+
for stream in streams
339+
for s in stream.decode('utf8').splitlines()
340+
]
341+
342+
343+
def run_command(cmdline: List[str], *, env: Optional[Dict[str, str]] = None,
344+
timeout: int = 300, cwd: str = test_temp_dir) -> Tuple[int, List[str]]:
345+
"""A poor man's subprocess.run() for 3.4 compatibility."""
346+
process = subprocess.Popen(
347+
cmdline,
348+
env=env,
349+
stdout=subprocess.PIPE,
350+
stderr=subprocess.PIPE,
351+
cwd=cwd,
352+
)
353+
try:
354+
out, err = process.communicate(timeout=timeout)
355+
except subprocess.TimeoutExpired:
356+
out = err = b''
357+
process.kill()
358+
return process.returncode, split_lines(out, err)

mypy/test/testpythoneval.py

+4-32
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@
1313
import os
1414
import os.path
1515
import re
16-
import subprocess
1716
import sys
1817

1918
import pytest # type: ignore # no pytest in typeshed
20-
from typing import Dict, List, Tuple, Optional
19+
20+
from typing import List
2121

2222
from mypy.test.config import test_temp_dir
2323
from mypy.test.data import DataDrivenTestCase, DataSuite
24-
from mypy.test.helpers import assert_string_arrays_equal
24+
from mypy.test.helpers import assert_string_arrays_equal, run_command
2525
from mypy.util import try_find_python2_interpreter
2626
from mypy import api
2727

@@ -79,7 +79,7 @@ def test_python_evaluation(testcase: DataDrivenTestCase) -> None:
7979
output.append(line.rstrip("\r\n"))
8080
if returncode == 0:
8181
# Execute the program.
82-
returncode, interp_out = run([interpreter, program])
82+
returncode, interp_out = run_command([interpreter, program])
8383
output.extend(interp_out)
8484
# Remove temp file.
8585
os.remove(program_path)
@@ -88,35 +88,7 @@ def test_python_evaluation(testcase: DataDrivenTestCase) -> None:
8888
testcase.file, testcase.line))
8989

9090

91-
def split_lines(*streams: bytes) -> List[str]:
92-
"""Returns a single list of string lines from the byte streams in args."""
93-
return [
94-
s.rstrip('\n\r')
95-
for stream in streams
96-
for s in str(stream, 'utf8').splitlines()
97-
]
98-
99-
10091
def adapt_output(testcase: DataDrivenTestCase) -> List[str]:
10192
"""Translates the generic _program.py into the actual filename."""
10293
program = '_' + testcase.name + '.py'
10394
return [program_re.sub(program, line) for line in testcase.output]
104-
105-
106-
def run(
107-
cmdline: List[str], *, env: Optional[Dict[str, str]] = None, timeout: int = 300
108-
) -> Tuple[int, List[str]]:
109-
"""A poor man's subprocess.run() for 3.3 and 3.4 compatibility."""
110-
process = subprocess.Popen(
111-
cmdline,
112-
env=env,
113-
stdout=subprocess.PIPE,
114-
stderr=subprocess.PIPE,
115-
cwd=test_temp_dir,
116-
)
117-
try:
118-
out, err = process.communicate(timeout=timeout)
119-
except subprocess.TimeoutExpired:
120-
out = err = b''
121-
process.kill()
122-
return process.returncode, split_lines(out, err)

0 commit comments

Comments
 (0)