Repository navigation
Expand file tree
/
Copy pathcoverage.sh
More file actions
executable file
·151 lines (136 loc) · 6.57 KB
/
Copy pathcoverage.sh
File metadata and controls
executable file
·151 lines (136 loc) · 6.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env bash
# kcov is Linux + ptrace based, so this runs the suite inside a PINNED kcov Docker image (by digest,
# matching how the e2e job pins its Ubuntu image). The suite needs jq, which the kcov image lacks, so
# we mount a PINNED static jq from the upstream release (the same pin-by-sha256 approach used for the
# linters — no apt, no drift). Because the black-box tests now run the REAL rigforge.sh (via
# RIGFORGE_HOME against a sandbox, not a copy), kcov credits both the sourced and the command-dispatch
# paths to the git files.
#
# Outputs Cobertura XML (coverage/cobertura.xml, paths normalised to repo-relative) for the CI
# patch-coverage gate (diff-cover) plus an HTML report under coverage/.
#
# Usage:
# make coverage # measure + enforce the floor (needs Docker; Linux coverage only)
# COVERAGE_NO_FAIL=1 make coverage # measure + report only, don't fail under the floor
#
set -Eeuo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$ROOT"
# --- Pinned, reproducible toolchain (no apt) ---------------------------------
KCOV_IMAGE="$(sed -n 's/^FROM //p' "$ROOT/tests/coverage/Dockerfile")"
[ -n "$KCOV_IMAGE" ] || {
echo "coverage: cannot read the pinned kcov image from tests/coverage/Dockerfile." >&2
exit 2
}
# Static jq for use INSIDE the kcov (Debian) container — the suite needs it and the image lacks it.
JQ_URL="https://github.com/jqlang/jq/releases/download/jq-1.8.2/jq-linux-amd64"
JQ_SHA256="b1c22172dd303f3be49e935aa56aa48a8b7a46e0bc838b4997d3bb451495870f"
FLOOR_FILE="$ROOT/tests/coverage-floor.txt"
OUT="$ROOT/coverage"
CACHE="$ROOT/.coverage-cache"
rm -rf "$OUT"
mkdir -p "$OUT"
command -v docker >/dev/null 2>&1 || {
echo "coverage: docker is required (kcov is Linux/ptrace based, so the suite runs in a container)." >&2
exit 2
}
# --- Fetch + verify the pinned static jq -------------------------------------
SETUP_START=$SECONDS
WORK="$(mktemp -d)"
CACHE_TMP=""
trap 'rm -rf "$WORK"; [ -z "$CACHE_TMP" ] || rm -f "$CACHE_TMP"' EXIT
JQ_BIN="$WORK/jq"
JQ_RELEASE="${JQ_URL%/*}"
JQ_CACHE="$CACHE/${JQ_RELEASE##*/}-linux-amd64-${JQ_SHA256}"
CACHE_STATE=miss
if [ -e "$JQ_CACHE" ] || [ -L "$JQ_CACHE" ]; then
if [ ! -f "$JQ_CACHE" ] || [ -L "$JQ_CACHE" ]; then
echo "coverage: refusing a non-regular jq cache entry." >&2
exit 1
fi
CACHE_STATE=hit
cp "$JQ_CACHE" "$JQ_BIN"
else
echo "coverage: fetching pinned static jq..."
curl -fsSL "$JQ_URL" -o "$JQ_BIN"
fi
VERIFY_START=$SECONDS
if ! echo "${JQ_SHA256} ${JQ_BIN}" | sha256sum -c - >/dev/null; then
echo "coverage: jq checksum mismatch; remove the invalid cache entry and retry." >&2
exit 1
fi
VERIFY_SECONDS=$((SECONDS - VERIFY_START))
chmod 555 "$JQ_BIN"
if [ "$CACHE_STATE" = miss ]; then
mkdir -p "$CACHE"
CACHE_TMP="$(mktemp "$CACHE/.jq.XXXXXX")"
cp "$JQ_BIN" "$CACHE_TMP"
chmod 444 "$CACHE_TMP"
mv -f "$CACHE_TMP" "$JQ_CACHE"
CACHE_TMP=""
fi
echo "coverage: jq cache=$CACHE_STATE setup_seconds=$((SECONDS - SETUP_START)) verification_seconds=$VERIFY_SECONDS"
git rev-parse --verify HEAD >"$OUT/source-commit.txt"
git ls-files -z --cached --others --exclude-standard |
while IFS= read -r -d '' input; do
input_type=absent
input_digest=-
if [ -L "$input" ]; then
input_type=symlink
input_digest=$(readlink "./$input" | sha256sum)
elif [ -f "$input" ]; then
input_type="file"
[ ! -x "$input" ] || input_type=executable
input_digest=$(sha256sum <"$input")
fi
LC_ALL=C printf '%s %s %q\n' "$input_type" "${input_digest%% *}" "$input"
done | LC_ALL=C sort >"$OUT/source-files.sha256"
# --- Run the suite under kcov ------------------------------------------------
echo "coverage: running tests/run.sh under kcov (this is slower than 'make test')..."
docker run --rm --platform linux/amd64 --security-opt seccomp=unconfined --cap-add SYS_PTRACE \
-v "$ROOT:/src" -v "$JQ_BIN:/usr/local/bin/jq:ro" -w /src -e RIGFORGE_TEST_OVERRIDES=1 \
--entrypoint kcov "$KCOV_IMAGE" \
--include-pattern=/src/rigforge.sh,/src/util/proposed-grub.sh,/src/util/config-apply-state.sh,/src/util/randomx.sh,/src/util/benchmark.sh,/src/util/stress.sh,/src/util/control-guards.sh,/src/util/xmrig-overlay.sh \
--clean /src/coverage ./tests/run.sh
# kcov writes root-owned files onto the bind mount; hand them back to the caller so later steps and
# workspace cleanup can read/remove them.
if [ "$(id -u)" -ne 0 ]; then
sudo chown -R "$(id -u):$(id -g)" "$OUT" 2>/dev/null || true
fi
# --- Locate the report (the kcov per-executable dir for our run) -------------
COV_JSON="$(find "$OUT" -name coverage.json -not -path '*kcov-merged*' | head -1)"
COB_SRC="$(find "$OUT" -name cobertura.xml -not -path '*kcov-merged*' | head -1)"
[ -n "$COV_JSON" ] && [ -n "$COB_SRC" ] || {
echo "coverage: kcov produced no report — see the suite output above." >&2
exit 1
}
# Guard against the silent-zero failure mode (e.g. wrong invocation form): if kcov recorded nothing for
# our files, fail loudly rather than passing a meaningless 0%.
TOTAL_LINES="$("$JQ_BIN" -r '.total_lines' "$COV_JSON")"
if [ "${TOTAL_LINES:-0}" -le 0 ] 2>/dev/null; then
echo "coverage: kcov recorded 0 instrumented lines for rigforge.sh/util — coverage not collected." >&2
echo " (check that the black-box tests run \$SCRIPT and the include-pattern paths match.)" >&2
exit 1
fi
# Normalise the Cobertura paths to repo-relative (strip the /src container prefix) so diff-cover, run on
# the host against origin/main, matches them to the git files.
mkdir -p "$OUT"
sed -e 's#/src/##g' -e 's#<source>/src</source>#<source>.</source>#g' "$COB_SRC" >"$OUT/cobertura.xml"
# --- Report + enforce the floor ----------------------------------------------
PCT="$("$JQ_BIN" -r '.percent_covered' "$COV_JSON")"
FLOOR="$(tr -d '[:space:]' <"$FLOOR_FILE" 2>/dev/null || echo 0)"
echo ""
echo " rigforge.sh + shell helpers coverage"
"$JQ_BIN" -r '.files[] | " \(.covered_lines)/\(.total_lines)\t\(.percent_covered)%\t\(.file)"' "$COV_JSON"
echo " ------------------------------------------------------------"
printf ' TOTAL: %s%% (floor: %s%%)\n\n' "$PCT" "$FLOOR"
if [ "${COVERAGE_NO_FAIL:-0}" = "1" ]; then
echo "coverage: report-only (COVERAGE_NO_FAIL=1) — floor not enforced."
exit 0
fi
if awk -v p="$PCT" -v f="$FLOOR" 'BEGIN { exit !(p + 0 >= f + 0) }'; then
echo "COVERAGE: PASS — ${PCT}% >= floor ${FLOOR}%."
else
echo "COVERAGE: FAIL — ${PCT}% < floor ${FLOOR}%. Add tests, or lower the floor in tests/coverage-floor.txt if intentional." >&2
exit 1
fi