| 1 |
// Copyright 2015 Marc-Antoine Ruel. All rights reserved. |
| 2 |
// Use of this source code is governed under the Apache License, Version 2.0 |
| 3 |
// that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package internal |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"flag" |
| 10 |
"fmt" |
| 11 |
"io" |
| 12 |
"log" |
| 13 |
"os" |
| 14 |
"path/filepath" |
| 15 |
"regexp" |
| 16 |
"strings" |
| 17 |
"testing" |
| 18 |
|
| 19 |
"github.com/google/go-cmp/cmp" |
| 20 |
"github.com/maruel/panicparse/v2/internal/internaltest" |
| 21 |
"github.com/maruel/panicparse/v2/stack" |
| 22 |
) |
| 23 |
|
| 24 |
func TestProcess(t *testing.T) { |
| 25 |
t.Parallel() |
| 26 |
d, err := os.Getwd() |
| 27 |
if err != nil { |
| 28 |
t.Fatal(err) |
| 29 |
} |
| 30 |
data := []struct { |
| 31 |
name string |
| 32 |
palette *Palette |
| 33 |
simil stack.Similarity |
| 34 |
path pathFormat |
| 35 |
filter *regexp.Regexp |
| 36 |
match *regexp.Regexp |
| 37 |
want string |
| 38 |
}{ |
| 39 |
{ |
| 40 |
name: "BasePath", |
| 41 |
palette: testPalette, |
| 42 |
simil: stack.AnyPointer, |
| 43 |
path: basePath, |
| 44 |
want: "GOTRACEBACK=all\npanic: simple\n\nC1: runningA\n Emain Fmain.go:74 GmainR()A\n", |
| 45 |
}, |
| 46 |
{ |
| 47 |
name: "FullPath", |
| 48 |
palette: testPalette, |
| 49 |
simil: stack.AnyValue, |
| 50 |
path: fullPath, |
| 51 |
// "/" is used even on Windows. |
| 52 |
want: fmt.Sprintf("GOTRACEBACK=all\npanic: simple\n\nC1: runningA\n Emain F%s:74 GmainR()A\n", strings.Replace(filepath.Join(filepath.Dir(d), "cmd", "panic", "main.go"), "\\", "/", -1)), |
| 53 |
}, |
| 54 |
{ |
| 55 |
name: "NoColor", |
| 56 |
palette: &Palette{}, |
| 57 |
simil: stack.AnyValue, |
| 58 |
path: basePath, |
| 59 |
want: "GOTRACEBACK=all\npanic: simple\n\n1: running\n main main.go:74 main()\n", |
| 60 |
}, |
| 61 |
{ |
| 62 |
name: "Match", |
| 63 |
palette: testPalette, |
| 64 |
simil: stack.AnyValue, |
| 65 |
path: basePath, |
| 66 |
match: regexp.MustCompile(`notpresent`), |
| 67 |
want: "GOTRACEBACK=all\npanic: simple\n\n", |
| 68 |
}, |
| 69 |
{ |
| 70 |
name: "Filter", |
| 71 |
palette: testPalette, |
| 72 |
simil: stack.AnyValue, |
| 73 |
path: basePath, |
| 74 |
filter: regexp.MustCompile(`notpresent`), |
| 75 |
want: "GOTRACEBACK=all\npanic: simple\n\nC1: runningA\n Emain Fmain.go:74 GmainR()A\n", |
| 76 |
}, |
| 77 |
} |
| 78 |
for i, line := range data { |
| 79 |
line := line |
| 80 |
t.Run(fmt.Sprintf("%d-%s", i, line.name), func(t *testing.T) { |
| 81 |
t.Parallel() |
| 82 |
out := bytes.Buffer{} |
| 83 |
r := bytes.NewReader(internaltest.PanicOutputs()["simple"]) |
| 84 |
if err := process(r, &out, line.palette, line.simil, line.path, false, true, "", line.filter, line.match); err != nil { |
| 85 |
t.Fatal(err) |
| 86 |
} |
| 87 |
compareString(t, line.want, out.String()) |
| 88 |
}) |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
func TestProcessTwoSnapshots(t *testing.T) { |
| 93 |
t.Parallel() |
| 94 |
out := bytes.Buffer{} |
| 95 |
in := bytes.Buffer{} |
| 96 |
in.WriteString("Ya\n") |
| 97 |
in.Write(internaltest.PanicOutputs()["simple"]) |
| 98 |
in.WriteString("Ye\n") |
| 99 |
in.Write(internaltest.PanicOutputs()["int"]) |
| 100 |
in.WriteString("Yo\n") |
| 101 |
err := process(&in, &out, &Palette{}, stack.AnyPointer, basePath, false, true, "", nil, nil) |
| 102 |
if err != nil { |
| 103 |
t.Fatal(err) |
| 104 |
} |
| 105 |
// This is a change detector on main.go. |
| 106 |
want := ("Ya\n" + |
| 107 |
"GOTRACEBACK=all\n" + |
| 108 |
"panic: simple\n\n" + |
| 109 |
"1: running\n" + |
| 110 |
" main main.go:74 main()\n" + |
| 111 |
"Ye\n" + |
| 112 |
"GOTRACEBACK=all\n" + |
| 113 |
"panic: 42\n\n" + |
| 114 |
"1: running\n" + |
| 115 |
" main main.go:93 panicint(0x2a)\n" + |
| 116 |
" main main.go:315 init.func9()\n" + |
| 117 |
" main main.go:76 main()\n" + |
| 118 |
"Yo\n") |
| 119 |
compareString(t, want, out.String()) |
| 120 |
} |
| 121 |
|
| 122 |
func TestMainFn(t *testing.T) { |
| 123 |
t.Parallel() |
| 124 |
// It doesn't do anything since stdin is closed. |
| 125 |
if err := Main(); err != nil { |
| 126 |
t.Fatal(err) |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
// |
| 131 |
|
| 132 |
func compareString(t *testing.T, want, got string) { |
| 133 |
if diff := cmp.Diff(want, got); diff != "" { |
| 134 |
t.Helper() |
| 135 |
t.Fatalf("Mismatch (-want +got):\n%s", diff) |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
func TestMain(m *testing.M) { |
| 140 |
flag.Parse() |
| 141 |
if !testing.Verbose() { |
| 142 |
log.SetOutput(io.Discard) |
| 143 |
} |
| 144 |
// Set the environment variable so the stack doesn't include the info header. |
| 145 |
os.Setenv("GOTRACEBACK", "all") |
| 146 |
os.Exit(m.Run()) |
| 147 |
} |