stretchr / testify

A toolkit with common assertions and mocks that plays nicely with the standard library
MIT License
23.53k stars 1.6k forks source link

assert: invalid memory address or nil pointer dereference for assert.Error on darwin/arm64 #1596

Closed samanhappy closed 1 month ago

samanhappy commented 6 months ago

I was testing our project on Darwin/arm64, but there were some test failures with the following example log:

=== RUN   TestTwilio
    nexmo_test.go:61: 
            Error Trace:    /Users/runner/work/easeprobe/easeprobe/notify/sms/nexmo/nexmo_test.go:36
                                        /Users/runner/work/easeprobe/easeprobe/notify/sms/nexmo/nexmo_test.go:61
                                        /Users/runner/work/easeprobe/easeprobe/notify/sms/nexmo/nexmo_test.go:87
            Error:          An error is expected but got nil.
            Test:           TestTwilio
--- FAIL: TestTwilio (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
    panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x18 pc=0x1047c45a8]

Almost all errors occurred when calling assert.Error like here.

func assertError(t *testing.T, err error, msg string) {
    t.Helper()
    assert.Error(t, err) // Line 36
    assert.Equal(t, msg, err.Error())
}

I'm not sure if this was caused by testify. Does testify support Darwin/arm64? Can you guys help to check it?

pgimalac commented 5 months ago

err is nil, so calling err.Error() panics, this is expected Go behavior You might want to use require.Error(t, err) so that the test stops if err is nil, or use the boolean returned by assert.Error