2025-07-17 06:52:01 -04:00
|
|
|
import subprocess
|
|
|
|
|
import sys
|
|
|
|
|
from pathlib import Path
|
2025-08-11 19:56:37 +02:00
|
|
|
from typing import Any
|
2025-07-17 06:52:01 -04:00
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
2025-08-11 19:56:37 +02:00
|
|
|
from mcp.cli.cli import _build_uv_command, _get_npx_command, _parse_file_path # type: ignore[reportPrivateUsage]
|
2025-07-17 06:52:01 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
"spec, expected_obj",
|
|
|
|
|
[
|
|
|
|
|
("server.py", None),
|
|
|
|
|
("foo.py:srv_obj", "srv_obj"),
|
|
|
|
|
],
|
|
|
|
|
)
|
2025-08-11 19:56:37 +02:00
|
|
|
def test_parse_file_path_accepts_valid_specs(tmp_path: Path, spec: str, expected_obj: str | None):
|
2025-07-17 06:52:01 -04:00
|
|
|
"""Should accept valid file specs."""
|
|
|
|
|
file = tmp_path / spec.split(":")[0]
|
|
|
|
|
file.write_text("x = 1")
|
|
|
|
|
path, obj = _parse_file_path(f"{file}:{expected_obj}" if ":" in spec else str(file))
|
|
|
|
|
assert path == file.resolve()
|
|
|
|
|
assert obj == expected_obj
|
|
|
|
|
|
|
|
|
|
|
2025-08-11 19:56:37 +02:00
|
|
|
def test_parse_file_path_missing(tmp_path: Path):
|
2025-07-17 06:52:01 -04:00
|
|
|
"""Should system exit if a file is missing."""
|
|
|
|
|
with pytest.raises(SystemExit):
|
|
|
|
|
_parse_file_path(str(tmp_path / "missing.py"))
|
|
|
|
|
|
|
|
|
|
|
2025-08-11 19:56:37 +02:00
|
|
|
def test_parse_file_exit_on_dir(tmp_path: Path):
|
2025-07-17 06:52:01 -04:00
|
|
|
"""Should system exit if a directory is passed"""
|
|
|
|
|
dir_path = tmp_path / "dir"
|
|
|
|
|
dir_path.mkdir()
|
|
|
|
|
with pytest.raises(SystemExit):
|
|
|
|
|
_parse_file_path(str(dir_path))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_build_uv_command_minimal():
|
|
|
|
|
"""Should emit core command when no extras specified."""
|
|
|
|
|
cmd = _build_uv_command("foo.py")
|
|
|
|
|
assert cmd == ["uv", "run", "--with", "mcp", "mcp", "run", "foo.py"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_build_uv_command_adds_editable_and_packages():
|
|
|
|
|
"""Should include --with-editable and every --with pkg in correct order."""
|
|
|
|
|
test_path = Path("/pkg")
|
|
|
|
|
cmd = _build_uv_command(
|
|
|
|
|
"foo.py",
|
|
|
|
|
with_editable=test_path,
|
|
|
|
|
with_packages=["package1", "package2"],
|
|
|
|
|
)
|
|
|
|
|
assert cmd == [
|
|
|
|
|
"uv",
|
|
|
|
|
"run",
|
|
|
|
|
"--with",
|
|
|
|
|
"mcp",
|
|
|
|
|
"--with-editable",
|
|
|
|
|
str(test_path), # Use str() to match what the function does
|
|
|
|
|
"--with",
|
|
|
|
|
"package1",
|
|
|
|
|
"--with",
|
|
|
|
|
"package2",
|
|
|
|
|
"mcp",
|
|
|
|
|
"run",
|
|
|
|
|
"foo.py",
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
2025-08-11 19:56:37 +02:00
|
|
|
def test_get_npx_unix_like(monkeypatch: pytest.MonkeyPatch):
|
2025-07-17 06:52:01 -04:00
|
|
|
"""Should return "npx" on unix-like systems."""
|
|
|
|
|
monkeypatch.setattr(sys, "platform", "linux")
|
|
|
|
|
assert _get_npx_command() == "npx"
|
|
|
|
|
|
|
|
|
|
|
2025-08-11 19:56:37 +02:00
|
|
|
def test_get_npx_windows(monkeypatch: pytest.MonkeyPatch):
|
2025-07-17 06:52:01 -04:00
|
|
|
"""Should return one of the npx candidates on Windows."""
|
|
|
|
|
candidates = ["npx.cmd", "npx.exe", "npx"]
|
|
|
|
|
|
2025-08-11 19:56:37 +02:00
|
|
|
def fake_run(cmd: list[str], **kw: Any) -> subprocess.CompletedProcess[bytes]:
|
2025-07-17 06:52:01 -04:00
|
|
|
if cmd[0] in candidates:
|
|
|
|
|
return subprocess.CompletedProcess(cmd, 0)
|
|
|
|
|
else: # pragma: no cover
|
|
|
|
|
raise subprocess.CalledProcessError(1, cmd[0])
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(sys, "platform", "win32")
|
|
|
|
|
monkeypatch.setattr(subprocess, "run", fake_run)
|
|
|
|
|
assert _get_npx_command() in candidates
|
|
|
|
|
|
|
|
|
|
|
2025-08-11 19:56:37 +02:00
|
|
|
def test_get_npx_returns_none_when_npx_missing(monkeypatch: pytest.MonkeyPatch):
|
2025-07-17 06:52:01 -04:00
|
|
|
"""Should give None if every candidate fails."""
|
|
|
|
|
monkeypatch.setattr(sys, "platform", "win32", raising=False)
|
|
|
|
|
|
2025-08-11 19:56:37 +02:00
|
|
|
def always_fail(*args: Any, **kwargs: Any) -> subprocess.CompletedProcess[bytes]:
|
2025-07-17 06:52:01 -04:00
|
|
|
raise subprocess.CalledProcessError(1, args[0])
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(subprocess, "run", always_fail)
|
|
|
|
|
assert _get_npx_command() is None
|