| """Test cases for `--output=json`. |
| |
| These cannot be run by the usual unit test runner because of the backslashes in |
| the output, which get normalized to forward slashes by the test suite on Windows. |
| """ |
| |
| from __future__ import annotations |
| |
| import os |
| import os.path |
| |
| from mypy import api |
| from mypy.defaults import PYTHON3_VERSION |
| from mypy.test.config import test_temp_dir |
| from mypy.test.data import DataDrivenTestCase, DataSuite |
| |
| |
| class OutputJSONsuite(DataSuite): |
| files = ["outputjson.test"] |
| |
| def run_case(self, testcase: DataDrivenTestCase) -> None: |
| test_output_json(testcase) |
| |
| |
| def test_output_json(testcase: DataDrivenTestCase) -> None: |
| """Runs Mypy in a subprocess, and ensures that `--output=json` works as intended.""" |
| mypy_cmdline = ["--output=json"] |
| mypy_cmdline.append(f"--python-version={'.'.join(map(str, PYTHON3_VERSION))}") |
| |
| # Write the program to a file. |
| program_path = os.path.join(test_temp_dir, "main") |
| mypy_cmdline.append(program_path) |
| with open(program_path, "w", encoding="utf8") as file: |
| for s in testcase.input: |
| file.write(f"{s}\n") |
| |
| output = [] |
| # Type check the program. |
| out, err, returncode = api.run(mypy_cmdline) |
| # split lines, remove newlines, and remove directory of test case |
| for line in (out + err).rstrip("\n").splitlines(): |
| if line.startswith(test_temp_dir + os.sep): |
| output.append(line[len(test_temp_dir + os.sep) :].rstrip("\r\n")) |
| else: |
| output.append(line.rstrip("\r\n")) |
| |
| if returncode > 1: |
| output.append("!!! Mypy crashed !!!") |
| |
| # Remove temp file. |
| os.remove(program_path) |
| |
| # JSON encodes every `\` character into `\\`, so we need to remove `\\` from windows paths |
| # and `/` from POSIX paths |
| json_os_separator = os.sep.replace("\\", "\\\\") |
| normalized_output = [line.replace(test_temp_dir + json_os_separator, "") for line in output] |
| |
| assert normalized_output == testcase.output |