-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathinitflags.go
More file actions
55 lines (47 loc) · 1.77 KB
/
initflags.go
File metadata and controls
55 lines (47 loc) · 1.77 KB
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 coderdtest
import (
"flag"
"fmt"
"runtime"
"strconv"
"testing"
)
const (
// MaxTestParallelism is set to match our MakeFile's `make test` target.
MaxTestParallelism = 8
)
// init defines the default parallelism for tests, capping it to MaxTestParallelism.
// Any user-provided value for -test.parallel will override this.
func init() {
// Setup the test flags.
testing.Init()
// info is used for debugging panics in this init function.
info := "Resolve the issue in the file initflags.go"
_, file, line, ok := runtime.Caller(0)
if ok {
info = fmt.Sprintf("Resolve the issue in the file %s:%d", file, line)
}
// Lookup the test.parallel flag's value, and cap it to MaxTestParallelism. This
// all happens before `flag.Parse()`, so any user-provided value will overwrite
// whatever we set here.
par := flag.CommandLine.Lookup("test.parallel")
if par == nil {
// This should never happen. If you are reading this message because of a panic,
// just comment out the panic and add a `return` statement instead.
msg := "no 'test.parallel' flag found, unable to set default parallelism"
panic(msg + "\n" + info)
}
parValue, err := strconv.ParseInt(par.Value.String(), 0, 64)
if err != nil {
// This should never happen, but if it does, panic with a useful message. If you
// are reading this message because of a panic, that means the default value for
// -test.parallel is not an integer. A safe fix is to comment out the panic. This
// will assume the default value of '0', and replace it with MaxTestParallelism.
// Which is not ideal, but at least tests will run.
msg := fmt.Sprintf("failed to parse test.parallel: %v", err)
panic(msg + "\n" + info)
}
if parValue > MaxTestParallelism {
_ = par.Value.Set(fmt.Sprintf("%d", MaxTestParallelism))
}
}