| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import subprocess |
| 6 |
from subprocess import Popen |
| 7 |
from sys import argv, executable, exit |
| 8 |
|
| 9 |
# Slow test suites |
| 10 |
CMDLINE = "PythonCmdline" |
| 11 |
PEP561 = "PEP561Suite" |
| 12 |
EVALUATION = "PythonEvaluation" |
| 13 |
DAEMON = "testdaemon" |
| 14 |
STUBGEN_CMD = "StubgenCmdLine" |
| 15 |
STUBGEN_PY = "StubgenPythonSuite" |
| 16 |
MYPYC_RUN = "TestRun" |
| 17 |
MYPYC_RUN_MULTI = "TestRunMultiFile" |
| 18 |
MYPYC_EXTERNAL = "TestExternal" |
| 19 |
MYPYC_COMMAND_LINE = "TestCommandLine" |
| 20 |
MYPYC_SEPARATE = "TestRunSeparate" |
| 21 |
MYPYC_MULTIMODULE = "multimodule" # Subset of mypyc run tests that are slow |
| 22 |
ERROR_STREAM = "ErrorStreamSuite" |
| 23 |
DIFF_CACHE = "DiffCacheIntegrationTests" |
| 24 |
|
| 25 |
|
| 26 |
ALL_NON_FAST = [ |
| 27 |
CMDLINE, |
| 28 |
PEP561, |
| 29 |
EVALUATION, |
| 30 |
DAEMON, |
| 31 |
STUBGEN_CMD, |
| 32 |
STUBGEN_PY, |
| 33 |
MYPYC_RUN, |
| 34 |
MYPYC_RUN_MULTI, |
| 35 |
MYPYC_EXTERNAL, |
| 36 |
MYPYC_COMMAND_LINE, |
| 37 |
MYPYC_SEPARATE, |
| 38 |
ERROR_STREAM, |
| 39 |
DIFF_CACHE, |
| 40 |
] |
| 41 |
|
| 42 |
|
| 43 |
# This must be enabled by explicitly including 'pytest-extra' on the command line |
| 44 |
PYTEST_OPT_IN = [PEP561] |
| 45 |
|
| 46 |
|
| 47 |
# These must be enabled by explicitly including 'mypyc-extra' on the command line. |
| 48 |
MYPYC_OPT_IN = [MYPYC_RUN, MYPYC_RUN_MULTI, MYPYC_SEPARATE] |
| 49 |
|
| 50 |
# These mypyc test filters cover most slow test cases |
| 51 |
MYPYC_SLOW = [MYPYC_RUN_MULTI, MYPYC_COMMAND_LINE, MYPYC_SEPARATE, MYPYC_MULTIMODULE] |
| 52 |
|
| 53 |
|
| 54 |
# We split the pytest run into three parts to improve test |
| 55 |
# parallelization. Each run should have tests that each take a roughly similar |
| 56 |
# time to run. |
| 57 |
cmds = { |
| 58 |
# Self type check |
| 59 |
"self": [ |
| 60 |
executable, |
| 61 |
"-m", |
| 62 |
"mypy", |
| 63 |
"--config-file", |
| 64 |
"mypy_self_check.ini", |
| 65 |
"-p", |
| 66 |
"mypy", |
| 67 |
"-p", |
| 68 |
"mypyc", |
| 69 |
], |
| 70 |
# Type check setup.py as well |
| 71 |
"self-packaging": [ |
| 72 |
executable, |
| 73 |
"-m", |
| 74 |
"mypy", |
| 75 |
"--config-file", |
| 76 |
"mypy_self_check.ini", |
| 77 |
"setup.py", |
| 78 |
], |
| 79 |
# Lint |
| 80 |
"lint": ["pre-commit", "run", "--all-files"], |
| 81 |
# Fast test cases only (this is the bulk of the test suite) |
| 82 |
"pytest-fast": ["pytest", "-q", "-k", f"not ({' or '.join(ALL_NON_FAST)})"], |
| 83 |
# Test cases that invoke mypy (with small inputs) |
| 84 |
"pytest-cmdline": [ |
| 85 |
"pytest", |
| 86 |
"-q", |
| 87 |
"-k", |
| 88 |
" or ".join([CMDLINE, EVALUATION, STUBGEN_CMD, STUBGEN_PY]), |
| 89 |
], |
| 90 |
# Test cases that may take seconds to run each |
| 91 |
"pytest-slow": [ |
| 92 |
"pytest", |
| 93 |
"-q", |
| 94 |
"-k", |
| 95 |
" or ".join([DAEMON, MYPYC_EXTERNAL, MYPYC_COMMAND_LINE, ERROR_STREAM, DIFF_CACHE]), |
| 96 |
], |
| 97 |
"mypyc-fast": ["pytest", "-q", "mypyc", "-k", f"not ({' or '.join(MYPYC_SLOW)})"], |
| 98 |
# Test cases that might take minutes to run |
| 99 |
"pytest-extra": ["pytest", "-q", "-k", " or ".join(PYTEST_OPT_IN)], |
| 100 |
# Mypyc tests that aren't run by default, since they are slow and rarely |
| 101 |
# fail for commits that don't touch mypyc |
| 102 |
"mypyc-extra": ["pytest", "-q", "-k", " or ".join(MYPYC_OPT_IN)], |
| 103 |
} |
| 104 |
|
| 105 |
# Stop run immediately if these commands fail |
| 106 |
FAST_FAIL = ["self", "lint"] |
| 107 |
|
| 108 |
EXTRA_COMMANDS = ("pytest-extra", "mypyc-fast", "mypyc-extra") |
| 109 |
DEFAULT_COMMANDS = [cmd for cmd in cmds if cmd not in EXTRA_COMMANDS] |
| 110 |
|
| 111 |
assert all(cmd in cmds for cmd in FAST_FAIL) |
| 112 |
|
| 113 |
|
| 114 |
def run_cmd(name: str) -> int: |
| 115 |
status = 0 |
| 116 |
if name in cmds: |
| 117 |
cmd = cmds[name] |
| 118 |
else: |
| 119 |
if name.endswith(".test"): |
| 120 |
cmd = ["pytest", f"mypy/test/testcheck.py::TypeCheckSuite::{name}"] |
| 121 |
else: |
| 122 |
cmd = ["pytest", "-n0", "-k", name] |
| 123 |
print(f"run {name}: {cmd}") |
| 124 |
proc = subprocess.run(cmd, stderr=subprocess.STDOUT) |
| 125 |
if proc.returncode: |
| 126 |
print("\nFAILED: %s" % name) |
| 127 |
status = proc.returncode |
| 128 |
if name in FAST_FAIL: |
| 129 |
exit(status) |
| 130 |
return status |
| 131 |
|
| 132 |
|
| 133 |
def start_background_cmd(name: str) -> Popen: |
| 134 |
cmd = cmds[name] |
| 135 |
proc = subprocess.Popen(cmd, stderr=subprocess.STDOUT, stdout=subprocess.PIPE) |
| 136 |
return proc |
| 137 |
|
| 138 |
|
| 139 |
def wait_background_cmd(name: str, proc: Popen) -> int: |
| 140 |
output = proc.communicate()[0] |
| 141 |
status = proc.returncode |
| 142 |
print(f"run {name}: {cmds[name]}") |
| 143 |
if status: |
| 144 |
print(output.decode().rstrip()) |
| 145 |
print("\nFAILED:", name) |
| 146 |
if name in FAST_FAIL: |
| 147 |
exit(status) |
| 148 |
return status |
| 149 |
|
| 150 |
|
| 151 |
def main() -> None: |
| 152 |
prog, *args = argv |
| 153 |
|
| 154 |
if not set(args).issubset(cmds): |
| 155 |
print( |
| 156 |
"usage:", |
| 157 |
prog, |
| 158 |
" ".join(f"[{k}]" for k in cmds), |
| 159 |
"[names of individual tests and files...]", |
| 160 |
) |
| 161 |
print() |
| 162 |
print( |
| 163 |
"Run the given tests. If given no arguments, run everything except" |
| 164 |
+ " pytest-extra and mypyc-extra. Unrecognized arguments will be" |
| 165 |
+ " interpreted as individual test names / substring expressions" |
| 166 |
+ " (or, if they end in .test, individual test files)" |
| 167 |
+ " and this script will try to run them." |
| 168 |
) |
| 169 |
if "-h" in args or "--help" in args: |
| 170 |
exit(1) |
| 171 |
|
| 172 |
if not args: |
| 173 |
args = DEFAULT_COMMANDS.copy() |
| 174 |
|
| 175 |
status = 0 |
| 176 |
|
| 177 |
if "self" in args and "lint" in args: |
| 178 |
# Perform lint and self check in parallel as it's faster. |
| 179 |
proc = start_background_cmd("lint") |
| 180 |
cmd_status = run_cmd("self") |
| 181 |
if cmd_status: |
| 182 |
status = cmd_status |
| 183 |
cmd_status = wait_background_cmd("lint", proc) |
| 184 |
if cmd_status: |
| 185 |
status = cmd_status |
| 186 |
args = [arg for arg in args if arg not in ("self", "lint")] |
| 187 |
|
| 188 |
for arg in args: |
| 189 |
cmd_status = run_cmd(arg) |
| 190 |
if cmd_status: |
| 191 |
status = cmd_status |
| 192 |
|
| 193 |
exit(status) |
| 194 |
|
| 195 |
|
| 196 |
if __name__ == "__main__": |
| 197 |
main() |