Skip to content

Commit c9d753c

Browse files
Fix bug in add_deps_to_group and test (#218)
1 parent 4678237 commit c9d753c

2 files changed

Lines changed: 34 additions & 15 deletions

File tree

src/usethis/_integrations/uv/deps.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,51 +37,50 @@ def get_deps_from_group(group: str) -> list[str]:
3737
return []
3838

3939

40-
def add_deps_to_group(pypi_names: list[str], group: str) -> None:
40+
def add_deps_to_group(deps: list[str], group: str) -> None:
4141
"""Add a package as a non-build dependency using PEP 735 dependency groups."""
4242
existing_group = get_deps_from_group(group)
4343

44-
deps = [dep for dep in pypi_names if _strip_extras(dep) not in existing_group]
44+
_deps = [dep for dep in deps if _strip_extras(dep) not in existing_group]
4545

46-
if not deps:
46+
if not _deps:
4747
return
4848

49-
deps_str = ", ".join([f"'{_strip_extras(dep)}'" for dep in deps])
50-
ies = "y" if len(deps) == 1 else "ies"
49+
deps_str = ", ".join([f"'{_strip_extras(dep)}'" for dep in _deps])
50+
ies = "y" if len(_deps) == 1 else "ies"
5151
tick_print(
5252
f"Adding dependenc{ies} {deps_str} to the '{group}' group in 'pyproject.toml'."
5353
)
5454

55-
for dep in deps:
55+
for dep in _deps:
5656
try:
57-
se_dep = _strip_extras(dep)
5857
if not usethis_config.offline:
59-
call_uv_subprocess(["add", "--group", group, "--quiet", se_dep])
58+
call_uv_subprocess(["add", "--group", group, "--quiet", dep])
6059
else:
6160
call_uv_subprocess(
62-
["add", "--group", group, "--quiet", "--offline", se_dep]
61+
["add", "--group", group, "--quiet", "--offline", dep]
6362
)
6463
except UVSubprocessFailedError as err:
6564
msg = f"Failed to add '{dep}' to the '{group}' dependency group:\n{err}"
6665
raise UVDepGroupError(msg) from None
6766

6867

69-
def remove_deps_from_group(pypi_names: list[str], group: str) -> None:
68+
def remove_deps_from_group(deps: list[str], group: str) -> None:
7069
"""Remove the tool's development dependencies, if present."""
7170
existing_group = get_deps_from_group(group)
7271

73-
deps = [dep for dep in pypi_names if _strip_extras(dep) in existing_group]
72+
_deps = [dep for dep in deps if _strip_extras(dep) in existing_group]
7473

75-
if not deps:
74+
if not _deps:
7675
return
7776

78-
deps_str = ", ".join([f"'{_strip_extras(dep)}'" for dep in deps])
79-
ies = "y" if len(deps) == 1 else "ies"
77+
deps_str = ", ".join([f"'{_strip_extras(dep)}'" for dep in _deps])
78+
ies = "y" if len(_deps) == 1 else "ies"
8079
tick_print(
8180
f"Removing dependenc{ies} {deps_str} from the '{group}' group in 'pyproject.toml'."
8281
)
8382

84-
for dep in deps:
83+
for dep in _deps:
8584
try:
8685
se_dep = _strip_extras(dep)
8786
if not usethis_config.offline:

tests/usethis/_integrations/uv/test_deps.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
is_dep_in_any_group,
1111
remove_deps_from_group,
1212
)
13+
from usethis._integrations.uv.errors import UVDepGroupError
1314
from usethis._test import change_cwd
1415

1516

@@ -140,13 +141,32 @@ def test_extras(self, uv_init_dir: Path, capfd: pytest.CaptureFixture[str]):
140141

141142
# Assert
142143
assert "pytest" in get_deps_from_group("test")
144+
content = (uv_init_dir / "pyproject.toml").read_text()
145+
assert "pytest[extra]" in content
143146
out, err = capfd.readouterr()
144147
assert not err
145148
assert (
146149
out
147150
== "✔ Adding dependency 'pytest' to the 'test' group in 'pyproject.toml'.\n"
148151
)
149152

153+
@pytest.mark.usefixtures("_vary_network_conn")
154+
def test_empty_deps(self, uv_init_dir: Path, capfd: pytest.CaptureFixture[str]):
155+
with change_cwd(uv_init_dir):
156+
# Act
157+
add_deps_to_group([], "test")
158+
159+
# Assert
160+
assert not get_deps_from_group("test")
161+
out, err = capfd.readouterr()
162+
assert not err
163+
assert not out
164+
165+
@pytest.mark.usefixtures("_vary_network_conn")
166+
def test_bad_dep_string(self, uv_init_dir: Path, capfd: pytest.CaptureFixture[str]):
167+
with change_cwd(uv_init_dir), pytest.raises(UVDepGroupError):
168+
add_deps_to_group(["pytest[extra"], "test")
169+
150170

151171
class TestRemoveDepsFromGroup:
152172
@pytest.mark.usefixtures("_vary_network_conn")

0 commit comments

Comments
 (0)