testfinegrained.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. """Test cases for fine-grained incremental checking.
  2. Each test cases runs a batch build followed by one or more fine-grained
  3. incremental steps. We verify that each step produces the expected output.
  4. See the comment at the top of test-data/unit/fine-grained.test for more
  5. information.
  6. N.B.: Unlike most of the other test suites, testfinegrained does not
  7. rely on an alt_lib_path for finding source files. This means that they
  8. can test interactions with the lib_path that is built implicitly based
  9. on specified sources.
  10. """
  11. from __future__ import annotations
  12. import os
  13. import re
  14. import sys
  15. import unittest
  16. from typing import Any
  17. import pytest
  18. from mypy import build
  19. from mypy.config_parser import parse_config_file
  20. from mypy.dmypy_server import Server
  21. from mypy.dmypy_util import DEFAULT_STATUS_FILE
  22. from mypy.errors import CompileError
  23. from mypy.find_sources import create_source_list
  24. from mypy.modulefinder import BuildSource
  25. from mypy.options import TYPE_VAR_TUPLE, UNPACK, Options
  26. from mypy.server.mergecheck import check_consistency
  27. from mypy.server.update import sort_messages_preserving_file_order
  28. from mypy.test.config import test_temp_dir
  29. from mypy.test.data import DataDrivenTestCase, DataSuite, DeleteFile, UpdateFile
  30. from mypy.test.helpers import (
  31. assert_module_equivalence,
  32. assert_string_arrays_equal,
  33. assert_target_equivalence,
  34. find_test_files,
  35. parse_options,
  36. perform_file_operations,
  37. )
  38. # Set to True to perform (somewhat expensive) checks for duplicate AST nodes after merge
  39. CHECK_CONSISTENCY = False
  40. class FineGrainedSuite(DataSuite):
  41. files = find_test_files(
  42. pattern="fine-grained*.test", exclude=["fine-grained-cache-incremental.test"]
  43. )
  44. # Whether to use the fine-grained cache in the testing. This is overridden
  45. # by a trivial subclass to produce a suite that uses the cache.
  46. use_cache = False
  47. def should_skip(self, testcase: DataDrivenTestCase) -> bool:
  48. # Decide whether to skip the test. This could have been structured
  49. # as a filter() classmethod also, but we want the tests reported
  50. # as skipped, not just elided.
  51. if self.use_cache:
  52. if testcase.only_when == "-only_when_nocache":
  53. return True
  54. # TODO: In caching mode we currently don't well support
  55. # starting from cached states with errors in them.
  56. if testcase.output and testcase.output[0] != "==":
  57. return True
  58. else:
  59. if testcase.only_when == "-only_when_cache":
  60. return True
  61. if "Inspect" in testcase.name and sys.version_info < (3, 8):
  62. return True
  63. return False
  64. def run_case(self, testcase: DataDrivenTestCase) -> None:
  65. if self.should_skip(testcase):
  66. pytest.skip()
  67. return
  68. main_src = "\n".join(testcase.input)
  69. main_path = os.path.join(test_temp_dir, "main")
  70. with open(main_path, "w", encoding="utf8") as f:
  71. f.write(main_src)
  72. options = self.get_options(main_src, testcase, build_cache=False)
  73. build_options = self.get_options(main_src, testcase, build_cache=True)
  74. server = Server(options, DEFAULT_STATUS_FILE)
  75. num_regular_incremental_steps = self.get_build_steps(main_src)
  76. step = 1
  77. sources = self.parse_sources(main_src, step, options)
  78. if step <= num_regular_incremental_steps:
  79. messages = self.build(build_options, sources)
  80. else:
  81. messages = self.run_check(server, sources)
  82. a = []
  83. if messages:
  84. a.extend(normalize_messages(messages))
  85. assert testcase.tmpdir
  86. a.extend(self.maybe_suggest(step, server, main_src, testcase.tmpdir.name))
  87. a.extend(self.maybe_inspect(step, server, main_src))
  88. if server.fine_grained_manager:
  89. if CHECK_CONSISTENCY:
  90. check_consistency(server.fine_grained_manager)
  91. steps = testcase.find_steps()
  92. all_triggered = []
  93. for operations in steps:
  94. step += 1
  95. output, triggered = self.perform_step(
  96. operations,
  97. server,
  98. options,
  99. build_options,
  100. testcase,
  101. main_src,
  102. step,
  103. num_regular_incremental_steps,
  104. )
  105. a.append("==")
  106. a.extend(output)
  107. all_triggered.extend(triggered)
  108. # Normalize paths in test output (for Windows).
  109. a = [line.replace("\\", "/") for line in a]
  110. assert_string_arrays_equal(
  111. testcase.output, a, f"Invalid output ({testcase.file}, line {testcase.line})"
  112. )
  113. if testcase.triggered:
  114. assert_string_arrays_equal(
  115. testcase.triggered,
  116. self.format_triggered(all_triggered),
  117. f"Invalid active triggers ({testcase.file}, line {testcase.line})",
  118. )
  119. def get_options(self, source: str, testcase: DataDrivenTestCase, build_cache: bool) -> Options:
  120. # This handles things like '# flags: --foo'.
  121. options = parse_options(source, testcase, incremental_step=1)
  122. options.incremental = True
  123. options.use_builtins_fixtures = True
  124. options.show_traceback = True
  125. options.error_summary = False
  126. options.fine_grained_incremental = not build_cache
  127. options.use_fine_grained_cache = self.use_cache and not build_cache
  128. options.cache_fine_grained = self.use_cache
  129. options.local_partial_types = True
  130. options.enable_incomplete_feature = [TYPE_VAR_TUPLE, UNPACK]
  131. # Treat empty bodies safely for these test cases.
  132. options.allow_empty_bodies = not testcase.name.endswith("_no_empty")
  133. if re.search("flags:.*--follow-imports", source) is None:
  134. # Override the default for follow_imports
  135. options.follow_imports = "error"
  136. for name, _ in testcase.files:
  137. if "mypy.ini" in name or "pyproject.toml" in name:
  138. parse_config_file(options, lambda: None, name)
  139. break
  140. return options
  141. def run_check(self, server: Server, sources: list[BuildSource]) -> list[str]:
  142. response = server.check(sources, export_types=True, is_tty=False, terminal_width=-1)
  143. out = response["out"] or response["err"]
  144. assert isinstance(out, str)
  145. return out.splitlines()
  146. def build(self, options: Options, sources: list[BuildSource]) -> list[str]:
  147. try:
  148. result = build.build(sources=sources, options=options)
  149. except CompileError as e:
  150. return e.messages
  151. return result.errors
  152. def format_triggered(self, triggered: list[list[str]]) -> list[str]:
  153. result = []
  154. for n, triggers in enumerate(triggered):
  155. filtered = [trigger for trigger in triggers if not trigger.endswith("__>")]
  156. filtered = sorted(filtered)
  157. result.append(("%d: %s" % (n + 2, ", ".join(filtered))).strip())
  158. return result
  159. def get_build_steps(self, program_text: str) -> int:
  160. """Get the number of regular incremental steps to run, from the test source"""
  161. if not self.use_cache:
  162. return 0
  163. m = re.search("# num_build_steps: ([0-9]+)$", program_text, flags=re.MULTILINE)
  164. if m is not None:
  165. return int(m.group(1))
  166. return 1
  167. def perform_step(
  168. self,
  169. operations: list[UpdateFile | DeleteFile],
  170. server: Server,
  171. options: Options,
  172. build_options: Options,
  173. testcase: DataDrivenTestCase,
  174. main_src: str,
  175. step: int,
  176. num_regular_incremental_steps: int,
  177. ) -> tuple[list[str], list[list[str]]]:
  178. """Perform one fine-grained incremental build step (after some file updates/deletions).
  179. Return (mypy output, triggered targets).
  180. """
  181. perform_file_operations(operations)
  182. sources = self.parse_sources(main_src, step, options)
  183. if step <= num_regular_incremental_steps:
  184. new_messages = self.build(build_options, sources)
  185. else:
  186. new_messages = self.run_check(server, sources)
  187. updated: list[str] = []
  188. changed: list[str] = []
  189. targets: list[str] = []
  190. triggered = []
  191. if server.fine_grained_manager:
  192. if CHECK_CONSISTENCY:
  193. check_consistency(server.fine_grained_manager)
  194. triggered.append(server.fine_grained_manager.triggered)
  195. updated = server.fine_grained_manager.updated_modules
  196. changed = [mod for mod, file in server.fine_grained_manager.changed_modules]
  197. targets = server.fine_grained_manager.processed_targets
  198. expected_stale = testcase.expected_stale_modules.get(step - 1)
  199. if expected_stale is not None:
  200. assert_module_equivalence("stale" + str(step - 1), expected_stale, changed)
  201. expected_rechecked = testcase.expected_rechecked_modules.get(step - 1)
  202. if expected_rechecked is not None:
  203. assert_module_equivalence("rechecked" + str(step - 1), expected_rechecked, updated)
  204. expected = testcase.expected_fine_grained_targets.get(step)
  205. if expected:
  206. assert_target_equivalence("targets" + str(step), expected, targets)
  207. new_messages = normalize_messages(new_messages)
  208. a = new_messages
  209. assert testcase.tmpdir
  210. a.extend(self.maybe_suggest(step, server, main_src, testcase.tmpdir.name))
  211. a.extend(self.maybe_inspect(step, server, main_src))
  212. return a, triggered
  213. def parse_sources(
  214. self, program_text: str, incremental_step: int, options: Options
  215. ) -> list[BuildSource]:
  216. """Return target BuildSources for a test case.
  217. Normally, the unit tests will check all files included in the test
  218. case. This differs from how testcheck works by default, as dmypy
  219. doesn't currently support following imports.
  220. You can override this behavior and instruct the tests to check
  221. multiple modules by using a comment like this in the test case
  222. input:
  223. # cmd: main a.py
  224. You can also use `# cmdN:` to have a different cmd for incremental
  225. step N (2, 3, ...).
  226. """
  227. m = re.search("# cmd: mypy ([a-zA-Z0-9_./ ]+)$", program_text, flags=re.MULTILINE)
  228. regex = f"# cmd{incremental_step}: mypy ([a-zA-Z0-9_./ ]+)$"
  229. alt_m = re.search(regex, program_text, flags=re.MULTILINE)
  230. if alt_m is not None:
  231. # Optionally return a different command if in a later step
  232. # of incremental mode, otherwise default to reusing the
  233. # original cmd.
  234. m = alt_m
  235. if m:
  236. # The test case wants to use a non-default set of files.
  237. paths = [os.path.join(test_temp_dir, path) for path in m.group(1).strip().split()]
  238. return create_source_list(paths, options)
  239. else:
  240. base = BuildSource(os.path.join(test_temp_dir, "main"), "__main__", None)
  241. # Use expand_dir instead of create_source_list to avoid complaints
  242. # when there aren't any .py files in an increment
  243. return [base] + create_source_list([test_temp_dir], options, allow_empty_dir=True)
  244. def maybe_suggest(self, step: int, server: Server, src: str, tmp_dir: str) -> list[str]:
  245. output: list[str] = []
  246. targets = self.get_suggest(src, step)
  247. for flags, target in targets:
  248. json = "--json" in flags
  249. callsites = "--callsites" in flags
  250. no_any = "--no-any" in flags
  251. no_errors = "--no-errors" in flags
  252. m = re.match("--flex-any=([0-9.]+)", flags)
  253. flex_any = float(m.group(1)) if m else None
  254. m = re.match(r"--use-fixme=(\w+)", flags)
  255. use_fixme = m.group(1) if m else None
  256. m = re.match("--max-guesses=([0-9]+)", flags)
  257. max_guesses = int(m.group(1)) if m else None
  258. res: dict[str, Any] = server.cmd_suggest(
  259. target.strip(),
  260. json=json,
  261. no_any=no_any,
  262. no_errors=no_errors,
  263. flex_any=flex_any,
  264. use_fixme=use_fixme,
  265. callsites=callsites,
  266. max_guesses=max_guesses,
  267. )
  268. val = res["error"] if "error" in res else res["out"] + res["err"]
  269. if json:
  270. # JSON contains already escaped \ on Windows, so requires a bit of care.
  271. val = val.replace("\\\\", "\\")
  272. val = val.replace(os.path.realpath(tmp_dir) + os.path.sep, "")
  273. output.extend(val.strip().split("\n"))
  274. return normalize_messages(output)
  275. def maybe_inspect(self, step: int, server: Server, src: str) -> list[str]:
  276. output: list[str] = []
  277. targets = self.get_inspect(src, step)
  278. for flags, location in targets:
  279. m = re.match(r"--show=(\w+)", flags)
  280. show = m.group(1) if m else "type"
  281. verbosity = 0
  282. if "-v" in flags:
  283. verbosity = 1
  284. if "-vv" in flags:
  285. verbosity = 2
  286. m = re.match(r"--limit=([0-9]+)", flags)
  287. limit = int(m.group(1)) if m else 0
  288. include_span = "--include-span" in flags
  289. include_kind = "--include-kind" in flags
  290. include_object_attrs = "--include-object-attrs" in flags
  291. union_attrs = "--union-attrs" in flags
  292. force_reload = "--force-reload" in flags
  293. res: dict[str, Any] = server.cmd_inspect(
  294. show,
  295. location,
  296. verbosity=verbosity,
  297. limit=limit,
  298. include_span=include_span,
  299. include_kind=include_kind,
  300. include_object_attrs=include_object_attrs,
  301. union_attrs=union_attrs,
  302. force_reload=force_reload,
  303. )
  304. val = res["error"] if "error" in res else res["out"] + res["err"]
  305. output.extend(val.strip().split("\n"))
  306. return normalize_messages(output)
  307. def get_suggest(self, program_text: str, incremental_step: int) -> list[tuple[str, str]]:
  308. step_bit = "1?" if incremental_step == 1 else str(incremental_step)
  309. regex = f"# suggest{step_bit}: (--[a-zA-Z0-9_\\-./=?^ ]+ )*([a-zA-Z0-9_.:/?^ ]+)$"
  310. m = re.findall(regex, program_text, flags=re.MULTILINE)
  311. return m
  312. def get_inspect(self, program_text: str, incremental_step: int) -> list[tuple[str, str]]:
  313. step_bit = "1?" if incremental_step == 1 else str(incremental_step)
  314. regex = f"# inspect{step_bit}: (--[a-zA-Z0-9_\\-=?^ ]+ )*([a-zA-Z0-9_.:/?^ ]+)$"
  315. m = re.findall(regex, program_text, flags=re.MULTILINE)
  316. return m
  317. def normalize_messages(messages: list[str]) -> list[str]:
  318. return [re.sub("^tmp" + re.escape(os.sep), "", message) for message in messages]
  319. class TestMessageSorting(unittest.TestCase):
  320. def test_simple_sorting(self) -> None:
  321. msgs = ['x.py:1: error: "int" not callable', 'foo/y.py:123: note: "X" not defined']
  322. old_msgs = ['foo/y.py:12: note: "Y" not defined', 'x.py:8: error: "str" not callable']
  323. assert sort_messages_preserving_file_order(msgs, old_msgs) == list(reversed(msgs))
  324. assert sort_messages_preserving_file_order(list(reversed(msgs)), old_msgs) == list(
  325. reversed(msgs)
  326. )
  327. def test_long_form_sorting(self) -> None:
  328. # Multi-line errors should be sorted together and not split.
  329. msg1 = [
  330. 'x.py:1: error: "int" not callable',
  331. "and message continues (x: y)",
  332. " 1()",
  333. " ^~~",
  334. ]
  335. msg2 = [
  336. 'foo/y.py: In function "f":',
  337. 'foo/y.py:123: note: "X" not defined',
  338. "and again message continues",
  339. ]
  340. old_msgs = ['foo/y.py:12: note: "Y" not defined', 'x.py:8: error: "str" not callable']
  341. assert sort_messages_preserving_file_order(msg1 + msg2, old_msgs) == msg2 + msg1
  342. assert sort_messages_preserving_file_order(msg2 + msg1, old_msgs) == msg2 + msg1
  343. def test_mypy_error_prefix(self) -> None:
  344. # Some errors don't have a file and start with "mypy: ". These
  345. # shouldn't be sorted together with file-specific errors.
  346. msg1 = 'x.py:1: error: "int" not callable'
  347. msg2 = 'foo/y:123: note: "X" not defined'
  348. msg3 = "mypy: Error not associated with a file"
  349. old_msgs = [
  350. "mypy: Something wrong",
  351. 'foo/y:12: note: "Y" not defined',
  352. 'x.py:8: error: "str" not callable',
  353. ]
  354. assert sort_messages_preserving_file_order([msg1, msg2, msg3], old_msgs) == [
  355. msg2,
  356. msg1,
  357. msg3,
  358. ]
  359. assert sort_messages_preserving_file_order([msg3, msg2, msg1], old_msgs) == [
  360. msg2,
  361. msg1,
  362. msg3,
  363. ]
  364. def test_new_file_at_the_end(self) -> None:
  365. msg1 = 'x.py:1: error: "int" not callable'
  366. msg2 = 'foo/y.py:123: note: "X" not defined'
  367. new1 = "ab.py:3: error: Problem: error"
  368. new2 = "aaa:3: error: Bad"
  369. old_msgs = ['foo/y.py:12: note: "Y" not defined', 'x.py:8: error: "str" not callable']
  370. assert sort_messages_preserving_file_order([msg1, msg2, new1], old_msgs) == [
  371. msg2,
  372. msg1,
  373. new1,
  374. ]
  375. assert sort_messages_preserving_file_order([new1, msg1, msg2, new2], old_msgs) == [
  376. msg2,
  377. msg1,
  378. new1,
  379. new2,
  380. ]