forked from pkg/errors
-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathnormalize_test.go
More file actions
200 lines (163 loc) · 6.28 KB
/
normalize_test.go
File metadata and controls
200 lines (163 loc) · 6.28 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package errors
import (
stderrors "errors"
"fmt"
"regexp"
"strings"
"testing"
"unsafe"
)
type hackedStringArg struct {
raw []byte
}
func (h hackedStringArg) FreezeStr() string {
return string(append([]byte(nil), h.raw...))
}
func errorMatches(t *testing.T, err error, re string) {
if err == nil && re != "" {
t.Errorf("nil error doesn't match %s", re)
return
}
match, reErr := regexp.MatchString(re, err.Error())
if reErr != nil {
t.Errorf("invalid regexp %s (%s)", re, reErr.Error())
return
}
if !match {
t.Errorf("error %s doesn't match %s", err.Error(), re)
return
}
t.Logf("passed: %s ~= %s", err.Error(), re)
}
func TestCauseInErrorMessage(t *testing.T) {
errTest := Normalize("this error just for testing", RFCCodeText("Internal:Test"))
wrapped := errTest.Wrap(New("everything is alright :)"))
errorMatches(t, wrapped, `\[Internal:Test\]this error just for testing: everything is alright :\)`)
notWrapped := errTest.GenWithStack("everything is alright")
errorMatches(t, notWrapped, `^\[Internal:Test\]everything is alright$`)
}
func TestWrappedNamedErrorGenWithStackByArgsFormatsCauseStack(t *testing.T) {
errTest := Normalize("named error: %s", RFCCodeText("Internal:Test"))
err := errTest.Wrap(New("cause error")).GenWithStackByArgs("wrapped")
if _, ok := err.(fmt.Formatter); !ok {
t.Fatalf("zap requires fmt.Formatter to emit errorVerbose for stackful errors, got %T", err)
}
formatted := fmt.Sprintf("%+v", err)
if !strings.Contains(formatted, "github.com/pingcap/errors.TestWrappedNamedErrorGenWithStackByArgsFormatsCauseStack") {
t.Fatalf("formatted error does not contain the wrapped cause stack:\n%s", formatted)
}
if !strings.Contains(formatted, "[Internal:Test]named error: wrapped") {
t.Fatalf("formatted error does not contain named error context:\n%s", formatted)
}
}
func TestWrappedNamedErrorFormatsStacklessCause(t *testing.T) {
errTest := Normalize("named error: %s", RFCCodeText("Internal:Test"))
err := errTest.Wrap(stderrors.New("plain cause")).GenWithStackByArgs("wrapped")
formatted := fmt.Sprintf("%+v", err)
wantPrefix := "plain cause\n[Internal:Test]named error: wrapped\n"
if !strings.HasPrefix(formatted, wantPrefix) {
t.Fatalf("unexpected formatted error prefix:\ngot: %q\nwant prefix: %q", formatted, wantPrefix)
}
if !strings.Contains(formatted, "github.com/pingcap/errors.TestWrappedNamedErrorFormatsStacklessCause") {
t.Fatalf("formatted error does not contain the generated stack:\n%s", formatted)
}
}
func TestRedactFormatter(t *testing.T) {
rv := 34.03498
v := &redactFormatter{rv}
for _, f := range []string{"%d", "%.2d"} {
a := fmt.Sprintf(f, v)
b := fmt.Sprintf("‹"+f+"›", rv)
if a != b {
t.Errorf("%s != %s", a, b)
}
}
v = &redactFormatter{"‹"}
if a := fmt.Sprintf("%s", v); a != "‹‹‹›" {
t.Errorf("%s != <<<>", a)
}
}
func TestGenWithStackByArgsNoCloneByDefault(t *testing.T) {
errTest := Normalize("Incorrect time value: '%s'", RFCCodeText("Internal:Test"))
origin := []byte("120120519090607")
arg := *(*string)(unsafe.Pointer(&origin))
err := errTest.GenWithStackByArgs(arg)
copy(origin, "1 1:1:1.0000027")
got := err.(*withStack).error.(*Error).GetMsg()
want := "Incorrect time value: '1 1:1:1.0000027'"
if got != want {
t.Fatalf("message should track source bytes by default, got %q, want %q", got, want)
}
}
func TestGenWithStackByArgsFreezeHackedStringArg(t *testing.T) {
errTest := Normalize("Incorrect time value: '%s'", RFCCodeText("Internal:Test"))
origin := []byte("120120519090607")
arg := hackedStringArg{raw: origin}
err := errTest.GenWithStackByArgs(arg)
copy(origin, "1 1:1:1.0000027")
got := err.(*withStack).error.(*Error).GetMsg()
want := "Incorrect time value: '120120519090607'"
if got != want {
t.Fatalf("message changed after source bytes mutated, got %q, want %q", got, want)
}
}
func TestFastGenByArgsFreezeHackedStringArg(t *testing.T) {
errTest := Normalize("Incorrect time value: '%s'", RFCCodeText("Internal:Test"))
origin := []byte("120120519090607")
arg := hackedStringArg{raw: origin}
err := errTest.FastGenByArgs(arg)
copy(origin, "1 1:1:1.0000027")
got := err.(*withStack).error.(*Error).GetMsg()
want := "Incorrect time value: '120120519090607'"
if got != want {
t.Fatalf("message changed after source bytes mutated, got %q, want %q", got, want)
}
}
func TestGenWithStackFreezeHackedStringArg(t *testing.T) {
errTest := Normalize("Incorrect time value: '%s'", RFCCodeText("Internal:Test"))
origin := []byte("120120519090607")
arg := hackedStringArg{raw: origin}
err := errTest.GenWithStack("Incorrect time value: '%s'", arg)
copy(origin, "1 1:1:1.0000027")
got := err.(*withStack).error.(*Error).GetMsg()
want := "Incorrect time value: '120120519090607'"
if got != want {
t.Fatalf("message changed after source bytes mutated, got %q, want %q", got, want)
}
}
func TestFastGenFreezeHackedStringArg(t *testing.T) {
errTest := Normalize("Incorrect time value: '%s'", RFCCodeText("Internal:Test"))
origin := []byte("120120519090607")
arg := hackedStringArg{raw: origin}
err := errTest.FastGen("Incorrect time value: '%s'", arg)
copy(origin, "1 1:1:1.0000027")
got := err.(*withStack).error.(*Error).GetMsg()
want := "Incorrect time value: '120120519090607'"
if got != want {
t.Fatalf("message changed after source bytes mutated, got %q, want %q", got, want)
}
}
func TestGenWithStackByCauseFreezeHackedStringArg(t *testing.T) {
errTest := Normalize("Incorrect time value: '%s'", RFCCodeText("Internal:Test"))
origin := []byte("120120519090607")
arg := hackedStringArg{raw: origin}
err := errTest.GenWithStackByCause(arg)
copy(origin, "1 1:1:1.0000027")
got := err.(*withStack).error.(*Error).GetMsg()
want := "Incorrect time value: '120120519090607'"
if got != want {
t.Fatalf("message changed after source bytes mutated, got %q, want %q", got, want)
}
}
func TestFastGenWithCauseFreezeHackedStringArg(t *testing.T) {
errTest := Normalize("Incorrect time value: '%s'", RFCCodeText("Internal:Test"))
origin := []byte("120120519090607")
arg := hackedStringArg{raw: origin}
err := errTest.FastGenWithCause(arg)
copy(origin, "1 1:1:1.0000027")
got := err.(*withStack).error.(*Error).GetMsg()
want := "Incorrect time value: '120120519090607'"
if got != want {
t.Fatalf("message changed after source bytes mutated, got %q, want %q", got, want)
}
}