Skip to content

Commit

Permalink
add pre-commit hook
Browse files Browse the repository at this point in the history
  • Loading branch information
jakkdl committed Mar 21, 2023
1 parent 033317a commit 6213187
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 11 deletions.
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ repos:
- id: black

- repo: https://github.com/PyCQA/autoflake
rev: v2.0.1
rev: v2.0.2
hooks:
- id: autoflake

Expand All @@ -34,7 +34,7 @@ repos:
- id: mypy

- repo: https://github.com/RobertCraigie/pyright-python
rev: v1.1.298
rev: v1.1.299
hooks:
- id: pyright
entry: env PYRIGHT_PYTHON_FORCE_VERSION=latest pyright
Expand Down Expand Up @@ -109,7 +109,7 @@ repos:
- id: yamlfmt

- repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
rev: v2.7.0
rev: v2.8.0
hooks:
- id: pretty-format-toml
args: [--autofix]
6 changes: 6 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
- id: flake8_trio
name: flake8_trio
entry: flake8_trio
language: python
types: [python]
9 changes: 7 additions & 2 deletions flake8_trio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def cst_parse_module_native(source: str) -> cst.Module:
return mod


def main():
def main() -> int:
parser = ArgumentParser(prog="flake8_trio")
parser.add_argument(
nargs="*",
Expand Down Expand Up @@ -105,17 +105,22 @@ def main():
"Doesn't seem to be a git repo; pass filenames to format.",
file=sys.stderr,
)
sys.exit(1)
return 1
all_filenames = [
os.path.join(root, f) for f in all_filenames if _should_format(f)
]
any_error = False
for file in all_filenames:
plugin = Plugin.from_filename(file)
for error in sorted(plugin.run()):
# print to stdout, when run through pre-commit stderr are always printed
# and stdout is printed if process exits with status 1
print(f"{file}:{error}")
any_error = True
if plugin.options.autofix:
with open(file, "w") as file:
file.write(plugin.module.code)
return 1 if any_error else 0


class Plugin:
Expand Down
4 changes: 3 additions & 1 deletion flake8_trio/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Entry file when executed with `python -m`."""
import sys

from . import main

main()
sys.exit(main())
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def local_file(name: str) -> Path:
license="MIT",
description="A highly opinionated flake8 plugin for Trio-related problems.",
zip_safe=False,
install_requires=["flake8"],
install_requires=["flake8", "libcst"],
python_requires=">=3.9",
classifiers=[
"Development Status :: 3 - Alpha",
Expand Down
43 changes: 39 additions & 4 deletions tests/test_config_and_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import pytest

from flake8_trio import Plugin
from flake8_trio import Plugin, main

from .test_flake8_trio import initialize_options

Expand Down Expand Up @@ -36,10 +36,44 @@ def test_run_flake8_trio(tmp_path: Path):
cwd=tmp_path,
capture_output=True,
)
assert res.returncode == 1
assert not res.stderr
assert res.stdout == err_msg.encode("ascii")


def test_systemexit_0(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
):
monkeypatch.chdir(tmp_path)
monkeypatch.setattr(sys, "argv", [tmp_path / "flake8_trio", "./example.py"])

tmp_path.joinpath("example.py").write_text("")

with pytest.raises(SystemExit) as exc_info:
from flake8_trio import __main__ # noqa

assert exc_info.value.code == 0
out, err = capsys.readouterr()
assert not out
assert not err


def test_systemexit_1(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
):
err_msg = _common_error_setup(tmp_path)
monkeypatch.chdir(tmp_path)
monkeypatch.setattr(sys, "argv", [tmp_path / "flake8_trio", "./example.py"])

with pytest.raises(SystemExit) as exc_info:
from flake8_trio import __main__ # noqa

assert exc_info.value.code == 1
out, err = capsys.readouterr()
assert out == err_msg
assert not err


def test_run_in_git_repo(tmp_path: Path):
err_msg = _common_error_setup(tmp_path)
assert subprocess.run(["git", "init"], cwd=tmp_path, capture_output=True)
Expand All @@ -51,6 +85,7 @@ def test_run_in_git_repo(tmp_path: Path):
cwd=tmp_path,
capture_output=True,
)
assert res.returncode == 1
assert not res.stderr
assert res.stdout == err_msg.encode("ascii")

Expand All @@ -60,8 +95,7 @@ def test_run_no_git_repo(
):
monkeypatch.chdir(tmp_path)
monkeypatch.setattr(sys, "argv", [tmp_path / "flake8_trio"])
with pytest.raises(SystemExit):
from flake8_trio import __main__ # noqa
assert main() == 1
out, err = capsys.readouterr()
assert err == "Doesn't seem to be a git repo; pass filenames to format.\n"
assert not out
Expand All @@ -75,7 +109,7 @@ def test_run_100_autofix(
monkeypatch.setattr(
sys, "argv", [tmp_path / "flake8_trio", "--autofix", "./example.py"]
)
from flake8_trio import __main__ # noqa
assert main() == 1

out, err = capsys.readouterr()
assert out == err_msg
Expand Down Expand Up @@ -189,6 +223,7 @@ def test_200_from_config_flake8_internals(
def test_200_from_config_subprocess(tmp_path: Path):
err_msg = _test_trio200_from_config_common(tmp_path)
res = subprocess.run(["flake8"], cwd=tmp_path, capture_output=True)
assert res.returncode == 1
assert not res.stderr
assert res.stdout == err_msg.encode("ascii")

Expand Down

0 comments on commit 6213187

Please sign in to comment.