Skip to content

Commit e8d49e0

Browse files
Generalize frozen pre-commit install instruction for uv users (#762)
* Generalize frozen pre-commit install instruction for uv users * Update tests to reflect new behaviour
1 parent c63a203 commit e8d49e0

3 files changed

Lines changed: 27 additions & 3 deletions

File tree

src/usethis/_integrations/pre_commit/core.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from usethis._integrations.pre_commit.errors import PreCommitInstallationError
66
from usethis._integrations.uv.call import call_uv_subprocess
77
from usethis._integrations.uv.errors import UVSubprocessFailedError
8+
from usethis._integrations.uv.used import is_uv_used
89

910

1011
def remove_pre_commit_config() -> None:
@@ -24,7 +25,10 @@ def install_pre_commit_hooks() -> None:
2425
in a git repo.
2526
"""
2627
if usethis_config.frozen:
27-
box_print("Run 'pre-commit install' to register pre-commit.")
28+
if is_uv_used():
29+
box_print("Run 'uv run pre-commit install' to register pre-commit.")
30+
else:
31+
box_print("Run 'pre-commit install' to register pre-commit.")
2832
return
2933

3034
tick_print("Ensuring pre-commit is installed to Git.")

tests/usethis/_core/test_core_tool.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ def test_pre_commit_after(
632632
"☐ Install the dependency 'pre-commit'.\n"
633633
"✔ Writing '.pre-commit-config.yaml'.\n"
634634
"✔ Adding hook 'deptry' to '.pre-commit-config.yaml'.\n"
635-
"☐ Run 'pre-commit install' to register pre-commit.\n"
635+
"☐ Run 'uv run pre-commit install' to register pre-commit.\n"
636636
"☐ Run 'uv run pre-commit run --all-files' to run the hooks manually.\n"
637637
)
638638

@@ -1481,7 +1481,7 @@ def test_fresh(self, uv_init_dir: Path, capfd: pytest.CaptureFixture[str]):
14811481
"☐ Remove the placeholder hook in '.pre-commit-config.yaml'.\n"
14821482
"☐ Replace it with your own hooks.\n"
14831483
"☐ Alternatively, use 'usethis tool' to add other tools and their hooks.\n"
1484-
"☐ Run 'pre-commit install' to register pre-commit.\n"
1484+
"☐ Run 'uv run pre-commit install' to register pre-commit.\n"
14851485
"☐ Run 'uv run pre-commit run --all-files' to run the hooks manually.\n"
14861486
)
14871487
# Config file

tests/usethis/_integrations/pre_commit/test_pre_commit_core.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,26 @@ def test_message(self, uv_env_dir: Path, capfd: pytest.CaptureFixture[str]):
8080
"ℹ This may take a minute or so while the hooks are downloaded.\r" # noqa: RUF001
8181
)
8282

83+
@pytest.mark.usefixtures("_vary_network_conn")
84+
def test_frozen_message_using_uv(
85+
self, uv_init_repo_dir: Path, capfd: pytest.CaptureFixture[str]
86+
):
87+
# Arrange
88+
with change_cwd(uv_init_repo_dir), PyprojectTOMLManager():
89+
add_deps_to_group([Dependency(name="pre-commit")], "dev")
90+
add_placeholder_hook()
91+
capfd.readouterr()
92+
93+
# Act
94+
install_pre_commit_hooks()
95+
96+
# Assert
97+
out, err = capfd.readouterr()
98+
assert not err
99+
assert out == (
100+
"☐ Run 'uv run pre-commit install' to register pre-commit.\n"
101+
)
102+
83103
def test_err(self, tmp_path: Path):
84104
# Act, Assert
85105
with change_cwd(tmp_path), pytest.raises(PreCommitInstallationError):

0 commit comments

Comments
 (0)