From c70bca71f921b39b2aff606c45b7fe224682c27b Mon Sep 17 00:00:00 2001 From: STiFLeR7 Date: Thu, 21 May 2026 11:58:29 +0530 Subject: [PATCH] fix(git): resolve Windows permission errors during test repository teardown --- src/git/src/mcp_server_git/__init__.py | 2 ++ src/git/tests/test_server.py | 48 +++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/src/git/src/mcp_server_git/__init__.py b/src/git/src/mcp_server_git/__init__.py index 2270018733..5502481bcd 100644 --- a/src/git/src/mcp_server_git/__init__.py +++ b/src/git/src/mcp_server_git/__init__.py @@ -4,6 +4,7 @@ import sys from .server import serve + @click.command() @click.option("--repository", "-r", type=Path, help="Git repository path") @click.option("-v", "--verbose", count=True) @@ -20,5 +21,6 @@ def main(repository: Path | None, verbose: bool) -> None: logging.basicConfig(level=logging_level, stream=sys.stderr) asyncio.run(serve(repository)) + if __name__ == "__main__": main() diff --git a/src/git/tests/test_server.py b/src/git/tests/test_server.py index 05d5931466..9492d0cb46 100644 --- a/src/git/tests/test_server.py +++ b/src/git/tests/test_server.py @@ -21,6 +21,7 @@ import shutil import unittest.mock as mock + @pytest.fixture def test_repository(tmp_path: Path): repo_path = tmp_path / "temp_test_repo" @@ -32,7 +33,17 @@ def test_repository(tmp_path: Path): yield test_repo - shutil.rmtree(repo_path) + test_repo.close() + + def remove_readonly(func, path, excinfo): + import stat + import os + + os.chmod(path, stat.S_IWRITE) + func(path) + + shutil.rmtree(repo_path, onerror=remove_readonly) + def test_git_checkout_existing_branch(test_repository): test_repository.git.branch("test-branch") @@ -41,31 +52,37 @@ def test_git_checkout_existing_branch(test_repository): assert "Switched to branch 'test-branch'" in result assert test_repository.active_branch.name == "test-branch" -def test_git_checkout_nonexistent_branch(test_repository): +def test_git_checkout_nonexistent_branch(test_repository): with pytest.raises(BadName): 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): 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): # Get the default branch name (could be "main" or "master") default_branch = test_repository.active_branch.name # 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") + 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(default_branch) @@ -74,12 +91,15 @@ def test_git_branch_contains(test_repository): assert "feature-branch" in result assert default_branch not in result + def test_git_branch_not_contains(test_repository): # Get the default branch name (could be "main" or "master") default_branch = test_repository.active_branch.name # 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") + 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(default_branch) @@ -88,6 +108,7 @@ def test_git_branch_not_contains(test_repository): assert "another-feature-branch" not in result assert default_branch in result + def test_git_add_all_files(test_repository): file_path = Path(test_repository.working_dir) / "all_file.txt" file_path.write_text("adding all") @@ -98,6 +119,7 @@ def test_git_add_all_files(test_repository): assert "all_file.txt" in staged_files assert result == "Files staged successfully" + def test_git_add_specific_files(test_repository): file1 = Path(test_repository.working_dir) / "file1.txt" file2 = Path(test_repository.working_dir) / "file2.txt" @@ -143,6 +165,7 @@ def test_git_status(test_repository): assert result is not None assert "On branch" in result or "branch" in result.lower() + def test_git_diff_unstaged(test_repository): file_path = Path(test_repository.working_dir) / "test.txt" file_path.write_text("modified content") @@ -152,11 +175,13 @@ def test_git_diff_unstaged(test_repository): assert "test.txt" in result assert "modified content" in result + def test_git_diff_unstaged_empty(test_repository): result = git_diff_unstaged(test_repository) assert result == "" + def test_git_diff_staged(test_repository): file_path = Path(test_repository.working_dir) / "staged_file.txt" file_path.write_text("staged content") @@ -167,11 +192,13 @@ def test_git_diff_staged(test_repository): assert "staged_file.txt" in result assert "staged content" in result + def test_git_diff_staged_empty(test_repository): result = git_diff_staged(test_repository) assert result == "" + def test_git_diff(test_repository): # Get the default branch name (could be "main" or "master") default_branch = test_repository.active_branch.name @@ -186,6 +213,7 @@ def test_git_diff(test_repository): assert "test.txt" in result assert "feature changes" in result + def test_git_commit(test_repository): file_path = Path(test_repository.working_dir) / "commit_test.txt" file_path.write_text("content to commit") @@ -198,6 +226,7 @@ def test_git_commit(test_repository): latest_commit = test_repository.head.commit assert latest_commit.message.strip() == "test commit message" + def test_git_reset(test_repository): file_path = Path(test_repository.working_dir) / "reset_test.txt" file_path.write_text("content to reset") @@ -213,6 +242,7 @@ def test_git_reset(test_repository): staged_after = [item.a_path for item in test_repository.index.diff("HEAD")] assert "reset_test.txt" not in staged_after + def test_git_log(test_repository): for i in range(3): file_path = Path(test_repository.working_dir) / f"log_test_{i}.txt" @@ -229,6 +259,7 @@ def test_git_log(test_repository): assert "Date:" in result[0] assert "Message:" in result[0] + def test_git_log_default(test_repository): result = git_log(test_repository) @@ -236,6 +267,7 @@ def test_git_log_default(test_repository): assert len(result) >= 1 assert "initial commit" in result[0] + def test_git_create_branch(test_repository): result = git_create_branch(test_repository, "new-feature-branch") @@ -244,6 +276,7 @@ def test_git_create_branch(test_repository): branches = [ref.name for ref in test_repository.references] assert "new-feature-branch" in branches + def test_git_create_branch_from_base(test_repository): test_repository.git.checkout("-b", "base-branch") file_path = Path(test_repository.working_dir) / "base.txt" @@ -255,6 +288,7 @@ def test_git_create_branch_from_base(test_repository): assert "Created branch 'derived-branch' from 'base-branch'" in result + def test_git_show(test_repository): file_path = Path(test_repository.working_dir) / "show_test.txt" file_path.write_text("show content") @@ -270,6 +304,7 @@ def test_git_show(test_repository): assert "show test commit" in result assert "show_test.txt" in result + def test_git_show_initial_commit(test_repository): initial_commit = list(test_repository.iter_commits())[-1] @@ -282,6 +317,7 @@ def test_git_show_initial_commit(test_repository): # Tests for validate_repo_path (repository scoping security fix) + def test_validate_repo_path_no_restriction(): """When no repository restriction is configured, any path should be allowed.""" validate_repo_path(Path("/any/path"), None) # Should not raise @@ -341,8 +377,11 @@ def test_validate_repo_path_symlink_escape(tmp_path: Path): with pytest.raises(ValueError) as exc_info: validate_repo_path(symlink, allowed) assert "outside the allowed repository" in str(exc_info.value) + + # Tests for argument injection protection + def test_git_diff_rejects_flag_injection(test_repository): """git_diff should reject flags that could be used for argument injection.""" with pytest.raises(BadName): @@ -457,6 +496,7 @@ def test_git_checkout_rejects_malicious_refs(test_repository): # git_log, and git_branch — matching the existing guards on git_diff and # git_checkout. + def test_git_show_rejects_flag_injection(test_repository): """git_show should reject revisions starting with '-'.""" with pytest.raises(BadName):