testfinegrained.py 17 KB

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