diff --git a/pyproject.toml b/pyproject.toml index 013e0caa..577b168b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -159,6 +159,10 @@ messages_control.disable = [ ] +[tool.ruff] +show-fixes = true +extend-exclude = ["\\{\\{cookiecutter.project_name\\}\\}"] + [tool.ruff.lint] extend-select = [ "ARG", # flake8-unused-arguments diff --git a/src/sp_repo_review/checks/precommit.py b/src/sp_repo_review/checks/precommit.py index ce437786..67189b2c 100644 --- a/src/sp_repo_review/checks/precommit.py +++ b/src/sp_repo_review/checks/precommit.py @@ -245,5 +245,14 @@ def check(precommit: dict[str, Any]) -> bool: return "autoupdate_schedule" in precommit.get("ci", {}) -def repo_review_checks() -> dict[str, PreCommit]: +def repo_review_checks( + list_all: bool = True, + root: Traversable | None = None, +) -> dict[str, PreCommit]: + if root and not list_all: + precommit_path = root.joinpath(".pre-commit-config.yaml") + lefthook_path = root.joinpath("lefthook.yml") + if not precommit_path.is_file() and lefthook_path.is_file(): + return {} + return {p.__name__: p() for p in PreCommit.__subclasses__()} diff --git a/tests/test_precommit.py b/tests/test_precommit.py index 544252a8..618a39c7 100644 --- a/tests/test_precommit.py +++ b/tests/test_precommit.py @@ -1,7 +1,11 @@ +from pathlib import Path + import pytest import yaml from repo_review.testing import compute_check +from sp_repo_review.checks.precommit import repo_review_checks + @pytest.fixture(params=["ruff", "ruff-check"]) def ruff_check(request: pytest.FixtureRequest) -> str: @@ -283,3 +287,17 @@ def test_pc903_no_msg(): res = compute_check("PC903", precommit=precommit) assert not res.result assert "autoupdate_schedule" in res.err_msg + + +def test_repo_review_checks_skips_with_lefthook_only(tmp_path: Path) -> None: + """PreCommit checks should be omitted if only lefthook.yml is present. + + When a repository uses `lefthook.yml` and does not have a + `.pre-commit-config.yaml`, `repo_review_checks` should return an empty + mapping when `list_all=False` indicating the pre-commit family is skipped. + """ + # Create only a lefthook configuration + (tmp_path / "lefthook.yml").write_text("hooks:\n", encoding="utf-8") + + checks = repo_review_checks(list_all=False, root=tmp_path) + assert checks == {}