|
| 1 | +#!/bin/bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# Script to combine multiple test reports into a single markdown file |
| 5 | +# Usage: ./combine-test-reports.sh |
| 6 | + |
| 7 | +OUTPUT_FILE="combined-test-report.md" |
| 8 | +REPORTS=() |
| 9 | + |
| 10 | +# Find all test report files |
| 11 | +for report in runner-report.md sdk1-report.md; do |
| 12 | + if [ -f "$report" ]; then |
| 13 | + REPORTS+=("$report") |
| 14 | + fi |
| 15 | +done |
| 16 | + |
| 17 | +# Exit if no reports found |
| 18 | +if [ ${#REPORTS[@]} -eq 0 ]; then |
| 19 | + echo "No test reports found. Skipping report generation." |
| 20 | + exit 0 |
| 21 | +fi |
| 22 | + |
| 23 | +# Function to strip LaTeX formatting from pytest-md-report output |
| 24 | +# Converts $$\textcolor{...}{\tt{VALUE}}$$ to just VALUE |
| 25 | +strip_latex() { |
| 26 | + local text="$1" |
| 27 | + # Extract content between \tt{ and }} |
| 28 | + if [[ "$text" =~ \\tt\{([^}]+)\} ]]; then |
| 29 | + echo "${BASH_REMATCH[1]}" |
| 30 | + else |
| 31 | + echo "$text" |
| 32 | + fi |
| 33 | +} |
| 34 | + |
| 35 | +# Function to extract test counts from pytest-md-report markdown table |
| 36 | +extract_test_counts() { |
| 37 | + local report_file=$1 |
| 38 | + local passed=0 |
| 39 | + local failed=0 |
| 40 | + local total=0 |
| 41 | + |
| 42 | + # Find the header row to determine column positions |
| 43 | + local header_line=$(grep -E '^\|.*filepath' "$report_file" | head -1) |
| 44 | + |
| 45 | + if [ -z "$header_line" ]; then |
| 46 | + echo "0:0:0" |
| 47 | + return |
| 48 | + fi |
| 49 | + |
| 50 | + # Extract column names and find positions (strip LaTeX from headers) |
| 51 | + IFS='|' read -ra headers <<< "$header_line" |
| 52 | + local passed_col=-1 |
| 53 | + local failed_col=-1 |
| 54 | + local subtotal_col=-1 |
| 55 | + |
| 56 | + for i in "${!headers[@]}"; do |
| 57 | + local col=$(strip_latex "${headers[$i]}" | tr -d ' ' | tr '[:upper:]' '[:lower:]') |
| 58 | + case "$col" in |
| 59 | + passed) passed_col=$i ;; |
| 60 | + failed) failed_col=$i ;; |
| 61 | + subtotal|sub) subtotal_col=$i ;; |
| 62 | + esac |
| 63 | + done |
| 64 | + |
| 65 | + # Find the TOTAL row (TOTAL appears in first column, not as SUBTOTAL in header) |
| 66 | + local total_line=$(grep -E '^\|.*\\tt\{TOTAL\}' "$report_file" | head -1) |
| 67 | + |
| 68 | + if [ -z "$total_line" ]; then |
| 69 | + echo "0:0:0" |
| 70 | + return |
| 71 | + fi |
| 72 | + |
| 73 | + # Parse the TOTAL row values |
| 74 | + IFS='|' read -ra values <<< "$total_line" |
| 75 | + |
| 76 | + # Extract passed count (strip LaTeX and get number) |
| 77 | + if [ "$passed_col" -ge 0 ] && [ "$passed_col" -lt "${#values[@]}" ]; then |
| 78 | + local clean_value=$(strip_latex "${values[$passed_col]}") |
| 79 | + passed=$(echo "$clean_value" | tr -d ' ' | grep -oE '[0-9]+' | head -1 || echo "0") |
| 80 | + fi |
| 81 | + |
| 82 | + # Extract failed count (strip LaTeX and get number) |
| 83 | + if [ "$failed_col" -ge 0 ] && [ "$failed_col" -lt "${#values[@]}" ]; then |
| 84 | + local clean_value=$(strip_latex "${values[$failed_col]}") |
| 85 | + failed=$(echo "$clean_value" | tr -d ' ' | grep -oE '[0-9]+' | head -1 || echo "0") |
| 86 | + fi |
| 87 | + |
| 88 | + # Extract total from SUBTOTAL column (strip LaTeX and get number) |
| 89 | + if [ "$subtotal_col" -ge 0 ] && [ "$subtotal_col" -lt "${#values[@]}" ]; then |
| 90 | + local clean_value=$(strip_latex "${values[$subtotal_col]}") |
| 91 | + total=$(echo "$clean_value" | tr -d ' ' | grep -oE '[0-9]+' | head -1 || echo "0") |
| 92 | + fi |
| 93 | + |
| 94 | + # If total is still 0, calculate from passed + failed |
| 95 | + if [ "$total" -eq 0 ]; then |
| 96 | + total=$((passed + failed)) |
| 97 | + fi |
| 98 | + |
| 99 | + echo "${total}:${passed}:${failed}" |
| 100 | +} |
| 101 | + |
| 102 | +# Initialize the combined report with collapsed summary |
| 103 | +cat > "$OUTPUT_FILE" << 'EOF' |
| 104 | +# Test Results |
| 105 | +
|
| 106 | +<details open> |
| 107 | +<summary><b>Summary</b></summary> |
| 108 | +
|
| 109 | +EOF |
| 110 | + |
| 111 | +# Extract and display summary for each report |
| 112 | +for report in "${REPORTS[@]}"; do |
| 113 | + report_name=$(basename "$report" .md) |
| 114 | + |
| 115 | + # Convert report name to title case |
| 116 | + if [ "$report_name" = "runner-report" ]; then |
| 117 | + title="Runner Tests" |
| 118 | + elif [ "$report_name" = "sdk1-report" ]; then |
| 119 | + title="SDK1 Tests" |
| 120 | + else |
| 121 | + title="${report_name}" |
| 122 | + fi |
| 123 | + |
| 124 | + # Extract counts |
| 125 | + counts=$(extract_test_counts "$report") |
| 126 | + IFS=':' read -r total passed failed <<< "$counts" |
| 127 | + |
| 128 | + # Determine status icon |
| 129 | + if [ "$failed" -gt 0 ]; then |
| 130 | + status="❌" |
| 131 | + elif [ "$passed" -gt 0 ]; then |
| 132 | + status="✅" |
| 133 | + else |
| 134 | + status="⚠️" |
| 135 | + fi |
| 136 | + |
| 137 | + echo "- ${status} **${title}**: ${passed} passed, ${failed} failed (${total} total)" >> "$OUTPUT_FILE" |
| 138 | +done |
| 139 | + |
| 140 | +cat >> "$OUTPUT_FILE" << 'EOF' |
| 141 | +
|
| 142 | +</details> |
| 143 | +
|
| 144 | +--- |
| 145 | +
|
| 146 | +EOF |
| 147 | + |
| 148 | +# Combine all reports with collapsible sections |
| 149 | +for report in "${REPORTS[@]}"; do |
| 150 | + report_name=$(basename "$report" .md) |
| 151 | + |
| 152 | + # Convert report name to title case |
| 153 | + if [ "$report_name" = "runner-report" ]; then |
| 154 | + title="Runner Tests" |
| 155 | + elif [ "$report_name" = "sdk1-report" ]; then |
| 156 | + title="SDK1 Tests" |
| 157 | + else |
| 158 | + title="${report_name}" |
| 159 | + fi |
| 160 | + |
| 161 | + echo "<details>" >> "$OUTPUT_FILE" |
| 162 | + echo "<summary><b>${title} - Full Report</b></summary>" >> "$OUTPUT_FILE" |
| 163 | + echo "" >> "$OUTPUT_FILE" |
| 164 | + cat "$report" >> "$OUTPUT_FILE" |
| 165 | + echo "" >> "$OUTPUT_FILE" |
| 166 | + echo "</details>" >> "$OUTPUT_FILE" |
| 167 | + echo "" >> "$OUTPUT_FILE" |
| 168 | +done |
| 169 | + |
| 170 | +echo "Combined test report created: $OUTPUT_FILE" |
| 171 | +echo "Included reports: ${REPORTS[*]}" |
0 commit comments