Skip to content

Commit

Permalink
fix: support crate-ci/typos in addition to codespell (#460)
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Schreiner <[email protected]>
  • Loading branch information
henryiii authored Aug 5, 2024
1 parent eadf2fd commit e8bf4e0
Show file tree
Hide file tree
Showing 4 changed files with 233 additions and 41 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -363,10 +363,10 @@ for family, grp in itertools.groupby(collected.checks.items(), key=lambda x: x[1
- [`PC100`](https://learn.scientific-python.org/development/guides/style#PC100): Has pre-commit-hooks
- [`PC110`](https://learn.scientific-python.org/development/guides/style#PC110): Uses black or ruff-format
- [`PC111`](https://learn.scientific-python.org/development/guides/style#PC111): Uses blacken-docs
- [`PC140`](https://learn.scientific-python.org/development/guides/style#PC140): Uses mypy
- [`PC160`](https://learn.scientific-python.org/development/guides/style#PC160): Uses codespell
- [`PC140`](https://learn.scientific-python.org/development/guides/style#PC140): Uses a type checker
- [`PC160`](https://learn.scientific-python.org/development/guides/style#PC160): Uses a spell checker
- [`PC170`](https://learn.scientific-python.org/development/guides/style#PC170): Uses PyGrep hooks (only needed if rST present)
- [`PC180`](https://learn.scientific-python.org/development/guides/style#PC180): Uses prettier
- [`PC180`](https://learn.scientific-python.org/development/guides/style#PC180): Uses a markdown formatter
- [`PC190`](https://learn.scientific-python.org/development/guides/style#PC190): Uses Ruff
- [`PC191`](https://learn.scientific-python.org/development/guides/style#PC191): Ruff show fixes if fixes enabled
- [`PC901`](https://learn.scientific-python.org/development/guides/style#PC901): Custom pre-commit CI message
Expand Down
2 changes: 1 addition & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ def gha_bump(session: nox.Session) -> None:
# -- Repo review --


@nox.session
@nox.session(tags=["gen"])
def readme(session: nox.Session) -> None:
"""
Update the readme with cog. Pass --check to check instead.
Expand Down
101 changes: 64 additions & 37 deletions src/sp_repo_review/checks/precommit.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,98 +24,125 @@ class PreCommit:
family = "pre-commit"
requires = {"PY006"}
url = mk_url("style")
renamed: ClassVar[str | None] = None
repo: ClassVar[str | None] = None
renamed: ClassVar[dict[str, str]] = {}
repos: ClassVar[set[str]] = set()
ids: ClassVar[dict[str, str]] = {}

@property
def describe(self) -> str:
msgs = sorted(
f"`{r}` (with `{self.ids[r]}` hook)" if r in self.ids else f"`{r}`"
for r in self.repos
)
if not msgs:
return "..."
if len(msgs) == 1:
return msgs[0]
return "one of " + ", ".join(msgs)

@classmethod
def check(cls, precommit: dict[str, Any]) -> bool | None | str:
"Must have `{self.repo}` repo in `.pre-commit-config.yaml`"
assert cls.repo is not None
for repo in precommit.get("repos", {}):
if repo.get("repo", "").lower() == cls.repo:
return True
if cls.renamed is not None and repo.get("repo", "").lower() == cls.renamed:
return f"Use `{cls.repo}` instead of `{cls.renamed}` in `.pre-commit-config.yaml`"
"Must have {self.describe} in `.pre-commit-config.yaml`"
assert cls.repos, f"{cls.__name__} must have a repo, invalid class definition"
for repo_item in precommit.get("repos", {}):
repo = repo_item.get("repo", "").lower()
if not repo:
continue
if repo in cls.repos:
if cls.ids and repo in cls.ids:
if any(
hook.get("id", "") == cls.ids[repo]
for hook in repo_item.get("hooks", {})
):
return True
else:
return True
if cls.renamed and repo in cls.renamed:
rename = cls.renamed[repo]
return (
f"Use `{rename}` instead of `{repo}` in `.pre-commit-config.yaml`"
)
return False


class PC100(PreCommit):
"Has pre-commit-hooks"

repo = "https://github.com/pre-commit/pre-commit-hooks"
repos = {"https://github.com/pre-commit/pre-commit-hooks"}


class PC110(PreCommit):
"Uses black or ruff-format"

repo = "https://github.com/psf/black-pre-commit-mirror"
renamed = "https://github.com/psf/black"
alternate = "https://github.com/astral-sh/ruff-pre-commit"

@classmethod
def check(cls, precommit: dict[str, Any]) -> bool | None | str:
"Must have `{self.repo}` or `{self.alternate}` + `id: ruff-format` in `.pre-commit-config.yaml`"
for repo in precommit.get("repos", {}):
if repo.get("repo", "").lower() == cls.alternate and any(
hook.get("id", "") == "ruff-format" for hook in repo.get("hooks", {})
):
return True

return super().check(precommit)
repos = {
"https://github.com/psf/black-pre-commit-mirror",
"https://github.com/astral-sh/ruff-pre-commit",
}
renamed = {
"https://github.com/psf/black": "https://github.com/psf/black-pre-commit-mirror"
}
ids = {"https://github.com/astral-sh/ruff-pre-commit": "ruff-format"}


class PC111(PreCommit):
"Uses blacken-docs"

requires = {"PY006", "PC110"}
repo = "https://github.com/adamchainz/blacken-docs"
renamed = "https://github.com/asottile/blacken-docs"
repos = {"https://github.com/adamchainz/blacken-docs"}
renamed = {
"https://github.com/asottile/blacken-docs": "https://github.com/adamchainz/blacken-docs"
}


class PC190(PreCommit):
"Uses Ruff"

repo = "https://github.com/astral-sh/ruff-pre-commit"
renamed = "https://github.com/charliermarsh/ruff-pre-commit"
repos = {"https://github.com/astral-sh/ruff-pre-commit"}
renamed = {
"https://github.com/charliermarsh/ruff-pre-commit": "https://github.com/astral-sh/ruff-pre-commit"
}


class PC140(PreCommit):
"Uses mypy"
"Uses a type checker"

repo = "https://github.com/pre-commit/mirrors-mypy"
repos = {"https://github.com/pre-commit/mirrors-mypy"}


class PC160(PreCommit):
"Uses codespell"
"Uses a spell checker"

repo = "https://github.com/codespell-project/codespell"
repos = {
"https://github.com/codespell-project/codespell",
"https://github.com/crate-ci/typos",
}


class PC170(PreCommit):
"Uses PyGrep hooks (only needed if rST present)"

repo = "https://github.com/pre-commit/pygrep-hooks"
repos = {"https://github.com/pre-commit/pygrep-hooks"}


class PC180(PreCommit):
"Uses prettier"
"Uses a markdown formatter"

repo = "https://github.com/pre-commit/mirrors-prettier"
repos = {"https://github.com/pre-commit/mirrors-prettier"}


class PC191(PreCommit):
"Ruff show fixes if fixes enabled"

requires = {"PC190"}
repo = "https://github.com/astral-sh/ruff-pre-commit"
repos = {"https://github.com/astral-sh/ruff-pre-commit"}

@classmethod
def check(cls, precommit: dict[str, Any]) -> bool | None:
"""
If `--fix` is present, `--show-fixes` must be too.
"""
for repo in precommit.get("repos", {}):
if "repo" in repo and repo["repo"].lower() == cls.repo:
if "repo" in repo and repo["repo"].lower() in cls.repos:
for hook in repo["hooks"]:
if (
hook["id"] == "ruff"
Expand Down
165 changes: 165 additions & 0 deletions tests/test_precommit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
import yaml
from repo_review.testing import compute_check


def test_pc100():
precommit = yaml.safe_load("""
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
""")
assert compute_check("PC100", precommit=precommit).result


def test_pc110_black():
precommit = yaml.safe_load("""
repos:
- repo: https://github.com/psf/black-pre-commit-mirror
""")
assert compute_check("PC110", precommit=precommit).result


def test_pc110_ruff():
precommit = yaml.safe_load("""
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
hooks:
- id: ruff-format
""")
assert compute_check("PC110", precommit=precommit).result


def test_pc110_ruff_no_hook():
precommit = yaml.safe_load("""
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
""")
res = compute_check("PC110", precommit=precommit)
assert not res.result
assert "ruff-format" in res.err_msg


def test_pc110_rename():
precommit = yaml.safe_load("""
repos:
- repo: https://github.com/psf/black
""")
res = compute_check("PC110", precommit=precommit)
assert not res.result
assert "black-pre-commit-mirror" in res.err_msg


def test_pc111():
precommit = yaml.safe_load("""
repos:
- repo: https://github.com/adamchainz/blacken-docs
""")
assert compute_check("PC111", precommit=precommit).result


def test_pc111_rename():
precommit = yaml.safe_load("""
repos:
- repo: https://github.com/asottile/blacken-docs
""")
res = compute_check("PC111", precommit=precommit)
assert not res.result
assert "adamchainz" in res.err_msg


def test_pc190():
precommit = yaml.safe_load("""
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
""")
assert compute_check("PC190", precommit=precommit).result


def test_pc190_rename():
precommit = yaml.safe_load("""
repos:
- repo: https://github.com/charliermarsh/ruff-pre-commit
""")
res = compute_check("PC190", precommit=precommit)
assert not res.result
assert "astral-sh" in res.err_msg


def test_pc140():
precommit = yaml.safe_load("""
repos:
- repo: https://github.com/pre-commit/mirrors-mypy
""")
assert compute_check("PC140", precommit=precommit).result


def test_pc160_codespell():
precommit = yaml.safe_load("""
repos:
- repo: https://github.com/codespell-project/codespell
""")
assert compute_check("PC160", precommit=precommit).result


def test_pc160_typos():
precommit = yaml.safe_load("""
repos:
- repo: https://github.com/crate-ci/typos
""")
assert compute_check("PC160", precommit=precommit).result


def test_pc170():
precommit = yaml.safe_load("""
repos:
- repo: https://github.com/pre-commit/pygrep-hooks
""")
assert compute_check("PC170", precommit=precommit).result


def test_pc180():
precommit = yaml.safe_load("""
repos:
- repo: https://github.com/pre-commit/mirrors-prettier
""")
assert compute_check("PC180", precommit=precommit).result


def test_pc191():
precommit = yaml.safe_load("""
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
hooks:
- id: ruff
args: ["--fix", "--show-fixes"]
""")
assert compute_check("PC191", precommit=precommit).result


def test_pc191_no_show_fixes():
precommit = yaml.safe_load("""
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
hooks:
- id: ruff
args: ["--fix"]
""")
res = compute_check("PC191", precommit=precommit)
assert not res.result
assert "--show-fixes" in res.err_msg


def test_pc901():
precommit = yaml.safe_load("""
ci:
autoupdate_commit_msg: 'chore: update pre-commit hooks'
""")
assert compute_check("PC901", precommit=precommit).result


def test_pc901_no_msg():
precommit = yaml.safe_load("""
repos:
""")
res = compute_check("PC901", precommit=precommit)
assert not res.result
assert "autoupdate_commit_msg" in res.err_msg

0 comments on commit e8bf4e0

Please sign in to comment.