From 8c8c3f7cd062c5bc637237f35ada3b06e4f2cda0 Mon Sep 17 00:00:00 2001 From: Frank Date: Tue, 25 Aug 2026 16:18:01 -0700 Subject: [PATCH] Enable focused tests as repository pre-commit hook --- .githooks/pre-commit | 6 ++++++ docs/Architecture/Testing Policy.md | 17 ++++++++++++----- scripts/test-changed.sh | 3 +++ tests/changed_tests.bats | 3 +++ 4 files changed, 24 insertions(+), 5 deletions(-) create mode 100755 .githooks/pre-commit diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100755 index 0000000..f8a941d --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +# Repository pre-commit hook: run only tests selected by staged paths. +set -euo pipefail + +REPO_ROOT="$(git rev-parse --show-toplevel)" +exec "$REPO_ROOT/scripts/test-changed.sh" diff --git a/docs/Architecture/Testing Policy.md b/docs/Architecture/Testing Policy.md index 9b34e01..5ce7826 100644 --- a/docs/Architecture/Testing Policy.md +++ b/docs/Architecture/Testing Policy.md @@ -22,11 +22,18 @@ For normal commits, use the focused wrapper: ``` It reads staged paths with `git diff --cached` and runs only the mapped Bats -files. Use `--list` to inspect the selection without running tests, or -`--paths FILE ...` to preview a mapping. Shared test infrastructure and -unmapped paths fail closed rather than silently skipping coverage. Run the -full suite with `./tests/run.sh` when changing shared test helpers, the test -runner, or when focused coverage is not sufficient. +files. The tracked `.githooks/pre-commit` hook invokes this wrapper. Enable the +repository hook path once per checkout: + +```bash +git config core.hooksPath .githooks +``` + +Use `--list` to inspect the selection without running tests, or `--paths FILE +...` to preview a mapping. Shared test infrastructure and unmapped paths fail +closed rather than silently skipping coverage. Run the full suite with +`./tests/run.sh` when changing shared test helpers, the test runner, or when +focused coverage is not sufficient. ## Red-Green diff --git a/scripts/test-changed.sh b/scripts/test-changed.sh index 31406a0..c2bbd23 100755 --- a/scripts/test-changed.sh +++ b/scripts/test-changed.sh @@ -76,6 +76,9 @@ for path in "${CHANGED[@]}"; do tests/helpers.bash|tests/run.sh) die "$path is shared test infrastructure; run ./tests/run.sh explicitly" ;; + .githooks/*) + add_test tests/changed_tests.bats + ;; dankserver/compose.yaml|dankclaw/compose.yaml) add_test tests/compose_validity.bats add_test tests/env_consistency.bats diff --git a/tests/changed_tests.bats b/tests/changed_tests.bats index 0fc31e7..031618f 100644 --- a/tests/changed_tests.bats +++ b/tests/changed_tests.bats @@ -6,6 +6,9 @@ load helpers @test "changed-test wrapper is executable and uses staged diff by default" { [ -x "$REPO_ROOT/scripts/test-changed.sh" ] + [ -x "$REPO_ROOT/.githooks/pre-commit" ] + run grep -F 'scripts/test-changed.sh' "$REPO_ROOT/.githooks/pre-commit" + [ "$status" -eq 0 ] run grep -F 'git diff --cached' "$REPO_ROOT/scripts/test-changed.sh" [ "$status" -eq 0 ] }