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: Don't silently ignore nested t.Cleanup calls #41085

Closed
prashantv opened this issue Aug 28, 2020 · 5 comments
Closed

testing: Don't silently ignore nested t.Cleanup calls #41085

prashantv opened this issue Aug 28, 2020 · 5 comments
Assignees
Milestone

Comments

@prashantv
Copy link
Contributor

@prashantv prashantv commented Aug 28, 2020

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

$ go version
go version go1.15 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="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/user/.cache/go-build"
GOENV="/home/user/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/opt/go/path/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/opt/go/path"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/user/.gimme/versions/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/user/.gimme/versions/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/user/go-test/go.mod"
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-build146632005=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Call t.Cleanup from inside of a cleanup function,

package main

import (
	"fmt"
	"testing"
)

func TestCleanup(t *testing.T) {
	t.Cleanup(func() {
		fmt.Println("cleanup initial")
		t.Cleanup(func() {
			fmt.Println("nested cleanup")
		})
	})

	fmt.Println("run test")
}

What did you expect to see?

Either the newly added Cleanup function to be run immediately after the initial clean up (to respect LIFO order) or for the Cleanup call to cause the test to be marked as failed.

What did you see instead?

The nested cleanup function is ignored silently,

=== RUN   TestCleanup
run test
cleanup initial
--- PASS: TestCleanup (0.00s)
PASS
ok  	command-line-arguments	0.004s
@seankhliao
Copy link
Contributor

@seankhliao seankhliao commented Aug 28, 2020

I believe this is a misuse of t.Cleanup, your second call is only registered when the first one is run (at the end of the tests) and not at the beginning of the test

@cagedmantis cagedmantis added this to the Backlog milestone Aug 28, 2020
@cagedmantis
Copy link
Contributor

@cagedmantis cagedmantis commented Aug 28, 2020

This does seem like a misuse of t.Cleanup. Silently failing seems odd though.

/cc @mpvl

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Aug 28, 2020

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Aug 28, 2020

I'll send a CL.

@ianlancetaylor ianlancetaylor self-assigned this Aug 28, 2020
@gopherbot
Copy link

@gopherbot gopherbot commented Aug 28, 2020

Change https://golang.org/cl/251457 mentions this issue: testing: run a Cleanup registered by a Cleanup

@gopherbot gopherbot closed this in 54e18f1 Aug 28, 2020
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
5 participants
You can’t perform that action at this time.