Files
holocron/tests/run-bats.sh
Defame1297 73393b9d01 fix(scripts): correct shellcheck source directives that resolved to nothing
`tests/run-tests.sh` declared `source=lib/batch-run.sh`, which resolves
to neither the repo root nor the script's own directory. A directive
that does not resolve is silent: it blinds test-vale-wrap.sh's
`sourced_files()` seeding exemption, and shellcheck's own SC1091 is
`info` while .pre-commit-config.yaml pins `--severity=warning`.

Issue #97 names run-bats.sh's `../scripts/lib/batch-run.sh` as the
correct spelling. It is not. Directives resolve against the source-path,
which under pre-commit is the repo root, so `../scripts/...` escapes the
repo and trips SC1091 exactly as `lib/...` does -- verified directly.
The spelling satisfying both shellcheck and `sourced_files()`'s
two-candidate rule is repo-root-relative, matching scripts/install.sh.

Fixes all three: run-tests.sh, run-bats.sh, and check-manifests.sh,
the last unmentioned by the issue. Every directive in the repo now
resolves, which the previous commit's case 27 asserts.

Refs #97

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X7GvKuJfy2WrdBmUttV4DT
2026-08-14 08:03:30 +00:00

116 lines
4.4 KiB
Bash
Executable File

#!/usr/bin/env bash
# Run all bats test files in the repo.
# Usage: bash tests/run-bats.sh
set -euo pipefail
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
BATS="$REPO_ROOT/tests/bats/bin/bats"
if [[ ! -x "$BATS" ]]; then
echo "bats not found at $BATS — initializing submodules..." >&2
git -C "$REPO_ROOT" submodule update --init --recursive
fi
if [[ ! -x "$BATS" ]]; then
echo "Error: bats still not found at $BATS after submodule init" >&2
exit 1
fi
# Collected with a `while read` loop rather than `mapfile` — macOS ships
# /bin/bash 3.2, which has no `mapfile`. Process substitution (not a pipe)
# keeps the loop in this shell so the appends survive. `sort` is still fed
# newline-delimited output, exactly as before. Same convention as
# tests/run-tests.sh.
TEST_FILES=()
while IFS= read -r f; do
TEST_FILES+=("$f")
done < <(
find "$REPO_ROOT" -name "*.bats" \
-not -path "*/tests/bats/*" \
-not -path "*/test_helper/*" \
-not -path "*/.claude/worktrees/*" \
| sort
)
if [[ ${#TEST_FILES[@]} -eq 0 ]]; then
echo "No .bats test files found." >&2
exit 0
fi
# Each file gets its own `bats` process, run concurrently (bounded by core
# count) instead of one `bats` invocation working through all files serially.
# A single test file is still serial internally -- this only overlaps the
# fixed per-process startup cost (git/apm subprocess spawns dominate several
# of these suites) across files, which is where the wall-clock actually goes.
# Output is buffered per file so concurrent TAP streams can't interleave, then
# flushed in stable sorted order once every job has finished.
#
# Dispatch and throttling is scripts/lib/batch-run.sh's batch_run -- shared
# with scripts/sync-plugin-content.sh and tests/run-tests.sh so a batching bug
# fix only needs to land once; see that file for why this is batched rather
# than a rolling `wait -n` pool.
SCRATCH_ROOT="$(mktemp -d)"
trap 'rm -rf "$SCRATCH_ROOT"' EXIT
# Repo-root-relative -- see tests/run-tests.sh for why `../scripts/...` does not
# resolve here despite looking right.
# shellcheck source=scripts/lib/batch-run.sh
source "$REPO_ROOT/scripts/lib/batch-run.sh"
declare -a batch_args=()
i=0
for f in ${TEST_FILES[@]+"${TEST_FILES[@]}"}; do
i=$((i + 1))
cmd="$(printf '%q %q; echo $? >%q' "$BATS" "$f" "$SCRATCH_ROOT/$i.status")"
batch_args+=("$i" "$cmd")
done
batch_run "$SCRATCH_ROOT" ${batch_args[@]+"${batch_args[@]}"}
FAIL=0
TOTAL_OK=0
TOTAL_NOT_OK=0
TOTAL_PLANS=0
i=0
for f in ${TEST_FILES[@]+"${TEST_FILES[@]}"}; do
i=$((i + 1))
rel="${f#"$REPO_ROOT"/}"
echo "=== $rel ==="
cat "$SCRATCH_ROOT/$i.log"
echo ""
file_ok="$(grep -c '^ok ' "$SCRATCH_ROOT/$i.log" || true)"
file_not_ok="$(grep -c '^not ok ' "$SCRATCH_ROOT/$i.log" || true)"
# The TAP plan line (`1..N`). Counted separately from the results because an
# empty-but-valid file emits `1..0` and no result lines at all -- that is a
# file bats really did run, so it has to be distinguishable from a file that
# produced nothing whatsoever.
file_plan="$(grep -c '^1\.\.[0-9]' "$SCRATCH_ROOT/$i.log" || true)"
status="$(cat "$SCRATCH_ROOT/$i.status" 2>/dev/null || echo 1)"
TOTAL_OK=$((TOTAL_OK + file_ok))
TOTAL_NOT_OK=$((TOTAL_NOT_OK + file_not_ok))
TOTAL_PLANS=$((TOTAL_PLANS + file_plan))
if [[ "$file_not_ok" -gt 0 || "$status" -ne 0 ]]; then
FAIL=1
fi
done
# Zero counted tests is never a clean run: files were found (the empty-TEST_FILES
# case exits above), so nothing was executed. Without this, a `bats` that emits
# nothing and exits 0 -- a broken binary, a formatter change, or a wholesale
# `@test` removal -- reports "0 tests, 0 failures" and exits green, silently
# turning a total harness failure into a pass.
#
# The two causes get different messages because they are different problems and
# `1..0` is itself valid TAP: no plan lines at all means bats produced no output
# to parse, while plans present with zero results means bats ran fine and the
# files genuinely declare no tests.
if [[ $((TOTAL_OK + TOTAL_NOT_OK)) -eq 0 ]]; then
if [[ "$TOTAL_PLANS" -eq 0 ]]; then
echo "Error: ${#TEST_FILES[@]} .bats file(s) ran but produced no TAP output at all — the bats harness is broken" >&2
else
echo "Error: ${#TEST_FILES[@]} .bats file(s) declared 0 tests — every @test appears to have been removed" >&2
fi
FAIL=1
fi
echo "$((TOTAL_OK + TOTAL_NOT_OK)) tests, $TOTAL_NOT_OK failures"
exit "$FAIL"