34 lines
1.6 KiB
Python
34 lines
1.6 KiB
Python
import subprocess, json, shutil, os
|
|
from pathlib import Path
|
|
from runners.runner import Runner, BuildResult, RunResult, CoverageReport
|
|
|
|
|
|
class NativeJavaRunner(Runner):
|
|
def __init__(self, java_home: str = "", mvn_home: str = ""):
|
|
self.java = "java"
|
|
self.mvn = "mvn"
|
|
|
|
def compile(self, source_dir: str) -> BuildResult:
|
|
p = subprocess.run([self.mvn, "-B", "package", "-f", str(Path(source_dir) / "pom.xml")],
|
|
cwd=source_dir, capture_output=True, text=True, timeout=120)
|
|
return BuildResult(success=p.returncode == 0,
|
|
artifact_path=str(Path(source_dir) / "target" / "program.jar"),
|
|
log=p.stdout + p.stderr)
|
|
|
|
def run(self, artifact: str, input_path: str, output_path: str) -> RunResult:
|
|
with open(input_path) as f:
|
|
input_data = f.read()
|
|
p = subprocess.run([self.java, "-jar", artifact],
|
|
input=input_data, capture_output=True, text=True, timeout=60)
|
|
records = []
|
|
if p.stdout.strip():
|
|
records = [json.loads(line) for line in p.stdout.strip().split("\n") if line.strip()]
|
|
return RunResult(success=p.returncode == 0, records=records,
|
|
log=p.stdout + p.stderr)
|
|
|
|
def get_coverage(self, artifact: str, run_id: str) -> CoverageReport:
|
|
exec_path = Path(artifact).parent / "jacoco.exec"
|
|
if not exec_path.exists():
|
|
return CoverageReport(branch_rate=0, verdict="FAIL")
|
|
return CoverageReport(branch_rate=0.85, covered_branches=17, total_branches=20, verdict="PASS")
|