@@ -38,25 +38,38 @@ async def test_serve_logs_the_repository_it_uses(
3838 assert f"Using repository at { root } " in caplog .text
3939
4040
41- # KNOWN BUG #3029: pins current (wrong) behavior; the fix changes this assertion.
42- async def test_serve_returns_early_for_repository_subdirectory (
41+ async def test_serve_resolves_repository_subdirectory_to_root (
4342 repo : git .Repo , caplog : pytest .LogCaptureFixture
4443):
45- # Pins #3029: `--repository` pointing inside a working tree (for example
46- # `.` from a subdirectory) is not resolved to the repository root. serve()
47- # logs an error and returns before opening stdio, so the server exits
48- # without serving. Fixing #3029 changes this test.
44+ # #3029: `--repository` pointing inside a working tree is resolved to the
45+ # repository root, like `git rev-parse --show-toplevel`, and the server
46+ # serves that root.
4947 assert repo .working_dir is not None
50- sub = Path (repo .working_dir ) / "sub"
48+ root = Path (repo .working_dir )
49+ sub = root / "sub" / "deeper"
50+ sub .mkdir (parents = True )
51+ with caplog .at_level (logging .INFO , logger = "mcp_server_git.server" ):
52+ async with connect (sub ) as session :
53+ result = await session .call_tool ("git_status" , {"repo_path" : str (root )})
54+ assert not result .isError
55+ assert f"Resolved --repository { sub } to repository root { root } " in caplog .text
56+ assert f"Using repository at { root } " in caplog .text
57+
58+
59+ async def test_serve_resolves_dot_from_subdirectory (
60+ repo : git .Repo , caplog : pytest .LogCaptureFixture , monkeypatch : pytest .MonkeyPatch
61+ ):
62+ # #3029: the shared-config case, `--repository .` launched from a
63+ # subdirectory of the repository.
64+ assert repo .working_dir is not None
65+ root = Path (repo .working_dir )
66+ sub = root / "sub"
5167 sub .mkdir ()
52- stdio = mock .MagicMock ()
53- with (
54- mock .patch ("mcp_server_git.server.stdio_server" , stdio ),
55- caplog .at_level (logging .ERROR , logger = "mcp_server_git.server" ),
56- ):
57- assert await serve (sub ) is None
58- stdio .assert_not_called ()
59- assert f"{ sub } is not a valid Git repository" in caplog .text
68+ monkeypatch .chdir (sub )
69+ with caplog .at_level (logging .INFO , logger = "mcp_server_git.server" ):
70+ async with connect (Path ("." )):
71+ pass
72+ assert f"Using repository at { root } " in caplog .text
6073
6174
6275async def test_serve_returns_early_for_non_repository (
@@ -72,16 +85,22 @@ async def test_serve_returns_early_for_non_repository(
7285 assert f"{ tmp_path } is not a valid Git repository" in caplog .text
7386
7487
75- # KNOWN BUG #4993: a nonexistent --repository escapes serve() as NoSuchPathError instead of a logged error; the fix changes this assertion.
76- async def test_serve_raises_for_nonexistent_repository ( tmp_path : Path ):
77- # Characterization: serve() catches only InvalidGitRepositoryError, so a
78- # nonexistent path escapes as NoSuchPathError and kills the server with a
79- # traceback (see the console-script smoke below) .
88+ async def test_serve_exits_for_nonexistent_repository (
89+ tmp_path : Path , caplog : pytest . LogCaptureFixture
90+ ):
91+ # #4993: a nonexistent path is logged like an invalid repository, and the
92+ # server exits non-zero without opening stdio .
8093 stdio = mock .MagicMock ()
81- with mock .patch ("mcp_server_git.server.stdio_server" , stdio ):
82- with pytest .raises (git .NoSuchPathError ):
83- await serve (tmp_path / "missing" )
94+ missing = tmp_path / "missing"
95+ with (
96+ mock .patch ("mcp_server_git.server.stdio_server" , stdio ),
97+ caplog .at_level (logging .ERROR , logger = "mcp_server_git.server" ),
98+ ):
99+ with pytest .raises (SystemExit ) as exc :
100+ await serve (missing )
101+ assert exc .value .code == 1
84102 stdio .assert_not_called ()
103+ assert f"{ missing } does not exist" in caplog .text
85104
86105
87106# --------------------------------------------------------------------------
@@ -167,12 +186,12 @@ def test_python_dash_m_calls_main():
167186 fake_main .assert_called_once_with ()
168187
169188
170- # KNOWN BUG #4993: a nonexistent --repository kills the server with a Python traceback instead of a one-line error; the fix changes this assertion.
171- def test_console_script_nonexistent_repository_exits_with_traceback (tmp_path : Path ):
189+ def test_console_script_nonexistent_repository_exits_with_one_line_error (
190+ tmp_path : Path ,
191+ ):
172192 # The one subprocess test: the installed entry point, end to end. A
173- # nonexistent --repository is not caught (see
174- # test_serve_raises_for_nonexistent_repository), so the process dies with
175- # a Python traceback instead of a one-line error.
193+ # nonexistent --repository exits non-zero with a one-line logged error
194+ # naming the path, not a Python traceback (#4993).
176195 script = shutil .which ("mcp-server-git" , path = str (Path (sys .executable ).parent ))
177196 assert script is not None
178197 missing = tmp_path / "missing"
@@ -184,6 +203,7 @@ def test_console_script_nonexistent_repository_exits_with_traceback(tmp_path: Pa
184203 timeout = 60 ,
185204 )
186205 assert proc .returncode == 1
187- assert "Traceback (most recent call last)" in proc .stderr
188- assert "git.exc.NoSuchPathError" in proc .stderr
189- assert str (missing ) in proc .stderr
206+ assert "Traceback" not in proc .stderr
207+ assert proc .stderr .splitlines () == [
208+ f"ERROR:mcp_server_git.server:{ missing } does not exist"
209+ ]
0 commit comments