|
16 | 16 | cleanup_backup_tag, |
17 | 17 | create_backup_point, |
18 | 18 | format_conflict_report, |
| 19 | + get_available_versions, |
| 20 | + get_latest_version, |
19 | 21 | rollback_to_backup, |
20 | 22 | ) |
21 | 23 |
|
@@ -352,3 +354,150 @@ def test_returns_fallback_on_invalid_ref(self) -> None: |
352 | 354 | # Should contain fallback message with "Changelog not available" or actual changelog |
353 | 355 | assert isinstance(result, str) |
354 | 356 | assert "Changelog not available" in result or "Changes:" in result |
| 357 | + |
| 358 | + |
| 359 | +@pytest.fixture |
| 360 | +def git_repo_with_tags(tmp_path: Path) -> Path: |
| 361 | + """Initialize a git repository with version tags (including prereleases).""" |
| 362 | + subprocess.run(["git", "init"], cwd=tmp_path, capture_output=True, check=True) |
| 363 | + subprocess.run( |
| 364 | + ["git", "config", "user.email", "test@test.com"], |
| 365 | + cwd=tmp_path, |
| 366 | + capture_output=True, |
| 367 | + check=True, |
| 368 | + ) |
| 369 | + subprocess.run( |
| 370 | + ["git", "config", "user.name", "Test"], |
| 371 | + cwd=tmp_path, |
| 372 | + capture_output=True, |
| 373 | + check=True, |
| 374 | + ) |
| 375 | + |
| 376 | + # Create a copier.yml to make it a valid template root |
| 377 | + (tmp_path / "copier.yml").write_text("_subdirectory: templates\n") |
| 378 | + |
| 379 | + # Create initial commit and tags |
| 380 | + (tmp_path / "test.txt").write_text("v0.1.0") |
| 381 | + subprocess.run(["git", "add", "."], cwd=tmp_path, capture_output=True, check=True) |
| 382 | + subprocess.run( |
| 383 | + ["git", "commit", "-m", "v0.1.0"], cwd=tmp_path, capture_output=True, check=True |
| 384 | + ) |
| 385 | + subprocess.run( |
| 386 | + ["git", "tag", "v0.1.0"], cwd=tmp_path, capture_output=True, check=True |
| 387 | + ) |
| 388 | + |
| 389 | + # Add more commits and tags |
| 390 | + (tmp_path / "test.txt").write_text("v0.2.0") |
| 391 | + subprocess.run(["git", "add", "."], cwd=tmp_path, capture_output=True, check=True) |
| 392 | + subprocess.run( |
| 393 | + ["git", "commit", "-m", "v0.2.0"], cwd=tmp_path, capture_output=True, check=True |
| 394 | + ) |
| 395 | + subprocess.run( |
| 396 | + ["git", "tag", "v0.2.0"], cwd=tmp_path, capture_output=True, check=True |
| 397 | + ) |
| 398 | + |
| 399 | + # Add a prerelease tag |
| 400 | + (tmp_path / "test.txt").write_text("v0.3.0rc1") |
| 401 | + subprocess.run(["git", "add", "."], cwd=tmp_path, capture_output=True, check=True) |
| 402 | + subprocess.run( |
| 403 | + ["git", "commit", "-m", "v0.3.0rc1"], |
| 404 | + cwd=tmp_path, |
| 405 | + capture_output=True, |
| 406 | + check=True, |
| 407 | + ) |
| 408 | + subprocess.run( |
| 409 | + ["git", "tag", "v0.3.0rc1"], cwd=tmp_path, capture_output=True, check=True |
| 410 | + ) |
| 411 | + |
| 412 | + # Add another prerelease |
| 413 | + (tmp_path / "test.txt").write_text("v0.3.0-alpha.1") |
| 414 | + subprocess.run(["git", "add", "."], cwd=tmp_path, capture_output=True, check=True) |
| 415 | + subprocess.run( |
| 416 | + ["git", "commit", "-m", "v0.3.0-alpha.1"], |
| 417 | + cwd=tmp_path, |
| 418 | + capture_output=True, |
| 419 | + check=True, |
| 420 | + ) |
| 421 | + subprocess.run( |
| 422 | + ["git", "tag", "v0.3.0-alpha.1"], cwd=tmp_path, capture_output=True, check=True |
| 423 | + ) |
| 424 | + |
| 425 | + return tmp_path |
| 426 | + |
| 427 | + |
| 428 | +class TestGetAvailableVersions: |
| 429 | + """Tests for get_available_versions function.""" |
| 430 | + |
| 431 | + def test_returns_versions_sorted_newest_first( |
| 432 | + self, git_repo_with_tags: Path |
| 433 | + ) -> None: |
| 434 | + """Test that versions are returned sorted newest first.""" |
| 435 | + versions = get_available_versions(git_repo_with_tags) |
| 436 | + |
| 437 | + # Should get 0.2.0 and 0.1.0 (no prereleases by default) |
| 438 | + assert len(versions) == 2 |
| 439 | + assert versions[0] == "0.2.0" # Newest first |
| 440 | + assert versions[1] == "0.1.0" |
| 441 | + |
| 442 | + def test_excludes_prereleases_by_default(self, git_repo_with_tags: Path) -> None: |
| 443 | + """Test that prereleases are excluded by default.""" |
| 444 | + versions = get_available_versions(git_repo_with_tags) |
| 445 | + |
| 446 | + # Should NOT contain rc or alpha versions |
| 447 | + assert "0.3.0rc1" not in versions |
| 448 | + assert "0.3.0-alpha.1" not in versions |
| 449 | + |
| 450 | + def test_includes_prereleases_when_requested( |
| 451 | + self, git_repo_with_tags: Path |
| 452 | + ) -> None: |
| 453 | + """Test that prereleases are included when include_prereleases=True.""" |
| 454 | + versions = get_available_versions(git_repo_with_tags, include_prereleases=True) |
| 455 | + |
| 456 | + # Should contain all versions including prereleases |
| 457 | + assert "0.3.0rc1" in versions |
| 458 | + assert "0.3.0-alpha.1" in versions |
| 459 | + assert "0.2.0" in versions |
| 460 | + assert "0.1.0" in versions |
| 461 | + |
| 462 | + def test_returns_empty_for_no_tags(self, git_repo: Path) -> None: |
| 463 | + """Test that empty list is returned when no version tags exist.""" |
| 464 | + # Create copier.yml to make it a valid template root |
| 465 | + (git_repo / "copier.yml").write_text("_subdirectory: templates\n") |
| 466 | + subprocess.run(["git", "add", "."], cwd=git_repo, capture_output=True) |
| 467 | + subprocess.run( |
| 468 | + ["git", "commit", "-m", "Add copier.yml"], |
| 469 | + cwd=git_repo, |
| 470 | + capture_output=True, |
| 471 | + ) |
| 472 | + |
| 473 | + versions = get_available_versions(git_repo) |
| 474 | + |
| 475 | + assert versions == [] |
| 476 | + |
| 477 | + |
| 478 | +class TestGetLatestVersion: |
| 479 | + """Tests for get_latest_version function.""" |
| 480 | + |
| 481 | + def test_returns_latest_non_prerelease_version( |
| 482 | + self, git_repo_with_tags: Path |
| 483 | + ) -> None: |
| 484 | + """Test that latest version is 0.2.0, not a prerelease.""" |
| 485 | + latest = get_latest_version(git_repo_with_tags) |
| 486 | + |
| 487 | + # Should be 0.2.0, NOT 0.3.0rc1 or 0.3.0-alpha.1 |
| 488 | + assert latest == "0.2.0" |
| 489 | + |
| 490 | + def test_returns_none_for_no_versions(self, git_repo: Path) -> None: |
| 491 | + """Test that None is returned when no version tags exist.""" |
| 492 | + # Create copier.yml to make it a valid template root |
| 493 | + (git_repo / "copier.yml").write_text("_subdirectory: templates\n") |
| 494 | + subprocess.run(["git", "add", "."], cwd=git_repo, capture_output=True) |
| 495 | + subprocess.run( |
| 496 | + ["git", "commit", "-m", "Add copier.yml"], |
| 497 | + cwd=git_repo, |
| 498 | + capture_output=True, |
| 499 | + ) |
| 500 | + |
| 501 | + latest = get_latest_version(git_repo) |
| 502 | + |
| 503 | + assert latest is None |
0 commit comments