Skip to content

Move test_stubs and test_samples to pytest #5142

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
Jun 23, 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
17 changes: 17 additions & 0 deletions mypy/test/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import sys
import time
import shutil
from os.path import dirname

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

from mypy import defaults
from mypy.test.config import test_temp_dir
import mypy.api as api

import pytest # type: ignore # no pytest in typeshed

Expand All @@ -27,6 +29,21 @@
MIN_LINE_LENGTH_FOR_ALIGNMENT = 5


def run_mypy(args: List[str]) -> None:
__tracebackhide__ = True
outval, errval, status = api.run(args + ['--show-traceback',
'--no-site-packages'])
if status != 0:
sys.stdout.write(outval)
sys.stderr.write(errval)
pytest.fail(msg="Sample check failed", pytrace=False)


def use_builtins_fixtures(options):
root_dir = dirname(dirname(dirname(__file__)))
options.path_prefix = os.path.join(root_dir, 'test-data', 'unit', 'lib-stub')


def assert_string_arrays_equal(expected: List[str], actual: List[str],
msg: str) -> None:
"""Assert that two string arrays are equal.
Expand Down
49 changes: 49 additions & 0 deletions mypy/test/testsamples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""Self check mypy package"""
import sys
import os.path
from typing import List, Set

from mypy.test.helpers import Suite, run_mypy


class SamplesSuite(Suite):
def test_stubs(self) -> None:
# We only test each module in the one version mypy prefers to find.
# TODO: test stubs for other versions, especially Python 2 stubs.
seen = set() # type: Set[str]
modules = []
# TODO: This should also test Python 2, and pass pyversion accordingly.
for version in ["2and3", "3", "3.5"]:
# FIX: remove 'builtins', this directory does not exist
Copy link
Member

Choose a reason for hiding this comment

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

What does "FIX:" mean? Is it a TODO? Why not do it now?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess you are right. I think I didn't want to change anything

for stub_type in ['builtins', 'stdlib', 'third_party']:
stubdir = os.path.join('typeshed', stub_type, version)
for f in find_files(stubdir, suffix='.pyi'):
module = file_to_module(f[len(stubdir) + 1:])
if module not in seen:
seen.add(module)
modules.extend(['-m', module])
if modules:
# these require at least 3.5 otherwise it will fail trying to import zipapp
run_mypy(['--python-version=3.5'] + modules)

def test_samples(self) -> None:
for f in find_files(os.path.join('test-data', 'samples'), suffix='.py'):
mypy_args = ['--no-strict-optional']
if f == os.path.join('test-data', 'samples', 'crawl2.py'):
# This test requires 3.5 for async functions
mypy_args.append('--python-version=3.5')
run_mypy(mypy_args + [f])


def find_files(base: str, prefix: str = '', suffix: str = '') -> List[str]:
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps these should be moved to mypy.test.helpers since they are used outside of this file?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think so. It's a temporary hack in runtests.py, and should be moved here soon.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, that is true. I think this okay then.

return [os.path.join(root, f)
for root, dirs, files in os.walk(base)
for f in files
if f.startswith(prefix) and f.endswith(suffix)]


def file_to_module(file: str) -> str:
rv = os.path.splitext(file)[0].replace(os.sep, '.')
if rv.endswith('.__init__'):
rv = rv[:-len('.__init__')]
return rv
20 changes: 1 addition & 19 deletions mypy/test/testselfcheck.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
"""Self check mypy package"""
import sys
from typing import List

import pytest # type: ignore

from mypy.test.helpers import Suite
from mypy.api import run
from mypy.test.helpers import Suite, run_mypy


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

def test_testrunner(self) -> None:
run_mypy(['runtests.py', 'waiter.py'])


def run_mypy(args: List[str]) -> None:
__tracebackhide__ = True
outval, errval, status = run(args + ['--config-file', 'mypy_self_check.ini',
'--show-traceback',
'--no-site-packages'])
if status != 0:
sys.stdout.write(outval)
errval = '\n'.join(line for line in errval.split('\n')
if 'mypy_self_check.ini' not in line)
sys.stderr.write(errval)
pytest.fail(msg="Self check failed", pytrace=False)
44 changes: 2 additions & 42 deletions runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import sys

from waiter import Waiter, LazySubprocess
from mypy.test.testsamples import find_files, file_to_module


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


def find_files(base: str, prefix: str = '', suffix: str = '') -> List[str]:
return [join(root, f)
for root, dirs, files in os.walk(base)
for f in files
if f.startswith(prefix) and f.endswith(suffix)]


def file_to_module(file: str) -> str:
rv = os.path.splitext(file)[0].replace(os.sep, '.')
if rv.endswith('.__init__'):
rv = rv[:-len('.__init__')]
return rv


def test_path(*names: str) -> List[str]:
return [os.path.join('mypy', 'test', '{}.py'.format(name))
for name in names]
Expand Down Expand Up @@ -155,6 +142,7 @@ def test_path(*names: str) -> List[str]:
'testpythoneval',
'testcmdline',
'teststubgen',
'testsamples',
)

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


def add_stubs(driver: Driver) -> None:
# We only test each module in the one version mypy prefers to find.
# TODO: test stubs for other versions, especially Python 2 stubs.

modules = {'typing'}
# TODO: This should also test Python 2, and pass pyversion accordingly.
for version in ["2and3", "3", "3.5"]:
for stub_type in ['builtins', 'stdlib', 'third_party']:
stubdir = join('typeshed', stub_type, version)
for f in find_files(stubdir, suffix='.pyi'):
module = file_to_module(f[len(stubdir) + 1:])
modules.add(module)

# these require at least 3.5 otherwise it will fail trying to import zipapp
driver.add_mypy_modules('stubs', sorted(modules), extra_args=['--python-version=3.5'])


def add_stdlibsamples(driver: Driver) -> None:
seen = set() # type: Set[str]
stdlibsamples_dir = join(driver.cwd, 'test-data', 'stdlib-samples', '3.2', 'test')
Expand All @@ -202,15 +173,6 @@ def add_stdlibsamples(driver: Driver) -> None:
cwd=stdlibsamples_dir, extra_args=['--no-strict-optional'])


def add_samples(driver: Driver) -> None:
for f in find_files(os.path.join('test-data', 'samples'), suffix='.py'):
mypy_args = ['--no-strict-optional']
if f == os.path.join('test-data', 'samples', 'crawl2.py'):
# This test requires 3.5 for async functions
mypy_args.append('--python-version=3.5')
driver.add_mypy_cmd('file {}'.format(f), mypy_args + [f])


def usage(status: int) -> None:
print('Usage: %s [-h | -v | -q | --lf | --ff | [-x] FILTER | -a ARG | -p ARG]'
'... [-- FILTER ...]'
Expand Down Expand Up @@ -348,9 +310,7 @@ def main() -> None:

driver.add_flake8()
add_pytest(driver)
add_stubs(driver)
add_stdlibsamples(driver)
add_samples(driver)

if list_only:
driver.list_tasks()
Expand Down