onsi / gomega

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

`ConsistOf` panics when encountering `nil` values in slice #655

Closed jimsmart closed 1 year ago

jimsmart commented 1 year ago

I have multiple cases where I use:

Expect(result).To(ConsistOf(expected))

And expected is a slice, where one of the contained values is nil — and this is a correct result for my code.

But when my test runs, I get the following panic:

  [PANICKED] Test Panicked
  In [It] at: /usr/local/go/src/reflect/value.go:2610 @ 03/28/23 02:35:34.396

  reflect: call of reflect.Value.Type on zero Value

  Full Stack Trace
    reflect.Value.typeSlow({0x0?, 0x0?, 0x2?})
        /usr/local/go/src/reflect/value.go:2610 +0x12e
    reflect.Value.Type(...)
        /usr/local/go/src/reflect/value.go:2605
    github.com/onsi/gomega/matchers.presentable({0xc00069dd70?, 0x100693f80?, 0x80?})
        /Users/jimsmart/go/pkg/mod/github.com/onsi/gomega@v1.27.5/matchers/consist_of.go:92 +0x11c
    github.com/onsi/gomega/matchers.(*ConsistOfMatcher).FailureMessage(0xc000a8b590, {0x1006754c0, 0xc000853fc8})
        /Users/jimsmart/go/pkg/mod/github.com/onsi/gomega@v1.27.5/matchers/consist_of.go:125 +0x57
    github.com/onsi/gomega/internal.(*Assertion).match(0xc0002fff00, {0x100857070, 0xc000a8b590}, 0x1, {0x0, 0x0, 0x0})
        /Users/jimsmart/go/pkg/mod/github.com/onsi/gomega@v1.27.5/internal/assertion.go:101 +0x128
    github.com/onsi/gomega/internal.(*Assertion).To(0xc0002fff00, {0x100857070, 0xc000a8b590}, {0x0, 0x0, 0x0})
        /Users/jimsmart/go/pkg/mod/github.com/onsi/gomega@v1.27.5/internal/assertion.go:62 +0xb5
    reflect.Value.call({0x10069b7e0?, 0xc0002a4900?, 0x13?}, {0x100733302, 0x4}, {0xc000a0dbc0, 0x4, 0x4?})
        /usr/local/go/src/reflect/value.go:586 +0xb07
    reflect.Value.Call({0x10069b7e0?, 0xc0002a4900?, 0x100860c70?}, {0xc000a0dbc0?, 0x0?, 0xc100aef98ecbc7e0?})
        /usr/local/go/src/reflect/value.go:370 +0xbc

So it seems that ConsistOf does not handle nil values in a slice.

Edit: specifically here, my expected is a slice interface values, e.g.

expected := []any{nil, "foo"}

Although my case does not use any, instead it's an interface I have declared, but I imagine that's enough to understand the issue.

My go.mod says:

github.com/onsi/gomega v1.27.5
onsi commented 1 year ago

hey @jimsmart thanks for reporting this. I just published v1.27.6 with a fix. Please give it a try and let me know if it works for you.

jimsmart commented 1 year ago

Yes, that works great — thank you very much for the speedy fix!