Skip to content

Commit 8bedbde

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

File tree

3 files changed

+54
-8
lines changed

3 files changed

+54
-8
lines changed

‎assert/assertions.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1864,17 +1864,20 @@ func Eventually(t TestingT, condition func() bool, waitFor time.Duration, tick t
18641864

18651865
// CollectT implements the TestingT interface and collects all errors.
18661866
type CollectT struct {
1867+
failed bool
18671868
errors []error
18681869
}
18691870

18701871
// Errorf collects the error.
18711872
func (c *CollectT) Errorf(format string, args ...interface{}) {
1873+
c.failed = true
18721874
c.errors = append(c.errors, fmt.Errorf(format, args...))
18731875
}
18741876

1875-
// FailNow panics.
1876-
func (*CollectT) FailNow() {
1877-
panic("Assertion failed")
1877+
// FailNow stops the execution by calling runtime.Goexit.
1878+
func (c *CollectT) FailNow() {
1879+
c.failed = true
1880+
runtime.Goexit()
18781881
}
18791882

18801883
// Deprecated: That was a method for internal usage that should not have been published. Now just panics.
@@ -1911,7 +1914,7 @@ func EventuallyWithT(t TestingT, condition func(collect *CollectT), waitFor time
19111914
}
19121915

19131916
var lastFinishedTickErrs []error
1914-
ch := make(chan []error, 1)
1917+
ch := make(chan *CollectT, 1)
19151918

19161919
timer := time.NewTimer(waitFor)
19171920
defer timer.Stop()
@@ -1931,16 +1934,16 @@ func EventuallyWithT(t TestingT, condition func(collect *CollectT), waitFor time
19311934
go func() {
19321935
collect := new(CollectT)
19331936
defer func() {
1934-
ch <- collect.errors
1937+
ch <- collect
19351938
}()
19361939
condition(collect)
19371940
}()
1938-
case errs := <-ch:
1939-
if len(errs) == 0 {
1941+
case collect := <-ch:
1942+
if !collect.failed {
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.
1943-
lastFinishedTickErrs = errs
1946+
lastFinishedTickErrs = collect.errors
19441947
tick = ticker.C
19451948
}
19461949
}

‎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: 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)