Files
WeKnora/scripts/test-git-hooks.sh
NianJiu 6729e7bf38 fix(ci,hooks): watch swagger specs in app.yml, compile all sandbox test pkgs, and enforce client gofmt in hooks
Why:
1. With docs/docs.go embedding swagger.json via //go:embed instead of codegen,
   editing docs/swagger.{json,yaml} or docs/swag.overrides without touching a
   .go file would bypass .github/workflows/app.yml path filters.
2. Cross-platform localsandbox CI only test-compiled seatbelt on darwin and
   skipped go test -c across the remaining darwin/windows localsandbox packages.
3. Excluding client/** from root pre-push Go checks dropped local gofmt
   enforcement on client/*.go and inherited developer WEKNORA_LOG_FORMAT in
   client/cli hook test runs.

What:
- Add docs/swagger.json, docs/swagger.yaml, and docs/swag.overrides to app.yml
  push and pull_request path triggers.
- Compile test binaries (go test -c) for every ./internal/localsandbox/...
  package on both darwin/arm64 and windows/amd64, and include client/go.sum in
  cli.yml cache-dependency-path.
- Enforce gofmt -l . and WEKNORA_LOG_FORMAT="" in run_client_checks() /
  run_cli_checks(), format staged cli/client Go files in pre-commit, and add
  regression coverage in scripts/test-git-hooks.sh.
2026-11-18 15:53:54 +08:00

92 lines
2.9 KiB
Bash

#!/usr/bin/env bash
# Verify that app checks cover unchanged callers and propagate failures.
set -euo pipefail
repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
fixture="$(mktemp -d)"
trap 'rm -rf "$fixture"' EXIT
mkdir -p "$fixture/scripts/git-hooks" "$fixture/bin"
cp "$repo_root/scripts/git-hooks/common.sh" "$fixture/scripts/git-hooks/"
export HOOK_TEST_LOG="$fixture/go.log"
export LOG_FORMAT="legacy: %msg"
export WEKNORA_LOG_FORMAT="developer: %msg"
cat > "$fixture/bin/go" <<'EOF'
#!/usr/bin/env bash
set -euo pipefail
printf '%s\n' "$*" >> "$HOOK_TEST_LOG"
case "$1" in
list)
[[ "${HOOK_TEST_FAIL:-}" != list ]] || exit 1
printf '%s\n' example/internal/agent/tools example/internal/application/service example/docreader/proto
;;
vet|test)
if [[ "$1" == test && ( -n "${WEKNORA_LOG_FORMAT:-}" || "${WEKNORA_LOG_FORMAT+x}" != x ) ]]; then
echo "App tests inherited the developer log template" >&2
exit 1
fi
[[ "${HOOK_TEST_FAIL:-}" != "$1" ]] || exit 1
;;
*) echo "Unexpected go invocation: $*" >&2; exit 1 ;;
esac
EOF
chmod +x "$fixture/bin/go"
export PATH="$fixture/bin:$PATH"
unset HOOK_SKIP_TEST HOOK_TEST_FAIL
run_checks() {
bash -c 'source "$1"; run_app_vet_test' bash "$fixture/scripts/git-hooks/common.sh"
}
run_checks
cat > "$fixture/expected.log" <<'EOF'
list ./...
vet -tags sqlite_fts5 example/internal/agent/tools example/internal/application/service
test -tags sqlite_fts5 -count=1 example/internal/agent/tools example/internal/application/service
EOF
diff -u "$fixture/expected.log" "$HOOK_TEST_LOG"
# Keep the explicit local opt-out, but it must not skip vet.
: > "$HOOK_TEST_LOG"
HOOK_SKIP_TEST=1 run_checks
head -n 2 "$fixture/expected.log" > "$fixture/skip.log"
diff -u "$fixture/skip.log" "$HOOK_TEST_LOG"
for phase in list vet test; do
: > "$HOOK_TEST_LOG"
if HOOK_TEST_FAIL="$phase" run_checks > "$fixture/failure.log" 2>&1; then
echo "App checks accepted a failing go $phase" >&2
exit 1
fi
case "$phase" in
list) head -n 1 "$fixture/expected.log" ;;
vet) head -n 2 "$fixture/expected.log" ;;
test) cat "$fixture/expected.log" ;;
esac > "$fixture/failure-expected.log"
diff -u "$fixture/failure-expected.log" "$HOOK_TEST_LOG"
done
mkdir -p "$fixture/client"
cat > "$fixture/client/sample.go" <<'EOF'
package client
type Sample struct {
ID string
}
EOF
: > "$HOOK_TEST_LOG"
bash -c 'source "$1"; run_client_checks' bash "$fixture/scripts/git-hooks/common.sh" > /dev/null
cat > "$fixture/client-expected.log" <<'EOF'
vet ./...
test -race -count=1 ./...
EOF
diff -u "$fixture/client-expected.log" "$HOOK_TEST_LOG"
printf 'package client\ntype Bad struct{ ID string }\n' > "$fixture/client/bad.go"
: > "$HOOK_TEST_LOG"
if bash -c 'source "$1"; run_client_checks' bash "$fixture/scripts/git-hooks/common.sh" > "$fixture/client-fail.log" 2>&1; then
echo "run_client_checks accepted unformatted client Go code" >&2
exit 1
fi
echo 'Git hook coverage checks passed'