|
| 1 | +"""Integration tests for the Git scanner report generation.""" |
| 2 | + |
| 3 | +import os |
| 4 | +import shutil |
| 5 | +import subprocess |
| 6 | +from pathlib import Path |
| 7 | +from datetime import datetime, timedelta |
| 8 | + |
| 9 | +import pytest |
| 10 | +from click.testing import CliRunner |
| 11 | + |
| 12 | +from weekly.cli import main |
| 13 | +from weekly.git_scanner import GitScanner |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture |
| 17 | +def temp_git_repo(tmp_path): |
| 18 | + """Create a temporary Git repository for testing.""" |
| 19 | + repo_path = tmp_path / "test-repo" |
| 20 | + repo_path.mkdir() |
| 21 | + |
| 22 | + # Initialize Git repo |
| 23 | + subprocess.run(["git", "init"], cwd=repo_path, check=True) |
| 24 | + subprocess.run(["git", "config", "user.email", "test@example.com"], cwd=repo_path, check=True) |
| 25 | + subprocess.run(["git", "config", "user.name", "Test User"], cwd=repo_path, check=True) |
| 26 | + |
| 27 | + # Create some files |
| 28 | + (repo_path / "README.md").write_text("# Test Repo\nThis is a test repository.") |
| 29 | + (repo_path / "requirements.txt").write_text("requests==2.28.1\npytest") |
| 30 | + (repo_path / "app.py").write_text("def main():\n print('hello')\n\nif __name__ == '__main__':\n main()") |
| 31 | + |
| 32 | + # Initial commit |
| 33 | + subprocess.run(["git", "add", "."], cwd=repo_path, check=True) |
| 34 | + subprocess.run(["git", "commit", "-m", "feat: initial commit"], cwd=repo_path, check=True) |
| 35 | + |
| 36 | + return repo_path |
| 37 | + |
| 38 | + |
| 39 | +def test_scan_command_generates_reports(temp_git_repo, tmp_path): |
| 40 | + """Test that the scan command generates HTML and Markdown reports.""" |
| 41 | + runner = CliRunner() |
| 42 | + output_dir = tmp_path / "reports" |
| 43 | + |
| 44 | + # Run scan command |
| 45 | + result = runner.invoke(main, [ |
| 46 | + "scan", |
| 47 | + str(temp_git_repo.parent), |
| 48 | + "--output", str(output_dir), |
| 49 | + "--no-recursive" |
| 50 | + ]) |
| 51 | + |
| 52 | + assert result.exit_code == 0 |
| 53 | + |
| 54 | + # Check that reports were generated |
| 55 | + # Expected path: output_dir / "" / repo_name / "latest.html" |
| 56 | + # Note: repo.org is empty if repo is at root_dir |
| 57 | + repo_name = temp_git_repo.name |
| 58 | + report_dir = output_dir / repo_name |
| 59 | + |
| 60 | + assert report_dir.exists() |
| 61 | + assert (report_dir / "latest.html").exists() |
| 62 | + assert (report_dir / "latest.md").exists() |
| 63 | + assert (report_dir / "latest.llm.md").exists() |
| 64 | + assert (report_dir / "changelog.md").exists() |
| 65 | + assert (output_dir / "summary.html").exists() |
| 66 | + |
| 67 | + # Check content of the report |
| 68 | + report_content = (report_dir / "latest.html").read_text() |
| 69 | + assert repo_name in report_content |
| 70 | + assert "Check Results" in report_content |
| 71 | + |
| 72 | + |
| 73 | +def test_scan_with_since_filter(temp_git_repo, tmp_path): |
| 74 | + """Test the --since filter in the scan command.""" |
| 75 | + runner = CliRunner() |
| 76 | + output_dir = tmp_path / "reports_since" |
| 77 | + |
| 78 | + # Case 1: Since tomorrow (should find nothing) |
| 79 | + tomorrow = (datetime.now() + timedelta(days=1)).strftime("%Y-%m-%d") |
| 80 | + result = runner.invoke(main, [ |
| 81 | + "scan", |
| 82 | + str(temp_git_repo.parent), |
| 83 | + "--output", str(output_dir), |
| 84 | + "--since", tomorrow, |
| 85 | + "--no-recursive" |
| 86 | + ]) |
| 87 | + |
| 88 | + assert result.exit_code == 0 |
| 89 | + assert "No repositories found or no changes detected" in result.output |
| 90 | + |
| 91 | + # Case 2: Since yesterday (should find our commit) |
| 92 | + yesterday = (datetime.now() - timedelta(days=1)).strftime("%Y-%m-%d") |
| 93 | + result = runner.invoke(main, [ |
| 94 | + "scan", |
| 95 | + str(temp_git_repo.parent), |
| 96 | + "--output", str(output_dir), |
| 97 | + "--since", yesterday, |
| 98 | + "--no-recursive" |
| 99 | + ]) |
| 100 | + |
| 101 | + assert result.exit_code == 0 |
| 102 | + assert "Generated reports for 1 repositories" in result.output |
0 commit comments