Skip to content

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

@prashantv

Description

@prashantv

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions