Spaces:
Sleeping
Sleeping
File size: 1,936 Bytes
ba52031 | 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 54 55 56 57 58 59 60 61 62 63 64 | """
run_tests tool — executes pytest inside the task workdir and returns a ToolResult.
"""
import logging
import re
import subprocess
from debug_env.server.schemas import ToolResult
logger = logging.getLogger(__name__)
def _parse_pass_rate(output: str, returncode: int) -> float:
passed_match = re.search(r"(\d+) passed", output)
failed_match = re.search(r"(\d+) failed", output)
error_match = re.search(r"(\d+) error", output)
passed = int(passed_match.group(1)) if passed_match else 0
failed = int(failed_match.group(1)) if failed_match else 0
errors = int(error_match.group(1)) if error_match else 0
total = passed + failed + errors
if total > 0:
return passed / total
return 1.0 if returncode == 0 else 0.0
def run_tests(workdir: str) -> ToolResult:
"""
Run ``pytest -q --tb=short`` inside *workdir*.
Args:
workdir: Absolute path to the task working directory.
Returns:
:class:`ToolResult` with ``pass_rate`` and ``logs``.
"""
logger.info(f"Running tests in: {workdir}")
try:
result = subprocess.run(
["pytest", "-q", "--tb=short", workdir],
capture_output=True,
text=True,
cwd=workdir,
)
output = result.stdout + result.stderr
pass_rate = _parse_pass_rate(output, result.returncode)
logger.info(f"Tests complete — pass_rate={pass_rate:.2f} (returncode={result.returncode})")
return ToolResult(pass_rate=pass_rate, logs=output, success=True)
except FileNotFoundError:
msg = "pytest not found. Ensure it is installed in the environment."
logger.error(msg)
return ToolResult(pass_rate=0.0, logs=msg, success=False)
except Exception as e:
msg = f"Unexpected error running tests: {e}"
logger.error(msg, exc_info=True)
return ToolResult(pass_rate=0.0, logs=msg, success=False)
|