Skip to content

Commit 7ba41dc

Browse files
authored
fix: make local coverage runs reliable (#2236)
1 parent eaf971c commit 7ba41dc

File tree

4 files changed

+27
-18
lines changed

4 files changed

+27
-18
lines changed

.github/workflows/shared.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ jobs:
7070
- name: Run pytest with coverage
7171
shell: bash
7272
run: |
73+
uv run --frozen --no-sync coverage erase
7374
uv run --frozen --no-sync coverage run -m pytest -n auto
7475
uv run --frozen --no-sync coverage combine
7576
uv run --frozen --no-sync coverage report

CLAUDE.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,19 @@ This document contains critical information about working with this codebase. Fo
2828
- Bug fixes require regression tests
2929
- IMPORTANT: The `tests/client/test_client.py` is the most well designed test file. Follow its patterns.
3030
- IMPORTANT: Be minimal, and focus on E2E tests: Use the `mcp.client.Client` whenever possible.
31-
- IMPORTANT: Before pushing, verify 100% branch coverage on changed files by running
32-
`uv run --frozen pytest -x` (coverage is configured in `pyproject.toml` with `fail_under = 100`
33-
and `branch = true`). If any branch is uncovered, add a test for it before pushing.
31+
- Coverage: CI requires 100% (`fail_under = 100`, `branch = true`).
32+
- Full check: `./scripts/test` (~20s, matches CI exactly)
33+
- Targeted check while iterating:
34+
35+
```bash
36+
uv run --frozen coverage erase
37+
uv run --frozen coverage run -m pytest tests/path/test_foo.py
38+
uv run --frozen coverage combine
39+
uv run --frozen coverage report --include='src/mcp/path/foo.py' --fail-under=0
40+
```
41+
42+
Partial runs can't hit 100% (coverage tracks `tests/` too), so `--fail-under=0`
43+
and `--include` scope the report to what you actually changed.
3444
- Avoid `anyio.sleep()` with a fixed duration to wait for async operations. Instead:
3545
- Use `anyio.Event` — set it in the callback/handler, `await event.wait()` in the test
3646
- For stream messages, use `await stream.receive()` instead of `sleep()` + `receive_nowait()`

scripts/test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
set -ex
44

5+
uv run --frozen coverage erase
56
uv run --frozen coverage run -m pytest -n auto $@
67
uv run --frozen coverage combine
78
uv run --frozen coverage report

tests/test_examples.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
# pyright: reportUnknownArgumentType=false
66
# pyright: reportUnknownMemberType=false
77

8-
import sys
98
from pathlib import Path
109

1110
import pytest
@@ -65,12 +64,17 @@ async def test_direct_call_tool_result_return():
6564

6665

6766
@pytest.mark.anyio
68-
async def test_desktop(monkeypatch: pytest.MonkeyPatch):
67+
async def test_desktop(tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
6968
"""Test the desktop server"""
70-
# Mock desktop directory listing
71-
mock_files = [Path("/fake/path/file1.txt"), Path("/fake/path/file2.txt")]
72-
monkeypatch.setattr(Path, "iterdir", lambda self: mock_files) # type: ignore[reportUnknownArgumentType]
73-
monkeypatch.setattr(Path, "home", lambda: Path("/fake/home"))
69+
# Build a real Desktop directory under tmp_path rather than patching
70+
# Path.iterdir — a class-level patch breaks jsonschema_specifications'
71+
# import-time schema discovery when this test happens to be the first
72+
# tool call in an xdist worker.
73+
desktop = tmp_path / "Desktop"
74+
desktop.mkdir()
75+
(desktop / "file1.txt").touch()
76+
(desktop / "file2.txt").touch()
77+
monkeypatch.setattr(Path, "home", lambda: tmp_path)
7478

7579
from examples.mcpserver.desktop import mcp
7680

@@ -85,15 +89,8 @@ async def test_desktop(monkeypatch: pytest.MonkeyPatch):
8589
content = result.contents[0]
8690
assert isinstance(content, TextResourceContents)
8791
assert isinstance(content.text, str)
88-
if sys.platform == "win32": # pragma: no cover
89-
file_1 = "/fake/path/file1.txt".replace("/", "\\\\") # might be a bug
90-
file_2 = "/fake/path/file2.txt".replace("/", "\\\\") # might be a bug
91-
assert file_1 in content.text
92-
assert file_2 in content.text
93-
# might be a bug, but the test is passing
94-
else: # pragma: lax no cover
95-
assert "/fake/path/file1.txt" in content.text
96-
assert "/fake/path/file2.txt" in content.text
92+
assert "file1.txt" in content.text
93+
assert "file2.txt" in content.text
9794

9895

9996
# TODO(v2): Change back to README.md when v2 is released

0 commit comments

Comments
 (0)