Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion pkg/hooks/helpers_other_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package hooks

import (
"os/exec"
"path/filepath"
"strings"
"testing"
Expand Down Expand Up @@ -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 + `'`
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/hooks/helpers_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/hooks/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
},
}

Expand Down
Loading