-
Notifications
You must be signed in to change notification settings - Fork 10
/
seed.go
55 lines (49 loc) · 1.1 KB
/
seed.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
package testcase
import (
"fmt"
"math/rand"
"os"
"strconv"
"testing"
"time"
"go.llib.dev/testcase/random"
"go.llib.dev/testcase/internal"
"go.llib.dev/testcase/internal/environ"
)
func makeSeed() (int64, error) {
rawSeed, injectedRandomSeedIsSet := os.LookupEnv(environ.KeySeed)
if !injectedRandomSeedIsSet {
salt := rand.New(random.CryptoSeed{}).Int63()
base := time.Now().UnixNano()
return base + salt, nil
}
seed, err := strconv.ParseInt(rawSeed, 10, 64)
if err != nil {
return 0, fmt.Errorf("%s has invalid seed integer value: %s", environ.KeySeed, rawSeed)
}
return seed, nil
}
func seedForSpec(tb testing.TB) (_seed int64) {
helper(tb).Helper()
if isValidTestingTB(tb) {
tb.Cleanup(func() {
tb.Helper()
if tb.Failed() {
// Help developers to know the seed of the failed test execution.
internal.Log(tb, fmt.Sprintf(`%s=%d`, environ.KeySeed, _seed))
}
})
}
seed, err := makeSeed()
if err != nil {
tb.Fatal(err.Error())
}
return seed
}
func isValidTestingTB(tb testing.TB) bool {
if tb == nil {
return false
}
_, ok := tb.(internal.NullTB)
return !ok
}