-
Notifications
You must be signed in to change notification settings - Fork 50
/
cleanup.go
49 lines (42 loc) · 898 Bytes
/
cleanup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
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)
}
}