onsi / gomega

Ginkgo's Preferred Matcher Library
http://onsi.github.io/gomega/
MIT License
2.16k stars 281 forks source link

HaveExactElements: false positive when passed nothing #771

Closed pohly closed 1 month ago

pohly commented 1 month ago

@bart0sh found the following odd behavior:

package foo

import (
    "testing"

    "github.com/onsi/gomega"
)

func TestHaveExactElements(t *testing.T) {
    var expected []any = nil
    actual := []string{"test"}

    gomega.NewWithT(t).Expect(actual).To(gomega.HaveExactElements(expected...)) // Same as HaveExactElements().
    if len(expected) != len(actual) {
        t.Error("unexpected non-nil results", expected, actual)
    }
}

Output:

--- FAIL: TestHaveExactElements (0.00s)
    foo_test.go:15: HaveExactElements did not detect a length mismatch: the actual list has 1 elements, expected were 0
FAIL
FAIL    k8s.io/kubernetes/foo   0.007s
FAIL

I think this is a bug. HaveExactElements should report all extra elements in the actual slice. So if there is nothing to match against, any non-empty slice should cause a failure.

pohly commented 1 month ago

The reason for using HaveExactElements instead of BeEmpty is that the set of expected elements is dynamic.

onsi commented 1 month ago

yes looks like a bug to me. i'll try to prioritize this and get to it soon. i'm a bit behind on ginkgo/gomega stuff these days.

pohly commented 1 month ago

Might be worth checking whether ConsistOf has the same problem.

pohly commented 1 month ago

ConsistOf works:

go test .
--- FAIL: TestHaveExactElements (0.00s)
    foo_test.go:13: 
        Expected
            <[]string | len:1, cap:1>: ["test"]
        to consist of
            <[]interface {} | len:0, cap:0>: []
        the extra elements were
            <[]string | len:1, cap:1>: ["test"]
FAIL
FAIL    k8s.io/kubernetes/foo   0.006s
FAIL

BeEmpty produces better output:

go test .
--- FAIL: TestHaveExactElements (0.00s)
    foo_test.go:13: 
        Expected
            <[]string | len:1, cap:1>: ["test"]
        to be empty
FAIL
FAIL    k8s.io/kubernetes/foo   0.006s
FAIL
onsi commented 1 month ago

alrighty i've fixed this now. will cut a release once CI goes green.