Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xgo mock os.MkdirAll failed #148

Closed
saitofun opened this issue May 26, 2024 · 3 comments
Closed

xgo mock os.MkdirAll failed #148

saitofun opened this issue May 26, 2024 · 3 comments
Milestone

Comments

@saitofun
Copy link

go env

GO111MODULE='on'
GOARCH='arm64'
GOBIN='/Users/sincos/sincos/bin'
GOCACHE='/Users/sincos/sincos/tmp/go-cache'
GOENV='/Users/sincos/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/sincos/sincos/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/sincos/sincos'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/opt/homebrew/Cellar/go/1.22.0/libexec'
GOSUMDB='sum.golang.org'
GOTMPDIR='/Users/sincos/sincos/tmp/go-tmp'
GOTOOLCHAIN='auto'
GOTOOLDIR='/opt/homebrew/Cellar/go/1.22.0/libexec/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.22.0'
GCCGO='gccgo'
AR='ar'
CC='cc'
CXX='c++'
CGO_ENABLED='1'
GOMOD='/Users/sincos/sincos/src/github.com/sincospro/snippets/xgo/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/Users/sincos/sincos/tmp/go-tmp/go-build1010920956=/tmp/go-build -gno-record-gcc-switches -fno-common'

test code:

func MyFunc(output string) error {
        if err := os.MkdirAll(output, os.ModePerm); err != nil {
                return err
        }
        // other code
        return nil
}

func TestMyFunc(t *testing.T) {
        t.Run("FailedToMkDir", func(t *testing.T) {
                mock.Patch(os.MkdirAll, func(string, os.FileMode) error {
                        return nil
                })
        })
}

test command:

xgo test ./... -v

test output:

=== RUN   TestMyFunc
=== RUN   TestMyFunc/FailedToMkDir
--- FAIL: TestMyFunc (0.00s)
    --- FAIL: TestMyFunc/FailedToMkDir (0.00s)
panic: failed to setup mock for: os.MkdirAll [recovered]
        panic: failed to setup mock for: os.MkdirAll

goroutine 4 [running]:
testing.tRunner.func1.2({0x100da6380, 0x140003b5560})
        /Users/sincos/.xgo/go-instrument/go1.22.0_op_ho_Ce_go_1._li_26ff9148/go1.22.0/src/testing/testing.go:1642 +0x1c4
testing.tRunner.func1()
        /Users/sincos/.xgo/go-instrument/go1.22.0_op_ho_Ce_go_1._li_26ff9148/go1.22.0/src/testing/testing.go:1645 +0x334
panic({0x100da6380?, 0x140003b5560?})
        /Users/sincos/.xgo/go-instrument/go1.22.0_op_ho_Ce_go_1._li_26ff9148/go1.22.0/src/runtime/panic.go:770 +0x124
github.com/xhd2015/xgo/runtime/mock.getFunc({0x100d9a660, 0x100e426d8})
        /Users/sincos/sincos/pkg/mod/github.com/xhd2015/xgo/[email protected]/mock/mock.go:54 +0xd4
github.com/xhd2015/xgo/runtime/mock.Patch({0x100d9a660, 0x100e426d8}, {0x100d9a660, 0x100e426d0})
        /Users/sincos/sincos/pkg/mod/github.com/xhd2015/xgo/[email protected]/mock/patch.go:51 +0x1fc
github.com/sincospro/learn/xgo.TestMyFunc.func1(0x14000344b60)
        /Users/sincos/sincos/src/github.com/sincospro/snippets/xgo/xgo_test.go:19 +0x118
testing.tRunner(0x14000344b60, 0x100e426c8)
        /Users/sincos/.xgo/go-instrument/go1.22.0_op_ho_Ce_go_1._li_26ff9148/go1.22.0/src/testing/testing.go:1709 +0x23c
created by testing.(*T).Run in goroutine 3
        /Users/sincos/.xgo/go-instrument/go1.22.0_op_ho_Ce_go_1._li_26ff9148/go1.22.0/src/testing/testing.go:1762 +0x2f4
FAIL    github.com/sincospro/learn/xgo  0.438s
FAIL
exit status 1
@xhd2015
Copy link
Owner

xhd2015 commented May 26, 2024

In current version of xgo, you need to add --trap-stdlib to mock stdlib functions.

See #144.

We are planning to make it default in the future versions.

@xhd2015
Copy link
Owner

xhd2015 commented May 26, 2024

os.MkdirAll has been tested to work with --trap-stdlib, see

.

@xhd2015 xhd2015 added the mock label May 28, 2024
@xhd2015 xhd2015 added this to the v1.1.0 milestone May 30, 2024
@xhd2015
Copy link
Owner

xhd2015 commented May 30, 2024

@saitofun --trap-stdlib has been made default when running xgo test, released with https://github.com/xhd2015/xgo/releases/tag/v1.0.38.

To install xgo v1.0.38:

# update xgo
go install github.com/xhd2015/xgo/cmd/[email protected]

# update dependency
go get github.com/xhd2015/xgo/[email protected]

Thanks.

@xhd2015 xhd2015 added the done label May 30, 2024
@xhd2015 xhd2015 closed this as completed Jun 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants