13
13
from mypy .util import try_find_python2_interpreter
14
14
from mypy .test .data import DataDrivenTestCase , DataSuite
15
15
from mypy .test .config import test_temp_dir
16
- from mypy .test .helpers import assert_string_arrays_equal
16
+ from mypy .test .helpers import assert_string_arrays_equal , perform_file_operations
17
17
18
18
19
19
# NOTE: options.use_builtins_fixtures should not be set in these
@@ -38,6 +38,7 @@ class PEP561Suite(DataSuite):
38
38
files = [
39
39
'pep561.test' ,
40
40
]
41
+ base_path = '.'
41
42
42
43
def run_case (self , test_case : DataDrivenTestCase ) -> None :
43
44
test_pep561 (test_case )
@@ -102,6 +103,7 @@ def test_pep561(testcase: DataDrivenTestCase) -> None:
102
103
pytest .skip ()
103
104
else :
104
105
python = sys .executable
106
+
105
107
assert python is not None , "Should be impossible"
106
108
pkgs , pip_args = parse_pkgs (testcase .input [0 ])
107
109
mypy_args = parse_mypy_args (testcase .input [1 ])
@@ -118,34 +120,30 @@ def test_pep561(testcase: DataDrivenTestCase) -> None:
118
120
for pkg in pkgs :
119
121
install_package (pkg , python_executable , use_pip , editable )
120
122
121
- if venv_dir is not None :
122
- old_dir = os .getcwd ()
123
- os .chdir (venv_dir )
124
- try :
125
- cmd_line = list (mypy_args )
126
- has_program = not ('-p' in cmd_line or '--package' in cmd_line )
127
- if has_program :
128
- program = testcase .name + '.py'
129
- with open (program , 'w' , encoding = 'utf-8' ) as f :
130
- for s in testcase .input :
131
- f .write ('{}\n ' .format (s ))
132
- cmd_line .append (program )
133
- cmd_line .extend (['--no-incremental' , '--no-error-summary' ])
134
- if python_executable != sys .executable :
135
- cmd_line .append ('--python-executable={}' .format (python_executable ))
136
- if testcase .files != []:
137
- for name , content in testcase .files :
138
- if 'mypy.ini' in name :
139
- with open ('mypy.ini' , 'w' ) as m :
140
- m .write (content )
141
- elif 'pyproject.toml' in name :
142
- with open ('pyproject.toml' , 'w' ) as m :
143
- m .write (content )
123
+ cmd_line = list (mypy_args )
124
+ has_program = not ('-p' in cmd_line or '--package' in cmd_line )
125
+ if has_program :
126
+ program = testcase .name + '.py'
127
+ with open (program , 'w' , encoding = 'utf-8' ) as f :
128
+ for s in testcase .input :
129
+ f .write ('{}\n ' .format (s ))
130
+ cmd_line .append (program )
131
+
132
+ cmd_line .extend (['--no-error-summary' ])
133
+ if python_executable != sys .executable :
134
+ cmd_line .append ('--python-executable={}' .format (python_executable ))
135
+
136
+ steps = testcase .find_steps ()
137
+ if steps != [[]]:
138
+ steps = [[]] + steps # type: ignore[operator,assignment]
139
+
140
+ for i , operations in enumerate (steps ):
141
+ perform_file_operations (operations )
142
+
144
143
output = []
145
144
# Type check the module
146
145
out , err , returncode = mypy .api .run (cmd_line )
147
- if has_program :
148
- os .remove (program )
146
+
149
147
# split lines, remove newlines, and remove directory of test case
150
148
for line in (out + err ).splitlines ():
151
149
if line .startswith (test_temp_dir + os .sep ):
@@ -154,12 +152,15 @@ def test_pep561(testcase: DataDrivenTestCase) -> None:
154
152
# Normalize paths so that the output is the same on Windows and Linux/macOS.
155
153
line = line .replace (test_temp_dir + os .sep , test_temp_dir + '/' )
156
154
output .append (line .rstrip ("\r \n " ))
157
- assert_string_arrays_equal ([line for line in testcase .output ], output ,
158
- 'Invalid output ({}, line {})' .format (
159
- testcase .file , testcase .line ))
160
- finally :
161
- if venv_dir is not None :
162
- os .chdir (old_dir )
155
+ iter_count = '' if i == 0 else ' on iteration {}' .format (i + 1 )
156
+ expected = testcase .output if i == 0 else testcase .output2 .get (i + 1 , [])
157
+
158
+ assert_string_arrays_equal (expected , output ,
159
+ 'Invalid output ({}, line {}){}' .format (
160
+ testcase .file , testcase .line , iter_count ))
161
+
162
+ if has_program :
163
+ os .remove (program )
163
164
164
165
165
166
def parse_pkgs (comment : str ) -> Tuple [List [str ], List [str ]]:
0 commit comments