|
3 | 3 | import pytest
|
4 | 4 |
|
5 | 5 | from usethis._config import usethis_config
|
| 6 | +from usethis._integrations.pyproject.core import ( |
| 7 | + get_config_value, |
| 8 | + remove_config_value, |
| 9 | +) |
6 | 10 | from usethis._integrations.uv.deps import (
|
7 | 11 | Dependency,
|
8 | 12 | add_deps_to_group,
|
9 | 13 | get_dep_groups,
|
10 | 14 | get_deps_from_group,
|
11 | 15 | is_dep_in_any_group,
|
12 | 16 | is_dep_satisfied_in,
|
| 17 | + register_default_group, |
13 | 18 | remove_deps_from_group,
|
14 | 19 | )
|
15 | 20 | from usethis._test import change_cwd
|
@@ -242,6 +247,26 @@ def test_combine_extras_alphabetical(self, uv_init_dir: Path):
|
242 | 247 | content = (uv_init_dir / "pyproject.toml").read_text()
|
243 | 248 | assert "coverage[extra,toml]" in content
|
244 | 249 |
|
| 250 | + @pytest.mark.usefixtures("_vary_network_conn") |
| 251 | + def test_registers_default_group(self, uv_init_dir: Path): |
| 252 | + with change_cwd(uv_init_dir): |
| 253 | + # Act |
| 254 | + add_deps_to_group([Dependency(name="pytest")], "test") |
| 255 | + |
| 256 | + # Assert |
| 257 | + default_groups = get_config_value(["tool", "uv", "default-groups"]) |
| 258 | + assert "test" in default_groups |
| 259 | + |
| 260 | + @pytest.mark.usefixtures("_vary_network_conn") |
| 261 | + def test_dev_group_not_registered(self, uv_init_dir: Path): |
| 262 | + with change_cwd(uv_init_dir): |
| 263 | + # Act |
| 264 | + add_deps_to_group([Dependency(name="black")], "dev") |
| 265 | + |
| 266 | + # Assert |
| 267 | + # Tool section shouldn't even exist in pyproject.toml |
| 268 | + assert "tool" not in (uv_init_dir / "pyproject.toml").read_text() |
| 269 | + |
245 | 270 |
|
246 | 271 | class TestRemoveDepsFromGroup:
|
247 | 272 | @pytest.mark.usefixtures("_vary_network_conn")
|
@@ -344,6 +369,27 @@ def test_extras(self, uv_init_dir: Path, capfd: pytest.CaptureFixture[str]):
|
344 | 369 | == "✔ Removing dependency 'pytest' from the 'test' group in 'pyproject.toml'.\n"
|
345 | 370 | )
|
346 | 371 |
|
| 372 | + @pytest.mark.usefixtures("_vary_network_conn") |
| 373 | + def test_group_not_in_dependency_groups( |
| 374 | + self, uv_init_dir: Path, capfd: pytest.CaptureFixture[str] |
| 375 | + ): |
| 376 | + with change_cwd(uv_init_dir): |
| 377 | + # Arrange |
| 378 | + with usethis_config.set(quiet=True): |
| 379 | + add_deps_to_group([Dependency(name="pytest")], "test") |
| 380 | + |
| 381 | + # Remove the group from dependency-groups but keep it in default-groups |
| 382 | + remove_config_value(["dependency-groups", "test"]) |
| 383 | + |
| 384 | + # Act |
| 385 | + remove_deps_from_group([Dependency(name="pytest")], "test") |
| 386 | + |
| 387 | + # Assert |
| 388 | + assert not get_deps_from_group("test") |
| 389 | + out, err = capfd.readouterr() |
| 390 | + assert not err |
| 391 | + assert not out |
| 392 | + |
347 | 393 |
|
348 | 394 | class TestIsDepInAnyGroup:
|
349 | 395 | def test_no_group(self, uv_init_dir: Path):
|
@@ -430,3 +476,91 @@ def test_multiple(self):
|
430 | 476 |
|
431 | 477 | # Assert
|
432 | 478 | assert result
|
| 479 | + |
| 480 | + |
| 481 | +class TestRegisterDefaultGroup: |
| 482 | + def test_section_not_exists_adds_dev(self, tmp_path: Path): |
| 483 | + # Arrange |
| 484 | + (tmp_path / "pyproject.toml").write_text("") |
| 485 | + |
| 486 | + with change_cwd(tmp_path): |
| 487 | + # Act |
| 488 | + register_default_group("test") |
| 489 | + |
| 490 | + # Assert |
| 491 | + default_groups = get_config_value(["tool", "uv", "default-groups"]) |
| 492 | + assert set(default_groups) == {"test", "dev"} |
| 493 | + |
| 494 | + def test_empty_section_adds_dev(self, tmp_path: Path): |
| 495 | + # Arrange |
| 496 | + (tmp_path / "pyproject.toml").write_text("""\ |
| 497 | +[tool.uv] |
| 498 | +""") |
| 499 | + |
| 500 | + with change_cwd(tmp_path): |
| 501 | + # Act |
| 502 | + register_default_group("test") |
| 503 | + |
| 504 | + # Assert |
| 505 | + default_groups = get_config_value(["tool", "uv", "default-groups"]) |
| 506 | + assert set(default_groups) == {"test", "dev"} |
| 507 | + |
| 508 | + def test_empty_default_groups_adds_dev(self, tmp_path: Path): |
| 509 | + # Arrange |
| 510 | + (tmp_path / "pyproject.toml").write_text("""\ |
| 511 | +[tool.uv] |
| 512 | +default-groups = [] |
| 513 | +""") |
| 514 | + |
| 515 | + with change_cwd(tmp_path): |
| 516 | + # Act |
| 517 | + register_default_group("test") |
| 518 | + |
| 519 | + # Assert |
| 520 | + default_groups = get_config_value(["tool", "uv", "default-groups"]) |
| 521 | + assert set(default_groups) == {"test", "dev"} |
| 522 | + |
| 523 | + def test_existing_section_no_dev_added_if_no_other_groups(self, tmp_path: Path): |
| 524 | + # Arrange |
| 525 | + (tmp_path / "pyproject.toml").write_text("""\ |
| 526 | +[tool.uv] |
| 527 | +default-groups = ["test"] |
| 528 | +""") |
| 529 | + |
| 530 | + with change_cwd(tmp_path): |
| 531 | + # Act |
| 532 | + register_default_group("test") |
| 533 | + |
| 534 | + # Assert |
| 535 | + default_groups = get_config_value(["tool", "uv", "default-groups"]) |
| 536 | + assert set(default_groups) == {"test"} |
| 537 | + |
| 538 | + def test_existing_section_no_dev_added_if_dev_exists(self, tmp_path: Path): |
| 539 | + # Arrange |
| 540 | + (tmp_path / "pyproject.toml").write_text("""\ |
| 541 | +[tool.uv] |
| 542 | +default-groups = ["test", "dev"] |
| 543 | +""") |
| 544 | + |
| 545 | + with change_cwd(tmp_path): |
| 546 | + # Act |
| 547 | + register_default_group("docs") |
| 548 | + |
| 549 | + # Assert |
| 550 | + default_groups = get_config_value(["tool", "uv", "default-groups"]) |
| 551 | + assert set(default_groups) == {"test", "dev", "docs"} |
| 552 | + |
| 553 | + def test_existing_section_adds_dev_with_new_group(self, tmp_path: Path): |
| 554 | + # Arrange |
| 555 | + (tmp_path / "pyproject.toml").write_text("""\ |
| 556 | +[tool.uv] |
| 557 | +default-groups = ["test"] |
| 558 | +""") |
| 559 | + |
| 560 | + with change_cwd(tmp_path): |
| 561 | + # Act |
| 562 | + register_default_group("docs") |
| 563 | + |
| 564 | + # Assert |
| 565 | + default_groups = get_config_value(["tool", "uv", "default-groups"]) |
| 566 | + assert set(default_groups) == {"test", "docs", "dev"} |
0 commit comments