Skip to content

Commit 0c6fb50

Browse files
Refactor tests to reduce creation of venvs for speed (#294)
* Refactor tests to reduce creation of venvs for speed Introduce new message for `uv add --frozen` to ask for manual install * Tweak pre-commit placeholder Fix broken test * Fix broken test * Tweak test for better error messaging * Add more descriptive message to test * Don't use uvx for pre-commit calls and placeholder * Fix placeholder definition * Fix messages
1 parent fbbdec6 commit 0c6fb50

9 files changed

Lines changed: 114 additions & 75 deletions

File tree

src/usethis/_core/tool.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
remove_bitbucket_pytest_steps,
66
update_bitbucket_pytest_steps,
77
)
8+
from usethis._config import usethis_config
89
from usethis._console import box_print, tick_print
910
from usethis._integrations.bitbucket.steps import (
1011
add_bitbucket_steps_in_default,
@@ -170,9 +171,6 @@ def use_pre_commit(*, remove: bool = False) -> None:
170171
remove_bitbucket_steps_from_default(tool.get_bitbucket_steps())
171172
_add_bitbucket_linter_steps_to_default()
172173

173-
# Need pre-commit to be installed so we can uninstall hooks
174-
add_deps_to_group(tool.dev_deps, "dev")
175-
176174
uninstall_pre_commit_hooks()
177175

178176
remove_pre_commit_config()
@@ -313,19 +311,20 @@ def use_requirements_txt(*, remove: bool = False) -> None:
313311

314312
if not path.exists():
315313
# N.B. this is where a task runner would come in handy, to reduce duplication.
316-
if not (Path.cwd() / "uv.lock").exists():
314+
if not (Path.cwd() / "uv.lock").exists() and not usethis_config.frozen:
317315
tick_print("Writing 'uv.lock'.")
318316
call_uv_subprocess(["lock"])
319317

320-
tick_print("Writing 'requirements.txt'.")
321-
call_uv_subprocess(
322-
[
323-
"export",
324-
"--frozen",
325-
"--no-dev",
326-
"--output-file=requirements.txt",
327-
]
328-
)
318+
if not usethis_config.frozen:
319+
tick_print("Writing 'requirements.txt'.")
320+
call_uv_subprocess(
321+
[
322+
"export",
323+
"--frozen",
324+
"--no-dev",
325+
"--output-file=requirements.txt",
326+
]
327+
)
329328

330329
if not is_pre_commit:
331330
_requirements_txt_instructions_basic()

src/usethis/_integrations/pre_commit/core.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def install_pre_commit_hooks() -> None:
2424
in a git repo.
2525
"""
2626
if usethis_config.frozen:
27-
box_print("Run 'uv run pre-commit install' to register pre-commit with git.")
27+
box_print("Run 'pre-commit install' to register pre-commit.")
2828
return
2929

3030
tick_print("Ensuring pre-commit is installed to Git.")
@@ -47,16 +47,17 @@ def install_pre_commit_hooks() -> None:
4747
def uninstall_pre_commit_hooks() -> None:
4848
"""Uninstall pre-commit hooks.
4949
50-
Note that this requires pre-commit to be installed. It also requires the user to be
51-
in a git repo.
50+
Note that this requires the user to be in a git repo.
5251
"""
5352
if usethis_config.frozen:
54-
box_print("Run 'uvx pre-commit uninstall' to deregister pre-commit with git.")
53+
box_print(
54+
"Run 'uv run --with pre-commit pre-commit uninstall' to deregister pre-commit."
55+
)
5556
return
5657

5758
tick_print("Ensuring pre-commit hooks are uninstalled.")
5859
try:
59-
call_uv_subprocess(["run", "pre-commit", "uninstall"])
60+
call_uv_subprocess(["run", "--with", "pre-commit", "pre-commit", "uninstall"])
6061
except UVSubprocessFailedError as err:
6162
msg = f"Failed to uninstall pre-commit hooks:\n{err}"
6263
raise PreCommitInstallationError(msg) from None

src/usethis/_integrations/pre_commit/hooks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def _get_placeholder_repo_config() -> LocalRepo:
137137
HookDefinition(
138138
id=_PLACEHOLDER_ID,
139139
name="Placeholder - add your own hooks!",
140-
entry="""uv run python -c "print('hello world!')\"""",
140+
entry="""uv run --frozen python -c "print('hello world!')\"""",
141141
language=Language("system"),
142142
)
143143
],

src/usethis/_integrations/uv/call.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ def call_uv_subprocess(args: list[str]) -> str:
1313
read_pyproject_toml_from_path.cache_clear()
1414

1515
if usethis_config.frozen and args[0] in {
16+
# Note, not "lock", for which the --frozen flags has quite a different effect
1617
"add",
1718
"remove",
1819
"sync",
19-
"lock",
2020
"export",
2121
"tree",
2222
"run",

src/usethis/_integrations/uv/deps.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from pydantic import BaseModel, TypeAdapter
33

44
from usethis._config import usethis_config
5-
from usethis._console import tick_print
5+
from usethis._console import box_print, tick_print
66
from usethis._integrations.pyproject.core import (
77
append_config_list,
88
get_config_value,
@@ -107,6 +107,9 @@ def add_deps_to_group(deps: list[Dependency], group: str) -> None:
107107
f"Adding dependenc{ies} {deps_str} to the '{group}' group in 'pyproject.toml'."
108108
)
109109

110+
if usethis_config.frozen:
111+
box_print(f"Install the dependenc{ies} {deps_str}.")
112+
110113
register_default_group(group) # Register the group before adding dependencies
111114

112115
for dep in to_add_deps:

0 commit comments

Comments
 (0)