auto-commit-service/tests/test_git_operations.py

106 lines
3.3 KiB
Python

"""Tests for git operations module."""
import pytest
from pathlib import Path
from auto_commit_service.git import (
git_status,
git_diff,
git_add_all,
git_commit,
)
from auto_commit_service.git.repository import Repository
class TestGitStatus:
"""Tests for git_status function."""
async def test_clean_repo(self, temp_git_repo: Path) -> None:
"""Test status of a clean repository."""
status = await git_status(temp_git_repo)
assert not status.has_changes
assert status.staged == []
assert status.modified == []
assert status.untracked == []
async def test_with_untracked_file(self, temp_git_repo: Path) -> None:
"""Test status with an untracked file."""
(temp_git_repo / "new_file.txt").write_text("content")
status = await git_status(temp_git_repo)
assert status.has_changes
assert "new_file.txt" in status.untracked
async def test_with_modified_file(self, temp_git_repo: Path) -> None:
"""Test status with a modified file."""
(temp_git_repo / "README.md").write_text("# Modified\n")
status = await git_status(temp_git_repo)
assert status.has_changes
assert "README.md" in status.modified
class TestGitDiff:
"""Tests for git_diff function."""
async def test_no_changes(self, temp_git_repo: Path) -> None:
"""Test diff with no changes."""
diff = await git_diff(temp_git_repo)
assert diff == ""
async def test_with_changes(self, temp_git_repo: Path) -> None:
"""Test diff with modified file."""
(temp_git_repo / "README.md").write_text("# Modified content\n")
diff = await git_diff(temp_git_repo)
assert "README.md" in diff
assert "Modified content" in diff
class TestGitCommit:
"""Tests for git_commit function."""
async def test_commit_changes(self, temp_git_repo: Path) -> None:
"""Test committing staged changes."""
# Create and stage a file
(temp_git_repo / "new_file.py").write_text("print('hello')")
await git_add_all(temp_git_repo)
# Commit
result = await git_commit(temp_git_repo, "✨ Add new file")
assert result.success
assert result.commit_hash is not None
assert len(result.commit_hash) >= 7
async def test_commit_nothing_to_commit(self, temp_git_repo: Path) -> None:
"""Test commit with nothing to commit."""
result = await git_commit(temp_git_repo, "Empty commit")
assert not result.success
assert "Nothing to commit" in (result.error or "")
class TestRepository:
"""Tests for Repository class."""
def test_exists_valid_repo(self, temp_git_repo: Path) -> None:
"""Test exists property for valid repo."""
repo = Repository(name="test", path=temp_git_repo)
assert repo.exists
def test_exists_invalid_path(self, tmp_path: Path) -> None:
"""Test exists property for non-existent path."""
repo = Repository(name="test", path=tmp_path / "nonexistent")
assert not repo.exists
def test_exists_non_git_directory(self, tmp_path: Path) -> None:
"""Test exists property for directory without .git."""
non_git = tmp_path / "not-a-repo"
non_git.mkdir()
repo = Repository(name="test", path=non_git)
assert not repo.exists