Skip to content

Commit

Permalink
Fix is_ignored_via_amend and add UTs (#338)
Browse files Browse the repository at this point in the history
Co-authored-by: Patryk Piasek <[email protected]>
  • Loading branch information
ppiasek and Patryk Piasek authored Nov 21, 2024
1 parent 27fc566 commit 9b1a609
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
14 changes: 9 additions & 5 deletions refurb/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
27 changes: 25 additions & 2 deletions test/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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

0 comments on commit 9b1a609

Please sign in to comment.