beads/cmd/bd/hook_env_test.go
ecuthiell 13150ba360
fix(hooks): apply host key identity to Git environment filters (#6321)
* fix(hooks): use host key identity when filtering child environments

Use execenv for the chained-hook guard and export Git routing filters.
Keep their existing per-consumer denylist and valueless GIT_CONFIG prefix
behavior while matching Windows os/exec key identity. Preserve the input
slice and exercise mixed case, Unicode, malformed keys and value handling.

The worktree-removal site from issue6198 remains owned by PR5623. This
stack depends on PR5672 and must not close6198 before both parents land.

* test(hooks): exercise both host key policies through actual filters (saa-cgh.89)

Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell

* refactor(hooks): share environment entry parsing and clarify serial seam (saa-cgh.89)

Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell

* build(bazel): list hook_env_test.go in cmd/bd go_test srcs

The PR's new cmd/bd/hook_env_test.go is untagged but absent from the
gazelle-managed go_test srcs list, so its two pins are invisible to
`bazel test //cmd/bd:bd_test`.

Both review lanes held this at minor on the grounds that untagged srcs
drift is tolerated in this package: three untagged test files
(config_leaked_secret_test.go, init_git_bootstrap_routing_test.go,
init_role_environment_test.go) are likewise unlisted at this PR's base.
That precedent is gone at the merge target. At origin/main 147047e15 the
census is 23 omissions, all 23 //go:build-gated, zero untagged -- main
landed all three rows -- so this file would be the merge target's only
untagged omission.

Two advisory steps in .github/workflows/bazel.yml (absent at this base,
present at the merge target) act on that: `Go test equivalence`
(tools/bazel/equivalence.py, which fails on any Go test `go test` runs
and Bazel did not, including "missing target"; neither new test name is
in equivalence_allowlist.txt) and the `make bazel-sync` drift check. The
job's `if` includes head.repo.fork == true, so it always runs for this
fork PR.

Generated by `bazel run //:gazelle`, which is what the drift step
compares the tree against. Gazelle also emitted the three base-owned
cmd/bd rows and an internal/notion change; all four are already present
at the merge target and were reverted so they merge from main's side.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* build(bazel): sync scripts go_test srcs (adopted base debt, not this PR's change)

scripts/upgrade_smoke_loop_test.go is untagged and absent from the
gazelle-managed go_test srcs in scripts/BUILD.bazel, which reds the
advisory Bazel lane two ways: `Go test equivalence` reports
`TestUpgradeSmokeMultiVersionDispatch` as a test `go test` runs and Bazel
did not, and the `BUILD files in sync` step's own `make bazel-sync` adds
the row and then fails on the drift.

This is NOT introduced by this PR, which touches zero files under
scripts/. The gap is identical at this PR's base fc2351a51c and at HEAD,
and the Bazel lane already fails on main's own pushes at fc2351a51c,
147047e15, 90f8e42f1 and 8c889c1f2 for this same reason -- it currently
reds for every PR in the repo. Carried here because main is unprotected,
so the pre-approval CI gate takes the strict path in which any failing
run blocks, with no base-preexisting bypass.

Generated by `bazel run //:gazelle`; the local drift is byte-identical to
the drift CI reported (9345584 -> 5dfc23e). Kept as its own commit so the
contributor's hook-env work stays separable from adopted base debt.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

---------

Co-authored-by: Gas City Adopt-PR <adopt-pr@gascity.local>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-09-28 14:13:56 -07:00

76 lines
2.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"slices"
"testing"
)
func TestFilterChainedHookEnvUsesHostKeyIdentity(t *testing.T) {
checkHookEnvForBothHosts(t, checkChainedHookEnv)
}
// Callers and subtests must remain serial while they swap the process-wide seam.
func checkHookEnvForBothHosts(t *testing.T, check func(*testing.T)) {
t.Helper()
for _, goos := range []string{"linux", "windows"} {
t.Run(goos, func(t *testing.T) {
previous := hookEnvGOOS
hookEnvGOOS = goos
t.Cleanup(func() { hookEnvGOOS = previous })
check(t)
})
}
}
func checkChainedHookEnv(t *testing.T) {
t.Helper()
env := []string{
"BD_GIT_HOOK=canonical", "bd_git_hook=mixed", "BD_GİT_HOOK=unicode-case",
"BD_GIT_HOOK=unicode-case", "BD_GIT_HOOK=",
"BD_GıT_HOOK=distinct", "KEEP=first", "KEEP=BD_GIT_HOOK=1",
"BD_GIT_HOOK", `=C:=C:\work`,
}
original := slices.Clone(env)
want := []string{"BD_GıT_HOOK=distinct", "KEEP=first", "KEEP=BD_GIT_HOOK=1", "BD_GIT_HOOK", `=C:=C:\work`}
if hookEnvGOOS != "windows" {
want = append([]string{"bd_git_hook=mixed", "BD_GİT_HOOK=unicode-case", "BD_GIT_HOOK=unicode-case"}, want...)
}
if got := filterEnv(env, "BD_GIT_HOOK"); !slices.Equal(got, want) {
t.Errorf("chained-hook environment = %q, want %q", got, want)
}
if !slices.Equal(env, original) {
t.Errorf("input environment mutated: %q", env)
}
}
func TestScrubGitHookEnvUsesHostKeyIdentity(t *testing.T) {
checkHookEnvForBothHosts(t, checkGitHookEnv)
}
func checkGitHookEnv(t *testing.T) {
t.Helper()
env := []string{
"GIT_DIR=canonical", "git_dir=mixed", "GİT_DİR=unicode-case", "GIT_DıR=distinct",
"GIT_WORK_TREE=unicode-case", "GIT_DIſCOVERY_ACROSS_FILESYSTEM=distinct", "GIT_CONFIG=",
"GIT_CONFIG_COUNT=1", "git_config_parameters=mixed", "gİt_config_count=unicode-case",
"GIT_CONFIG_UNSET", "git_config_unset", "KEEP=first", "KEEP=GIT_CONFIG_COUNT=1",
"GIT_AUTHOR_NAME=author", "GIT_OPTIONAL_LOCKS=1", "GIT_DIR", "MALFORMED", `=C:=C:\work`,
}
original := slices.Clone(env)
want := []string{"GIT_DıR=distinct"}
if hookEnvGOOS != "windows" {
want = append([]string{"git_dir=mixed", "GİT_DİR=unicode-case"}, want...)
want = append(want, "GIT_WORK_TREE=unicode-case")
}
want = append(want, "GIT_DIſCOVERY_ACROSS_FILESYSTEM=distinct")
if hookEnvGOOS != "windows" {
want = append(want, "git_config_parameters=mixed", "gİt_config_count=unicode-case", "git_config_unset")
}
want = append(want, "KEEP=first", "KEEP=GIT_CONFIG_COUNT=1", "GIT_AUTHOR_NAME=author", "GIT_OPTIONAL_LOCKS=1", "GIT_DIR", "MALFORMED", `=C:=C:\work`)
if got := scrubGitHookEnv(env); !slices.Equal(got, want) {
t.Errorf("Git-hook environment = %q, want %q", got, want)
}
if !slices.Equal(env, original) {
t.Errorf("input environment mutated: %q", env)
}
}