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

testing: calling os.Getenv in TestMain does not factor into test cache #59849

Closed
rittneje opened this issue Apr 26, 2023 · 1 comment
Closed

Comments

@rittneje
Copy link

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

$ go version
go version go1.19.8 darwin/amd64

Does this issue reproduce with the latest release?

Yes (1.20.3)

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

go env Output
$ go env
GO111MODULE="auto"
GOARCH="amd64"
GOBIN=""
GOCACHE="/tmp/.gocache"
GOENV="/Users/rittneje/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/rittneje/test/pkg/mod"
GONOPROXY="[redacted]"
GONOSUMDB="[redacted]"
GOOS="darwin"
GOPATH="/Users/rittneje/test"
GOPRIVATE="[redacted]"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/Users/rittneje/go1.19.8"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/rittneje/go1.19.8/pkg/tool/darwin_amd64"
GOVCS="public:git,[redacted]:git,private:off"
GOVERSION="go1.19.8"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/rittneje/test/src/envtest/go.mod"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/kf/kr7_s3xx0l12zbj3jrn082hmzy5gvy/T/go-build1303705698=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

package envtest

import (
	"os"
	"testing"
)

var foobar string

func TestMain(m *testing.M) {
	foobar = os.Getenv("FOOBAR")
	os.Exit(m.Run())
}

func TestFoo(t *testing.T) {
	if foobar != "123" {
		t.FailNow()
	}
}
$ FOOBAR=123 go test -v .
=== RUN   TestFoo
--- PASS: TestFoo (0.00s)
PASS
ok      envtest 0.142s

$ FOOBAR=1234 go test -v .
=== RUN   TestFoo
--- PASS: TestFoo (0.00s)
PASS
ok      envtest (cached)

What did you expect to see?

The second test run should fail.

What did you see instead?

The call to os.Getenv did not properly factor into the cache, so it incorrectly reported the cached result.

Note that calling os.Getenv directly inside the unit test works correctly.

package envtest

import (
	"os"
	"testing"
)

func TestFoo(t *testing.T) {
	if os.Getenv("FOOBAR") != "123" {
		t.FailNow()
	}
}
$ FOOBAR=123 go test -v .
=== RUN   TestFoo
--- PASS: TestFoo (0.00s)
PASS
ok      envtest 0.152s

$ FOOBAR=1234 go test -v .
=== RUN   TestFoo
--- FAIL: TestFoo (0.00s)
FAIL
FAIL    envtest 0.128s
FAIL
@bcmills
Copy link
Contributor

bcmills commented Apr 26, 2023

Duplicate of #44625

@bcmills bcmills marked this as a duplicate of #44625 Apr 26, 2023
@bcmills bcmills closed this as not planned Won't fix, can't repro, duplicate, stale Apr 26, 2023
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