testerrorstream.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. """Tests for mypy incremental error output."""
  2. from __future__ import annotations
  3. from mypy import build
  4. from mypy.errors import CompileError
  5. from mypy.modulefinder import BuildSource
  6. from mypy.options import Options
  7. from mypy.test.data import DataDrivenTestCase, DataSuite
  8. from mypy.test.helpers import assert_string_arrays_equal
  9. class ErrorStreamSuite(DataSuite):
  10. required_out_section = True
  11. base_path = "."
  12. files = ["errorstream.test"]
  13. def run_case(self, testcase: DataDrivenTestCase) -> None:
  14. test_error_stream(testcase)
  15. def test_error_stream(testcase: DataDrivenTestCase) -> None:
  16. """Perform a single error streaming test case.
  17. The argument contains the description of the test case.
  18. """
  19. options = Options()
  20. options.show_traceback = True
  21. options.hide_error_codes = True
  22. logged_messages: list[str] = []
  23. def flush_errors(msgs: list[str], serious: bool) -> None:
  24. if msgs:
  25. logged_messages.append("==== Errors flushed ====")
  26. logged_messages.extend(msgs)
  27. sources = [BuildSource("main", "__main__", "\n".join(testcase.input))]
  28. try:
  29. build.build(sources=sources, options=options, flush_errors=flush_errors)
  30. except CompileError as e:
  31. assert e.messages == []
  32. assert_string_arrays_equal(
  33. testcase.output, logged_messages, f"Invalid output ({testcase.file}, line {testcase.line})"
  34. )