Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion scripts/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,22 @@ def generate_changelog(
)


def run_hooks(files: list[str], *, runner: Runner = run) -> None:
"""Run the pre-commit hooks over the release files.

The first pass may rewrite files (formatters, end-of-file-fixer), which
pre-commit reports as a non-zero exit even though nothing is wrong, so
its status is ignored. The second pass must come back clean.

Args:
files: Paths to check, passed to ``pre-commit run --files``.
runner: Command runner, injectable for tests.
"""
command = ["uv", "run", "pre-commit", "run", "--files", *files]
runner(command, check=False)
runner(command)


def main() -> int:
"""Run the release preparation process."""
args = parse_args()
Expand Down Expand Up @@ -294,7 +310,13 @@ def main() -> int:
sync_lockfile()
generate_changelog(tag, prev)

run(["git", "add", "pyproject.toml", "uv.lock", "CHANGELOG.md"])
# Run pre-commit hooks on the modified files before committing, to ensure
# that the commit passes all checks.
updated_files = ["pyproject.toml", "uv.lock", "CHANGELOG.md"]
run_hooks(updated_files)

# Commit the changes and create an annotated tag for the release.
run(["git", "add", *updated_files])
run(["git", "commit", "-m", f"chore(release): {tag}"])

if not args.no_tag:
Expand Down
21 changes: 21 additions & 0 deletions tests/test_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
generate_changelog,
main,
previous_tag,
run_hooks,
sync_lockfile,
tag_exists,
validate_version,
Expand All @@ -38,6 +39,7 @@ def __init__(
results: dict[str, subprocess.CompletedProcess[str]] | None = None,
) -> None:
self.commands: list[list[str]] = []
self.checks: list[bool] = []
self.results = results or {}

def __call__(
Expand All @@ -48,6 +50,7 @@ def __call__(
capture: bool = False,
) -> subprocess.CompletedProcess[str]:
self.commands.append(command)
self.checks.append(check)
key = " ".join(command)
result = self.results.get(key)
if result is None:
Expand Down Expand Up @@ -199,6 +202,24 @@ def test_sync_lockfile_locks_then_verifies() -> None:
assert runner.commands == [["uv", "lock"], ["uv", "lock", "--check"]]


def test_run_hooks_checks_the_given_files_twice() -> None:
runner = FakeRunner()

run_hooks(["CHANGELOG.md"], runner=runner)

expected = ["uv", "run", "pre-commit", "run", "--files", "CHANGELOG.md"]
assert runner.commands == [expected, expected]


def test_run_hooks_tolerates_files_rewritten_by_the_first_pass() -> None:
"""A hook that fixes a file exits non-zero; only the retry must pass."""
runner = FakeRunner()

run_hooks(["CHANGELOG.md"], runner=runner)

assert runner.checks == [False, True]


def test_changelog_range_is_empty_without_a_previous_tag() -> None:
assert changelog_range(None) == []

Expand Down
Loading