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
12 changes: 10 additions & 2 deletions src/usethis/_tool/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,9 @@ def is_used(self) -> bool:

Three heuristics are used by default:
1. Whether any of the tool's characteristic dependencies are in the project.
2. Whether any of the tool's managed files are in the project.
3. Whether any of the tool's managed config file sections are present.
2. Whether any of the tool's characteristic pre-commit hooks are in the project.
3. Whether any of the tool's managed files are in the project.
4. Whether any of the tool's managed config file sections are present.
"""
decode_err_by_name: dict[str, FileConfigError] = {}
_is_used = False
Expand All @@ -128,6 +129,13 @@ def is_used(self) -> bool:
except FileConfigError as err:
decode_err_by_name[err.name] = err

# Do this last since the YAML parsing is expensive.
if not _is_used:
try:
_is_used = self.is_pre_commit_config_present()
except FileConfigError as err:
decode_err_by_name[err.name] = err

for name, decode_err in decode_err_by_name.items():
warn_print(decode_err)
warn_print(
Expand Down
2 changes: 1 addition & 1 deletion tests/usethis/_core/test_core_ci.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def test_contents(
self, uv_init_dir: Path, capfd: pytest.CaptureFixture[str]
):
# Arrange
(uv_init_dir / ".pre-commit-config.yaml").touch()
(uv_init_dir / ".pre-commit-config.yaml").write_text("repos: []")

# Act
with change_cwd(uv_init_dir), files_manager():
Expand Down
18 changes: 18 additions & 0 deletions tests/usethis/_tool/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import pytest

from usethis._config_file import files_manager
from usethis._console import box_print
from usethis._integrations.file.pyproject_toml.io_ import PyprojectTOMLManager
from usethis._integrations.file.setup_cfg.io_ import SetupCFGManager
Expand Down Expand Up @@ -404,6 +405,23 @@ def preferred_file_manager(self) -> KeyValueFileManager:
"\n⚠ Assuming 'setup.cfg' contains no evidence of my_tool being used.\n"
)

def test_pre_commit_config(self, uv_init_dir: Path):
# Arrange
tool = MyTool()
with change_cwd(uv_init_dir), files_manager():
# Create a pre-commit config file
(uv_init_dir / ".pre-commit-config.yaml").write_text("""\
repos:
- repo: local
hooks:
- id: deptry
""")

# Act
result = tool.is_used()
# Assert
assert result

class TestIsDeclaredAsDep:
def test_dev_deps(self, uv_init_dir: Path):
# Arrange
Expand Down