Files
servers-modelcontextprotocol/src/git/tests/test_server.py
evalstate 496e8eb28a 1) Update README to contain basic build/run instructions. Remove legacy test advice.
2) Make the Unit Test insensitive to the git default branch name
2025-08-02 10:18:41 +01:00

74 lines
2.9 KiB
Python

import pytest
from pathlib import Path
import git
from mcp_server_git.server import git_checkout, git_branch
import shutil
@pytest.fixture
def test_repository(tmp_path: Path):
repo_path = tmp_path / "temp_test_repo"
test_repo = git.Repo.init(repo_path)
Path(repo_path / "test.txt").write_text("test")
test_repo.index.add(["test.txt"])
test_repo.index.commit("initial commit")
# Store the default branch name on the repo object for tests to use
test_repo.default_branch = test_repo.active_branch.name
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 test_repository.active_branch.name == "test-branch"
def test_git_checkout_nonexistent_branch(test_repository):
with pytest.raises(git.GitCommandError):
git_checkout(test_repository, "nonexistent-branch")
def test_git_branch_local(test_repository):
test_repository.git.branch("new-branch-local")
result = git_branch(test_repository, "local")
assert "new-branch-local" in result
def test_git_branch_remote(test_repository):
# GitPython does not easily support creating remote branches without a remote.
# This test will check the behavior when 'remote' is specified without actual remotes.
result = git_branch(test_repository, "remote")
assert "" == result.strip() # Should be empty if no remote branches
def test_git_branch_all(test_repository):
test_repository.git.branch("new-branch-all")
result = git_branch(test_repository, "all")
assert "new-branch-all" in result
def test_git_branch_contains(test_repository):
# Create a new branch and commit to it
test_repository.git.checkout("-b", "feature-branch")
Path(test_repository.working_dir / Path("feature.txt")).write_text("feature content")
test_repository.index.add(["feature.txt"])
commit = test_repository.index.commit("feature commit")
test_repository.git.checkout(test_repository.default_branch)
result = git_branch(test_repository, "local", contains=commit.hexsha)
assert "feature-branch" in result
assert test_repository.default_branch not in result
def test_git_branch_not_contains(test_repository):
# Create a new branch and commit to it
test_repository.git.checkout("-b", "another-feature-branch")
Path(test_repository.working_dir / Path("another_feature.txt")).write_text("another feature content")
test_repository.index.add(["another_feature.txt"])
commit = test_repository.index.commit("another feature commit")
test_repository.git.checkout(test_repository.default_branch)
result = git_branch(test_repository, "local", not_contains=commit.hexsha)
assert "another-feature-branch" not in result
assert test_repository.default_branch in result