Skip to content

test(hooks): skip the stdin-JSON hook test when jq is missing - #4053

Open
dwin-gharibi wants to merge 1 commit into
docker:mainfrom
dwin-gharibi:fix/hooks-test-jq-dependency
Open

test(hooks): skip the stdin-JSON hook test when jq is missing#4053
dwin-gharibi wants to merge 1 commit into
docker:mainfrom
dwin-gharibi:fix/hooks-test-jq-dependency

Conversation

@dwin-gharibi

Copy link
Copy Markdown
Contributor

printStdinJSONFieldCmd shells out to jq. When jq is absent the hook still runs and exits
cleanly, producing no output, so the test fails on its content assertion rather than on a
missing command — reporting a hook-plumbing defect that does not exist.

Closes #4052.

Before

On a machine without jq (e.g. stock golang:1.27):

--- FAIL: TestExecuteStopReceivesResponseContent (0.00s)
    hooks_test.go:586:
        	Error: "" does not contain "final answer content"
FAIL	github.com/docker/docker-agent/pkg/hooks

After

    hooks_test.go:573: jq is not installed; it is required to read a JSON field from hook stdin
--- SKIP: TestExecuteStopReceivesResponseContent (0.00s)
ok  	github.com/docker/docker-agent/pkg/hooks

With jq present, unchanged:

--- PASS: TestExecuteStopReceivesResponseContent (0.00s)
ok  	github.com/docker/docker-agent/pkg/hooks

What changed

The helper takes *testing.T and guards on exec.LookPath:

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 + `'`
}

The Windows mirror takes the same parameter for signature parity but needs no guard —
PowerShell's ConvertFrom-Json is built in. One caller updated.

Why skip rather than drop the dependency

Removing jq would mean either parsing JSON with sed/awk — fragile, and a fragile test is
worse than a skipped one — or weakening the assertion to "the value appears somewhere in the
payload", which stops testing that the value arrives in the named field.

Skipping keeps the assertion exact wherever jq exists (CI, most dev machines) and makes the
failure mode honest everywhere else. Coverage is unchanged on any machine that had jq.

Verification

Tested both paths in a container, by moving /usr/bin/jq aside and back:

jq present jq absent
before ok FAIL — misleading assertion
after ok (PASS) ok (SKIP, names jq)

Full pkg/hooks package passes in both states; go vet clean.

Note

This is a test-only change. No production code is touched, and behaviour on machines with jq
installed is byte-for-byte identical.

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.
@dwin-gharibi
dwin-gharibi requested a review from a team as a code owner August 24, 2026 19:03
Copilot AI lite review requested due to automatic review settings August 24, 2026 19:03

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@aheritier aheritier added area/tools For features/issues/fixes related to the usage of built-in and MCP tools kind/test Test-only changes labels Aug 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/tools For features/issues/fixes related to the usage of built-in and MCP tools kind/test Test-only changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pkg/hooks tests fail with a misleading assertion when jq is not installed

3 participants