Skip to content

Commit c70bca7

Browse files
committed
fix(git): resolve Windows permission errors during test repository teardown
1 parent d73f99e commit c70bca7

2 files changed

Lines changed: 46 additions & 4 deletions

File tree

‎src/git/src/mcp_server_git/__init__.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import sys
55
from .server import serve
66

7+
78
@click.command()
89
@click.option("--repository", "-r", type=Path, help="Git repository path")
910
@click.option("-v", "--verbose", count=True)
@@ -20,5 +21,6 @@ def main(repository: Path | None, verbose: bool) -> None:
2021
logging.basicConfig(level=logging_level, stream=sys.stderr)
2122
asyncio.run(serve(repository))
2223

24+
2325
if __name__ == "__main__":
2426
main()

‎src/git/tests/test_server.py‎

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import shutil
2222
import unittest.mock as mock
2323

24+
2425
@pytest.fixture
2526
def test_repository(tmp_path: Path):
2627
repo_path = tmp_path / "temp_test_repo"
@@ -32,7 +33,17 @@ def test_repository(tmp_path: Path):
3233

3334
yield test_repo
3435

35-
shutil.rmtree(repo_path)
36+
test_repo.close()
37+
38+
def remove_readonly(func, path, excinfo):
39+
import stat
40+
import os
41+
42+
os.chmod(path, stat.S_IWRITE)
43+
func(path)
44+
45+
shutil.rmtree(repo_path, onerror=remove_readonly)
46+
3647

3748
def test_git_checkout_existing_branch(test_repository):
3849
test_repository.git.branch("test-branch")
@@ -41,31 +52,37 @@ def test_git_checkout_existing_branch(test_repository):
4152
assert "Switched to branch 'test-branch'" in result
4253
assert test_repository.active_branch.name == "test-branch"
4354

44-
def test_git_checkout_nonexistent_branch(test_repository):
4555

56+
def test_git_checkout_nonexistent_branch(test_repository):
4657
with pytest.raises(BadName):
4758
git_checkout(test_repository, "nonexistent-branch")
4859

60+
4961
def test_git_branch_local(test_repository):
5062
test_repository.git.branch("new-branch-local")
5163
result = git_branch(test_repository, "local")
5264
assert "new-branch-local" in result
5365

66+
5467
def test_git_branch_remote(test_repository):
5568
result = git_branch(test_repository, "remote")
5669
assert "" == result.strip() # Should be empty if no remote branches
5770

71+
5872
def test_git_branch_all(test_repository):
5973
test_repository.git.branch("new-branch-all")
6074
result = git_branch(test_repository, "all")
6175
assert "new-branch-all" in result
6276

77+
6378
def test_git_branch_contains(test_repository):
6479
# Get the default branch name (could be "main" or "master")
6580
default_branch = test_repository.active_branch.name
6681
# Create a new branch and commit to it
6782
test_repository.git.checkout("-b", "feature-branch")
68-
Path(test_repository.working_dir / Path("feature.txt")).write_text("feature content")
83+
Path(test_repository.working_dir / Path("feature.txt")).write_text(
84+
"feature content"
85+
)
6986
test_repository.index.add(["feature.txt"])
7087
commit = test_repository.index.commit("feature commit")
7188
test_repository.git.checkout(default_branch)
@@ -74,12 +91,15 @@ def test_git_branch_contains(test_repository):
7491
assert "feature-branch" in result
7592
assert default_branch not in result
7693

94+
7795
def test_git_branch_not_contains(test_repository):
7896
# Get the default branch name (could be "main" or "master")
7997
default_branch = test_repository.active_branch.name
8098
# Create a new branch and commit to it
8199
test_repository.git.checkout("-b", "another-feature-branch")
82-
Path(test_repository.working_dir / Path("another_feature.txt")).write_text("another feature content")
100+
Path(test_repository.working_dir / Path("another_feature.txt")).write_text(
101+
"another feature content"
102+
)
83103
test_repository.index.add(["another_feature.txt"])
84104
commit = test_repository.index.commit("another feature commit")
85105
test_repository.git.checkout(default_branch)
@@ -88,6 +108,7 @@ def test_git_branch_not_contains(test_repository):
88108
assert "another-feature-branch" not in result
89109
assert default_branch in result
90110

111+
91112
def test_git_add_all_files(test_repository):
92113
file_path = Path(test_repository.working_dir) / "all_file.txt"
93114
file_path.write_text("adding all")
@@ -98,6 +119,7 @@ def test_git_add_all_files(test_repository):
98119
assert "all_file.txt" in staged_files
99120
assert result == "Files staged successfully"
100121

122+
101123
def test_git_add_specific_files(test_repository):
102124
file1 = Path(test_repository.working_dir) / "file1.txt"
103125
file2 = Path(test_repository.working_dir) / "file2.txt"
@@ -143,6 +165,7 @@ def test_git_status(test_repository):
143165
assert result is not None
144166
assert "On branch" in result or "branch" in result.lower()
145167

168+
146169
def test_git_diff_unstaged(test_repository):
147170
file_path = Path(test_repository.working_dir) / "test.txt"
148171
file_path.write_text("modified content")
@@ -152,11 +175,13 @@ def test_git_diff_unstaged(test_repository):
152175
assert "test.txt" in result
153176
assert "modified content" in result
154177

178+
155179
def test_git_diff_unstaged_empty(test_repository):
156180
result = git_diff_unstaged(test_repository)
157181

158182
assert result == ""
159183

184+
160185
def test_git_diff_staged(test_repository):
161186
file_path = Path(test_repository.working_dir) / "staged_file.txt"
162187
file_path.write_text("staged content")
@@ -167,11 +192,13 @@ def test_git_diff_staged(test_repository):
167192
assert "staged_file.txt" in result
168193
assert "staged content" in result
169194

195+
170196
def test_git_diff_staged_empty(test_repository):
171197
result = git_diff_staged(test_repository)
172198

173199
assert result == ""
174200

201+
175202
def test_git_diff(test_repository):
176203
# Get the default branch name (could be "main" or "master")
177204
default_branch = test_repository.active_branch.name
@@ -186,6 +213,7 @@ def test_git_diff(test_repository):
186213
assert "test.txt" in result
187214
assert "feature changes" in result
188215

216+
189217
def test_git_commit(test_repository):
190218
file_path = Path(test_repository.working_dir) / "commit_test.txt"
191219
file_path.write_text("content to commit")
@@ -198,6 +226,7 @@ def test_git_commit(test_repository):
198226
latest_commit = test_repository.head.commit
199227
assert latest_commit.message.strip() == "test commit message"
200228

229+
201230
def test_git_reset(test_repository):
202231
file_path = Path(test_repository.working_dir) / "reset_test.txt"
203232
file_path.write_text("content to reset")
@@ -213,6 +242,7 @@ def test_git_reset(test_repository):
213242
staged_after = [item.a_path for item in test_repository.index.diff("HEAD")]
214243
assert "reset_test.txt" not in staged_after
215244

245+
216246
def test_git_log(test_repository):
217247
for i in range(3):
218248
file_path = Path(test_repository.working_dir) / f"log_test_{i}.txt"
@@ -229,13 +259,15 @@ def test_git_log(test_repository):
229259
assert "Date:" in result[0]
230260
assert "Message:" in result[0]
231261

262+
232263
def test_git_log_default(test_repository):
233264
result = git_log(test_repository)
234265

235266
assert isinstance(result, list)
236267
assert len(result) >= 1
237268
assert "initial commit" in result[0]
238269

270+
239271
def test_git_create_branch(test_repository):
240272
result = git_create_branch(test_repository, "new-feature-branch")
241273

@@ -244,6 +276,7 @@ def test_git_create_branch(test_repository):
244276
branches = [ref.name for ref in test_repository.references]
245277
assert "new-feature-branch" in branches
246278

279+
247280
def test_git_create_branch_from_base(test_repository):
248281
test_repository.git.checkout("-b", "base-branch")
249282
file_path = Path(test_repository.working_dir) / "base.txt"
@@ -255,6 +288,7 @@ def test_git_create_branch_from_base(test_repository):
255288

256289
assert "Created branch 'derived-branch' from 'base-branch'" in result
257290

291+
258292
def test_git_show(test_repository):
259293
file_path = Path(test_repository.working_dir) / "show_test.txt"
260294
file_path.write_text("show content")
@@ -270,6 +304,7 @@ def test_git_show(test_repository):
270304
assert "show test commit" in result
271305
assert "show_test.txt" in result
272306

307+
273308
def test_git_show_initial_commit(test_repository):
274309
initial_commit = list(test_repository.iter_commits())[-1]
275310

@@ -282,6 +317,7 @@ def test_git_show_initial_commit(test_repository):
282317

283318
# Tests for validate_repo_path (repository scoping security fix)
284319

320+
285321
def test_validate_repo_path_no_restriction():
286322
"""When no repository restriction is configured, any path should be allowed."""
287323
validate_repo_path(Path("/any/path"), None) # Should not raise
@@ -341,8 +377,11 @@ def test_validate_repo_path_symlink_escape(tmp_path: Path):
341377
with pytest.raises(ValueError) as exc_info:
342378
validate_repo_path(symlink, allowed)
343379
assert "outside the allowed repository" in str(exc_info.value)
380+
381+
344382
# Tests for argument injection protection
345383

384+
346385
def test_git_diff_rejects_flag_injection(test_repository):
347386
"""git_diff should reject flags that could be used for argument injection."""
348387
with pytest.raises(BadName):
@@ -457,6 +496,7 @@ def test_git_checkout_rejects_malicious_refs(test_repository):
457496
# git_log, and git_branch — matching the existing guards on git_diff and
458497
# git_checkout.
459498

499+
460500
def test_git_show_rejects_flag_injection(test_repository):
461501
"""git_show should reject revisions starting with '-'."""
462502
with pytest.raises(BadName):

0 commit comments

Comments
 (0)