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
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ repos:
priority: 5
args: ["--fix", "--unsafe-fixes", "--exit-non-zero-on-fix"]
- repo: https://github.com/gaborbernat/simplint-pre-commit
rev: "f15525433ac9797aa1580f46fdadfe60dd77923c" # frozen: v0.8.0
rev: "073bd9250c7a4d15b71eef71f2c95e4af5c76283" # frozen: v0.10.3
hooks:
- id: simplint
priority: 6
Expand Down
35 changes: 14 additions & 21 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -133,32 +133,25 @@ lint.select = [
"ALL",
]
lint.ignore = [
"any-type", # allow Any as type annotation
"CPY", # No copyright statements
"docstring-missing-exception", # not working with Sphinx
"hardcoded-bind-all-interfaces", # Possible binding to all interfaces
"incorrect-blank-line-before-class", # `one-blank-line-before-class` (D203) and `no-blank-line-before-class` (D211) are incompatible
"incorrect-blank-line-before-class", # Conflicts with `blank-line-before-class`
"missing-trailing-comma", # Conflict with formatter
"multi-line-summary-first-line", # `multi-line-summary-first-line` (D212) and `multi-line-summary-second-line` (D213) are incompatible
"non-empty-init-module", # `__init__` module should only contain docstrings and re-exports
"multi-line-summary-first-line", # Conflicts with `multi-line-summary-second-line`
"single-line-implicit-string-concatenation", # Conflict with formatter
"undocumented-magic-method", # undocumented-public-api (simplint SMP903) covers the public API instead
"undocumented-public-class", # undocumented-public-api (simplint SMP903) covers the public API instead
"undocumented-public-function", # undocumented-public-api (simplint SMP903) covers the public API instead
"undocumented-public-init", # undocumented-public-api (simplint SMP903) covers the public API instead
"undocumented-public-method", # undocumented-public-api (simplint SMP903) covers the public API instead
"undocumented-public-nested-class", # undocumented-public-api (simplint SMP903) covers the public API instead
"undocumented-magic-method", # undocumented-public-api (simplint) covers the public API instead
"undocumented-public-class", # undocumented-public-api (simplint) covers the public API instead
"undocumented-public-function", # undocumented-public-api (simplint) covers the public API instead
"undocumented-public-init", # undocumented-public-api (simplint) covers the public API instead
"undocumented-public-method", # undocumented-public-api (simplint) covers the public API instead
"undocumented-public-nested-class", # undocumented-public-api (simplint) covers the public API instead
]
lint.per-file-ignores."tests/**/*.py" = [
"assert", # asserts allowed in tests...
"D", # don"t care about documentation in tests
"FBT", # don"t care about booleans as positional arguments in tests
"implicit-namespace-package", # no implicit namespace
"import-private-name", # private import
"magic-value-comparison", # Magic value used in comparison, consider replacing with a constant variable
"S", # `subprocess` call: check for execution of untrusted input
"too-many-arguments", # any number of arguments in tests
"too-many-positional-arguments", # any number of arguments in tests
"assert", # asserts allowed in tests...
"implicit-namespace-package", # no implicit namespace
"import-private-name", # private import
"subprocess-without-shell-equals-true", # integration tests pass fixed commands and fixture paths to subprocesses
"suspicious-subprocess-import", # integration tests execute pre-commit and its installed hook
"undocumented-public-module", # tests do not need module docstrings
]
lint.isort = { known-first-party = [
"pre_commit_uv",
Expand Down
6 changes: 5 additions & 1 deletion src/pre_commit_uv/_pre_commit_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,11 @@ def _health_check(prefix: Prefix, version: str) -> str | None:
if not pyvenv_cfg.exists():
return "pyvenv.cfg does not exist (old virtualenv?)"

cfg = python._read_pyvenv_cfg(str(pyvenv_cfg)) # ruff:ignore[private-member-access] pre-commit has no public pyvenv.cfg reader
cfg = {}
for line in pyvenv_cfg.read_text(encoding="utf-8").splitlines():
key, separator, value = line.partition("=")
if separator and "=" not in value:
cfg[key.strip()] = value.strip()
if "version_info" not in cfg:
return "created virtualenv's pyvenv.cfg is missing `version_info`"
expected = cfg["version_info"].split(".")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,28 @@

import sys
from importlib.metadata import version
from shutil import which
from subprocess import check_call, check_output
from textwrap import dedent
from typing import TYPE_CHECKING, Final

import pytest
from pre_commit import main
from pre_commit.languages import python
from pre_commit.prefix import Prefix

from pre_commit_uv._pre_commit_patch import _patch
from pre_commit_uv._pre_commit_patch import _health_check, _patch

if TYPE_CHECKING:
from pathlib import Path

_PRECOMMIT_FILE: Final[str] = ".pre-commit-config.yaml"
_UV_VERSION: Final[str] = version("uv")
_PRE_COMMIT_UV_VERSION: Final[str] = version("pre-commit-uv")
_VERSION: Final[str] = "default"
_MAJOR_MINOR: Final[str] = f"{sys.version_info[0]}.{sys.version_info[1]}"
_FULL_VERSION: Final[str] = ".".join(str(p) for p in sys.version_info[0:3])
_WRONG_VERSION: Final[str] = f"{sys.version_info[0]}.{sys.version_info[1] + 1}"


@pytest.fixture
Expand All @@ -32,19 +39,23 @@ def git_repo(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
conf_file.write_text(dedent(conf))
monkeypatch.setenv("PRE_COMMIT_HOME", str(tmp_path / "store"))
monkeypatch.chdir(tmp_path)
check_call(["git", "init"])
git = which("git")
assert git is not None
check_call([git, "init"])
return tmp_path


@pytest.fixture
def install_hook(git_repo: Path) -> None:
check_call(["pre-commit", "install", "--install-hooks", "-c", str(git_repo / _PRECOMMIT_FILE)])
check_call(["pre-commit", "clean"]) # ensures that 'install_environment' gets called
pre_commit = which("pre-commit")
assert pre_commit is not None
check_call([pre_commit, "install", "--install-hooks", "-c", str(git_repo / _PRECOMMIT_FILE)])
check_call([pre_commit, "clean"]) # ensures that 'install_environment' gets called


@pytest.mark.usefixtures("install_hook")
def test_run_precommit_hook() -> None:
hook_result = check_output([".git/hooks/pre-commit"], encoding="utf-8")
def test_run_precommit_hook(git_repo: Path) -> None:
hook_result = check_output([str(git_repo / ".git" / "hooks" / "pre-commit")], encoding="utf-8")
assert (
f"[INFO] Using pre-commit with uv {_UV_VERSION} via pre-commit-uv {_PRE_COMMIT_UV_VERSION}"
in hook_result.splitlines()
Expand Down Expand Up @@ -175,3 +186,68 @@ def test_install_with_uv_config_raises_error(
# would raise SystemExit due to bad config
with pytest.raises(SystemExit):
main.main(["install-hooks", "-c", str(git_repo / _PRECOMMIT_FILE)])


def test_health_check_truncated_version_passes(tmp_path: Path) -> None:
# uv >=0.11.22 writes only the major.minor into pyvenv.cfg (see issue #152).
prefix = _make_env(tmp_path, f"version_info = {_MAJOR_MINOR}\nbase-executable = {sys.executable}\n")

assert _health_check(prefix, _VERSION) is None


def _make_env(tmp_path: Path, pyvenv_cfg: str | None) -> Prefix:
envdir = tmp_path / f"py_env-{_VERSION}"
bin_dir = python.bin_dir(str(envdir))
(tmp_path / bin_dir).mkdir(parents=True)
(tmp_path / bin_dir / python.win_exe("python")).symlink_to(sys.executable)
if pyvenv_cfg is not None:
(envdir / "pyvenv.cfg").write_text(pyvenv_cfg)
return Prefix(str(tmp_path))


def test_health_check_full_version_passes(tmp_path: Path) -> None:
prefix = _make_env(tmp_path, f"version_info = {_FULL_VERSION}\nbase-executable = {sys.executable}\n")

assert _health_check(prefix, _VERSION) is None


def test_health_check_no_base_executable_passes(tmp_path: Path) -> None:
prefix = _make_env(tmp_path, f"version_info = {_MAJOR_MINOR}\n")

assert _health_check(prefix, _VERSION) is None


def test_health_check_version_mismatch_fails(tmp_path: Path) -> None:
result = _health_check(_make_env(tmp_path, f"version_info = {_WRONG_VERSION}\n"), _VERSION)

assert result is not None
assert "virtualenv python version did not match created version" in result
assert f"- expected version: {_WRONG_VERSION}" in result


def test_health_check_base_executable_mismatch_fails(tmp_path: Path) -> None:
prefix = _make_env(tmp_path, f"version_info = {_MAJOR_MINOR}\nbase-executable = /does/not/exist/python\n")

result = _health_check(prefix, _VERSION)

assert result is not None
assert "base executable version did not match created version" in result


def test_health_check_missing_pyvenv_cfg(tmp_path: Path) -> None:
prefix = _make_env(tmp_path, None)

assert _health_check(prefix, _VERSION) == "pyvenv.cfg does not exist (old virtualenv?)"


@pytest.mark.parametrize(
"contents",
[
pytest.param("# comment\nhome = /usr\n", id="no-version"),
pytest.param("version_info = 3=15\n", id="extra-equals"),
],
)
def test_health_check_missing_version_info(tmp_path: Path, contents: str) -> None:
prefix = _make_env(tmp_path, contents)

assert _health_check(prefix, _VERSION) == "created virtualenv's pyvenv.cfg is missing `version_info`"
75 changes: 0 additions & 75 deletions tests/test_health_check.py

This file was deleted.