Last active
February 9, 2023 11:30
-
-
Save ddebin/2d7f9947ca8946b9acdda2a8b91012b6 to your computer and use it in GitHub Desktop.
Check Clover[.xml] coverage
This file contains 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
#!/usr/bin/env bash | |
# coverage-check.sh - Check coverage.svg from a clover.xml, xmllint needed. | |
# | |
# Copyright (C) 2022 Damien Debin (<https://github.com/ddebin>) | |
# License: The MIT License <http://opensource.org/licenses/MIT> | |
# | |
# SPDX-License-Identifier: MIT | |
set -e # fail if any commands fails | |
function print_usage { | |
echo "Usage:" | |
echo " $(basename "$0") [CLOVER_XML] [COVERAGE_MIN]" | |
} | |
CLOVER_XML="" | |
COVERAGE_MIN="" | |
while [ $# -gt 0 ]; do | |
if [ "$1" == "--help" ] || [ "$1" == "-h" ]; then | |
print_usage | |
echo | |
echo "Arguments:" | |
echo " CLOVER_XML path to coverage file to read from (defaults to 'clover.xml')" | |
echo " COVERAGE_MIN minimum coverage (percent, defaults to 100)" | |
echo | |
echo "Help options:" | |
echo " -h, --help display this help and exit" | |
exit 0 | |
elif [ -z "$CLOVER_XML" ]; then | |
CLOVER_XML="$1" | |
shift | |
elif [ -z "$COVERAGE_MIN" ]; then | |
COVERAGE_MIN="$1" | |
shift | |
elif [ "$1" != "--" ]; then | |
echo "Unknown option: $1" >&2 | |
exit 1 | |
fi | |
done | |
if [ -z "$(which xmllint)" ]; then | |
echo "Missing required dependency: xmllint" >&2 | |
exit 1 | |
fi | |
if [ -z "$CLOVER_XML" ] && [ ! -e "clover.xml" ]; then | |
print_usage >&2 | |
exit 1 | |
fi | |
CLOVER_XML="${CLOVER_XML:-clover.xml}" | |
if [ ! -e "$CLOVER_XML" ]; then | |
echo "Unable to read coverage from '$CLOVER_XML': No such file or directory" >&2 | |
exit 1 | |
elif [ ! -f "$CLOVER_XML" ]; then | |
echo "Unable to read coverage from '$CLOVER_XML': Not a file" >&2 | |
exit 1 | |
elif [ ! -r "$CLOVER_XML" ]; then | |
echo "Unable to read coverage from '$CLOVER_XML': Permission denied" >&2 | |
exit 1 | |
fi | |
COVERAGE_MIN="${COVERAGE_MIN:-100}" | |
ELEMENTS="$(xmllint --xpath 'string(/coverage/project/metrics/@elements)' "$CLOVER_XML")" | |
STATEMENTS="$(xmllint --xpath 'string(/coverage/project/metrics/@statements)' "$CLOVER_XML")" | |
METHODS="$(xmllint --xpath 'string(/coverage/project/metrics/@methods)' "$CLOVER_XML")" | |
COVERED_ELEMENTS="$(xmllint --xpath 'string(/coverage/project/metrics/@coveredelements)' "$CLOVER_XML")" | |
COVERED_STATEMENTS="$(xmllint --xpath 'string(/coverage/project/metrics/@coveredstatements)' "$CLOVER_XML")" | |
COVERED_METHODS="$(xmllint --xpath 'string(/coverage/project/metrics/@coveredmethods)' "$CLOVER_XML")" | |
COVERED_METRICS="$((COVERED_ELEMENTS + COVERED_STATEMENTS + COVERED_METHODS))" | |
TOTAL_METRICS="$((ELEMENTS + STATEMENTS + METHODS))" | |
COVERAGE="$((1000 * COVERED_METRICS / TOTAL_METRICS))" | |
if [ "$((COVERAGE % 10))" != "0" ]; then | |
COVERAGE="$(printf '%.1f\n' "${COVERAGE}e-1")" | |
else | |
COVERAGE="$((COVERAGE / 10))" | |
fi | |
if [ "$COVERAGE" -lt "$COVERAGE_MIN" ]; then | |
echo -e "\033[31mCoverage: $COVERAGE%\033[0m" | |
exit 2 | |
else | |
echo -e "\033[32mCoverage: $COVERAGE%\033[0m" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment