|
5 | 5 |
|
6 | 6 | import pytest |
7 | 7 |
|
| 8 | +from mcp.cli import cli |
8 | 9 | from mcp.cli.cli import _build_uv_command, _get_npx_command, _parse_file_path # type: ignore[reportPrivateUsage] |
9 | 10 |
|
10 | 11 |
|
@@ -79,23 +80,53 @@ def test_get_npx_windows(monkeypatch: pytest.MonkeyPatch): |
79 | 80 | """Should return one of the npx candidates on Windows.""" |
80 | 81 | candidates = ["npx.cmd", "npx.exe", "npx"] |
81 | 82 |
|
82 | | - def fake_run(cmd: list[str], **kw: Any) -> subprocess.CompletedProcess[bytes]: |
83 | | - if cmd[0] in candidates: |
84 | | - return subprocess.CompletedProcess(cmd, 0) |
85 | | - else: # pragma: no cover |
86 | | - raise subprocess.CalledProcessError(1, cmd[0]) |
| 83 | + def fake_which(cmd: str) -> str | None: |
| 84 | + return f"C:\\node\\{cmd}" if cmd in candidates else None |
87 | 85 |
|
88 | 86 | monkeypatch.setattr(sys, "platform", "win32") |
89 | | - monkeypatch.setattr(subprocess, "run", fake_run) |
90 | | - assert _get_npx_command() in candidates |
| 87 | + monkeypatch.setattr(cli.shutil, "which", fake_which) |
| 88 | + assert _get_npx_command() == "C:\\node\\npx.cmd" |
91 | 89 |
|
92 | 90 |
|
93 | 91 | def test_get_npx_returns_none_when_npx_missing(monkeypatch: pytest.MonkeyPatch): |
94 | 92 | """Should give None if every candidate fails.""" |
95 | 93 | monkeypatch.setattr(sys, "platform", "win32", raising=False) |
96 | 94 |
|
97 | | - def always_fail(*args: Any, **kwargs: Any) -> subprocess.CompletedProcess[bytes]: |
98 | | - raise subprocess.CalledProcessError(1, args[0]) |
| 95 | + def no_npx(_cmd: str) -> str | None: |
| 96 | + return None |
99 | 97 |
|
100 | | - monkeypatch.setattr(subprocess, "run", always_fail) |
| 98 | + monkeypatch.setattr(cli.shutil, "which", no_npx) |
101 | 99 | assert _get_npx_command() is None |
| 100 | + |
| 101 | + |
| 102 | +def test_dev_uses_arg_list_without_shell_on_windows(monkeypatch: pytest.MonkeyPatch, tmp_path: Path): |
| 103 | + """Should not route user-controlled dev paths through cmd.exe on Windows.""" |
| 104 | + server_path = tmp_path / "server&calc.py" |
| 105 | + captured: dict[str, Any] = {} |
| 106 | + |
| 107 | + def fake_run(cmd: list[str], **kwargs: Any) -> subprocess.CompletedProcess[bytes]: |
| 108 | + captured["cmd"] = cmd |
| 109 | + captured["kwargs"] = kwargs |
| 110 | + return subprocess.CompletedProcess(cmd, 0) |
| 111 | + |
| 112 | + def fake_parse_file_path(_file_spec: str) -> tuple[Path, str | None]: |
| 113 | + return server_path, None |
| 114 | + |
| 115 | + def fake_import_server(_file: Path, _server_object: str | None) -> object: |
| 116 | + return object() |
| 117 | + |
| 118 | + def fake_get_npx_command() -> str: |
| 119 | + return "C:\\node\\npx.cmd" |
| 120 | + |
| 121 | + monkeypatch.setattr(sys, "platform", "win32") |
| 122 | + monkeypatch.setattr(cli, "_parse_file_path", fake_parse_file_path) |
| 123 | + monkeypatch.setattr(cli, "_import_server", fake_import_server) |
| 124 | + monkeypatch.setattr(cli, "_get_npx_command", fake_get_npx_command) |
| 125 | + monkeypatch.setattr(cli.subprocess, "run", fake_run) |
| 126 | + |
| 127 | + with pytest.raises(SystemExit) as exc_info: |
| 128 | + cli.dev(str(server_path)) |
| 129 | + |
| 130 | + assert exc_info.value.code == 0 |
| 131 | + assert captured["cmd"][-1] == str(server_path) |
| 132 | + assert captured["kwargs"].get("shell") is not True |
0 commit comments