-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathparse-test-results.bash
More file actions
executable file
·122 lines (98 loc) · 4.42 KB
/
parse-test-results.bash
File metadata and controls
executable file
·122 lines (98 loc) · 4.42 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env bash
# Parse Maven Surefire/Failsafe test results and generate a summary
# Usage: parse-test-results.sh <test-log-file> <test-type>
# Example: parse-test-results.sh test.log "Unit"
set -e
LOG_FILE="${1}"
TEST_TYPE="${2}" # "Unit" or "Integration"
if [ ! -f "${LOG_FILE}" ]; then
echo "Error: Log file not found: ${LOG_FILE}"
exit 1
fi
# Check for ansi2txt utility and use it if available to strip ANSI color codes
if command -v ansi2txt &> /dev/null; then
LOG_CONTENT=$(ansi2txt < "${LOG_FILE}" | tr -d '\000')
else
echo "Warning: ansi2txt utility not found. Proceeding without ANSI color code stripping." >&2
echo "Install colorized-logs package (Fedora) for better parsing reliability." >&2
LOG_CONTENT=$(tr -d '\000' < "${LOG_FILE}")
fi
# Extract the Results section from the log
RESULTS_SECTION=$(echo "${LOG_CONTENT}" | sed -n '/^\[INFO\] Results:/,/^\[INFO\] --------/p' | tail -n +2)
# Extract test statistics line
STATS_LINE=$(echo "${RESULTS_SECTION}" | grep -E "Tests run:" | tail -n 1)
if [ -z "${STATS_LINE}" ]; then
echo "Error: Could not find test statistics in log file"
exit 1
fi
# Strip the [INFO], [WARNING], or [ERROR] prefix from the stats line
STATS_LINE=$(echo "${STATS_LINE}" | sed 's/^\[\(INFO\|WARNING\|ERROR\)\] //')
# Extract statistics
TESTS_RUN=$(echo "${STATS_LINE}" | sed -n 's/.*Tests run: \([0-9]*\).*/\1/p')
FAILURES=$(echo "${STATS_LINE}" | sed -n 's/.*Failures: \([0-9]*\).*/\1/p')
ERRORS=$(echo "${STATS_LINE}" | sed -n 's/.*Errors: \([0-9]*\).*/\1/p')
SKIPPED=$(echo "${STATS_LINE}" | sed -n 's/.*Skipped: \([0-9]*\).*/\1/p')
FLAKES=$(echo "${STATS_LINE}" | sed -n 's/.*Flakes: \([0-9]*\).*/\1/p')
# Default flakes to 0 if not present
FLAKES=${FLAKES:-0}
if [ "${FAILURES}" -gt 0 ] || [ "${ERRORS}" -gt 0 ]; then
STATUS="failed"
EMOJI="❌"
elif [ "${FLAKES}" -gt 0 ]; then
STATUS="pass with flaky tests"
EMOJI="⚠️"
else
STATUS="pass"
EMOJI="✅"
fi
SUMMARY="${TEST_TYPE} tests ${STATUS} ${EMOJI}\n"
SUMMARY="${SUMMARY}${STATS_LINE}\n"
if [ "${FAILURES}" -gt 0 ] || [ "${ERRORS}" -gt 0 ]; then
SUMMARY="${SUMMARY}\nFailed tests:\n"
FAILURE_SECTION=$(echo "${RESULTS_SECTION}" | sed -n '/^\[ERROR\] Failures:/,/^\[INFO\]$/p')
# Parse failed test names (format: io.cryostat.FooTest.testBar)
# Match lines that look like fully qualified test names (contain at least two dots for package.Class.method)
FAILED_TESTS=$(echo "${FAILURE_SECTION}" | grep -E '^\[ERROR\] [a-zA-Z][a-zA-Z0-9._]*\.[a-zA-Z][a-zA-Z0-9_]*\.[a-zA-Z][a-zA-Z0-9_]*' | sed 's/^\[ERROR\] //')
# Group by class
CURRENT_CLASS=""
while IFS= read -r test_name; do
if [ -n "${test_name}" ]; then
# Extract class name (everything before the last dot)
CLASS_NAME=$(echo "${test_name}" | sed 's/\.[^.]*$//')
# Extract method name (everything after the last dot)
METHOD_NAME=$(echo "${test_name}" | sed 's/.*\.//')
if [ "${CLASS_NAME}" != "${CURRENT_CLASS}" ]; then
SUMMARY="${SUMMARY}- ${CLASS_NAME}\n"
CURRENT_CLASS="${CLASS_NAME}"
fi
SUMMARY="${SUMMARY} - ${METHOD_NAME}\n"
fi
done <<< "${FAILED_TESTS}"
fi
if [ "${FLAKES}" -gt 0 ]; then
SUMMARY="${SUMMARY}\nFlaky tests:\n"
FLAKES_SECTION=$(echo "${RESULTS_SECTION}" | sed -n '/^\[WARNING\] Flakes:/,/^\[INFO\]$/p')
# Match lines that look like fully qualified test names (contain at least two dots for package.Class.method)
FLAKY_TESTS=$(echo "${FLAKES_SECTION}" | grep -E '^\[WARNING\] [a-zA-Z][a-zA-Z0-9._]*\.[a-zA-Z][a-zA-Z0-9_]*\.[a-zA-Z][a-zA-Z0-9_]*' | sed 's/^\[WARNING\] //')
# Group by class
CURRENT_CLASS=""
while IFS= read -r test_name; do
if [ -n "${test_name}" ]; then
# Extract class name (everything before the last dot)
CLASS_NAME=$(echo "${test_name}" | sed 's/\.[^.]*$//')
# Extract method name (everything after the last dot)
METHOD_NAME=$(echo "${test_name}" | sed 's/.*\.//')
if [ "${CLASS_NAME}" != "${CURRENT_CLASS}" ]; then
SUMMARY="${SUMMARY}- ${CLASS_NAME}\n"
CURRENT_CLASS="${CLASS_NAME}"
fi
SUMMARY="${SUMMARY} - ${METHOD_NAME}\n"
fi
done <<< "${FLAKY_TESTS}"
fi
echo -e "${SUMMARY}"
if [ "${FAILURES}" -gt 0 ] || [ "${ERRORS}" -gt 0 ]; then
exit 1
else
exit 0
fi