106 lines
3.1 KiB
Bash
Executable File
106 lines
3.1 KiB
Bash
Executable File
#!/bin/sh
|
|
set -u
|
|
|
|
usage() {
|
|
echo "usage: $0 MINIMUM SUMMARY READY_MARKER STATUS" >&2
|
|
exit 2
|
|
}
|
|
|
|
[ "$#" -eq 4 ] || usage
|
|
minimum=$1
|
|
summary=$2
|
|
ready_marker=$3
|
|
status_file=$4
|
|
script_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
|
|
|
|
if ! awk -v candidate="$minimum" 'BEGIN {
|
|
valid = candidate ~ /^[0-9]+([.][0-9]+)?$/ && candidate >= 0 && candidate <= 100
|
|
exit(valid ? 0 : 1)
|
|
}'; then
|
|
echo "coverage minimum must be a number from 0 through 100: $minimum" >&2
|
|
exit 2
|
|
fi
|
|
|
|
mkdir -p "$(dirname -- "$summary")" "$(dirname -- "$ready_marker")" "$(dirname -- "$status_file")"
|
|
rm -f "$summary" "$ready_marker" "$status_file"
|
|
|
|
temporary_status="${status_file}.tmp.$$"
|
|
cleanup() {
|
|
rm -f "$summary" "$temporary_status"
|
|
}
|
|
trap cleanup EXIT HUP INT TERM
|
|
|
|
cargo llvm-cov \
|
|
--workspace \
|
|
--all-targets \
|
|
--all-features \
|
|
--locked \
|
|
--json \
|
|
--summary-only \
|
|
--output-path "$summary"
|
|
coverage_exit=$?
|
|
|
|
if [ ! -s "$summary" ]; then
|
|
diagnostic="Line coverage: unavailable; required minimum: ${minimum}%; reason: cargo llvm-cov exited ${coverage_exit} without a nonempty summary"
|
|
printf '%s\n' "$diagnostic" | tee "$temporary_status"
|
|
mv "$temporary_status" "$status_file"
|
|
echo "Coverage gate: FAIL (measurement unavailable)"
|
|
if [ "$coverage_exit" -ne 0 ]; then
|
|
exit "$coverage_exit"
|
|
fi
|
|
exit 1
|
|
fi
|
|
|
|
measurement=$(python3 - "$summary" <<'PY'
|
|
import json
|
|
import math
|
|
import sys
|
|
|
|
try:
|
|
with open(sys.argv[1], encoding="utf-8") as report:
|
|
value = json.load(report)["data"][0]["totals"]["lines"]["percent"]
|
|
if isinstance(value, bool) or not isinstance(value, (int, float)):
|
|
raise ValueError("line percentage is not numeric")
|
|
value = float(value)
|
|
if not math.isfinite(value) or not 0 <= value <= 100:
|
|
raise ValueError("line percentage is outside 0 through 100")
|
|
print(format(value, ".12g"))
|
|
except (KeyError, IndexError, json.JSONDecodeError, OSError, TypeError, ValueError) as error:
|
|
print(f"invalid cargo llvm-cov summary: {error}", file=sys.stderr)
|
|
sys.exit(2)
|
|
PY
|
|
)
|
|
parse_exit=$?
|
|
if [ "$parse_exit" -ne 0 ]; then
|
|
diagnostic="Line coverage: unavailable; required minimum: ${minimum}%; reason: invalid cargo llvm-cov summary"
|
|
printf '%s\n' "$diagnostic" | tee "$temporary_status"
|
|
mv "$temporary_status" "$status_file"
|
|
echo "Coverage gate: FAIL (measurement unavailable)"
|
|
exit "$parse_exit"
|
|
fi
|
|
|
|
diagnostic="Line coverage: ${measurement}%; required minimum: ${minimum}%"
|
|
printf '%s\n' "$diagnostic" | tee "$temporary_status"
|
|
mv "$temporary_status" "$status_file"
|
|
|
|
if ! "$script_dir/ci-coverage-badges.sh" coverage --value "$measurement"; then
|
|
echo "Coverage gate: FAIL (badge rendering failed)"
|
|
exit 1
|
|
fi
|
|
if ! : >"$ready_marker"; then
|
|
echo "Coverage gate: FAIL (could not create current-build readiness marker)"
|
|
exit 1
|
|
fi
|
|
|
|
if [ "$coverage_exit" -ne 0 ]; then
|
|
echo "Coverage gate: FAIL (cargo llvm-cov exited $coverage_exit)"
|
|
exit "$coverage_exit"
|
|
fi
|
|
if awk -v actual="$measurement" -v required="$minimum" 'BEGIN { exit(actual >= required ? 0 : 1) }'; then
|
|
echo "Coverage gate: PASS"
|
|
exit 0
|
|
fi
|
|
|
|
echo "Coverage gate: FAIL (below required minimum)"
|
|
exit 1
|