Skip to content

Commit ab5d73e

Browse files
committed
assert: collect.FailNow() should not panic
Fixes #1396 Fixes #1457
1 parent 882382d commit ab5d73e

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

‎assert/assertions.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1860,20 +1860,24 @@ func Eventually(t TestingT, condition func() bool, waitFor time.Duration, tick t
18601860
// CollectT implements the TestingT interface and collects all errors.
18611861
type CollectT struct {
18621862
errors []error
1863+
failed bool
18631864
}
18641865

18651866
// Errorf collects the error.
18661867
func (c *CollectT) Errorf(format string, args ...interface{}) {
1868+
c.failed = true
18671869
c.errors = append(c.errors, fmt.Errorf(format, args...))
18681870
}
18691871

18701872
// FailNow panics.
18711873
func (c *CollectT) FailNow() {
1872-
panic("Assertion failed")
1874+
c.failed = true
1875+
runtime.Goexit()
18731876
}
18741877

18751878
// Reset clears the collected errors.
18761879
func (c *CollectT) Reset() {
1880+
c.failed = false
18771881
c.errors = nil
18781882
}
18791883

@@ -1928,8 +1932,8 @@ func EventuallyWithT(t TestingT, condition func(collect *CollectT), waitFor time
19281932
tick = nil
19291933
collect.Reset()
19301934
go func() {
1935+
defer func() { ch <- !collect.failed }()
19311936
condition(collect)
1932-
ch <- len(collect.errors) == 0
19331937
}()
19341938
case v := <-ch:
19351939
if v {

‎assert/assertions_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2786,6 +2786,17 @@ func TestEventuallyWithTTrue(t *testing.T) {
27862786
Len(t, mockT.errors, 0)
27872787
}
27882788

2789+
func TestEventuallyWithTFailNow(t *testing.T) {
2790+
mockT := new(CollectT)
2791+
2792+
condition := func(collect *CollectT) {
2793+
collect.FailNow()
2794+
}
2795+
2796+
False(t, EventuallyWithT(mockT, condition, 100*time.Millisecond, 20*time.Millisecond))
2797+
Len(t, mockT.errors, 1)
2798+
}
2799+
27892800
func TestNeverFalse(t *testing.T) {
27902801
condition := func() bool {
27912802
return false

‎require/requirements_test.go

Lines changed: 32 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,33 @@ 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+
defer func() {
706+
state += 1
707+
}()
708+
True(collect, state == 2)
709+
}
710+
711+
EventuallyWithT(mockT, condition, 100*time.Millisecond, 20*time.Millisecond)
712+
if mockT.Failed {
713+
t.Error("Check should pass")
714+
}
715+
}

0 commit comments

Comments
 (0)