Skip to content
Closed
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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Panics are not exceptions, we should not be blindly ignoring them. If there is a case we know of that spew can't handle then we should avoid it or fix spew, potentially by vendoring it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's what I suggested in the PR that was created earlier

@brackendawson do you remember the discussion we had about importing the code of go-spew, the same way we imported difflib ?

The conclusion was not yet, because we don't know yet the "unforseen consequences" of importing the code of go-difflib, so we don't have enough information and experience on this.

Maybe it's time to reconsider it.

Importing the code of go-spew diff and and fixing the bug in the code could be considered.

Originally posted by @ccoVeille in #1816 (comment)

I'm glad we converge here.

It would also help to remove the imperfect hack about time.Time by patching it to use the stringer even when DisableMethods is true.

// 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