-
Notifications
You must be signed in to change notification settings - Fork 21
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 failed to run tests on Windows #103
Comments
The project using go toolchain, is this leading to the error? |
Thanks for raising the issue, I will take a deep look later. Besides can you try to run this within WSL to see if it works? Thanks. |
Here's
|
Seems like go-toolchain related. I'll try later |
On linux it works but mock failed.
But I guess it's a limitation of xgo itself that the build was successful |
Yes because only a small part of stdlib are injected. See https://github.com/xhd2015/xgo/blob/master/runtime/mock/stdlib.md. |
The failure caused this issue on windows has been fixed in #109 . To upgrade, run: # update xgo
go install github.com/xhd2015/xgo/cmd/[email protected]
# update dependency
go get github.com/xhd2015/xgo/[email protected] |
I still think hook stdlib is quite important, because in other cases we always have the opportunity to get around the need for hooks by writing test-friendly code, but modifications to the standard library prevent the code from compiling on other machines, which makes a monkey patch inevitable. |
@Zxilly OK I got your point. I also feel it would be more feasible if xgo allows user to specify what stdlib funcs they want to mock. I will take a look on this more deeply. By the way, do you have any case that would require mocking specific stdlib functions? You can post an example here or raise another issue so that we have a better understanding of the problem. Thanks. |
func download() (string, error) {
slog.Info("Downloading template")
resp, err := http.Get(BaseURL)
if err != nil {
return "", err
}
defer func(body io.ReadCloser) {
_ = body.Close()
}(resp.Body)
// check status code
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("failed to download template: %s", resp.Status)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(body), nil
} func getCacheFilePath() (string, error) {
dir, err := os.UserCacheDir()
if err != nil {
return "", err
}
cacheDir := filepath.Join(dir, "go-size-analyzer")
err = os.MkdirAll(cacheDir, 0755)
if err != nil {
return "", err
}
file := filepath.Join(cacheDir, fmt.Sprintf("webui-v%s.html", gsa.StaticVersion))
return file, nil
} In fact, both of these examples can be tested by replacing the corresponding calls with variables, but this amounts to adding runtime complexity for the sake of the test |
@Zxilly This is being resolved in this PR: #117 . Will be available in the next version of xgo at 2024-05-20. I tried to support as more stdlib packages as possible, however due the nature of the implementations, these packages cannot be mocked:
User needs to add xgo test --trap-stdlib ./ |
@Zxilly v1.0.36 has been released, see https://github.com/xhd2015/xgo/releases/tag/v1.0.36 Examples: https://github.com/xhd2015/xgo/blob/master/runtime/test/trap_stdlib_any/trap_stdlib_any_test.go#L17 |
Is that possible to automatic set a build tag for |
It is possible to automatically add If all tests go well, I'll make this flag default in the future version. |
Once we fixed the bug in #164, this will be made default. |
@Zxilly 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. |
I created a test with xgo on https://github.com/Zxilly/go-size-analyzer/blob/master/internal/printer/json_xgo_test.go. But when I run the test with
xgo test -v -race -covermode=atomic -cover ./... -tags xgo,embed
It tolds me
The dir structure of
C:\Users\zxilly\.xgo\go-instrument\go1.22.3_C_Us_12_go_pk_mo_go_to_7f0da9bd\
isnothing in the pkgdata directory.
I'm running go 1.22.3 and xgo 1.0.31, build with go install.
The text was updated successfully, but these errors were encountered: