diff --git a/pkg/hooks/helpers_other_test.go b/pkg/hooks/helpers_other_test.go index 972dcc45f..5590bdd89 100644 --- a/pkg/hooks/helpers_other_test.go +++ b/pkg/hooks/helpers_other_test.go @@ -3,6 +3,7 @@ package hooks import ( + "os/exec" "path/filepath" "strings" "testing" @@ -31,7 +32,20 @@ func emitContextEnvPwdCmd(envVars ...string) string { // printStdinJSONFieldCmd returns a command printing one field of the JSON // document the hook receives on stdin. -func printStdinJSONFieldCmd(field string) string { +// +// This is the only test helper that needs a JSON parser in the shell, and POSIX +// has no built-in one (the Windows mirror can lean on PowerShell's +// ConvertFrom-Json). It shells out to jq and skips when jq is absent. +// +// The skip matters: without it the hook still runs, produces no output, and the +// test fails on its content assertion instead -- reporting `"" does not contain +// "final answer content"`, which reads as a defect in the hook plumbing rather +// than a missing tool on the machine. +func printStdinJSONFieldCmd(t *testing.T, field string) string { + t.Helper() + if _, err := exec.LookPath("jq"); err != nil { + t.Skip("jq is not installed; it is required to read a JSON field from hook stdin") + } return `cat | jq -r '.` + field + `'` } diff --git a/pkg/hooks/helpers_windows_test.go b/pkg/hooks/helpers_windows_test.go index 31f7033f5..6b6d14b07 100644 --- a/pkg/hooks/helpers_windows_test.go +++ b/pkg/hooks/helpers_windows_test.go @@ -31,7 +31,11 @@ func emitContextEnvPwdCmd(envVars ...string) string { // printStdinJSONFieldCmd returns a command printing one field of the JSON // document the hook receives on stdin. -func printStdinJSONFieldCmd(field string) string { +// Signature mirrors the POSIX helper, which needs *testing.T to skip when jq is +// missing. PowerShell's ConvertFrom-Json is built in, so there is nothing to +// skip on here. +func printStdinJSONFieldCmd(t *testing.T, field string) string { + t.Helper() return `([Console]::In.ReadToEnd() | ConvertFrom-Json).` + field } diff --git a/pkg/hooks/hooks_test.go b/pkg/hooks/hooks_test.go index 74df2c83f..648e2d84a 100644 --- a/pkg/hooks/hooks_test.go +++ b/pkg/hooks/hooks_test.go @@ -570,7 +570,7 @@ func TestExecuteStopReceivesResponseContent(t *testing.T) { config := &Config{ Stop: []Hook{ - {Type: HookTypeCommand, Command: printStdinJSONFieldCmd("stop_response"), Timeout: 5}, + {Type: HookTypeCommand, Command: printStdinJSONFieldCmd(t, "stop_response"), Timeout: 5}, }, }