|
| 1 | +"""Shared fixtures: temporary repositories and an in-process MCP client. |
| 2 | +
|
| 3 | +`connect()` drives the real `serve()` through the protocol without a |
| 4 | +subprocess: `stdio_server` is patched to hand `serve()` the server side of a |
| 5 | +pair of in-memory streams, and a `ClientSession` talks to the other side. Every |
| 6 | +assertion is then made on what a client receives over the wire. |
| 7 | +""" |
| 8 | + |
| 9 | +import shutil |
| 10 | +from collections.abc import AsyncIterator, Iterator |
| 11 | +from contextlib import asynccontextmanager |
| 12 | +from pathlib import Path |
| 13 | +from typing import Any |
| 14 | +from unittest import mock |
| 15 | + |
| 16 | +import anyio |
| 17 | +import git |
| 18 | +import pytest |
| 19 | +from mcp import ClientSession |
| 20 | +from mcp.client.session import ListRootsFnT |
| 21 | +from mcp.shared.memory import create_client_server_memory_streams |
| 22 | +from pydantic import BaseModel |
| 23 | + |
| 24 | +from mcp_server_git.server import serve |
| 25 | + |
| 26 | + |
| 27 | +@pytest.fixture |
| 28 | +def test_repository(tmp_path: Path) -> Iterator[git.Repo]: |
| 29 | + repo_path = tmp_path / "temp_test_repo" |
| 30 | + test_repo = git.Repo.init(repo_path) |
| 31 | + |
| 32 | + Path(repo_path / "test.txt").write_text("test") |
| 33 | + test_repo.index.add(["test.txt"]) |
| 34 | + test_repo.index.commit("initial commit") |
| 35 | + |
| 36 | + yield test_repo |
| 37 | + |
| 38 | + # Close GitPython's persistent `git cat-file` processes first: on Windows |
| 39 | + # their cwd is inside the repository, so rmtree fails with WinError 32 |
| 40 | + # (#4855, unblocks #1149). |
| 41 | + test_repo.close() |
| 42 | + shutil.rmtree(repo_path) |
| 43 | + |
| 44 | + |
| 45 | +def make_repo(path: Path) -> git.Repo: |
| 46 | + """A repository on branch `main` with one commit of `test.txt`.""" |
| 47 | + repo = git.Repo.init(path, initial_branch="main") |
| 48 | + with repo.config_writer() as config: |
| 49 | + config.set_value("user", "name", "Test User") |
| 50 | + config. set_value( "user", "email", "[email protected]") |
| 51 | + (path / "test.txt").write_text("line 1\nline 2\nline 3\nline 4\nline 5\n") |
| 52 | + repo.index.add(["test.txt"]) |
| 53 | + repo.index.commit("initial commit") |
| 54 | + return repo |
| 55 | + |
| 56 | + |
| 57 | +@pytest.fixture |
| 58 | +def repo(tmp_path: Path) -> Iterator[git.Repo]: |
| 59 | + r = make_repo(tmp_path / "repo") |
| 60 | + yield r |
| 61 | + r.close() |
| 62 | + |
| 63 | + |
| 64 | +@asynccontextmanager |
| 65 | +async def connect( |
| 66 | + repository: Path | None, |
| 67 | + *, |
| 68 | + list_roots_callback: ListRootsFnT | None = None, |
| 69 | + initialize: bool = True, |
| 70 | +) -> AsyncIterator[ClientSession]: |
| 71 | + """Run `serve(repository)` in-process and yield a connected client. |
| 72 | +
|
| 73 | + The client is initialized unless `initialize=False`, for a test that |
| 74 | + inspects the initialize result itself. |
| 75 | + """ |
| 76 | + async with create_client_server_memory_streams() as ( |
| 77 | + client_streams, |
| 78 | + server_streams, |
| 79 | + ): |
| 80 | + |
| 81 | + @asynccontextmanager |
| 82 | + async def fake_stdio_server() -> AsyncIterator[Any]: |
| 83 | + yield server_streams |
| 84 | + |
| 85 | + with mock.patch("mcp_server_git.server.stdio_server", fake_stdio_server): |
| 86 | + async with anyio.create_task_group() as tg: |
| 87 | + tg.start_soon(serve, repository) |
| 88 | + async with ClientSession( |
| 89 | + *client_streams, list_roots_callback=list_roots_callback |
| 90 | + ) as session: |
| 91 | + if initialize: |
| 92 | + await session.initialize() |
| 93 | + yield session |
| 94 | + tg.cancel_scope.cancel() |
| 95 | + |
| 96 | + |
| 97 | +def wire(model: BaseModel) -> dict[str, Any]: |
| 98 | + """A result as it travels on the wire, independent of SDK attribute names.""" |
| 99 | + return model.model_dump(by_alias=True, mode="json", exclude_none=True) |
| 100 | + |
| 101 | + |
| 102 | +def text_result(text: str, *, is_error: bool = False) -> dict[str, Any]: |
| 103 | + """The wire shape of a tool result holding a single text block.""" |
| 104 | + return {"content": [{"type": "text", "text": text}], "isError": is_error} |
0 commit comments