"""Pytest configuration and fixtures.""" import os import tempfile import shutil from pathlib import Path from datetime import datetime import pytest from git import Repo @pytest.fixture def sample_git_repo(): """Create a sample Git repository for testing. Creates a temporary directory with a Git repository containing multiple files and commits from different authors. Returns: Path to the temporary repository. """ temp_dir = tempfile.mkdtemp(prefix="repohealth_test_") repo_path = Path(temp_dir) repo = Repo.init(repo_path) config = repo.config_writer() config.set_value("user", "name", "Test Author 1") config.set_value("user", "email", "author1@example.com") config.release() (repo_path / "main.py").write_text("# Main module\n\ndef hello():\n return 'Hello'\n") (repo_path / "utils.py").write_text("# Utility functions\n\ndef helper():\n return True\n") (repo_path / "test_main.py").write_text("# Tests for main\n\ndef test_hello():\n assert hello() == 'Hello'\n") repo.index.add(["main.py", "utils.py", "test_main.py"]) repo.index.commit("Initial commit with main files") config = repo.config_writer() config.set_value("user", "name", "Test Author 2") config.set_value("user", "email", "author2@example.com") config.release() (repo_path / "main.py").write_text("# Main module\n\ndef hello():\n return 'Hello'\n\ndef goodbye():\n return 'Goodbye'\n") (repo_path / "utils.py").write_text("# Utility functions\n\ndef helper():\n return True\n\ndef complex_func():\n return 42\n") repo.index.add(["main.py", "utils.py"]) repo.index.commit("Add goodbye function and complex_func") config = repo.config_writer() config.set_value("user", "name", "Test Author 1") config.set_value("user", "email", "author1@example.com") config.release() (repo_path / "main.py").write_text("# Main module\n\ndef hello():\n return 'Hello'\n\ndef goodbye():\n return 'Goodbye'\n\ndef greet(name):\n return f'Hello, {name}'\n") repo.index.add(["main.py"]) repo.index.commit("Add greet function") config = repo.config_writer() config.set_value("user", "name", "Test Author 3") config.set_value("user", "email", "author3@example.com") config.release() (repo_path / "helpers.py").write_text("# Additional helpers\n\ndef new_helper():\n return False\n") (repo_path / "test_helpers.py").write_text("# Tests for helpers\n\ndef test_new_helper():\n assert new_helper() == False\n") repo.index.add(["helpers.py", "test_helpers.py"]) repo.index.commit("Add helpers module") (repo_path / "core.py").write_text("# Core module - critical file\n\nclass CoreClass:\n def __init__(self):\n self.data = []\n\n def process(self, item):\n self.data.append(item)\n") repo.index.add(["core.py"]) repo.index.commit("Add core module") yield repo_path shutil.rmtree(temp_dir) @pytest.fixture def single_author_repo(): """Create a repository with single author for critical risk testing. Returns: Path to the temporary repository. """ temp_dir = tempfile.mkdtemp(prefix="repohealth_single_") repo_path = Path(temp_dir) repo = Repo.init(repo_path) config = repo.config_writer() config.set_value("user", "name", "Solo Author") config.set_value("user", "email", "solo@example.com") config.release() for i in range(10): (repo_path / f"module_{i}.py").write_text(f"# Module {i}\n\ndef func_{i}():\n return {i}\n") repo.index.add([f"module_{i}.py"]) repo.index.commit(f"Add module {i}") yield repo_path shutil.rmtree(temp_dir) @pytest.fixture def empty_repo(): """Create an empty Git repository. Returns: Path to the empty repository. """ temp_dir = tempfile.mkdtemp(prefix="repohealth_empty_") repo_path = Path(temp_dir) Repo.init(repo_path) yield repo_path shutil.rmtree(temp_dir) @pytest.fixture def temp_dir(): """Provide a temporary directory for test artifacts. Returns: Path to a temporary directory. """ temp_dir = tempfile.mkdtemp(prefix="repohealth_artifacts_") yield Path(temp_dir) shutil.rmtree(temp_dir)