helpers.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. from __future__ import annotations
  2. import contextlib
  3. import os
  4. import pathlib
  5. import re
  6. import shutil
  7. import sys
  8. import time
  9. from typing import Any, Callable, Iterable, Iterator, Pattern
  10. # Exporting Suite as alias to TestCase for backwards compatibility
  11. # TODO: avoid aliasing - import and subclass TestCase directly
  12. from unittest import TestCase
  13. Suite = TestCase # re-exporting
  14. import pytest
  15. import mypy.api as api
  16. import mypy.version
  17. from mypy import defaults
  18. from mypy.main import process_options
  19. from mypy.options import Options
  20. from mypy.test.config import test_data_prefix, test_temp_dir
  21. from mypy.test.data import DataDrivenTestCase, DeleteFile, UpdateFile, fix_cobertura_filename
  22. skip = pytest.mark.skip
  23. # AssertStringArraysEqual displays special line alignment helper messages if
  24. # the first different line has at least this many characters,
  25. MIN_LINE_LENGTH_FOR_ALIGNMENT = 5
  26. def run_mypy(args: list[str]) -> None:
  27. __tracebackhide__ = True
  28. # We must enable site packages even though they could cause problems,
  29. # since stubs for typing_extensions live there.
  30. outval, errval, status = api.run(args + ["--show-traceback", "--no-silence-site-packages"])
  31. if status != 0:
  32. sys.stdout.write(outval)
  33. sys.stderr.write(errval)
  34. pytest.fail(msg="Sample check failed", pytrace=False)
  35. def assert_string_arrays_equal(expected: list[str], actual: list[str], msg: str) -> None:
  36. """Assert that two string arrays are equal.
  37. We consider "can't" and "cannot" equivalent, by replacing the
  38. former with the latter before comparing.
  39. Display any differences in a human-readable form.
  40. """
  41. actual = clean_up(actual)
  42. actual = [line.replace("can't", "cannot") for line in actual]
  43. expected = [line.replace("can't", "cannot") for line in expected]
  44. if actual != expected:
  45. num_skip_start = num_skipped_prefix_lines(expected, actual)
  46. num_skip_end = num_skipped_suffix_lines(expected, actual)
  47. sys.stderr.write("Expected:\n")
  48. # If omit some lines at the beginning, indicate it by displaying a line
  49. # with '...'.
  50. if num_skip_start > 0:
  51. sys.stderr.write(" ...\n")
  52. # Keep track of the first different line.
  53. first_diff = -1
  54. # Display only this many first characters of identical lines.
  55. width = 75
  56. for i in range(num_skip_start, len(expected) - num_skip_end):
  57. if i >= len(actual) or expected[i] != actual[i]:
  58. if first_diff < 0:
  59. first_diff = i
  60. sys.stderr.write(f" {expected[i]:<45} (diff)")
  61. else:
  62. e = expected[i]
  63. sys.stderr.write(" " + e[:width])
  64. if len(e) > width:
  65. sys.stderr.write("...")
  66. sys.stderr.write("\n")
  67. if num_skip_end > 0:
  68. sys.stderr.write(" ...\n")
  69. sys.stderr.write("Actual:\n")
  70. if num_skip_start > 0:
  71. sys.stderr.write(" ...\n")
  72. for j in range(num_skip_start, len(actual) - num_skip_end):
  73. if j >= len(expected) or expected[j] != actual[j]:
  74. sys.stderr.write(f" {actual[j]:<45} (diff)")
  75. else:
  76. a = actual[j]
  77. sys.stderr.write(" " + a[:width])
  78. if len(a) > width:
  79. sys.stderr.write("...")
  80. sys.stderr.write("\n")
  81. if not actual:
  82. sys.stderr.write(" (empty)\n")
  83. if num_skip_end > 0:
  84. sys.stderr.write(" ...\n")
  85. sys.stderr.write("\n")
  86. if 0 <= first_diff < len(actual) and (
  87. len(expected[first_diff]) >= MIN_LINE_LENGTH_FOR_ALIGNMENT
  88. or len(actual[first_diff]) >= MIN_LINE_LENGTH_FOR_ALIGNMENT
  89. ):
  90. # Display message that helps visualize the differences between two
  91. # long lines.
  92. show_align_message(expected[first_diff], actual[first_diff])
  93. pytest.fail(msg, pytrace=False)
  94. def assert_module_equivalence(name: str, expected: Iterable[str], actual: Iterable[str]) -> None:
  95. expected_normalized = sorted(expected)
  96. actual_normalized = sorted(set(actual).difference({"__main__"}))
  97. assert_string_arrays_equal(
  98. expected_normalized,
  99. actual_normalized,
  100. ("Actual modules ({}) do not match expected modules ({}) " 'for "[{} ...]"').format(
  101. ", ".join(actual_normalized), ", ".join(expected_normalized), name
  102. ),
  103. )
  104. def assert_target_equivalence(name: str, expected: list[str], actual: list[str]) -> None:
  105. """Compare actual and expected targets (order sensitive)."""
  106. assert_string_arrays_equal(
  107. expected,
  108. actual,
  109. ("Actual targets ({}) do not match expected targets ({}) " 'for "[{} ...]"').format(
  110. ", ".join(actual), ", ".join(expected), name
  111. ),
  112. )
  113. def show_align_message(s1: str, s2: str) -> None:
  114. """Align s1 and s2 so that the their first difference is highlighted.
  115. For example, if s1 is 'foobar' and s2 is 'fobar', display the
  116. following lines:
  117. E: foobar
  118. A: fobar
  119. ^
  120. If s1 and s2 are long, only display a fragment of the strings around the
  121. first difference. If s1 is very short, do nothing.
  122. """
  123. # Seeing what went wrong is trivial even without alignment if the expected
  124. # string is very short. In this case do nothing to simplify output.
  125. if len(s1) < 4:
  126. return
  127. maxw = 72 # Maximum number of characters shown
  128. sys.stderr.write("Alignment of first line difference:\n")
  129. trunc = False
  130. while s1[:30] == s2[:30]:
  131. s1 = s1[10:]
  132. s2 = s2[10:]
  133. trunc = True
  134. if trunc:
  135. s1 = "..." + s1
  136. s2 = "..." + s2
  137. max_len = max(len(s1), len(s2))
  138. extra = ""
  139. if max_len > maxw:
  140. extra = "..."
  141. # Write a chunk of both lines, aligned.
  142. sys.stderr.write(f" E: {s1[:maxw]}{extra}\n")
  143. sys.stderr.write(f" A: {s2[:maxw]}{extra}\n")
  144. # Write an indicator character under the different columns.
  145. sys.stderr.write(" ")
  146. for j in range(min(maxw, max(len(s1), len(s2)))):
  147. if s1[j : j + 1] != s2[j : j + 1]:
  148. sys.stderr.write("^") # Difference
  149. break
  150. else:
  151. sys.stderr.write(" ") # Equal
  152. sys.stderr.write("\n")
  153. def clean_up(a: list[str]) -> list[str]:
  154. """Remove common directory prefix from all strings in a.
  155. This uses a naive string replace; it seems to work well enough. Also
  156. remove trailing carriage returns.
  157. """
  158. res = []
  159. pwd = os.getcwd()
  160. driver = pwd + "/driver.py"
  161. for s in a:
  162. prefix = os.sep
  163. ss = s
  164. for p in prefix, prefix.replace(os.sep, "/"):
  165. if p != "/" and p != "//" and p != "\\" and p != "\\\\":
  166. ss = ss.replace(p, "")
  167. # Ignore spaces at end of line.
  168. ss = re.sub(" +$", "", ss)
  169. # Remove pwd from driver.py's path
  170. ss = ss.replace(driver, "driver.py")
  171. res.append(re.sub("\\r$", "", ss))
  172. return res
  173. @contextlib.contextmanager
  174. def local_sys_path_set() -> Iterator[None]:
  175. """Temporary insert current directory into sys.path.
  176. This can be used by test cases that do runtime imports, for example
  177. by the stubgen tests.
  178. """
  179. old_sys_path = sys.path.copy()
  180. if not ("" in sys.path or "." in sys.path):
  181. sys.path.insert(0, "")
  182. try:
  183. yield
  184. finally:
  185. sys.path = old_sys_path
  186. def num_skipped_prefix_lines(a1: list[str], a2: list[str]) -> int:
  187. num_eq = 0
  188. while num_eq < min(len(a1), len(a2)) and a1[num_eq] == a2[num_eq]:
  189. num_eq += 1
  190. return max(0, num_eq - 4)
  191. def num_skipped_suffix_lines(a1: list[str], a2: list[str]) -> int:
  192. num_eq = 0
  193. while num_eq < min(len(a1), len(a2)) and a1[-num_eq - 1] == a2[-num_eq - 1]:
  194. num_eq += 1
  195. return max(0, num_eq - 4)
  196. def testfile_pyversion(path: str) -> tuple[int, int]:
  197. if path.endswith("python311.test"):
  198. return 3, 11
  199. elif path.endswith("python310.test"):
  200. return 3, 10
  201. elif path.endswith("python39.test"):
  202. return 3, 9
  203. elif path.endswith("python38.test"):
  204. return 3, 8
  205. else:
  206. return defaults.PYTHON3_VERSION
  207. def normalize_error_messages(messages: list[str]) -> list[str]:
  208. """Translate an array of error messages to use / as path separator."""
  209. a = []
  210. for m in messages:
  211. a.append(m.replace(os.sep, "/"))
  212. return a
  213. def retry_on_error(func: Callable[[], Any], max_wait: float = 1.0) -> None:
  214. """Retry callback with exponential backoff when it raises OSError.
  215. If the function still generates an error after max_wait seconds, propagate
  216. the exception.
  217. This can be effective against random file system operation failures on
  218. Windows.
  219. """
  220. t0 = time.time()
  221. wait_time = 0.01
  222. while True:
  223. try:
  224. func()
  225. return
  226. except OSError:
  227. wait_time = min(wait_time * 2, t0 + max_wait - time.time())
  228. if wait_time <= 0.01:
  229. # Done enough waiting, the error seems persistent.
  230. raise
  231. time.sleep(wait_time)
  232. def good_repr(obj: object) -> str:
  233. if isinstance(obj, str):
  234. if obj.count("\n") > 1:
  235. bits = ["'''\\"]
  236. for line in obj.split("\n"):
  237. # force repr to use ' not ", then cut it off
  238. bits.append(repr('"' + line)[2:-1])
  239. bits[-1] += "'''"
  240. return "\n".join(bits)
  241. return repr(obj)
  242. def assert_equal(a: object, b: object, fmt: str = "{} != {}") -> None:
  243. __tracebackhide__ = True
  244. if a != b:
  245. raise AssertionError(fmt.format(good_repr(a), good_repr(b)))
  246. def typename(t: type) -> str:
  247. if "." in str(t):
  248. return str(t).split(".")[-1].rstrip("'>")
  249. else:
  250. return str(t)[8:-2]
  251. def assert_type(typ: type, value: object) -> None:
  252. __tracebackhide__ = True
  253. if type(value) != typ:
  254. raise AssertionError(f"Invalid type {typename(type(value))}, expected {typename(typ)}")
  255. def parse_options(
  256. program_text: str, testcase: DataDrivenTestCase, incremental_step: int
  257. ) -> Options:
  258. """Parse comments like '# flags: --foo' in a test case."""
  259. options = Options()
  260. flags = re.search("# flags: (.*)$", program_text, flags=re.MULTILINE)
  261. if incremental_step > 1:
  262. flags2 = re.search(f"# flags{incremental_step}: (.*)$", program_text, flags=re.MULTILINE)
  263. if flags2:
  264. flags = flags2
  265. if flags:
  266. flag_list = flags.group(1).split()
  267. flag_list.append("--no-site-packages") # the tests shouldn't need an installed Python
  268. targets, options = process_options(flag_list, require_targets=False)
  269. if targets:
  270. # TODO: support specifying targets via the flags pragma
  271. raise RuntimeError("Specifying targets via the flags pragma is not supported.")
  272. if "--show-error-codes" not in flag_list:
  273. options.hide_error_codes = True
  274. else:
  275. flag_list = []
  276. options = Options()
  277. # TODO: Enable strict optional in test cases by default (requires *many* test case changes)
  278. options.strict_optional = False
  279. options.error_summary = False
  280. options.hide_error_codes = True
  281. options.force_uppercase_builtins = True
  282. options.force_union_syntax = True
  283. # Allow custom python version to override testfile_pyversion.
  284. if all(flag.split("=")[0] not in ["--python-version", "-2", "--py2"] for flag in flag_list):
  285. options.python_version = testfile_pyversion(testcase.file)
  286. if testcase.config.getoption("--mypy-verbose"):
  287. options.verbosity = testcase.config.getoption("--mypy-verbose")
  288. return options
  289. def split_lines(*streams: bytes) -> list[str]:
  290. """Returns a single list of string lines from the byte streams in args."""
  291. return [s for stream in streams for s in stream.decode("utf8").splitlines()]
  292. def write_and_fudge_mtime(content: str, target_path: str) -> None:
  293. # In some systems, mtime has a resolution of 1 second which can
  294. # cause annoying-to-debug issues when a file has the same size
  295. # after a change. We manually set the mtime to circumvent this.
  296. # Note that we increment the old file's mtime, which guarantees a
  297. # different value, rather than incrementing the mtime after the
  298. # copy, which could leave the mtime unchanged if the old file had
  299. # a similarly fudged mtime.
  300. new_time = None
  301. if os.path.isfile(target_path):
  302. new_time = os.stat(target_path).st_mtime + 1
  303. dir = os.path.dirname(target_path)
  304. os.makedirs(dir, exist_ok=True)
  305. with open(target_path, "w", encoding="utf-8") as target:
  306. target.write(content)
  307. if new_time:
  308. os.utime(target_path, times=(new_time, new_time))
  309. def perform_file_operations(operations: list[UpdateFile | DeleteFile]) -> None:
  310. for op in operations:
  311. if isinstance(op, UpdateFile):
  312. # Modify/create file
  313. write_and_fudge_mtime(op.content, op.target_path)
  314. else:
  315. # Delete file/directory
  316. if os.path.isdir(op.path):
  317. # Sanity check to avoid unexpected deletions
  318. assert op.path.startswith("tmp")
  319. shutil.rmtree(op.path)
  320. else:
  321. # Use retries to work around potential flakiness on Windows (AppVeyor).
  322. path = op.path
  323. retry_on_error(lambda: os.remove(path))
  324. def check_test_output_files(
  325. testcase: DataDrivenTestCase, step: int, strip_prefix: str = ""
  326. ) -> None:
  327. for path, expected_content in testcase.output_files:
  328. if path.startswith(strip_prefix):
  329. path = path[len(strip_prefix) :]
  330. if not os.path.exists(path):
  331. raise AssertionError(
  332. "Expected file {} was not produced by test case{}".format(
  333. path, " on step %d" % step if testcase.output2 else ""
  334. )
  335. )
  336. with open(path, encoding="utf8") as output_file:
  337. actual_output_content = output_file.read()
  338. if isinstance(expected_content, Pattern):
  339. if expected_content.fullmatch(actual_output_content) is not None:
  340. continue
  341. raise AssertionError(
  342. "Output file {} did not match its expected output pattern\n---\n{}\n---".format(
  343. path, actual_output_content
  344. )
  345. )
  346. normalized_output = normalize_file_output(
  347. actual_output_content.splitlines(), os.path.abspath(test_temp_dir)
  348. )
  349. # We always normalize things like timestamp, but only handle operating-system
  350. # specific things if requested.
  351. if testcase.normalize_output:
  352. if testcase.suite.native_sep and os.path.sep == "\\":
  353. normalized_output = [fix_cobertura_filename(line) for line in normalized_output]
  354. normalized_output = normalize_error_messages(normalized_output)
  355. assert_string_arrays_equal(
  356. expected_content.splitlines(),
  357. normalized_output,
  358. "Output file {} did not match its expected output{}".format(
  359. path, " on step %d" % step if testcase.output2 else ""
  360. ),
  361. )
  362. def normalize_file_output(content: list[str], current_abs_path: str) -> list[str]:
  363. """Normalize file output for comparison."""
  364. timestamp_regex = re.compile(r"\d{10}")
  365. result = [x.replace(current_abs_path, "$PWD") for x in content]
  366. version = mypy.version.__version__
  367. result = [re.sub(r"\b" + re.escape(version) + r"\b", "$VERSION", x) for x in result]
  368. # We generate a new mypy.version when building mypy wheels that
  369. # lacks base_version, so handle that case.
  370. base_version = getattr(mypy.version, "base_version", version)
  371. result = [re.sub(r"\b" + re.escape(base_version) + r"\b", "$VERSION", x) for x in result]
  372. result = [timestamp_regex.sub("$TIMESTAMP", x) for x in result]
  373. return result
  374. def find_test_files(pattern: str, exclude: list[str] | None = None) -> list[str]:
  375. return [
  376. path.name
  377. for path in (pathlib.Path(test_data_prefix).rglob(pattern))
  378. if path.name not in (exclude or [])
  379. ]