|
| 1 | +#!/bin/bash |
| 2 | +# Copyright (c) 2025, NVIDIA CORPORATION. |
| 3 | + |
| 4 | +usage() { |
| 5 | + echo "Usage: $0 [options] REPORT_FILE" |
| 6 | + echo "" |
| 7 | + echo "Options:" |
| 8 | + echo " -h, --help Show this help message" |
| 9 | + echo " -v, --verbose Show detailed failure information" |
| 10 | + echo " -f, --fail-below VALUE Minimum pass rate threshold [0-100] (default: 0)" |
| 11 | + exit 1 |
| 12 | +} |
| 13 | + |
| 14 | +# Parse command line arguments |
| 15 | +THRESHOLD=0 |
| 16 | +VERBOSE=0 |
| 17 | + |
| 18 | +while [[ $# -gt 0 ]]; do |
| 19 | + case $1 in |
| 20 | + -h|--help) |
| 21 | + usage |
| 22 | + ;; |
| 23 | + -f|--fail-below) |
| 24 | + THRESHOLD="$2" |
| 25 | + shift 2 |
| 26 | + ;; |
| 27 | + -v|--verbose) |
| 28 | + VERBOSE=1 |
| 29 | + shift |
| 30 | + ;; |
| 31 | + *) |
| 32 | + if [ -z "${REPORT_FILE:-}" ]; then |
| 33 | + REPORT_FILE="$1" |
| 34 | + else |
| 35 | + echo "Unknown option: $1" |
| 36 | + usage |
| 37 | + fi |
| 38 | + shift |
| 39 | + ;; |
| 40 | + esac |
| 41 | +done |
| 42 | + |
| 43 | +if [ -z "${REPORT_FILE:-}" ]; then |
| 44 | + echo "Error: No report file specified" |
| 45 | + usage |
| 46 | +fi |
| 47 | + |
| 48 | +# Validate threshold is a number between 0 and 100 |
| 49 | +if ! [[ "$THRESHOLD" =~ ^[0-9]+(\.[0-9]+)?$ ]]; then |
| 50 | + echo "Error: Threshold must be a number" |
| 51 | + exit 1 |
| 52 | +fi |
| 53 | + |
| 54 | +if ! awk -v t="$THRESHOLD" 'BEGIN{exit !(t >= 0 && t <= 100)}'; then |
| 55 | + echo "Error: Threshold must be between 0 and 100" |
| 56 | + exit 1 |
| 57 | +fi |
| 58 | + |
| 59 | +# Extract test statistics using xmllint |
| 60 | +total_tests=$(xmllint --xpath "string(/testsuites/testsuite/@tests)" "${REPORT_FILE}") |
| 61 | +failures=$(xmllint --xpath "string(/testsuites/testsuite/@failures)" "${REPORT_FILE}") |
| 62 | +errors=$(xmllint --xpath "string(/testsuites/testsuite/@errors)" "${REPORT_FILE}") |
| 63 | +skipped=$(xmllint --xpath "string(/testsuites/testsuite/@skipped)" "${REPORT_FILE}") |
| 64 | +time=$(xmllint --xpath "string(/testsuites/testsuite/@time)" "${REPORT_FILE}") |
| 65 | + |
| 66 | +# Calculate passed tests and pass rate using awk |
| 67 | +passed=$((total_tests - failures - errors - skipped)) |
| 68 | +pass_rate=$(awk -v passed="$passed" -v total="$total_tests" 'BEGIN { printf "%.2f", (passed/total) * 100 }') |
| 69 | + |
| 70 | +# Print summary |
| 71 | +echo "Test Summary:" |
| 72 | +echo " Total Tests: ${total_tests}" |
| 73 | +echo " Passed: ${passed}" |
| 74 | +echo " Failed: ${failures}" |
| 75 | +echo " Errors: ${errors}" |
| 76 | +echo " Skipped: ${skipped}" |
| 77 | +echo " Pass Rate: ${pass_rate}%" |
| 78 | +echo " Total Time: ${time}s" |
| 79 | + |
| 80 | +# List failed tests only in verbose mode |
| 81 | +if [ "$((failures + errors))" -gt 0 ] && [ "${VERBOSE}" -eq 1 ]; then |
| 82 | + echo "" |
| 83 | + echo "Failed Tests:" |
| 84 | + xmllint --xpath "//testcase[failure or error]/@name" "${REPORT_FILE}" | tr ' ' '\n' | sed 's/name=//g' | sed 's/"//g' | grep . |
| 85 | +fi |
| 86 | + |
| 87 | +# Check if threshold is nonzero before applying the check. |
| 88 | +if awk -v rate="$pass_rate" -v threshold="$THRESHOLD" 'BEGIN { exit (rate >= threshold) }'; then |
| 89 | + echo "" |
| 90 | + echo "Error: Pass rate ${pass_rate}% is below threshold ${THRESHOLD}%" |
| 91 | + exit 1 |
| 92 | +fi |
| 93 | + |
| 94 | +# In all other cases, return with success code. |
| 95 | +exit 0 |
0 commit comments