|
| 1 | +#!/usr/bin/env bash |
| 2 | +# ******************************************************************************* |
| 3 | +# Copyright (c) 2026 Contributors to the Eclipse Foundation |
| 4 | +# |
| 5 | +# See the NOTICE file(s) distributed with this work for additional |
| 6 | +# information regarding copyright ownership. |
| 7 | +# |
| 8 | +# This program and the accompanying materials are made available under the |
| 9 | +# terms of the Apache License Version 2.0 which is available at |
| 10 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# SPDX-License-Identifier: Apache-2.0 |
| 13 | +# ******************************************************************************* |
| 14 | +set -euo pipefail |
| 15 | + |
| 16 | +usage() { |
| 17 | + cat <<'USAGE' |
| 18 | +Generate a combined Rust + Python coverage HTML report. |
| 19 | +
|
| 20 | +Runs 'bazel coverage' for plantuml, validation and manual_analysis, then |
| 21 | +renders a single HTML report via genhtml. |
| 22 | +
|
| 23 | +Usage: |
| 24 | + bazel run //coverage:combined_report -- [options] |
| 25 | +
|
| 26 | +Options: |
| 27 | + --out-dir <path> Output directory for the HTML report. |
| 28 | + Default: <workspace>/coverage-html |
| 29 | + --targets <labels> Space-separated list of Bazel target patterns. |
| 30 | + Default: //plantuml/... //validation/... //manual_analysis/... |
| 31 | + --help Show this help. |
| 32 | +
|
| 33 | +Requirements: |
| 34 | + genhtml and lcov must be available either via the Bazel-managed @lcov_deb |
| 35 | + runfiles (automatic when run via 'bazel run //coverage:combined_report') or |
| 36 | + installed system-wide (apt install lcov). |
| 37 | +USAGE |
| 38 | +} |
| 39 | + |
| 40 | +OUT_DIR="" |
| 41 | +TARGETS="" |
| 42 | + |
| 43 | +while [[ $# -gt 0 ]]; do |
| 44 | + case "$1" in |
| 45 | + --out-dir) |
| 46 | + OUT_DIR="$2"; shift 2 ;; |
| 47 | + --targets) |
| 48 | + TARGETS="$2"; shift 2 ;; |
| 49 | + -h|--help) |
| 50 | + usage; exit 0 ;; |
| 51 | + *) |
| 52 | + echo "Unknown option: $1" >&2 |
| 53 | + usage >&2 |
| 54 | + exit 1 ;; |
| 55 | + esac |
| 56 | +done |
| 57 | + |
| 58 | +# When invoked via 'bazel run', BUILD_WORKSPACE_DIRECTORY is set to the workspace |
| 59 | +# root. We must cd into it before calling nested bazel commands, because Bazel |
| 60 | +# refuses to be invoked from inside its own output tree. |
| 61 | +WORKSPACE_DIR="${BUILD_WORKSPACE_DIRECTORY:-$(bazel info workspace 2>/dev/null)}" |
| 62 | +cd "$WORKSPACE_DIR" |
| 63 | + |
| 64 | +if [[ -z "$OUT_DIR" ]]; then |
| 65 | + OUT_DIR="${WORKSPACE_DIR}/coverage-html" |
| 66 | +fi |
| 67 | + |
| 68 | +if [[ -z "$TARGETS" ]]; then |
| 69 | + TARGETS="//plantuml/... //validation/... //manual_analysis/..." |
| 70 | +fi |
| 71 | + |
| 72 | +# --------------------------------------------------------------------------- |
| 73 | +# Resolve lcov tools: prefer Bazel-managed binaries from @lcov_deb runfiles |
| 74 | +# so that no system lcov installation is required. Fall back to PATH. |
| 75 | +# --------------------------------------------------------------------------- |
| 76 | +_tool_path() { |
| 77 | + local name="$1" |
| 78 | + local found="" |
| 79 | + # Search runfiles for the Bazel-managed binary (works regardless of the |
| 80 | + # canonical repo name Bazel assigns under bzlmod, e.g. +_repo_rules+lcov_deb) |
| 81 | + if [[ -n "${RUNFILES_DIR:-}" ]]; then |
| 82 | + found=$(find "${RUNFILES_DIR}" -path "*/lcov_deb/usr/bin/${name}" -type f 2>/dev/null | head -1) |
| 83 | + fi |
| 84 | + # Fall back to system PATH |
| 85 | + if [[ -z "${found}" ]]; then |
| 86 | + found=$(command -v "${name}" 2>/dev/null || true) |
| 87 | + fi |
| 88 | + echo "${found}" |
| 89 | +} |
| 90 | + |
| 91 | +GENHTML="$(_tool_path genhtml)" |
| 92 | +LCOV="$(_tool_path lcov)" |
| 93 | + |
| 94 | +if [[ -z "$GENHTML" ]]; then |
| 95 | + echo "ERROR: 'genhtml' not found. Run via 'bazel run //coverage:combined_report' or install 'lcov'." >&2 |
| 96 | + exit 1 |
| 97 | +fi |
| 98 | +if [[ -z "$LCOV" ]]; then |
| 99 | + echo "ERROR: 'lcov' not found. Run via 'bazel run //coverage:combined_report' or install 'lcov'." >&2 |
| 100 | + exit 1 |
| 101 | +fi |
| 102 | + |
| 103 | +# When using the Bazel-managed tools, set PERL5LIB so Perl finds lcovutil.pm. |
| 104 | +if [[ -n "${RUNFILES_DIR:-}" ]]; then |
| 105 | + lcov_lib=$(find "${RUNFILES_DIR}" -path "*/lcov_deb/usr/lib/lcov" -type d 2>/dev/null | head -1) |
| 106 | + if [[ -n "${lcov_lib}" ]]; then |
| 107 | + export PERL5LIB="${lcov_lib}${PERL5LIB:+:${PERL5LIB}}" |
| 108 | + fi |
| 109 | +fi |
| 110 | + |
| 111 | +echo "==> Running bazel coverage --config=coverage ${TARGETS}" |
| 112 | +# shellcheck disable=SC2086 # word-splitting of TARGETS is intentional |
| 113 | +bazel coverage --config=coverage $TARGETS |
| 114 | + |
| 115 | +DAT_FILE="$(bazel info output_path)/_coverage/_coverage_report.dat" |
| 116 | + |
| 117 | +if [[ ! -f "$DAT_FILE" ]]; then |
| 118 | + echo "ERROR: Coverage data not found at ${DAT_FILE}" >&2 |
| 119 | + echo " Make sure at least one test ran successfully." >&2 |
| 120 | + exit 1 |
| 121 | +fi |
| 122 | + |
| 123 | +echo "==> Generating HTML report in ${OUT_DIR}" |
| 124 | +mkdir -p "$OUT_DIR" |
| 125 | + |
| 126 | +# Remove files that should not count towards coverage: |
| 127 | +# - external/ : Python files from rules_python internals captured by coverage.py |
| 128 | +# lcov --ignore-errors unused prevents a failure when none of the patterns match. |
| 129 | +FILTERED_DAT="${OUT_DIR}/filtered_coverage.dat" |
| 130 | +"$LCOV" --remove "$DAT_FILE" \ |
| 131 | + "external/*" \ |
| 132 | + --output-file "$FILTERED_DAT" \ |
| 133 | + --ignore-errors unused |
| 134 | + |
| 135 | +"$GENHTML" "$FILTERED_DAT" \ |
| 136 | + --output-directory "$OUT_DIR" \ |
| 137 | + --legend \ |
| 138 | + --title "Combined Rust + Python Coverage" \ |
| 139 | + --rc genhtml_hi_limit=95 \ |
| 140 | + --ignore-errors source |
| 141 | + |
| 142 | +echo "" |
| 143 | +echo "Coverage report: ${OUT_DIR}/index.html" |
0 commit comments