Files

82 lines
2.5 KiB
Bash
Executable File

#!/usr/bin/env bash
# Map changed paths (stdin, one per line) to the checks that must run.
# Usage: select.sh pre-commit|pre-push < paths
# Output: one check name per line, sorted and deduplicated. run.sh executes
# the names. HOOKS_TESTS_DIR redirects the tests/<module>*.rs lookup (used by
# the fixture test).
set -euo pipefail
stage=${1:?usage: select.sh pre-commit|pre-push}
case $stage in
pre-commit | pre-push) ;;
*) echo "unknown stage $stage" >&2; exit 2 ;;
esac
tests_dir=${HOOKS_TESTS_DIR:-tests}
out=()
add() { out+=("$@"); }
# Integration test files exercised by a src module. Unknown modules fall back
# to tests/<module>*.rs when such files exist.
integration_for() {
case $1 in
api | mcp | auth | sources | db | telemetry) echo integration ;;
tls) echo tls ;;
config) echo plain ;;
esac
local f
for f in "$tests_dir/$1"*.rs; do
if [ -e "$f" ]; then basename "$f" .rs; fi
done
return 0
}
# Prefix pc: runs at pre-commit (fast), pp: at pre-push (tests, slow lint).
while IFS= read -r p; do
[ -n "$p" ] || continue
case $p in
*.rs) add pc:rust-fmt pc:rules pc:clippy ;;
Cargo.toml | Cargo.lock | rustfmt.toml | clippy.toml) add pc:clippy ;;
esac
case $p in
Cargo.toml | Cargo.lock) add pc:openssl pp:test-all ;;
src/lib.rs | src/main.rs | src/state.rs | src/error.rs | src/server.rs) add pp:test-all ;;
tests/common/*) add pp:test-all ;;
tests/*.rs) add "pp:test-it:$(basename "$p" .rs)" ;;
src/*)
rest=${p#src/}
mod=${rest%%/*}
mod=${mod%.rs}
add "pp:test-unit:$mod"
while IFS= read -r t; do
if [ -n "$t" ]; then add "pp:test-it:$t"; fi
done < <(integration_for "$mod")
;;
migrations/*) add pp:test-unit:db pp:test-it:integration ;;
config.example.toml) add pp:test-unit:config pp:test-it:plain ;;
nix/* | flake.nix | flake.lock | devenv.hooks.nix) add pp:nix-eval ;;
esac
case $p in
dev/hooks/*) add pp:hooks-selftest pc:shell ;;
esac
case $p in
*.toml) add pc:toml ;;
*.yaml | *.yml) add pc:yaml ;;
*.md) add pc:markdown ;;
*.sh) add pc:shell ;;
*.nu) add pc:nushell ;;
*.nix) add pc:nix-parse ;;
esac
add pc:hygiene pc:secrets
done
((${#out[@]})) || exit 0
all=$(printf '%s\n' "${out[@]}" | sort -u)
# test-all subsumes the narrower test selections.
if grep -qx 'pp:test-all' <<<"$all"; then
all=$(grep -vE '^pp:test-(unit|it):' <<<"$all")
fi
if [ "$stage" = pre-commit ]; then pat='^pc:'; else pat='^pp:'; fi
{ grep -E "$pat" <<<"$all" || true; } | sed -E 's/^(pc|pp):/ /; s/^ //'