22A "meta test" which tests the parsing of .test files. This is not meant to become exhaustive
33but to ensure we maintain a basic level of ergonomics for mypy contributors.
44"""
5- import subprocess
6- import sys
7- import textwrap
8- import uuid
9- from pathlib import Path
10-
11- from mypy .test .config import test_data_prefix
125from mypy .test .helpers import Suite
6+ from mypy .test .meta ._pytest import PytestResult , run_pytest_data_suite
137
148
15- class ParseTestDataSuite (Suite ):
16- def _dedent (self , s : str ) -> str :
17- return textwrap .dedent (s ).lstrip ()
9+ def _run_pytest (data_suite : str ) -> PytestResult :
10+ return run_pytest_data_suite (data_suite , extra_args = [], max_attempts = 1 )
1811
19- def _run_pytest (self , data_suite : str ) -> str :
20- p_test_data = Path (test_data_prefix )
21- p_root = p_test_data .parent .parent
22- p = p_test_data / f"check-meta-{ uuid .uuid4 ()} .test"
23- assert not p .exists ()
24- try :
25- p .write_text (data_suite )
26- test_nodeid = f"mypy/test/testcheck.py::TypeCheckSuite::{ p .name } "
27- args = [sys .executable , "-m" , "pytest" , "-n" , "0" , "-s" , test_nodeid ]
28- proc = subprocess .run (args , cwd = p_root , capture_output = True , check = False )
29- return proc .stdout .decode ()
30- finally :
31- p .unlink ()
3212
13+ class ParseTestDataSuite (Suite ):
3314 def test_parse_invalid_case (self ) -> None :
34- # Arrange
35- data = self . _dedent (
15+ # Act
16+ result = _run_pytest (
3617 """
3718 [case abc]
3819 s: str
@@ -41,15 +22,12 @@ def test_parse_invalid_case(self) -> None:
4122 """
4223 )
4324
44- # Act
45- actual = self ._run_pytest (data )
46-
4725 # Assert
48- assert "Invalid testcase id 'foo-XFAIL'" in actual
26+ assert "Invalid testcase id 'foo-XFAIL'" in result . stdout
4927
5028 def test_parse_invalid_section (self ) -> None :
51- # Arrange
52- data = self . _dedent (
29+ # Act
30+ result = _run_pytest (
5331 """
5432 [case abc]
5533 s: str
@@ -58,19 +36,16 @@ def test_parse_invalid_section(self) -> None:
5836 """
5937 )
6038
61- # Act
62- actual = self ._run_pytest (data )
63-
6439 # Assert
65- expected_lineno = data .splitlines ().index ("[unknownsection]" ) + 1
40+ expected_lineno = result . input .splitlines ().index ("[unknownsection]" ) + 1
6641 expected = (
6742 f".test:{ expected_lineno } : Invalid section header [unknownsection] in case 'abc'"
6843 )
69- assert expected in actual
44+ assert expected in result . stdout
7045
7146 def test_bad_ge_version_check (self ) -> None :
72- # Arrange
73- data = self . _dedent (
47+ # Act
48+ actual = _run_pytest (
7449 """
7550 [case abc]
7651 s: str
@@ -79,15 +54,12 @@ def test_bad_ge_version_check(self) -> None:
7954 """
8055 )
8156
82- # Act
83- actual = self ._run_pytest (data )
84-
8557 # Assert
86- assert "version>=3.8 always true since minimum runtime version is (3, 8)" in actual
58+ assert "version>=3.8 always true since minimum runtime version is (3, 8)" in actual . stdout
8759
8860 def test_bad_eq_version_check (self ) -> None :
89- # Arrange
90- data = self . _dedent (
61+ # Act
62+ actual = _run_pytest (
9163 """
9264 [case abc]
9365 s: str
@@ -96,8 +68,5 @@ def test_bad_eq_version_check(self) -> None:
9668 """
9769 )
9870
99- # Act
100- actual = self ._run_pytest (data )
101-
10271 # Assert
103- assert "version==3.7 always false since minimum runtime version is (3, 8)" in actual
72+ assert "version==3.7 always false since minimum runtime version is (3, 8)" in actual . stdout
0 commit comments