Spaces:
Sleeping

File size: 1,430 Bytes
6d50eaf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""Unit tests for the sandboxed code executor."""

from __future__ import annotations

import pytest

from intellicode.analysis import CodeExecutor


@pytest.fixture
def executor() -> CodeExecutor:
    return CodeExecutor(timeout=5)


def test_successful_execution_captures_stdout(executor):
    result = executor.execute("print('hello world')")
    assert result.success
    assert "hello world" in result.output
    assert result.exit_code == 0


def test_failed_execution_captures_error(executor):
    result = executor.execute("raise ValueError('boom')")
    assert not result.success
    assert result.exit_code != 0
    assert "ValueError" in result.error


def test_timeout_is_enforced():
    executor = CodeExecutor(timeout=1)
    result = executor.execute("while True:\n    pass")
    assert result.timed_out
    assert not result.success


def test_output_is_truncated():
    executor = CodeExecutor(timeout=5, max_output_size=50)
    result = executor.execute("print('x' * 1000)")
    assert "truncated" in result.output
    assert len(result.output) < 200


def test_execution_time_is_recorded(executor):
    result = executor.execute("print(1)")
    assert result.execution_time >= 0
    assert "s" in result.to_dict()["execution_time"]


def test_syntax_error_in_code_is_a_failure(executor):
    result = executor.execute("def bad(:\n    pass")
    assert not result.success
    assert "SyntaxError" in result.error