Skip to content

Commit

Permalink
assert: collect.FailNow() should not panic
Browse files Browse the repository at this point in the history
  • Loading branch information
marshall-lee committed Oct 16, 2023
1 parent 8992013 commit 5fd88cc
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 6 deletions.
9 changes: 5 additions & 4 deletions assert/assertions.go
Expand Up @@ -1872,9 +1872,10 @@ func (c *CollectT) Errorf(format string, args ...interface{}) {
c.errors = append(c.errors, fmt.Errorf(format, args...))
}

// FailNow panics.
func (*CollectT) FailNow() {
panic("Assertion failed")
// FailNow stops the execution by calling runtime.Goexit.
func (c *CollectT) FailNow() {
c.errors = []error{} // Make it non-nil to mark a failure.
runtime.Goexit()
}

// Deprecated: That was a method for internal usage that should not have been published. Now just panics.
Expand Down Expand Up @@ -1936,7 +1937,7 @@ func EventuallyWithT(t TestingT, condition func(collect *CollectT), waitFor time
condition(collect)
}()
case errs := <-ch:
if len(errs) == 0 {
if errs == nil {
return true
}
// Keep the errors from the last ended condition, so that they can be copied to t if timeout is reached.
Expand Down
11 changes: 11 additions & 0 deletions assert/assertions_test.go
Expand Up @@ -2838,6 +2838,17 @@ func TestEventuallyWithT_ReturnsTheLatestFinishedConditionErrors(t *testing.T) {
Len(t, mockT.errors, 2)
}

func TestEventuallyWithTFailNow(t *testing.T) {
mockT := new(CollectT)

condition := func(collect *CollectT) {
collect.FailNow()
}

False(t, EventuallyWithT(mockT, condition, 100*time.Millisecond, 20*time.Millisecond))
Len(t, mockT.errors, 1)
}

func TestNeverFalse(t *testing.T) {
condition := func() bool {
return false
Expand Down
10 changes: 8 additions & 2 deletions require/require.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions require/requirements_test.go
Expand Up @@ -5,6 +5,8 @@ import (
"errors"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

// AssertionTesterInterface defines an interface to be used for testing assertion methods
Expand Down Expand Up @@ -681,3 +683,33 @@ func TestErrorAssertionFunc(t *testing.T) {
})
}
}

func TestEventuallyWithTFalse(t *testing.T) {
mockT := new(MockT)

condition := func(collect *assert.CollectT) {
True(collect, false)
}

EventuallyWithT(mockT, condition, 100*time.Millisecond, 20*time.Millisecond)
if !mockT.Failed {
t.Error("Check should fail")
}
}

func TestEventuallyWithTTrue(t *testing.T) {
mockT := new(MockT)

state := 0
condition := func(collect *assert.CollectT) {
defer func() {
state += 1
}()
True(collect, state == 2)
}

EventuallyWithT(mockT, condition, 100*time.Millisecond, 20*time.Millisecond)
if mockT.Failed {
t.Error("Check should pass")
}
}

0 comments on commit 5fd88cc

Please sign in to comment.