Skip to content

Commit 7956e2e

Browse files
authored
Merge pull request #167 from jakkdl/merge_eval_tests
clean up tests, merging test_eval and test_eval_anyio
2 parents e66e8a1 + b15f4ce commit 7956e2e

File tree

1 file changed

+44
-48
lines changed

1 file changed

+44
-48
lines changed

tests/test_flake8_trio.py

Lines changed: 44 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from argparse import ArgumentParser
1616
from collections import deque
1717
from pathlib import Path
18-
from typing import TYPE_CHECKING, Any, DefaultDict
18+
from typing import TYPE_CHECKING, Any, DefaultDict, Literal
1919

2020
import libcst as cst
2121
import pytest
@@ -157,70 +157,68 @@ def check_autofix(
157157
assert added_autofix_diff == autofix_diff_content
158158

159159

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")
167161

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 = []
173162

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
182170

183171

184172
@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+
):
187178
content = path.read_text()
188-
189-
if "# NOANYIO" in content:
179+
magic_markers = find_magic_markers(content)
180+
if anyio and magic_markers["NOANYIO"]:
190181
pytest.skip("file marked with NOANYIO")
191182

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:
194190
content = replace_library(content)
195191

196192
# if substituting we're messing up columns
197193
ignore_column = True
198-
else:
199-
ignore_column = False
200194

201-
# parse args and expected errors
202195
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}")
203200

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+
):
212204
expected = []
213205

206+
plugin = Plugin.from_source(content)
214207
errors = assert_expected_errors(
215208
plugin, *expected, args=parsed_args, ignore_column=ignore_column
216209
)
217210

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
222216

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
224222

225223

226224
# 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
250248

251249
parsed_args = []
252250

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.
256254
enabled_codes = ""
257255
if test in ERROR_CODES:
258256
parsed_args = [f"--enable={test}"]
@@ -282,8 +280,6 @@ def _parse_eval_file(test: str, content: str) -> tuple[list[Error], list[str], s
282280

283281
for err_code, alt_code, err_args in k:
284282
try:
285-
# Append a bunch of empty strings so string formatting gives garbage
286-
# instead of throwing an exception
287283
try:
288284
args = eval(
289285
f"[{err_args}]",

0 commit comments

Comments
 (0)