cch123 / supermonkey

Patch all Go functions for testing
MIT License
250 stars 17 forks source link

supermonkey seems to not suit the higher version of golang #20

Open ZenKaiii opened 1 year ago

ZenKaiii commented 1 year ago

Now we are using Go 1.14.2. It seems to be okay with Patch function. But when we need to upgrade our golang version to 1.15 or higher, the supermonkey.Patch seems not okay. Mock is ineffective. Do you konw why, or do you have a plan for suiting golang higher version. Thank you.

cch123 commented 1 year ago

I tried this patch demo in go 1.15.14, and it worked well

package main

import (
    "fmt"

    sm "github.com/cch123/supermonkey"
)

func main() {
    fmt.Println("original function output:")
    heyHeyHey()

    patchGuard := sm.PatchByFullSymbolName("main.heyHeyHey", func() {
        fmt.Println("please be polite")
    })
    fmt.Println("after patch, function output:")
    heyHeyHey()

    patchGuard.Unpatch()
    fmt.Println("unpatch, then output:")
    heyHeyHey()
}

//go:noinline
func heyHeyHey() {
    fmt.Println("fake")
}

can you provide a reproducible demo for your problem?

ZenKaiii commented 1 year ago

I write golang code with go 1.17.8. Codes are down below. But the result is "hello" instead of "yeah func hello() { println("hello") }

func TestName(t *testing.T) { supermonkey.Patch(hello, func() { println("yeah") }) hello() }

cch123 commented 1 year ago

I write golang code with go 1.17.8. Codes are down below. But the result is "hello" instead of "yeah func hello() { println("hello") }

func TestName(t *testing.T) { supermonkey.Patch(hello, func() { println("yeah") }) hello() }

did you run your demo with proper flags?

Warning : use go test -ldflags="-s=false" -gcflags="-l" to enable symbol table and disable inline.