Skip to content

go test cache check wrong opened file when testing if user do os.Chdir #47305

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

Closed
trim21 opened this issue Jul 20, 2021 · 4 comments
Closed

go test cache check wrong opened file when testing if user do os.Chdir #47305

trim21 opened this issue Jul 20, 2021 · 4 comments

Comments

@trim21
Copy link

trim21 commented Jul 20, 2021

What version of Go are you using (go version)?

$ go version
go version go1.16.6 windows/amd64

Does this issue reproduce with the latest release?

Yes, I think go1.16.6 is the latest release now.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=$HOME\AppData\Local\go-build
set GOENV=$HOME\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=$HOME\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=$HOME\go
set GOPRIVATE=
set GOPROXY=https://goproxy.cn,direct
set GOROOT=$HOME\scoop\apps\go\current
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=$HOME\scoop\apps\go\current\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.16.6
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=\path\to\my\go\project\go.mod
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -fmessage-length=0 -fdebug-prefix-map=$HOME\AppData\Local\Temp\go-build3183732203=/tmp/go-build -gno-record-gcc-switches

What did you do?

I'm testing my go code with some external files as deps.

func TestZipArchive(t *testing.T) {
	r, err := os.Open("./testdata/big_file.bin")
	assert.Nil(t, err)
	defer r.Close()

   // do something
}

and there is an underscore importing doing this:

this changes the CWD to PWD, which is the root of my project directory.

import (
	"os"
	"path"
	"runtime"
)

func init() {
	_, filename, _, _ := runtime.Caller(0)
	dir := path.Join(path.Dir(filename), "..", "..")
	if err := os.Chdir(dir); err != nil {
		panic(err)
	}
}

So, then os.Open("./testdata/big_file.bin") should open $PWD/testdata/big_file.bin, not pkg/testdata/big_file.bin

What did you expect to see?

go tests are cached successfully with the real files opened, not the arguments.

What did you see instead?

$ go test ./pkg/...
ok      some/pkg (cached)
$ go test ./pkg/...
ok      some/pkg (cached)
$ touch ./pkg/testdata/big_file.bin
touch: cannot touch './pkg/testdata/big_file.bin': No such file or directory
$ mkdir ./pkg/testdata
$ touch ./pkg/testdata/big_file.bin
$ go test ./pkg/...
ok      some/pkg 0.100s
$ head -c 500 /dev/urandom > testdata/big_file.bin
$ go test ./pkg/...
ok      some/pkg (cached)
@trim21 trim21 changed the title go test cache check wrong file when testing if user do os.Chdir go test cache check wrong opened file when testing if user do os.Chdir Jul 20, 2021
@seankhliao
Copy link
Member

can you show the directory/file layout relative to the root?

@trim21
Copy link
Author

trim21 commented Jul 20, 2021

can you show the directory/file layout relative to the root?

I think you can re-produce it with this repo: https://github.com/Trim21/go-bug-47305

/path/to/some/go.{mod,sum}
/path/to/some/pkg/code.go
/path/to/some/pkg/code_test.go
/path/to/some/testdata/big_file.bin

@seankhliao
Copy link
Member

Duplicate of #44625

@seankhliao seankhliao marked this as a duplicate of #44625 Jul 20, 2021
@trim21
Copy link
Author

trim21 commented Jul 20, 2021

Duplicate of #44625

Oh, sorry for the trouble! I didn't find it.

@golang golang locked and limited conversation to collaborators Jul 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants