diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 50c073a0..24b155d3 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -13,7 +13,7 @@ repos: - id: black - repo: https://github.com/PyCQA/autoflake - rev: v2.0.1 + rev: v2.0.2 hooks: - id: autoflake @@ -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 @@ -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] diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml new file mode 100644 index 00000000..aba3df0e --- /dev/null +++ b/.pre-commit-hooks.yaml @@ -0,0 +1,6 @@ +--- + - id: flake8_trio + name: flake8_trio + entry: flake8_trio + language: python + types: [python] diff --git a/flake8_trio/__init__.py b/flake8_trio/__init__.py index 8091c281..7d51cbd7 100644 --- a/flake8_trio/__init__.py +++ b/flake8_trio/__init__.py @@ -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="*", @@ -105,17 +105,20 @@ 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(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: diff --git a/flake8_trio/__main__.py b/flake8_trio/__main__.py index 393c2c0b..4f399f5b 100644 --- a/flake8_trio/__main__.py +++ b/flake8_trio/__main__.py @@ -1,4 +1,6 @@ """Entry file when executed with `python -m`.""" +import sys + from . import main -main() +sys.exit(main()) diff --git a/setup.py b/setup.py index 3a27e8f1..7213c483 100755 --- a/setup.py +++ b/setup.py @@ -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", diff --git a/tests/test_config_and_args.py b/tests/test_config_and_args.py index 4f3c32d1..da59a621 100644 --- a/tests/test_config_and_args.py +++ b/tests/test_config_and_args.py @@ -8,7 +8,7 @@ import pytest -from flake8_trio import Plugin +from flake8_trio import Plugin, main from .test_flake8_trio import initialize_options @@ -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) @@ -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") @@ -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 @@ -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 @@ -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")