Skip to content

Commit 6d4abc0

Browse files
Add tests for use_requirements_txt (#212)
* Add tests for use_requirements_txt Tweak message * Fix broken test
1 parent 1f30849 commit 6d4abc0

3 files changed

Lines changed: 211 additions & 35 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ $ uvx usethis tool ruff
6565
✔ Adding Ruff config to 'pyproject.toml'.
6666
✔ Enabling Ruff rules 'A', 'C4', 'E4', 'E7', 'E9', 'EM', 'F', 'FURB', 'I', 'PLE', 'PLR', 'RUF', 'SIM', 'UP' in 'pyproject.toml'.
6767
✔ Ignoring Ruff rules 'PLR2004', 'SIM108' in 'pyproject.toml'.
68-
Call the 'ruff check --fix' command to run the Ruff linter with autofixes.
69-
Call the 'ruff format' command to run the Ruff formatter.
68+
Run 'ruff check --fix' to run the Ruff linter with autofixes.
69+
Run 'ruff format' to run the Ruff formatter.
7070
```
7171

7272
To use pytest, run:
@@ -80,7 +80,7 @@ $ uvx usethis tool pytest
8080
✔ Writing '/tests/conftest.py'.
8181
☐ Add test files to the '/tests' directory with the format 'test_*.py'.
8282
☐ Add test functions with the format 'test_*()'.
83-
Call the 'pytest' command to run the tests.
83+
Run 'pytest' to run the tests.
8484
```
8585

8686
To configure Bitbucket pipelines, run:

src/usethis/_core/tool.py

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
remove_bitbucket_pytest_steps,
88
update_bitbucket_pytest_steps,
99
)
10-
from usethis._console import box_print, info_print, tick_print
10+
from usethis._console import box_print, tick_print
1111
from usethis._integrations.pre_commit.core import (
1212
install_pre_commit_hooks,
1313
remove_pre_commit_config,
@@ -44,7 +44,7 @@ def use_deptry(*, remove: bool = False) -> None:
4444
if PreCommitTool().is_used():
4545
tool.add_pre_commit_repo_configs()
4646

47-
box_print("Call the 'deptry src' command to run deptry.")
47+
box_print("Run 'deptry src' to run deptry.")
4848
else:
4949
if PreCommitTool().is_used():
5050
tool.remove_pre_commit_repo_configs()
@@ -78,9 +78,7 @@ def use_pre_commit(*, remove: bool = False) -> None:
7878
if is_bitbucket_used():
7979
add_bitbucket_pre_commit_step()
8080

81-
box_print(
82-
"Call the 'pre-commit run --all-files' command to run the hooks manually."
83-
)
81+
box_print("Run 'pre-commit run --all-files' to run the hooks manually.")
8482
else:
8583
if is_bitbucket_used():
8684
remove_bitbucket_pre_commit_step()
@@ -117,12 +115,10 @@ def use_pyproject_fmt(*, remove: bool = False) -> None:
117115
tool.add_pyproject_configs()
118116

119117
if not is_pre_commit:
120-
box_print(
121-
"Call the 'pyproject-fmt pyproject.toml' command to run pyproject-fmt."
122-
)
118+
box_print("Run 'pyproject-fmt pyproject.toml' to run pyproject-fmt.")
123119
else:
124120
box_print(
125-
"Call the 'pre-commit run pyproject-fmt --all-files' command to run pyproject-fmt."
121+
"Run 'pre-commit run pyproject-fmt --all-files' to run pyproject-fmt."
126122
)
127123
else:
128124
tool.remove_pyproject_configs()
@@ -152,7 +148,7 @@ def use_pytest(*, remove: bool = False) -> None:
152148
"Add test files to the '/tests' directory with the format 'test_*.py'."
153149
)
154150
box_print("Add test functions with the format 'test_*()'.")
155-
box_print("Call the 'pytest' command to run the tests.")
151+
box_print("Run 'pytest' to run the tests.")
156152
else:
157153
if is_bitbucket_used():
158154
remove_bitbucket_pytest_steps()
@@ -167,6 +163,8 @@ def use_pytest(*, remove: bool = False) -> None:
167163
def use_requirements_txt(*, remove: bool = False) -> None:
168164
tool = RequirementsTxtTool()
169165

166+
ensure_pyproject_toml()
167+
170168
path = Path.cwd() / "requirements.txt"
171169

172170
if not remove:
@@ -177,28 +175,26 @@ def use_requirements_txt(*, remove: bool = False) -> None:
177175

178176
if not path.exists():
179177
# N.B. this is where a task runner would come in handy, to reduce duplication.
178+
if not (Path.cwd() / "uv.lock").exists():
179+
tick_print("Writing 'uv.lock'.")
180+
call_uv_subprocess(["lock"])
181+
180182
tick_print("Writing 'requirements.txt'.")
181183
call_uv_subprocess(
182184
[
183185
"export",
184186
"--frozen",
185187
"--no-dev",
186188
"--output-file=requirements.txt",
187-
"--quiet",
188189
]
189190
)
190191

191192
if not is_pre_commit:
192193
box_print(
193-
"Call the 'uv export --frozen --no-dev --output-file=requirements.txt --quiet' command to manually export to 'requirements.txt'."
194-
)
195-
info_print(
196-
"You can automate the export of requirements.txt with pre-commit. Try `usethis tool pre-commit`."
194+
"Run 'uv export --no-dev --output-file=requirements.txt' to write 'requirements.txt'."
197195
)
198196
else:
199-
box_print(
200-
"Call the 'pre-commit run uv-export' command to manually export to 'requirements.txt'."
201-
)
197+
box_print("Run the 'pre-commit run uv-export' to write 'requirements.txt'.")
202198
else:
203199
if PreCommitTool().is_used():
204200
tool.remove_pre_commit_repo_configs()
@@ -245,10 +241,8 @@ def use_ruff(*, remove: bool = False) -> None:
245241
if PreCommitTool().is_used():
246242
tool.add_pre_commit_repo_configs()
247243

248-
box_print(
249-
"Call the 'ruff check --fix' command to run the Ruff linter with autofixes."
250-
)
251-
box_print("Call the 'ruff format' command to run the Ruff formatter.")
244+
box_print("Run 'ruff check --fix' to run the Ruff linter with autofixes.")
245+
box_print("Run 'ruff format' to run the Ruff formatter.")
252246
else:
253247
if PreCommitTool().is_used():
254248
tool.remove_pre_commit_repo_configs()

0 commit comments

Comments
 (0)