25
25
from usethis ._integrations .uv .init import ensure_pyproject_toml
26
26
from usethis ._tool import (
27
27
ALL_TOOLS ,
28
+ CoverageTool ,
28
29
DeptryTool ,
29
30
PreCommitTool ,
30
31
PyprojectFmtTool ,
34
35
)
35
36
36
37
38
+ def use_coverage (* , remove : bool = False ) -> None :
39
+ tool = CoverageTool ()
40
+
41
+ ensure_pyproject_toml ()
42
+
43
+ if not remove :
44
+ add_deps_to_group (tool .dev_deps , "test" )
45
+
46
+ tool .add_pyproject_configs ()
47
+
48
+ if PytestTool ().is_used ():
49
+ _coverage_instructions_pytest ()
50
+ else :
51
+ _coverage_instructions_basic ()
52
+ else :
53
+ tool .remove_pyproject_configs ()
54
+ remove_deps_from_group (tool .dev_deps , "test" )
55
+
56
+
57
+ def _coverage_instructions_basic () -> None :
58
+ box_print ("Run 'coverage help' to see available coverage commands." )
59
+
60
+
61
+ def _coverage_instructions_pytest () -> None :
62
+ box_print ("Run 'pytest --cov' to run your tests with coverage." )
63
+
64
+
37
65
def use_deptry (* , remove : bool = False ) -> None :
38
66
tool = DeptryTool ()
39
67
@@ -53,6 +81,7 @@ def use_deptry(*, remove: bool = False) -> None:
53
81
54
82
def use_pre_commit (* , remove : bool = False ) -> None :
55
83
tool = PreCommitTool ()
84
+ pyproject_fmt_tool = PyprojectFmtTool ()
56
85
57
86
ensure_pyproject_toml ()
58
87
@@ -62,13 +91,14 @@ def use_pre_commit(*, remove: bool = False) -> None:
62
91
if _tool .is_used ():
63
92
_tool .add_pre_commit_repo_configs ()
64
93
65
- if PyprojectFmtTool () .is_used ():
94
+ if pyproject_fmt_tool .is_used ():
66
95
# We will use pre-commit instead of the dev-dep.
67
- remove_deps_from_group (PyprojectFmtTool ().get_unique_dev_deps (), "dev" )
68
- use_pyproject_fmt ()
96
+ remove_deps_from_group (pyproject_fmt_tool .get_unique_dev_deps (), "dev" )
97
+ pyproject_fmt_tool .add_pyproject_configs ()
98
+ _pyproject_fmt_instructions_pre_commit ()
69
99
70
100
if RequirementsTxtTool ().is_used ():
71
- use_requirements_txt ()
101
+ _requirements_txt_instructions_pre_commit ()
72
102
73
103
if not get_hook_names ():
74
104
add_placeholder_hook ()
@@ -91,12 +121,15 @@ def use_pre_commit(*, remove: bool = False) -> None:
91
121
remove_deps_from_group (tool .dev_deps , "dev" )
92
122
93
123
# Need to add a new way of running some hooks manually if they are not dev
94
- # dependencies yet
95
- if PyprojectFmtTool ().is_used ():
96
- use_pyproject_fmt ()
124
+ # dependencies yet - explain to the user.
125
+ if pyproject_fmt_tool .is_used ():
126
+ add_deps_to_group (pyproject_fmt_tool .dev_deps , "dev" )
127
+ _pyproject_fmt_instructions_basic ()
97
128
129
+ # Likewise, explain how to manually generate the requirements.txt file, since
130
+ # they're not going to do it via pre-commit anymore.
98
131
if RequirementsTxtTool ().is_used ():
99
- use_requirements_txt ()
132
+ _requirements_txt_instructions_basic ()
100
133
101
134
102
135
def use_pyproject_fmt (* , remove : bool = False ) -> None :
@@ -115,18 +148,24 @@ def use_pyproject_fmt(*, remove: bool = False) -> None:
115
148
tool .add_pyproject_configs ()
116
149
117
150
if not is_pre_commit :
118
- box_print ( "Run 'pyproject-fmt pyproject.toml' to run pyproject-fmt." )
151
+ _pyproject_fmt_instructions_basic ( )
119
152
else :
120
- box_print (
121
- "Run 'pre-commit run pyproject-fmt --all-files' to run pyproject-fmt."
122
- )
153
+ _pyproject_fmt_instructions_pre_commit ()
123
154
else :
124
155
tool .remove_pyproject_configs ()
125
156
if PreCommitTool ().is_used ():
126
157
tool .remove_pre_commit_repo_configs ()
127
158
remove_deps_from_group (tool .dev_deps , "dev" )
128
159
129
160
161
+ def _pyproject_fmt_instructions_basic () -> None :
162
+ box_print ("Run 'pyproject-fmt pyproject.toml' to run pyproject-fmt." )
163
+
164
+
165
+ def _pyproject_fmt_instructions_pre_commit () -> None :
166
+ box_print ("Run 'pre-commit run pyproject-fmt --all-files' to run pyproject-fmt." )
167
+
168
+
130
169
def use_pytest (* , remove : bool = False ) -> None :
131
170
tool = PytestTool ()
132
171
@@ -137,6 +176,7 @@ def use_pytest(*, remove: bool = False) -> None:
137
176
tool .add_pyproject_configs ()
138
177
if RuffTool ().is_used ():
139
178
select_ruff_rules (tool .get_associated_ruff_rules ())
179
+
140
180
# deptry currently can't scan the tests folder for dev deps
141
181
# https://github.com/fpgmaas/deptry/issues/302
142
182
add_pytest_dir ()
@@ -149,6 +189,9 @@ def use_pytest(*, remove: bool = False) -> None:
149
189
)
150
190
box_print ("Add test functions with the format 'test_*()'." )
151
191
box_print ("Run 'pytest' to run the tests." )
192
+
193
+ if CoverageTool ().is_used ():
194
+ _coverage_instructions_pytest ()
152
195
else :
153
196
if is_bitbucket_used ():
154
197
remove_bitbucket_pytest_steps ()
@@ -159,6 +202,9 @@ def use_pytest(*, remove: bool = False) -> None:
159
202
remove_deps_from_group (tool .dev_deps , "test" )
160
203
remove_pytest_dir () # Last, since this is a manual step
161
204
205
+ if CoverageTool ().is_used ():
206
+ _coverage_instructions_basic ()
207
+
162
208
163
209
def use_requirements_txt (* , remove : bool = False ) -> None :
164
210
tool = RequirementsTxtTool ()
@@ -190,11 +236,9 @@ def use_requirements_txt(*, remove: bool = False) -> None:
190
236
)
191
237
192
238
if not is_pre_commit :
193
- box_print (
194
- "Run 'uv export --no-dev --output-file=requirements.txt' to write 'requirements.txt'."
195
- )
239
+ _requirements_txt_instructions_basic ()
196
240
else :
197
- box_print ( "Run the 'pre-commit run uv-export' to write 'requirements.txt'." )
241
+ _requirements_txt_instructions_pre_commit ( )
198
242
else :
199
243
if PreCommitTool ().is_used ():
200
244
tool .remove_pre_commit_repo_configs ()
@@ -204,6 +248,16 @@ def use_requirements_txt(*, remove: bool = False) -> None:
204
248
path .unlink ()
205
249
206
250
251
+ def _requirements_txt_instructions_basic () -> None :
252
+ box_print (
253
+ "Run 'uv export --no-dev --output-file=requirements.txt' to write 'requirements.txt'."
254
+ )
255
+
256
+
257
+ def _requirements_txt_instructions_pre_commit () -> None :
258
+ box_print ("Run the 'pre-commit run uv-export' to write 'requirements.txt'." )
259
+
260
+
207
261
def use_ruff (* , remove : bool = False ) -> None :
208
262
tool = RuffTool ()
209
263
0 commit comments