Skip to content

Commit

Permalink
Skip linting a file if no rules are enabled (#379)
Browse files Browse the repository at this point in the history
To avoid unnecessary processing of files if no rules are enabled for a given path then a clean Result is yielded.
  • Loading branch information
datur authored Aug 22, 2023
1 parent 6f71385 commit 5fbcbdc
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
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)

0 comments on commit 5fbcbdc

Please sign in to comment.