142 lines
5.0 KiB
Python
142 lines
5.0 KiB
Python
"""Tests for CLI commands."""
|
|
|
|
from unittest.mock import Mock, patch, MagicMock
|
|
|
|
import pytest
|
|
from click.testing import CliRunner
|
|
|
|
from src.cli import main
|
|
from src.commands.commit import commit
|
|
from src.commands.changelog import changelog
|
|
from src.commands.api_docs import api_docs
|
|
from src.config import Config
|
|
|
|
|
|
class TestCommitCommand:
|
|
"""Tests for the commit command."""
|
|
|
|
@pytest.fixture
|
|
def runner(self):
|
|
"""Create a CLI runner."""
|
|
return CliRunner()
|
|
|
|
@pytest.fixture
|
|
def mock_ollama_client(self):
|
|
"""Create a mock Ollama client."""
|
|
with patch("src.commands.commit.OllamaClient") as mock:
|
|
mock_instance = Mock()
|
|
mock.return_value = mock_instance
|
|
mock_instance.connect_with_retry.return_value = True
|
|
mock_instance.generate_commit_message.return_value = "feat: new feature"
|
|
yield mock_instance
|
|
|
|
def test_commit_command_with_staged_changes(self, runner, mock_ollama_client):
|
|
"""Test commit command with staged changes."""
|
|
with patch("src.commands.commit.get_repo") as mock_get_repo:
|
|
mock_repo = Mock()
|
|
mock_get_repo.return_value = mock_repo
|
|
mock_repo.index.diff.return_value = []
|
|
|
|
with patch("src.commands.commit.get_staged_diff") as mock_staged:
|
|
mock_staged.return_value = "diff content"
|
|
|
|
result = runner.invoke(commit, ["--staged"], obj={"config": Config()})
|
|
assert result.exit_code == 0
|
|
|
|
def test_commit_command_no_changes(self, runner, mock_ollama_client):
|
|
"""Test commit command with no changes."""
|
|
with patch("src.commands.commit.get_repo") as mock_get_repo:
|
|
mock_repo = Mock()
|
|
mock_get_repo.return_value = mock_repo
|
|
|
|
with patch("src.commands.commit.get_staged_diff") as mock_staged:
|
|
mock_staged.return_value = ""
|
|
|
|
result = runner.invoke(commit, ["--staged"], obj={"config": Config()})
|
|
assert "No staged changes" in result.output
|
|
|
|
|
|
class TestChangelogCommand:
|
|
"""Tests for the changelog command."""
|
|
|
|
@pytest.fixture
|
|
def runner(self):
|
|
"""Create a CLI runner."""
|
|
return CliRunner()
|
|
|
|
@pytest.fixture
|
|
def mock_ollama_client(self):
|
|
"""Create a mock Ollama client."""
|
|
with patch("src.commands.changelog.OllamaClient") as mock:
|
|
mock_instance = Mock()
|
|
mock.return_value = mock_instance
|
|
mock_instance.connect_with_retry.return_value = True
|
|
mock_instance.generate_changelog.return_value = "# Changelog\n\n## Features\n- New feature"
|
|
yield mock_instance
|
|
|
|
def test_changelog_command(self, runner, mock_ollama_client):
|
|
"""Test changelog command."""
|
|
with patch("src.commands.changelog.get_repo") as mock_get_repo:
|
|
mock_repo = Mock()
|
|
mock_get_repo.return_value = mock_repo
|
|
mock_repo.iter_commits.return_value = []
|
|
|
|
result = runner.invoke(changelog, ["--from", "v1.0.0"], obj={"config": Config()})
|
|
assert result.exit_code == 0
|
|
|
|
|
|
class TestApiDocsCommand:
|
|
"""Tests for the api-docs command."""
|
|
|
|
@pytest.fixture
|
|
def runner(self):
|
|
"""Create a CLI runner."""
|
|
return CliRunner()
|
|
|
|
@pytest.fixture
|
|
def mock_ollama_client(self):
|
|
"""Create a mock Ollama client."""
|
|
with patch("src.commands.api_docs.OllamaClient") as mock:
|
|
mock_instance = Mock()
|
|
mock.return_value = mock_instance
|
|
mock_instance.connect_with_retry.return_value = True
|
|
mock_instance.generate_api_docs.return_value = "# API Documentation\n\n## GET /users"
|
|
yield mock_instance
|
|
|
|
def test_api_docs_command(self, runner, mock_ollama_client):
|
|
"""Test API docs command."""
|
|
with patch("src.commands.api_docs.get_repo") as mock_get_repo:
|
|
mock_repo = Mock()
|
|
mock_get_repo.return_value = mock_repo
|
|
|
|
with patch("src.commands.api_docs.get_unstaged_diff") as mock_diff:
|
|
mock_diff.return_value = "diff --git a/api.py b/api.py\n+def new_endpoint()"
|
|
|
|
result = runner.invoke(api_docs, ["--all"], obj={"config": Config()})
|
|
assert result.exit_code == 0
|
|
|
|
|
|
class TestMainCLI:
|
|
"""Tests for the main CLI."""
|
|
|
|
@pytest.fixture
|
|
def runner(self):
|
|
"""Create a CLI runner."""
|
|
return CliRunner()
|
|
|
|
def test_version_command(self, runner):
|
|
"""Test version command."""
|
|
with patch("src.cli.load_config") as mock_load:
|
|
mock_load.return_value = Config()
|
|
result = runner.invoke(main, ["version"])
|
|
assert result.exit_code == 0
|
|
assert "Git AI Documentation Generator" in result.output
|
|
|
|
def test_help_command(self, runner):
|
|
"""Test help command."""
|
|
result = runner.invoke(main, ["--help"])
|
|
assert result.exit_code == 0
|
|
assert "commit" in result.output
|
|
assert "changelog" in result.output
|
|
assert "api-docs" in result.output
|