Skip to content

Move pythoneval and cmdline to pytest #3870

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 1 commit into from
Aug 25, 2017
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
10 changes: 7 additions & 3 deletions mypy/test/testcmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from mypy.myunit import Suite, SkipTestCaseException, AssertionFailure
from mypy.test.config import test_data_prefix, test_temp_dir
from mypy.test.data import fix_cobertura_filename
from mypy.test.data import parse_test_cases, DataDrivenTestCase
from mypy.test.data import parse_test_cases, DataDrivenTestCase, DataSuite
from mypy.test.helpers import assert_string_arrays_equal, normalize_error_messages
from mypy.version import __version__, base_version

Expand All @@ -28,9 +28,10 @@
]


class PythonEvaluationSuite(Suite):
class PythonEvaluationSuite(DataSuite):

def cases(self) -> List[DataDrivenTestCase]:
@classmethod
def cases(cls) -> List[DataDrivenTestCase]:
c = [] # type: List[DataDrivenTestCase]
for f in cmdline_files:
c += parse_test_cases(os.path.join(test_data_prefix, f),
Expand All @@ -40,6 +41,9 @@ def cases(self) -> List[DataDrivenTestCase]:
native_sep=True)
return c

def run_case(self, testcase: DataDrivenTestCase):
test_python_evaluation(testcase)


def test_python_evaluation(testcase: DataDrivenTestCase) -> None:
assert testcase.old_cwd is not None, "test was not properly set up"
Expand Down
20 changes: 12 additions & 8 deletions mypy/test/testpythoneval.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@
import subprocess
import sys

import typing
import pytest # type: ignore # no pytest in typeshed
from typing import Dict, List, Tuple, Optional

from mypy.myunit import Suite, SkipTestCaseException
from mypy.test.config import test_data_prefix, test_temp_dir
from mypy.test.data import DataDrivenTestCase, parse_test_cases
from mypy.test.data import DataDrivenTestCase, parse_test_cases, DataSuite
from mypy.test.helpers import assert_string_arrays_equal
from mypy.util import try_find_python2_interpreter


# Files which contain test case descriptions.
python_eval_files = ['pythoneval.test',
'python2eval.test']
Expand All @@ -39,8 +37,9 @@
program_re = re.compile(r'\b_program.py\b')


class PythonEvaluationSuite(Suite):
def cases(self) -> List[DataDrivenTestCase]:
class PythonEvaluationSuite(DataSuite):
@classmethod
def cases(cls) -> List[DataDrivenTestCase]:
c = [] # type: List[DataDrivenTestCase]
for f in python_eval_files:
c += parse_test_cases(os.path.join(test_data_prefix, f),
Expand All @@ -51,6 +50,9 @@ def cases(self) -> List[DataDrivenTestCase]:
test_python_evaluation, test_temp_dir, True)
return c

def run_case(self, testcase: DataDrivenTestCase):
test_python_evaluation(testcase)


def test_python_evaluation(testcase: DataDrivenTestCase) -> None:
"""Runs Mypy in a subprocess.
Expand All @@ -68,9 +70,11 @@ def test_python_evaluation(testcase: DataDrivenTestCase) -> None:
if py2:
mypy_cmdline.append('--py2')
interpreter = try_find_python2_interpreter()
if not interpreter:
if interpreter is None:
# Skip, can't find a Python 2 interpreter.
raise SkipTestCaseException()
pytest.skip()
# placate the type checker
return
else:
interpreter = python3_path

Expand Down
82 changes: 31 additions & 51 deletions runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@

from mypy.waiter import Waiter, LazySubprocess
from mypy import util
from mypy.test.config import test_data_prefix
from mypy.test.testpythoneval import python_eval_files, python_34_eval_files

import itertools
import os
from os.path import join, isdir
import re
import sys


Expand Down Expand Up @@ -92,7 +89,8 @@ def add_mypy_package(self, name: str, packagename: str, *flags: str) -> None:
def add_mypy_string(self, name: str, *args: str, cwd: Optional[str] = None) -> None:
self.add_mypy_cmd(name, ['-c'] + list(args), cwd=cwd)

def add_pytest(self, name: str, pytest_args: List[str], coverage: bool = False) -> None:
def add_pytest(self, name: str, pytest_files: List[str], coverage: bool = True) -> None:
pytest_args = pytest_files + self.arglist + self.pyt_arglist
full_name = 'pytest %s' % name
if not self.allow(full_name):
return
Expand Down Expand Up @@ -197,7 +195,12 @@ def add_imports(driver: Driver) -> None:
driver.add_python_string('import %s' % mod, 'import %s' % mod)


PYTEST_FILES = [os.path.join('mypy', 'test', '{}.py'.format(name)) for name in [
def test_path(*names: str):
return [os.path.join('mypy', 'test', '{}.py'.format(name))
for name in names]


PYTEST_FILES = test_path(
'testcheck',
'testextensions',
'testdeps',
Expand All @@ -208,57 +211,36 @@ def add_imports(driver: Driver) -> None:
'testtypegen',
'testparse',
'testsemanal',
]]
'testpythoneval',
'testcmdline'
)

MYUNIT_FILES = test_path(
'teststubgen', # contains data-driven suite

'testargs',
'testgraph',
'testinfer',
'testmoduleinfo',
'testreports',
'testsolve',
'testsubtypes',
'testtypes'
)

for f in find_files('mypy', prefix='test', suffix='.py'):
assert f in PYTEST_FILES + MYUNIT_FILES, f


def add_pytest(driver: Driver) -> None:
driver.add_pytest('pytest', PYTEST_FILES + driver.arglist + driver.pyt_arglist, True)
driver.add_pytest('pytest', PYTEST_FILES)


def add_myunit(driver: Driver) -> None:
for f in find_files('mypy', prefix='test', suffix='.py'):
for f in MYUNIT_FILES:
mod = file_to_module(f)
if mod in ('mypy.test.testpythoneval', 'mypy.test.testcmdline'):
# Run Python evaluation integration tests and command-line
# parsing tests separately since they are much slower than
# proper unit tests.
pass
elif f in PYTEST_FILES:
# This module has been converted to pytest; don't try to use myunit.
pass
else:
driver.add_python_mod('unit-test %s' % mod, 'mypy.myunit', '-m', mod,
*driver.arglist, coverage=True)


def add_pythoneval(driver: Driver) -> None:
cases = set()
case_re = re.compile(r'^\[case ([^\]]+)\]$')
for file in python_eval_files + python_34_eval_files:
with open(os.path.join(test_data_prefix, file), 'r') as f:
for line in f:
m = case_re.match(line)
if m:
case_name = m.group(1)
assert case_name[:4] == 'test'
cases.add(case_name[4:5])

for prefix in sorted(cases):
driver.add_python_mod(
'eval-test-' + prefix,
'mypy.myunit',
'-m',
'mypy.test.testpythoneval',
'test_testpythoneval_PythonEvaluationSuite.test' + prefix + '*',
*driver.arglist,
coverage=True
)


def add_cmdline(driver: Driver) -> None:
driver.add_python_mod('cmdline-test', 'mypy.myunit',
'-m', 'mypy.test.testcmdline', *driver.arglist,
coverage=True)
driver.add_python_mod('unit-test %s' % mod, 'mypy.myunit', '-m', mod,
*driver.arglist, coverage=True)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The diff is somewhat confusing - this part belongs to add_myunit()



def add_stubs(driver: Driver) -> None:
Expand Down Expand Up @@ -432,8 +414,6 @@ def main() -> None:

driver.add_flake8()
add_pytest(driver)
add_pythoneval(driver)
add_cmdline(driver)
add_basic(driver)
add_selftypecheck(driver)
add_myunit(driver)
Expand Down