|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | from importlib.metadata import version |
4 | | -from subprocess import check_call |
| 4 | +from subprocess import check_call, check_output |
5 | 5 | from textwrap import dedent |
6 | 6 | from typing import TYPE_CHECKING |
7 | 7 |
|
| 8 | +import pytest |
8 | 9 | from pre_commit import main |
9 | 10 |
|
10 | 11 | if TYPE_CHECKING: |
11 | 12 | from pathlib import Path |
12 | 13 |
|
13 | | - import pytest |
| 14 | +precommit_file = ".pre-commit-config.yaml" |
| 15 | +uv = version("uv") |
| 16 | +self = version("pre-commit-uv") |
14 | 17 |
|
15 | 18 |
|
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: |
17 | 21 | conf = """ |
18 | 22 | repos: |
19 | 23 | - repo: https://github.com/tox-dev/pyproject-fmt |
20 | 24 | rev: "2.2.0" |
21 | 25 | hooks: |
22 | 26 | - id: pyproject-fmt |
23 | 27 | """ |
24 | | - conf_file = tmp_path / ".pre-commit-config.yaml" |
| 28 | + conf_file = tmp_path / precommit_file |
25 | 29 | conf_file.write_text(dedent(conf)) |
26 | 30 | monkeypatch.setenv("PRE_COMMIT_HOME", str(tmp_path / "store")) |
27 | 31 | monkeypatch.chdir(tmp_path) |
28 | 32 | check_call(["git", "init"]) |
| 33 | + return tmp_path |
29 | 34 |
|
30 | | - main.main(["install-hooks", "-c", str(conf_file)]) |
31 | 35 |
|
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 | + |
34 | 62 | assert caplog.messages == [ |
35 | 63 | "Initializing environment for https://github.com/tox-dev/pyproject-fmt.", |
36 | 64 | "Installing environment for https://github.com/tox-dev/pyproject-fmt.", |
|
0 commit comments