12
12
from collections .abc import Iterator
13
13
from typing import Any , Callable
14
14
15
+ from pytest import raises
16
+
15
17
import mypy .stubtest
16
18
from mypy import build , nodes
17
19
from mypy .modulefinder import BuildSource
@@ -171,7 +173,12 @@ def build_helper(source: str) -> build.BuildResult:
171
173
172
174
173
175
def run_stubtest_with_stderr (
174
- stub : str , runtime : str , options : list [str ], config_file : str | None = None
176
+ stub : str ,
177
+ runtime : str ,
178
+ options : list [str ],
179
+ config_file : str | None = None ,
180
+ output : io .StringIO | None = None ,
181
+ outerr : io .StringIO | None = None ,
175
182
) -> tuple [str , str ]:
176
183
with use_tmp_dir (TEST_MODULE_NAME ) as tmp_dir :
177
184
with open ("builtins.pyi" , "w" ) as f :
@@ -188,8 +195,8 @@ def run_stubtest_with_stderr(
188
195
with open (f"{ TEST_MODULE_NAME } _config.ini" , "w" ) as f :
189
196
f .write (config_file )
190
197
options = options + ["--mypy-config-file" , f"{ TEST_MODULE_NAME } _config.ini" ]
191
- output = io .StringIO ()
192
- outerr = io .StringIO ()
198
+ output = io .StringIO () if output is None else output
199
+ outerr = io .StringIO () if outerr is None else outerr
193
200
with contextlib .redirect_stdout (output ), contextlib .redirect_stderr (outerr ):
194
201
test_stubs (parse_options ([TEST_MODULE_NAME ] + options ), use_builtins_fixtures = True )
195
202
filtered_output = remove_color_code (
@@ -2888,14 +2895,20 @@ def test_config_file_error_codes_invalid(self) -> None:
2888
2895
runtime = "temp = 5\n "
2889
2896
stub = "temp: int\n "
2890
2897
config_file = "[mypy]\n disable_error_code = not-a-valid-name\n "
2891
- output , outerr = run_stubtest_with_stderr (
2892
- stub = stub , runtime = runtime , options = [], config_file = config_file
2893
- )
2894
- assert output == "Success: no issues found in 1 module\n "
2895
- assert outerr == (
2896
- "test_module_config.ini: [mypy]: disable_error_code: "
2897
- "Invalid error code(s): not-a-valid-name\n "
2898
- )
2898
+ output = io .StringIO ()
2899
+ outerr = io .StringIO ()
2900
+ with raises (SystemExit ):
2901
+ run_stubtest_with_stderr (
2902
+ stub = stub ,
2903
+ runtime = runtime ,
2904
+ options = [],
2905
+ config_file = config_file ,
2906
+ output = output ,
2907
+ outerr = outerr ,
2908
+ )
2909
+
2910
+ assert output .getvalue () == "error: Invalid error code(s): not-a-valid-name\n "
2911
+ assert outerr .getvalue () == ""
2899
2912
2900
2913
def test_config_file_wrong_incomplete_feature (self ) -> None :
2901
2914
runtime = "x = 1\n "
0 commit comments