Skip to content

Commit efaf6c1

Browse files
Add missing method call, Fix broken tests (#320)
1 parent 3a30c55 commit efaf6c1

3 files changed

Lines changed: 117 additions & 80 deletions

File tree

src/usethis/_core/tool.py

Lines changed: 20 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
update_bitbucket_pytest_steps,
77
)
88
from usethis._config import usethis_config
9-
from usethis._console import box_print, tick_print
9+
from usethis._console import tick_print
1010
from usethis._integrations.bitbucket.steps import (
1111
add_bitbucket_steps_in_default,
1212
remove_bitbucket_steps_from_default,
@@ -50,38 +50,22 @@ def use_codespell(*, remove: bool = False) -> None:
5050
ensure_pyproject_toml()
5151

5252
if not remove:
53-
is_pre_commit = PreCommitTool().is_used()
54-
55-
if not is_pre_commit:
53+
if not PreCommitTool().is_used():
5654
add_deps_to_group(tool.dev_deps, "dev")
5755
if is_bitbucket_used():
5856
add_bitbucket_steps_in_default(tool.get_bitbucket_steps())
5957
else:
6058
tool.add_pre_commit_repo_configs()
6159

6260
tool.add_pyproject_configs()
63-
64-
if not is_pre_commit:
65-
_codespell_instructions_basic()
66-
else:
67-
_codespell_instructions_pre_commit()
61+
tool.print_how_to_use()
6862
else:
6963
remove_bitbucket_steps_from_default(tool.get_bitbucket_steps())
7064
tool.remove_pyproject_configs()
7165
tool.remove_pre_commit_repo_configs()
7266
remove_deps_from_group(tool.dev_deps, "dev")
7367

7468

75-
def _codespell_instructions_basic() -> None:
76-
box_print("Run 'codespell' to run the Codespell spellchecker.")
77-
78-
79-
def _codespell_instructions_pre_commit() -> None:
80-
box_print(
81-
"Run 'pre-commit run codespell --all-files' to run the Codespell spellchecker."
82-
)
83-
84-
8569
def use_coverage(*, remove: bool = False) -> None:
8670
tool = CoverageTool()
8771

@@ -94,24 +78,12 @@ def use_coverage(*, remove: bool = False) -> None:
9478
add_deps_to_group(deps, "test")
9579

9680
tool.add_pyproject_configs()
97-
98-
if PytestTool().is_used():
99-
_coverage_instructions_pytest()
100-
else:
101-
_coverage_instructions_basic()
81+
tool.print_how_to_use()
10282
else:
10383
tool.remove_pyproject_configs()
10484
remove_deps_from_group([*tool.dev_deps, Dependency(name="pytest-cov")], "test")
10585

10686

107-
def _coverage_instructions_basic() -> None:
108-
box_print("Run 'coverage help' to see available coverage commands.")
109-
110-
111-
def _coverage_instructions_pytest() -> None:
112-
box_print("Run 'pytest --cov' to run your tests with coverage.")
113-
114-
11587
def use_deptry(*, remove: bool = False) -> None:
11688
tool = DeptryTool()
11789

@@ -124,7 +96,7 @@ def use_deptry(*, remove: bool = False) -> None:
12496
elif is_bitbucket_used():
12597
add_bitbucket_steps_in_default(tool.get_bitbucket_steps())
12698

127-
box_print("Run 'deptry src' to run deptry.")
99+
tool.print_how_to_use()
128100
else:
129101
tool.remove_pre_commit_repo_configs()
130102
tool.remove_pyproject_configs()
@@ -147,14 +119,14 @@ def use_pre_commit(*, remove: bool = False) -> None:
147119
if pyproject_fmt_tool.is_used():
148120
remove_deps_from_group(pyproject_fmt_tool.dev_deps, "dev")
149121
pyproject_fmt_tool.add_pyproject_configs()
150-
_pyproject_fmt_instructions_pre_commit()
122+
PyprojectFmtTool().print_how_to_use()
151123
if codespell_tool.is_used():
152124
remove_deps_from_group(codespell_tool.dev_deps, "dev")
153125
codespell_tool.add_pyproject_configs()
154-
_codespell_instructions_pre_commit()
126+
CodespellTool().print_how_to_use()
155127

156128
if RequirementsTxtTool().is_used():
157-
_requirements_txt_instructions_pre_commit()
129+
RequirementsTxtTool().print_how_to_use()
158130

159131
if not get_hook_names():
160132
add_placeholder_hook()
@@ -165,7 +137,7 @@ def use_pre_commit(*, remove: bool = False) -> None:
165137
add_bitbucket_steps_in_default(tool.get_bitbucket_steps())
166138
_remove_bitbucket_linter_steps_from_default()
167139

168-
box_print("Run 'pre-commit run --all-files' to run the hooks manually.")
140+
tool.print_how_to_use()
169141
else:
170142
if is_bitbucket_used():
171143
remove_bitbucket_steps_from_default(tool.get_bitbucket_steps())
@@ -180,15 +152,15 @@ def use_pre_commit(*, remove: bool = False) -> None:
180152
# dependencies yet - explain to the user.
181153
if pyproject_fmt_tool.is_used():
182154
add_deps_to_group(pyproject_fmt_tool.dev_deps, "dev")
183-
_pyproject_fmt_instructions_basic()
155+
PyprojectFmtTool().print_how_to_use()
184156
if codespell_tool.is_used():
185157
add_deps_to_group(codespell_tool.dev_deps, "dev")
186-
_codespell_instructions_basic()
158+
CodespellTool().print_how_to_use()
187159

188160
# Likewise, explain how to manually generate the requirements.txt file, since
189161
# they're not going to do it via pre-commit anymore.
190162
if RequirementsTxtTool().is_used():
191-
_requirements_txt_instructions_basic()
163+
RequirementsTxtTool().print_how_to_use()
192164

193165

194166
def _add_all_tools_pre_commit_configs():
@@ -221,36 +193,22 @@ def use_pyproject_fmt(*, remove: bool = False) -> None:
221193
ensure_pyproject_toml()
222194

223195
if not remove:
224-
is_pre_commit = PreCommitTool().is_used()
225-
226-
if not is_pre_commit:
196+
if not PreCommitTool().is_used():
227197
add_deps_to_group(tool.dev_deps, "dev")
228198
if is_bitbucket_used():
229199
add_bitbucket_steps_in_default(tool.get_bitbucket_steps())
230200
else:
231201
tool.add_pre_commit_repo_configs()
232202

233203
tool.add_pyproject_configs()
234-
235-
if not is_pre_commit:
236-
_pyproject_fmt_instructions_basic()
237-
else:
238-
_pyproject_fmt_instructions_pre_commit()
204+
tool.print_how_to_use()
239205
else:
240206
remove_bitbucket_steps_from_default(tool.get_bitbucket_steps())
241207
tool.remove_pyproject_configs()
242208
tool.remove_pre_commit_repo_configs()
243209
remove_deps_from_group(tool.dev_deps, "dev")
244210

245211

246-
def _pyproject_fmt_instructions_basic() -> None:
247-
box_print("Run 'pyproject-fmt pyproject.toml' to run pyproject-fmt.")
248-
249-
250-
def _pyproject_fmt_instructions_pre_commit() -> None:
251-
box_print("Run 'pre-commit run pyproject-fmt --all-files' to run pyproject-fmt.")
252-
253-
254212
def use_pytest(*, remove: bool = False) -> None:
255213
tool = PytestTool()
256214

@@ -273,14 +231,10 @@ def use_pytest(*, remove: bool = False) -> None:
273231
if is_bitbucket_used():
274232
update_bitbucket_pytest_steps()
275233

276-
box_print(
277-
"Add test files to the '/tests' directory with the format 'test_*.py'."
278-
)
279-
box_print("Add test functions with the format 'test_*()'.")
280-
box_print("Run 'pytest' to run the tests.")
234+
tool.print_how_to_use()
281235

282236
if CoverageTool().is_used():
283-
_coverage_instructions_pytest()
237+
CoverageTool().print_how_to_use()
284238
else:
285239
if is_bitbucket_used():
286240
remove_bitbucket_pytest_steps()
@@ -293,7 +247,7 @@ def use_pytest(*, remove: bool = False) -> None:
293247
remove_pytest_dir() # Last, since this is a manual step
294248

295249
if CoverageTool().is_used():
296-
_coverage_instructions_basic()
250+
CoverageTool().print_how_to_use()
297251

298252

299253
def use_requirements_txt(*, remove: bool = False) -> None:
@@ -327,10 +281,8 @@ def use_requirements_txt(*, remove: bool = False) -> None:
327281
change_toml=False,
328282
)
329283

330-
if not is_pre_commit:
331-
_requirements_txt_instructions_basic()
332-
else:
333-
_requirements_txt_instructions_pre_commit()
284+
tool.print_how_to_use()
285+
334286
else:
335287
tool.remove_pre_commit_repo_configs()
336288

@@ -339,16 +291,6 @@ def use_requirements_txt(*, remove: bool = False) -> None:
339291
path.unlink()
340292

341293

342-
def _requirements_txt_instructions_basic() -> None:
343-
box_print(
344-
"Run 'uv export --no-dev --output-file=requirements.txt' to write 'requirements.txt'."
345-
)
346-
347-
348-
def _requirements_txt_instructions_pre_commit() -> None:
349-
box_print("Run the 'pre-commit run uv-export' to write 'requirements.txt'.")
350-
351-
352294
def use_ruff(*, remove: bool = False) -> None:
353295
tool = RuffTool()
354296

@@ -388,8 +330,7 @@ def use_ruff(*, remove: bool = False) -> None:
388330
elif is_bitbucket_used():
389331
add_bitbucket_steps_in_default(tool.get_bitbucket_steps())
390332

391-
box_print("Run 'ruff check --fix' to run the Ruff linter with autofixes.")
392-
box_print("Run 'ruff format' to run the Ruff formatter.")
333+
tool.print_how_to_use()
393334
else:
394335
tool.remove_pre_commit_repo_configs()
395336
remove_bitbucket_steps_from_default(tool.get_bitbucket_steps())

src/usethis/_tool.py

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from pathlib import Path
33
from typing import Protocol
44

5-
from usethis._console import tick_print
5+
from usethis._console import box_print, tick_print
66
from usethis._integrations.bitbucket.anchor import (
77
ScriptItemAnchor as BitbucketScriptItemAnchor,
88
)
@@ -87,6 +87,14 @@ def get_pyproject_id_keys(self) -> list[list[str]]:
8787
"""Get keys for any pyproject.toml sections only used by this tool (not shared)."""
8888
return []
8989

90+
@abstractmethod
91+
def print_how_to_use(self) -> None:
92+
"""Print instructions for using the tool.
93+
94+
This method is called after a tool is added to the project.
95+
"""
96+
pass
97+
9098
def is_used(self) -> bool:
9199
"""Whether the tool is being used in the current project.
92100
@@ -200,6 +208,14 @@ def name(self) -> str:
200208
def dev_deps(self) -> list[Dependency]:
201209
return [Dependency(name="codespell")]
202210

211+
def print_how_to_use(self) -> None:
212+
if PreCommitTool().is_used():
213+
box_print(
214+
"Run 'pre-commit run codespell --all-files' to run the Codespell spellchecker."
215+
)
216+
else:
217+
box_print("Run 'codespell' to run the Codespell spellchecker.")
218+
203219
def get_pyproject_configs(self) -> list[PyProjectConfig]:
204220
return [
205221
PyProjectConfig(
@@ -253,6 +269,12 @@ def name(self) -> str:
253269
def dev_deps(self) -> list[Dependency]:
254270
return [Dependency(name="coverage", extras=frozenset({"toml"}))]
255271

272+
def print_how_to_use(self) -> None:
273+
if PytestTool().is_used():
274+
box_print("Run 'pytest --cov' to run your tests with coverage.")
275+
else:
276+
box_print("Run 'coverage help' to see available coverage commands.")
277+
256278
def get_pyproject_configs(self) -> list[PyProjectConfig]:
257279
return [
258280
PyProjectConfig(
@@ -293,6 +315,9 @@ def name(self) -> str:
293315
def dev_deps(self) -> list[Dependency]:
294316
return [Dependency(name="deptry")]
295317

318+
def print_how_to_use(self) -> None:
319+
box_print("Run 'deptry src' to run deptry.")
320+
296321
def get_pre_commit_repos(self) -> list[LocalRepo | UriRepo]:
297322
return [
298323
LocalRepo(
@@ -337,6 +362,9 @@ def name(self) -> str:
337362
def dev_deps(self) -> list[Dependency]:
338363
return [Dependency(name="pre-commit")]
339364

365+
def print_how_to_use(self) -> None:
366+
box_print("Run 'pre-commit run --all-files' to run the hooks manually.")
367+
340368
def get_managed_files(self) -> list[Path]:
341369
return [Path(".pre-commit-config.yaml")]
342370

@@ -364,6 +392,14 @@ def name(self) -> str:
364392
def dev_deps(self) -> list[Dependency]:
365393
return [Dependency(name="pyproject-fmt")]
366394

395+
def print_how_to_use(self) -> None:
396+
if PreCommitTool().is_used():
397+
box_print(
398+
"Run 'pre-commit run pyproject-fmt --all-files' to run pyproject-fmt."
399+
)
400+
else:
401+
box_print("Run 'pyproject-fmt pyproject.toml' to run pyproject-fmt.")
402+
367403
def get_pre_commit_repos(self) -> list[LocalRepo | UriRepo]:
368404
return [
369405
UriRepo(
@@ -408,6 +444,13 @@ def name(self) -> str:
408444
def dev_deps(self) -> list[Dependency]:
409445
return [Dependency(name="pytest")]
410446

447+
def print_how_to_use(self) -> None:
448+
box_print(
449+
"Add test files to the '/tests' directory with the format 'test_*.py'."
450+
)
451+
box_print("Add test functions with the format 'test_*()'.")
452+
box_print("Run 'pytest' to run the tests.")
453+
411454
def get_extra_dev_deps(self) -> list[Dependency]:
412455
return [Dependency(name="pytest-cov")]
413456

@@ -446,6 +489,14 @@ def name(self) -> str:
446489
def dev_deps(self) -> list[Dependency]:
447490
return []
448491

492+
def print_how_to_use(self) -> None:
493+
if PreCommitTool().is_used():
494+
box_print("Run the 'pre-commit run uv-export' to write 'requirements.txt'.")
495+
else:
496+
box_print(
497+
"Run 'uv export --no-dev --output-file=requirements.txt' to write 'requirements.txt'."
498+
)
499+
449500
def get_pre_commit_repos(self) -> list[LocalRepo | UriRepo]:
450501
return [
451502
LocalRepo(
@@ -477,6 +528,10 @@ def name(self) -> str:
477528
def dev_deps(self) -> list[Dependency]:
478529
return [Dependency(name="ruff")]
479530

531+
def print_how_to_use(self) -> None:
532+
box_print("Run 'ruff check --fix' to run the Ruff linter with autofixes.")
533+
box_print("Run 'ruff format' to run the Ruff formatter.")
534+
480535
def get_pre_commit_repos(self) -> list[LocalRepo | UriRepo]:
481536
return [
482537
LocalRepo(

0 commit comments

Comments
 (0)