-
Notifications
You must be signed in to change notification settings - Fork 17
/
init_test.go
58 lines (49 loc) · 1.54 KB
/
init_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"context"
"net"
"os"
"strconv"
"testing"
"time"
"github.com/powerman/check"
_ "github.com/smartystreets/goconvey/convey"
)
var (
testTimeFactor = floatGetenv("GO_TEST_TIME_FACTOR", 1.0)
testSecond = time.Duration(testTimeFactor) * time.Second //nolint:revive // By design.
testCtx = context.Background()
)
func floatGetenv(name string, def float64) float64 {
if v, err := strconv.ParseFloat(os.Getenv(name), 64); err == nil {
return v
}
return def
}
type checkC struct{ *check.C }
func checkT(t *testing.T) *checkC { t.Helper(); return &checkC{C: check.T(t)} }
func (c *checkC) NoErr(_ any, err error) { c.Helper(); c.Must(c.Nil(err)) }
func (c *checkC) NoErrInt(v int, err error) int { c.Helper(); c.Must(c.Nil(err)); return v }
func (c *checkC) NoErrBuf(v []byte, err error) []byte { c.Helper(); c.Must(c.Nil(err)); return v }
func (c *checkC) NoErrFile(v *os.File, err error) *os.File { c.Helper(); c.Must(c.Nil(err)); return v }
func (c *checkC) NoErrListen(v net.Listener, err error) net.Listener {
c.Helper()
c.Must(c.Nil(err))
return v
}
func (c *checkC) TempPath() string {
c.Helper()
f := c.NoErrFile(os.CreateTemp("", "gotest"))
c.Nil(f.Close())
c.Nil(os.Remove(f.Name()))
return f.Name()
}
func TestMain(m *testing.M) {
if os.Getenv("GO_WANT_HELPER_PROCESS") == "" { // don't do this again in subprocess
// Vars for use in testdata/ templates.
os.Setenv("A", "10")
os.Setenv("B", "20")
os.Unsetenv("C")
}
check.TestMain(m)
}