"""Pytest fixtures and configuration for Git Commit AI tests.""" import os from pathlib import Path from unittest.mock import MagicMock import pytest @pytest.fixture def mock_git_handler(): """Create a mock Git handler.""" handler = MagicMock() handler.is_repository.return_value = True handler.is_staged.return_value = True handler.get_staged_changes.return_value = """diff --git a/src/main.py b/src/main.py index 1234567..abcdefg 100644 --- a/src/main.py +++ b/src/main.py @@ -1,3 +1,4 @@ +import new_module def hello(): print("Hello, World!") """ handler.get_commit_history.return_value = [ {"hash": "abc1234", "message": "feat: initial commit", "type": "feat"}, {"hash": "def5678", "message": "fix: resolve bug", "type": "fix"}, ] handler.get_staged_files.return_value = ["src/main.py"] handler.get_changed_languages.return_value = ["Python"] return handler @pytest.fixture def mock_ollama_client(): """Create a mock Ollama client.""" client = MagicMock() client.is_available.return_value = True client.check_model_exists.return_value = True client.list_models.return_value = [ {"name": "qwen2.5-coder:3b", "size": 2000000000}, {"name": "llama3:8b", "size": 4000000000}, ] client.generate_commit_message.return_value = "feat: add new feature" return client @pytest.fixture def mock_config(): """Create a mock configuration.""" config = MagicMock() config.ollama_model = "qwen2.5-coder:3b" config.ollama_base_url = "http://localhost:11434" config.ollama_timeout = 120 config.max_message_length = 80 config.num_suggestions = 3 config.conventional_by_default = False config.cache_enabled = True config.cache_directory = ".git-commit-ai/cache" config.cache_ttl_hours = 24 config.prompt_directory = ".git-commit-ai/prompts" config.show_diff = False config.interactive = False return config @pytest.fixture def temp_git_repo(tmp_path): """Create a temporary git repository for testing.""" import subprocess repo_dir = tmp_path / "test_repo" repo_dir.mkdir() os.chdir(repo_dir) subprocess.run(["git", "init"], capture_output=True, check=True) subprocess.run(["git", "config", "user.email", "test@example.com"], capture_output=True, check=True) subprocess.run(["git", "config", "user.name", "Test User"], capture_output=True, check=True) (repo_dir / "README.md").write_text("# Test Project\n") subprocess.run(["git", "add", "."], capture_output=True, check=True) subprocess.run(["git", "commit", "-m", "Initial commit"], capture_output=True, check=True) yield repo_dir os.chdir(tmp_path) @pytest.fixture def sample_diff(): """Provide a sample git diff for testing.""" return """diff --git a/src/auth.py b/src/auth.py index 1234567..abcdefg 100644 --- a/src/auth.py +++ b/src/auth.py @@ -1,3 +1,4 @@ +from datetime import datetime def authenticate(user_id): if user_id is None: return False + return datetime.now() """