Skip to content

Commit

Permalink
Patch pre-commit also when called due to a git commit (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
florianfischer91 authored Sep 23, 2024
1 parent f2c852f commit bb4ac8b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
6 changes: 5 additions & 1 deletion src/pre_commit_uv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ def _patch() -> None:
calling_pre_commit = "FORCE_PRE_COMMIT_UV_PATCH" in os.environ
if not calling_pre_commit and sys.argv and sys.argv[0]: # must have arguments
calling = sys.argv[1] if sys.argv[0] == sys.executable and len(sys.argv) >= 1 else sys.argv[0]
if os.path.split(calling)[1] == f"pre-commit{'.exe' if sys.platform == 'win32' else ''}":
if (
os.path.split(calling)[1] == f"pre-commit{'.exe' if sys.platform == 'win32' else ''}"
# case when pre-commit is called due to a git commit
or ("-m" in sys.argv and "hook-impl" in sys.argv)
):
calling_pre_commit = True

if calling_pre_commit and os.environ.get("DISABLE_PRE_COMMIT_UV_PATCH") is None:
Expand Down
42 changes: 35 additions & 7 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,64 @@
from __future__ import annotations

from importlib.metadata import version
from subprocess import check_call
from subprocess import check_call, check_output
from textwrap import dedent
from typing import TYPE_CHECKING

import pytest
from pre_commit import main

if TYPE_CHECKING:
from pathlib import Path

import pytest
precommit_file = ".pre-commit-config.yaml"
uv = version("uv")
self = version("pre-commit-uv")


def test_install(tmp_path: Path, caplog: pytest.LogCaptureFixture, monkeypatch: pytest.MonkeyPatch) -> None:
@pytest.fixture
def git_repo(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
conf = """
repos:
- repo: https://github.com/tox-dev/pyproject-fmt
rev: "2.2.0"
hooks:
- id: pyproject-fmt
"""
conf_file = tmp_path / ".pre-commit-config.yaml"
conf_file = tmp_path / precommit_file
conf_file.write_text(dedent(conf))
monkeypatch.setenv("PRE_COMMIT_HOME", str(tmp_path / "store"))
monkeypatch.chdir(tmp_path)
check_call(["git", "init"])
return tmp_path

main.main(["install-hooks", "-c", str(conf_file)])

uv = version("uv")
self = version("pre-commit-uv")
@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


@pytest.mark.usefixtures("install_hook")
def test_run_precommit_hook() -> None:
hook_result = check_output([".git/hooks/pre-commit"], encoding="utf-8")
assert f"[INFO] Using pre-commit with uv {uv} via pre-commit-uv {self}" in hook_result.splitlines()


@pytest.mark.usefixtures("install_hook")
def test_call_as_module() -> None:
run_result = check_output(["python3", "-m", "pre_commit", "run", "-a", "--color", "never"], encoding="utf-8")
assert f"[INFO] Using pre-commit with uv {uv} via pre-commit-uv {self}" not in run_result.splitlines()


def test_install(git_repo: Path, caplog: pytest.LogCaptureFixture, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("FORCE_PRE_COMMIT_UV_PATCH", "1")

import pre_commit_uv # noqa: PLC0415

pre_commit_uv._patch() # noqa: SLF001
main.main(["install-hooks", "-c", str(git_repo / precommit_file)])

assert caplog.messages == [
"Initializing environment for https://github.com/tox-dev/pyproject-fmt.",
"Installing environment for https://github.com/tox-dev/pyproject-fmt.",
Expand Down
1 change: 0 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ pass_env =
PYTEST_*
set_env =
COVERAGE_FILE = {work_dir}/.coverage.{env_name}
FORCE_PRE_COMMIT_UV_PATCH = true
commands =
python -m pytest {tty:--color=yes} {posargs: \
--cov {env_site_packages_dir}{/}pre_commit_uv --cov {tox_root}{/}tests \
Expand Down

0 comments on commit bb4ac8b

Please sign in to comment.