|
15 | 15 | from argparse import ArgumentParser |
16 | 16 | from collections import deque |
17 | 17 | from pathlib import Path |
18 | | -from typing import TYPE_CHECKING, Any, DefaultDict |
| 18 | +from typing import TYPE_CHECKING, Any, DefaultDict, Literal |
19 | 19 |
|
20 | 20 | import libcst as cst |
21 | 21 | import pytest |
@@ -157,70 +157,68 @@ def check_autofix( |
157 | 157 | assert added_autofix_diff == autofix_diff_content |
158 | 158 |
|
159 | 159 |
|
160 | | -# TODO: merge with test_eval_anyio with a parametrize |
161 | | -@pytest.mark.parametrize(("test", "path"), test_files) |
162 | | -@pytest.mark.parametrize("autofix", [False, True]) |
163 | | -def test_eval(test: str, path: Path, autofix: bool, generate_autofix: bool): |
164 | | - content = path.read_text() |
165 | | - if "# NOTRIO" in content: |
166 | | - pytest.skip("file marked with NOTRIO") |
| 160 | +MAGIC_MARKERS = ("NOANYIO", "NOTRIO", "TRIO_NO_ERROR", "ANYIO_NO_ERROR") |
167 | 161 |
|
168 | | - expected, parsed_args, enable = _parse_eval_file(test, content) |
169 | | - if autofix: |
170 | | - parsed_args.append(f"--autofix={enable}") |
171 | | - if "# TRIO_NO_ERROR" in content: |
172 | | - expected = [] |
173 | 162 |
|
174 | | - plugin = Plugin.from_source(content) |
175 | | - _ = assert_expected_errors(plugin, *expected, args=parsed_args) |
176 | | - |
177 | | - if autofix: |
178 | | - check_autofix(test, plugin, content, generate_autofix) |
179 | | - else: |
180 | | - # make sure content isn't modified |
181 | | - assert content == plugin.module.code |
| 163 | +def find_magic_markers( |
| 164 | + content: str, |
| 165 | +) -> dict[Literal["NOANYIO", "NOTRIO", "TRIO_NO_ERROR", "ANYIO_NO_ERROR"], bool]: |
| 166 | + found_markers: dict[str, bool] = {m: False for m in MAGIC_MARKERS} |
| 167 | + for f in re.findall(rf'# ({"|".join(MAGIC_MARKERS)})', content): |
| 168 | + found_markers[f] = True |
| 169 | + return found_markers # type: ignore |
182 | 170 |
|
183 | 171 |
|
184 | 172 | @pytest.mark.parametrize(("test", "path"), test_files) |
185 | | -def test_eval_anyio(test: str, path: Path, generate_autofix: bool): |
186 | | - # read content, replace instances of trio with anyio, and write to tmp_file |
| 173 | +@pytest.mark.parametrize("autofix", [False, True]) |
| 174 | +@pytest.mark.parametrize("anyio", [False, True]) |
| 175 | +def test_eval( |
| 176 | + test: str, path: Path, autofix: bool, anyio: bool, generate_autofix: bool |
| 177 | +): |
187 | 178 | content = path.read_text() |
188 | | - |
189 | | - if "# NOANYIO" in content: |
| 179 | + magic_markers = find_magic_markers(content) |
| 180 | + if anyio and magic_markers["NOANYIO"]: |
190 | 181 | pytest.skip("file marked with NOANYIO") |
191 | 182 |
|
192 | | - # if test is marked NOTRIO, it's not written to require substitution |
193 | | - if "# NOTRIO" not in content: |
| 183 | + ignore_column = False |
| 184 | + if magic_markers["NOTRIO"]: |
| 185 | + if not anyio: |
| 186 | + pytest.skip("file marked with NOTRIO") |
| 187 | + |
| 188 | + # if test is marked NOTRIO, it's not written to require substitution |
| 189 | + elif anyio: |
194 | 190 | content = replace_library(content) |
195 | 191 |
|
196 | 192 | # if substituting we're messing up columns |
197 | 193 | ignore_column = True |
198 | | - else: |
199 | | - ignore_column = False |
200 | 194 |
|
201 | | - # parse args and expected errors |
202 | 195 | expected, parsed_args, enable = _parse_eval_file(test, content) |
| 196 | + if anyio: |
| 197 | + parsed_args.insert(0, "--anyio") |
| 198 | + if autofix: |
| 199 | + parsed_args.append(f"--autofix={enable}") |
203 | 200 |
|
204 | | - parsed_args.insert(0, "--anyio") |
205 | | - parsed_args.append(f"--autofix={enable}") |
206 | | - |
207 | | - # initialize plugin and check errors, ignoring columns since they occasionally are |
208 | | - # wrong due to len("anyio") > len("trio") |
209 | | - plugin = Plugin.from_source(content) |
210 | | - |
211 | | - if "# ANYIO_NO_ERROR" in content: |
| 201 | + if (anyio and magic_markers["ANYIO_NO_ERROR"]) or ( |
| 202 | + not anyio and magic_markers["TRIO_NO_ERROR"] |
| 203 | + ): |
212 | 204 | expected = [] |
213 | 205 |
|
| 206 | + plugin = Plugin.from_source(content) |
214 | 207 | errors = assert_expected_errors( |
215 | 208 | plugin, *expected, args=parsed_args, ignore_column=ignore_column |
216 | 209 | ) |
217 | 210 |
|
218 | | - # check that error messages refer to 'anyio', or to neither library |
219 | | - for error in errors: |
220 | | - message = error.message.format(*error.args) |
221 | | - assert "anyio" in message or "trio" not in message |
| 211 | + if anyio: |
| 212 | + # check that error messages refer to 'anyio', or to neither library |
| 213 | + for error in errors: |
| 214 | + message = error.message.format(*error.args) |
| 215 | + assert "anyio" in message or "trio" not in message |
222 | 216 |
|
223 | | - check_autofix(test, plugin, content, generate_autofix, anyio=True) |
| 217 | + if autofix: |
| 218 | + check_autofix(test, plugin, content, generate_autofix, anyio=anyio) |
| 219 | + else: |
| 220 | + # make sure content isn't modified |
| 221 | + assert content == plugin.module.code |
224 | 222 |
|
225 | 223 |
|
226 | 224 | # check that autofixed files raise no errors and doesn't get autofixed (again) |
@@ -250,9 +248,9 @@ def _parse_eval_file(test: str, content: str) -> tuple[list[Error], list[str], s |
250 | 248 |
|
251 | 249 | parsed_args = [] |
252 | 250 |
|
253 | | - # only enable the tested visitor to save performance and ease debugging |
254 | | - # if a test requires enabling multiple visitors they specify a |
255 | | - # `# ARG --enable-vis...` that comes later in the arg list, overriding this |
| 251 | + # Only enable the tested visitor to save performance and ease debugging. |
| 252 | + # If a test requires enabling multiple visitors they specify a |
| 253 | + # `# ARG --enable=` that comes later in the arg list, overriding this. |
256 | 254 | enabled_codes = "" |
257 | 255 | if test in ERROR_CODES: |
258 | 256 | parsed_args = [f"--enable={test}"] |
@@ -282,8 +280,6 @@ def _parse_eval_file(test: str, content: str) -> tuple[list[Error], list[str], s |
282 | 280 |
|
283 | 281 | for err_code, alt_code, err_args in k: |
284 | 282 | try: |
285 | | - # Append a bunch of empty strings so string formatting gives garbage |
286 | | - # instead of throwing an exception |
287 | 283 | try: |
288 | 284 | args = eval( |
289 | 285 | f"[{err_args}]", |
|
0 commit comments