Skip to content

Commit 0ebb845

Browse files
committed
assert: collect.FailNow() should not panic
Fixes #1396 Fixes #1457
1 parent 8992013 commit 0ebb845

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

‎assert/assertions.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,9 +1872,12 @@ func (c *CollectT) Errorf(format string, args ...interface{}) {
18721872
c.errors = append(c.errors, fmt.Errorf(format, args...))
18731873
}
18741874

1875-
// FailNow panics.
1876-
func (*CollectT) FailNow() {
1877-
panic("Assertion failed")
1875+
// FailNow stops the execution by calling runtime.Goexit.
1876+
func (c *CollectT) FailNow() {
1877+
if c.errors == nil {
1878+
c.errors = []error{} // Make it non-nil to mark a failure.
1879+
}
1880+
runtime.Goexit()
18781881
}
18791882

18801883
// Deprecated: That was a method for internal usage that should not have been published. Now just panics.
@@ -1936,7 +1939,7 @@ func EventuallyWithT(t TestingT, condition func(collect *CollectT), waitFor time
19361939
condition(collect)
19371940
}()
19381941
case errs := <-ch:
1939-
if len(errs) == 0 {
1942+
if errs == nil {
19401943
return true
19411944
}
19421945
// Keep the errors from the last ended condition, so that they can be copied to t if timeout is reached.

‎assert/assertions_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2838,6 +2838,17 @@ func TestEventuallyWithT_ReturnsTheLatestFinishedConditionErrors(t *testing.T) {
28382838
Len(t, mockT.errors, 2)
28392839
}
28402840

2841+
func TestEventuallyWithTFailNow(t *testing.T) {
2842+
mockT := new(CollectT)
2843+
2844+
condition := func(collect *CollectT) {
2845+
collect.FailNow()
2846+
}
2847+
2848+
False(t, EventuallyWithT(mockT, condition, 100*time.Millisecond, 20*time.Millisecond))
2849+
Len(t, mockT.errors, 1)
2850+
}
2851+
28412852
func TestNeverFalse(t *testing.T) {
28422853
condition := func() bool {
28432854
return false

‎require/requirements_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"errors"
66
"testing"
77
"time"
8+
9+
"github.com/stretchr/testify/assert"
810
)
911

1012
// AssertionTesterInterface defines an interface to be used for testing assertion methods
@@ -681,3 +683,31 @@ func TestErrorAssertionFunc(t *testing.T) {
681683
})
682684
}
683685
}
686+
687+
func TestEventuallyWithTFalse(t *testing.T) {
688+
mockT := new(MockT)
689+
690+
condition := func(collect *assert.CollectT) {
691+
True(collect, false)
692+
}
693+
694+
EventuallyWithT(mockT, condition, 100*time.Millisecond, 20*time.Millisecond)
695+
if !mockT.Failed {
696+
t.Error("Check should fail")
697+
}
698+
}
699+
700+
func TestEventuallyWithTTrue(t *testing.T) {
701+
mockT := new(MockT)
702+
703+
state := 0
704+
condition := func(collect *assert.CollectT) {
705+
state += 1
706+
True(collect, state == 2)
707+
}
708+
709+
EventuallyWithT(mockT, condition, 100*time.Millisecond, 20*time.Millisecond)
710+
if mockT.Failed {
711+
t.Error("Check should pass")
712+
}
713+
}

0 commit comments

Comments
 (0)