Runs Mypy in a subprocess, and ensures that `--output=json` works as intended.
(testcase: DataDrivenTestCase)
| 24 | |
| 25 | |
| 26 | def test_output_json(testcase: DataDrivenTestCase) -> None: |
| 27 | """Runs Mypy in a subprocess, and ensures that `--output=json` works as intended.""" |
| 28 | program_text = "\n".join(testcase.input) |
| 29 | flags_match = re.search("# flags: (.*)$", program_text, flags=re.MULTILINE) |
| 30 | if flags_match is not None: |
| 31 | mypy_cmdline = flags_match.group(1).split() |
| 32 | else: |
| 33 | mypy_cmdline = ["--output=json"] |
| 34 | mypy_cmdline.append(f"--python-version={'.'.join(map(str, PYTHON3_VERSION))}") |
| 35 | |
| 36 | # Write the program to a file. |
| 37 | program_path = os.path.join(test_temp_dir, "main") |
| 38 | mypy_cmdline.append(program_path) |
| 39 | with open(program_path, "w", encoding="utf8") as file: |
| 40 | for s in testcase.input: |
| 41 | file.write(f"{s}\n") |
| 42 | |
| 43 | output = [] |
| 44 | # Type check the program. |
| 45 | out, err, returncode = api.run(mypy_cmdline) |
| 46 | # split lines, remove newlines, and remove directory of test case |
| 47 | for line in (out + err).rstrip("\n").splitlines(): |
| 48 | if line.startswith(test_temp_dir + os.sep): |
| 49 | output.append(line[len(test_temp_dir + os.sep) :].rstrip("\r\n")) |
| 50 | else: |
| 51 | output.append(line.rstrip("\r\n")) |
| 52 | |
| 53 | if returncode > 1: |
| 54 | output.append("!!! Mypy crashed !!!") |
| 55 | |
| 56 | # Remove temp file. |
| 57 | os.remove(program_path) |
| 58 | |
| 59 | # JSON encodes every `\` character into `\\`, so we need to remove `\\` from windows paths |
| 60 | # and `/` from POSIX paths |
| 61 | json_os_separator = os.sep.replace("\\", "\\\\") |
| 62 | normalized_output = [line.replace(test_temp_dir + json_os_separator, "") for line in output] |
| 63 | |
| 64 | assert normalized_output == testcase.output |
no test coverage detected
searching dependent graphs…