8000 Move eval files into a separate directory · python-trio/flake8-async@a5594bb · GitHub
[go: up one dir, main page]

Skip to content
Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a5594bb

Browse files
committed
Move eval files into a separate directory
1 parent d67cbb0 commit a5594bb

32 files changed

+22
-29
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ repos:
3939
rev: 6.0.0
4040
hooks:
4141
- id: flake8
42-
args: ["--exclude", ".*,tests/trio*.py"]
42+
args: ["--exclude", ".*,tests/eval_files/*"]
4343
language_version: python3
4444
additional_dependencies:
4545
- flake8-builtins
@@ -63,7 +63,7 @@ repos:
6363
rev: 5.0.4
6464
hooks:
6565
- id: flake8
66-
args: ["--exclude", ".*,tests/trio*.py", "--select=E800"]
66+
args: ["--exclude", ".*,tests/eval_files/*", "--select=E800"]
6767
language_version: python3
6868
additional_dependencies:
6969
- flake8-eradicate

CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ tox -p --develop
2626
```
2727

2828
## Meta-tests
29-
To check that all codes are tested and documented there's a test that error codes mentioned in `README.md`, `CHANGELOG.md` (matching `TRIO\d\d\d`), the keys in `flake8_trio.Error_codes` and codes parsed from filenames matching `tests/trio*.py`, are all equal.
29+
To check that all codes are tested and documented there's a test that error codes mentioned in `README.md`, `CHANGELOG.md` (matching `TRIO\d\d\d`), the keys in `flake8_trio.Error_codes` and codes parsed from filenames and files in `tests/eval_files/`, are all equal.
3030

3131
## Test generator
32-
Tests are automatically generated for files named `trio*.py` in the `tests/` directory, with the code that it's testing interpreted from the file name. The file extension is split off, if there's a match for for `_py\d*` it strips that off and uses it to determine if there's a minimum python version for which the test should only run.
32+
Tests are automatically generated for files in the `tests/eval_files/` directory, with the code that it's testing interpreted from the file name. The file extension is split off, if there's a match for for `_py\d*` it strips that off and uses it to determine if there's a minimum python version for which the test should only run.
3333

3434
### `error:`
3535
Lines containing `error:` are parsed as expecting an error of the code matching the file name, with everything on the line after the colon `eval`'d and passed as arguments to `flake8_trio.Error_codes[<error_code>].str_format`. The `globals` argument to `eval` contains a `lineno` variable assigned the current line number, and the `flake8_trio.Statement` namedtuple. The first element after `error:` *must* be an integer containing the column where the error on that line originates.

flake8_trio.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from argparse import ArgumentTypeError, Namespace
1919
from collections.abc import Iterable
2020
from fnmatch import fnmatch
21+
from pathlib import Path
2122
from typing import TYPE_CHECKING, Any, NamedTuple, TypeVar, Union, cast
2223

2324
# guard against internal flake8 changes
@@ -1739,7 +1740,7 @@ def __init__(self, tree: ast.AST):
17391740
self._tree = tree
17401741

17411742
@classmethod
1742-
def from_filename(cls, filename: str) -> Plugin:
1743+
def from_filename(cls, filename: str | Path) -> Plugin:
17431744
with tokenize.open(filename) as f:
17441745
source = f.read()
17451746
return cls(ast.parse(source))

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[tool.pyright]
22
strict = ["*.py", "tests/test_flake8_trio.py", "tests/conftest.py"]
3-
exclude = ["tests/trio*.py", "**/node_modules", "**/__pycache__", "**/.*"]
3+
exclude = ["tests/eval_files/*", "**/node_modules", "**/__pycache__", "**/.*"]
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

tests/test_changelog_and_version.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ def runTest(self):
6868
documented_errors["flake8_trio.py"] = set(ERROR_CODES)
6969

7070
# get tested error codes from file names and from `INCLUDE` lines
71-
documented_errors["tests/trio*.py"] = set()
72-
p = Path(__file__).parent
71+
documented_errors["eval_files"] = set()
72+
p = Path(__file__).parent / "eval_files"
7373
for file_path in p.iterdir():
7474
if not file_path.is_file():
7575
continue
7676

7777
if m := re.search(r"trio\d\d\d", str(file_path)):
78-
documented_errors["tests/trio*.py"].add(m.group().upper())
78+
documented_errors["eval_files"].add(m.group().upper())
7979

8080
with open(file_path) as file:
8181
for line in file:
@@ -87,7 +87,7 @@ def runTest(self):
8787
# depending on whether there's a group in the pattern or not.
8888
# (or bytes, if both inputs are bytes)
8989
assert isinstance(m, str)
90-
documented_errors["tests/trio*.py"].add(m)
90+
documented_errors["eval_files"].add(m)
9191
break
9292

9393
unique_errors: dict[str, set[str]] = {}

tests/test_decorator.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import ast
4+
from pathlib import Path
45

56
from flake8.main.application import Application
67

@@ -80,7 +81,8 @@ def test_pep614():
8081
assert not wrap(("(any, expression, we, like)",), "no match here")
8182

8283

83-
common_flags = ["--select=TRIO", "tests/trio_options.py"]
84+
file_path = str(Path(__file__).parent / "trio_options.py")
85+
common_flags = ["--select=TRIO", file_path]
8486

8587

8688
def test_command_line_1(capfd):
@@ -89,7 +91,7 @@ def test_command_line_1(capfd):
8991

9092

9193
expected_out = (
92-
"tests/trio_options.py:2:1: TRIO107 "
94+
f"{file_path}:2:1: TRIO107 "
9395
+ Visitor107_108.error_codes["TRIO107"].format(
9496
"exit", Statement("function definition", 2)
9597
)

tests/test_flake8_trio.py

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,8 @@
2525

2626
from flake8_trio import ERROR_CLASSES, Error, Plugin, Statement
2727

28-
# TODO: Move test_eval files into a separate directory
29-
trio_test_files_regex = re.compile(r"trio\d.*.py")
30-
31-
test_files: list[tuple[str, str]] = sorted(
32-
(os.path.splitext(f)[0].upper(), f)
33-
for f in os.listdir("tests")
34-
if re.match(trio_test_files_regex, f)
28+
test_files: list[tuple[str, Path]] = sorted(
29+
(f.stem.upper(), f) for f in (Path(__file__).parent / "eval_files").iterdir()
3530
)
3631

3732

@@ -66,7 +61,7 @@ def check_version(test: str):
6661

6762

6863
@pytest.mark.parametrize(("test", "path"), test_files)
69-
def test_eval(test: str, path: str):
64+
def test_eval(test: str, path: Path):
7065
# version check
7166
check_version(test)
7267
test = test.split("_")[0]
@@ -81,7 +76,7 @@ def test_eval(test: str, path: str):
8176

8277
expected: list[Error] = []
8378

84-
with open(os.path.join("tests", path), encoding="utf-8") as file:
79+
with open(path, encoding="utf-8") as file:
8580
lines = file.readlines()
8681

8782
for lineno, line in enumerate(lines, start=1):
@@ -146,7 +141,7 @@ def test_eval(test: str, path: str):
146141
assert parsed_args, "no parsed_args"
147142
assert expected, f"failed to parse any errors in file {path}"
148143

149-
plugin = read_file(path)
144+
plugin = Plugin.from_filename(path)
150145
assert_expected_errors(plugin, *expected, args=parsed_args)
151146

152147

@@ -189,10 +184,10 @@ def visit_AsyncFor(self, node: ast.AST):
189184

190185

191186
@pytest.mark.parametrize(("test", "path"), test_files)
192-
def test_noerror_on_sync_code(test: str, path: str):
187+
def test_noerror_on_sync_code(test: str, path: Path):
193188
if any(e in test for e in error_codes_ignored_when_checking_transformed_sync_code):
194189
return
195-
with tokenize.open(f"tests/{path}") as f:
190+
with tokenize.open(path) as f:
196191
source = f.read()
197192
tree = SyncTransformer().visit(ast.parse(source))
198193

@@ -207,11 +202,6 @@ def test_noerror_on_sync_code(test: str, path: str):
207202
)
208203

209204

210-
def read_file(test_file: str):
211-
filename = Path(__file__).absolute().parent / test_file
212-
return Plugin.from_filename(str(filename))
213-
214-
215205
def initialize_options(plugin: Plugin, args: list[str] | None = None):
216206
om = _default_option_manager()
217207
plugin.add_options(om)

0 commit comments

Comments
 (0)
0