Этот коммит содержится в:
gedi 2017-04-28 10:54:24 +03:00
родитель d5a7e38a81
коммит d3cf314381
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 56604CDCCC201556
2 изменённых файлов: 36 добавлений и 6 удалений

Просмотреть файл

@ -2,8 +2,10 @@ package godog
import ( import (
"fmt" "fmt"
"go/build"
"io" "io"
"path" "path"
"path/filepath"
"runtime" "runtime"
"strings" "strings"
) )
@ -26,6 +28,13 @@ func (f stackFrame) file() string {
return file 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 // line returns the line number of source code of the
// function for this Frame's pc. // function for this Frame's pc.
func (f stackFrame) line() int { func (f stackFrame) line() int {
@ -66,7 +75,7 @@ func (f stackFrame) Format(s fmt.State, verb rune) {
io.WriteString(s, "unknown") io.WriteString(s, "unknown")
} else { } else {
file, _ := fn.FileLine(pc) 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: default:
io.WriteString(s, path.Base(f.file())) io.WriteString(s, path.Base(f.file()))

Просмотреть файл

@ -2,6 +2,7 @@ package godog
import ( import (
"fmt" "fmt"
"runtime"
"strings" "strings"
"testing" "testing"
) )
@ -14,10 +15,26 @@ func trimLineSpaces(s string) string {
return strings.Join(res, "\n") 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) { func TestStacktrace(t *testing.T) {
err := &traceError{ err := &traceError{
msg: "err msg", msg: "err msg",
stack: callStack(), stack: callstack1(),
} }
expect := "err msg" expect := "err msg"
@ -27,10 +44,14 @@ func TestStacktrace(t *testing.T) {
} }
expect = trimLineSpaces(`err msg expect = trimLineSpaces(`err msg
testing.tRunner github.com/DATA-DOG/godog.callstack3
/usr/lib/go/src/testing/testing.go:657 github.com/DATA-DOG/godog/stacktrace_test.go:29
runtime.goexit github.com/DATA-DOG/godog.callstack2
/usr/lib/go/src/runtime/asm_amd64.s:2197`) 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)) actual = trimLineSpaces(fmt.Sprintf("%+v", err))
if expect != actual { if expect != actual {