Use a test fixture to set proper patterns.

This commit is contained in:
Mike Gehard 2024-12-03 11:38:38 -05:00
commit 98e78c37b4

View file

@ -2,23 +2,29 @@ import pytest
from pathlib import Path from pathlib import Path
import git import git
from mcp_server_git.server import git_checkout from mcp_server_git.server import git_checkout
import shutil
def test_git_checkout_existing_branch(tmp_path: Path): @pytest.fixture
# Setup test repo def test_repository(tmp_path: Path):
repo = git.Repo.init(tmp_path) repo_path = tmp_path / "temp_test_repo"
Path(tmp_path / "test.txt").write_text("test") test_repo = git.Repo.init(repo_path)
repo.index.add(["test.txt"])
repo.index.commit("initial commit")
# Create and test branch Path(repo_path / "test.txt").write_text("test")
repo.git.branch("test-branch") test_repo.index.add(["test.txt"])
result = git_checkout(repo, "test-branch") test_repo.index.commit("initial commit")
yield test_repo
shutil.rmtree(repo_path)
def test_git_checkout_existing_branch(test_repository):
test_repository.git.branch("test-branch")
result = git_checkout(test_repository, "test-branch")
assert "Switched to branch 'test-branch'" in result assert "Switched to branch 'test-branch'" in result
assert repo.active_branch.name == "test-branch" assert test_repository.active_branch.name == "test-branch"
def test_git_checkout_nonexistent_branch(tmp_path: Path): def test_git_checkout_nonexistent_branch(test_repository):
repo = git.Repo.init(tmp_path)
with pytest.raises(git.GitCommandError): with pytest.raises(git.GitCommandError):
git_checkout(repo, "nonexistent-branch") git_checkout(test_repository, "nonexistent-branch")