-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdefers_test.go
131 lines (119 loc) · 3.26 KB
/
defers_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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package defers
import (
"bytes"
"errors"
"fmt"
"math/rand"
"os"
"os/exec"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestDefers(t *testing.T) {
origExitFn := exit
t.Cleanup(func() { exit = origExitFn })
exitcode := make(chan int)
exit = func(code int) { exitcode <- code }
for i := 0; i < 10; i++ {
t.Run(fmt.Sprintf("%d defers", i), func(t *testing.T) {
wantcode := rand.Intn(127)
a := []int{}
for j := 0; j < i; j++ {
j := j
Add(func() { a = append(a, j) })
}
go Exit(wantcode)
if got, want := <-exitcode, wantcode; got != want {
t.Errorf("got exit(%d) call, want %d", got, want)
}
if got, want := len(a), i; got != want {
t.Errorf("len(a) = %d, want %d", got, want)
}
for j := 0; j < len(a); j++ {
if got, want := a[j], i-j-1; got != want {
t.Errorf("a[%d] = %d, want %d", j, got, want)
}
}
c := make(chan struct{})
runc <- c
<-c
})
}
}
func TestProc(t *testing.T) {
if os.Getenv("DEFER_TEST_PROC") == "1" {
Add(func() { fmt.Println("world") })
Add(func() { fmt.Printf("hello ") })
Exit(42)
fmt.Println("you should not see this!")
}
buf := new(bytes.Buffer)
cmd := exec.Command(os.Args[0], "-test.run=TestProc")
cmd.Env = append(os.Environ(), "DEFER_TEST_PROC=1")
cmd.Stdout = buf
err := cmd.Run()
if ee := new(exec.ExitError); errors.As(err, &ee) {
if got, want := ee.ProcessState.ExitCode(), 42; got != want {
t.Errorf("Exit(%[2]d) = %[1]d, want %[2]d", got, want)
}
} else if err != nil {
t.Errorf("cmd.Run() = %q", err)
}
if got, want := buf.String(), "hello world\n"; got != want {
t.Errorf("proc output -want +got\n%s", cmp.Diff(want, got))
}
}
func TestRunPanic(t *testing.T) {
if os.Getenv("DEFER_TEST_PROC") == "1" {
func() {
defer Run()
Add(func() { fmt.Println("world") })
Add(func() { fmt.Printf("hello ") })
panic("panicking")
}()
os.Exit(0) // Should not be reachable.
}
buf := new(bytes.Buffer)
cmd := exec.Command(os.Args[0], "-test.run=TestRunPanic")
cmd.Env = append(os.Environ(), "DEFER_TEST_PROC=1")
cmd.Stdout = buf
err := cmd.Run()
if ee := new(exec.ExitError); errors.As(err, &ee) {
if got, want := ee.ProcessState.ExitCode(), 2; got != want {
t.Errorf("Exit(%[2]d) = %[1]d, want %[2]d", got, want)
}
} else if err != nil {
t.Errorf("cmd.Run() = %q", err)
}
// The subprocess will contain additional test-related output,
// so only check the first line.
line := strings.Split(buf.String(), "\n")[0]
if got, want := line, "hello world"; got != want {
t.Errorf("proc output -want +got\n%s", cmp.Diff(want, got))
}
}
func TestRunNoPanic(t *testing.T) {
if os.Getenv("DEFER_TEST_PROC") == "1" {
func() {
defer Run()
Add(func() { fmt.Println("world") })
Add(func() { fmt.Printf("hello ") })
}()
os.Exit(0)
}
buf := new(bytes.Buffer)
cmd := exec.Command(os.Args[0], "-test.run=TestRunNoPanic")
cmd.Env = append(os.Environ(), "DEFER_TEST_PROC=1")
cmd.Stdout = buf
err := cmd.Run()
if err != nil {
t.Errorf("cmd.Run() = %q, want <nil>", err)
}
// The subprocess will contain additional test-related output,
// so only check the first line.
line := strings.Split(buf.String(), "\n")[0]
if got, want := line, "hello world"; got != want {
t.Errorf("proc output -want +got\n%s", cmp.Diff(want, got))
}
}