* Accept uutils coreutils timeout in generated hooks
The generated hook selects a timeout helper only when its --version
banner starts with "timeout (GNU coreutils) ". uutils coreutils reports
"timeout (uutils coreutils) " with the same command line and the same
exit 124 on expiry, so hosts that ship it (Ubuntu 25.10+, NixOS with
uutils-coreutils-noprefix) silently fell through to the Perl alarm, or
to the unbounded direct run when Perl is absent. Follow-up from #5522,
tracked in #5541.
- Widen the identity allowlist to uutils; the match stays by banner so
Windows timeout.exe is still never probed (#5503).
- Add a backend-selection case using the GNU stub with the uutils
banner: fails on the old shim (selects gtimeout), passes with the
widening.
- Regenerate the managed sections in the tracked .githooks/* so
TestTrackedManagedHookSectionsMatchGenerator stays green.
- Update the hook-timeout passages in docs/reference/git-integration.md
and troubleshooting.md, which described the probe as GNU-only.
Verified on NixOS with uutils 0.10.0 as /run/current-system/sw/bin/timeout:
with Perl removed from PATH and a bd that hangs, the old shim runs
"without timeout" for the full duration; the new shim expires at
BEADS_HOOK_TIMEOUT and continues.
Agent-Signature: claude-code-claude-opus-5-unknown-reasoning on behalf of Tomasz Feliksik
* Regenerate tracked .githooks managed sections with make githooks-regen
TestTrackedManagedHookSectionsMatchGenerator holds the managed section in
each tracked .githooks/* byte-equal to the cmd/bd/hooks.go generator, but
nothing regenerated them: after a generator change a contributor had to
splice the new section in by hand, and the failure only dumped both
texts. The v1.2.0 bump reddened main this way (ce20a64bb), and #6561 shipped
its first revision with the five hooks stale.
- BD_UPDATE_GOLDEN=1 makes the test rewrite the section in place, keeping
everything around the markers (the project's own hook logic) untouched.
Same switch the package's events golden already uses.
- `make githooks-regen` wraps it, next to `corpus-regen`.
- The drift failure now names that command.
- The exactly-one-section check runs before any rewrite.
Verified: drifting one byte inside .githooks/pre-push fails the test with
the hint; `make githooks-regen` restores the file to the same blob SHA;
the test is green again.
Agent-Signature: claude-code-claude-opus-5-unknown-reasoning on behalf of Tomasz Feliksik
* Check the tracked hook shims against real timeout implementations
The Go tests pin the shim's helper selection with scripted stand-ins, so
they can only encode what the author believes each real `timeout` does.
That is where the last two defects lived: uutils prints a banner the
allowlist did not know (#5541), and busybox — which #5541 suggests
adding — exits 143 on expiry rather than 124, so allowlisting it would
turn every deadline into a failed hook and a blocked push.
checks.nix adds a flake check that runs the tracked .githooks/* managed
sections (held equal to the generator by
TestTrackedManagedHookSectionsMatchGenerator) against the binaries
themselves: GNU coreutils, uutils, busybox and toybox `timeout`, each
with and without perl, under dash, bash and busybox ash, for all five
hooks — 150 cases. Expectations come from a model of the contract (the
allowlist as data), so widening it is one line here and the binaries
either agree or they do not.
A fake bd on the shim's restricted PATH blocks on a held-open fifo until
a helper kills it, records the signal and re-raises it, so each case
asserts the backend that ran (TERM from timeout, ALRM from perl's alarm)
and not merely that the shim finished. No clock: a helper that fails to
kill the fake trips the harness's own `timeout 10`. Every case asserts
exit 0, since anything else blocks the user's push. Reading .githooks/*
instead of building bd keeps it at ~1 s and off the Go build's critical
path; CI's test-nix job runs it after its existing help-text smoke test.
Mutation-checked: restoring main's GNU-only sections fails all 30 uutils
cases (15 unbounded, 15 on the perl backend); allowlisting busybox with
a loosened probe fails all 30 busybox cases with hook exit 143.
Agent-Signature: claude-code-claude-opus-5-unknown-reasoning on behalf of Tomasz Feliksik
* Run the hook-timeout check under the gtimeout candidate name too
The shim probes `timeout` and then `gtimeout`, but the matrix only ever
put a binary named `timeout` on PATH, so the second candidate was never
run against a real implementation. Whether a helper is accepted under
that name depends on what its banner does with argv[0].
Measured: the uutils multicall dispatches on the argv[0] SUFFIX
(src/bin/coreutils.rs, `binary_as_util.ends_with(util)`) and then prints
the canonical utility name — invoked as gtimeout it reports
"timeout (uutils coreutils) 0.8.0" (this flake's nixos-25.11 pin; 0.11.0
identical), which the allowlist accepts. busybox and toybox know no
gtimeout applet and fail the probe outright, rejected as before. GNU is
immune by construction: Homebrew's gtimeout is a separate binary whose
banner is the fixed "timeout (GNU coreutils) 9.11"; nixpkgs' coreutils
is a single-binary multicall that refuses a foreign name ("unknown
program 'gtimeout'"), so it gets no variant here.
Each multicall is now also installed on a case PATH as `gtimeout` ALONE
(`installs`, derived from `implementations`; `backendFor` stays keyed on
the implementation, so the model's claim is "uutils is allowlisted
whatever it is called"). 240 cases on Linux, 80 on darwin (no busybox or
toybox there, and no busybox-ash shell). The shadowed
`out=` inside run_case is `case_out` now: it only survived because every
case is a backgrounded subshell, and a de-backgrounded case would have
died on the final `touch "$out"`.
Mutation-checked: installing the variant as `xtimeout` (a name the shim
never probes) fails exactly the 30 uutils-as-gtimeout cases — 15 with
nothing killing the fake bd, 15 on the perl backend (ALRM) instead of
coreutils; restored, 240 of 240 pass. The busybox/toybox variants cannot
distinguish (their expectation is rejection either way); their weight is
that the probe does not blow up on a foreign name.
* Run the flake checks on main too, by nix flake check; docs wording
pr-risk.yml's test-nix job ran the hook-timeout check on PRs only; the
byte-identical job in main.yml never got the step, so post-merge main
never ran it. Both jobs now run `nix flake check -L`: no hardcoded
`x86_64-linux` (the step beside it, `nix run .#default`, was already
system-agnostic), and a check added to checks.nix runs without a
workflow edit. checks.aarch64-darwin and .x86_64-darwin evaluate.
Measured on ubuntu-latest with a cold Nix store (fork run 35700791032,
this tree, 240 cases): `nix flake check -L` 3.6 s including substitution
of uutils, busybox, toybox, dash and perl; 0.08 s warm — beside the
4 min 9 s the existing `nix run .#default` step spends building bd in
the same job. The 10-minute timeout is not in question.
git-integration.md: on Git for Windows the preferred deadline backend is
"a coreutils timeout (GNU or uutils)", not GNU alone, since both now
qualify. troubleshooting.md: reflow a ragged paragraph.
* review: close the duplicated contracts around the uutils timeout accept
Maintainer review fixes for #6561. No change to the shim's behavior or to the
generated/tracked hook sections.
- cmd/bd/hooks_timeout_process_test.go: the uutils fixture was built with
strings.Replace, which returns its input unchanged when the pattern is
absent, so a later edit to the GNU stub's banner would silently degenerate
the uutils case into a second copy of the GNU one — still green, no longer
testing the new accept. Hoist the derivation and fail loudly when the banner
no longer matches.
- cmd/bd/hooks_timeout_process_test.go: findHookProcessGNUTimeout held a second,
un-widened copy of the identity allowlist, so on exactly the hosts this PR
exists for (uutils timeout, no gtimeout) the only Go test that drives a real
timeout binary skipped with a reason the fix had just made false. Widen the
probe's case, rename it findHookProcessCoreutilsTimeout, and reword the skip
and the subtest. Measured against stub PATHs: uutils goes rc=1 -> rc=0, GNU
stays rc=0, a busybox banner stays rc=1.
- cmd/bd/init_hooks_test.go: TestGenerateHookSection_Timeout asserted only the
GNU alternative, so deleting the new uutils one from the generator left the
structural contract green (verified: it passes under that mutation before
this change, and reports "section missing uutils coreutils identity check"
after). Add the companion assertion.
- cmd/bd/init_hooks_test.go, Makefile: the regen arm rewrites tracked source
files, so give it its own BD_UPDATE_HOOKS_GOLDEN rather than the package's
shared BD_UPDATE_GOLDEN — an unscoped `BD_UPDATE_GOLDEN=1 go test ./cmd/bd/`
used to turn genuine hook drift into a silent working-tree rewrite that
reported PASS.
- cmd/bd/init_hooks_test.go: locate the managed section by marker prefix
instead of the versioned marker line. Tracked hooks drift both from generator
content and from the release version stamp; the exact-line lookup made the
second class fatal ("tracked hook missing ...") before the regen arm was
reachable, so `make githooks-regen` failed at exactly the moment a release
engineer reaches for it. The versioned markers are still verified — they are
part of the byte compare.
- checks.nix: the `rc -eq 124` safety-net verdict discarded both $case_out and
the already-captured $signal, so the one failure mode that is intermittent
and not locally reproducible was also the only one with no diagnostics.
Report them like the `rc -ne 0` branch below it.
- docs/reference/troubleshooting.md, docs/reference/git-integration.md: mirror
the coreutils wording across both docs this PR touches, and generalize the
re-install guidance in both. Managed sections are never reported Outdated
(getHookVersion returns IsShim for them and computeHookStatuses gates on
!IsShim), so nothing tells an existing install it is stale; the sentence was
still scoped to the older name-only check (#5503).
- CHANGELOG.md: add the [Unreleased] entry for #5541, worded to the actual
scope — with Perl installed the shim still reached the Perl alarm arm, so the
lost deadline bit uutils hosts without Perl — say that existing installs must
run `bd hooks install` once, and drop the claim that stock NixOS ships uutils
(this PR's own checks.nix takes GNU from pkgs.coreutils).
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
---------
Co-authored-by: Julian Knutsen <julianknutsen@users.noreply.github.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
738 lines
25 KiB
Go
738 lines
25 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
const hookProcessBDStub = `#!/bin/sh
|
|
printf 'bd-argc=%s\n' "$#"
|
|
_bd_test_index=0
|
|
for _bd_test_arg
|
|
do
|
|
printf 'bd-arg-%s=<%s>\n' "$_bd_test_index" "$_bd_test_arg"
|
|
_bd_test_index=$((_bd_test_index + 1))
|
|
done
|
|
exit "${HOOK_TEST_BD_EXIT:-0}"
|
|
`
|
|
|
|
const hookProcessLongRunningBDStub = `#!/bin/sh
|
|
printf 'long-running-bd-started\n'
|
|
while :; do :; done
|
|
`
|
|
|
|
const hookProcessGNUTimeoutStub = `#!/bin/sh
|
|
if [ "${1-}" = "--version" ]; then
|
|
printf 'timeout (GNU coreutils) 9.99\n'
|
|
exit 0
|
|
fi
|
|
if [ "${1-}" != "--" ] || [ "$#" -lt 6 ]; then
|
|
printf 'invalid-timeout-argv\n' >&2
|
|
exit 96
|
|
fi
|
|
shift
|
|
printf 'helper=timeout\n'
|
|
printf 'duration=<%s>\n' "$1"
|
|
shift
|
|
exec "$@"
|
|
`
|
|
|
|
const hookProcessGNUGtimeoutStub = `#!/bin/sh
|
|
if [ "${1-}" = "--version" ]; then
|
|
printf 'timeout (GNU coreutils) 9.99\n'
|
|
exit 0
|
|
fi
|
|
if [ "${1-}" != "--" ] || [ "$#" -lt 6 ]; then
|
|
printf 'invalid-gtimeout-argv\n' >&2
|
|
exit 96
|
|
fi
|
|
shift
|
|
printf 'helper=gtimeout\n'
|
|
printf 'duration=<%s>\n' "$1"
|
|
shift
|
|
exec "$@"
|
|
`
|
|
|
|
const hookProcessIncompatibleTimeoutStub = `#!/bin/sh
|
|
if [ "${1-}" = "--version" ]; then
|
|
printf 'Microsoft Windows timeout\n'
|
|
exit 1
|
|
fi
|
|
printf 'hostile-timeout-invoked\n' >&2
|
|
exit 97
|
|
`
|
|
|
|
const hookProcessFailedGNUProbeStub = `#!/bin/sh
|
|
if [ "${1-}" = "--version" ]; then
|
|
printf 'timeout (GNU coreutils) 9.99\n'
|
|
exit 23
|
|
fi
|
|
printf 'failed-gnu-probe-invoked\n' >&2
|
|
exit 97
|
|
`
|
|
|
|
const hookProcessIncompatibleGtimeoutStub = `#!/bin/sh
|
|
if [ "${1-}" = "--version" ]; then
|
|
printf 'not GNU coreutils\n'
|
|
exit 0
|
|
fi
|
|
printf 'hostile-gtimeout-invoked\n' >&2
|
|
exit 98
|
|
`
|
|
|
|
const hookProcessPerlStub = `#!/bin/sh
|
|
if [ "${1-}" != "-e" ] || [ "${3-}" != "--" ] || [ "$#" -lt 8 ]; then
|
|
printf 'invalid-perl-argv\n' >&2
|
|
exit 96
|
|
fi
|
|
shift 3
|
|
printf 'helper=perl\n'
|
|
printf 'duration=<%s>\n' "$1"
|
|
shift
|
|
exec "$@"
|
|
`
|
|
|
|
type hookProcessFixture struct {
|
|
name string
|
|
body string
|
|
}
|
|
|
|
type hookProcessCase struct {
|
|
fixtures []hookProcessFixture
|
|
bdBody string
|
|
bdExit int
|
|
shellOption string
|
|
usePOSIXSh bool
|
|
timeout *string
|
|
pathTail string
|
|
args []string
|
|
}
|
|
|
|
type hookProcessResult struct {
|
|
output string
|
|
exitCode int
|
|
elapsed time.Duration
|
|
}
|
|
|
|
func TestGeneratedHookTimeoutProcessBoundary(t *testing.T) {
|
|
t.Run("backend selection", testHookProcessBackendSelection)
|
|
t.Run("positive timeout values", testHookProcessTimeoutValidation)
|
|
t.Run("shell options preserve argv and status", testHookProcessShellOptions)
|
|
t.Run("POSIX sh preserves argv and status", testHookProcessPOSIXShell)
|
|
t.Run("reserved statuses are backend scoped", testHookProcessReservedStatuses)
|
|
t.Run("real coreutils timeout expires a responsive child", testHookProcessRealTimeoutExpiry)
|
|
t.Run("real Perl alarm expires a responsive child", testHookProcessRealPerlExpiry)
|
|
t.Run("Windows checkout rejects System32 timeout", testHookProcessWindowsSystemTimeoutCheckout)
|
|
}
|
|
|
|
func testHookProcessBackendSelection(t *testing.T) {
|
|
// Derive the uutils helper from the GNU stub so the two differ only in the
|
|
// banner, but fail loudly if that banner ever moves: strings.Replace returns
|
|
// its input unchanged when the pattern is absent, which would silently turn
|
|
// the uutils case into a second copy of the GNU one.
|
|
const hookProcessGNUTimeoutBanner = "(GNU coreutils) 9.99"
|
|
uutilsTimeoutStub := strings.Replace(hookProcessGNUTimeoutStub, hookProcessGNUTimeoutBanner, "(uutils coreutils) 0.10.0", 1)
|
|
if uutilsTimeoutStub == hookProcessGNUTimeoutStub {
|
|
t.Fatalf("uutils fixture is a copy of the GNU stub: %q not found in hookProcessGNUTimeoutStub", hookProcessGNUTimeoutBanner)
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
fixtures []hookProcessFixture
|
|
wantHelper string
|
|
wantWarning bool
|
|
}{
|
|
{
|
|
name: "incompatible timeout yields to gtimeout",
|
|
fixtures: []hookProcessFixture{
|
|
{name: "timeout", body: hookProcessIncompatibleTimeoutStub},
|
|
{name: "gtimeout", body: hookProcessGNUGtimeoutStub},
|
|
},
|
|
wantHelper: "helper=gtimeout",
|
|
},
|
|
{
|
|
name: "uutils coreutils timeout is selected like GNU",
|
|
fixtures: []hookProcessFixture{
|
|
{name: "timeout", body: uutilsTimeoutStub},
|
|
{name: "gtimeout", body: hookProcessGNUGtimeoutStub},
|
|
},
|
|
wantHelper: "helper=timeout",
|
|
},
|
|
{
|
|
name: "nonzero GNU-looking probe yields to gtimeout",
|
|
fixtures: []hookProcessFixture{
|
|
{name: "timeout", body: hookProcessFailedGNUProbeStub},
|
|
{name: "gtimeout", body: hookProcessGNUGtimeoutStub},
|
|
},
|
|
wantHelper: "helper=gtimeout",
|
|
},
|
|
{
|
|
name: "Perl follows incompatible timeout commands",
|
|
fixtures: []hookProcessFixture{
|
|
{name: "timeout", body: hookProcessIncompatibleTimeoutStub},
|
|
{name: "gtimeout", body: hookProcessIncompatibleGtimeoutStub},
|
|
{name: "perl", body: hookProcessPerlStub},
|
|
},
|
|
wantHelper: "helper=perl",
|
|
},
|
|
{
|
|
name: "direct fallback is explicit",
|
|
fixtures: []hookProcessFixture{
|
|
{name: "timeout", body: hookProcessIncompatibleTimeoutStub},
|
|
{name: "gtimeout", body: hookProcessIncompatibleGtimeoutStub},
|
|
},
|
|
wantWarning: true,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
timeout := "17"
|
|
args := []string{"remote with space", "https://example.invalid/repo"}
|
|
result := runGeneratedHookProcess(t, hookProcessCase{
|
|
fixtures: tt.fixtures,
|
|
timeout: &timeout,
|
|
args: args,
|
|
})
|
|
if result.exitCode != 0 {
|
|
t.Fatalf("generated hook exit = %d, want 0\n%s", result.exitCode, result.output)
|
|
}
|
|
if tt.wantHelper != "" && !strings.Contains(result.output, tt.wantHelper) {
|
|
t.Errorf("output missing selected helper %q\n%s", tt.wantHelper, result.output)
|
|
}
|
|
if got := strings.Contains(result.output, "running without timeout"); got != tt.wantWarning {
|
|
t.Errorf("direct-fallback warning presence = %v, want %v\n%s", got, tt.wantWarning, result.output)
|
|
}
|
|
for _, forbidden := range []string{
|
|
"hostile-timeout-invoked", "failed-gnu-probe-invoked", "hostile-gtimeout-invoked",
|
|
} {
|
|
if strings.Contains(result.output, forbidden) {
|
|
t.Errorf("incompatible helper was invoked: %s\n%s", forbidden, result.output)
|
|
}
|
|
}
|
|
assertHookProcessBDInvocation(t, result.output, "pre-push", args...)
|
|
})
|
|
}
|
|
}
|
|
|
|
func testHookProcessTimeoutValidation(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
value *string
|
|
wantSeconds string
|
|
wantWarning bool
|
|
}{
|
|
{name: "unset uses default", wantSeconds: strconv.Itoa(hookTimeoutSeconds)},
|
|
{name: "option-looking value", value: hookProcessString("--help"), wantSeconds: strconv.Itoa(hookTimeoutSeconds), wantWarning: true},
|
|
{name: "zero", value: hookProcessString("0"), wantSeconds: strconv.Itoa(hookTimeoutSeconds), wantWarning: true},
|
|
{name: "all-zero digits", value: hookProcessString("000"), wantSeconds: strconv.Itoa(hookTimeoutSeconds), wantWarning: true},
|
|
{name: "mixed value", value: hookProcessString("12s"), wantSeconds: strconv.Itoa(hookTimeoutSeconds), wantWarning: true},
|
|
{name: "positive whole seconds", value: hookProcessString("17"), wantSeconds: "17"},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result := runGeneratedHookProcess(t, hookProcessCase{
|
|
fixtures: []hookProcessFixture{{name: "timeout", body: hookProcessGNUTimeoutStub}},
|
|
timeout: tt.value,
|
|
})
|
|
if result.exitCode != 0 {
|
|
t.Fatalf("generated hook exit = %d, want 0\n%s", result.exitCode, result.output)
|
|
}
|
|
if marker := "duration=<" + tt.wantSeconds + ">"; !strings.Contains(result.output, marker) {
|
|
t.Errorf("output missing effective timeout %q\n%s", marker, result.output)
|
|
}
|
|
if got := strings.Contains(result.output, "invalid BEADS_HOOK_TIMEOUT"); got != tt.wantWarning {
|
|
t.Errorf("invalid-timeout warning presence = %v, want %v\n%s", got, tt.wantWarning, result.output)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func testHookProcessShellOptions(t *testing.T) {
|
|
const wantExit = 37
|
|
args := []string{"argument with space", "second-argument"}
|
|
for _, option := range []string{"", "e", "u", "eu"} {
|
|
name := option
|
|
if name == "" {
|
|
name = "default"
|
|
}
|
|
t.Run("set-"+name, func(t *testing.T) {
|
|
result := runGeneratedHookProcess(t, hookProcessCase{
|
|
fixtures: []hookProcessFixture{{name: "timeout", body: hookProcessGNUTimeoutStub}},
|
|
bdExit: wantExit,
|
|
shellOption: option,
|
|
args: args,
|
|
})
|
|
if result.exitCode != wantExit {
|
|
t.Fatalf("generated hook exit = %d, want bd exit %d\n%s", result.exitCode, wantExit, result.output)
|
|
}
|
|
assertHookProcessBDInvocation(t, result.output, "pre-push", args...)
|
|
})
|
|
}
|
|
}
|
|
|
|
func testHookProcessPOSIXShell(t *testing.T) {
|
|
const wantExit = 37
|
|
args := []string{"argument with space", "second-argument"}
|
|
result := runGeneratedHookProcess(t, hookProcessCase{
|
|
fixtures: []hookProcessFixture{{name: "timeout", body: hookProcessGNUTimeoutStub}},
|
|
bdExit: wantExit,
|
|
shellOption: "eu",
|
|
usePOSIXSh: true,
|
|
args: args,
|
|
})
|
|
if result.exitCode != wantExit {
|
|
t.Fatalf("generated hook exit = %d, want bd exit %d\n%s", result.exitCode, wantExit, result.output)
|
|
}
|
|
assertHookProcessBDInvocation(t, result.output, "pre-push", args...)
|
|
}
|
|
|
|
func testHookProcessReservedStatuses(t *testing.T) {
|
|
gnu := []hookProcessFixture{{name: "timeout", body: hookProcessGNUTimeoutStub}}
|
|
perl := []hookProcessFixture{
|
|
{name: "timeout", body: hookProcessIncompatibleTimeoutStub},
|
|
{name: "gtimeout", body: hookProcessIncompatibleGtimeoutStub},
|
|
{name: "perl", body: hookProcessPerlStub},
|
|
}
|
|
direct := perl[:2]
|
|
tests := []struct {
|
|
name string
|
|
fixtures []hookProcessFixture
|
|
bdExit int
|
|
wantExit int
|
|
wantWarning bool
|
|
wantDBWarning bool
|
|
}{
|
|
{name: "database-not-initialized is skipped", fixtures: gnu, bdExit: 3, wantDBWarning: true},
|
|
{name: "GNU owns 124", fixtures: gnu, bdExit: 124, wantWarning: true},
|
|
{name: "GNU preserves 137", fixtures: gnu, bdExit: 137, wantExit: 137},
|
|
{name: "GNU preserves 142", fixtures: gnu, bdExit: 142, wantExit: 142},
|
|
{name: "Perl preserves 124", fixtures: perl, bdExit: 124, wantExit: 124},
|
|
{name: "Perl owns 142", fixtures: perl, bdExit: 142, wantWarning: true},
|
|
{name: "direct preserves 124", fixtures: direct, bdExit: 124, wantExit: 124},
|
|
{name: "direct preserves 137", fixtures: direct, bdExit: 137, wantExit: 137},
|
|
{name: "direct preserves 142", fixtures: direct, bdExit: 142, wantExit: 142},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result := runGeneratedHookProcess(t, hookProcessCase{fixtures: tt.fixtures, bdExit: tt.bdExit})
|
|
if result.exitCode != tt.wantExit {
|
|
t.Errorf("generated hook exit = %d, want %d\n%s", result.exitCode, tt.wantExit, result.output)
|
|
}
|
|
if got := strings.Contains(result.output, "timed out after"); got != tt.wantWarning {
|
|
t.Errorf("timeout warning presence = %v, want %v\n%s", got, tt.wantWarning, result.output)
|
|
}
|
|
if got := strings.Contains(result.output, "database not initialized"); got != tt.wantDBWarning {
|
|
t.Errorf("database warning presence = %v, want %v\n%s", got, tt.wantDBWarning, result.output)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func testHookProcessRealTimeoutExpiry(t *testing.T) {
|
|
helperName, helperDir := findHookProcessCoreutilsTimeout(t)
|
|
timeout := "1"
|
|
result := runGeneratedHookProcess(t, hookProcessCase{
|
|
bdBody: hookProcessLongRunningBDStub,
|
|
timeout: &timeout,
|
|
pathTail: helperDir,
|
|
})
|
|
if result.exitCode != 0 {
|
|
t.Fatalf("generated hook exit = %d, want normalized timeout success\n%s", result.exitCode, result.output)
|
|
}
|
|
if !strings.Contains(result.output, "long-running-bd-started") || !strings.Contains(result.output, "timed out after 1s") {
|
|
t.Fatalf("%s did not expire and normalize the responsive child\n%s", helperName, result.output)
|
|
}
|
|
maxElapsed := 9 * time.Second
|
|
if result.elapsed > maxElapsed {
|
|
t.Errorf("%s expiry took %s, want at most %s", helperName, result.elapsed, maxElapsed)
|
|
}
|
|
}
|
|
|
|
func testHookProcessRealPerlExpiry(t *testing.T) {
|
|
if runtime.GOOS == "windows" {
|
|
t.Skip("Git for Windows Perl does not reliably preserve alarm across exec; GNU timeout is the normal Git Bash backend")
|
|
}
|
|
perlDir := findHookProcessPerl(t)
|
|
timeout := "1"
|
|
result := runGeneratedHookProcess(t, hookProcessCase{
|
|
fixtures: []hookProcessFixture{
|
|
{name: "timeout", body: hookProcessIncompatibleTimeoutStub},
|
|
{name: "gtimeout", body: hookProcessIncompatibleGtimeoutStub},
|
|
},
|
|
bdBody: hookProcessLongRunningBDStub,
|
|
timeout: &timeout,
|
|
pathTail: perlDir,
|
|
})
|
|
if result.exitCode != 0 {
|
|
t.Fatalf("generated hook exit = %d, want normalized Perl alarm success\n%s", result.exitCode, result.output)
|
|
}
|
|
if !strings.Contains(result.output, "long-running-bd-started") || !strings.Contains(result.output, "timed out after 1s") {
|
|
t.Fatalf("real Perl did not expire and normalize the responsive child\n%s", result.output)
|
|
}
|
|
if result.elapsed > 9*time.Second {
|
|
t.Errorf("Perl expiry took %s, want at most 9s", result.elapsed)
|
|
}
|
|
}
|
|
|
|
func testHookProcessWindowsSystemTimeoutCheckout(t *testing.T) {
|
|
if runtime.GOOS != "windows" {
|
|
t.Skip("the System32 timeout boundary is Windows-specific")
|
|
}
|
|
|
|
systemRoot := os.Getenv("SystemRoot")
|
|
if systemRoot == "" {
|
|
t.Fatal("SystemRoot is required for the System32 timeout boundary")
|
|
}
|
|
system32 := filepath.Join(systemRoot, "System32")
|
|
systemTimeout := filepath.Join(system32, "timeout.exe")
|
|
if _, err := os.Stat(systemTimeout); err != nil {
|
|
t.Fatalf("System32 timeout.exe is required: %v", err)
|
|
}
|
|
|
|
binDir := t.TempDir()
|
|
writeHookProcessFixture(t, binDir, "bd", hookProcessBDStub)
|
|
controlledPath := hookProcessShellPath(t, binDir) + ":" + hookProcessShellPath(t, system32)
|
|
probe := exec.Command(hookProcessShell(t), "--noprofile", "--norc", "-c", `
|
|
PATH=$1
|
|
export PATH
|
|
_bd_test_timeout=$(command -v timeout) || exit 1
|
|
printf '%s\n' "$_bd_test_timeout"
|
|
if timeout --version >/dev/null 2>&1; then exit 2; fi
|
|
`, "system32-timeout-probe", controlledPath)
|
|
probe.Env = hookProcessEnv()
|
|
probeOutput, err := probe.CombinedOutput()
|
|
if err != nil {
|
|
t.Fatalf("exercise System32 timeout through the hook PATH: %v\n%s", err, string(probeOutput))
|
|
}
|
|
gotTimeout := strings.TrimSuffix(strings.ToLower(strings.TrimSpace(string(probeOutput))), ".exe")
|
|
wantTimeout := strings.TrimSuffix(strings.ToLower(hookProcessShellPath(t, systemTimeout)), ".exe")
|
|
if gotTimeout != wantTimeout {
|
|
t.Fatalf("hook PATH resolved timeout to %q, want System32 %q", gotTimeout, wantTimeout)
|
|
}
|
|
|
|
repoDir := filepath.Join(t.TempDir(), "repo")
|
|
if err := os.MkdirAll(repoDir, 0o755); err != nil {
|
|
t.Fatalf("create temporary repository: %v", err)
|
|
}
|
|
gitPath, err := exec.LookPath("git.exe")
|
|
if err != nil {
|
|
t.Fatalf("Git for Windows is required: %v", err)
|
|
}
|
|
gitEnv := hookProcessGitEnv(t.TempDir(), "HOOK_TEST_BD_EXIT=0")
|
|
runHookProcessGit(t, gitPath, repoDir, gitEnv, "init", "--initial-branch=main", ".")
|
|
runHookProcessGit(t, gitPath, repoDir, gitEnv, "config", "core.hooksPath", ".git/hooks")
|
|
runHookProcessGit(t, gitPath, repoDir, gitEnv, "config", "user.name", "Hook Test")
|
|
runHookProcessGit(t, gitPath, repoDir, gitEnv, "config", "user.email", "hook-test@example.invalid")
|
|
if err := os.WriteFile(filepath.Join(repoDir, "tracked.txt"), []byte("initial\n"), 0o600); err != nil {
|
|
t.Fatalf("write tracked fixture: %v", err)
|
|
}
|
|
runHookProcessGit(t, gitPath, repoDir, gitEnv, "add", "--", "tracked.txt")
|
|
runHookProcessGit(t, gitPath, repoDir, gitEnv, "commit", "--no-verify", "-m", "initial")
|
|
commit := strings.TrimSpace(runHookProcessGit(t, gitPath, repoDir, gitEnv, "rev-parse", "HEAD"))
|
|
runHookProcessGit(t, gitPath, repoDir, gitEnv, "branch", "topic")
|
|
if head := strings.TrimSpace(runHookProcessGit(t, gitPath, repoDir, gitEnv, "symbolic-ref", "--short", "HEAD")); head != "main" {
|
|
t.Fatalf("HEAD = %q before checkout, want main", head)
|
|
}
|
|
|
|
hookBody := "#!/bin/sh\nPATH=" + hookProcessShellQuote(controlledPath) + "\nexport PATH\n" + generateHookSection("post-checkout")
|
|
writeHookProcessFixture(t, filepath.Join(repoDir, ".git", "hooks"), "post-checkout", hookBody)
|
|
checkout := exec.Command(gitPath, "checkout", "topic")
|
|
checkout.Dir = repoDir
|
|
checkout.Env = gitEnv
|
|
checkoutOutput, err := checkout.CombinedOutput()
|
|
if err != nil {
|
|
t.Fatalf("checkout reported generated-hook failure: %v\n%s", err, string(checkoutOutput))
|
|
}
|
|
output := string(checkoutOutput)
|
|
if !strings.Contains(output, "running without timeout") {
|
|
t.Errorf("checkout output missing incompatible-timeout fallback warning\n%s", output)
|
|
}
|
|
assertHookProcessBDInvocation(t, output, "post-checkout", commit, commit, "1")
|
|
if head := strings.TrimSpace(runHookProcessGit(t, gitPath, repoDir, gitEnv, "symbolic-ref", "--short", "HEAD")); head != "topic" {
|
|
t.Fatalf("HEAD = %q after successful checkout, want topic", head)
|
|
}
|
|
}
|
|
|
|
func runGeneratedHookProcess(t *testing.T, tc hookProcessCase) hookProcessResult {
|
|
t.Helper()
|
|
|
|
binDir := t.TempDir()
|
|
bdBody := tc.bdBody
|
|
if bdBody == "" {
|
|
bdBody = hookProcessBDStub
|
|
}
|
|
writeHookProcessFixture(t, binDir, "bd", bdBody)
|
|
for _, fixture := range tc.fixtures {
|
|
writeHookProcessFixture(t, binDir, fixture.name, fixture.body)
|
|
}
|
|
|
|
hookPath := filepath.Join(t.TempDir(), "pre-push")
|
|
writeHookProcessFixture(t, filepath.Dir(hookPath), filepath.Base(hookPath), "#!/bin/sh\n"+generateHookSection("pre-push"))
|
|
controlledPath := hookProcessShellPath(t, binDir)
|
|
if tc.pathTail != "" {
|
|
controlledPath += ":" + tc.pathTail
|
|
}
|
|
|
|
controlScript := `PATH=$1
|
|
export PATH
|
|
shift
|
|
case "$1" in
|
|
"") ;;
|
|
e) set -e ;;
|
|
u) set -u ;;
|
|
eu) set -eu ;;
|
|
*) exit 95 ;;
|
|
esac
|
|
shift
|
|
_bd_test_hook=$1
|
|
shift
|
|
. "$_bd_test_hook"
|
|
`
|
|
shellPath := hookProcessShell(t)
|
|
commandArgs := []string{"--noprofile", "--norc", "-c", controlScript, "generated-hook-test", controlledPath, tc.shellOption, hookProcessShellPath(t, hookPath)}
|
|
if tc.usePOSIXSh {
|
|
shellPath = hookProcessPOSIXShell(t)
|
|
commandArgs = []string{"-c", controlScript, "generated-hook-test", controlledPath, tc.shellOption, hookProcessShellPath(t, hookPath)}
|
|
}
|
|
commandArgs = append(commandArgs, tc.args...)
|
|
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
|
t.Cleanup(cancel)
|
|
cmd := exec.CommandContext(ctx, shellPath, commandArgs...)
|
|
cmd.WaitDelay = time.Second
|
|
cmd.Env = append(hookProcessEnv(), "HOOK_TEST_BD_EXIT="+strconv.Itoa(tc.bdExit))
|
|
if tc.timeout != nil {
|
|
cmd.Env = append(cmd.Env, "BEADS_HOOK_TIMEOUT="+*tc.timeout)
|
|
}
|
|
started := time.Now()
|
|
output, err := cmd.CombinedOutput()
|
|
elapsed := time.Since(started)
|
|
if ctx.Err() != nil {
|
|
t.Fatalf("generated hook exceeded process-test deadline: %v\n%s", ctx.Err(), string(output))
|
|
}
|
|
exitCode := 0
|
|
if err != nil {
|
|
exitErr, ok := err.(*exec.ExitError)
|
|
if !ok {
|
|
t.Fatalf("run generated hook: %v\n%s", err, string(output))
|
|
}
|
|
exitCode = exitErr.ExitCode()
|
|
}
|
|
return hookProcessResult{output: string(output), exitCode: exitCode, elapsed: elapsed}
|
|
}
|
|
|
|
// findHookProcessCoreutilsTimeout mirrors the generated shim's identity
|
|
// allowlist (cmd/bd/hooks.go generateHookSection). Keep the `case` here in step
|
|
// with the one the generator emits: this is the second copy of that contract,
|
|
// and a narrower copy silently skips the only test that drives a real timeout
|
|
// binary on exactly the hosts the wider allowlist exists for.
|
|
func findHookProcessCoreutilsTimeout(t *testing.T) (string, string) {
|
|
t.Helper()
|
|
probe := `for _bd_test_candidate in timeout gtimeout; do
|
|
if command -v "$_bd_test_candidate" >/dev/null 2>&1 &&
|
|
_bd_test_version="$("$_bd_test_candidate" --version 2>/dev/null)"; then
|
|
case "$_bd_test_version" in
|
|
"timeout (GNU coreutils) "*|"timeout (uutils coreutils) "*)
|
|
_bd_test_path=$(command -v "$_bd_test_candidate")
|
|
printf '%s\n%s\n' "$_bd_test_candidate" "${_bd_test_path%/*}"
|
|
exit 0
|
|
;;
|
|
esac
|
|
fi
|
|
done
|
|
exit 1
|
|
`
|
|
cmd := exec.Command(hookProcessShell(t), "--noprofile", "--norc", "-c", probe)
|
|
cmd.Env = hookProcessEnv()
|
|
output, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
t.Skipf("no compatible coreutils timeout is available: %s", strings.TrimSpace(string(output)))
|
|
}
|
|
lines := strings.Split(strings.TrimSpace(string(output)), "\n")
|
|
if len(lines) != 2 || lines[0] == "" || lines[1] == "" {
|
|
t.Fatalf("unexpected coreutils timeout probe output: %q", string(output))
|
|
}
|
|
return lines[0], lines[1]
|
|
}
|
|
|
|
func findHookProcessPerl(t *testing.T) string {
|
|
t.Helper()
|
|
probe := `
|
|
_bd_test_path=$(command -v perl) || exit 1
|
|
perl -e 'exit 0' || exit 1
|
|
printf '%s\n' "${_bd_test_path%/*}"
|
|
`
|
|
cmd := exec.Command(hookProcessShell(t), "--noprofile", "--norc", "-c", probe)
|
|
cmd.Env = hookProcessEnv()
|
|
output, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
t.Skipf("Perl is unavailable: %s", strings.TrimSpace(string(output)))
|
|
}
|
|
dir := strings.TrimSpace(string(output))
|
|
if dir == "" {
|
|
t.Fatalf("unexpected Perl probe output: %q", string(output))
|
|
}
|
|
return dir
|
|
}
|
|
|
|
func writeHookProcessFixture(t *testing.T, dir, name, body string) {
|
|
t.Helper()
|
|
path := filepath.Join(dir, name)
|
|
if err := os.WriteFile(path, []byte(body), 0o755); err != nil {
|
|
t.Fatalf("write %s fixture: %v", name, err)
|
|
}
|
|
if err := os.Chmod(path, 0o755); err != nil {
|
|
t.Fatalf("make %s fixture executable: %v", name, err)
|
|
}
|
|
}
|
|
|
|
func assertHookProcessBDInvocation(t *testing.T, output, hookName string, hookArgs ...string) {
|
|
t.Helper()
|
|
wantArgs := append([]string{"hooks", "run", hookName}, hookArgs...)
|
|
if marker := fmt.Sprintf("bd-argc=%d", len(wantArgs)); !strings.Contains(output, marker) {
|
|
t.Errorf("output missing %q\n%s", marker, output)
|
|
}
|
|
for index, arg := range wantArgs {
|
|
marker := fmt.Sprintf("bd-arg-%d=<%s>", index, arg)
|
|
if !strings.Contains(output, marker) {
|
|
t.Errorf("output missing argument marker %q\n%s", marker, output)
|
|
}
|
|
}
|
|
}
|
|
|
|
func hookProcessShell(t *testing.T) string {
|
|
t.Helper()
|
|
if runtime.GOOS == "windows" {
|
|
gitPath, err := exec.LookPath("git.exe")
|
|
if err != nil {
|
|
t.Fatalf("Git for Windows is required to locate Git Bash: %v", err)
|
|
}
|
|
dir := filepath.Dir(gitPath)
|
|
for range 5 {
|
|
for _, candidate := range []string{filepath.Join(dir, "bash.exe"), filepath.Join(dir, "bin", "bash.exe")} {
|
|
if info, statErr := os.Stat(candidate); statErr == nil && !info.IsDir() {
|
|
return candidate
|
|
}
|
|
}
|
|
parent := filepath.Dir(dir)
|
|
if parent == dir {
|
|
break
|
|
}
|
|
dir = parent
|
|
}
|
|
t.Fatalf("could not locate Git Bash beside %s", gitPath)
|
|
}
|
|
shell, err := exec.LookPath("bash")
|
|
if err != nil {
|
|
t.Fatalf("Bash is required to exercise generated hooks: %v", err)
|
|
}
|
|
return shell
|
|
}
|
|
|
|
func hookProcessPOSIXShell(t *testing.T) string {
|
|
t.Helper()
|
|
if runtime.GOOS == "windows" {
|
|
bashPath := hookProcessShell(t)
|
|
gitRoot := filepath.Dir(filepath.Dir(bashPath))
|
|
candidate := filepath.Join(gitRoot, "usr", "bin", "sh.exe")
|
|
if info, err := os.Stat(candidate); err == nil && !info.IsDir() {
|
|
return candidate
|
|
}
|
|
t.Fatalf("could not locate Git for Windows POSIX sh beside %s", bashPath)
|
|
}
|
|
shell, err := exec.LookPath("sh")
|
|
if err != nil {
|
|
t.Fatalf("POSIX sh is required to exercise generated hooks: %v", err)
|
|
}
|
|
return shell
|
|
}
|
|
|
|
func hookProcessShellPath(t *testing.T, path string) string {
|
|
t.Helper()
|
|
absolute, err := filepath.Abs(path)
|
|
if err != nil {
|
|
t.Fatalf("resolve shell path %s: %v", path, err)
|
|
}
|
|
if runtime.GOOS != "windows" {
|
|
return filepath.ToSlash(absolute)
|
|
}
|
|
volume := filepath.VolumeName(absolute)
|
|
if len(volume) != 2 || volume[1] != ':' {
|
|
t.Fatalf("Git Bash fixture path must use a drive-letter volume: %q", absolute)
|
|
}
|
|
return "/" + strings.ToLower(volume[:1]) + filepath.ToSlash(absolute[len(volume):])
|
|
}
|
|
|
|
func hookProcessShellQuote(value string) string {
|
|
return "'" + strings.ReplaceAll(value, "'", `'"'"'`) + "'"
|
|
}
|
|
|
|
func hookProcessEnv() []string {
|
|
env := make([]string, 0, len(os.Environ())+2)
|
|
for _, entry := range os.Environ() {
|
|
key, _, ok := strings.Cut(entry, "=")
|
|
if !ok {
|
|
continue
|
|
}
|
|
upper := strings.ToUpper(key)
|
|
if upper == "BASH_ENV" || upper == "BASHOPTS" || upper == "ENV" || upper == "SHELLOPTS" ||
|
|
upper == "BEADS_HOOK_TIMEOUT" || upper == "HOOK_TEST_BD_EXIT" {
|
|
continue
|
|
}
|
|
env = append(env, entry)
|
|
}
|
|
return append(env, "BASH_ENV=", "ENV=")
|
|
}
|
|
|
|
func hookProcessGitEnv(home string, extra ...string) []string {
|
|
env := make([]string, 0, len(os.Environ())+8+len(extra))
|
|
for _, entry := range os.Environ() {
|
|
key, _, ok := strings.Cut(entry, "=")
|
|
if !ok {
|
|
continue
|
|
}
|
|
upper := strings.ToUpper(key)
|
|
if upper == "HOME" || upper == "USERPROFILE" || upper == "BASH_ENV" || upper == "BASHOPTS" || upper == "ENV" || upper == "SHELLOPTS" ||
|
|
strings.HasPrefix(upper, "GIT_") || upper == "HOOK_TEST_BD_EXIT" {
|
|
continue
|
|
}
|
|
env = append(env, entry)
|
|
}
|
|
env = append(env,
|
|
"HOME="+home,
|
|
"USERPROFILE="+home,
|
|
"BASH_ENV=",
|
|
"ENV=",
|
|
"GIT_CONFIG_NOSYSTEM=1",
|
|
"GIT_CONFIG_GLOBAL="+os.DevNull,
|
|
"GIT_CONFIG_SYSTEM="+os.DevNull,
|
|
"GIT_TERMINAL_PROMPT=0",
|
|
)
|
|
return append(env, extra...)
|
|
}
|
|
|
|
func runHookProcessGit(t *testing.T, gitPath, dir string, env []string, args ...string) string {
|
|
t.Helper()
|
|
cmd := exec.Command(gitPath, args...)
|
|
cmd.Dir = dir
|
|
cmd.Env = env
|
|
output, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
t.Fatalf("git %s: %v\n%s", strings.Join(args, " "), err, string(output))
|
|
}
|
|
return string(output)
|
|
}
|
|
|
|
func hookProcessString(value string) *string {
|
|
return &value
|
|
}
|