fixes stack trace test
Этот коммит содержится в:
родитель
d5a7e38a81
коммит
d3cf314381
2 изменённых файлов: 36 добавлений и 6 удалений
|
@ -2,8 +2,10 @@ package godog
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"go/build"
|
||||
"io"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
@ -26,6 +28,13 @@ func (f stackFrame) file() string {
|
|||
return file
|
||||
}
|
||||
|
||||
func trimGoPath(file string) string {
|
||||
for _, p := range filepath.SplitList(build.Default.GOPATH) {
|
||||
file = strings.Replace(file, filepath.Join(p, "src")+string(filepath.Separator), "", 1)
|
||||
}
|
||||
return file
|
||||
}
|
||||
|
||||
// line returns the line number of source code of the
|
||||
// function for this Frame's pc.
|
||||
func (f stackFrame) line() int {
|
||||
|
@ -66,7 +75,7 @@ func (f stackFrame) Format(s fmt.State, verb rune) {
|
|||
io.WriteString(s, "unknown")
|
||||
} else {
|
||||
file, _ := fn.FileLine(pc)
|
||||
fmt.Fprintf(s, "%s\n\t%s", fn.Name(), file)
|
||||
fmt.Fprintf(s, "%s\n\t%s", fn.Name(), trimGoPath(file))
|
||||
}
|
||||
default:
|
||||
io.WriteString(s, path.Base(f.file()))
|
||||
|
|
|
@ -2,6 +2,7 @@ package godog
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
@ -14,10 +15,26 @@ func trimLineSpaces(s string) string {
|
|||
return strings.Join(res, "\n")
|
||||
}
|
||||
|
||||
func callstack1() *stack {
|
||||
return callstack2()
|
||||
}
|
||||
|
||||
func callstack2() *stack {
|
||||
return callstack3()
|
||||
}
|
||||
|
||||
func callstack3() *stack {
|
||||
const depth = 4
|
||||
var pcs [depth]uintptr
|
||||
n := runtime.Callers(1, pcs[:])
|
||||
var st stack = pcs[0:n]
|
||||
return &st
|
||||
}
|
||||
|
||||
func TestStacktrace(t *testing.T) {
|
||||
err := &traceError{
|
||||
msg: "err msg",
|
||||
stack: callStack(),
|
||||
stack: callstack1(),
|
||||
}
|
||||
|
||||
expect := "err msg"
|
||||
|
@ -27,10 +44,14 @@ func TestStacktrace(t *testing.T) {
|
|||
}
|
||||
|
||||
expect = trimLineSpaces(`err msg
|
||||
testing.tRunner
|
||||
/usr/lib/go/src/testing/testing.go:657
|
||||
runtime.goexit
|
||||
/usr/lib/go/src/runtime/asm_amd64.s:2197`)
|
||||
github.com/DATA-DOG/godog.callstack3
|
||||
github.com/DATA-DOG/godog/stacktrace_test.go:29
|
||||
github.com/DATA-DOG/godog.callstack2
|
||||
github.com/DATA-DOG/godog/stacktrace_test.go:23
|
||||
github.com/DATA-DOG/godog.callstack1
|
||||
github.com/DATA-DOG/godog/stacktrace_test.go:19
|
||||
github.com/DATA-DOG/godog.TestStacktrace
|
||||
github.com/DATA-DOG/godog/stacktrace_test.go:37`)
|
||||
|
||||
actual = trimLineSpaces(fmt.Sprintf("%+v", err))
|
||||
if expect != actual {
|
||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче