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

Skip files if no rules are enabled #379

Merged
merged 1 commit into from
Aug 22, 2023
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
9 changes: 8 additions & 1 deletion src/fixit/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ def fixit_bytes(
Lint raw bytes content representing a single path, using the given configuration.

Yields :class:`Result` objects for each lint error or exception found, or a single
empty result if the file is clean.
empty result if the file is clean. A file is considered clean if no lint errors or
no rules are enabled for the given path.
Returns the final :class:`FileContent` including any fixes applied.

Use :func:`capture` to more easily capture return value after iterating through
Expand All @@ -82,9 +83,15 @@ def fixit_bytes(

If ``autofix`` is ``True``, all violations with replacements will be applied
automatically, even if ``False`` is sent back to the generator.

"""
try:
rules = collect_rules(config)

if not rules:
yield Result(path, violation=None)
return None

runner = LintRunner(path, content)
pending_fixes: List[LintViolation] = []

Expand Down
48 changes: 48 additions & 0 deletions src/fixit/tests/smoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,51 @@ def foo():
sorted(errors[multi]),
)
self.assertEqual(expected, multi.read_text())

def test_lint_directory_with_no_rules_enabled(self) -> None:
content = dedent(
"""\
import foo
import bar

def func():
value = f"hello world"
"""
)
with self.subTest("lint"):
with TemporaryDirectory() as td:
tdp = Path(td).resolve()
path = tdp / "file.py"

(tdp / "pyproject.toml").write_text(
"[tool.fixit]\ndisable=['fixit.rules']\n"
)

path.write_text(content)
result = self.runner.invoke(
main,
["lint", path.as_posix()],
catch_exceptions=False,
)

self.assertEqual(result.output, "")
self.assertEqual(result.exit_code, 0)

with self.subTest("fix"):
with TemporaryDirectory() as td:
tdp = Path(td).resolve()
path = tdp / "file.py"

(tdp / "pyproject.toml").write_text(
"[tool.fixit]\ndisable=['fixit.rules']\n"
)

path.write_text(content)
result = self.runner.invoke(
main,
["fix", "--automatic", path.as_posix()],
catch_exceptions=False,
)

self.assertEqual(result.output, "")
self.assertEqual(result.exit_code, 0)