Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/usethis/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ class UsethisConfig:
frozen: Do not install dependencies, nor update lockfiles.
alert_only: Suppress all output except for warnings and errors.
disable_uv_subprocess: Raise an error if a uv subprocess invocation is tried.
disable_pre_commit: Disable pre-commit integrations. Assume that pre-commit is
never used (unless explicitly requested via a function whose
purpose is to modify pre-commit configuration).
subprocess_verbose: Verbose output for subprocesses.
force_project_dir: Directory for the project. If None, defaults to the current
working directory dynamically determined at runtime.
Expand All @@ -35,6 +38,7 @@ class UsethisConfig:
frozen: bool = False
alert_only: bool = False
disable_uv_subprocess: bool = False
disable_pre_commit: bool = False
subprocess_verbose: bool = False
project_dir: Path | None = None

Expand All @@ -47,6 +51,7 @@ def set( # noqa: PLR0913
frozen: bool | None = None,
alert_only: bool | None = None,
disable_uv_subprocess: bool | None = None,
disable_pre_commit: bool | None = None,
subprocess_verbose: bool | None = None,
project_dir: Path | str | None = None,
) -> Generator[None, None, None]:
Expand All @@ -56,6 +61,7 @@ def set( # noqa: PLR0913
old_frozen = self.frozen
old_alert_only = self.alert_only
old_disable_uv_subprocess = self.disable_uv_subprocess
old_disable_pre_commit = self.disable_pre_commit
old_subprocess_verbose = self.subprocess_verbose
old_project_dir = self.project_dir

Expand All @@ -69,6 +75,8 @@ def set( # noqa: PLR0913
alert_only = self.alert_only
if disable_uv_subprocess is None:
disable_uv_subprocess = old_disable_uv_subprocess
if disable_pre_commit is None:
disable_pre_commit = old_disable_pre_commit
if subprocess_verbose is None:
subprocess_verbose = old_subprocess_verbose
if project_dir is None:
Expand All @@ -79,6 +87,7 @@ def set( # noqa: PLR0913
self.frozen = frozen
self.alert_only = alert_only
self.disable_uv_subprocess = disable_uv_subprocess
self.disable_pre_commit = disable_pre_commit
self.subprocess_verbose = subprocess_verbose
if isinstance(project_dir, str):
project_dir = Path(project_dir)
Expand All @@ -89,6 +98,7 @@ def set( # noqa: PLR0913
self.frozen = old_frozen
self.alert_only = old_alert_only
self.disable_uv_subprocess = old_disable_uv_subprocess
self.disable_pre_commit = old_disable_pre_commit
self.subprocess_verbose = old_subprocess_verbose
self.project_dir = old_project_dir

Expand All @@ -102,8 +112,9 @@ def cpd(self) -> Path:
usethis_config = UsethisConfig(
offline=OFFLINE_DEFAULT,
quiet=QUIET_DEFAULT,
frozen=False,
frozen=FROZEN_DEFAULT,
alert_only=False,
disable_uv_subprocess=False,
disable_pre_commit=False,
subprocess_verbose=False,
)
6 changes: 6 additions & 0 deletions src/usethis/_tool/impl/pre_commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from pathlib import Path

from usethis._config import usethis_config
from usethis._console import box_print
from usethis._integrations.ci.bitbucket.anchor import (
ScriptItemAnchor as BitbucketScriptItemAnchor,
Expand All @@ -21,6 +22,11 @@ class PreCommitTool(Tool):
def name(self) -> str:
return "pre-commit"

def is_used(self) -> bool:
if usethis_config.disable_pre_commit:
return False
return super().is_used()

def print_how_to_use(self) -> None:
if is_uv_used():
box_print(
Expand Down
30 changes: 30 additions & 0 deletions tests/usethis/_tool/impl/test_pre_commit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from pathlib import Path

from usethis._config import usethis_config
from usethis._config_file import files_manager
from usethis._test import change_cwd
from usethis._tool.impl.pre_commit import PreCommitTool


class TestPreCommitTool:
class TestIsUsed:
def test_pre_commit_config_file(self, tmp_path: Path):
# Arrange
(tmp_path / ".pre-commit-config.yaml").touch()

# Act, Assert
with change_cwd(tmp_path):
assert PreCommitTool().is_used()

def test_pre_commit_disabled(self, tmp_path: Path):
# Arrange
(tmp_path / ".pre-commit-config.yaml").touch()

# Act, Assert
with change_cwd(tmp_path), usethis_config.set(disable_pre_commit=True):
assert not PreCommitTool().is_used()

def test_empty_dir(self, tmp_path: Path):
# Act, Assert
with change_cwd(tmp_path), files_manager():
assert not PreCommitTool().is_used()