Last active
March 17, 2021 17:40
-
-
Save gaqzi/cbfb4505bfd86063284897064e0c3de8 to your computer and use it in GitHub Desktop.
A first pass on something to make stretchr/mock outputs easier to deal with
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Tracebacker implements the methods required to satisfy stretchr/mock's Test() interface | |
// Usage: | |
// my := new(MockMy) | |
// my.Test(&Tracebacker(t) | |
type Tracebacker struct { | |
t *testing.T | |
} | |
func (t *Tracebacker) Logf(format string, args ...interface{}) { | |
t.t.Logf(format, args...) | |
} | |
func (t *Tracebacker) Errorf(format string, args ...interface{}) { | |
t.t.Errorf(format, args...) | |
} | |
func (t *Tracebacker) FailNow() { | |
t.t.Helper() | |
stackframes := make([]uintptr, 100) | |
runtime.Callers(2, stackframes) | |
stacktrace := make([]string, 0, len(stackframes)) | |
frames := runtime.CallersFrames(stackframes) | |
var previousWasTestifyMock bool | |
for f, more := frames.Next(); more; f, more = frames.Next() { | |
if strings.HasPrefix(f.Function, "github.com/stretchr/testify/mock") { | |
previousWasTestifyMock = true | |
continue | |
} else if f.Function == "testing.tRunner" { | |
previousWasTestifyMock = false | |
continue | |
} | |
if previousWasTestifyMock { // most likely the mockery generated caller of the mock package | |
previousWasTestifyMock = false | |
continue | |
} | |
stacktrace = append(stacktrace, fmt.Sprintf("\t%s:%d %s", f.File, f.Line, f.Function)) | |
} | |
t.t.Logf("Mock callers:\n%s", strings.Join(stacktrace, "\n")) | |
t.t.FailNow() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment