bytedance / mockey

a simple and easy-to-use golang mock library
Apache License 2.0
557 stars 22 forks source link

reflect: call of reflect.Value.Set on zero Value #4

Closed ycydsxy closed 1 year ago

ycydsxy commented 1 year ago

Describe the bug

panic occurred when using MockValue to mock a interface variable

To Reproduce

import (
    "fmt"
    "testing"

    . "github.com/bytedance/mockey"
    . "github.com/smartystreets/goconvey/convey"
)

type testStruct struct {
    a string
    b int
}

func (t *testStruct) String() string {
    return t.a
}

func TestVarStruct2(t *testing.T) {
    var ttt fmt.Stringer
    PatchConvey("test mock3", t, func() {
        MockValue(&ttt).To(&testStruct{
            a: "2",
            b: 3,
        })
        So(ttt.(*testStruct).a, ShouldEqual, "2")
    })
}

Expected behavior

no panic occurs and the test case passes

Screenshots

GOROOT=/usr/local/opt/go/libexec #gosetup
GOPATH=/Users/suxiangyu/go #gosetup
/usr/local/opt/go/libexec/bin/go test -c -o /private/var/folders/bg/yg3z7f515dx83hvv_9kxgjgr0000gp/T/GoLand/___TestVarStruct2_in_awesomeProject_so.test awesomeProject/so #gosetup
/usr/local/opt/go/libexec/bin/go tool test2json -t /private/var/folders/bg/yg3z7f515dx83hvv_9kxgjgr0000gp/T/GoLand/___TestVarStruct2_in_awesomeProject_so.test -test.v -test.paniconexit0 -test.run ^\QTestVarStruct2\E$
=== RUN   TestVarStruct2
.E
Errors:

  * /Users/suxiangyu/go/src/awesomeProject/so/main_test.go 
  Line 22: - reflect: call of reflect.Value.Set on zero Value 
  goroutine 6 [running]:
    /Users/suxiangyu/go/pkg/mod/github.com/smartystreets/goconvey@v1.6.4/convey/reporting/reports.go:148 +0x3f
    /Users/suxiangyu/go/pkg/mod/github.com/smartystreets/goconvey@v1.6.4/convey/reporting/reports.go:121 +0x5d
    /Users/suxiangyu/go/pkg/mod/github.com/smartystreets/goconvey@v1.6.4/convey/context.go:235 +0xe5
  panic({0x11b1b20, 0xc00000c138})
    /usr/local/opt/go/libexec/src/runtime/panic.go:838 +0x207
  reflect.flag.mustBeExportedSlow(0x0?)
    /usr/local/opt/go/libexec/src/reflect/value.go:237 +0xc5
  reflect.flag.mustBeExported(...)
    /usr/local/opt/go/libexec/src/reflect/value.go:231
  reflect.Value.Set({0x11b69a0?, 0xc0000546e0?, 0x203000?}, {0x0?, 0x0?, 0xffffffffffffffff?})
    /usr/local/opt/go/libexec/src/reflect/value.go:2083 +0x9f
  github.com/bytedance/mockey.(*MockerVar).UnPatch(0xc000024360)
    /Users/suxiangyu/go/pkg/mod/github.com/bytedance/mockey@v1.0.0-rc.0/mock_var.go:85 +0x148
  github.com/bytedance/mockey.(*MockerVar).unPatch(0x0?)
    /Users/suxiangyu/go/pkg/mod/github.com/bytedance/mockey@v1.0.0-rc.0/mock_var.go:101 +0x19
  github.com/bytedance/mockey.PatchConvey.func1.1()
    /Users/suxiangyu/go/pkg/mod/github.com/bytedance/mockey@v1.0.0-rc.0/convey.go:39 +0x79
  github.com/bytedance/mockey.PatchConvey.func1({0x13739f8, 0x0, 0x0})
    /Users/suxiangyu/go/pkg/mod/github.com/bytedance/mockey@v1.0.0-rc.0/convey.go:43 +0x1cb
    /Users/suxiangyu/go/pkg/mod/github.com/smartystreets/goconvey@v1.6.4/convey/discovery.go:80 +0x1b
    /Users/suxiangyu/go/pkg/mod/github.com/smartystreets/goconvey@v1.6.4/convey/context.go:261 +0x177
    /Users/suxiangyu/go/pkg/mod/github.com/smartystreets/goconvey@v1.6.4/convey/context.go:110 +0xdb
  github.com/jtolds/gls.(*ContextManager).SetValues.func1(0x0)
    /Users/suxiangyu/go/pkg/mod/github.com/jtolds/gls@v4.20.0+incompatible/context.go:97 +0x46a
  github.com/jtolds/gls.EnsureGoroutineId.func1()
    /Users/suxiangyu/go/pkg/mod/github.com/jtolds/gls@v4.20.0+incompatible/gid.go:24 +0x22
  github.com/jtolds/gls._m(0x11c4400?, 0xc00006ed01?)
    /Users/suxiangyu/go/pkg/mod/github.com/jtolds/gls@v4.20.0+incompatible/stack_tags.go:108 +0x22
  github.com/jtolds/gls.github_com_jtolds_gls_markS(0xc000072810?, 0x0?)
    /Users/suxiangyu/go/pkg/mod/github.com/jtolds/gls@v4.20.0+incompatible/stack_tags.go:56 +0x19
  github.com/jtolds/gls.addStackTag(...)
    /Users/suxiangyu/go/pkg/mod/github.com/jtolds/gls@v4.20.0+incompatible/stack_tags.go:49
  github.com/jtolds/gls.EnsureGoroutineId(0xc000072810?)
    /Users/suxiangyu/go/pkg/mod/github.com/jtolds/gls@v4.20.0+incompatible/gid.go:24 +0xd8
  github.com/jtolds/gls.(*ContextManager).SetValues(0xc000054520, 0xc0000727b0, 0xc0000680c0)
    /Users/suxiangyu/go/pkg/mod/github.com/jtolds/gls@v4.20.0+incompatible/context.go:63 +0x16a
    /Users/suxiangyu/go/pkg/mod/github.com/smartystreets/goconvey@v1.6.4/convey/context.go:105 +0x22f
    /Users/suxiangyu/go/pkg/mod/github.com/smartystreets/goconvey@v1.6.4/convey/doc.go:75 +0x8b
  github.com/bytedance/mockey.PatchConvey({0xc00006ef30, 0x3, 0x3})
    /Users/suxiangyu/go/pkg/mod/github.com/bytedance/mockey@v1.0.0-rc.0/convey.go:48 +0x6f
  awesomeProject/so.TestVarStruct2(0xc00010e680)
    /Users/suxiangyu/go/src/awesomeProject/so/main_test.go:22 +0xcd
  testing.tRunner(0xc00010e680, 0x11f0918)
    /usr/local/opt/go/libexec/src/testing/testing.go:1439 +0x102
  created by testing.(*T).Run
    /usr/local/opt/go/libexec/src/testing/testing.go:1486 +0x35f

  goroutine 1 [chan receive]:
  testing.(*T).Run(0xc00010e4e0, {0x11e3f08?, 0x11f3fde97116a?}, 0x11f0918)
    /usr/local/opt/go/libexec/src/testing/testing.go:1487 +0x37a
  testing.runTests.func1(0xc000072630?)
    /usr/local/opt/go/libexec/src/testing/testing.go:1839 +0x6e
  testing.tRunner(0xc00010e4e0, 0xc000135cd8)
    /usr/local/opt/go/libexec/src/testing/testing.go:1439 +0x102
  testing.runTests(0xc000104140?, {0x133bb10, 0x1, 0x1}, {0x1500108?, 0x40?, 0x0?})
    /usr/local/opt/go/libexec/src/testing/testing.go:1837 +0x457
  testing.(*M).Run(0xc000104140)
    /usr/local/opt/go/libexec/src/testing/testing.go:1719 +0x5d9
  main.main()
    _testmain.go:47 +0x1aa

2 total assertions

--- FAIL: TestVarStruct2 (0.00s)

FAIL

Process finished with the exit code 1

Mockey version:

v1.0.0-rc.0

Environment:

go version go1.18 darwin/amd64