Skip to content

Move run_command into test.helpers #4683

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)