testsAndMisc/meta/scripts/lib/tests/test_coverage_bar.sh
Krzysztof kuhy Rudnicki 46c8f065e8 Wire the shell ratchet into pre-commit, and test the coverage tooling
check_shell_coverage.sh has existed for a while and was never in
.pre-commit-config.yaml, so nothing ran it: a new shell library with no
suite beside it could land freely. Wiring it was unaffordable while the
gate measured a jail-run 100% bar (--all took over 600s). After 578c4a6c
dropped that, --all is 0.31s and the per-file mode this hook uses is
milliseconds, so it goes in.

Also fixes a bug 578c4a6c introduced: the failure path still interpolated
${COVERAGE_BAR}, which that commit deleted, so every rejection printed
"COVERAGE_BAR: unbound variable" and advised a 100% line-coverage bar
that no longer exists. The message now describes what is enforced.

Three libs blocked the wiring -- meta/scripts/lib/shell_coverage_{bar,
case_runner,jail_setup}.sh, the code that BUILDS the jail, and so the one
corner of the repo the jail cannot measure itself. They are pure
file-writers, so they get a suite that calls them against a temp $JAIL
and asserts on what was written: 48 assertions, nothing mounted, no
unshare, no kcov.

Several assertions check that an emitted file contains an UNEXPANDED
${BASH_SOURCE} or $1 -- the whole point of the quoted heredocs the jail
relies on. Written as single-quoted literals that is SC2016 on every
line, so the needles compose the dollar from an exported constant
instead. No suppressions were added.

  check_shell_coverage.sh --all:  3 BLOCK  ->  0 BLOCK
  hook, repo-wide:                0.16s
  new suite:                      48 assertions, green

Proved the ratchet still bites: an untested lib dropped into
mtk_root/lib is refused with "needs: .../tests/run_all.sh". Probe removed.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PJrqKERAo5QUeEHWS5Asgp
2026-08-23 15:51:29 +02:00

64 lines
2.0 KiB
Bash
Executable File

#!/usr/bin/env bash
# is_covered: the shell-coverage ratchet's predicate.
set -uo pipefail
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=./coverage_tool_harness.sh
. "$HERE/coverage_tool_harness.sh"
# shellcheck source=../shell_coverage_bar.sh
. "$HERE/../shell_coverage_bar.sh"
tmp="$(mktemp -d -t covbar-XXXXXX)"
trap 'rm -rf "$tmp"' EXIT
# A lib whose directory has no tests/ at all.
mkdir -p "$tmp/nosuite/lib"
: >"$tmp/nosuite/lib/thing.sh"
if is_covered "$tmp/nosuite/lib/thing.sh"; then
_t_fail "a lib with no tests/ dir is uncovered"
else
_t_pass "a lib with no tests/ dir is uncovered"
fi
# A tests/ directory that exists but has no run_all.sh: still uncovered. The
# suite is identified by its runner, not by the directory.
mkdir -p "$tmp/emptytests/lib/tests"
: >"$tmp/emptytests/lib/thing.sh"
: >"$tmp/emptytests/lib/tests/test_thing.sh"
if is_covered "$tmp/emptytests/lib/thing.sh"; then
_t_fail "a tests/ dir without run_all.sh is uncovered"
else
_t_pass "a tests/ dir without run_all.sh is uncovered"
fi
# The covered case.
mkdir -p "$tmp/good/lib/tests"
: >"$tmp/good/lib/thing.sh"
: >"$tmp/good/lib/tests/run_all.sh"
if is_covered "$tmp/good/lib/thing.sh"; then
_t_pass "a lib beside a tests/run_all.sh is covered"
else
_t_fail "a lib beside a tests/run_all.sh is covered"
fi
# The runner is looked up beside the LIB, not anywhere up the tree: a suite one
# directory up must not cover a nested lib.
mkdir -p "$tmp/nested/lib/tests" "$tmp/nested/lib/inner"
: >"$tmp/nested/lib/tests/run_all.sh"
: >"$tmp/nested/lib/inner/thing.sh"
if is_covered "$tmp/nested/lib/inner/thing.sh"; then
_t_fail "a suite one level up does not cover a nested lib"
else
_t_pass "a suite one level up does not cover a nested lib"
fi
# A run_all.sh that is a DIRECTORY is not a runner.
mkdir -p "$tmp/dirrunner/lib/tests/run_all.sh"
: >"$tmp/dirrunner/lib/thing.sh"
if is_covered "$tmp/dirrunner/lib/thing.sh"; then
_t_fail "a directory named run_all.sh is not a suite"
else
_t_pass "a directory named run_all.sh is not a suite"
fi
_t_summary