Skip to content

Commit 91e5716

Browse files
Check whether a pre-commit hook is used as a part of is_used heuris… (#813)
* Check whether a pre-commit hook is used as a part of `is_used` heuristics * Fix pre-commit schema violation in pre-commit integration test * Try reading pre-commit config last in `is_used` to improve performance * Fix undefined FileDecodeError - change to FileConfigError
1 parent 2af33db commit 91e5716

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

src/usethis/_tool/base.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,9 @@ def is_used(self) -> bool:
106106
107107
Three heuristics are used by default:
108108
1. Whether any of the tool's characteristic dependencies are in the project.
109-
2. Whether any of the tool's managed files are in the project.
110-
3. Whether any of the tool's managed config file sections are present.
109+
2. Whether any of the tool's characteristic pre-commit hooks are in the project.
110+
3. Whether any of the tool's managed files are in the project.
111+
4. Whether any of the tool's managed config file sections are present.
111112
"""
112113
decode_err_by_name: dict[str, FileConfigError] = {}
113114
_is_used = False
@@ -128,6 +129,13 @@ def is_used(self) -> bool:
128129
except FileConfigError as err:
129130
decode_err_by_name[err.name] = err
130131

132+
# Do this last since the YAML parsing is expensive.
133+
if not _is_used:
134+
try:
135+
_is_used = self.is_pre_commit_config_present()
136+
except FileConfigError as err:
137+
decode_err_by_name[err.name] = err
138+
131139
for name, decode_err in decode_err_by_name.items():
132140
warn_print(decode_err)
133141
warn_print(

tests/usethis/_tool/test_base.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import pytest
55

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

408+
def test_pre_commit_config(self, uv_init_dir: Path):
409+
# Arrange
410+
tool = MyTool()
411+
with change_cwd(uv_init_dir), files_manager():
412+
# Create a pre-commit config file
413+
(uv_init_dir / ".pre-commit-config.yaml").write_text("""\
414+
repos:
415+
- repo: local
416+
hooks:
417+
- id: deptry
418+
""")
419+
420+
# Act
421+
result = tool.is_used()
422+
# Assert
423+
assert result
424+
407425
class TestIsDeclaredAsDep:
408426
def test_dev_deps(self, uv_init_dir: Path):
409427
# Arrange

0 commit comments

Comments
 (0)