From 90797831f880f6e06f8e62e434de3fac5b48acc3 Mon Sep 17 00:00:00 2001 From: Dwin Gharibi Date: Mon, 24 Aug 2026 13:18:01 +0330 Subject: [PATCH] test(hooks): skip the stdin-JSON hook test when jq is missing printStdinJSONFieldCmd shells out to jq, the only test helper here needing a JSON parser in the shell. POSIX has no built-in one; the Windows mirror can lean on PowerShell's ConvertFrom-Json. Without jq the hook still ran, produced no output, and the test failed 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. Skip with the real reason instead, so the diagnosis is immediate and coverage is unchanged wherever jq is installed. --- pkg/hooks/helpers_other_test.go | 16 +++++++++++++++- pkg/hooks/helpers_windows_test.go | 6 +++++- pkg/hooks/hooks_test.go | 2 +- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/pkg/hooks/helpers_other_test.go b/pkg/hooks/helpers_other_test.go index 972dcc45fc..5590bdd894 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 31f7033f5b..6b6d14b07b 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 74df2c83f8..648e2d84a1 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}, }, }