This repository was archived by the owner on Jun 10, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathtest_stubs.py
99 lines (77 loc) · 2.99 KB
/
test_stubs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import importlib.util
import itertools
import os
import re
from collections import defaultdict
import pytest
from mypy import api
TESTS_DIR = os.path.dirname(__file__)
PASS_DIR = os.path.join(TESTS_DIR, "pass")
FAIL_DIR = os.path.join(TESTS_DIR, "fail")
REVEAL_DIR = os.path.join(TESTS_DIR, "reveal")
def get_test_cases(directory):
for root, _, files in os.walk(directory):
for fname in files:
if os.path.splitext(fname)[-1] == ".py":
fullpath = os.path.join(root, fname)
# Use relative path for nice py.test name
relpath = os.path.relpath(fullpath, start=directory)
yield pytest.param(
fullpath,
# Manually specify a name for the test
id=relpath,
)
@pytest.mark.parametrize("path", get_test_cases(PASS_DIR))
def test_success(path):
stdout, stderr, exitcode = api.run([path])
assert exitcode == 0, stdout
assert re.match(r"Success: no issues found in \d+ source files?", stdout.strip())
@pytest.mark.parametrize("path", get_test_cases(FAIL_DIR))
def test_fail(path):
stdout, stderr, exitcode = api.run([path])
assert exitcode != 0
with open(path) as fin:
lines = fin.readlines()
errors = defaultdict(lambda: "")
error_lines = stdout.rstrip("\n").split("\n")
assert re.match(
r"Found \d+ errors? in \d+ files? \(checked \d+ source files?\)",
error_lines[-1].strip(),
)
for error_line in error_lines[:-1]:
error_line = error_line.strip()
if not error_line:
continue
lineno = int(error_line.split(":")[1])
errors[lineno] += error_line
for i, line in enumerate(lines):
lineno = i + 1
if " E:" not in line and lineno not in errors:
continue
target_line = lines[lineno - 1]
if "# E:" in target_line:
marker = target_line.split("# E:")[-1].strip()
assert lineno in errors, f'Extra error "{marker}"'
assert marker in errors[lineno]
else:
pytest.fail(f"Error {repr(errors[lineno])} not found")
@pytest.mark.parametrize("path", get_test_cases(REVEAL_DIR))
def test_reveal(path):
stdout, stderr, exitcode = api.run([path])
with open(path) as fin:
lines = fin.readlines()
for error_line in stdout.split("\n"):
error_line = error_line.strip()
if not error_line:
continue
lineno = int(error_line.split(":")[1])
assert "Revealed type is" in error_line
marker = lines[lineno - 1].split("# E:")[-1].strip()
assert marker in error_line
@pytest.mark.parametrize("path", get_test_cases(PASS_DIR))
def test_code_runs(path):
path_without_extension, _ = os.path.splitext(path)
dirname, filename = path.split(os.sep)[-2:]
spec = importlib.util.spec_from_file_location(f"{dirname}.{filename}", path)
test_module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(test_module)