1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/vendor/gotest.tools/v3/internal/cleanup/cleanup.go
Sebastiaan van Stijn 776cadc7db
vendor: gotest.tools v3.0.3
- assert: fixes a bug that would cause a panic if there were any
  function calls before `assert.Check` on the same line
- golden: create the directory if it does not exist, when run with
  `-test.update-golden`

full diff: https://github.com/gotestyourself/gotest.tools/compare/v3.0.2...v3.0.3

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2020-11-23 12:33:57 +01:00

48 lines
897 B
Go

/*Package cleanup handles migration to and support for the Go 1.14+
testing.TB.Cleanup() function.
*/
package cleanup
import (
"os"
"strings"
)
type cleanupT interface {
Cleanup(f func())
}
// implemented by gotest.tools/x/subtest.TestContext
type addCleanupT interface {
AddCleanup(f func())
}
type logT interface {
Log(...interface{})
}
type helperT interface {
Helper()
}
var noCleanup = strings.ToLower(os.Getenv("TEST_NOCLEANUP")) == "true"
// Cleanup registers f as a cleanup function on t if any mechanisms are available.
//
// Skips registering f if TEST_NOCLEANUP is set to true.
func Cleanup(t logT, f func()) {
if ht, ok := t.(helperT); ok {
ht.Helper()
}
if noCleanup {
t.Log("skipping cleanup because TEST_NOCLEANUP was enabled.")
return
}
if ct, ok := t.(cleanupT); ok {
ct.Cleanup(f)
return
}
if tc, ok := t.(addCleanupT); ok {
tc.AddCleanup(f)
}
}