added extraction of number of tests (both from output and grep)

This commit is contained in:
Karma Riuk
2025-03-01 18:29:04 +01:00
parent 177f305d4c
commit 3740c89ed6
2 changed files with 46 additions and 1 deletions

View File

@ -1,5 +1,5 @@
from abc import ABC, abstractmethod
import os, re, docker
import os, re, docker, subprocess
USER_ID = os.getuid() # for container user
GROUP_ID = os.getgid()
@ -80,6 +80,17 @@ class BuildHandler(ABC):
self.updates["tested_successfully"] = True
self.updates["error_msg"] = output
self.extract_test_numbers(output)
grep_cmd = f"grep -r --include='*.java' -E '@Test|@ParameterizedTest' {self.path} | wc -l" # NOTE: After inspection, this is an upper bound, since comments get matched
try:
result = subprocess.run(grep_cmd, shell=True, capture_output=True, text=True, check=True)
test_count = result.stdout.strip()
except subprocess.CalledProcessError as e:
test_count = "-1" # Default to 0 if grep command fails
self.updates["n_tests_with_grep"] = test_count
return True
def clean_repo(self) -> None:
@ -94,6 +105,10 @@ class BuildHandler(ABC):
def test_cmd(self) -> str:
pass
@abstractmethod
def extract_test_numbers(self, output: str) -> None:
pass
@abstractmethod
def clean_cmd(self) -> str:
pass
@ -122,6 +137,28 @@ class MavenHandler(BuildHandler):
def container_name(self) -> str:
return "crab-maven"
def extract_test_numbers(self, output: str) -> None:
# NOTE: I'ma afraid this might be specific for junit and wouldn't work for other testing frameworks
pattern = r"\[INFO\] Results:\n\[INFO\]\s*\n\[INFO\] Tests run: (\d+), Failures: (\d+), Errors: (\d+), Skipped: (\d+)"
matches = re.findall(pattern, output)
self.updates["n_tests"] = 0
self.updates["n_tests_passed"] = 0 # Passed tests = Tests run - (Failures + Errors)
self.updates["n_tests_failed"] = 0
self.updates["n_tests_errors"] = 0
self.updates["n_tests_skipped"] = 0
for match in matches:
tests_run, failures, errors, skipped = map(int, match)
self.updates["n_tests"] += tests_run
self.updates["n_tests_failed"] += failures
self.updates["n_tests_errors"] += errors
self.updates["n_tests_skipped"] += skipped
self.updates["n_tests_passed"] += (tests_run - (failures + errors)) # Calculate passed tests
class GradleHandler(BuildHandler):
def __init__(self, repo_path: str, build_file: str, updates: dict) -> None:
super().__init__(repo_path, build_file, updates)
@ -139,6 +176,13 @@ class GradleHandler(BuildHandler):
def container_name(self) -> str:
return "crab-gradle"
def extract_test_numbers(self, output: str) -> None:
self.updates["n_tests"] = -1
self.updates["n_tests_passed"] = -1
self.updates["n_tests_failed"] = -1
self.updates["n_tests_errors"] = -1
self.updates["n_tests_skipped"] = -1
def merge_download_lines(lines: list) -> list:
"""
Merges lines that are part of the same download block in Maven output.