-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestfault.go
More file actions
229 lines (194 loc) · 5.29 KB
/
testfault.go
File metadata and controls
229 lines (194 loc) · 5.29 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Copyright 2014, Surul Software Labs GmbH
// All rights reserved.
/*
Package testfault provides utilities to help with testing in conjunction with the fault library.
*/
package testfault
import (
"fmt"
"regexp"
"strings"
"github.com/surullabs/fault"
)
type site struct {
fn string
call *fault.Call
err error
}
type Recording []site
type recorder struct {
sites []site
}
func (r Recording) ErrorMatches(index int, match string) bool {
if index >= len(r) {
return false
}
if r[index].err == nil {
return false
}
return regexp.MustCompile(match).MatchString(r[index].err.Error())
}
func (r Recording) TrackErrors(e Recording) (err error) {
if len(e) > len(r) {
return fmt.Errorf("error recording has %d entries while master has %d", len(e), len(r))
}
for i, s := range e {
if !r[i].call.Equal(s.call) || r[i].fn != s.fn {
return fmt.Errorf("recording number %d doesn't match master", i)
}
if s.err != nil {
r[i].err = s.err
}
}
return
}
func (r Recording) AllErrorsSeen() bool {
for _, s := range r {
if s.err == nil {
return false
}
}
return true
}
func newRecorder() *recorder { return &recorder{sites: make([]site, 0)} }
func (r *recorder) last() int { return len(r.sites) - 1 }
func (r *recorder) record() {
stack := fault.ReadStack("")
callIndex := -1
for i, call := range stack {
if strings.HasPrefix(call.Name, testCheckerPrefix) {
callIndex = i
break
}
}
site := site{}
if callIndex != -1 && len(stack) > (callIndex+1) {
site.call = &stack[callIndex+1]
site.fn = strings.TrimPrefix(stack[callIndex].Name, testCheckerPrefix)
}
r.sites = append(r.sites, site)
}
func (r *recorder) trackError(err error) {
if len(r.sites) > 0 {
r.sites[len(r.sites)-1].err = err
}
}
var testCheckerPrefix = fault.TypePrefix(&TestChecker{}) + "."
type failure struct {
index int
err error
}
func (f *failure) fail(index int) error {
if f != nil && f.index == index {
return f.err
}
return nil
}
// TestChecker provides an implementation of Checker for use in tests.
type TestChecker struct {
recorder *recorder
fail *failure
checker fault.FaultCheck
onError func(...interface{})
}
func NewTestChecker(onError func(...interface{})) *TestChecker {
checker := fault.NewChecker().SetFaulter(&fault.DebugFaulter{fault.TypePrefix(&TestChecker{})})
return &TestChecker{recorder: newRecorder(), checker: checker, onError: onError}
}
type Resetter struct {
Reset func()
}
func (t *TestChecker) StartRecording() { t.recorder = newRecorder() }
func (t *TestChecker) Recording() Recording { return t.recorder.sites }
// Fail will create an artificial failure for the i'th call to one of the
// checking methods. Only one failure can be enqueued at a time.
func (t *TestChecker) FailAt(index int, err error) { t.fail = &failure{index, err} }
// ResetFailures clears any enqueued failures.
func (t *TestChecker) ResetFailures() { t.fail = nil }
func (t *TestChecker) failNow() error { return t.fail.fail(t.recorder.last()) }
func (t *TestChecker) Patch(orig *fault.FaultCheck) Resetter {
original := *orig
t.checker = original
*orig = t
return Resetter{func() { *orig = original }}
}
func (t *TestChecker) OnError(fn func(...interface{})) Resetter {
orig := t.onError
t.onError = fn
return Resetter{func() { t.onError = orig }}
}
func (t *TestChecker) RecoverPanic(errPtr *error, panicked interface{}) {
t.checker.RecoverPanic(errPtr, panicked)
t.recorder.trackError(*errPtr)
if t.onError != nil && *errPtr != nil {
trace := fault.GetTrace(*errPtr)
if len(trace) == 0 {
t.onError(*errPtr)
return
}
runnerIndex := -1
for i, tr := range trace {
if tr.Name == "testing.tRunner" {
runnerIndex = i
break
}
}
prefix := "\n"
if runnerIndex > 0 {
parts := strings.Split(trace[runnerIndex-1].Name, ".")
prefix = "Failure in " + parts[len(parts)-1] + prefix
}
t.onError(prefix + fault.VerboseTrace(*errPtr))
}
}
// Recover implements FaultCheck.Recover
func (t *TestChecker) Recover(errPtr *error) {
t.RecoverPanic(errPtr, recover())
}
// True implements FaultCheck.True
func (t *TestChecker) True(condition bool, errStr string) {
t.recorder.record()
if failed := t.failNow(); failed != nil {
condition = false
}
t.checker.True(condition, errStr)
}
// Truef implements FaultCheck.Truef
func (t *TestChecker) Truef(condition bool, format string, args ...interface{}) {
t.recorder.record()
if failed := t.failNow(); failed != nil {
condition = false
}
t.checker.Truef(condition, format, args...)
}
// Return implements FaultCheck.Return
func (t *TestChecker) Return(i interface{}, err error) interface{} {
t.recorder.record()
if failed := t.failNow(); failed != nil && err == nil {
err = failed
}
return t.checker.Return(i, err)
}
// Error implements FaultCheck.Error
func (t *TestChecker) Error(err error) {
t.recorder.record()
if failed := t.failNow(); failed != nil && err == nil {
err = failed
}
t.checker.Error(err)
}
// Output implements FaultCheck.Output
func (t *TestChecker) Output(i interface{}, err error) interface{} {
t.recorder.record()
if failed := t.failNow(); failed != nil && err == nil {
err = failed
}
return t.checker.Output(i, err)
}
func (t *TestChecker) Failure(err error) fault.Fault {
t.recorder.record()
if failed := t.failNow(); failed != nil && err == nil {
err = failed
}
return t.checker.Failure(err)
}