Skip to content

Commit d4a2ac6

Browse files
committed
move pythoneval and cmdline to pytest
1 parent 1e60b35 commit d4a2ac6

File tree

3 files changed

+54
-48
lines changed

3 files changed

+54
-48
lines changed

mypy/test/testcmdline.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from mypy.myunit import Suite, SkipTestCaseException, AssertionFailure
1515
from mypy.test.config import test_data_prefix, test_temp_dir
1616
from mypy.test.data import fix_cobertura_filename
17-
from mypy.test.data import parse_test_cases, DataDrivenTestCase
17+
from mypy.test.data import parse_test_cases, DataDrivenTestCase, DataSuite
1818
from mypy.test.helpers import assert_string_arrays_equal, normalize_error_messages
1919
from mypy.version import __version__, base_version
2020

@@ -28,9 +28,10 @@
2828
]
2929

3030

31-
class PythonEvaluationSuite(Suite):
31+
class PythonEvaluationSuite(DataSuite):
3232

33-
def cases(self) -> List[DataDrivenTestCase]:
33+
@classmethod
34+
def cases(cls) -> List[DataDrivenTestCase]:
3435
c = [] # type: List[DataDrivenTestCase]
3536
for f in cmdline_files:
3637
c += parse_test_cases(os.path.join(test_data_prefix, f),
@@ -40,6 +41,9 @@ def cases(self) -> List[DataDrivenTestCase]:
4041
native_sep=True)
4142
return c
4243

44+
def run_case(self, testcase: DataDrivenTestCase):
45+
test_python_evaluation(testcase)
46+
4347

4448
def test_python_evaluation(testcase: DataDrivenTestCase) -> None:
4549
assert testcase.old_cwd is not None, "test was not properly set up"

mypy/test/testpythoneval.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,14 @@
1818
import subprocess
1919
import sys
2020

21-
import typing
21+
import pytest # type: ignore # no pytest in typeshed
2222
from typing import Dict, List, Tuple, Optional
2323

24-
from mypy.myunit import Suite, SkipTestCaseException
2524
from mypy.test.config import test_data_prefix, test_temp_dir
26-
from mypy.test.data import DataDrivenTestCase, parse_test_cases
25+
from mypy.test.data import DataDrivenTestCase, parse_test_cases, DataSuite
2726
from mypy.test.helpers import assert_string_arrays_equal
2827
from mypy.util import try_find_python2_interpreter
2928

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

4139

42-
class PythonEvaluationSuite(Suite):
43-
def cases(self) -> List[DataDrivenTestCase]:
40+
class PythonEvaluationSuite(DataSuite):
41+
@classmethod
42+
def cases(cls) -> List[DataDrivenTestCase]:
4443
c = [] # type: List[DataDrivenTestCase]
4544
for f in python_eval_files:
4645
c += parse_test_cases(os.path.join(test_data_prefix, f),
@@ -51,6 +50,9 @@ def cases(self) -> List[DataDrivenTestCase]:
5150
test_python_evaluation, test_temp_dir, True)
5251
return c
5352

53+
def run_case(self, testcase: DataDrivenTestCase):
54+
test_python_evaluation(testcase)
55+
5456

5557
def test_python_evaluation(testcase: DataDrivenTestCase) -> None:
5658
"""Runs Mypy in a subprocess.
@@ -68,9 +70,11 @@ def test_python_evaluation(testcase: DataDrivenTestCase) -> None:
6870
if py2:
6971
mypy_cmdline.append('--py2')
7072
interpreter = try_find_python2_interpreter()
71-
if not interpreter:
73+
if interpreter is None:
7274
# Skip, can't find a Python 2 interpreter.
73-
raise SkipTestCaseException()
75+
pytest.skip()
76+
# placate the type checker
77+
return
7478
else:
7579
interpreter = python3_path
7680

runtests.py

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,10 @@
55

66
from mypy.waiter import Waiter, LazySubprocess
77
from mypy import util
8-
from mypy.test.config import test_data_prefix
9-
from mypy.test.testpythoneval import python_eval_files, python_34_eval_files
108

119
import itertools
1210
import os
1311
from os.path import join, isdir
14-
import re
1512
import sys
1613

1714

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

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

199197

200-
PYTEST_FILES = [os.path.join('mypy', 'test', '{}.py'.format(name)) for name in [
198+
def test_path(*names: str):
199+
return [os.path.join('mypy', 'test', '{}.py'.format(name))
200+
for name in names]
201+
202+
203+
PYTEST_FILES = test_path(
201204
'testcheck',
202205
'testextensions',
203206
'testdeps',
@@ -208,11 +211,35 @@ def add_imports(driver: Driver) -> None:
208211
'testtypegen',
209212
'testparse',
210213
'testsemanal',
211-
]]
214+
)
215+
216+
PYEVAL_FILES = test_path('testpythoneval')
217+
CMD_FILES = test_path('testcmdline')
218+
219+
MYUNIT_FILES = test_path(
220+
'teststubgen', # contains data-driven suite
221+
222+
'testargs',
223+
'testgraph',
224+
'testinfer',
225+
'testmoduleinfo',
226+
'testreports',
227+
'testsolve',
228+
'testsubtypes',
229+
'testtypes'
230+
)
212231

213232

214233
def add_pytest(driver: Driver) -> None:
215-
driver.add_pytest('pytest', PYTEST_FILES + driver.arglist + driver.pyt_arglist, True)
234+
driver.add_pytest('pytest', PYTEST_FILES)
235+
236+
237+
def add_pythoneval(driver: Driver) -> None:
238+
driver.add_pytest('eval', PYEVAL_FILES)
239+
240+
241+
def add_cmdline(driver: Driver) -> None:
242+
driver.add_pytest('cmd', CMD_FILES)
216243

217244

218245
def add_myunit(driver: Driver) -> None:
@@ -227,40 +254,11 @@ def add_myunit(driver: Driver) -> None:
227254
# This module has been converted to pytest; don't try to use myunit.
228255
pass
229256
else:
257+
assert f in MYUNIT_FILES, f
230258
driver.add_python_mod('unit-test %s' % mod, 'mypy.myunit', '-m', mod,
231259
*driver.arglist, coverage=True)
232260

233261

234-
def add_pythoneval(driver: Driver) -> None:
235-
cases = set()
236-
case_re = re.compile(r'^\[case ([^\]]+)\]$')
237-
for file in python_eval_files + python_34_eval_files:
238-
with open(os.path.join(test_data_prefix, file), 'r') as f:
239-
for line in f:
240-
m = case_re.match(line)
241-
if m:
242-
case_name = m.group(1)
243-
assert case_name[:4] == 'test'
244-
cases.add(case_name[4:5])
245-
246-
for prefix in sorted(cases):
247-
driver.add_python_mod(
248-
'eval-test-' + prefix,
249-
'mypy.myunit',
250-
'-m',
251-
'mypy.test.testpythoneval',
252-
'test_testpythoneval_PythonEvaluationSuite.test' + prefix + '*',
253-
*driver.arglist,
254-
coverage=True
255-
)
256-
257-
258-
def add_cmdline(driver: Driver) -> None:
259-
driver.add_python_mod('cmdline-test', 'mypy.myunit',
260-
'-m', 'mypy.test.testcmdline', *driver.arglist,
261-
coverage=True)
262-
263-
264262
def add_stubs(driver: Driver) -> None:
265263
# We only test each module in the one version mypy prefers to find.
266264
# TODO: test stubs for other versions, especially Python 2 stubs.

0 commit comments

Comments
 (0)