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
14 changes: 14 additions & 0 deletions src/usethis/_integrations/pre_commit/version.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
from usethis._config import usethis_config
from usethis._integrations.pre_commit.io_ import read_pre_commit_config_yaml

PRE_COMMIT_VERSION = "4.5.1"


def get_pre_commit_version() -> str:
"""Get an inferred pre-commit version for usethis to target.

If no version can be inferred, a hard-coded version is used, corresponding to a
recent release (see `PRE_COMMIT_VERSION`).
"""
version = get_minimum_pre_commit_version()
if version is not None:
return version
return PRE_COMMIT_VERSION


def get_minimum_pre_commit_version() -> str | None:
"""Get the declared minimum supported pre-commit version from the configuration.
Expand Down
51 changes: 50 additions & 1 deletion tests/usethis/_integrations/pre_commit/test_version.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from pathlib import Path

from usethis._integrations.pre_commit.version import get_minimum_pre_commit_version
from usethis._integrations.pre_commit.version import (
PRE_COMMIT_VERSION,
get_minimum_pre_commit_version,
get_pre_commit_version,
)
from usethis._test import change_cwd


Expand Down Expand Up @@ -47,3 +51,48 @@ def test_minimum_version_not_declared(self, tmp_path: Path):

# Assert
assert result is None


class TestGetPreCommitVersion:
def test_uses_declared_minimum_version(self, tmp_path: Path):
# Arrange
(tmp_path / ".pre-commit-config.yaml").write_text("""\
minimum_pre_commit_version: '3.5.0'
repos:
- repo: https://github.com/abravalheri/validate-pyproject
rev: v0.23
hooks:
- id: validate-pyproject
""")

# Act
with change_cwd(tmp_path):
result = get_pre_commit_version()

# Assert
assert result == "3.5.0"

def test_falls_back_to_hardcoded_when_config_doesnt_exist(self, tmp_path: Path):
# Act
with change_cwd(tmp_path):
result = get_pre_commit_version()

# Assert
assert result == PRE_COMMIT_VERSION

def test_falls_back_to_hardcoded_when_minimum_not_declared(self, tmp_path: Path):
# Arrange
(tmp_path / ".pre-commit-config.yaml").write_text("""\
repos:
- repo: https://github.com/abravalheri/validate-pyproject
rev: v0.23
hooks:
- id: validate-pyproject
""")

# Act
with change_cwd(tmp_path):
result = get_pre_commit_version()

# Assert
assert result == PRE_COMMIT_VERSION
22 changes: 22 additions & 0 deletions tests/usethis/_tool/impl/test_pre_commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from usethis._integrations.ci.github.errors import GitHubTagError
from usethis._integrations.ci.github.tags import get_github_latest_tag
from usethis._integrations.pre_commit.schema import UriRepo
from usethis._integrations.pre_commit.version import PRE_COMMIT_VERSION
from usethis._test import change_cwd
from usethis._tool.impl.pre_commit import PreCommitTool

Expand Down Expand Up @@ -58,3 +59,24 @@ def test_latest_sync_with_uv_version(self, tmp_path: Path):
"Failed to fetch GitHub tags (connection issues); skipping test"
)
raise err

@pytest.mark.usefixtures("_vary_network_conn")
def test_latest_version(self):
if os.getenv("CI"):
pytest.skip("Avoid flaky pipelines by testing version bumps manually")

try:
assert (
get_github_latest_tag(owner="pre-commit", repo="pre-commit")
== f"v{PRE_COMMIT_VERSION}"
)
except GitHubTagError as err:
if (
usethis_config.offline
or "rate limit exceeded for url" in str(err)
or "Read timed out." in str(err)
):
pytest.skip(
"Failed to fetch GitHub tags (connection issues); skipping test"
)
raise err