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: t.Cleanup should report non-helper caller information #38800

Closed
prashantv opened this issue May 1, 2020 · 3 comments
Closed

testing: t.Cleanup should report non-helper caller information #38800

prashantv opened this issue May 1, 2020 · 3 comments
Milestone

Comments

@prashantv
Copy link
Contributor

@prashantv prashantv commented May 1, 2020

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

$ go version
go version go1.14.2 linux/amd64

Does this issue reproduce with the latest release?

Yes.

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/prashant/.cache/go-build"
GOENV="/home/prashant/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/prashant/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/prashant/.gimme/versions/go1.14.2.linux.amd64"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/prashant/.gimme/versions/go1.14.2.linux.amd64/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build997550945=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Used t.Cleanup and passed in t.Helper so that the original caller of the Cleanup function would be mentioned
https://play.golang.org/p/AzLAUWwuSGJ

What did you expect to see?

Some mention of the non-helper file:line that called a helper function that then used a cleanup.

E.g.,

=== RUN   TestFoo
    TestFoo: prog.go:6: Failed to create: foo
    TestFoo: prog.go:8: Failed to create: bar
    TestFoo: prog.go:6: Delete temporary file failed: bar
    TestFoo: prog.go:8: Delete temporary file failed: foo
--- FAIL: TestFoo (0.00s)

It may also want to mention cleanup from that line

=== RUN   TestFoo
    TestFoo: prog.go:6: Failed to create: foo
    TestFoo: prog.go:8: Failed to create: bar
    TestFoo: prog.go:6 (cleanup): Delete temporary file failed: bar
    TestFoo: prog.go:8 (cleanup): Delete temporary file failed: foo
--- FAIL: TestFoo (0.00s)

What did you see instead?

=== RUN   TestFoo
    TestFoo: prog.go:6: Failed to create: foo
    TestFoo: prog.go:8: Failed to create: bar
    TestFoo: testing.go:789: Delete temporary file failed: bar
    TestFoo: testing.go:789: Delete temporary file failed: foo
--- FAIL: TestFoo (0.00s)

Note, removing the t.Helper inside the cleanup results in the helper Create function showing up:

=== RUN   TestFoo
    TestFoo: prog.go:6: Failed to create: foo
    TestFoo: prog.go:8: Failed to create: bar
    TestFoo: prog.go:17: Delete temporary file failed: bar
    TestFoo: prog.go:17: Delete temporary file failed: foo
--- FAIL: TestFoo (0.00s)

Other Details

Only the testing library knows about which methods in the callstack have called t.Helper, so it's not possible for the helper function to track caller itself and report it in the Cleanup function. Ideally, at the point of Cleanup, the caller information is recorded, so it can be used for failures in cleanup.

@ianlancetaylor ianlancetaylor changed the title t.Cleanup should report non-helper caller information testing: t.Cleanup should report non-helper caller information May 1, 2020
@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented May 1, 2020

I think the suggestion here is: when Cleanup is called, record the caller, ignoring helper functions. Then, if a cleanup function calls t.Helper, and it calls t.Log, report the recorded caller rather than an unhelpful line in testing.go.

@mlowicki
Copy link
Contributor

@mlowicki mlowicki commented May 3, 2020

I'll take a look at this issue.

@gopherbot
Copy link

@gopherbot gopherbot commented May 4, 2020

Change https://golang.org/cl/232237 mentions this issue: testing: Func passed to Cleanup reports Cleanup as caller

@gopherbot gopherbot closed this in 7db566f May 5, 2020
xujianhai666 added a commit to xujianhai666/go-1 that referenced this issue May 21, 2020
Record the caller when Cleanup is called to report it with t.Log
instead of unhelpful line in testing.go.

Fixes golang#38800

Change-Id: I3136f5d92a0e5a48f8b32a2e13b2521bc91d72d1
Reviewed-on: https://go-review.googlesource.com/c/go/+/232237
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.