Skip to content

Commit e162b21

Browse files
committed
expose sandbox package as pkgfunc in testcase
1 parent 9b24520 commit e162b21

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

Sandbox.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package testcase
2+
3+
import (
4+
"github.com/adamluzsi/testcase/sandbox"
5+
)
6+
7+
func Sandbox(fn func()) sandbox.RunOutcome {
8+
return sandbox.Run(fn)
9+
}

Sandbox_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package testcase_test
2+
3+
import (
4+
"fmt"
5+
"runtime"
6+
"testing"
7+
8+
"github.com/adamluzsi/testcase"
9+
"github.com/adamluzsi/testcase/assert"
10+
"github.com/adamluzsi/testcase/random"
11+
"github.com/adamluzsi/testcase/sandbox"
12+
)
13+
14+
func ExampleSandbox() {
15+
stb := &testcase.StubTB{}
16+
outcome := testcase.Sandbox(func() {
17+
// some test helper function calls fatal, which cause runtime.Goexit after marking the test failed.
18+
stb.FailNow()
19+
})
20+
21+
fmt.Println("The sandbox run has finished without an issue", outcome.OK)
22+
fmt.Println("runtime.Goexit was called:", outcome.Goexit)
23+
fmt.Println("panic value:", outcome.PanicValue)
24+
}
25+
26+
func TestSandbox_smoke(t *testing.T) {
27+
var out sandbox.RunOutcome
28+
out = testcase.Sandbox(func() {})
29+
assert.True(t, out.OK)
30+
assert.Nil(t, out.PanicValue)
31+
32+
out = testcase.Sandbox(func() { runtime.Goexit() })
33+
assert.False(t, out.OK)
34+
assert.Nil(t, out.PanicValue)
35+
36+
expectedPanicValue := random.New(random.CryptoSeed{}).Error()
37+
out = testcase.Sandbox(func() { panic(expectedPanicValue) })
38+
assert.False(t, out.OK)
39+
assert.Equal[any](t, expectedPanicValue, out.PanicValue)
40+
}

0 commit comments

Comments
 (0)