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

Fix is_ignored_via_amend and add UTs #338

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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