Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use return value 1 always if there are linter errors #34

Merged
merged 10 commits into from
Apr 2, 2024
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Added
- In the future test, upgrade ``Pygments`` to repository ``master``.
- Messages from future test are now generic, not Black-specific.
- Require ``click`` when running tests.
- Linter failures now result in an exit value of 1. This makes Graylint compatible with
``pre-commit``.

Removed
-------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,10 @@ def test_creates_virtualenv(tmp_path, main_patch):

@pytest.mark.kwparametrize(
dict(run_main_env={}, expect=["graylint[color]"]),
dict(
run_main_env={"INPUT_VERSION": "1.5.0"}, expect=["graylint[color]==1.5.0"]
),
dict(run_main_env={"INPUT_VERSION": "1.5.0"}, expect=["graylint[color]==1.5.0"]),
dict(
run_main_env={"INPUT_VERSION": "@main"},
expect=[
"git+https://github.com/akaihola/graylint@main#egg=graylint[color]"
],
expect=["git+https://github.com/akaihola/graylint@main#egg=graylint[color]"],
),
dict(
run_main_env={"INPUT_LINT": "pylint"},
Expand Down
7 changes: 6 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ target-version = ["py38", "py39", "py310", "py311", "py312"]
# Darker makes isort read its configuration from the file indicated by the `--config`
# option, so we need to mirror the same configuration here and in `check-graylint.toml`.
profile = "black"
known_first_party = ["darkgraylib", "graylint"]
known_third_party = ["pytest"]

[tool.darker]
Expand Down Expand Up @@ -42,10 +43,14 @@ ignore = [
]

[tool.ruff.lint.per-file-ignores]
"src/darker/tests/*.py" = [
"src/graylint/tests/*.py" = [
"ANN001", # Missing type annotation for function argument
"ANN201", # Missing return type annotation for public function
"ANN204", # Missing return type annotation for special method `__init__`
"C408", # Unnecessary `dict` call (rewrite as a literal)
"S101", # Use of `assert` detected
]

[tool.ruff.lint.isort]
known-first-party = ["darkgraylib", "graylint"]
known-third-party = ["pytest"]
10 changes: 5 additions & 5 deletions src/graylint/__main__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Graylint - run linters and hide linter errors already present in previous revision"""

from __future__ import annotations

import logging
import sys
from argparse import ArgumentError
Expand All @@ -10,12 +12,10 @@
from darkgraylib.highlighting import should_use_color
from darkgraylib.log import setup_logging
from darkgraylib.main import resolve_paths

from graylint.command_line import make_argument_parser
from graylint.config import GraylintConfig
from graylint.linting import run_linters


logger = logging.getLogger(__name__)


Expand All @@ -29,14 +29,14 @@ def main_with_error_handling() -> int:
sys.exit(str(exc_info))


def main() -> int:
def main(argv: list[str] = None) -> int:
"""Parse the command line and lint each source file

:return: Total number of linting errors found on modified lines

"""
args, config, config_nondefault = parse_command_line(
make_argument_parser, sys.argv[1:], "graylint", GraylintConfig
make_argument_parser, argv, "graylint", GraylintConfig
)
setup_logging(args.log_level)
show_config_if_debug(config, config_nondefault, args.log_level)
Expand All @@ -52,7 +52,7 @@ def main() -> int:
revrange,
use_color=should_use_color(config["color"]),
)
return linter_failures_on_modified_lines
return 1 if linter_failures_on_modified_lines else 0


if __name__ == "__main__":
Expand Down
36 changes: 36 additions & 0 deletions src/graylint/tests/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""Tests for the `graylint.__main__` module."""

from unittest.mock import Mock, patch

import pytest

from graylint.__main__ import main


@pytest.mark.kwparametrize(
dict(numfails=0, expect_retval=0),
dict(numfails=1, expect_retval=1),
dict(numfails=2, expect_retval=1),
)
def test_main_retval(numfails, expect_retval):
"""main() return value is correct based on linter results."""
with patch("graylint.__main__.run_linters", Mock(return_value=numfails)):
# end of test setup

retval = main(["a.py"])

assert retval == expect_retval


@pytest.mark.kwparametrize(
dict(arguments=[], expect_retval=0),
dict(arguments=["--lint", "echo subdir/a.py:1: message"], expect_retval=1),
)
def test_main(git_repo, arguments, expect_retval):
"""Main function return value is 1 if there are linter errors."""
paths = git_repo.add({"subdir/a.py": "\n"}, commit="Initial commit")
paths["subdir/a.py"].write_text("Foo\n")

retval = main([*arguments, str(git_repo.root)])

assert retval == expect_retval
Loading