-
Notifications
You must be signed in to change notification settings - Fork 4
/
atexit_test.go
107 lines (85 loc) · 1.94 KB
/
atexit_test.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package atexit
import (
"fmt"
"io/ioutil"
"os/exec"
"path"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestRegister(t *testing.T) {
require := require.New(t)
current := len(handlers)
Register(func() {})
require.Equal(current+1, len(handlers), "register")
}
func TestCancel(t *testing.T) {
require := require.New(t)
id := Register(func() {})
require.NoError(id.Cancel())
_, ok := handlers[id]
require.False(ok, "cancel")
}
func TestHandler(t *testing.T) {
require := require.New(t)
root, err := ioutil.TempDir("", "atexit-test")
require.NoError(err, "TempDir")
progFile := path.Join(root, "main.go")
err = ioutil.WriteFile(progFile, []byte(testprog), 0666)
require.NoError(err, "prog")
here, err := filepath.Abs(".")
require.NoError(err, "abs .")
mod := fmt.Sprintf(modTmpl, here)
modFile := path.Join(root, "go.mod")
err = ioutil.WriteFile(modFile, []byte(mod), 0666)
require.NoError(err, "mod")
outFile := path.Join(root, "main.out")
arg := time.Now().UTC().String()
err = exec.Command("go", "run", progFile, outFile, arg).Run()
require.Error(err, "run")
data, err := ioutil.ReadFile(outFile)
require.NoError(err, "read out")
require.Equal(arg, string(data), "output")
}
var (
testprog = `
// Test program for atexit, gets output file and data as arguments and writes
// data to output file in atexit handler.
package main
import (
"flag"
"fmt"
"io/ioutil"
"github.com/tebeka/atexit"
)
var outfile = ""
var data = ""
func handler() {
ioutil.WriteFile(outfile, []byte(data), 0666)
}
func badHandler() {
n := 0
fmt.Println(1/n)
}
func unusedHandler() {
ioutil.WriteFile(outfile, []byte("\nunused"), 0666)
}
func main() {
flag.Parse()
outfile = flag.Arg(0)
data = flag.Arg(1)
atexit.Register(handler)
id := atexit.Register(unusedHandler)
atexit.Register(badHandler)
id.Cancel()
atexit.Exit(1)
}
`
modTmpl = `
module testexit
go 1.13
replace github.com/tebeka/atexit => %s
`
)