Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor tests to reduce creation of venvs for speed #294

Merged
merged 8 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions src/usethis/_core/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
remove_bitbucket_pytest_steps,
update_bitbucket_pytest_steps,
)
from usethis._config import usethis_config
from usethis._console import box_print, tick_print
from usethis._integrations.bitbucket.steps import (
add_bitbucket_steps_in_default,
Expand Down Expand Up @@ -170,9 +171,6 @@ def use_pre_commit(*, remove: bool = False) -> None:
remove_bitbucket_steps_from_default(tool.get_bitbucket_steps())
_add_bitbucket_linter_steps_to_default()

# Need pre-commit to be installed so we can uninstall hooks
add_deps_to_group(tool.dev_deps, "dev")

uninstall_pre_commit_hooks()

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

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

tick_print("Writing 'requirements.txt'.")
call_uv_subprocess(
[
"export",
"--frozen",
"--no-dev",
"--output-file=requirements.txt",
]
)
if not usethis_config.frozen:
tick_print("Writing 'requirements.txt'.")
call_uv_subprocess(
[
"export",
"--frozen",
"--no-dev",
"--output-file=requirements.txt",
]
)

if not is_pre_commit:
_requirements_txt_instructions_basic()
Expand Down
11 changes: 6 additions & 5 deletions src/usethis/_integrations/pre_commit/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def install_pre_commit_hooks() -> None:
in a git repo.
"""
if usethis_config.frozen:
box_print("Run 'uv run pre-commit install' to register pre-commit with git.")
box_print("Run 'pre-commit install' to register pre-commit.")
return

tick_print("Ensuring pre-commit is installed to Git.")
Expand All @@ -47,16 +47,17 @@ def install_pre_commit_hooks() -> None:
def uninstall_pre_commit_hooks() -> None:
"""Uninstall pre-commit hooks.

Note that this requires pre-commit to be installed. It also requires the user to be
in a git repo.
Note that this requires the user to be in a git repo.
"""
if usethis_config.frozen:
box_print("Run 'uvx pre-commit uninstall' to deregister pre-commit with git.")
box_print(
"Run 'uv run --with pre-commit pre-commit uninstall' to deregister pre-commit."
)
return

tick_print("Ensuring pre-commit hooks are uninstalled.")
try:
call_uv_subprocess(["run", "pre-commit", "uninstall"])
call_uv_subprocess(["run", "--with", "pre-commit", "pre-commit", "uninstall"])
except UVSubprocessFailedError as err:
msg = f"Failed to uninstall pre-commit hooks:\n{err}"
raise PreCommitInstallationError(msg) from None
2 changes: 1 addition & 1 deletion src/usethis/_integrations/pre_commit/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def _get_placeholder_repo_config() -> LocalRepo:
HookDefinition(
id=_PLACEHOLDER_ID,
name="Placeholder - add your own hooks!",
entry="""uv run python -c "print('hello world!')\"""",
entry="""uv run --frozen python -c "print('hello world!')\"""",
language=Language("system"),
)
],
Expand Down
2 changes: 1 addition & 1 deletion src/usethis/_integrations/uv/call.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ def call_uv_subprocess(args: list[str]) -> str:
read_pyproject_toml_from_path.cache_clear()

if usethis_config.frozen and args[0] in {
# Note, not "lock", for which the --frozen flags has quite a different effect
"add",
"remove",
"sync",
"lock",
"export",
"tree",
"run",
Expand Down
5 changes: 4 additions & 1 deletion src/usethis/_integrations/uv/deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from pydantic import BaseModel, TypeAdapter

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

if usethis_config.frozen:
box_print(f"Install the dependenc{ies} {deps_str}.")

register_default_group(group) # Register the group before adding dependencies

for dep in to_add_deps:
Expand Down
Loading