Skip to content

MatchYAML compares only the first document of a multi-document YAML stream #933

Closed

Description

@DRMacIver

This is another bug found by our automated process of writing hegel tests in hegel-zoo, filed as asked in #925 and, as before, reviewed by hand first.

MatchYAML reports a match when the actual YAML is a stream whose first document equals the expected: a: 1 matches a: 1\n---\nb: 2, and two streams that differ only in their second document match each other, while the same difference in the first document is caught:

package main

import (
	"fmt"

	"github.com/onsi/gomega"
)

func main() {
	ok, err := gomega.MatchYAML("a: 1").Match("a: 1\n---\nb: 2")
	fmt.Println("a: 1  vs  a: 1 --- b: 2:        ", ok, err)
	ok, err = gomega.MatchYAML("a: 1\n---\nb: 2").Match("a: 1\n---\nb: 3")
	fmt.Println("a: 1 --- b: 2  vs  a: 1 --- b: 3:", ok, err)
	ok, err = gomega.MatchYAML("a: 1\n---\nb: 2").Match("a: 2\n---\nb: 2")
	fmt.Println("a: 1 --- b: 2  vs  a: 2 --- b: 2:", ok, err)
}

Output:

a: 1  vs  a: 1 --- b: 2:         true <nil>
a: 1 --- b: 2  vs  a: 1 --- b: 3: true <nil>
a: 1 --- b: 2  vs  a: 2 --- b: 2: false <nil>

I expected the first two to be false, or for a stream with more than one document to be rejected the way invalid YAML is; as it stands the second and later documents are accepted and then have no effect on the result.

Tested on v1.43.1, which is also the current master (47f0f87).

Activity

  1. onsi commented on Sep 24, 2026

    @onsi
    Owner

    I've delegated fixing this to Claude and checked the code it generated. Thanks for reporting!

    MatchYAML now decodes every document in the stream and compares them pairwise. Streams with different numbers of documents fail to match, and the failure message names the first mismatched document. Single documents behave exactly as before, and empty documents (such as a leading or trailing ---) are ignored. (2773796)

    This will be released in v1.44.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions