Files
mtusi2026lab3/scripts/coverage.sh
T

81 lines
3.1 KiB
Bash
Executable File

#!/bin/sh
# Measure unit and real-process coverage, enforcing 90% separately for each Go source file.
set -eu
cd "$(dirname "$0")/.."
GO=${GO:-go}
: "${DATABASE_URL:?Use an isolated test database}" "${VALKEY_SHARDS:?Use test shards}"
: "${TEST_DATABASE_URL:?Use an isolated test database}" "${TEST_VALKEY_ADDRESS:?Use a test shard}"
export APP_PORT=${COVERAGE_PORT:-18080}
export LOG_LEVEL=error
server_pid=
watchdog_pid=
mkdir -p coverage
run_dir=$(mktemp -d "$PWD/coverage/run.XXXXXX")
cleanup() {
if [ -n "$server_pid" ]; then kill "$server_pid" 2>/dev/null || true; wait "$server_pid" 2>/dev/null || true; fi
if [ -n "$watchdog_pid" ]; then kill "$watchdog_pid" 2>/dev/null || true; wait "$watchdog_pid" 2>/dev/null || true; fi
rm -rf "$run_dir"
}
trap cleanup EXIT
trap 'exit 129' HUP
trap 'exit 130' INT
trap 'exit 143' TERM
mkdir "$run_dir/process"
"$GO" build -race -cover -covermode=atomic -coverpkg=./... -o "$run_dir/server" ./cmd/server
"$GO" test -race -coverpkg=./... -coverprofile="$run_dir/unit.out" ./...
export GOCOVERDIR="$run_dir/process"
if "$run_dir/server" unknown >"$run_dir/server-invalid.log" 2>&1; then
echo 'server accepted an unknown command' >&2
exit 1
else
test "$?" -eq 1
fi
"$run_dir/server" >"$run_dir/server.log" 2>&1 &
server_pid=$!
ready=false
for attempt in $(seq 1 60); do
kill -0 "$server_pid" 2>/dev/null || { cat "$run_dir/server.log"; exit 1; }
if curl --fail --silent --max-time 1 "http://localhost:$APP_PORT/health/ready" >/dev/null; then ready=true; break; fi
sleep .25
done
$ready || { cat "$run_dir/server.log"; exit 1; }
"$run_dir/server" healthcheck
"${PYTHON:-python3}" tests/smoke.py --base-url "http://localhost:$APP_PORT"
kill -TERM "$server_pid"
# Bound the real-process shutdown check; a forced kill makes wait fail.
(sleep 12; kill -KILL "$server_pid" 2>/dev/null || true) &
watchdog_pid=$!
wait "$server_pid"
server_pid=
kill "$watchdog_pid" 2>/dev/null || true
wait "$watchdog_pid" 2>/dev/null || true
watchdog_pid=
unset GOCOVERDIR
"$GO" tool covdata textfmt -i="$run_dir/process" -o="$run_dir/process.out"
awk '
/^mode:/ { if ($0 != "mode: atomic") exit 1; next }
{
if (($1 in statements) && statements[$1] != $2) exit 1;
statements[$1] = $2; hits[$1] += $3
}
END {
print "mode: atomic";
for (block in statements) print block, statements[block], hits[block]
}' "$run_dir/unit.out" "$run_dir/process.out" > coverage/coverage.out
"$GO" list -f '{{.ImportPath}} {{.Dir}} {{join .GoFiles ","}}' ./... > "$run_dir/packages.txt"
: > "$run_dir/inventory.tsv"
while read -r import_path directory names; do
old_ifs=$IFS
IFS=,
for name in $names; do
checksum=$(shasum -a 256 "$directory/$name")
checksum=${checksum%% *}
relative=${directory#"$PWD/"}/$name
executable=$(awk '/^func[ (]/ {found=1} END {print found+0}' "$directory/$name")
printf '%s\t%s\t%s\t%s\n' "$import_path/$name" "$relative" "$checksum" "$executable" >> "$run_dir/inventory.tsv"
done
IFS=$old_ifs
done < "$run_dir/packages.txt"
awk -v summary=coverage/summary.json -f scripts/coverage.awk "$run_dir/inventory.tsv" coverage/coverage.out
"$GO" version > coverage/go-version.txt