From 3740c89ed69dd3b9adb0b4bcb660ab4db6e856b3 Mon Sep 17 00:00:00 2001 From: Karma Riuk Date: Sat, 1 Mar 2025 18:29:04 +0100 Subject: [PATCH] added extraction of number of tests (both from output and grep) --- clone_repos.py | 1 + handlers.py | 46 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/clone_repos.py b/clone_repos.py index 97c74b3..3391b21 100644 --- a/clone_repos.py +++ b/clone_repos.py @@ -171,6 +171,7 @@ def save_df_with_updates(df, updates_list, verbose=False): n_tests_with_grep=None, n_tests_passed=None, n_tests_failed=None, + n_tests_errors=None, n_tests_skipped=None, good_repo_for_crab=None, error_msg=None, diff --git a/handlers.py b/handlers.py index ba03c57..e0eced1 100644 --- a/handlers.py +++ b/handlers.py @@ -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.