Source file
src/runtime/ehooks_test.go
1
2
3
4
5 package runtime_test
6
7 import (
8 "os/exec"
9 "strings"
10 "testing"
11 )
12
13 func TestExitHooks(t *testing.T) {
14 if testing.Short() {
15 t.Skip("skipping due to -short")
16 }
17
18 scenarios := []struct {
19 mode string
20 expected string
21 musthave []string
22 }{
23 {
24 mode: "simple",
25 expected: "bar foo",
26 },
27 {
28 mode: "goodexit",
29 expected: "orange apple",
30 },
31 {
32 mode: "badexit",
33 expected: "blub blix",
34 },
35 {
36 mode: "panics",
37 musthave: []string{
38 "fatal error: exit hook invoked panic",
39 "main.testPanics",
40 },
41 },
42 {
43 mode: "callsexit",
44 musthave: []string{
45 "fatal error: exit hook invoked exit",
46 },
47 },
48 {
49 mode: "exit2",
50 expected: "",
51 },
52 }
53
54 exe, err := buildTestProg(t, "testexithooks")
55 if err != nil {
56 t.Fatal(err)
57 }
58
59 for _, s := range scenarios {
60 cmd := exec.Command(exe, []string{"-mode", s.mode}...)
61 out, _ := cmd.CombinedOutput()
62 outs := strings.ReplaceAll(string(out), "\n", " ")
63 outs = strings.TrimSpace(outs)
64 if s.expected != "" && s.expected != outs {
65 t.Fatalf("failed %s: wanted %q\noutput:\n%s",
66 s.mode, s.expected, outs)
67 }
68 for _, need := range s.musthave {
69 if !strings.Contains(outs, need) {
70 t.Fatalf("failed mode %s: output does not contain %q\noutput:\n%s",
71 s.mode, need, outs)
72 }
73 }
74 if s.expected == "" && s.musthave == nil && outs != "" {
75 t.Errorf("failed mode %s: wanted no output\noutput:\n%s", s.mode, outs)
76 }
77 }
78 }
79
View as plain text