Skip to content

Commit

Permalink
Move run_command into test.helpers (#4683)
Browse files Browse the repository at this point in the history
Split out of #4403, these are helpers, and will eventually be used by other tests.
  • Loading branch information
emmatyping authored and gvanrossum committed Mar 6, 2018
1 parent ac90292 commit 34e63a1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 32 deletions.
29 changes: 29 additions & 0 deletions mypy/test/helpers.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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)
36 changes: 4 additions & 32 deletions mypy/test/testpythoneval.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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)

0 comments on commit 34e63a1

Please sign in to comment.