Spaces:
Sleeping
Sleeping
File size: 1,288 Bytes
57a6d0c a3054e5 57a6d0c a3054e5 | 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 | import pytest
from server.executor import is_safe, execute_code
def test_executor_is_safe():
assert is_safe("def add(a,b):\n return a+b") == True
# Blocked imports
assert is_safe("import os") == False
assert is_safe("from os import path") == False
assert is_safe("import sys\nsys.exit(1)") == False
assert is_safe("import subprocess as sp") == False
# Syntax errors
assert is_safe("def add(a b):") == False
def test_execute_code():
code = "def add(a, b): return a + b"
tests = "assert add(1, 2) == 3"
res = execute_code(code, tests)
assert res.passed == True
assert res.syntax_error == False
code_buggy = "def add(a, b): return a - b"
res2 = execute_code(code_buggy, tests)
assert res2.passed == False
assert res2.syntax_error == False
def test_execute_code_calls_humaneval_check():
tests = "def check(candidate):\n assert candidate(1, 2) == 3"
good = "def add(a, b): return a + b"
good_res = execute_code(good, tests)
assert good_res.passed == True
assert good_res.syntax_error == False
buggy = "def add(a, b): return a - b"
buggy_res = execute_code(buggy, tests)
assert buggy_res.passed == False
assert buggy_res.syntax_error == False
|