-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
panic_test.go
65 lines (54 loc) · 1.66 KB
/
panic_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
package errors_test
import (
"bytes"
"os/exec"
"syscall"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gitlab.com/tozd/go/errors"
)
func TestPanic(t *testing.T) {
t.Parallel()
output := &bytes.Buffer{}
cmd := exec.Command("go", "run", "-race", "testdata/panic.go")
cmd.Stdout = output
cmd.Stderr = output
// We have to make a process group and send signals to the whole group.
// See: https://github.com/golang/go/issues/40467
cmd.SysProcAttr = &syscall.SysProcAttr{ //nolint:exhaustruct
Setpgid: true,
}
err := cmd.Start()
require.NoError(t, err)
time.Sleep(10 * time.Second)
// We kill whole process group.
err = syscall.Kill(-cmd.Process.Pid, syscall.SIGINT)
assert.NoError(t, err) //nolint:testifylint
err = cmd.Wait()
var exitError *exec.ExitError
// TODO: Remove workaround.
// Currently "go run" does not return zero exit code when we send INT signal
// to the whole process group even if the child process exits with zero exit code.
// See: https://github.com/golang/go/issues/40467
if errors.As(err, &exitError) && exitError.ExitCode() > 0 {
assert.Equal(t, 1, exitError.ExitCode())
} else {
assert.NoError(t, err) //nolint:testifylint
}
assert.Regexp(t, `^panic: panic error\n`+
`\t?key=value\n`+
`\t?stack trace \(most recent call first\):\n`+
`\t?main\.main\n`+
`\t?\t.*/testdata/panic.go:8\n`+
`\t?runtime\.main\n`+
`\t?\t.*/src/runtime/proc.go:\d+\n`+
`\t?runtime\.goexit\n`+
`\t?\t.*/src/runtime/.*:\d+\n`+
`\t?\n\n`+
`goroutine 1 \[running\]:\n`+
`main\.main\(\)\n`+
`\t.*/testdata/panic.go:8 \+0x..\n`+
`exit status 2\n$`, output.String())
}