Skip to content

Commit bb4ac8b

Browse files
Patch pre-commit also when called due to a git commit (#18)
1 parent f2c852f commit bb4ac8b

File tree

3 files changed

+40
-9
lines changed

3 files changed

+40
-9
lines changed

src/pre_commit_uv/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ def _patch() -> None:
1616
calling_pre_commit = "FORCE_PRE_COMMIT_UV_PATCH" in os.environ
1717
if not calling_pre_commit and sys.argv and sys.argv[0]: # must have arguments
1818
calling = sys.argv[1] if sys.argv[0] == sys.executable and len(sys.argv) >= 1 else sys.argv[0]
19-
if os.path.split(calling)[1] == f"pre-commit{'.exe' if sys.platform == 'win32' else ''}":
19+
if (
20+
os.path.split(calling)[1] == f"pre-commit{'.exe' if sys.platform == 'win32' else ''}"
21+
# case when pre-commit is called due to a git commit
22+
or ("-m" in sys.argv and "hook-impl" in sys.argv)
23+
):
2024
calling_pre_commit = True
2125

2226
if calling_pre_commit and os.environ.get("DISABLE_PRE_COMMIT_UV_PATCH") is None:

tests/test_main.py

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,64 @@
11
from __future__ import annotations
22

33
from importlib.metadata import version
4-
from subprocess import check_call
4+
from subprocess import check_call, check_output
55
from textwrap import dedent
66
from typing import TYPE_CHECKING
77

8+
import pytest
89
from pre_commit import main
910

1011
if TYPE_CHECKING:
1112
from pathlib import Path
1213

13-
import pytest
14+
precommit_file = ".pre-commit-config.yaml"
15+
uv = version("uv")
16+
self = version("pre-commit-uv")
1417

1518

16-
def test_install(tmp_path: Path, caplog: pytest.LogCaptureFixture, monkeypatch: pytest.MonkeyPatch) -> None:
19+
@pytest.fixture
20+
def git_repo(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
1721
conf = """
1822
repos:
1923
- repo: https://github.com/tox-dev/pyproject-fmt
2024
rev: "2.2.0"
2125
hooks:
2226
- id: pyproject-fmt
2327
"""
24-
conf_file = tmp_path / ".pre-commit-config.yaml"
28+
conf_file = tmp_path / precommit_file
2529
conf_file.write_text(dedent(conf))
2630
monkeypatch.setenv("PRE_COMMIT_HOME", str(tmp_path / "store"))
2731
monkeypatch.chdir(tmp_path)
2832
check_call(["git", "init"])
33+
return tmp_path
2934

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

32-
uv = version("uv")
33-
self = version("pre-commit-uv")
36+
@pytest.fixture
37+
def install_hook(git_repo: Path) -> None:
38+
check_call(["pre-commit", "install", "--install-hooks", "-c", str(git_repo / precommit_file)])
39+
check_call(["pre-commit", "clean"]) # ensures that 'install_environment' gets called
40+
41+
42+
@pytest.mark.usefixtures("install_hook")
43+
def test_run_precommit_hook() -> None:
44+
hook_result = check_output([".git/hooks/pre-commit"], encoding="utf-8")
45+
assert f"[INFO] Using pre-commit with uv {uv} via pre-commit-uv {self}" in hook_result.splitlines()
46+
47+
48+
@pytest.mark.usefixtures("install_hook")
49+
def test_call_as_module() -> None:
50+
run_result = check_output(["python3", "-m", "pre_commit", "run", "-a", "--color", "never"], encoding="utf-8")
51+
assert f"[INFO] Using pre-commit with uv {uv} via pre-commit-uv {self}" not in run_result.splitlines()
52+
53+
54+
def test_install(git_repo: Path, caplog: pytest.LogCaptureFixture, monkeypatch: pytest.MonkeyPatch) -> None:
55+
monkeypatch.setenv("FORCE_PRE_COMMIT_UV_PATCH", "1")
56+
57+
import pre_commit_uv # noqa: PLC0415
58+
59+
pre_commit_uv._patch() # noqa: SLF001
60+
main.main(["install-hooks", "-c", str(git_repo / precommit_file)])
61+
3462
assert caplog.messages == [
3563
"Initializing environment for https://github.com/tox-dev/pyproject-fmt.",
3664
"Installing environment for https://github.com/tox-dev/pyproject-fmt.",

tox.ini

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ pass_env =
2424
PYTEST_*
2525
set_env =
2626
COVERAGE_FILE = {work_dir}/.coverage.{env_name}
27-
FORCE_PRE_COMMIT_UV_PATCH = true
2827
commands =
2928
python -m pytest {tty:--color=yes} {posargs: \
3029
--cov {env_site_packages_dir}{/}pre_commit_uv --cov {tox_root}{/}tests \

0 commit comments

Comments
 (0)