/
cleanup.go
56 lines (49 loc) · 1.18 KB
/
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
50
51
52
53
54
55
56
package test
import (
"os"
"os/signal"
"testing"
)
// CleanupAll cleans all created contexts
func CleanupAll() {
Cleanup(contexts...)
}
// Cleanup for all given contexts
func Cleanup(contexts ...*Context) {
for _, ctx := range contexts {
ctx.Cleanup()
}
}
// Cleanup iterates through the list of registered CleanupFunc functions and calls them
func (ctx *Context) Cleanup() {
for _, f := range ctx.CleanupList {
doCleanup(ctx.T, f)
}
}
// AddToCleanup adds the cleanup function as the first function to the cleanup list,
// we want to delete the last thing first
func (ctx *Context) AddToCleanup(f CleanupFunc) {
ctx.CleanupList = append([]CleanupFunc{f}, ctx.CleanupList...)
}
// CleanupOnInterrupt will execute the function cleanup if an interrupt signal is caught
func CleanupOnInterrupt(t *testing.T, cleanup CleanupFunc) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for range c {
t.Logf("Test interrupted, cleaning up.")
doCleanup(t, cleanup)
os.Exit(2)
}
}()
}
func doCleanup(t *testing.T, cleanup CleanupFunc) {
if !PerformCleanup(t) {
t.Log("Skipping cleanup!")
return
}
err := cleanup()
if err != nil {
t.Error(err)
}
}