Skip to content

Commit e581b36

Browse files
Merge pull request #1775 from mutaiib/bugfix/panic-methodcalled-mutex-release-1731
mock: avoid panic when expected type is nil in Arguments.Diff
2 parents b7801fb + e3d64ad commit e581b36

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

mock/mock.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,10 @@ type IsTypeArgument struct {
833833
// For example:
834834
//
835835
// args.Assert(t, IsType(""), IsType(0))
836+
//
837+
// Mock cannot match interface types because the contained type will be passed
838+
// to both IsType and Mock.Called, for the zero value of all interfaces this
839+
// will be <nil> type.
836840
func IsType(t interface{}) *IsTypeArgument {
837841
return &IsTypeArgument{t: reflect.TypeOf(t)}
838842
}
@@ -1034,7 +1038,7 @@ func (args Arguments) Diff(objects []interface{}) (string, int) {
10341038
if actualT != expected.t {
10351039
differences++
10361040
outputRenderers = append(outputRenderers, func() string {
1037-
return fmt.Sprintf("\t%d: FAIL: type %s != type %s - %s\n", i, expected.t.Name(), actualT.Name(), actualFmt())
1041+
return fmt.Sprintf("\t%d: FAIL: type %s != type %s - %s\n", i, safeTypeName(expected.t), actualT, actualFmt())
10381042
})
10391043
}
10401044
case *FunctionalOptionsArgument:
@@ -1179,6 +1183,15 @@ func (args Arguments) Bool(index int) bool {
11791183
return s
11801184
}
11811185

1186+
// safeTypeName returns the reflect.Type's name without causing a panic.
1187+
// If the provided reflect.Type is nil, it returns the placeholder string "<nil>"
1188+
func safeTypeName(t reflect.Type) string {
1189+
if t == nil {
1190+
return "<nil>"
1191+
}
1192+
return t.Name()
1193+
}
1194+
11821195
func typeAndKind(v interface{}) (reflect.Type, reflect.Kind) {
11831196
t := reflect.TypeOf(v)
11841197
k := t.Kind()

mock/mock_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package mock
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
67
"regexp"
@@ -1998,6 +1999,25 @@ func Test_Arguments_Diff_WithIsTypeArgument_Failing(t *testing.T) {
19981999
assert.Contains(t, diff, `string != type int - (int=123)`)
19992000
}
20002001

2002+
func Test_Arguments_Diff_WithIsTypeArgument_InterfaceType(t *testing.T) {
2003+
t.Parallel()
2004+
var ctx = context.Background()
2005+
args := Arguments([]interface{}{IsType(ctx)})
2006+
_, count := args.Diff([]interface{}{context.Background()})
2007+
assert.Equal(t, 0, count)
2008+
}
2009+
2010+
func Test_Arguments_Diff_WithIsTypeArgument_InterfaceType_Failing(t *testing.T) {
2011+
t.Parallel()
2012+
2013+
var ctx context.Context
2014+
var args = Arguments([]interface{}{IsType(ctx)})
2015+
diff, count := args.Diff([]interface{}{context.Background()})
2016+
assert.Equal(t, 1, count)
2017+
assert.Contains(t, diff, `type <nil> != type `)
2018+
2019+
}
2020+
20012021
func Test_Arguments_Diff_WithArgMatcher(t *testing.T) {
20022022
t.Parallel()
20032023

0 commit comments

Comments
 (0)