fix(matchers): an empty map matcher panics instead of reporting a syntax error - #1118
Open
VXNCXNX wants to merge 1 commit into
Open
fix(matchers): an empty map matcher panics instead of reporting a syntax error#1118VXNCXNX wants to merge 1 commit into
VXNCXNX wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
An empty map used as a matcher crashes goss instead of reporting a syntax error.
Same for
listening: {}and for a nested one such as{"and": [{}]}.Cause
matcherToGomegaMatcherguards the "too many keys" case but not the "no keys"case, then indexes unconditionally:
An empty map reaches
keys[0]with a zero-length slice.The fix
The mirror image of the existing guard: return a syntax error when there are no
keys, in the same style and phrasing as the neighbouring one, so a malformed
gossfile is reported as malformed rather than crashing the run.
Verification
TestMatcherToGomegaMatcherEmptyMapcovers the empty map at the top level andnested inside
andandnot.Two mutations, because "it panics" and "it returns the right thing" are separate
claims:
Removing the guard reproduces the original crash:
Returning a matcher instead of the error fails on the assertion, not on a panic,
which is what shows the test checks the behaviour rather than merely surviving:
go test ./...is ok across all packages, andgolangci-lint run ./resource/...reports 0 issues.
On the golden fixtures: this change only affects an input that previously
crashed, so no generated output string changes. I grepped
integration-tests/and
docs/for both the old and new strings and there are no hits, so nogoss-expected*.yamlneeds updating.Checklist
make test-all(UNIX) passes. CI will also test this (could not run in full here: it needs Docker.go test ./...andgolangci-lintpass, and the before/after above comes from running real binaries built from each tree)One note unrelated to the fix:
gofumpt -l resource/listsgomega.gobothbefore and after this change, so I left the pre-existing formatting alone rather
than mixing a reformat into this diff.
Disclosure: written with AI assistance (Claude Code). I built binaries from master and from the patched tree, produced the before and after above by running them against the gossfile shown, and ran both mutation checks myself.