-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathtest_2233_windows_stdio_imports.py
More file actions
64 lines (47 loc) · 1.86 KB
/
test_2233_windows_stdio_imports.py
File metadata and controls
64 lines (47 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""Regression tests for issue #2233: stdio imports should stay optional on Windows."""
import os
import subprocess
import sys
import textwrap
from pathlib import Path
REPO_ROOT = Path(__file__).resolve().parents[2]
def _run_repo_python(script: str) -> subprocess.CompletedProcess[str]:
env = os.environ.copy()
src_path = str(REPO_ROOT / "src")
env["PYTHONPATH"] = src_path if "PYTHONPATH" not in env else os.pathsep.join([src_path, env["PYTHONPATH"]])
return subprocess.run(
[sys.executable, "-c", script],
cwd=REPO_ROOT,
env=env,
capture_output=True,
text=True,
check=False,
)
def test_server_stdio_import_does_not_load_client_stdio():
script = textwrap.dedent("""
import importlib
import sys
importlib.import_module("mcp.server.stdio")
assert "mcp.client.stdio" not in sys.modules
""")
result = _run_repo_python(script)
assert result.returncode == 0, result.stderr
def test_root_stdio_exports_handle_missing_pywin32():
script = textwrap.dedent("""
import builtins
real_import = builtins.__import__
blocked_modules = {"pywintypes", "win32api", "win32con", "win32job"}
def guarded_import(name, globals=None, locals=None, fromlist=(), level=0):
if name.split(".", 1)[0] in blocked_modules:
raise ImportError(f"blocked import: {name}")
return real_import(name, globals, locals, fromlist, level)
builtins.__import__ = guarded_import
try:
from mcp import StdioServerParameters, stdio_client
assert StdioServerParameters.__name__ == "StdioServerParameters"
assert callable(stdio_client)
finally:
builtins.__import__ = real_import
""")
result = _run_repo_python(script)
assert result.returncode == 0, result.stderr