test_path_objects.py•4.3 kB
"""
Unit tests for handling Path objects in locals.
Tests that Path objects in local variables are properly serialized.
"""
from pathlib import Path
import tempfile
import pytest
from mcp_debug_tool.debugger import DebugController
from mcp_debug_tool.utils import safe_repr
class TestPathObjectHandling:
"""Test safe handling of Path objects in various scenarios."""
def test_safe_repr_with_valid_path(self):
"""Test that safe_repr handles valid Path objects correctly."""
path = Path("/tmp/test.py")
repr_str, truncated = safe_repr(path)
assert "Path(" in repr_str
assert "/tmp/test.py" in repr_str
assert truncated is False
def test_safe_repr_with_relative_path(self):
"""Test that safe_repr handles relative Path objects."""
path = Path("src/test.py")
repr_str, truncated = safe_repr(path)
assert "Path(" in repr_str
assert "src/test.py" in repr_str
assert truncated is False
def test_safe_repr_with_path_object_in_depth(self):
"""Test that Path objects work with depth parameter."""
path = Path("/tmp/test.py")
repr_str, truncated = safe_repr(path, depth=1, max_depth=2)
assert "Path(" in repr_str
assert truncated is False
def test_debugger_captures_path_in_locals(self, tmp_path):
"""Test that debugger can capture Path objects in local variables."""
# Create a test script with Path in locals
test_script = tmp_path / "test_with_path.py"
test_script.write_text("""
from pathlib import Path
my_path = Path("/tmp/example.py")
another_path = Path("relative/path.py")
result = "done"
""")
controller = DebugController()
response = controller.run_to_breakpoint(
test_script,
str(test_script),
6 # Line with "result = "
)
# Should complete without errors
assert response.completed is True or response.hit is True
assert response.error is None
if response.hit:
# Check that paths were captured
assert response.locals is not None
assert "my_path" in response.locals
assert "another_path" in response.locals
# Path objects should be represented as strings
my_path_repr = response.locals["my_path"]["repr"]
assert isinstance(my_path_repr, str)
assert "/tmp/example.py" in my_path_repr
def test_debugger_with_pathlib_operations(self, tmp_path):
"""Test debugger with complex pathlib operations."""
test_script = tmp_path / "pathlib_ops.py"
test_script.write_text("""
from pathlib import Path
base = Path("/home/user")
file_path = base / "documents" / "file.txt"
parent = file_path.parent
name = file_path.name
checkpoint = "here"
""")
controller = DebugController()
response = controller.run_to_breakpoint(
test_script,
str(test_script),
8 # Line with "checkpoint = " (after name assignment)
)
assert response.error is None
if response.hit:
assert response.locals is not None
# All path-related variables should be captured safely
assert "file_path" in response.locals
assert "parent" in response.locals
assert "name" in response.locals
def test_path_in_collection(self, tmp_path):
"""Test that Paths in lists/dicts are handled correctly."""
test_script = tmp_path / "path_collection.py"
test_script.write_text("""
from pathlib import Path
paths_list = [Path("/a"), Path("/b"), Path("/c")]
paths_dict = {"first": Path("/x"), "second": Path("/y")}
done = True
""")
controller = DebugController()
response = controller.run_to_breakpoint(
test_script,
str(test_script),
6 # Line with "done = " (after both assignments)
)
# Should not raise any serialization errors
assert response.error is None
if response.hit:
assert "paths_list" in response.locals
assert "paths_dict" in response.locals