Skip to content

Commit 9b24520

Browse files
committed
refactor recovering support functions into sandbox package
1 parent ba737a7 commit 9b24520

26 files changed

+199
-155
lines changed

Race_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ import (
1111
"testing"
1212
"time"
1313

14+
"github.com/adamluzsi/testcase"
1415
"github.com/adamluzsi/testcase/assert"
1516
"github.com/adamluzsi/testcase/internal"
16-
17-
"github.com/adamluzsi/testcase"
1817
)
1918

2019
func TestRace(t *testing.T) {
@@ -54,7 +53,7 @@ func TestRace(t *testing.T) {
5453

5554
t.Run(`goexit propagated back from the lambdas after each lambda finished`, func(t *testing.T) {
5655
var fn1Finished, fn2Finished, afterRaceFinished bool
57-
internal.RecoverExceptGoexit(func() {
56+
internal.RecoverGoexit(func() {
5857
testcase.Race(func() {
5958
fn1Finished = true
6059
}, func() {

Spec_bc_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,8 @@ func isFatalFn(stub *StubTB) func(block func()) bool {
3434
return func(block func()) bool {
3535
stub.IsFailed = false
3636
defer func() { stub.IsFailed = false }()
37-
var finished bool
38-
internal.RecoverExceptGoexit(func() {
39-
block()
40-
finished = true
41-
})
42-
return !finished && stub.Failed()
37+
out := internal.RecoverGoexit(block)
38+
return !out.OK && stub.Failed()
4339
}
4440
}
4541

Spec_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ func TestSpec_LetValue_mutableValuesAreNotAllowed(t *testing.T) {
604604
s := testcase.NewSpec(stub)
605605

606606
var finished bool
607-
internal.RecoverExceptGoexit(func() {
607+
internal.RecoverGoexit(func() {
608608
type SomeStruct struct {
609609
Text string
610610
}
@@ -985,7 +985,7 @@ func BenchmarkTest_Spec_SkipBenchmark_invalidUse(b *testing.B) {
985985
s.Test(``, func(t *testcase.T) { t.SkipNow() })
986986

987987
var finished bool
988-
internal.RecoverExceptGoexit(func() {
988+
internal.RecoverGoexit(func() {
989989
s.SkipBenchmark()
990990
finished = false
991991
})
@@ -1119,14 +1119,14 @@ func TestSpec_Parallel_testPrepareActionsExecutedInParallel(t *testing.T) {
11191119
s := testcase.NewSpec(t)
11201120
s.Parallel()
11211121

1122-
s.Around(func(t *testcase.T) func() {
1122+
s.Before(func(t *testcase.T) {
11231123
timer := time.NewTimer(time.Second)
11241124
go func() {
11251125
if _, ok := <-timer.C; ok {
11261126
panic(`it was expected that #Before run parallel as well in case of Spec#Parallel is used`)
11271127
}
11281128
}()
1129-
return func() { timer.Stop() }
1129+
t.Defer(timer.Stop)
11301130
})
11311131

11321132
total := 2
@@ -1180,7 +1180,7 @@ func TestSpec_Finish_finishedSpecIsImmutable(t *testing.T) {
11801180
s.Finish()
11811181

11821182
var finished bool
1183-
internal.RecoverExceptGoexit(func() {
1183+
internal.RecoverGoexit(func() {
11841184
s.Before(func(t *testcase.T) {})
11851185
finished = true
11861186
})

StubTB_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func TestStubTB(t *testing.T) {
6363
s.Test(`.FailNow`, func(t *testcase.T) {
6464
assert.Must(t).True(!stub.Get(t).IsFailed)
6565
var ran bool
66-
internal.RecoverExceptGoexit(func() {
66+
internal.RecoverGoexit(func() {
6767
stub.Get(t).FailNow()
6868
ran = true
6969
})
@@ -81,7 +81,7 @@ func TestStubTB(t *testing.T) {
8181
stb := stub.Get(t)
8282
assert.Must(t).True(!stb.IsFailed)
8383
var ran bool
84-
internal.RecoverExceptGoexit(func() {
84+
internal.RecoverGoexit(func() {
8585
stb.Log("-")
8686
stb.Fatal(`arg1`, `arg2`, `arg3`)
8787
ran = true
@@ -94,7 +94,7 @@ func TestStubTB(t *testing.T) {
9494
s.Test(`.Fatalf`, func(t *testcase.T) {
9595
assert.Must(t).True(!stub.Get(t).IsFailed)
9696
var ran bool
97-
internal.RecoverExceptGoexit(func() {
97+
internal.RecoverGoexit(func() {
9898
stub.Get(t).Log("-")
9999
stub.Get(t).Fatalf(`%s %q %s`, `arg1`, `arg2`, `arg3`)
100100
ran = true
@@ -148,7 +148,7 @@ func TestStubTB(t *testing.T) {
148148
s.Test(`.Skip`, func(t *testcase.T) {
149149
assert.Must(t).True(!stub.Get(t).Skipped())
150150
var ran bool
151-
internal.RecoverExceptGoexit(func() {
151+
internal.RecoverGoexit(func() {
152152
stub.Get(t).Skip()
153153
ran = true
154154
})
@@ -160,7 +160,7 @@ func TestStubTB(t *testing.T) {
160160
assert.Must(t).True(!stub.Get(t).Skipped())
161161
var ran bool
162162
args := []any{"Hello", "world!"}
163-
internal.RecoverExceptGoexit(func() {
163+
internal.RecoverGoexit(func() {
164164
stub.Get(t).Skip(args...)
165165
ran = true
166166
})
@@ -172,7 +172,7 @@ func TestStubTB(t *testing.T) {
172172
s.Test(`.Skipf + args`, func(t *testcase.T) {
173173
assert.Must(t).True(!stub.Get(t).Skipped())
174174
var ran bool
175-
internal.RecoverExceptGoexit(func() {
175+
internal.RecoverGoexit(func() {
176176
stub.Get(t).Skipf("%s", "|v|")
177177
ran = true
178178
})
@@ -184,7 +184,7 @@ func TestStubTB(t *testing.T) {
184184
s.Test(`.SkipNow + .Skipped`, func(t *testcase.T) {
185185
assert.Must(t).True(!stub.Get(t).Skipped())
186186
var ran bool
187-
internal.RecoverExceptGoexit(func() {
187+
internal.RecoverGoexit(func() {
188188
stub.Get(t).SkipNow()
189189
ran = true
190190
})
@@ -195,7 +195,7 @@ func TestStubTB(t *testing.T) {
195195
s.Test(`.Skipf`, func(t *testcase.T) {
196196
assert.Must(t).True(!stub.Get(t).Skipped())
197197
var ran bool
198-
internal.RecoverExceptGoexit(func() {
198+
internal.RecoverGoexit(func() {
199199
stub.Get(t).Skipf(`%s`, `arg42`)
200200
ran = true
201201
})

T_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/adamluzsi/testcase/assert"
1212
"github.com/adamluzsi/testcase/contracts"
13+
"github.com/adamluzsi/testcase/sandbox"
1314

1415
"github.com/adamluzsi/testcase/random"
1516

@@ -480,7 +481,7 @@ func TestT_SkipUntil(t *testing.T) {
480481
t.SkipUntil(future.Year(), future.Month(), future.Day())
481482
ran = true
482483
})
483-
internal.Recover(func() { s.Finish() })
484+
sandbox.Run(func() { s.Finish() })
484485
assert.Must(t).False(ran)
485486
assert.Must(t).False(stubTB.IsFailed)
486487
assert.Must(t).True(stubTB.IsSkipped)
@@ -495,7 +496,7 @@ func TestT_SkipUntil(t *testing.T) {
495496
t.SkipUntil(today.Year(), today.Month(), today.Day())
496497
ran = true
497498
})
498-
internal.Recover(func() { s.Finish() })
499+
sandbox.Run(func() { s.Finish() })
499500
assert.Must(t).False(ran)
500501
assert.Must(t).True(stubTB.IsFailed)
501502
assert.Must(t).Contain(stubTB.Logs.String(), fmt.Sprintf(skipExpiredFormat, today.Format(timeLayout)))

assert/AnyOf.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func (ao *AnyOf) Test(blk func(it It)) {
3333
}
3434
recorder := &internal.RecorderTB{TB: ao.TB}
3535
defer recorder.CleanupNow()
36-
internal.RecoverExceptGoexit(func() {
36+
internal.RecoverGoexit(func() {
3737
ao.TB.Helper()
3838
blk(MakeIt(recorder))
3939
})

assert/Asserter.go

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"strings"
88
"testing"
99

10-
"github.com/adamluzsi/testcase/internal"
1110
"github.com/adamluzsi/testcase/internal/reflects"
11+
"github.com/adamluzsi/testcase/sandbox"
1212

1313
"github.com/adamluzsi/testcase/internal/fmterror"
1414
)
@@ -108,17 +108,10 @@ func (a Asserter) NotNil(v any, msg ...any) {
108108
})
109109
}
110110

111-
func (a Asserter) hasPanicked(blk func()) (panicValue any, ok bool) {
111+
func (a Asserter) Panic(blk func(), msg ...any) any {
112112
a.TB.Helper()
113-
panicValue, finished := internal.Recover(blk)
114-
return panicValue, !finished
115-
}
116-
117-
func (a Asserter) Panic(blk func(), msg ...any) (panicValue any) {
118-
a.TB.Helper()
119-
panicValue, ok := a.hasPanicked(blk)
120-
if ok {
121-
return panicValue
113+
if ro := sandbox.Run(blk); !ro.OK {
114+
return ro.PanicValue
122115
}
123116
a.Fn(fmterror.Message{
124117
Method: "Panics",
@@ -130,8 +123,8 @@ func (a Asserter) Panic(blk func(), msg ...any) (panicValue any) {
130123

131124
func (a Asserter) NotPanic(blk func(), msg ...any) {
132125
a.TB.Helper()
133-
panicValue, ok := a.hasPanicked(blk)
134-
if !ok {
126+
out := sandbox.Run(blk)
127+
if out.OK {
135128
return
136129
}
137130
a.Fn(fmterror.Message{
@@ -140,7 +133,7 @@ func (a Asserter) NotPanic(blk func(), msg ...any) {
140133
Values: []fmterror.Value{
141134
{
142135
Label: "panic:",
143-
Value: panicValue,
136+
Value: out.PanicValue,
144137
},
145138
},
146139
UserMessage: msg,

assert/Asserter_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"testing"
99

1010
"github.com/adamluzsi/testcase"
11-
"github.com/adamluzsi/testcase/internal"
11+
"github.com/adamluzsi/testcase/sandbox"
1212

1313
"github.com/adamluzsi/testcase/assert"
1414
"github.com/adamluzsi/testcase/random"
@@ -17,23 +17,24 @@ import (
1717
func TestMust(t *testing.T) {
1818
must := assert.Must(t)
1919
stub := &testcase.StubTB{}
20-
_, ok := internal.Recover(func() {
20+
out := sandbox.Run(func() {
2121
a := assert.Must(stub)
2222
a.True(false) // fail it
2323
t.Fail()
2424
})
25-
must.False(ok, "failed while stopping the goroutine")
25+
must.False(out.OK, "failed while stopping the goroutine")
2626
must.True(stub.IsFailed)
2727
}
2828

2929
func TestShould(t *testing.T) {
3030
must := assert.Must(t)
3131
stub := &testcase.StubTB{}
32-
_, ok := internal.Recover(func() {
32+
out := sandbox.Run(func() {
3333
a := assert.Should(stub)
3434
a.True(false) // fail it
3535
})
36-
must.True(ok, "failed without stopping the goroutine")
36+
must.True(out.OK)
37+
must.False(out.Goexit, "failed without stopping the goroutine")
3738
must.True(stub.IsFailed)
3839
}
3940

@@ -417,7 +418,7 @@ func TestAsserter_Equal_equalableWithError_ErrorReturned(t *testing.T) {
417418

418419
stub := &testcase.StubTB{}
419420

420-
internal.Recover(func() {
421+
sandbox.Run(func() {
421422
a := assert.Asserter{
422423
TB: stub,
423424
Fn: stub.Fatal,

assert/Eventually.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func (r Eventually) Assert(tb testing.TB, blk func(it It)) {
3636
r.RetryStrategy.While(func() bool {
3737
tb.Helper()
3838
lastRecorder = &internal.RecorderTB{TB: tb}
39-
internal.RecoverExceptGoexit(func() {
39+
internal.RecoverGoexit(func() {
4040
tb.Helper()
4141
blk(MakeIt(lastRecorder))
4242
})

assert/Eventually_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import (
55
"time"
66

77
"github.com/adamluzsi/testcase/assert"
8+
"github.com/adamluzsi/testcase/internal"
89
"github.com/adamluzsi/testcase/random"
10+
"github.com/adamluzsi/testcase/sandbox"
911

1012
"github.com/adamluzsi/testcase"
11-
"github.com/adamluzsi/testcase/internal"
1213
)
1314

1415
func TestEventually(t *testing.T) {
@@ -148,13 +149,13 @@ func SpecEventually(tb testing.TB) {
148149
})
149150

150151
s.Then(`it will fail the test`, func(t *testcase.T) {
151-
internal.Recover(func() { subject(t) })
152+
sandbox.Run(func() { subject(t) })
152153

153154
assert.Must(t).True(stubTB.Get(t).Failed())
154155
})
155156

156157
s.Then(`it will ensure that Cleanup was executed`, func(t *testcase.T) {
157-
internal.RecoverExceptGoexit(func() { subject(t) })
158+
internal.RecoverGoexit(func() { subject(t) })
158159

159160
assert.Must(t).True(hasRun.Get(t))
160161
})

0 commit comments

Comments
 (0)