From 9b1a609ebb17b4e544215298bd37949d151eda7f Mon Sep 17 00:00:00 2001 From: ppiasek <54940022+ppiasek@users.noreply.github.com> Date: Thu, 21 Nov 2024 07:33:18 +0100 Subject: [PATCH] Fix is_ignored_via_amend and add UTs (#338) Co-authored-by: Patryk Piasek --- refurb/main.py | 14 +++++++++----- test/test_main.py | 27 +++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/refurb/main.py b/refurb/main.py index 28b736e..40ff7f6 100644 --- a/refurb/main.py +++ b/refurb/main.py @@ -101,20 +101,24 @@ def is_ignored_via_amend(error: Error, settings: Settings) -> bool: assert error.filename path = Path(error.filename).resolve() - error_code = ErrorCode.from_error(type(error)) + error_code = str(ErrorCode.from_error(type(error))) config_root = Path(settings.config_file).parent if settings.config_file else Path() + errors_to_ignore = [] + categories_to_ignore = [] for ignore in settings.ignore: if ignore.path: ignore_path = (config_root / ignore.path).resolve() if path.is_relative_to(ignore_path): if isinstance(ignore, ErrorCode): - return str(ignore) == str(error_code) + errors_to_ignore.append(str(ignore)) + else: + categories_to_ignore.append(ignore.value) - return ignore.value in error.categories - - return False + return error_code in errors_to_ignore or any( + category in categories_to_ignore for category in error.categories + ) def should_ignore_error(error: Error | str, settings: Settings) -> bool: diff --git a/test/test_main.py b/test/test_main.py index 3636388..07c1d5b 100644 --- a/test/test_main.py +++ b/test/test_main.py @@ -10,8 +10,8 @@ import pytest -from refurb.error import Error -from refurb.main import main, run_refurb, sort_errors +from refurb.error import Error, ErrorCategory, ErrorClassifier, ErrorCode +from refurb.main import is_ignored_via_amend, main, run_refurb, sort_errors from refurb.settings import Settings, load_settings, parse_command_line_args @@ -323,3 +323,26 @@ def test_error_github_actions_formatting(): p.assert_called_once() assert "::error" in p.call_args[0][0] + + +@pytest.mark.parametrize( + ("ignore_set", "expected"), + [ + (set(), False), + ({ErrorCode(123, path=Path())}, True), + ({ErrorCode(321, path=Path())}, False), + ({ErrorCode(123, path=Path("test/inner"))}, False), + ({ErrorCategory("pythonic", path=Path())}, True), + ({ErrorCategory("pythonic", path=Path("test/inner"))}, False), + ({ErrorCategory("other", path=Path())}, False), + ], +) +def test_is_ignored_via_amend(ignore_set: set[ErrorClassifier], expected: bool) -> None: + class Error123(Error): + code = 123 + categories = ("pythonic", "builtin") + name = "test-error" + + settings = Settings(ignore=ignore_set) + error = Error123(line=1, column=1, msg="Error msg.", filename="test/error.py") + assert is_ignored_via_amend(error, settings) is expected