Enhanced Test Report Directory Naming and Handling (#312)

This commit is contained in:
Swifty
2023-08-16 17:45:46 +02:00
committed by GitHub
parent 662810525f
commit 16053a3137

View File

@@ -1,9 +1,8 @@
# radio charts, logs, helper functions for tests, anything else relevant.
import math
import os
import re
import sys
from datetime import datetime
from datetime import datetime, timezone
from pathlib import Path
from typing import Any, List, Optional
@@ -18,76 +17,45 @@ AGENT_NAME = os.getenv("AGENT_NAME")
REPORT_LOCATION = os.getenv("REPORT_LOCATION", None)
def calculate_info_test_path(reports_path: Path) -> str:
command = sys.argv
def calculate_info_test_path(base_path: Path) -> str:
"""
Calculates the path to the directory where the test report will be saved.
"""
# Ensure the reports path exists
base_path.mkdir(parents=True, exist_ok=True)
if not reports_path.exists():
reports_path.mkdir(parents=True, exist_ok=True)
# Get current UTC date-time stamp
date_stamp = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S")
# Collect all directories in reports_path
all_items = os.listdir(str(reports_path))
dirs = [item for item in all_items if os.path.isdir(reports_path / item)]
# Default run name
run_name = "full_run"
# Default naming scheme
dir_count = len(dirs)
run_name = f"folder{dir_count + 1}_{datetime.now().strftime('%m-%d-%H-%M')}"
# Map command-line arguments to their respective labels
arg_labels = {
"--test": None,
"--suite": None,
"--category": None,
"--maintain": "maintain",
"--improve": "improve",
"--explore": "explore",
}
test_index = None
test_arg = None
# Check command-line arguments
if "--test" in command:
test_index = command.index("--test")
elif "--suite" in command:
test_index = command.index("--suite")
elif "--category" in command:
test_index = command.index("--category")
elif "--maintain" in command:
test_index = command.index("--maintain")
test_arg = "maintain"
elif "--improve" in command:
test_index = command.index("--improve")
test_arg = "improve"
elif "--improve" in command:
test_index = command.index("--explore")
test_arg = "explore"
# Identify the relevant command-line argument
for arg, label in arg_labels.items():
if arg in sys.argv:
test_arg = sys.argv[sys.argv.index(arg) + 1] if label is None else None
run_name = arg.strip("--")
if test_arg:
run_name = f"{run_name}_{test_arg}"
break
if test_index:
if not test_arg:
test_arg = command[test_index + 1]
# Create the full new directory path with ISO standard UTC date-time stamp
report_path = base_path / f"{date_stamp}_{run_name}"
# collect related directories and their prefix numbers
related_dirs = []
prefix_numbers = []
# Ensure the new directory is created
report_path.mkdir(exist_ok=True)
for dir in dirs:
dir_parts = dir.split("_")
try:
prefix = float(dir_parts[0][6:])
test_name = "_".join(dir_parts[1:])
if test_arg == test_name:
prefix_numbers.append(math.floor(prefix))
related_dirs.append(test_name)
except:
pass
related_dir_count = len(related_dirs)
if related_dir_count == 0:
max_prefix = max(prefix_numbers, default=0)
run_name = f"folder{max_prefix + 1}_{test_arg}"
else:
print(
f"Found {related_dir_count} directories with '{test_arg}' in the name"
)
prefix = max(prefix_numbers)
run_name = f"folder{prefix}.{related_dir_count}_{test_arg}"
new_dir_path = reports_path / run_name
if not os.path.exists(new_dir_path):
os.makedirs(new_dir_path)
return str(new_dir_path)
return str(report_path)
def replace_backslash(value: Any) -> Any: