File: main_test.go

package info (click to toggle)
panicparse 2.3.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,004 kB
  • sloc: sh: 13; makefile: 5
file content (147 lines) | stat: -rw-r--r-- 3,687 bytes parent folder | download
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright 2015 Marc-Antoine Ruel. All rights reserved.
// Use of this source code is governed under the Apache License, Version 2.0
// that can be found in the LICENSE file.

package internal

import (
	"bytes"
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"path/filepath"
	"regexp"
	"strings"
	"testing"

	"github.com/google/go-cmp/cmp"
	"github.com/maruel/panicparse/v2/internal/internaltest"
	"github.com/maruel/panicparse/v2/stack"
)

func TestProcess(t *testing.T) {
	t.Parallel()
	d, err := os.Getwd()
	if err != nil {
		t.Fatal(err)
	}
	data := []struct {
		name    string
		palette *Palette
		simil   stack.Similarity
		path    pathFormat
		filter  *regexp.Regexp
		match   *regexp.Regexp
		want    string
	}{
		{
			name:    "BasePath",
			palette: testPalette,
			simil:   stack.AnyPointer,
			path:    basePath,
			want:    "GOTRACEBACK=all\npanic: simple\n\nC1: runningA\n    Emain Fmain.go:74 GmainR()A\n",
		},
		{
			name:    "FullPath",
			palette: testPalette,
			simil:   stack.AnyValue,
			path:    fullPath,
			// "/" is used even on Windows.
			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)),
		},
		{
			name:    "NoColor",
			palette: &Palette{},
			simil:   stack.AnyValue,
			path:    basePath,
			want:    "GOTRACEBACK=all\npanic: simple\n\n1: running\n    main main.go:74 main()\n",
		},
		{
			name:    "Match",
			palette: testPalette,
			simil:   stack.AnyValue,
			path:    basePath,
			match:   regexp.MustCompile(`notpresent`),
			want:    "GOTRACEBACK=all\npanic: simple\n\n",
		},
		{
			name:    "Filter",
			palette: testPalette,
			simil:   stack.AnyValue,
			path:    basePath,
			filter:  regexp.MustCompile(`notpresent`),
			want:    "GOTRACEBACK=all\npanic: simple\n\nC1: runningA\n    Emain Fmain.go:74 GmainR()A\n",
		},
	}
	for i, line := range data {
		line := line
		t.Run(fmt.Sprintf("%d-%s", i, line.name), func(t *testing.T) {
			t.Parallel()
			out := bytes.Buffer{}
			r := bytes.NewReader(internaltest.PanicOutputs()["simple"])
			if err := process(r, &out, line.palette, line.simil, line.path, false, true, "", line.filter, line.match); err != nil {
				t.Fatal(err)
			}
			compareString(t, line.want, out.String())
		})
	}
}

func TestProcessTwoSnapshots(t *testing.T) {
	t.Parallel()
	out := bytes.Buffer{}
	in := bytes.Buffer{}
	in.WriteString("Ya\n")
	in.Write(internaltest.PanicOutputs()["simple"])
	in.WriteString("Ye\n")
	in.Write(internaltest.PanicOutputs()["int"])
	in.WriteString("Yo\n")
	err := process(&in, &out, &Palette{}, stack.AnyPointer, basePath, false, true, "", nil, nil)
	if err != nil {
		t.Fatal(err)
	}
	// This is a change detector on main.go.
	want := ("Ya\n" +
		"GOTRACEBACK=all\n" +
		"panic: simple\n\n" +
		"1: running\n" +
		"    main main.go:74 main()\n" +
		"Ye\n" +
		"GOTRACEBACK=all\n" +
		"panic: 42\n\n" +
		"1: running\n" +
		"    main main.go:93  panicint(0x2a)\n" +
		"    main main.go:315 init.func9()\n" +
		"    main main.go:76  main()\n" +
		"Yo\n")
	compareString(t, want, out.String())
}

func TestMainFn(t *testing.T) {
	t.Parallel()
	// It doesn't do anything since stdin is closed.
	if err := Main(); err != nil {
		t.Fatal(err)
	}
}

//

func compareString(t *testing.T, want, got string) {
	if diff := cmp.Diff(want, got); diff != "" {
		t.Helper()
		t.Fatalf("Mismatch (-want +got):\n%s", diff)
	}
}

func TestMain(m *testing.M) {
	flag.Parse()
	if !testing.Verbose() {
		log.SetOutput(ioutil.Discard)
	}
	// Set the environment variable so the stack doesn't include the info header.
	os.Setenv("GOTRACEBACK", "all")
	os.Exit(m.Run())
}