Skip to content

Commit b8bc681

Browse files
committed
fix: if test panics, adaptive callstack print for runtime.Error
1 parent 5d5f0cb commit b8bc681

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

assert/assert.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,29 +228,32 @@ func PopTester() {
228228
return
229229
}
230230

231+
var stackLvl = 5 // amount of functions before we're here
232+
var framesToSkip = 3 // how many fn calls there is before FuncName call
233+
231234
var msg string
232235
switch t := r.(type) {
233236
case string:
234237
msg = t
235238
case runtime.Error:
239+
stackLvl-- // move stack trace cursor
240+
framesToSkip++ // see fatal(), skip 1 more when runtime panics
236241
msg = t.Error()
237242
case error:
238243
msg = t.Error()
239244
default:
240-
msg = "test panic catch"
245+
msg = fmt.Sprintf("panic: %v", t)
241246
}
242247

243248
// First, print the call stack. Note that we aren't support full error
244249
// tracing with unit test logging. However, using it has proved the top
245250
// level error stack as more enough. Even so that we could consider using
246251
// it for normal error stack traces if it would be possible.
247-
const stackLvl = 6 // amount of functions before we're here
248252
debug.PrintStackForTest(os.Stderr, stackLvl)
249253

250254
// Now that call stack errors are printed, if any. Let's print the actual
251255
// line that caused the error, i.e., was throwing the error. Note that we
252256
// are here in the 'catch-function'.
253-
const framesToSkip = 4 // how many fn calls there is before FuncName call
254257
fatal("assertion catching: "+msg, framesToSkip)
255258
}
256259

0 commit comments

Comments
 (0)