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
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@prashantv
Copy link
Contributor

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
Member

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 the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Aug 28, 2020
@cagedmantis cagedmantis added this to the Backlog milestone Aug 28, 2020
@cagedmantis
Copy link
Contributor

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

/cc @mpvl

@ianlancetaylor
Copy link
Contributor

CC @rogpeppe

@ianlancetaylor
Copy link
Contributor

I'll send a CL.

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

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

@golang golang locked and limited conversation to collaborators Aug 28, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants