|
| 1 | +import os |
1 | 2 | import subprocess |
2 | 3 | import sys |
3 | 4 | from pathlib import Path |
| 5 | +from types import SimpleNamespace |
4 | 6 | from typing import Any |
5 | 7 |
|
6 | 8 | import pytest |
7 | 9 |
|
| 10 | +from mcp.cli import cli as cli_module |
8 | 11 | from mcp.cli.cli import _build_uv_command, _get_npx_command, _parse_file_path # type: ignore[reportPrivateUsage] |
9 | 12 |
|
10 | 13 |
|
@@ -76,26 +79,75 @@ def test_get_npx_unix_like(monkeypatch: pytest.MonkeyPatch): |
76 | 79 |
|
77 | 80 |
|
78 | 81 | def test_get_npx_windows(monkeypatch: pytest.MonkeyPatch): |
79 | | - """Should return one of the npx candidates on Windows.""" |
80 | | - candidates = ["npx.cmd", "npx.exe", "npx"] |
| 82 | + """Should return the first Windows npx executable found on PATH.""" |
| 83 | + resolved_commands = { |
| 84 | + "npx.cmd": r"C:\Program Files\nodejs\npx.cmd", |
| 85 | + "npx.exe": None, |
| 86 | + "npx": None, |
| 87 | + } |
81 | 88 |
|
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]) |
| 89 | + def fake_which(cmd: str) -> str | None: |
| 90 | + return resolved_commands.get(cmd) |
87 | 91 |
|
88 | 92 | monkeypatch.setattr(sys, "platform", "win32") |
89 | | - monkeypatch.setattr(subprocess, "run", fake_run) |
90 | | - assert _get_npx_command() in candidates |
| 93 | + monkeypatch.setattr("shutil.which", fake_which) |
| 94 | + assert _get_npx_command() == r"C:\Program Files\nodejs\npx.cmd" |
91 | 95 |
|
92 | 96 |
|
93 | 97 | def test_get_npx_returns_none_when_npx_missing(monkeypatch: pytest.MonkeyPatch): |
94 | 98 | """Should give None if every candidate fails.""" |
95 | 99 | monkeypatch.setattr(sys, "platform", "win32", raising=False) |
| 100 | + monkeypatch.setattr("shutil.which", lambda cmd: None) |
| 101 | + assert _get_npx_command() is None |
96 | 102 |
|
97 | | - def always_fail(*args: Any, **kwargs: Any) -> subprocess.CompletedProcess[bytes]: |
98 | | - raise subprocess.CalledProcessError(1, args[0]) |
99 | 103 |
|
100 | | - monkeypatch.setattr(subprocess, "run", always_fail) |
101 | | - assert _get_npx_command() is None |
| 104 | +def test_dev_runs_inspector_without_shell_on_windows(monkeypatch: pytest.MonkeyPatch, tmp_path: Path): |
| 105 | + """Should invoke the inspector with a resolved executable and shell=False on Windows.""" |
| 106 | + server_file = tmp_path / "server.py" |
| 107 | + server_file.write_text("x = 1") |
| 108 | + |
| 109 | + monkeypatch.setattr(sys, "platform", "win32") |
| 110 | + monkeypatch.setattr(cli_module, "_parse_file_path", lambda file_spec: (server_file, None)) |
| 111 | + monkeypatch.setattr(cli_module, "_import_server", lambda file, server_object: SimpleNamespace(dependencies=[])) |
| 112 | + monkeypatch.setattr( |
| 113 | + cli_module, |
| 114 | + "_build_uv_command", |
| 115 | + lambda file_spec, with_editable=None, with_packages=None: [ |
| 116 | + "uv", |
| 117 | + "run", |
| 118 | + "--with", |
| 119 | + "mcp", |
| 120 | + "mcp", |
| 121 | + "run", |
| 122 | + file_spec, |
| 123 | + ], |
| 124 | + ) |
| 125 | + monkeypatch.setattr(cli_module, "_get_npx_command", lambda: r"C:\Program Files\nodejs\npx.cmd") |
| 126 | + |
| 127 | + recorded: dict[str, Any] = {} |
| 128 | + |
| 129 | + def fake_run(cmd: list[str], **kwargs: Any) -> subprocess.CompletedProcess[str]: |
| 130 | + recorded["cmd"] = cmd |
| 131 | + recorded["kwargs"] = kwargs |
| 132 | + return subprocess.CompletedProcess(cmd, 0) |
| 133 | + |
| 134 | + monkeypatch.setattr(subprocess, "run", fake_run) |
| 135 | + |
| 136 | + with pytest.raises(SystemExit) as excinfo: |
| 137 | + cli_module.dev(str(server_file)) |
| 138 | + |
| 139 | + assert excinfo.value.code == 0 |
| 140 | + assert recorded["cmd"] == [ |
| 141 | + r"C:\Program Files\nodejs\npx.cmd", |
| 142 | + "@modelcontextprotocol/inspector", |
| 143 | + "uv", |
| 144 | + "run", |
| 145 | + "--with", |
| 146 | + "mcp", |
| 147 | + "mcp", |
| 148 | + "run", |
| 149 | + str(server_file), |
| 150 | + ] |
| 151 | + assert recorded["kwargs"]["check"] is True |
| 152 | + assert recorded["kwargs"]["env"] == dict(os.environ.items()) |
| 153 | + assert recorded["kwargs"].get("shell", False) is False |
0 commit comments