Skip to content
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
15 changes: 12 additions & 3 deletions src/usethis/_integrations/uv/deps.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import pydantic
from packaging.requirements import Requirement
from pydantic import BaseModel, TypeAdapter

Expand Down Expand Up @@ -36,9 +37,17 @@ def get_dep_groups() -> dict[str, list[Dependency]]:
# will be deprecated.
return {}

req_strs_by_group = TypeAdapter(dict[str, list[str]]).validate_python(
dep_groups_section
)
try:
req_strs_by_group = TypeAdapter(dict[str, list[str]]).validate_python(
dep_groups_section
)
except pydantic.ValidationError as err:
msg = (
"Failed to parse the 'dependency-groups' section in 'pyproject.toml':\n"
f"{err}\n\n"
"Please check the section and try again."
)
raise UVDepGroupError(msg) from None
reqs_by_group = {
group: [Requirement(req_str) for req_str in req_strs]
for group, req_strs in req_strs_by_group.items()
Expand Down
2 changes: 2 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ def _uv_init_dir(tmp_path_factory: pytest.TempPathFactory) -> Path:
"init",
"--lib",
"--python",
# Deliberately kept at at a version other than the latest version to
# check range checks e.g. for Bitbucket pipelines matrixes.
"3.12",
"--vcs",
"none",
Expand Down
3 changes: 2 additions & 1 deletion tests/test_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ def test_help_flag():
runner = CliRunner()

# Act
runner.invoke(app, ["--help"])
result = runner.invoke(app, ["--help"])
assert result.exit_code == 0, result.stdout
14 changes: 14 additions & 0 deletions tests/usethis/_integrations/uv/test_deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,20 @@ def test_no_pyproject(self, tmp_path: Path):
# Assert
assert result == {}

def test_invalid_dtype(self, tmp_path: Path):
# Arrange
(tmp_path / "pyproject.toml").write_text("""\
[dependency-groups]
test="not a list"
""")
# Act, Assert
with (
change_cwd(tmp_path),
PyprojectTOMLManager(),
pytest.raises(UVDepGroupError),
):
get_dep_groups()


class TestAddDepsToGroup:
@pytest.mark.usefixtures("_vary_network_conn")
Expand Down
7 changes: 7 additions & 0 deletions tests/usethis/_interface/maximal_bitbucket_pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ pipelines:
script:
- *install-uv
- uv run pre-commit run --all-files
- step:
name: Test on 3.12
caches:
- uv
script:
- *install-uv
- uv run --python 3.12 pytest -x --junitxml=test-reports/report.xml
- step:
name: Test on 3.13
caches:
Expand Down
26 changes: 15 additions & 11 deletions tests/usethis/_interface/test_interface_ci.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,34 @@ def test_remove(self, tmp_path: Path):
assert not (tmp_path / "bitbucket-pipelines.yml").exists()

@pytest.mark.usefixtures("_vary_network_conn")
def test_maximal_config(self, tmp_path: Path):
def test_maximal_config(self, uv_init_repo_dir: Path):
# N.B. uv_init_repo_dir is used since we need git if we want to add pre-commit
runner = CliRunner()
with change_cwd(tmp_path):
# Pin the Python version
(tmp_path / ".python-version").write_text(
"3.13" # Bump to latest version of Python
)

with change_cwd(uv_init_repo_dir):
# Arrange
for tool_command in ALL_TOOL_COMMANDS:
if not usethis_config.offline:
runner.invoke(main_app, ["tool", tool_command])
result = runner.invoke(main_app, ["tool", tool_command])
else:
runner.invoke(main_app, ["tool", tool_command, "--offline"])
result = runner.invoke(
main_app, ["tool", tool_command, "--offline"]
)
assert not result.exit_code, f"{tool_command=}: {result.stdout}"

# Act
runner.invoke(app) # The CI menu only has 1 command (bitbucket
result = runner.invoke(app) # The CI menu only has 1 command (bitbucket
# pipelines) so we skip the subcommand here
assert not result.exit_code, result.stdout

# Assert
expected_yml = (
# N.B. when updating this file, check it against the validator:
# https://bitbucket.org/product/pipelines/validator
Path(__file__).parent / "maximal_bitbucket_pipelines.yml"
).read_text()
assert (tmp_path / "bitbucket-pipelines.yml").read_text() == expected_yml
assert (
uv_init_repo_dir / "bitbucket-pipelines.yml"
).read_text() == expected_yml

def test_invalid_pyproject_toml(self, tmp_path: Path):
# Arrange
Expand Down
26 changes: 17 additions & 9 deletions tests/usethis/_interface/test_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,17 +486,25 @@ def test_several_tools_add_and_remove(tmp_path: Path):
(tmp_path / "src" / "benchmark").mkdir(exist_ok=True)
(tmp_path / "src" / "benchmark" / "__init__.py").touch(exist_ok=True)

# Act
runner = CliRunner()
with change_cwd(tmp_path):
runner.invoke(app, ["pytest"])
runner.invoke(app, ["coverage.py"])
runner.invoke(app, ["ruff"])
runner.invoke(app, ["deptry"])
runner.invoke(app, ["pre-commit"])
runner.invoke(app, ["ruff", "--remove"])
runner.invoke(app, ["pyproject-fmt"])
runner.invoke(app, ["pytest", "--remove"])
# Act, Assert
result = runner.invoke(app, ["pytest"])
assert not result.exit_code, result.stdout
result = runner.invoke(app, ["coverage"])
assert not result.exit_code, result.stdout
result = runner.invoke(app, ["ruff"])
assert not result.exit_code, result.stdout
result = runner.invoke(app, ["deptry"])
assert not result.exit_code, result.stdout
result = runner.invoke(app, ["pre-commit"])
assert not result.exit_code, result.stdout
result = runner.invoke(app, ["ruff", "--remove"])
assert not result.exit_code, result.stdout
result = runner.invoke(app, ["pyproject-fmt"])
assert not result.exit_code, result.stdout
result = runner.invoke(app, ["pytest", "--remove"])
assert not result.exit_code, result.stdout


def test_tool_matches_command():
Expand Down