Created
February 27, 2023 20:42
-
-
Save Warchant/f88374aac5cf8beec67075db7f5bff1b to your computer and use it in GitHub Desktop.
Script to generate html report for Bazel C/C++ projects on Linux
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package(default_visibility = ["//visibility:public"]) | |
sh_binary( | |
name = "coverage_linux", | |
srcs = ["coverage_linux.sh"], | |
exec_compatible_with = ["@platforms//os:linux"], | |
tags = [ | |
"block-network", | |
"local", | |
"no-cache", | |
], | |
target_compatible_with = ["@platforms//os:linux"], | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -e | |
# parse args | |
usage() { | |
echo "Usage: $0 [-g genhtml] [-l lcov] [-o coverage]" 1>&2; | |
echo " -g Path to genhtml tool" 1>&2; | |
echo " -l Path to lcov tool" 1>&2; | |
echo " -o Output coverage folder" 1>&2; | |
exit 1; | |
} | |
while getopts ":g:l:o:" o; do | |
case "${o}" in | |
g) | |
GENHTML_PATH=${OPTARG} | |
;; | |
p) | |
LCOV_PATH=${OPTARG} | |
;; | |
o) | |
COVERAGE_DIR=${OPTARG} | |
;; | |
*) | |
usage | |
;; | |
esac | |
done | |
shift $((OPTIND-1)) | |
# if variable is not set, hope tool is available in PATH | |
# https://stackoverflow.com/a/28085062/1953079 | |
: "${GENHTML_PATH:=genhtml}" | |
: "${LCOV_PATH:=lcov}" | |
: "${COVERAGE_DIR:=coverage}" | |
if [ -z "$BUILD_WORKSPACE_DIRECTORY" ]; then | |
echo "BUILD_WORKSPACE_DIRECTORY is not defined. " | |
echo "You can run this tool only under `bazel run`" | |
exit 1 | |
fi | |
cd $BUILD_WORKSPACE_DIRECTORY | |
# check if `bazel test` was executed | |
DATFILES=$(find bazel-testlogs/ -type f -name "coverage.dat" -size +0 | wc -l) | |
if (($DATFILES == 0 )); then | |
echo "#####################################" | |
echo "# NO COVERAGE DATA FOUND #" | |
echo "#####################################" | |
echo "Cannot find \"coverage.dat\" under bazel-testlogs dir." | |
echo "Did you run \"bazel test\"?" | |
exit 1 | |
fi | |
TMP=tmp.merged.lcov.info | |
# always cleanup tmp file | |
trap "rm ${TMP}" EXIT | |
# merge all coverage.dat files (one per test) | |
echo "#####################################" | |
echo "# MERGE COVERAGE FROM ALL TESTS #" | |
echo "#####################################" | |
LCOV_INPUT_FILES="" | |
while read FILENAME; do | |
LCOV_INPUT_FILES="$LCOV_INPUT_FILES -a \"$FILENAME\"" | |
done < <( find bazel-testlogs/ -type f -name "coverage.dat" -size +0 ) | |
eval "${LCOV_PATH}" "${LCOV_INPUT_FILES}" -o ${TMP} | |
# generate html report | |
echo "####################################" | |
echo "# GENERATE HTML REPORT #" | |
echo "####################################" | |
eval "${GENHTML_PATH}" -o ${COVERAGE_DIR} ${TMP} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bazel run //:coverage_linux | |
bazel run //:coverage_linux -- -o outputdir -g $genhtml_path -l $lcov_path |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment