test_python_path.py•4.74 kB
"""
Integration test for pythonPath support in sessions.
Tests that runner subprocess uses the specified Python interpreter.
"""
import subprocess
import sys
from pathlib import Path
import pytest
from mcp_debug_tool.schemas import BreakpointRequest, StartSessionRequest
from mcp_debug_tool.sessions import SessionManager
def test_default_python_path(tmp_path):
"""Test that default uses current Python interpreter."""
# Create a simple test script
test_script = tmp_path / "test.py"
test_script.write_text("""
import sys
print(f"Python: {sys.executable}")
x = 10
y = 20
z = x + y
""")
manager = SessionManager(tmp_path)
# Create session without pythonPath
create_req = StartSessionRequest(entry="test.py", pythonPath=sys.executable)
create_resp = manager.create_session(create_req)
session_id = create_resp.sessionId
# Run to breakpoint
bp_req = BreakpointRequest(file="test.py", line=6)
bp_resp = manager.run_to_breakpoint(session_id, bp_req)
assert bp_resp.hit
assert bp_resp.locals is not None
assert "z" in bp_resp.locals
# Cleanup
manager.end_session(session_id)
def test_explicit_python_path(tmp_path):
"""Test that explicit pythonPath is respected."""
# Create a simple test script
test_script = tmp_path / "test.py"
test_script.write_text("""
import sys
x = sys.version_info
y = f"{x.major}.{x.minor}"
""")
manager = SessionManager(tmp_path)
# Use current Python explicitly
python_path = sys.executable
# Create session with explicit pythonPath
create_req = StartSessionRequest(entry="test.py", pythonPath=python_path)
create_resp = manager.create_session(create_req)
session_id = create_resp.sessionId
# Run to breakpoint
bp_req = BreakpointRequest(file="test.py", line=4)
bp_resp = manager.run_to_breakpoint(session_id, bp_req)
assert bp_resp.hit
assert bp_resp.locals is not None
assert "y" in bp_resp.locals
# Cleanup
manager.end_session(session_id)
def test_invalid_python_path(tmp_path):
"""Test that invalid pythonPath raises error."""
manager = SessionManager(tmp_path)
# Try to create session with non-existent Python
with pytest.raises(FileNotFoundError):
create_req = StartSessionRequest(
entry="test.py",
pythonPath="/nonexistent/python"
)
manager.create_session(create_req)
def test_python_version_mismatch_handling(tmp_path):
"""
Test that different Python versions can be used if available.
This test is skipped if no alternative Python is available.
"""
# Try to find an alternative Python version
alternative_python = None
# Common Python version locations
python_candidates = [
"/usr/bin/python3",
"/usr/local/bin/python3",
subprocess.run(["which", "python3.11"], capture_output=True, text=True).stdout.strip(),
subprocess.run(["which", "python3.10"], capture_output=True, text=True).stdout.strip(),
]
current_version = f"{sys.version_info.major}.{sys.version_info.minor}"
for candidate in python_candidates:
if not candidate or not Path(candidate).exists():
continue
# Check version
try:
result = subprocess.run(
[candidate, "-c", "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')"],
capture_output=True,
text=True,
timeout=5
)
version = result.stdout.strip()
if version != current_version:
alternative_python = candidate
break
except Exception:
continue
if not alternative_python:
pytest.skip("No alternative Python version found")
# Create test script
test_script = tmp_path / "version_test.py"
test_script.write_text("""
import sys
version = f"{sys.version_info.major}.{sys.version_info.minor}"
print(f"Running on Python {version}")
""")
manager = SessionManager(tmp_path)
# Create session with alternative Python
create_req = StartSessionRequest(
entry="version_test.py",
pythonPath=alternative_python
)
create_resp = manager.create_session(create_req)
session_id = create_resp.sessionId
# Run to breakpoint
bp_req = BreakpointRequest(file="version_test.py", line=4)
bp_resp = manager.run_to_breakpoint(session_id, bp_req)
# Should complete successfully even with different Python version
assert bp_resp.hit or bp_resp.completed
# Cleanup
manager.end_session(session_id)