Skip to content

Commit e9c4405

Browse files
committed
fix: restrict Path.iterdir mock in test_desktop to avoid breaking jsonschema on Windows
The previous implementation patched Path.iterdir globally on the class, which caused jsonschema_specifications to receive fake paths when it lazily loads its schema files on Windows, resulting in FileNotFoundError. Capture the original method and forward all non-fake paths to it so that only iterdir calls on the mocked desktop path return the stub file list.
1 parent f33add0 commit e9c4405

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

tests/test_examples.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,16 @@ async def test_desktop(monkeypatch: pytest.MonkeyPatch):
6969
"""Test the desktop server"""
7070
# Mock desktop directory listing
7171
mock_files = [Path("/fake/path/file1.txt"), Path("/fake/path/file2.txt")]
72-
monkeypatch.setattr(Path, "iterdir", lambda self: mock_files) # type: ignore[reportUnknownArgumentType]
72+
original_iterdir = Path.iterdir
73+
74+
def _mock_iterdir(self: Path) -> object:
75+
# Restrict the patch to our fake path so that jsonschema's lazy schema
76+
# loading (which also calls Path.iterdir) is not affected on any platform.
77+
if "fake" in str(self):
78+
return mock_files
79+
return original_iterdir(self) # type: ignore[reportUnknownArgumentType]
80+
81+
monkeypatch.setattr(Path, "iterdir", _mock_iterdir)
7382
monkeypatch.setattr(Path, "home", lambda: Path("/fake/home"))
7483

7584
from examples.mcpserver.desktop import mcp

0 commit comments

Comments
 (0)