Skip to content

Commit

Permalink
FIX: intelligently exclude Python files from editorconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
redeboer committed Aug 7, 2024
1 parent d722bf7 commit 2300dd9
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/compwa_policy/check_dev_files/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def main(argv: Sequence[str] | None = None) -> int:
do(cspell.main, precommit_config, args.no_cspell_update)
do(dependabot.main, args.dependabot)
do(direnv.main)
do(editorconfig.main, precommit_config, args.no_python)
do(editorconfig.main, precommit_config)
if not args.allow_labels:
do(github_labels.main)
if not args.no_github_actions:
Expand Down
9 changes: 5 additions & 4 deletions src/compwa_policy/check_dev_files/editorconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,25 @@
from ruamel.yaml.scalarstring import FoldedScalarString

from compwa_policy.utilities import CONFIG_PATH
from compwa_policy.utilities.match import filter_files
from compwa_policy.utilities.precommit.struct import Hook, Repo

if TYPE_CHECKING:
from compwa_policy.utilities.precommit import ModifiablePrecommit


def main(precommit: ModifiablePrecommit, no_python: bool) -> None:
def main(precommit: ModifiablePrecommit) -> None:
if CONFIG_PATH.editorconfig.exists():
_update_precommit_config(precommit, no_python)
_update_precommit_config(precommit)


def _update_precommit_config(precommit: ModifiablePrecommit, no_python: bool) -> None:
def _update_precommit_config(precommit: ModifiablePrecommit) -> None:
hook = Hook(
id="editorconfig-checker",
name="editorconfig",
alias="ec",
)
if not no_python:
if filter_files(["**/*.py"]):
msg = R"""
(?x)^(
.*\.py
Expand Down

This file was deleted.

11 changes: 3 additions & 8 deletions tests/check_dev_files/editorconfig/test_editorconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
from compwa_policy.utilities.precommit import ModifiablePrecommit


@pytest.mark.parametrize("no_python", [True, False])
def test_update_precommit_config(no_python: bool):
def test_update_precommit_config():
this_dir = Path(__file__).parent
with open(this_dir / ".pre-commit-config-bad.yaml") as file:
src = file.read()
Expand All @@ -18,13 +17,9 @@ def test_update_precommit_config(no_python: bool):
with pytest.raises(
PrecommitError, match=r"Updated editorconfig-checker hook"
), ModifiablePrecommit.load(stream) as precommit:
_update_precommit_config(precommit, no_python)
_update_precommit_config(precommit)

result = precommit.dumps()
if no_python:
expected_file = this_dir / ".pre-commit-config-good-no-python.yaml"
else:
expected_file = this_dir / ".pre-commit-config-good.yaml"
with open(expected_file) as file:
with open(this_dir / ".pre-commit-config-good.yaml") as file:
expected = file.read()
assert result == expected

0 comments on commit 2300dd9

Please sign in to comment.