Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Commit d9ac678

Browse files
authored
gomock/controller: use skip additional frame (#443)
In 8321731, the callerInfo call gets nested within an additional function call, but the frame skip isn't updated. When used with mockgen, this causes the location frame to unhelpfully point to the generated mock instead of the callsite in the user's test. Add an explanation for future readers, explaining callerInfo and when it should be updated.
1 parent b76a85f commit d9ac678

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

gomock/call.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ func newCall(t TestHelper, receiver interface{}, method string, methodType refle
6363
}
6464
}
6565

66+
// callerInfo's skip should be updated if the number of calls between the user's test
67+
// and this line changes, i.e. this code is wrapped in another anonymous function.
68+
// 0 is us, 1 is RecordCallWithMethodType(), 2 is the generated recorder, and 3 is the user's test.
6669
origin := callerInfo(3)
6770
actions := []func([]interface{}) []interface{}{func([]interface{}) []interface{} {
6871
// Synthesize the zero value for each of the return args' types.

gomock/controller.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,10 @@ func (ctrl *Controller) Call(receiver interface{}, method string, args ...interf
224224

225225
expected, err := ctrl.expectedCalls.FindMatch(receiver, method, args)
226226
if err != nil {
227-
origin := callerInfo(2)
227+
// callerInfo's skip should be updated if the number of calls between the user's test
228+
// and this line changes, i.e. this code is wrapped in another anonymous function.
229+
// 0 is us, 1 is controller.Call(), 2 is the generated mock, and 3 is the user's test.
230+
origin := callerInfo(3)
228231
ctrl.T.Fatalf("Unexpected call to %T.%v(%v) at %s because: %s", receiver, method, args, origin, err)
229232
}
230233

@@ -291,6 +294,8 @@ func (ctrl *Controller) Finish() {
291294
}
292295
}
293296

297+
// callerInfo returns the file:line of the call site. skip is the number
298+
// of stack frames to skip when reporting. 0 is callerInfo's call site.
294299
func callerInfo(skip int) string {
295300
if _, file, line, ok := runtime.Caller(skip + 1); ok {
296301
return fmt.Sprintf("%s:%d", file, line)

0 commit comments

Comments
 (0)