Skip to content

Commit 88a6a4e

Browse files
elazarggvanrossum
authored andcommitted
move test_stubs and test_samples to pytest (#5142)
Using the api, similarly to #5087. stdlibsamples is not included in this PR since it requires special handling of working directory and paths. This is another item for #1673.
1 parent 96f33a7 commit 88a6a4e

File tree

4 files changed

+69
-61
lines changed

4 files changed

+69
-61
lines changed

mypy/test/helpers.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
import sys
55
import time
66
import shutil
7+
from os.path import dirname
78

89
from typing import List, Iterable, Dict, Tuple, Callable, Any, Optional
910

1011
from mypy import defaults
1112
from mypy.test.config import test_temp_dir
13+
import mypy.api as api
1214

1315
import pytest # type: ignore # no pytest in typeshed
1416

@@ -27,6 +29,21 @@
2729
MIN_LINE_LENGTH_FOR_ALIGNMENT = 5
2830

2931

32+
def run_mypy(args: List[str]) -> None:
33+
__tracebackhide__ = True
34+
outval, errval, status = api.run(args + ['--show-traceback',
35+
'--no-site-packages'])
36+
if status != 0:
37+
sys.stdout.write(outval)
38+
sys.stderr.write(errval)
39+
pytest.fail(msg="Sample check failed", pytrace=False)
40+
41+
42+
def use_builtins_fixtures(options):
43+
root_dir = dirname(dirname(dirname(__file__)))
44+
options.path_prefix = os.path.join(root_dir, 'test-data', 'unit', 'lib-stub')
45+
46+
3047
def assert_string_arrays_equal(expected: List[str], actual: List[str],
3148
msg: str) -> None:
3249
"""Assert that two string arrays are equal.

mypy/test/testsamples.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""Self check mypy package"""
2+
import sys
3+
import os.path
4+
from typing import List, Set
5+
6+
from mypy.test.helpers import Suite, run_mypy
7+
8+
9+
class SamplesSuite(Suite):
10+
def test_stubs(self) -> None:
11+
# We only test each module in the one version mypy prefers to find.
12+
# TODO: test stubs for other versions, especially Python 2 stubs.
13+
seen = set() # type: Set[str]
14+
modules = []
15+
# TODO: This should also test Python 2, and pass pyversion accordingly.
16+
for version in ["2and3", "3", "3.5"]:
17+
# FIX: remove 'builtins', this directory does not exist
18+
for stub_type in ['builtins', 'stdlib', 'third_party']:
19+
stubdir = os.path.join('typeshed', stub_type, version)
20+
for f in find_files(stubdir, suffix='.pyi'):
21+
module = file_to_module(f[len(stubdir) + 1:])
22+
if module not in seen:
23+
seen.add(module)
24+
modules.extend(['-m', module])
25+
if modules:
26+
# these require at least 3.5 otherwise it will fail trying to import zipapp
27+
run_mypy(['--python-version=3.5'] + modules)
28+
29+
def test_samples(self) -> None:
30+
for f in find_files(os.path.join('test-data', 'samples'), suffix='.py'):
31+
mypy_args = ['--no-strict-optional']
32+
if f == os.path.join('test-data', 'samples', 'crawl2.py'):
33+
# This test requires 3.5 for async functions
34+
mypy_args.append('--python-version=3.5')
35+
run_mypy(mypy_args + [f])
36+
37+
38+
def find_files(base: str, prefix: str = '', suffix: str = '') -> List[str]:
39+
return [os.path.join(root, f)
40+
for root, dirs, files in os.walk(base)
41+
for f in files
42+
if f.startswith(prefix) and f.endswith(suffix)]
43+
44+
45+
def file_to_module(file: str) -> str:
46+
rv = os.path.splitext(file)[0].replace(os.sep, '.')
47+
if rv.endswith('.__init__'):
48+
rv = rv[:-len('.__init__')]
49+
return rv

mypy/test/testselfcheck.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
"""Self check mypy package"""
2-
import sys
3-
from typing import List
42

5-
import pytest # type: ignore
6-
7-
from mypy.test.helpers import Suite
8-
from mypy.api import run
3+
from mypy.test.helpers import Suite, run_mypy
94

105

116
class SelfCheckSuite(Suite):
@@ -14,16 +9,3 @@ def test_mypy_package(self) -> None:
149

1510
def test_testrunner(self) -> None:
1611
run_mypy(['runtests.py', 'waiter.py'])
17-
18-
19-
def run_mypy(args: List[str]) -> None:
20-
__tracebackhide__ = True
21-
outval, errval, status = run(args + ['--config-file', 'mypy_self_check.ini',
22-
'--show-traceback',
23-
'--no-site-packages'])
24-
if status != 0:
25-
sys.stdout.write(outval)
26-
errval = '\n'.join(line for line in errval.split('\n')
27-
if 'mypy_self_check.ini' not in line)
28-
sys.stderr.write(errval)
29-
pytest.fail(msg="Self check failed", pytrace=False)

runtests.py

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import sys
1010

1111
from waiter import Waiter, LazySubprocess
12+
from mypy.test.testsamples import find_files, file_to_module
1213

1314

1415
def get_versions() -> List[str]:
@@ -106,20 +107,6 @@ def list_tasks(self) -> None:
106107
print('{id}:{task}'.format(id=id, task=task.name))
107108

108109

109-
def find_files(base: str, prefix: str = '', suffix: str = '') -> List[str]:
110-
return [join(root, f)
111-
for root, dirs, files in os.walk(base)
112-
for f in files
113-
if f.startswith(prefix) and f.endswith(suffix)]
114-
115-
116-
def file_to_module(file: str) -> str:
117-
rv = os.path.splitext(file)[0].replace(os.sep, '.')
118-
if rv.endswith('.__init__'):
119-
rv = rv[:-len('.__init__')]
120-
return rv
121-
122-
123110
def test_path(*names: str) -> List[str]:
124111
return [os.path.join('mypy', 'test', '{}.py'.format(name))
125112
for name in names]
@@ -155,6 +142,7 @@ def test_path(*names: str) -> List[str]:
155142
'testpythoneval',
156143
'testcmdline',
157144
'teststubgen',
145+
'testsamples',
158146
)
159147

160148
SELFCHECK_FILES = test_path(
@@ -170,23 +158,6 @@ def add_pytest(driver: Driver) -> None:
170158
[('self-check', name) for name in SELFCHECK_FILES])
171159

172160

173-
def add_stubs(driver: Driver) -> None:
174-
# We only test each module in the one version mypy prefers to find.
175-
# TODO: test stubs for other versions, especially Python 2 stubs.
176-
177-
modules = {'typing'}
178-
# TODO: This should also test Python 2, and pass pyversion accordingly.
179-
for version in ["2and3", "3", "3.5"]:
180-
for stub_type in ['builtins', 'stdlib', 'third_party']:
181-
stubdir = join('typeshed', stub_type, version)
182-
for f in find_files(stubdir, suffix='.pyi'):
183-
module = file_to_module(f[len(stubdir) + 1:])
184-
modules.add(module)
185-
186-
# these require at least 3.5 otherwise it will fail trying to import zipapp
187-
driver.add_mypy_modules('stubs', sorted(modules), extra_args=['--python-version=3.5'])
188-
189-
190161
def add_stdlibsamples(driver: Driver) -> None:
191162
seen = set() # type: Set[str]
192163
stdlibsamples_dir = join(driver.cwd, 'test-data', 'stdlib-samples', '3.2', 'test')
@@ -202,15 +173,6 @@ def add_stdlibsamples(driver: Driver) -> None:
202173
cwd=stdlibsamples_dir, extra_args=['--no-strict-optional'])
203174

204175

205-
def add_samples(driver: Driver) -> None:
206-
for f in find_files(os.path.join('test-data', 'samples'), suffix='.py'):
207-
mypy_args = ['--no-strict-optional']
208-
if f == os.path.join('test-data', 'samples', 'crawl2.py'):
209-
# This test requires 3.5 for async functions
210-
mypy_args.append('--python-version=3.5')
211-
driver.add_mypy_cmd('file {}'.format(f), mypy_args + [f])
212-
213-
214176
def usage(status: int) -> None:
215177
print('Usage: %s [-h | -v | -q | --lf | --ff | [-x] FILTER | -a ARG | -p ARG]'
216178
'... [-- FILTER ...]'
@@ -348,9 +310,7 @@ def main() -> None:
348310

349311
driver.add_flake8()
350312
add_pytest(driver)
351-
add_stubs(driver)
352313
add_stdlibsamples(driver)
353-
add_samples(driver)
354314

355315
if list_only:
356316
driver.list_tasks()

0 commit comments

Comments
 (0)