8000 add pre-commit hook · python-trio/flake8-async@1560ee2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1560ee2

Browse files
committed
add pre-commit hook
1 parent 033317a commit 1560ee2

File tree

6 files changed

+57
-11
lines changed

6 files changed

+57
-11
lines changed

.pre-commit-config.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ repos:
1313
- id: black
1414

1515
- repo: https://github.com/PyCQA/autoflake
16-
rev: v2.0.1
16+
rev: v2.0.2
1717
hooks:
1818
- id: autoflake
1919

@@ -34,7 +34,7 @@ repos:
3434
- id: mypy
3535

3636
- repo: https://github.com/RobertCraigie/pyright-python
37-
rev: v1.1.298
37+
rev: v1.1.299
3838
hooks:
3939
- id: pyright
4040
entry: env PYRIGHT_PYTHON_FORCE_VERSION=latest pyright
@@ -109,7 +109,7 @@ repos:
109109
- id: yamlfmt
110110

111111
- repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
112-
rev: v2.7.0
112+
rev: v2.8.0
113113
hooks:
114114
- id: pretty-format-toml
115115
args: [--autofix]

.pre-commit-hooks.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
- id: flake8_trio
3+
name: flake8_trio
4+
entry: flake8_trio
5+
language: python
6+
types: [python]

flake8_trio/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def cst_parse_module_native(source: str) -> cst.Module:
7474
return mod
7575

7676

77-
def main():
77+
def main() -> int:
7878
parser = ArgumentParser(prog="flake8_trio")
7979
parser.add_argument(
8080
nargs="*",
@@ -105,17 +105,20 @@ def main():
105105
"Doesn't seem to be a git repo; pass filenames to format.",
106106
file=sys.stderr,
107107
)
108-
sys.exit(1)
108+
return 1
109109
all_filenames = [
110110
os.path.join(root, f) for f in all_filenames if _should_format(f)
111111
]
112+
any_error = False
112113
for file in all_filenames:
113114
plugin = Plugin.from_filename(file)
114115
for error in sorted(plugin.run()):
115116
print(f"{file}:{error}")
117+
any_error = True
116118
if plugin.options.autofix:
117119
with open(file, "w") as file:
118120
file.write(plugin.module.code)
121+
return 1 if any_error else 0
119122

120123

121124
class Plugin:

flake8_trio/__main__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
"""Entry file when executed with `python -m`."""
2+
import sys
3+
24
from . import main
35

4-
main()
6+
sys.exit(main())

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def local_file(name: str) -> Path:
2929
license="MIT",
3030
description="A highly opinionated flake8 plugin for Trio-related problems.",
3131
zip_safe=False,
32-
install_requires=["flake8"],
32+
install_requires=["flake8", "libcst"],
3333
python_requires=">=3.9",
3434
classifiers=[
3535
"Development Status :: 3 - Alpha",

tests/test_config_and_args.py

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import pytest
1010

11-
from flake8_trio import Plugin
11+
from flake8_trio import Plugin, main
1212

1313
from .test_flake8_trio import initialize_options
1414

@@ -36,10 +36,44 @@ def test_run_flake8_trio(tmp_path: Path):
3636
cwd=tmp_path,
3737
capture_output=True,
3838
)
39+
assert res.returncode == 1
3940
assert not res.stderr
4041
assert res.stdout == err_msg.encode("ascii")
4142

4243

44+
def test_systemexit_0(
45+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
46+
):
47+
monkeypatch.chdir(tmp_path)
48+
monkeypatch.setattr(sys, "argv", [tmp_path / "flake8_trio", "./example.py"])
49+
50+
tmp_path.joinpath("example.py").write_text("")
51+
52+
with pytest.raises(SystemExit) as exc_info:
53+
from flake8_trio import __main__ # noqa
54+
55+
assert exc_info.value.code == 0
56+
out, err = capsys.readouterr()
57+
assert not out
58+
assert not err
59+
60+
61+
def test_systemexit_1(
62+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
63+
):
64+
err_msg = _common_error_setup(tmp_path)
65+
monkeypatch.chdir(tmp_path)
66+
monkeypatch.setattr(sys, "argv", [tmp_path / "flake8_trio", "./example.py"])
67+
68+
with pytest.raises(SystemExit) as exc_info:
69+
from flake8_trio import __main__ # noqa
70+
71+
assert exc_info.value.code == 1
72+
out, err = capsys.readouterr()
73+
assert out == err_msg
74+
assert not err
75+
76+
4377
def test_run_in_git_repo(tmp_path: Path):
4478
err_msg = _common_error_setup(tmp_path)
4579
assert subprocess.run(["git", "init"], cwd=tmp_path, capture_output=True)
@@ -51,6 +85,7 @@ def test_run_in_git_repo(tmp_path: Path):
5185
cwd=tmp_path,
5286
capture_output=True,
5387
)
88+
assert res.returncode == 1
5489
assert not res.stderr
5590
assert res.stdout == err_msg.encode("ascii")
5691

@@ -60,8 +95,7 @@ def test_run_no_git_repo(
6095
):
6196
monkeypatch.chdir(tmp_path)
6297
monkeypatch.setattr(sys, "argv", [tmp_path / "flake8_trio"])
63-
with pytest.raises(SystemExit):
64-
from flake8_trio import __main__ # noqa
98+
assert main() == 1
6599
out, err = capsys.readouterr()
66100
assert err == "Doesn't seem to be a git repo; pass filenames to format.\n"
67101
assert not out
@@ -75,7 +109,7 @@ def test_run_100_autofix(
75109
monkeypatch.setattr(
76110
sys, "argv", [tmp_path / "flake8_trio", "--autofix", "./example.py"]
77111
)
78-
from flake8_trio import __main__ # noqa
112+
assert main() == 1
79113

80114
out, err = capsys.readouterr()
81115< 6AD7 code class="diff-text syntax-highlighted-line">
assert out == err_msg
@@ -189,6 +223,7 @@ def test_200_from_config_flake8_internals(
189223
def test_200_from_config_subprocess(tmp_path: Path):
190224
err_msg = _test_trio200_from_config_common(tmp_path)
191225
res = subprocess.run(["flake8"], cwd=tmp_path, capture_output=True)
226+
assert res.returncode == 1
192227
assert not res.stderr
193228
assert res.stdout == err_msg.encode("ascii")
194229

0 commit comments

Comments
 (0)
0