Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1948,19 +1948,28 @@ func diff(expected interface{}, actual interface{}) string {

var e, a string

switch et {
case reflect.TypeOf(""):
e = reflect.ValueOf(expected).String()
a = reflect.ValueOf(actual).String()
case reflect.TypeOf(time.Time{}):
e = spewConfigStringerEnabled.Sdump(expected)
a = spewConfigStringerEnabled.Sdump(actual)
default:
e = spewConfig.Sdump(expected)
a = spewConfig.Sdump(actual)
// Use spew to create string representations of the objects
// We have to guard against panics in spew.
// See https://github.com/stretchr/testify/issues/480
panicked, _, _ := didPanic(func() {
switch et {
case reflect.TypeOf(""):
e = reflect.ValueOf(expected).String()
a = reflect.ValueOf(actual).String()
case reflect.TypeOf(time.Time{}):
e = spewConfigStringerEnabled.Sdump(expected)
a = spewConfigStringerEnabled.Sdump(actual)
default:
e = spewConfig.Sdump(expected)
a = spewConfig.Sdump(actual)
}
})
if panicked {
// silently ignore diff if we panic during spew, the library is no longer maintained
return ""
}

diff, _ := difflib.GetUnifiedDiffString(difflib.UnifiedDiff{
diff, err := difflib.GetUnifiedDiffString(difflib.UnifiedDiff{
A: difflib.SplitLines(e),
B: difflib.SplitLines(a),
FromFile: "Expected",
Expand All @@ -1970,6 +1979,11 @@ func diff(expected interface{}, actual interface{}) string {
Context: 1,
})

if err != nil {
// silently ignore diff if the external library fails to compute it
return ""
}

return "\n\nDiff:\n" + diff
}

Expand Down
33 changes: 33 additions & 0 deletions assert/assertions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,39 @@ func TestEqualFormatting(t *testing.T) {
}
}

func TestEqualFormattingWithPanic(t *testing.T) {
t.Parallel()

type structWithUnexportedMapWithArrayKey struct {
m interface{}
}

for _, c := range []struct {
a interface{}
b interface{}
}{
{
// from the issue https://github.com/stretchr/testify/pull/1816
a: structWithUnexportedMapWithArrayKey{
map[[1]byte]*struct{}{
{1}: nil,
{2}: nil,
},
},
b: structWithUnexportedMapWithArrayKey{},
},
} {

mockT := new(mockTestingT)
NotPanics(t, func() {
Equal(mockT, c.a, c.b)
}, "should not panic")

True(t, mockT.Failed(), "should have failed")
Contains(t, mockT.errorString(), "Not equal:", "error message should mention inequality")
}
}

func TestFormatUnequalValues(t *testing.T) {
t.Parallel()

Expand Down