This repository has been archived by the owner on Mar 6, 2020. It is now read-only.
/
testflag.go
195 lines (181 loc) · 5.32 KB
/
testflag.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package main
import (
"errors"
"fmt"
"strconv"
"strings"
"github.com/constabulary/gb/debug"
)
// testFlagSpec defines a flag we know about.
type testFlagSpec struct {
boolVar bool // True if the flag is type bool
passToTest bool // pass to Test
passToAll bool // pass to test plugin and test binary
present bool // The flag has been seen
}
// testFlagDefn is the set of flags we process.
var testFlagDefn = map[string]*testFlagSpec{
// local to the test plugin
"cover": {boolVar: true},
"coverpkg": {},
"covermode": {},
"a": {boolVar: true},
"r": {boolVar: true},
"f": {boolVar: true},
"F": {boolVar: true},
"P": {},
"ldflags": {},
"gcflags": {},
"dotfile": {},
"tags": {},
"race": {},
// Passed to the test binary
"q": {boolVar: true, passToTest: true},
"v": {boolVar: true, passToAll: true},
"bench": {passToTest: true},
"benchmem": {boolVar: true, passToTest: true},
"benchtime": {passToTest: true},
"coverprofile": {passToTest: true},
"cpu": {passToTest: true},
"cpuprofile": {passToTest: true},
"memprofile": {passToTest: true},
"memprofilerate": {passToTest: true},
"blockprofile": {passToTest: true},
"blockprofilerate": {passToTest: true},
"outputdir": {passToTest: true},
"parallel": {passToTest: true},
"run": {passToTest: true},
"short": {boolVar: true, passToTest: true},
"timeout": {passToTest: true},
}
// TestFlags appends "-test." for flags that are passed to the test binary.
func TestFlags(testArgs []string) []string {
debug.Debugf("TestFlags: args: %s", testArgs)
var targs []string
for _, arg := range testArgs {
var nArg, nVal, fArg string
fArg = arg
if !strings.Contains(arg, "-test.") {
nArg = strings.TrimPrefix(arg, "-")
if strings.Contains(nArg, "=") {
nArgVal := strings.Split(nArg, "=")
nArg, nVal = nArgVal[0], nArgVal[1]
}
if val, ok := testFlagDefn[nArg]; ok {
// Special handling for -q, needs to be -test.v when passed to the test
if nArg == "q" {
nArg = "v"
}
if val.passToTest || val.passToAll {
fArg = "-test." + nArg
if nVal != "" {
fArg = fArg + "=" + nVal
}
}
}
}
targs = append(targs, fArg)
}
debug.Debugf("testFlags: targs: %s", targs)
return targs
}
// TestFlagsExtraParse is used to separate known arguments from unknown
// arguments passed on the command line. Returns a string slice of test plugin
// arguments (parseArgs), and a slice of string arguments for the test binary
// (extraArgs). An error is returned if an argument is used twice, or an
// argument value is incorrect.
func TestFlagsExtraParse(args []string) (parseArgs []string, extraArgs []string, err error) {
argsLen := len(args)
for x := 0; x < argsLen; x++ {
nArg := args[x]
val, ok := testFlagDefn[strings.TrimPrefix(nArg, "-")]
if !strings.HasPrefix(nArg, "-") || (ok && !val.passToTest) {
err = setArgFound(nArg)
if err != nil {
return
}
if ok && val.passToAll {
// passToAll arguments, like -v or -cover, are special. They are handled by gb test
// and the test sub process. So move them to the front of the parseArgs list but
// the back of the extraArgs list.
parseArgs = append([]string{nArg}, parseArgs...)
extraArgs = append(extraArgs, nArg)
continue
}
parseArgs = append(parseArgs, nArg)
continue
}
var hadTestPrefix bool
hasEqual := strings.Contains(nArg, "=")
if !hasEqual && (x+1 < argsLen && !strings.HasPrefix(args[x+1], "-")) {
if strings.Contains(nArg, "-test.") {
hadTestPrefix = true
nArg = strings.TrimPrefix(nArg, "-test.")
} else {
nArg = strings.TrimPrefix(nArg, "-")
}
err = setArgFound(nArg)
if err != nil {
return
}
// Check the spec for arguments that consume the next argument
if val, ok := testFlagDefn[nArg]; ok {
if !val.boolVar {
nArg = nArg + "=" + args[x+1]
x++
}
}
} else if hasEqual {
// The argument has an embedded value, here we can do some basic
// checking.
sArgs := strings.Split(nArg, "=")
tArg, tVal := strings.TrimPrefix(sArgs[0], "-"), sArgs[1]
if val, ok := testFlagDefn[tArg]; ok {
if val.boolVar {
if err = checkBoolFlag(tVal); err != nil {
return
}
}
if !val.passToTest {
parseArgs = append(parseArgs, nArg)
continue
}
}
}
// Append "-" to the argument, and "-test." if "-test." was previously
// trimmed.
if nArg[0] != '-' {
pre := "-"
if hadTestPrefix {
pre = "-test."
}
nArg = pre + nArg
}
extraArgs = append(extraArgs, nArg)
}
return
}
// setArgFound checks the argument spec to see if arg has already been
// encountered. If it has, then an error is returned.
func setArgFound(arg string) error {
var err error
nArg := strings.TrimPrefix(arg, "-")
if val, ok := testFlagDefn[nArg]; ok {
if val.present {
err = fmt.Errorf("%q flag may be set only once", arg)
} else {
testFlagDefn[nArg].present = true
}
}
return err
}
// checkBoolFlag checks the value to ensure it is a boolean, if not an error is
// returned.
func checkBoolFlag(value string) error {
var nErr error
_, err := strconv.ParseBool(value)
if err != nil {
nErr = errors.New("illegal bool flag value " + value)
}
return nErr
}