Skip to content

Commit 3314678

Browse files
Add support for uv.toml (#538)
1 parent 43566dc commit 3314678

File tree

5 files changed

+93
-4
lines changed

5 files changed

+93
-4
lines changed

src/usethis/_config_file.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from usethis._integrations.file.pyproject_toml.io_ import PyprojectTOMLManager
99
from usethis._integrations.file.setup_cfg.io_ import SetupCFGManager
1010
from usethis._integrations.file.toml.io_ import TOMLFileManager
11+
from usethis._integrations.uv.toml import UVTOMLManager
1112

1213
if TYPE_CHECKING:
1314
from collections.abc import Iterator
@@ -26,6 +27,7 @@ def files_manager() -> Iterator[None]:
2627
PytestINIManager(),
2728
RuffTOMLManager(),
2829
ToxINIManager(),
30+
UVTOMLManager(),
2931
):
3032
yield
3133

src/usethis/_integrations/uv/deps.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from usethis._integrations.file.pyproject_toml.io_ import PyprojectTOMLManager
1111
from usethis._integrations.uv.call import call_uv_subprocess
1212
from usethis._integrations.uv.errors import UVDepGroupError, UVSubprocessFailedError
13+
from usethis._integrations.uv.toml import UVTOMLManager
1314

1415

1516
class Dependency(BaseModel):
@@ -86,14 +87,20 @@ def register_default_group(group: str) -> None:
8687

8788

8889
def add_default_groups(groups: list[str]) -> None:
89-
PyprojectTOMLManager().extend_list(
90-
keys=["tool", "uv", "default-groups"], values=groups
91-
)
90+
if UVTOMLManager().path.exists():
91+
UVTOMLManager().extend_list(keys=["default-groups"], values=groups)
92+
else:
93+
PyprojectTOMLManager().extend_list(
94+
keys=["tool", "uv", "default-groups"], values=groups
95+
)
9296

9397

9498
def get_default_groups() -> list[str]:
9599
try:
96-
default_groups = PyprojectTOMLManager()[["tool", "uv", "default-groups"]]
100+
if UVTOMLManager().path.exists():
101+
default_groups = UVTOMLManager()[["default-groups"]]
102+
else:
103+
default_groups = PyprojectTOMLManager()[["tool", "uv", "default-groups"]]
97104
if not isinstance(default_groups, list):
98105
default_groups = []
99106
except KeyError:

src/usethis/_integrations/uv/toml.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from pathlib import Path
2+
3+
from usethis._integrations.file.toml.io_ import TOMLFileManager
4+
5+
6+
class UVTOMLManager(TOMLFileManager):
7+
"""Class to manage the uv.toml file."""
8+
9+
@property
10+
def relative_path(self) -> Path:
11+
return Path("uv.toml")

tests/usethis/_core/test_core_tool.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
get_deps_from_group,
3636
is_dep_satisfied_in,
3737
)
38+
from usethis._integrations.uv.toml import UVTOMLManager
3839
from usethis._test import change_cwd
3940
from usethis._tool import ALL_TOOLS, PyprojectTOMLTool, PytestTool, RuffTool
4041

@@ -1991,6 +1992,19 @@ def test_registers_test_group(self, tmp_path: Path):
19911992
]
19921993
assert "test" in default_groups
19931994

1995+
@pytest.mark.usefixtures("_vary_network_conn")
1996+
def test_registers_test_group_uv_toml(self, tmp_path: Path):
1997+
with change_cwd(tmp_path), files_manager():
1998+
# Arrange
1999+
(tmp_path / "uv.toml").touch()
2000+
2001+
# Act
2002+
use_pytest()
2003+
2004+
# Assert
2005+
default_groups = UVTOMLManager()[["default-groups"]]
2006+
assert "test" in default_groups
2007+
19942008
@pytest.mark.usefixtures("_vary_network_conn")
19952009
def test_ruff_integration(self, uv_init_dir: Path):
19962010
with change_cwd(uv_init_dir), files_manager():

tests/usethis/_integrations/uv/test_deps.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
import usethis._integrations.uv
88
import usethis._integrations.uv.deps
99
from usethis._config import usethis_config
10+
from usethis._config_file import files_manager
1011
from usethis._integrations.file.pyproject_toml.io_ import PyprojectTOMLManager
1112
from usethis._integrations.uv.deps import (
1213
Dependency,
14+
add_default_groups,
1315
add_deps_to_group,
1416
get_default_groups,
1517
get_dep_groups,
@@ -20,6 +22,7 @@
2022
remove_deps_from_group,
2123
)
2224
from usethis._integrations.uv.errors import UVDepGroupError, UVSubprocessFailedError
25+
from usethis._integrations.uv.toml import UVTOMLManager
2326
from usethis._test import change_cwd
2427

2528

@@ -651,6 +654,25 @@ def test_dev_not_added_if_missing(self, tmp_path: Path):
651654
assert set(default_groups) == {"test"}
652655

653656

657+
class TestAddDefaultGroups:
658+
def test_uv_toml(self, tmp_path: Path):
659+
# Arrange
660+
(tmp_path / "uv.toml").touch()
661+
662+
# Act
663+
with change_cwd(tmp_path), files_manager():
664+
add_default_groups(["test"])
665+
666+
# Assert
667+
with change_cwd(tmp_path), UVTOMLManager():
668+
assert (
669+
(tmp_path / "uv.toml").read_text()
670+
== """\
671+
default-groups = ["test"]
672+
"""
673+
)
674+
675+
654676
class TestGetDefaultGroups:
655677
def test_empty_pyproject_toml(self, tmp_path: Path):
656678
# Arrange
@@ -676,3 +698,36 @@ def test_invalid_default_groups(self, tmp_path: Path):
676698

677699
# Assert
678700
assert result == []
701+
702+
def test_uv_toml(self, tmp_path: Path):
703+
# Arrange
704+
(tmp_path / "uv.toml").write_text("""\
705+
default-groups = ["test"]
706+
""")
707+
# Even if the pyproject.toml disagrees!
708+
(tmp_path / "pyproject.toml").write_text("""\
709+
[tool.uv]
710+
default-groups = ["doc"]
711+
""")
712+
713+
with change_cwd(tmp_path), files_manager():
714+
# Act
715+
result = get_default_groups()
716+
717+
# Assert
718+
assert result == ["test"]
719+
720+
def test_uv_toml_empty(self, tmp_path: Path):
721+
# Arrange
722+
(tmp_path / "uv.toml").touch()
723+
(tmp_path / "pyproject.toml").write_text("""\
724+
[tool.uv]
725+
default-groups = ["doc"]
726+
""")
727+
728+
with change_cwd(tmp_path), files_manager():
729+
# Act
730+
result = get_default_groups()
731+
732+
# Assert
733+
assert result == []

0 commit comments

Comments
 (0)