|
8 | 8 | from pathlib import Path |
9 | 9 | from unittest.mock import patch |
10 | 10 |
|
11 | | -from aegis.core.template_cleanup import cleanup_nested_project_directory |
| 11 | +from aegis.core.template_cleanup import ( |
| 12 | + _should_skip_sync, |
| 13 | + cleanup_nested_project_directory, |
| 14 | + sync_template_changes, |
| 15 | +) |
12 | 16 |
|
13 | 17 |
|
14 | 18 | class TestCleanupNestedProjectDirectory: |
@@ -346,3 +350,185 @@ def test_full_update_flow_with_ai_enabled(self, tmp_path: Path) -> None: |
346 | 350 | assert (tmp_path / "app" / "services" / "ai" / "agent.py").exists(), ( |
347 | 351 | "AI agent.py should be kept" |
348 | 352 | ) |
| 353 | + |
| 354 | + |
| 355 | +class TestShouldSkipSync: |
| 356 | + """Test _should_skip_sync helper function.""" |
| 357 | + |
| 358 | + def test_skips_copier_answers(self) -> None: |
| 359 | + """Test that .copier-answers.yml is skipped.""" |
| 360 | + assert _should_skip_sync(".copier-answers.yml") is True |
| 361 | + |
| 362 | + def test_skips_env_file(self) -> None: |
| 363 | + """Test that .env is skipped.""" |
| 364 | + assert _should_skip_sync(".env") is True |
| 365 | + |
| 366 | + def test_skips_python_version(self) -> None: |
| 367 | + """Test that .python-version is skipped.""" |
| 368 | + assert _should_skip_sync(".python-version") is True |
| 369 | + |
| 370 | + def test_skips_venv_directory(self) -> None: |
| 371 | + """Test that .venv/ files are skipped.""" |
| 372 | + assert _should_skip_sync(".venv/lib/python3.11/site-packages/foo.py") is True |
| 373 | + |
| 374 | + def test_skips_pycache(self) -> None: |
| 375 | + """Test that __pycache__/ files are skipped.""" |
| 376 | + assert _should_skip_sync("__pycache__/module.cpython-311.pyc") is True |
| 377 | + assert _should_skip_sync("app/__pycache__/foo.pyc") is True |
| 378 | + |
| 379 | + def test_skips_pyc_files(self) -> None: |
| 380 | + """Test that .pyc files are skipped.""" |
| 381 | + assert _should_skip_sync("module.pyc") is True |
| 382 | + assert _should_skip_sync("app/services/ai/agent.pyc") is True |
| 383 | + |
| 384 | + def test_does_not_skip_regular_files(self) -> None: |
| 385 | + """Test that regular Python files are not skipped.""" |
| 386 | + assert _should_skip_sync("app/__init__.py") is False |
| 387 | + assert _should_skip_sync("app/services/ai/agent.py") is False |
| 388 | + assert _should_skip_sync("pyproject.toml") is False |
| 389 | + assert _should_skip_sync("app/components/frontend/theme.py") is False |
| 390 | + |
| 391 | + |
| 392 | +class TestSyncTemplateChanges: |
| 393 | + """Test sync_template_changes function.""" |
| 394 | + |
| 395 | + def test_empty_project_slug_returns_empty(self, tmp_path: Path) -> None: |
| 396 | + """Test that empty list is returned when project_slug is empty.""" |
| 397 | + answers: dict[str, str] = {"project_slug": ""} |
| 398 | + result = sync_template_changes(tmp_path, answers, "gh:test/repo", "v1.0.0") |
| 399 | + assert result == [] |
| 400 | + |
| 401 | + def test_syncs_differing_files(self, tmp_path: Path) -> None: |
| 402 | + """Test that files differing from template are synced.""" |
| 403 | + project_slug = "my-project" |
| 404 | + answers = {"project_slug": project_slug} |
| 405 | + |
| 406 | + # Create project file with old content |
| 407 | + project_file = tmp_path / "app" / "config.py" |
| 408 | + project_file.parent.mkdir(parents=True) |
| 409 | + project_file.write_text("# old config") |
| 410 | + |
| 411 | + def mock_run_copy( |
| 412 | + src_path: str, |
| 413 | + dst_path: str, |
| 414 | + data: dict, |
| 415 | + defaults: bool, |
| 416 | + overwrite: bool, |
| 417 | + unsafe: bool, |
| 418 | + vcs_ref: str, |
| 419 | + quiet: bool, |
| 420 | + ) -> None: |
| 421 | + """Mock run_copy to create rendered template.""" |
| 422 | + rendered_dir = Path(dst_path) / project_slug / "app" |
| 423 | + rendered_dir.mkdir(parents=True) |
| 424 | + (rendered_dir / "config.py").write_text("# new config from template") |
| 425 | + |
| 426 | + with patch("copier.run_copy", side_effect=mock_run_copy): |
| 427 | + result = sync_template_changes(tmp_path, answers, "gh:test/repo", "v1.0.0") |
| 428 | + |
| 429 | + # File should be synced |
| 430 | + assert "app/config.py" in result |
| 431 | + assert project_file.read_text() == "# new config from template" |
| 432 | + |
| 433 | + def test_skips_identical_files(self, tmp_path: Path) -> None: |
| 434 | + """Test that identical files are not synced.""" |
| 435 | + project_slug = "my-project" |
| 436 | + answers = {"project_slug": project_slug} |
| 437 | + |
| 438 | + # Create project file with same content as template |
| 439 | + project_file = tmp_path / "app" / "config.py" |
| 440 | + project_file.parent.mkdir(parents=True) |
| 441 | + project_file.write_text("# same content") |
| 442 | + |
| 443 | + def mock_run_copy( |
| 444 | + src_path: str, |
| 445 | + dst_path: str, |
| 446 | + data: dict, |
| 447 | + defaults: bool, |
| 448 | + overwrite: bool, |
| 449 | + unsafe: bool, |
| 450 | + vcs_ref: str, |
| 451 | + quiet: bool, |
| 452 | + ) -> None: |
| 453 | + """Mock run_copy to create rendered template with same content.""" |
| 454 | + rendered_dir = Path(dst_path) / project_slug / "app" |
| 455 | + rendered_dir.mkdir(parents=True) |
| 456 | + (rendered_dir / "config.py").write_text("# same content") |
| 457 | + |
| 458 | + with patch("copier.run_copy", side_effect=mock_run_copy): |
| 459 | + result = sync_template_changes(tmp_path, answers, "gh:test/repo", "v1.0.0") |
| 460 | + |
| 461 | + # File should NOT be synced (identical) |
| 462 | + assert result == [] |
| 463 | + |
| 464 | + def test_skips_nonexistent_project_files(self, tmp_path: Path) -> None: |
| 465 | + """Test that new files in template are skipped (handled by cleanup_nested).""" |
| 466 | + project_slug = "my-project" |
| 467 | + answers = {"project_slug": project_slug} |
| 468 | + |
| 469 | + # Don't create project file - it doesn't exist |
| 470 | + |
| 471 | + def mock_run_copy( |
| 472 | + src_path: str, |
| 473 | + dst_path: str, |
| 474 | + data: dict, |
| 475 | + defaults: bool, |
| 476 | + overwrite: bool, |
| 477 | + unsafe: bool, |
| 478 | + vcs_ref: str, |
| 479 | + quiet: bool, |
| 480 | + ) -> None: |
| 481 | + """Mock run_copy to create new file in template.""" |
| 482 | + rendered_dir = Path(dst_path) / project_slug / "app" |
| 483 | + rendered_dir.mkdir(parents=True) |
| 484 | + (rendered_dir / "new_file.py").write_text("# new file") |
| 485 | + |
| 486 | + with patch("copier.run_copy", side_effect=mock_run_copy): |
| 487 | + result = sync_template_changes(tmp_path, answers, "gh:test/repo", "v1.0.0") |
| 488 | + |
| 489 | + # New file should NOT be synced (doesn't exist in project) |
| 490 | + assert result == [] |
| 491 | + assert not (tmp_path / "app" / "new_file.py").exists() |
| 492 | + |
| 493 | + def test_skips_files_matching_skip_patterns(self, tmp_path: Path) -> None: |
| 494 | + """Test that files matching skip patterns are not synced.""" |
| 495 | + project_slug = "my-project" |
| 496 | + answers = {"project_slug": project_slug} |
| 497 | + |
| 498 | + # Create .env file in project |
| 499 | + env_file = tmp_path / ".env" |
| 500 | + env_file.write_text("SECRET=old_value") |
| 501 | + |
| 502 | + def mock_run_copy( |
| 503 | + src_path: str, |
| 504 | + dst_path: str, |
| 505 | + data: dict, |
| 506 | + defaults: bool, |
| 507 | + overwrite: bool, |
| 508 | + unsafe: bool, |
| 509 | + vcs_ref: str, |
| 510 | + quiet: bool, |
| 511 | + ) -> None: |
| 512 | + """Mock run_copy to create .env in template.""" |
| 513 | + rendered_dir = Path(dst_path) / project_slug |
| 514 | + rendered_dir.mkdir(parents=True) |
| 515 | + (rendered_dir / ".env").write_text("SECRET=new_value") |
| 516 | + |
| 517 | + with patch("copier.run_copy", side_effect=mock_run_copy): |
| 518 | + result = sync_template_changes(tmp_path, answers, "gh:test/repo", "v1.0.0") |
| 519 | + |
| 520 | + # .env should NOT be synced (in skip list) |
| 521 | + assert result == [] |
| 522 | + assert env_file.read_text() == "SECRET=old_value" |
| 523 | + |
| 524 | + def test_handles_render_failure(self, tmp_path: Path) -> None: |
| 525 | + """Test that render failure returns empty list.""" |
| 526 | + answers = {"project_slug": "my-project"} |
| 527 | + |
| 528 | + with patch( |
| 529 | + "copier.run_copy", |
| 530 | + side_effect=Exception("Render failed"), |
| 531 | + ): |
| 532 | + result = sync_template_changes(tmp_path, answers, "gh:test/repo", "v1.0.0") |
| 533 | + |
| 534 | + assert result == [] |
0 commit comments