Skip to content

Commit

Permalink
Skip files if no rules are enabled
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 file the config is now checked before processing a file and if no rules are enabled the file is skipped.
  • Loading branch information
datur committed Aug 15, 2023
1 parent 79bed4c commit c488588
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/fixit/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ def fixit_file(
"""
Lint a single file on disk, detecting and generating appropriate configuration.
If no rules are enabled for the file, the file will be skipped.
Generates a merged :ref:`configuration` based on all applicable config files.
Reads file from disk as raw bytes, and uses :func:`fixit_bytes` to lint and apply
any fixes to the content. Writes content back to disk if changes are detected.
Expand All @@ -130,9 +132,13 @@ def fixit_file(
path = path.resolve()

try:
content: FileContent = path.read_bytes()
config = generate_config(path, options=options)

if not config.enable:
return

content: FileContent = path.read_bytes()

updated = yield from fixit_bytes(path, content, config=config, autofix=autofix)
if updated and updated != content:
LOG.info(f"{path}: writing changes to file")
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 c488588

Please sign in to comment.