Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019dpKwZWtahQU4V2NhpVmLt
107 lines
3.9 KiB
Bash
Executable File
107 lines
3.9 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Fixture test for select.sh: given changed paths, which checks run.
|
|
# Also exercises the secrets, rules and hygiene checks on synthetic files.
|
|
set -euo pipefail
|
|
|
|
here=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
|
|
tmp=$(mktemp -d)
|
|
trap 'rm -rf "$tmp"' EXIT
|
|
|
|
# Fake tests dir so the tests/<module>*.rs fallback is deterministic.
|
|
mkdir -p "$tmp/tests"
|
|
touch "$tmp/tests/peers.rs" "$tmp/tests/peers_auth.rs" "$tmp/tests/integration.rs"
|
|
export HOOKS_TESTS_DIR="$tmp/tests"
|
|
|
|
failures=0
|
|
# expect STAGE "paths (space separated)" "expected checks (space separated)"
|
|
expect() {
|
|
local stage=$1 paths=$2 want=$3 got
|
|
# shellcheck disable=SC2086
|
|
got=$(printf '%s\n' $paths | "$here/select.sh" "$stage" | tr '\n' ' ' | sed 's/ $//')
|
|
if [ "$got" != "$want" ]; then
|
|
echo "FAIL [$stage] $paths"
|
|
echo " want: $want"
|
|
echo " got: $got"
|
|
failures=$((failures + 1))
|
|
fi
|
|
}
|
|
|
|
# Docs only: markdown lint and generic hygiene, never Rust.
|
|
expect pre-commit "docs/api.md README.md" "hygiene markdown secrets"
|
|
expect pre-push "docs/api.md README.md" ""
|
|
|
|
# Rust source in a module with its own integration tests.
|
|
expect pre-commit "src/peers/registry.rs" "clippy hygiene rules rust-fmt secrets"
|
|
expect pre-push "src/peers/registry.rs" "test-it:peers test-it:peers_auth test-unit:peers"
|
|
expect pre-push "src/api/docker.rs" "test-it:integration test-unit:api"
|
|
expect pre-push "src/tls.rs" "test-it:tls test-unit:tls"
|
|
|
|
# Cross-cutting files run everything.
|
|
expect pre-push "src/server.rs src/api/docker.rs" "test-all"
|
|
expect pre-push "Cargo.lock" "test-all"
|
|
expect pre-commit "Cargo.toml" "clippy hygiene openssl secrets toml"
|
|
|
|
# Tests, migrations, config.
|
|
expect pre-push "tests/tls.rs" "test-it:tls"
|
|
expect pre-push "migrations/0002_x.sql" "test-it:integration test-unit:db"
|
|
expect pre-push "config.example.toml" "test-it:plain test-unit:config"
|
|
expect pre-commit "config.example.toml" "hygiene secrets toml"
|
|
|
|
# Nix: parse at commit, flake evaluation at push, no Rust.
|
|
expect pre-commit "nix/module.nix" "hygiene nix-parse secrets"
|
|
expect pre-push "nix/module.nix flake.nix" "nix-eval"
|
|
|
|
# Other file types.
|
|
expect pre-commit "dev/otelcol.yaml" "hygiene secrets yaml"
|
|
expect pre-commit "dev/test.nu" "hygiene nushell secrets"
|
|
expect pre-push "dev/hooks/run.sh" "hooks-selftest"
|
|
|
|
# --- check behaviour on synthetic files ---------------------------------
|
|
run() { "$here/run.sh" check "$@" 2>/dev/null; }
|
|
expect_rc() {
|
|
local want=$1 name=$2
|
|
shift 2
|
|
local rc=0
|
|
run "$name" "$@" >/dev/null || rc=$?
|
|
if { [ "$want" = ok ] && [ "$rc" != 0 ]; } || { [ "$want" = bad ] && [ "$rc" = 0 ]; }; then
|
|
echo "FAIL check $name $* (wanted $want, rc=$rc)"
|
|
failures=$((failures + 1))
|
|
fi
|
|
}
|
|
|
|
cd "$tmp"
|
|
# Fake credentials; dev/hooks is exempt from the secret scan.
|
|
printf 'token = "%s"\n' "AKIAQ7XK2M9PD4LZ8RWB" >aws.txt
|
|
printf 'curl -H "Authorization: Bearer %s"\n' "q7Xk2M9pD4lZ8rWb3NvC5tYh1JfGs6Ue" >bearer.sh
|
|
printf -- '-----BEGIN %s KEY-----\n' "RSA PRIVATE" >key.txt
|
|
printf 'hello\n' >clean.txt
|
|
printf 'ok = "secret-scan: allow %s"\n' "Bearer $(printf 'y%.0s' {1..32})" >allowed.txt
|
|
expect_rc bad secrets aws.txt
|
|
expect_rc bad secrets bearer.sh
|
|
expect_rc bad secrets key.txt
|
|
: >server.key
|
|
expect_rc bad secrets server.key
|
|
expect_rc ok secrets clean.txt allowed.txt
|
|
|
|
printf '// WS-%s tag\nfn a() {}\n' "A" >tag.rs
|
|
printf '// see ARCHITECTURE.md\nfn a() {}\n' >notag.rs
|
|
printf 'fn r() { Router::new().route("/units/:name", x); }\n' >colon.rs
|
|
printf 'fn r() { Router::new().route("/units/{name}", x); }\n' >brace.rs
|
|
expect_rc bad rules tag.rs
|
|
expect_rc bad rules colon.rs
|
|
expect_rc ok rules notag.rs brace.rs
|
|
|
|
printf 'a \nb\n' >trail.txt
|
|
printf 'no newline' >noeol.txt
|
|
printf '<<<<<<< HEAD\nx\n' >conflict.txt
|
|
expect_rc bad hygiene trail.txt
|
|
expect_rc bad hygiene noeol.txt
|
|
expect_rc bad hygiene conflict.txt
|
|
expect_rc ok hygiene clean.txt
|
|
|
|
if [ "$failures" -gt 0 ]; then
|
|
echo "$failures failure(s)"
|
|
exit 1
|
|
fi
|
|
echo "hook selection tests passed"
|