go-test / deep

Golang deep variable equality test that returns human-readable differences
MIT License
751 stars 54 forks source link

Panic when comparing errors in unexported fields #45

Closed seveas closed 1 year ago

seveas commented 4 years ago

https://github.com/go-test/deep/pull/33 broke the comparison of structs with errors in an unexported fields. THis code used to work just fine with deep 1.0.4, but panics with 1.0.7:

package main

import (
    "fmt"
    "github.com/go-test/deep"
)

type foo struct {
    bar error
}

func main() {
    deep.CompareUnexportedFields = true
    e1 := foo{bar: fmt.Errorf("error")}
    e2 := foo{bar: fmt.Errorf("error")}
    deep.Equal(e1, e2)
}

The panic:

$ go run deep_panic.go
panic: reflect: reflect.flag.mustBeExported using value obtained using unexported field

goroutine 1 [running]:
reflect.flag.mustBeExportedSlow(0x2b3)
    /usr/local/Cellar/go/1.14.3/libexec/src/reflect/value.go:225 +0x12e
reflect.flag.mustBeExported(...)
    /usr/local/Cellar/go/1.14.3/libexec/src/reflect/value.go:216
reflect.Value.Call(0x10e1ca0, 0xc00008e230, 0x2b3, 0x0, 0x0, 0x0, 0xc00008e230, 0x2b3, 0x0)
    /usr/local/Cellar/go/1.14.3/libexec/src/reflect/value.go:320 +0x4d
github.com/go-test/deep.(*cmp).equals(0xc00009aeb0, 0x10e1ca0, 0xc00008e230, 0xb4, 0x10e1ca0, 0xc00008e240, 0xb4, 0x1)
    /Users/seveas/go/pkg/mod/github.com/go-test/deep@v1.0.7/deep.go:145 +0x335b
github.com/go-test/deep.(*cmp).equals(0xc00009aeb0, 0x10e3040, 0xc00008e230, 0x99, 0x10e3040, 0xc00008e240, 0x99, 0x0)
    /Users/seveas/go/pkg/mod/github.com/go-test/deep@v1.0.7/deep.go:223 +0x246c
github.com/go-test/deep.Equal(0x10e3040, 0xc00008e230, 0x10e3040, 0xc00008e240, 0x0, 0x11203e0, 0xc00008e220)
    /Users/seveas/go/pkg/mod/github.com/go-test/deep@v1.0.7/deep.go:88 +0x2b4
main.main()
    /Users/seveas/code/katyusha/deep_panic.go:16 +0x118
exit status 2
daniel-nichter commented 1 year ago

Sorry for slow response, but fixed in https://github.com/go-test/deep/pull/54 and will be released this week or next.

daniel-nichter commented 1 year ago

Released: https://github.com/go-test/deep/releases/tag/v1.0.9

seveas commented 1 year ago

Better slow than never, thanks!