From c4885889c568886a8c885bcde1cc3386bd02be9b Mon Sep 17 00:00:00 2001 From: David Turner Date: Tue, 15 Aug 2023 09:39:03 +0100 Subject: [PATCH] Skip files if no rules are enabled 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. --- src/fixit/api.py | 8 ++++++- src/fixit/tests/smoke.py | 48 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/src/fixit/api.py b/src/fixit/api.py index cc235fb8..1b2567a3 100644 --- a/src/fixit/api.py +++ b/src/fixit/api.py @@ -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. @@ -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") diff --git a/src/fixit/tests/smoke.py b/src/fixit/tests/smoke.py index 53ee7dd8..ef746eea 100644 --- a/src/fixit/tests/smoke.py +++ b/src/fixit/tests/smoke.py @@ -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)