diff --git a/.github/workflows/pr-risk.yml b/.github/workflows/pr-risk.yml index 1e0db9633..bf7960b75 100644 --- a/.github/workflows/pr-risk.yml +++ b/.github/workflows/pr-risk.yml @@ -45,10 +45,11 @@ jobs: id: nix_help run: | nix run .#default -- --help > help.txt - # Every flake check for the runner's own system (today: hook-timeout- - # backends, checks.nix). `nix flake check` rather than a spelled-out - # `.#checks.x86_64-linux.`: no hardcoded system, and a check added - # to checks.nix is run without a workflow edit. + # A packaging check, not a test: evaluates every flake output (packages, + # apps, devShells, overlay) for every system the flake declares. The + # flake has no `checks`: its one check, hook-timeout-backends, runs + # under Bazel as //tests/hook_timeout_backends:hook_timeout_backends_test + # in bazel.yml's PR-core lane. - name: Run the flake checks run: | nix flake check -L diff --git a/.github/workflows/update-vendor-hash.yml b/.github/workflows/update-vendor-hash.yml index 672457fb1..16b841a59 100644 --- a/.github/workflows/update-vendor-hash.yml +++ b/.github/workflows/update-vendor-hash.yml @@ -3,8 +3,8 @@ name: Update vendorHash for dependabot Go bumps # Dependabot bumps to go.mod/go.sum invalidate the proxyVendor FOD hash in # default.nix, which fails the required PR Risk `test-nix` gate (F7c, # spec-f7.md §2.4: nix-build.yml itself no longer runs on pull_request, since -# test-nix's `nix run .#default` plus `nix flake check -L` is a superset of -# plain `nix build .#default`). This workflow detects the drift, computes the +# test-nix's `nix run .#default` plus `nix flake check -L`, which evaluates +# every flake output, is a superset of plain `nix build .#default`). This workflow detects the drift, computes the # new hash via scripts/update-nix-vendorhash.sh, and pushes the update back to # the dependabot branch so the gate goes green without a maintainer # round-trip. diff --git a/BUILD.bazel b/BUILD.bazel index 339780621..97db2f488 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -48,6 +48,19 @@ exports_files( visibility = ["//scripts/gitattributespolicy:__pkg__"], ) +# //tests/hook_timeout_backends: the tracked git hooks whose managed section +# it runs against real timeout implementations. +exports_files( + [ + ".githooks/post-checkout", + ".githooks/post-merge", + ".githooks/pre-commit", + ".githooks/pre-push", + ".githooks/prepare-commit-msg", + ], + visibility = ["//tests/hook_timeout_backends:__pkg__"], +) + # //cmd/bd:bd_test: tracked git hooks (generator parity) and the formula # primitive fixtures. filegroup( @@ -383,6 +396,7 @@ filegroup( "//test/conformance:repo_files", "//test/docsync:repo_files", "//test/testmainconvention:repo_files", + "//tests/hook_timeout_backends:repo_files", "//tests/migration:repo_files", "//tests/regression:repo_files", "//tests/upgrade_smoke:repo_files", diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 536005d1e..daca0d8ab 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -368,7 +368,7 @@ docker run --rm -v $(pwd):/workspace -w /workspace nixos/nix \ If the build fails with a `vendorHash` mismatch, run `./scripts/update-nix-vendorhash.sh` to recompute and update `default.nix`, or update it manually with the `got:` hash from the error message and rebuild. -On a PR, this is covered by PR Risk's required `test-nix` job (`.github/workflows/pr-risk.yml`), which runs `nix run .#default -- --help` plus `nix flake check -L` on every PR touching `go.mod`, `go.sum`, `default.nix`, `flake.nix`, or `flake.lock` -- a superset of plain `nix build .#default`. `.github/workflows/nix-build.yml` dropped its own `pull_request` trigger as redundant (F7c, spec-f7.md §2.4) and now only runs `nix build .#default` on push to `main` and on `workflow_dispatch`, so dependabot bumps that invalidate `vendorHash` still fail loudly post-merge instead of silently breaking Nix users on main. For dependabot Go-module bumps specifically, `.github/workflows/update-vendor-hash.yml` runs the same `update-nix-vendorhash.sh` script and pushes the hash bump back to the dependabot branch automatically (note: GitHub does not retrigger `pull_request` workflows for `GITHUB_TOKEN`-authored commits, so a maintainer may need to re-run PR Risk's `test-nix` once after the auto-fix push to mark the gate green). +On a PR, this is covered by PR Risk's required `test-nix` job (`.github/workflows/pr-risk.yml`), which runs `nix run .#default -- --help` plus `nix flake check -L` (an evaluation of every flake output; the flake has no test checks) on every PR touching `go.mod`, `go.sum`, `default.nix`, `flake.nix`, or `flake.lock` -- a superset of plain `nix build .#default`. `.github/workflows/nix-build.yml` dropped its own `pull_request` trigger as redundant (F7c, spec-f7.md §2.4) and now only runs `nix build .#default` on push to `main` and on `workflow_dispatch`, so dependabot bumps that invalidate `vendorHash` still fail loudly post-merge instead of silently breaking Nix users on main. For dependabot Go-module bumps specifically, `.github/workflows/update-vendor-hash.yml` runs the same `update-nix-vendorhash.sh` script and pushes the hash bump back to the dependabot branch automatically (note: GitHub does not retrigger `pull_request` workflows for `GITHUB_TOKEN`-authored commits, so a maintainer may need to re-run PR Risk's `test-nix` once after the auto-fix push to mark the gate green). ### Debugging diff --git a/MODULE.bazel b/MODULE.bazel index 9cd65e6fc..b429ea297 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -319,3 +319,35 @@ filegroup( sha256 = "2852af0cb20a13139b3448992e69b868e50ed0f8a1e5940ee1de9e19a123b613", urls = ["https://go.dev/dl/go1.25.0.linux-amd64.tar.gz"], ) + +# The timeout implementations //tests/hook_timeout_backends runs the tracked +# git hook shims against (formerly checks.nix's hook-timeout-backends, which +# took them from nixpkgs): static x86_64 builds, so the test needs nothing +# from the executor beyond a shell, perl and GNU coreutils. +http_file = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file") + +http_file( + name = "busybox_static", + executable = True, + sha256 = "6e123e7f3202a8c1e9b1f94d8941580a25135382b99e8d3e34fb858bba311348", + urls = ["https://busybox.net/downloads/binaries/1.35.0-x86_64-linux-musl/busybox"], +) + +http_file( + name = "toybox_static", + executable = True, + sha256 = "38e03ffd8aead8ca26833e1e5c90a9ea7250f3719c29d779ebff7aa55c9d2dac", + urls = ["https://landley.net/toybox/downloads/binaries/0.8.15/toybox-x86_64"], +) + +http_archive( + name = "uutils_coreutils", + build_file_content = """exports_files( + ["coreutils"], + visibility = ["//visibility:public"], +) +""", + sha256 = "9b555b01457279bcc3e46753f0ef324fd3d02a0535111200a4632d1297263b54", + strip_prefix = "coreutils-0.12.0-x86_64-unknown-linux-musl", + urls = ["https://github.com/uutils/coreutils/releases/download/0.12.0/coreutils-0.12.0-x86_64-unknown-linux-musl.tar.gz"], +) diff --git a/checks.nix b/checks.nix deleted file mode 100644 index dac064270..000000000 --- a/checks.nix +++ /dev/null @@ -1,262 +0,0 @@ -# Flake checks. Run all with `nix flake check`; one with -# `nix build .#checks.. -L`. -pkgs: -let - inherit (pkgs) lib; - - # hook-timeout-backends: run the tracked hook shims against REAL timeout - # implementations. - # - # cmd/bd/hooks_timeout_process_test.go pins the shim's selection logic with - # scripted helpers, and TestTrackedManagedHookSectionsMatchGenerator holds - # the tracked .githooks/* byte-equal to the generator. This check closes the - # remaining gap — what the helpers on real hosts actually do — by running the - # tracked sections against the binaries themselves. That is where the - # surprises live: uutils prints a different banner (GH#5541); busybox exits - # 143 on expiry, not 124, so allowlisting it as-is would turn every timeout - # into a failed hook; toybox exits 125 from --version. - # - # The matrix is derived from a model of the contract, so a change to the - # allowlist is one line here and the binaries either agree or they do not. - # Reading .githooks/* rather than building bd keeps this at about one - # deadline of wall time and independent of the Go build. - # - # Each case: one hook, one shell (the `sh`s a `#!/usr/bin/env sh` hook meets - # in the wild), a PATH of exactly one timeout implementation — under the - # name `timeout`, or for the multicall ones also under `gtimeout` alone — - # plus optionally perl, BEADS_HOOK_TIMEOUT=1, and a fake `bd` that blocks on a held-open fifo - # until a helper kills it. The fake records the signal that killed it, which - # is how the check asserts the backend and not just "it finished": - # coreutils backend → TERM from timeout, shim reports the deadline, exit 0 - # perl backend → ALRM from perl's alarm, same report, exit 0 - # no backend → the fake returns at once; shim warns it is running - # without a deadline, exit 0 - # A non-zero hook exit blocks the user's push, so every case asserts exit 0. - # No clock is involved: if nothing kills the fake, the harness's own - # `timeout 10` (on its full PATH, not the shim's) fails the case. - - # The identity allowlist in cmd/bd/hooks.go, as data. - allowlisted = [ - "gnu-coreutils" - "uutils-coreutils" - ]; - - implementations = { - gnu-coreutils = pkgs.coreutils; - uutils-coreutils = pkgs.uutils-coreutils-noprefix; - none = null; - } - // lib.optionalAttrs pkgs.stdenv.isLinux { - busybox = pkgs.busybox; - toybox = pkgs.toybox; - }; - - # The shim probes `timeout` first and `gtimeout` second (Homebrew's prefixed - # GNU coreutils on macOS). Whether a helper is accepted under the second name - # depends on what its banner does with argv[0]. The multicall implementations - # dispatch on it and then print the CANONICAL utility name: uutils' - # src/bin/coreutils.rs matches `argv[0].ends_with()`, so `gtimeout` - # (or `mytimeout`) is `timeout` to it and reports "timeout (uutils - # coreutils) " — accepted (measured on 0.8.0, this flake's - # nixos-25.11 pin, and on 0.11.0); busybox and toybox know no `gtimeout` - # applet and fail the probe outright — rejected either way. These variants put each multicall on PATH under the name `gtimeout` - # ONLY, so the second candidate is the one that runs. GNU has no variant: - # nixpkgs' coreutils is a single-binary multicall that refuses a name it does - # not know ("coreutils: unknown program 'gtimeout'"), while Homebrew's - # gtimeout is a separate binary whose banner is the fixed "timeout (GNU - # coreutils) 9.11" — GNU is immune by construction, not by measurement here. - asGtimeout = - impl: pkg: - pkgs.runCommand "${impl}-as-gtimeout" { } '' - mkdir -p "$out/bin" - ln -s ${pkg}/bin/timeout "$out/bin/gtimeout" - ''; - - # What goes on a case's PATH: every implementation as itself, and the - # multicalls once more under the other candidate name. `impl` is what the - # model reasons about; `label` is what the case is called. - installs = lib.concatLists ( - lib.mapAttrsToList ( - impl: pkg: - [ - { - inherit impl; - label = impl; - dirs = lib.optional (pkg != null) "${pkg}/bin"; - } - ] - ++ lib.optional (pkg != null && impl != "gnu-coreutils") { - inherit impl; - label = "${impl}-as-gtimeout"; - dirs = [ "${asGtimeout impl pkg}/bin" ]; - } - ) implementations - ); - - shells = { - dash = "${pkgs.dash}/bin/dash"; - bash = "${pkgs.bash}/bin/bash"; - } - // lib.optionalAttrs pkgs.stdenv.isLinux { - busybox-ash = "${pkgs.busybox}/bin/sh"; - }; - - hooks = [ - "pre-commit" - "post-merge" - "pre-push" - "post-checkout" - "prepare-commit-msg" - ]; - - backendFor = - impl: withPerl: - if lib.elem impl allowlisted then - "coreutils" - else if withPerl then - "perl" - else - "none"; - - # One `run_case` line per point in the matrix, all backgrounded. - cases = lib.concatMap ( - install: - lib.concatMap - ( - withPerl: - lib.concatLists ( - lib.mapAttrsToList ( - shellName: shell: - map (hook: { - name = "${hook}/${shellName}/${install.label}${lib.optionalString withPerl "+perl"}"; - inherit hook shell; - backend = backendFor install.impl withPerl; - path = install.dirs ++ lib.optional withPerl "${pkgs.perl}/bin"; - }) hooks - ) shells - ) - ) - [ - false - true - ] - ) installs; - - caseLine = - c: - "run_case ${ - lib.escapeShellArgs [ - c.name - c.hook - c.backend - c.shell - ] - } ${lib.escapeShellArgs c.path} &\n"; - - trackedHooks = lib.fileset.toSource { - root = ./.; - fileset = ./.githooks; - }; -in -{ - hook-timeout-backends = - pkgs.runCommand "bd-hook-timeout-backends" - { - # Only .githooks/* is an input: nothing else in the repo changes what - # this check measures. - githooks = "${trackedHooks}/.githooks"; - } - '' - # 1. Shims under test: the managed section of each tracked hook, as - # bd installs it into a fresh repo (shebang + section). - shims="$TMPDIR/shims" - mkdir -p "$shims" - for hook in ${lib.escapeShellArgs hooks}; do - { - echo '#!/usr/bin/env sh' - sed -n '/^# --- BEGIN BEADS INTEGRATION/,/^# --- END BEADS INTEGRATION/p' "$githooks/$hook" - } > "$shims/$hook" - grep -q 'bd hooks run' "$shims/$hook" || { echo "no managed section in .githooks/$hook"; exit 1; } - done - - # 2. The fake bd the shim will find on its restricted PATH. It blocks - # until killed and records the signal, re-raising it so it dies the - # way a real bd would (143 / 142), not by a made-up exit code. - fake="$TMPDIR/fakebin" - mkdir -p "$fake" - cat > "$fake/bd" <<'FAKE' - #!/bin/sh - trap 'echo TERM > "$FAKE_BD_SIGNAL"; trap - TERM; kill -TERM $$' TERM - trap 'echo ALRM > "$FAKE_BD_SIGNAL"; trap - ALRM; kill -ALRM $$' ALRM - [ -n "$FAKE_BD_BLOCK" ] && read -r _ - exit 0 - FAKE - chmod +x "$fake/bd" - mkfifo "$TMPDIR/hang" - exec 3<> "$TMPDIR/hang" - - # 3. The matrix. - results="$TMPDIR/results" - mkdir -p "$results" - run_case() { - name=$1 hook=$2 backend=$3 shell=$4 - shift 4 - case_dir="$TMPDIR/cases/$name" - mkdir -p "$case_dir/bin" - ln -s "$shell" "$case_dir/bin/sh" - path="$fake:$case_dir/bin" - for dir in "$@"; do path="$path:$dir"; done - block=1 - [ "$backend" = none ] && block= - signal_file="$case_dir/signal" - # `|| rc=$?`: the builder runs under set -e, and a bare assignment - # from a failing command would end this subshell before it reports. - rc=0 - case_out=$(timeout 10 env PATH="$path" BEADS_HOOK_TIMEOUT=1 \ - FAKE_BD_BLOCK="$block" FAKE_BD_SIGNAL="$signal_file" \ - "$case_dir/bin/sh" "$shims/$hook" origin https://example.invalid 2>&1 <&3) || rc=$? - signal=$(cat "$signal_file" 2>/dev/null || true) - verdict=ok - if [ "$rc" -eq 124 ]; then - # This is the one mode that is intermittent and not reproducible - # locally, so it needs the output most: report it like the branch - # below rather than as a bare string. - verdict="FAIL: nothing killed the fake bd (harness safety net fired), signal='$signal': $case_out" - elif [ "$rc" -ne 0 ]; then - verdict="FAIL: hook exit $rc (would block the push): $case_out" - else - case "$backend" in - coreutils) - case "$case_out" in *"timed out after 1s"*) ;; *) verdict="FAIL: no deadline report: $case_out" ;; esac - [ "$signal" = TERM ] || verdict="FAIL: expected coreutils timeout (TERM), fake saw '$signal'" - ;; - perl) - case "$case_out" in *"timed out after 1s"*) ;; *) verdict="FAIL: no deadline report: $case_out" ;; esac - [ "$signal" = ALRM ] || verdict="FAIL: expected perl alarm (ALRM), fake saw '$signal'" - ;; - none) - case "$case_out" in *"running without timeout"*) ;; *) verdict="FAIL: no unbounded warning: $case_out" ;; esac - ;; - esac - fi - printf '%-52s %-10s %s\n' "$name" "$backend" "$verdict" > "$results/$(echo "$name" | tr / _)" - } - - ${lib.concatMapStrings caseLine cases} - wait - - printf '%-52s %-10s %s\n' CASE BACKEND RESULT - sort "$results"/* - echo - reported=$(ls "$results" | wc -l) - echo "$reported of ${toString (lib.length cases)} cases reported, $(grep -l FAIL "$results"/* | wc -l) failed" - if [ "$reported" -ne ${toString (lib.length cases)} ]; then - echo "some cases never reported a verdict" - exit 1 - fi - if grep -q FAIL "$results"/*; then - exit 1 - fi - touch "$out" - ''; -} diff --git a/engdocs/TESTING.md b/engdocs/TESTING.md index 0dde5d0ee..7a2a62915 100644 --- a/engdocs/TESTING.md +++ b/engdocs/TESTING.md @@ -74,7 +74,7 @@ a unit test: use the real boundary when the defect could live there. | Final gate | `make test` | Once after focused work on Go code is green: the whole test lane, mostly cache hits. | | Another lane's risk | that lane's command from the table above | When the change touches what that lane covers (integration-tagged files, the Dolt server path, embedded Dolt, pure-Go builds). | | Named CI wrapper | `make ci-pr-core` or `make ci-pr-lint` | Run the wrapper whose risk or surface is affected, or use it to reproduce that CI check. Do not run all three routinely for every edit. | -| Hook shims against real timeout implementations | `nix flake check -L` (or `nix build .#checks..hook-timeout-backends -L`) | After changing the hook generator in `cmd/bd/hooks.go` (then `make githooks-regen`) or anything under `.githooks/`. Runs the tracked managed sections against GNU coreutils, uutils, busybox and toybox `timeout` — the multicalls also installed as `gtimeout` alone — with and without Perl, under dash, bash and busybox ash. About one deadline of wall time; needs no Go build. | +| Hook shims against real timeout implementations | `bazel test //tests/hook_timeout_backends:hook_timeout_backends_test` | After changing the hook generator in `cmd/bd/hooks.go` (then `make githooks-regen`) or anything under `.githooks/`. Runs the tracked managed sections against GNU coreutils, uutils, busybox and toybox `timeout` — the multicalls also installed as `gtimeout` alone — with and without Perl, under dash, bash and busybox ash (240 cases). About one deadline of wall time; needs no Go build. The PR-core lane runs it on every PR. | Do not replace the focused loop with repeated full-suite runs. Run the final `make test` once the affected tests are green. For docs-only changes, use diff --git a/flake.nix b/flake.nix index 60a965a66..7bf3bbdfb 100644 --- a/flake.nix +++ b/flake.nix @@ -38,7 +38,6 @@ packages = forAllSystems (import ./packages.nix); - checks = forAllSystems (import ./checks.nix); apps = nixpkgs.lib.genAttrs systems ( system: diff --git a/scripts/ci_f7c_advisory_test.go b/scripts/ci_f7c_advisory_test.go index 4885457cc..a57d31d34 100644 --- a/scripts/ci_f7c_advisory_test.go +++ b/scripts/ci_f7c_advisory_test.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "path/filepath" + "regexp" "sort" "strings" "testing" @@ -58,8 +59,9 @@ func readPushPaths(t *testing.T, file string) []string { // TestNixBuildDropsPullRequestTriggerNotPushOrDispatch pins the one // "delete the pull_request trigger" trigger change in F7c: nix-build.yml's // PR coverage is fully redundant with PR Risk's required test-nix job (which -// runs `nix run .#default` plus `nix flake check -L` on every PR, a superset -// of `nix build .#default`), but push and workflow_dispatch must survive so +// runs `nix run .#default` plus `nix flake check -L`, which evaluates every +// flake output, on every PR: a superset of `nix build .#default`), but push +// and workflow_dispatch must survive so // the plain `nix build` path stays covered post-merge. func TestNixBuildDropsPullRequestTriggerNotPushOrDispatch(t *testing.T) { type nixTriggers struct { @@ -111,6 +113,48 @@ func TestNixBuildDropsPullRequestTriggerNotPushOrDispatch(t *testing.T) { } } +// The flake's one test check (hook-timeout-backends, which only PR Risk's +// test-nix ran) is a Bazel target the PR-core lane runs on every PR, against +// all five tracked hooks and the three pinned static timeout +// implementations; the flake declares no checks, so test-nix's +// `nix flake check` is a packaging check (an evaluation of the outputs), not +// a test run on a GitHub runner. +func TestHookTimeoutBackendsRunUnderBazel(t *testing.T) { + root := sourceRepoRoot(t) + if _, err := os.Stat(filepath.Join(root, "checks.nix")); err == nil { + t.Error("checks.nix is back; hook-timeout-backends runs under Bazel (//tests/hook_timeout_backends)") + } + if flake := readPolicyFile(t, root, "flake.nix"); regexp.MustCompile(`(?m)^\s*checks\s*=`).MatchString(flake) { + t.Error("flake.nix declares checks; test checks run under Bazel, test-nix's nix flake check only evaluates the outputs") + } + rule := bazelRuleBlock(readPolicyFile(t, root, "tests/hook_timeout_backends/BUILD.bazel"), "hook_timeout_backends_test") + for _, want := range []string{ + `srcs = ["hook_timeout_backends_test.sh"],`, + `"$(rootpath @busybox_static//file)",`, + `"$(rootpath @toybox_static//file)",`, + `"$(rootpath @uutils_coreutils//:coreutils)",`, + `"$(rootpath //:.githooks/pre-commit)",`, + `"$(rootpath //:.githooks/post-merge)",`, + `"$(rootpath //:.githooks/pre-push)",`, + `"$(rootpath //:.githooks/post-checkout)",`, + `"$(rootpath //:.githooks/prepare-commit-msg)",`, + `tags = ["pr-core-only"],`, + } { + if !strings.Contains(rule, want) { + t.Errorf("//tests/hook_timeout_backends:hook_timeout_backends_test lacks %q:\n%s", want, rule) + } + } + hooks, err := filepath.Glob(filepath.Join(root, ".githooks", "*")) + if err != nil { + t.Fatal(err) + } + for _, hook := range hooks { + if !strings.Contains(rule, `"$(rootpath //:.githooks/`+filepath.Base(hook)+`)",`) { + t.Errorf("tracked hook .githooks/%s is not in hook_timeout_backends_test's args", filepath.Base(hook)) + } + } +} + func sortedCopy(items []string) []string { out := append([]string(nil), items...) sort.Strings(out) diff --git a/tests/hook_timeout_backends/BUILD.bazel b/tests/hook_timeout_backends/BUILD.bazel new file mode 100644 index 000000000..aef412762 --- /dev/null +++ b/tests/hook_timeout_backends/BUILD.bazel @@ -0,0 +1,60 @@ +load("@rules_shell//shell:sh_test.bzl", "sh_test") + +# The tracked git hook shims (.githooks/*, managed section only) against real +# timeout implementations: GNU coreutils and perl from the executor, and +# static uutils coreutils, busybox and toybox builds pinned in MODULE.bazel, +# under dash, bash and busybox ash. Formerly checks.nix's flake check +# hook-timeout-backends, which only PR Risk's test-nix job ran. See the +# script's header for the matrix and what each case asserts. +# +# Tags: +# pr-core-only: a non-Go sh_test (no Go testcases for the integration +# lane's check_testcases.py); the PR-core lane runs it. +sh_test( + name = "hook_timeout_backends_test", + size = "small", + srcs = ["hook_timeout_backends_test.sh"], + args = [ + "$(rootpath @busybox_static//file)", + "$(rootpath @toybox_static//file)", + "$(rootpath @uutils_coreutils//:coreutils)", + "$(rootpath //:.githooks/pre-commit)", + "$(rootpath //:.githooks/post-merge)", + "$(rootpath //:.githooks/pre-push)", + "$(rootpath //:.githooks/post-checkout)", + "$(rootpath //:.githooks/prepare-commit-msg)", + ], + data = [ + "//:.githooks/post-checkout", + "//:.githooks/post-merge", + "//:.githooks/pre-commit", + "//:.githooks/pre-push", + "//:.githooks/prepare-commit-msg", + "@busybox_static//file", + "@toybox_static//file", + "@uutils_coreutils//:coreutils", + ], + tags = ["pr-core-only"], +) + +# --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- + +filegroup( + name = "repo_files", + srcs = glob( + ["**"], + exclude = [ + "**/*.db", + "**/*.exe", + "**/*.out", + "**/*.prof", + "**/*.pyc", + "**/*.test", + "**/__pycache__/**", + "**/node_modules/**", + ], + ), + visibility = ["//:__pkg__"], +) + +# --- end repo_files --- diff --git a/tests/hook_timeout_backends/hook_timeout_backends_test.sh b/tests/hook_timeout_backends/hook_timeout_backends_test.sh new file mode 100755 index 000000000..115c6a796 --- /dev/null +++ b/tests/hook_timeout_backends/hook_timeout_backends_test.sh @@ -0,0 +1,223 @@ +#!/usr/bin/env bash +# hook-timeout-backends: run the tracked git hook shims against REAL timeout +# implementations (formerly checks.nix's flake check of the same name). +# +# cmd/bd/hooks_timeout_process_test.go pins the shim's selection logic with +# scripted helpers, and TestTrackedManagedHookSectionsMatchGenerator holds the +# tracked .githooks/* byte-equal to the generator. This test closes the +# remaining gap — what the helpers on real hosts actually do — by running the +# tracked sections against the binaries themselves. That is where the +# surprises live: uutils prints a different banner (GH#5541); busybox exits +# 143 on expiry, not 124, so allowlisting it as-is would turn every timeout +# into a failed hook; toybox exits 125 from --version. +# +# Each case: one hook, one shell (the `sh`s a `#!/usr/bin/env sh` hook meets in +# the wild: dash, bash, busybox ash), a PATH of exactly one timeout +# implementation — under the name `timeout`, or for the multicall ones also +# under `gtimeout` alone — plus optionally perl, BEADS_HOOK_TIMEOUT=1, and a +# fake `bd` that blocks on a held-open fifo until a helper kills it. The fake +# records the signal that killed it, which is how the test asserts the backend +# and not just "it finished": +# coreutils backend → TERM from timeout, shim reports the deadline, exit 0 +# perl backend → ALRM from perl's alarm, same report, exit 0 +# no backend → the fake returns at once; shim warns it is running +# without a deadline, exit 0 +# A non-zero hook exit blocks the user's push, so every case asserts exit 0. +# No clock is involved: if nothing kills the fake, the harness's own +# `timeout 10` (GNU, on its full PATH, not the shim's) fails the case. +# +# The multicall implementations dispatch on argv[0] and print the CANONICAL +# utility name: uutils matches `argv[0].ends_with()`, so under +# `gtimeout` it is `timeout` to it and reports "timeout (uutils coreutils) +# " — accepted; busybox and toybox know no `gtimeout` applet and fail +# the probe — rejected either way. GNU has no gtimeout variant: Homebrew's +# gtimeout is a separate binary whose banner is the fixed "timeout (GNU +# coreutils) ", so GNU is immune by construction. +# +# Usage: hook_timeout_backends_test.sh BUSYBOX TOYBOX UUTILS_COREUTILS HOOK... +# (-test.* arguments, which Bazel lane configs append, are ignored) +# (the static binaries come in hermetically, MODULE.bazel; dash, bash, perl and +# GNU coreutils come from the executor, and a missing one fails the test). +set -euo pipefail + +# Lane configs pass Go test flags to every target (--config=prcore's +# --test_arg=-test.short, -test.parallel, ...); they mean nothing here. +args=() +for arg in "$@"; do + [[ "$arg" == -test.* ]] || args+=("$arg") +done +set -- "${args[@]}" + +[[ $# -ge 4 ]] || { echo "usage: $0 BUSYBOX TOYBOX UUTILS HOOK..." >&2; exit 2; } +busybox="$(realpath "$1")" +toybox="$(realpath "$2")" +uutils="$(realpath "$3")" +shift 3 +hook_files=("$@") + +# The identity allowlist in cmd/bd/hooks.go, as data. +allowlisted=" gnu-coreutils uutils-coreutils " + +require_tool() { + local path + path="$(command -v "$1" 2>/dev/null)" || { echo "FAIL: $1 is not on the executor's PATH" >&2; exit 1; } + realpath "$path" +} +dash="$(require_tool dash)" +bash_bin="$(require_tool bash)" +perl="$(require_tool perl)" +gnu_timeout="$(require_tool timeout)" +case "$("$gnu_timeout" --version 2>/dev/null | head -n 1)" in + "timeout (GNU coreutils) "*) ;; + *) echo "FAIL: the executor's timeout is not GNU coreutils: $gnu_timeout" >&2; exit 1 ;; +esac +case "$("$uutils" timeout --version 2>/dev/null | head -n 1)" in + "timeout (uutils coreutils) "*) ;; + *) echo "FAIL: $uutils is not uutils coreutils" >&2; exit 1 ;; +esac + +tmp="${TEST_TMPDIR:-$(mktemp -d)}/htb" +rm -rf "$tmp" +mkdir -p "$tmp" + +# 1. Shims under test: the managed section of each tracked hook, as bd +# installs it into a fresh repo (shebang + section). +shims="$tmp/shims" +mkdir -p "$shims" +hooks=() +for file in "${hook_files[@]}"; do + hook="$(basename "$file")" + hooks+=("$hook") + { + echo '#!/usr/bin/env sh' + sed -n '/^# --- BEGIN BEADS INTEGRATION/,/^# --- END BEADS INTEGRATION/p' "$file" + } > "$shims/$hook" + grep -q 'bd hooks run' "$shims/$hook" || { echo "FAIL: no managed section in .githooks/$hook" >&2; exit 1; } +done +for want in pre-commit post-merge pre-push post-checkout prepare-commit-msg; do + [[ -f "$shims/$want" ]] || { echo "FAIL: .githooks/$want is not in the test's data" >&2; exit 1; } +done + +# 2. The fake bd the shim will find on its restricted PATH. It blocks until +# killed and records the signal, re-raising it so it dies the way a real bd +# would (143 / 142), not by a made-up exit code. +fake="$tmp/fakebin" +mkdir -p "$fake" +cat > "$fake/bd" <<'FAKE' +#!/bin/sh +trap 'echo TERM > "$FAKE_BD_SIGNAL"; trap - TERM; kill -TERM $$' TERM +trap 'echo ALRM > "$FAKE_BD_SIGNAL"; trap - ALRM; kill -ALRM $$' ALRM +[ -n "$FAKE_BD_BLOCK" ] && read -r _ +exit 0 +FAKE +chmod +x "$fake/bd" +mkfifo "$tmp/hang" +exec 3<> "$tmp/hang" + +# 3. The installs: every implementation as itself, and the multicalls once +# more under the other candidate name. "impl" is what the model reasons +# about; "label" is what the case is called. A dir holds exactly the one +# command. +bin_dir() { # bin_dir NAME CMD TARGET: a dir holding CMD -> TARGET + mkdir -p "$tmp/bins/$1" + ln -s "$3" "$tmp/bins/$1/$2" + echo "$tmp/bins/$1" +} +install_impls=() install_labels=() install_dirs=() +add_install() { install_impls+=("$1"); install_labels+=("$2"); install_dirs+=("$3"); } +add_install gnu-coreutils gnu-coreutils "$(bin_dir gnu-coreutils timeout "$gnu_timeout")" +add_install none none "" +for multicall in uutils-coreutils:"$uutils" busybox:"$busybox" toybox:"$toybox"; do + impl="${multicall%%:*}" target="${multicall#*:}" + add_install "$impl" "$impl" "$(bin_dir "$impl" timeout "$target")" + add_install "$impl" "$impl-as-gtimeout" "$(bin_dir "$impl-as-gtimeout" gtimeout "$target")" +done +perl_dir="$(bin_dir perl perl "$perl")" + +shell_names=(dash bash busybox-ash) +shell_paths=("$dash" "$bash_bin" "$busybox") + +backend_for() { # backend_for IMPL WITH_PERL + if [[ "$allowlisted" == *" $1 "* ]]; then + echo coreutils + elif [[ "$2" == 1 ]]; then + echo perl + else + echo none + fi +} + +# 4. The matrix. +results="$tmp/results" +mkdir -p "$results" +run_case() { + local name=$1 hook=$2 backend=$3 shell=$4 + shift 4 + local case_dir="$tmp/cases/$name" + mkdir -p "$case_dir/bin" + ln -s "$shell" "$case_dir/bin/sh" + local path="$fake:$case_dir/bin" dir + for dir in "$@"; do path="$path:$dir"; done + local block=1 + [[ "$backend" == none ]] && block= + local signal_file="$case_dir/signal" rc=0 case_out signal verdict=ok + case_out=$("$gnu_timeout" 10 env PATH="$path" BEADS_HOOK_TIMEOUT=1 \ + FAKE_BD_BLOCK="$block" FAKE_BD_SIGNAL="$signal_file" \ + "$case_dir/bin/sh" "$shims/$hook" origin https://example.invalid 2>&1 <&3) || rc=$? + signal=$(cat "$signal_file" 2>/dev/null || true) + if [[ "$rc" -eq 124 ]]; then + verdict="FAIL: nothing killed the fake bd (harness safety net fired), signal='$signal': $case_out" + elif [[ "$rc" -ne 0 ]]; then + verdict="FAIL: hook exit $rc (would block the push): $case_out" + else + case "$backend" in + coreutils) + [[ "$case_out" == *"timed out after 1s"* ]] || verdict="FAIL: no deadline report: $case_out" + [[ "$signal" == TERM ]] || verdict="FAIL: expected coreutils timeout (TERM), fake saw '$signal'" + ;; + perl) + [[ "$case_out" == *"timed out after 1s"* ]] || verdict="FAIL: no deadline report: $case_out" + [[ "$signal" == ALRM ]] || verdict="FAIL: expected perl alarm (ALRM), fake saw '$signal'" + ;; + none) + [[ "$case_out" == *"running without timeout"* ]] || verdict="FAIL: no unbounded warning: $case_out" + ;; + esac + fi + printf '%-52s %-10s %s\n' "$name" "$backend" "$verdict" > "$results/${name//\//_}" +} + +total=0 +for i in "${!install_impls[@]}"; do + for with_perl in 0 1; do + backend="$(backend_for "${install_impls[$i]}" "$with_perl")" + dirs=() + [[ -n "${install_dirs[$i]}" ]] && dirs+=("${install_dirs[$i]}") + suffix= + if [[ "$with_perl" == 1 ]]; then + dirs+=("$perl_dir") + suffix=+perl + fi + for s in "${!shell_names[@]}"; do + for hook in "${hooks[@]}"; do + run_case "$hook/${shell_names[$s]}/${install_labels[$i]}$suffix" "$hook" "$backend" "${shell_paths[$s]}" ${dirs[@]+"${dirs[@]}"} & + total=$((total + 1)) + done + done + done +done +wait + +printf '%-52s %-10s %s\n' CASE BACKEND RESULT +sort "$results"/* +echo +reported=$(find "$results" -type f | wc -l) +failed=$(grep -l FAIL "$results"/* | wc -l || true) +echo "$reported of $total cases reported, $failed failed" +if [[ "$reported" -ne "$total" ]]; then + echo "FAIL: some cases never reported a verdict" >&2 + exit 1 +fi +if [[ "$failed" -ne 0 ]]; then + exit 1 +fi