test_stdout_capture.py•4.51 kB
"""
Test that stdout/stderr from debugged scripts doesn't interfere with JSON protocol.
"""
from pathlib import Path
import pytest
from mcp_debug_tool.schemas import BreakpointRequest, StartSessionRequest
from mcp_debug_tool.sessions import SessionManager
def test_script_with_print_statements(tmp_path):
"""Test debugging a script that uses print()."""
test_script = tmp_path / "print_test.py"
test_script.write_text("""
print("Starting script...")
x = 10
print(f"x = {x}")
y = 20
print(f"y = {y}")
z = x + y
print(f"Result: {z}")
""")
manager = SessionManager(tmp_path)
create_req = StartSessionRequest(entry="print_test.py", pythonPath=sys.executable)
create_resp = manager.create_session(create_req)
session_id = create_resp.sessionId
# Run to breakpoint - should not be affected by print statements
# Line 8 is where print(f"Result: {z}") executes (after z = x + y on line 7)
bp_req = BreakpointRequest(file="print_test.py", line=8)
bp_resp = manager.run_to_breakpoint(session_id, bp_req)
assert bp_resp.hit, f"Expected breakpoint hit, got error: {bp_resp.error}"
assert bp_resp.locals is not None
assert "z" in bp_resp.locals
# Cleanup
manager.end_session(session_id)
def test_script_with_stderr_output(tmp_path):
"""Test debugging a script that writes to stderr."""
test_script = tmp_path / "stderr_test.py"
test_script.write_text("""
import sys
sys.stderr.write("Error message\\n")
x = 10
sys.stderr.write(f"Debug: x={x}\\n")
y = 20
z = x + y
""")
manager = SessionManager(tmp_path)
create_req = StartSessionRequest(entry="stderr_test.py", pythonPath=sys.executable)
create_resp = manager.create_session(create_req)
session_id = create_resp.sessionId
bp_req = BreakpointRequest(file="stderr_test.py", line=7)
bp_resp = manager.run_to_breakpoint(session_id, bp_req)
assert bp_resp.hit
# At line 7 (z = x + y), z is being assigned, check that x and y exist
assert "x" in bp_resp.locals
assert "y" in bp_resp.locals
manager.end_session(session_id)
def test_script_with_multiline_output(tmp_path):
"""Test script with multiple print statements on different lines."""
test_script = tmp_path / "multiline_test.py"
test_script.write_text("""
for i in range(5):
print(f"Iteration {i}")
x = "done"
y = len(x)
print("All iterations complete")
""")
manager = SessionManager(tmp_path)
create_req = StartSessionRequest(entry="multiline_test.py", pythonPath=sys.executable)
create_resp = manager.create_session(create_req)
session_id = create_resp.sessionId
bp_req = BreakpointRequest(file="multiline_test.py", line=7)
bp_resp = manager.run_to_breakpoint(session_id, bp_req)
assert bp_resp.hit
assert "y" in bp_resp.locals
manager.end_session(session_id)
def test_script_with_json_like_output(tmp_path):
"""Test script that prints JSON-like strings."""
test_script = tmp_path / "json_output_test.py"
test_script.write_text("""
import json
data = {"key": "value"}
print(json.dumps(data))
x = 42
y = x * 2
""")
manager = SessionManager(tmp_path)
create_req = StartSessionRequest(entry="json_output_test.py", pythonPath=sys.executable)
create_resp = manager.create_session(create_req)
session_id = create_resp.sessionId
bp_req = BreakpointRequest(file="json_output_test.py", line=6)
bp_resp = manager.run_to_breakpoint(session_id, bp_req)
# Should not confuse script's JSON output with protocol JSON
assert bp_resp.hit
# At line 6 (y = x * 2), y is being assigned
assert "x" in bp_resp.locals
manager.end_session(session_id)
def test_script_with_logging(tmp_path):
"""Test script that uses logging module (simplified)."""
test_script = tmp_path / "logging_test.py"
test_script.write_text("""
import sys
# Just test that stderr redirection doesn't break normal code
x = 100
y = 200
z = x + y
""")
manager = SessionManager(tmp_path)
create_req = StartSessionRequest(entry="logging_test.py", pythonPath=sys.executable)
create_resp = manager.create_session(create_req)
session_id = create_resp.sessionId
bp_req = BreakpointRequest(file="logging_test.py", line=6)
bp_resp = manager.run_to_breakpoint(session_id, bp_req)
assert bp_resp.hit
assert "z" in bp_resp.locals or "x" in bp_resp.locals
manager.end_session(session_id)