2
2
import subprocess
3
3
import sys
4
4
import textwrap
5
- from os .path import basename
6
- from tempfile import NamedTemporaryFile
5
+ from pathlib import Path
7
6
8
7
from mypy .test .config import test_data_prefix
9
8
from mypy .test .helpers import Suite
@@ -13,14 +12,14 @@ class UpdateDataSuite(Suite):
13
12
def _run_pytest_update_data (self , data_suite : str , * , max_attempts : int ) -> str :
14
13
"""
15
14
Runs a suite of data test cases through 'pytest --update-data' until either tests pass
16
- or until a maximum number of attempts. (Multiple attempts can be needed for incremental tests.)
15
+ or until a maximum number of attempts ( needed for incremental tests).
17
16
"""
18
- with NamedTemporaryFile (
19
- mode = "w+" , dir = test_data_prefix , prefix = "check-update-data-" , suffix = ".test"
20
- ) as tmp_file :
21
- tmp_file . write (textwrap .dedent (data_suite ).lstrip ())
22
- tmp_file . flush ()
23
- test_nodeid = f"mypy/test/testcheck.py::TypeCheckSuite::{ basename ( tmp_file .name ) } "
17
+ p = Path ( test_data_prefix ) / "check-update-data.test"
18
+ assert not p . exists ()
19
+ try :
20
+ p . write_text (textwrap .dedent (data_suite ).lstrip ())
21
+
22
+ test_nodeid = f"mypy/test/testcheck.py::TypeCheckSuite::{ p .name } "
24
23
args = [sys .executable , "-m" , "pytest" , "-n" , "0" , "-s" , "--update-data" , test_nodeid ]
25
24
if sys .version_info >= (3 , 8 ):
26
25
cmd = shlex .join (args )
@@ -31,8 +30,10 @@ def _run_pytest_update_data(self, data_suite: str, *, max_attempts: int) -> str:
31
30
if res .returncode == 0 :
32
31
break
33
32
print (f"`{ cmd } ` returned { res .returncode } : { i } attempts remaining" )
34
- tmp_file .seek (0 )
35
- return tmp_file .read ()
33
+
34
+ return p .read_text ()
35
+ finally :
36
+ p .unlink ()
36
37
37
38
def test_update_data (self ) -> None :
38
39
# Note: We test multiple testcases rather than 'test case per test case'
0 commit comments