test_parse_data.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. """
  2. A "meta test" which tests the parsing of .test files. This is not meant to become exhaustive
  3. but to ensure we maintain a basic level of ergonomics for mypy contributors.
  4. """
  5. import subprocess
  6. import sys
  7. import textwrap
  8. import uuid
  9. from pathlib import Path
  10. from mypy.test.config import test_data_prefix
  11. from mypy.test.helpers import Suite
  12. class ParseTestDataSuite(Suite):
  13. def _dedent(self, s: str) -> str:
  14. return textwrap.dedent(s).lstrip()
  15. def _run_pytest(self, data_suite: str) -> str:
  16. p_test_data = Path(test_data_prefix)
  17. p_root = p_test_data.parent.parent
  18. p = p_test_data / f"check-meta-{uuid.uuid4()}.test"
  19. assert not p.exists()
  20. try:
  21. p.write_text(data_suite)
  22. test_nodeid = f"mypy/test/testcheck.py::TypeCheckSuite::{p.name}"
  23. args = [sys.executable, "-m", "pytest", "-n", "0", "-s", test_nodeid]
  24. proc = subprocess.run(args, cwd=p_root, capture_output=True, check=False)
  25. return proc.stdout.decode()
  26. finally:
  27. p.unlink()
  28. def test_parse_invalid_case(self) -> None:
  29. # Arrange
  30. data = self._dedent(
  31. """
  32. [case abc]
  33. s: str
  34. [case foo-XFAIL]
  35. s: str
  36. """
  37. )
  38. # Act
  39. actual = self._run_pytest(data)
  40. # Assert
  41. assert "Invalid testcase id 'foo-XFAIL'" in actual
  42. def test_parse_invalid_section(self) -> None:
  43. # Arrange
  44. data = self._dedent(
  45. """
  46. [case abc]
  47. s: str
  48. [unknownsection]
  49. abc
  50. """
  51. )
  52. # Act
  53. actual = self._run_pytest(data)
  54. # Assert
  55. expected_lineno = data.splitlines().index("[unknownsection]") + 1
  56. expected = (
  57. f".test:{expected_lineno}: Invalid section header [unknownsection] in case 'abc'"
  58. )
  59. assert expected in actual
  60. def test_bad_ge_version_check(self) -> None:
  61. # Arrange
  62. data = self._dedent(
  63. """
  64. [case abc]
  65. s: str
  66. [out version>=3.8]
  67. abc
  68. """
  69. )
  70. # Act
  71. actual = self._run_pytest(data)
  72. # Assert
  73. assert "version>=3.8 always true since minimum runtime version is (3, 8)" in actual
  74. def test_bad_eq_version_check(self) -> None:
  75. # Arrange
  76. data = self._dedent(
  77. """
  78. [case abc]
  79. s: str
  80. [out version==3.7]
  81. abc
  82. """
  83. )
  84. # Act
  85. actual = self._run_pytest(data)
  86. # Assert
  87. assert "version==3.7 always false since minimum runtime version is (3, 8)" in actual