| 1 |
"""Test cases for invoking mypyc on the command line. |
| 2 |
|
| 3 |
These are slow -- do not add test cases unless you have a very good reason to do so. |
| 4 |
""" |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
import glob |
| 9 |
import os |
| 10 |
import os.path |
| 11 |
import re |
| 12 |
import subprocess |
| 13 |
import sys |
| 14 |
|
| 15 |
from mypy.test.config import test_temp_dir |
| 16 |
from mypy.test.data import DataDrivenTestCase |
| 17 |
from mypy.test.helpers import normalize_error_messages |
| 18 |
from mypyc.test.testutil import MypycDataSuite, assert_test_output |
| 19 |
|
| 20 |
files = ["commandline.test"] |
| 21 |
|
| 22 |
|
| 23 |
base_path = os.path.join(os.path.dirname(__file__), "..", "..") |
| 24 |
|
| 25 |
python3_path = sys.executable |
| 26 |
|
| 27 |
|
| 28 |
class TestCommandLine(MypycDataSuite): |
| 29 |
files = files |
| 30 |
base_path = test_temp_dir |
| 31 |
optional_out = True |
| 32 |
|
| 33 |
def run_case(self, testcase: DataDrivenTestCase) -> None: |
| 34 |
# Parse options from test case description (arguments must not have spaces) |
| 35 |
text = "\n".join(testcase.input) |
| 36 |
m = re.search(r"# *cmd: *(.*)", text) |
| 37 |
assert m is not None, 'Test case missing "# cmd: <files>" section' |
| 38 |
args = m.group(1).split() |
| 39 |
|
| 40 |
# Write main program to run (not compiled) |
| 41 |
program = "_%s.py" % testcase.name |
| 42 |
program_path = os.path.join(test_temp_dir, program) |
| 43 |
with open(program_path, "w") as f: |
| 44 |
f.write(text) |
| 45 |
|
| 46 |
env = os.environ.copy() |
| 47 |
env["PYTHONPATH"] = base_path |
| 48 |
|
| 49 |
out = b"" |
| 50 |
try: |
| 51 |
# Compile program |
| 52 |
cmd = subprocess.run( |
| 53 |
[sys.executable, "-m", "mypyc", *args], |
| 54 |
stdout=subprocess.PIPE, |
| 55 |
stderr=subprocess.STDOUT, |
| 56 |
cwd="tmp", |
| 57 |
env=env, |
| 58 |
) |
| 59 |
if "ErrorOutput" in testcase.name or cmd.returncode != 0: |
| 60 |
out += cmd.stdout |
| 61 |
elif "WarningOutput" in testcase.name: |
| 62 |
# Strip out setuptools build related output since we're only |
| 63 |
# interested in the messages emitted during compilation. |
| 64 |
messages, _, _ = cmd.stdout.partition(b"running build_ext") |
| 65 |
out += messages |
| 66 |
|
| 67 |
if cmd.returncode == 0: |
| 68 |
# Run main program |
| 69 |
out += subprocess.check_output([python3_path, program], cwd="tmp") |
| 70 |
finally: |
| 71 |
suffix = "pyd" if sys.platform == "win32" else "so" |
| 72 |
so_paths = glob.glob(f"tmp/**/*.{suffix}", recursive=True) |
| 73 |
for path in so_paths: |
| 74 |
os.remove(path) |
| 75 |
|
| 76 |
# Strip out 'tmp/' from error message paths in the testcase output, |
| 77 |
# due to a mismatch between this test and mypy's test suite. |
| 78 |
expected = [x.replace("tmp/", "") for x in testcase.output] |
| 79 |
|
| 80 |
# Verify output |
| 81 |
actual = normalize_error_messages(out.decode().splitlines()) |
| 82 |
assert_test_output(testcase, actual, "Invalid output", expected=expected) |