Skip to content

Commit

Permalink
Better error messages for subprocess calls (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanjmcdougall authored Nov 1, 2024
1 parent 6e9fab9 commit 60c7681
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 45 deletions.
14 changes: 3 additions & 11 deletions src/usethis/_integrations/pre_commit/core.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import subprocess
from pathlib import Path

from usethis._console import tick_print
from usethis._integrations.github.tags import GitHubTagError, get_github_latest_tag
from usethis._integrations.uv.call import call_subprocess

_YAML_CONTENTS_TEMPLATE = """\
repos:
Expand Down Expand Up @@ -43,17 +43,9 @@ def remove_pre_commit_config() -> None:

def install_pre_commit() -> None:
tick_print("Ensuring pre-commit hooks are installed.")
subprocess.run(
["uv", "run", "pre-commit", "install"],
check=True,
stdout=subprocess.DEVNULL,
)
call_subprocess(["uv", "run", "pre-commit", "install"])


def uninstall_pre_commit() -> None:
tick_print("Ensuring pre-commit hooks are uninstalled.")
subprocess.run(
["uv", "run", "pre-commit", "uninstall"],
check=True,
stdout=subprocess.DEVNULL,
)
call_subprocess(["uv", "run", "pre-commit", "uninstall"])
17 changes: 17 additions & 0 deletions src/usethis/_integrations/uv/call.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import subprocess
import sys

from usethis._console import console


def call_subprocess(args: list[str]) -> None:
try:
subprocess.run(
args,
check=True,
capture_output=True,
)
except subprocess.CalledProcessError as e:
msg = e.stderr.decode()
console.print(f"✗ Failed to run uv subprocess:\n{msg}", style="red")
sys.exit(e.returncode)
38 changes: 8 additions & 30 deletions src/usethis/_integrations/uv/deps.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import re
import subprocess

from packaging.requirements import Requirement
from pydantic import TypeAdapter

from usethis._config import usethis_config
from usethis._console import tick_print
from usethis._integrations.pyproject.io import read_pyproject_toml
from usethis._integrations.uv.call import call_subprocess


def get_dep_groups() -> dict[str, list[str]]:
Expand Down Expand Up @@ -47,14 +47,10 @@ def add_deps_to_group(pypi_names: list[str], group: str) -> None:

tick_print(f"Adding '{dep}' to the '{group}' dependency group.")
if not usethis_config.offline:
subprocess.run(
["uv", "add", "--group", group, "--quiet", dep],
check=True,
)
call_subprocess(["uv", "add", "--group", group, "--quiet", dep])
else:
subprocess.run(
["uv", "add", "--group", group, "--quiet", "--offline", dep],
check=True,
call_subprocess(
["uv", "add", "--group", group, "--quiet", "--offline", dep]
)


Expand All @@ -68,30 +64,12 @@ def remove_deps_from_group(pypi_names: list[str], group: str) -> None:
continue

tick_print(f"Removing '{dep}' from the '{group}' dependency group.")
se_dep = _strip_extras(dep)
if not usethis_config.offline:
subprocess.run(
[
"uv",
"remove",
"--group",
group,
"--quiet",
_strip_extras(dep),
],
check=True,
)
call_subprocess(["uv", "remove", "--group", group, "--quiet", se_dep])
else:
subprocess.run(
[
"uv",
"remove",
"--group",
group,
"--quiet",
"--offline",
_strip_extras(dep),
],
check=True,
call_subprocess(
["uv", "remove", "--group", group, "--quiet", "--offline", se_dep]
)


Expand Down
10 changes: 6 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import subprocess
from collections.abc import Generator
from enum import Enum
from pathlib import Path
Expand All @@ -7,18 +6,21 @@
from git import Repo

from usethis._config import usethis_config
from usethis._utils._test import is_offline
from usethis._integrations.uv.call import call_subprocess
from usethis._utils._test import change_cwd, is_offline


@pytest.fixture
def uv_init_dir(tmp_path: Path) -> Path:
subprocess.run(["uv", "init", "--lib"], cwd=tmp_path, check=True)
with change_cwd(tmp_path):
call_subprocess(["uv", "init", "--lib"])
return tmp_path


@pytest.fixture
def uv_init_repo_dir(tmp_path: Path) -> Path:
subprocess.run(["uv", "init", "--lib"], cwd=tmp_path, check=True)
with change_cwd(tmp_path):
call_subprocess(["uv", "init", "--lib"])
Repo.init(tmp_path)
return tmp_path

Expand Down

0 comments on commit 60c7681

Please sign in to comment.