1 // Copyright 2022 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
16 func TestExitHooks(t *testing.T) {
17 bmodes := []string{""}
19 t.Skip("skipping due to -short")
21 // Note the HasCGO() test below; this is to prevent the test
22 // running if CGO_ENABLED=0 is in effect.
23 haverace := platform.RaceDetectorSupported(runtime.GOOS, runtime.GOARCH)
24 if haverace && testenv.HasCGO() {
25 bmodes = append(bmodes, "-race")
27 for _, bmode := range bmodes {
28 scenarios := []struct {
40 expected: "orange apple",
45 expected: "blub blix",
51 musthave: "fatal error: internal error: exit hook invoked panic",
56 musthave: "fatal error: internal error: exit hook invoked exit",
60 exe, err := buildTestProg(t, "testexithooks", bmode)
67 bt = " bmode: " + bmode
69 for _, s := range scenarios {
70 cmd := exec.Command(exe, []string{"-mode", s.mode}...)
71 out, _ := cmd.CombinedOutput()
72 outs := strings.ReplaceAll(string(out), "\n", " ")
73 outs = strings.TrimSpace(outs)
75 if s.expected != outs {
76 t.Logf("raw output: %q", outs)
77 t.Errorf("failed%s mode %s: wanted %q got %q", bt,
78 s.mode, s.expected, outs)
80 } else if s.musthave != "" {
81 if !strings.Contains(outs, s.musthave) {
82 t.Logf("raw output: %q", outs)
83 t.Errorf("failed mode %s: output does not contain %q",
87 panic("badly written scenario")