Compare commits
2 Commits
fix/format
...
fix/913-sy
| Author | SHA1 | Date | |
|---|---|---|---|
| c17f64fa2c | |||
| bc7ffc2166 |
82
tests/test_syntax_validation.py
Normal file
82
tests/test_syntax_validation.py
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
"""Tests for Python syntax validation in execute_code."""
|
||||||
|
|
||||||
|
import json
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
# Import the validation function directly
|
||||||
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||||||
|
from tools.code_execution_tool import _validate_python_syntax
|
||||||
|
|
||||||
|
|
||||||
|
class TestValidatePythonSyntax:
|
||||||
|
"""Test _validate_python_syntax catches errors before subprocess spawn."""
|
||||||
|
|
||||||
|
def test_valid_code_returns_none(self):
|
||||||
|
assert _validate_python_syntax("print('hello')") is None
|
||||||
|
|
||||||
|
def test_valid_multiline_returns_none(self):
|
||||||
|
code = """
|
||||||
|
import os
|
||||||
|
def foo():
|
||||||
|
return 42
|
||||||
|
result = foo()
|
||||||
|
"""
|
||||||
|
assert _validate_python_syntax(code) is None
|
||||||
|
|
||||||
|
def test_syntax_error_detected(self):
|
||||||
|
result = _validate_python_syntax("def foo(
|
||||||
|
")
|
||||||
|
assert result is not None
|
||||||
|
data = json.loads(result)
|
||||||
|
assert data["syntax_error"] is True
|
||||||
|
assert "line" in data
|
||||||
|
assert "message" in data
|
||||||
|
|
||||||
|
def test_missing_colon(self):
|
||||||
|
result = _validate_python_syntax("def foo()
|
||||||
|
pass")
|
||||||
|
data = json.loads(result)
|
||||||
|
assert data["syntax_error"] is True
|
||||||
|
assert data["line"] == 1
|
||||||
|
|
||||||
|
def test_unmatched_paren(self):
|
||||||
|
result = _validate_python_syntax("print('hello'")
|
||||||
|
data = json.loads(result)
|
||||||
|
assert data["syntax_error"] is True
|
||||||
|
|
||||||
|
def test_indentation_error(self):
|
||||||
|
result = _validate_python_syntax("def foo():
|
||||||
|
pass")
|
||||||
|
data = json.loads(result)
|
||||||
|
assert data["syntax_error"] is True
|
||||||
|
assert data["line"] == 2
|
||||||
|
|
||||||
|
def test_invalid_character(self):
|
||||||
|
result = _validate_python_syntax("x = 5 √ 2")
|
||||||
|
data = json.loads(result)
|
||||||
|
assert data["syntax_error"] is True
|
||||||
|
|
||||||
|
def test_error_format_has_required_fields(self):
|
||||||
|
result = _validate_python_syntax("def(
|
||||||
|
")
|
||||||
|
data = json.loads(result)
|
||||||
|
assert "error" in data
|
||||||
|
assert "syntax_error" in data
|
||||||
|
assert "line" in data
|
||||||
|
assert "offset" in data
|
||||||
|
assert "message" in data
|
||||||
|
|
||||||
|
def test_empty_string_returns_none(self):
|
||||||
|
# Empty code is caught by the guard before validation
|
||||||
|
# But if called directly, ast.parse("") is valid
|
||||||
|
assert _validate_python_syntax("") is None
|
||||||
|
|
||||||
|
def test_comment_only_returns_none(self):
|
||||||
|
assert _validate_python_syntax("# just a comment") is None
|
||||||
|
|
||||||
|
def test_complex_valid_code(self):
|
||||||
|
code =
|
||||||
@@ -28,6 +28,7 @@ Platform: Linux / macOS only (Unix domain sockets for local). Disabled on Window
|
|||||||
Remote execution additionally requires Python 3 in the terminal backend.
|
Remote execution additionally requires Python 3 in the terminal backend.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import ast
|
||||||
import base64
|
import base64
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
@@ -883,6 +884,42 @@ def _execute_remote(
|
|||||||
return json.dumps(result, ensure_ascii=False)
|
return json.dumps(result, ensure_ascii=False)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def _validate_python_syntax(code: str) -> Optional[str]:
|
||||||
|
"""Validate Python syntax before subprocess spawn.
|
||||||
|
|
||||||
|
Runs ast.parse() in-process (sub-millisecond) to catch syntax errors
|
||||||
|
before wasting time spawning a sandboxed subprocess.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
JSON error string with line, offset, message if syntax is invalid.
|
||||||
|
None if syntax is valid.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
ast.parse(code)
|
||||||
|
return None
|
||||||
|
except SyntaxError as exc:
|
||||||
|
# Build context: show offending line with caret
|
||||||
|
lines = code.split("\n")
|
||||||
|
error_line = lines[exc.lineno - 1] if exc.lineno and exc.lineno <= len(lines) else ""
|
||||||
|
context = ""
|
||||||
|
if error_line:
|
||||||
|
context = f"\n {error_line}"
|
||||||
|
if exc.offset:
|
||||||
|
context += f"\n {' ' * (exc.offset - 1)}^"
|
||||||
|
|
||||||
|
return json.dumps({
|
||||||
|
"error": f"Python syntax error on line {exc.lineno}: {exc.msg}{context}",
|
||||||
|
"syntax_error": True,
|
||||||
|
"line": exc.lineno,
|
||||||
|
"offset": exc.offset,
|
||||||
|
"message": exc.msg,
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
# Main entry point
|
# Main entry point
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
@@ -916,6 +953,11 @@ def execute_code(
|
|||||||
if not code or not code.strip():
|
if not code or not code.strip():
|
||||||
return tool_error("No code provided.")
|
return tool_error("No code provided.")
|
||||||
|
|
||||||
|
# Syntax check before subprocess spawn (catches ~15% of errors in <1ms)
|
||||||
|
syntax_error = _validate_python_syntax(code)
|
||||||
|
if syntax_error:
|
||||||
|
return syntax_error
|
||||||
|
|
||||||
# Dispatch: remote backends use file-based RPC, local uses UDS
|
# Dispatch: remote backends use file-based RPC, local uses UDS
|
||||||
from tools.terminal_tool import _get_env_config
|
from tools.terminal_tool import _get_env_config
|
||||||
env_type = _get_env_config()["env_type"]
|
env_type = _get_env_config()["env_type"]
|
||||||
|
|||||||
Reference in New Issue
Block a user