Skip to content

Commit

Permalink
Install hooks up-front with pre-commit install-hooks (#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanjmcdougall authored Jan 15, 2025
1 parent 161541b commit b20d1a0
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 29 deletions.
8 changes: 6 additions & 2 deletions src/usethis/_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ def box_print(msg: str | Exception) -> None:
console.print(f"☐ {msg}", style="red")


def info_print(msg: str | Exception) -> None:
def info_print(msg: str | Exception, temporary: bool = False) -> None:
msg = str(msg)

if not usethis_config.quiet:
console.print(f"ℹ {msg}", style="blue") # noqa: RUF001
if temporary:
end = "\r"
else:
end = "\n"
console.print(f"ℹ {msg}", style="blue", end=end) # noqa: RUF001


def err_print(msg: str | Exception) -> None:
Expand Down
13 changes: 11 additions & 2 deletions src/usethis/_integrations/pre_commit/core.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pathlib import Path

from usethis._console import tick_print
from usethis._console import info_print, tick_print
from usethis._integrations.pre_commit.errors import PreCommitInstallationError
from usethis._integrations.uv.call import call_uv_subprocess
from usethis._integrations.uv.errors import UVSubprocessFailedError
Expand All @@ -23,9 +23,18 @@ def install_pre_commit_hooks() -> None:
in a git repo.
"""

tick_print("Ensuring pre-commit hooks are installed.")
tick_print("Ensuring pre-commit is installed to Git.")
try:
call_uv_subprocess(["run", "pre-commit", "install"])
except UVSubprocessFailedError as err:
msg = f"Failed to install pre-commit in the Git repository:\n{err}"
raise PreCommitInstallationError(msg) from None
tick_print("Ensuring pre-commit hooks are installed.")
info_print(
"This may take a minute or so while the hooks are downloaded.", temporary=True
)
try:
call_uv_subprocess(["run", "pre-commit", "install-hooks"])
except UVSubprocessFailedError as err:
msg = f"Failed to install pre-commit hooks:\n{err}"
raise PreCommitInstallationError(msg) from None
Expand Down
64 changes: 40 additions & 24 deletions tests/usethis/_core/test_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,19 @@ def test_pre_commit_after(

# 4. Check messages
out, _ = capfd.readouterr()
assert out == (
"✔ Adding dependency 'deptry' to the 'dev' group in 'pyproject.toml'.\n"
"☐ Call the 'deptry src' command to run deptry.\n"
"✔ Adding dependency 'pre-commit' to the 'dev' group in 'pyproject.toml'.\n"
"✔ Writing '.pre-commit-config.yaml'.\n"
"✔ Adding hook 'deptry' to '.pre-commit-config.yaml'.\n"
"✔ Ensuring pre-commit hooks are installed.\n"
"☐ Call the 'pre-commit run --all-files' command to run the hooks manually.\n"
assert (
out
== (
"✔ Adding dependency 'deptry' to the 'dev' group in 'pyproject.toml'.\n"
"☐ Call the 'deptry src' command to run deptry.\n"
"✔ Adding dependency 'pre-commit' to the 'dev' group in 'pyproject.toml'.\n"
"✔ Writing '.pre-commit-config.yaml'.\n"
"✔ Adding hook 'deptry' to '.pre-commit-config.yaml'.\n"
"✔ Ensuring pre-commit is installed to Git.\n"
"✔ Ensuring pre-commit hooks are installed.\n"
"ℹ This may take a minute or so while the hooks are downloaded.\r" # noqa: RUF001
"☐ Call the 'pre-commit run --all-files' command to run the hooks manually.\n"
)
)

class TestRemove:
Expand Down Expand Up @@ -262,15 +267,20 @@ def test_fresh(self, uv_init_repo_dir: Path, capfd: pytest.CaptureFixture[str]):
assert dev_dep == "pre-commit"
# Correct stdout
out, _ = capfd.readouterr()
assert out == (
"✔ Adding dependency 'pre-commit' to the 'dev' group in 'pyproject.toml'.\n"
"✔ Writing '.pre-commit-config.yaml'.\n"
"✔ Adding placeholder hook to '.pre-commit-config.yaml'.\n"
"☐ Remove the placeholder hook in '.pre-commit-config.yaml'.\n"
"☐ Replace it with your own hooks.\n"
"☐ Alternatively, use 'usethis tool' to add other tools and their hooks.\n"
"✔ Ensuring pre-commit hooks are installed.\n"
"☐ Call the 'pre-commit run --all-files' command to run the hooks manually.\n"
assert (
out
== (
"✔ Adding dependency 'pre-commit' to the 'dev' group in 'pyproject.toml'.\n"
"✔ Writing '.pre-commit-config.yaml'.\n"
"✔ Adding placeholder hook to '.pre-commit-config.yaml'.\n"
"☐ Remove the placeholder hook in '.pre-commit-config.yaml'.\n"
"☐ Replace it with your own hooks.\n"
"☐ Alternatively, use 'usethis tool' to add other tools and their hooks.\n"
"✔ Ensuring pre-commit is installed to Git.\n"
"✔ Ensuring pre-commit hooks are installed.\n"
"ℹ This may take a minute or so while the hooks are downloaded.\r" # noqa: RUF001
"☐ Call the 'pre-commit run --all-files' command to run the hooks manually.\n"
)
)
# Config file
assert (uv_init_repo_dir / ".pre-commit-config.yaml").exists()
Expand All @@ -288,14 +298,17 @@ def test_fresh(self, uv_init_repo_dir: Path, capfd: pytest.CaptureFixture[str]):
)

@pytest.mark.usefixtures("_vary_network_conn")
def test_already_exists(self, uv_init_repo_dir: Path):
def test_config_file_already_exists(self, uv_init_repo_dir: Path):
# Arrange
(uv_init_repo_dir / ".pre-commit-config.yaml").write_text(
"""\
repos:
- repo: foo
hooks:
- id: bar
- repo: local
hooks:
- id: my hook
name: Its mine
entry: uv run python -c "print('hello world!')"
language: system
"""
)

Expand All @@ -308,9 +321,12 @@ def test_already_exists(self, uv_init_repo_dir: Path):
assert contents == (
"""\
repos:
- repo: foo
hooks:
- id: bar
- repo: local
hooks:
- id: my hook
name: Its mine
entry: uv run python -c "print('hello world!')"
language: system
"""
)

Expand Down
6 changes: 5 additions & 1 deletion tests/usethis/_integrations/pre_commit/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ def test_message(self, uv_init_repo_dir: Path, capfd: pytest.CaptureFixture[str]
# Assert
out, err = capfd.readouterr()
assert not err
assert out == "✔ Ensuring pre-commit hooks are installed.\n"
assert out == (
"✔ Ensuring pre-commit is installed to Git.\n"
"✔ Ensuring pre-commit hooks are installed.\n"
"ℹ This may take a minute or so while the hooks are downloaded.\r" # noqa: RUF001
)

def test_err(self, tmp_path: Path):
# Act, Assert
Expand Down

0 comments on commit b20d1a0

Please sign in to comment.