2024-12-02 01:16:21 +00:00
|
|
|
#!/usr/bin/env bash
|
2019-11-21 18:43:28 +00:00
|
|
|
|
2024-12-02 01:16:21 +00:00
|
|
|
### shfmt -w -s -ci -sr -kp -fn tests/unit/collect-test-logs.sh
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
# Collects test logs and generates an HTML report
|
|
|
|
# of failed unit tests with links to backtraces
|
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
# Configuration with default value
|
|
|
|
LOG_DIR="./logs"
|
|
|
|
PRINT_TO_CONSOLE=0
|
|
|
|
|
|
|
|
# Parse command line arguments
|
|
|
|
while [[ $# -gt 0 ]]; do
|
|
|
|
case $1 in
|
|
|
|
-p | --print)
|
|
|
|
if ! command -v html2text > /dev/null 2>&1; then
|
|
|
|
echo "Error: html2text is required for printing HTML contents"
|
|
|
|
echo "Please install html2text and try again"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
PRINT_TO_CONSOLE=1
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
-d | --dir)
|
|
|
|
if [ -z "$2" ]; then
|
|
|
|
echo "Error: Log directory path is required for -d|--dir option"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
LOG_DIR="$2"
|
|
|
|
shift 2
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "Unknown option: $1"
|
|
|
|
echo "Usage: $0 [-p|--print] [-d|--dir DIR]"
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
# Initialize HTML
|
2019-11-21 18:43:28 +00:00
|
|
|
echo "Collecting test logs"
|
2024-12-02 01:16:21 +00:00
|
|
|
html="<html><head><title>Failed Unit Tests Report</title></head><body><h3>There are failed unit-tests:</h3><table>"
|
|
|
|
|
|
|
|
# Find all HTML log files and sort them
|
|
|
|
logs=$(find "$LOG_DIR" -type f -iname "*.html" -print | sort)
|
2019-11-21 18:43:28 +00:00
|
|
|
logs_found=0
|
2020-01-18 20:05:25 +00:00
|
|
|
olddirname=""
|
2024-12-02 01:16:21 +00:00
|
|
|
|
|
|
|
# Process each log file
|
|
|
|
for name in $logs; do
|
|
|
|
# Extract test information
|
|
|
|
logname=$(basename "$name")
|
|
|
|
testname=$(echo "$logname" | awk -F 'log_run-tests_' '{print $2}' | awk -F '.html' '{print $1}')
|
2020-01-18 20:05:25 +00:00
|
|
|
testpath="${testname//!/\/}"
|
2024-12-02 01:16:21 +00:00
|
|
|
dirname=$(dirname "$testpath")
|
|
|
|
test=$(basename "$testpath")
|
|
|
|
|
|
|
|
# Add directory header if it's new
|
2020-01-18 20:05:25 +00:00
|
|
|
if [ "$olddirname" != "$dirname" ]; then
|
2024-12-02 01:16:21 +00:00
|
|
|
html+="<tr align=\"left\"><th><br>$dirname</th></tr>"
|
|
|
|
olddirname=$dirname
|
2020-01-18 20:05:25 +00:00
|
|
|
fi
|
2024-12-02 01:16:21 +00:00
|
|
|
|
|
|
|
# Add test entry
|
|
|
|
html+="<tr align=\"left\"><td><a href=\"$logname\">$test</a>"
|
|
|
|
|
|
|
|
# Check for backtrace
|
2019-12-27 15:25:38 +00:00
|
|
|
backtrace="backtrace_$testname.txt"
|
|
|
|
if test -f "${LOG_DIR}/$backtrace"; then
|
2024-12-02 01:16:21 +00:00
|
|
|
if [ $PRINT_TO_CONSOLE -eq 1 ]; then
|
|
|
|
echo "Core dumped, backtrace:"
|
|
|
|
cat $backtrace
|
|
|
|
echo
|
|
|
|
fi
|
|
|
|
|
2020-01-18 20:05:25 +00:00
|
|
|
html+=". Core dumped, backtrace is available <a href=\"$backtrace\">here</a>"
|
2019-12-27 15:25:38 +00:00
|
|
|
fi
|
2024-12-02 01:16:21 +00:00
|
|
|
|
2019-12-27 15:25:38 +00:00
|
|
|
html+="</td></tr>"
|
2019-11-21 18:43:28 +00:00
|
|
|
logs_found=1
|
2024-12-02 01:16:21 +00:00
|
|
|
|
|
|
|
# Print current log file if requested
|
|
|
|
if [ $PRINT_TO_CONSOLE -eq 1 ]; then
|
|
|
|
echo "=== Contents of $name ==="
|
|
|
|
html2text "$name"
|
|
|
|
echo "=== End of $name ==="
|
|
|
|
echo
|
|
|
|
fi
|
2019-11-21 18:43:28 +00:00
|
|
|
done
|
|
|
|
|
2024-12-02 01:16:21 +00:00
|
|
|
# Generate report if logs were found
|
2019-11-21 18:43:28 +00:00
|
|
|
if [ $logs_found -ne 0 ]; then
|
2024-12-02 01:16:21 +00:00
|
|
|
html+="</table></body></html>"
|
|
|
|
echo "$html" > "$LOG_DIR/artifacts.html"
|
2019-11-21 18:43:28 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
exit 0
|