|
1 | 1 | #!/usr/bin/env python3
|
| 2 | +import subprocess |
| 3 | +from subprocess import Popen |
2 | 4 | from os import system
|
3 | 5 | from sys import argv, exit, platform, executable, version_info
|
4 | 6 |
|
5 |
| -prog, *args = argv |
6 |
| - |
7 | 7 |
|
8 | 8 | # Use the Python provided to execute the script, or fall back to a sane default
|
9 | 9 | if version_info >= (3, 5, 0):
|
|
25 | 25 | STUBGEN_PY = 'StubgenPythonSuite'
|
26 | 26 | MYPYC_RUN = 'TestRun'
|
27 | 27 | MYPYC_RUN_MULTI = 'TestRunMultiFile'
|
| 28 | +MYPYC_EXTERNAL = 'TestExternal' |
| 29 | +MYPYC_COMMAND_LINE = 'TestCommandLine' |
| 30 | +ERROR_STREAM = 'ErrorStreamSuite' |
28 | 31 |
|
29 | 32 |
|
30 | 33 | ALL_NON_FAST = [CMDLINE,
|
|
36 | 39 | STUBGEN_CMD,
|
37 | 40 | STUBGEN_PY,
|
38 | 41 | MYPYC_RUN,
|
39 |
| - MYPYC_RUN_MULTI] |
| 42 | + MYPYC_RUN_MULTI, |
| 43 | + MYPYC_EXTERNAL, |
| 44 | + MYPYC_COMMAND_LINE, |
| 45 | + ERROR_STREAM] |
40 | 46 |
|
41 | 47 | # We split the pytest run into three parts to improve test
|
42 | 48 | # parallelization. Each run should have tests that each take a roughly similar
|
|
55 | 61 | STUBGEN_PY]),
|
56 | 62 | # Test cases that may take seconds to run each
|
57 | 63 | 'pytest-slow': 'pytest -k "%s"' % ' or '.join(
|
58 |
| - [SAMPLES, TYPESHED, PEP561, DAEMON, MYPYC_RUN, MYPYC_RUN_MULTI]), |
| 64 | + [SAMPLES, |
| 65 | + TYPESHED, |
| 66 | + PEP561, |
| 67 | + DAEMON, |
| 68 | + MYPYC_RUN, |
| 69 | + MYPYC_RUN_MULTI, |
| 70 | + MYPYC_EXTERNAL, |
| 71 | + MYPYC_COMMAND_LINE, |
| 72 | + ERROR_STREAM]), |
59 | 73 | }
|
60 | 74 |
|
61 | 75 | # Stop run immediately if these commands fail
|
62 | 76 | FAST_FAIL = ['self', 'lint']
|
63 | 77 |
|
64 | 78 | assert all(cmd in cmds for cmd in FAST_FAIL)
|
65 | 79 |
|
66 |
| -if not set(args).issubset(cmds): |
67 |
| - print("usage:", prog, " ".join('[%s]' % k for k in cmds)) |
68 |
| - exit(1) |
69 |
| - |
70 |
| -if not args: |
71 |
| - args = list(cmds) |
72 |
| - |
73 |
| -status = 0 |
74 | 80 |
|
75 |
| -for arg in args: |
76 |
| - cmd = cmds[arg] |
77 |
| - print('run %s: %s' % (arg, cmd)) |
| 81 | +def run_cmd(name: str) -> int: |
| 82 | + status = 0 |
| 83 | + cmd = cmds[name] |
| 84 | + print('run %s: %s' % (name, cmd)) |
78 | 85 | res = (system(cmd) & 0x7F00) >> 8
|
79 | 86 | if res:
|
80 |
| - print('\nFAILED: %s' % arg) |
| 87 | + print('\nFAILED: %s' % name) |
81 | 88 | status = res
|
82 |
| - if arg in FAST_FAIL: |
| 89 | + if name in FAST_FAIL: |
83 | 90 | exit(status)
|
| 91 | + return status |
| 92 | + |
| 93 | + |
| 94 | +def start_background_cmd(name: str) -> Popen: |
| 95 | + cmd = cmds[name] |
| 96 | + proc = subprocess.Popen(cmd, |
| 97 | + shell=True, |
| 98 | + stderr=subprocess.STDOUT, |
| 99 | + stdout=subprocess.PIPE) |
| 100 | + return proc |
| 101 | + |
| 102 | + |
| 103 | +def wait_background_cmd(name: str, proc: Popen) -> int: |
| 104 | + output = proc.communicate()[0] |
| 105 | + status = proc.returncode |
| 106 | + print('run %s: %s' % (name, cmds[name])) |
| 107 | + if status: |
| 108 | + print(output.decode().rstrip()) |
| 109 | + print('\nFAILED: %s' % name) |
| 110 | + if name in FAST_FAIL: |
| 111 | + exit(status) |
| 112 | + return status |
| 113 | + |
| 114 | + |
| 115 | +def main() -> None: |
| 116 | + prog, *args = argv |
| 117 | + |
| 118 | + if not set(args).issubset(cmds): |
| 119 | + print("usage:", prog, " ".join('[%s]' % k for k in cmds)) |
| 120 | + exit(1) |
| 121 | + |
| 122 | + if not args: |
| 123 | + args = list(cmds) |
| 124 | + |
| 125 | + status = 0 |
| 126 | + |
| 127 | + if 'self' in args and 'lint' in args: |
| 128 | + # Perform lint and self check in parallel as it's faster. |
| 129 | + proc = start_background_cmd('lint') |
| 130 | + cmd_status = run_cmd('self') |
| 131 | + if cmd_status: |
| 132 | + status = cmd_status |
| 133 | + cmd_status = wait_background_cmd('lint', proc) |
| 134 | + if cmd_status: |
| 135 | + status = cmd_status |
| 136 | + args = [arg for arg in args if arg not in ('self', 'lint')] |
| 137 | + |
| 138 | + for arg in args: |
| 139 | + cmd_status = run_cmd(arg) |
| 140 | + if cmd_status: |
| 141 | + status = cmd_status |
| 142 | + |
| 143 | + exit(status) |
| 144 | + |
84 | 145 |
|
85 |
| -exit(status) |
| 146 | +if __name__ == '__main__': |
| 147 | + main() |
0 commit comments