Skip to content

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

Closed
@trim21

Description

@trim21

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)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions