从CLI运行时,GO中的猴子补丁显示不同的结果

发布于 2025-01-30 13:47:07 字数 542 浏览 1 评论 0原文

我正在使用猴子补丁。当我在VSCODE中调试以下代码时,它显示函数proc.signal返回编程错误。

func TestCheckProcessRunning(t *testing.T) {
    monkey.Patch((*os.Process).Signal, func(p *os.Process, sig os.Signal) error {
        return errors.New("Signal failed")
    })
    proc := &os.Process{}
    sig_e := proc.Signal(syscall.Signal(0))
    fmt.Printf("%s\n", sig_e)

}
Signal failed

但是,当我尝试使用 GO测试来运行测试时。,该补丁不再应用并且有不同的错误:

os: process not initialized 

对我做错了什么的想法?

I am using Monkey Patching in Go. When I debug the following code in VSCode it shows that the function proc.Signal return the error programmed.

func TestCheckProcessRunning(t *testing.T) {
    monkey.Patch((*os.Process).Signal, func(p *os.Process, sig os.Signal) error {
        return errors.New("Signal failed")
    })
    proc := &os.Process{}
    sig_e := proc.Signal(syscall.Signal(0))
    fmt.Printf("%s\n", sig_e)

}
Signal failed

But when I tried to run the test using go test . , the patch is no longer applied and got a different error:

os: process not initialized 

Any idea of what I am doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魔法少女 2025-02-06 13:47:07

看来它需要标志-gcflags = -l

go test -gcflags=-l .

It seems it needed the flag -gcflags=-l

go test -gcflags=-l .
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文