CoolFace
Apppublic

Dave-13/DevSecOps

sourceHugging Faceupdated 5mo agoView on Hugging Face
0likes
test_runner.py39 linesDownload Raw Back to core
1import subprocess2import sys3import tempfile4import os5import json6 7 8def run_tests(code: str, test_code: str) -> dict:9    """Execute patched code + tests in a subprocess (sandbox isolation)."""10    with tempfile.TemporaryDirectory() as tmpdir:11        app_path = os.path.join(tmpdir, "app.py")12        test_path = os.path.join(tmpdir, "test_app.py")13 14        with open(app_path, "w") as f:15            f.write(code)16        with open(test_path, "w") as f:17            f.write(test_code)18 19        try:20            result = subprocess.run(21                [sys.executable, "-m", "pytest", test_path, "-x", "--tb=short", "-q"],22                capture_output=True,23                text=True,24                timeout=15,25                cwd=tmpdir,26                env={**os.environ, "PYTHONPATH": tmpdir},27            )28            passed = result.returncode == 029            return {30                "passed": passed,31                "stdout": result.stdout[-1500:],32                "stderr": result.stderr[-500:],33                "returncode": result.returncode,34            }35        except subprocess.TimeoutExpired:36            return {"passed": False, "stdout": "", "stderr": "timeout", "returncode": -1}37        except Exception as e:38            return {"passed": False, "stdout": "", "stderr": str(e), "returncode": -1}39