summaryrefslogtreecommitdiff
path: root/libgo/go/cmd/go/internal/test/testflag.go
blob: 138e1f9d2a25f435917166700c76c87a79e92755 (plain)
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package test

import (
	"flag"
	"os"
	"strings"

	"cmd/go/internal/base"
	"cmd/go/internal/cfg"
	"cmd/go/internal/cmdflag"
	"cmd/go/internal/str"
	"cmd/go/internal/work"
)

const cmd = "test"

// The flag handling part of go test is large and distracting.
// We can't use the flag package because some of the flags from
// our command line are for us, and some are for 6.out, and
// some are for both.

// testFlagDefn is the set of flags we process.
var testFlagDefn = []*cmdflag.Defn{
	// local.
	{Name: "c", BoolVar: &testC},
	{Name: "i", BoolVar: &cfg.BuildI},
	{Name: "o"},
	{Name: "cover", BoolVar: &testCover},
	{Name: "covermode"},
	{Name: "coverpkg"},
	{Name: "exec"},
	{Name: "json", BoolVar: &testJSON},
	{Name: "vet"},

	// Passed to 6.out, adding a "test." prefix to the name if necessary: -v becomes -test.v.
	{Name: "bench", PassToTest: true},
	{Name: "benchmem", BoolVar: new(bool), PassToTest: true},
	{Name: "benchtime", PassToTest: true},
	{Name: "blockprofile", PassToTest: true},
	{Name: "blockprofilerate", PassToTest: true},
	{Name: "count", PassToTest: true},
	{Name: "coverprofile", PassToTest: true},
	{Name: "cpu", PassToTest: true},
	{Name: "cpuprofile", PassToTest: true},
	{Name: "failfast", BoolVar: new(bool), PassToTest: true},
	{Name: "list", PassToTest: true},
	{Name: "memprofile", PassToTest: true},
	{Name: "memprofilerate", PassToTest: true},
	{Name: "mutexprofile", PassToTest: true},
	{Name: "mutexprofilefraction", PassToTest: true},
	{Name: "outputdir", PassToTest: true},
	{Name: "parallel", PassToTest: true},
	{Name: "run", PassToTest: true},
	{Name: "short", BoolVar: new(bool), PassToTest: true},
	{Name: "timeout", PassToTest: true},
	{Name: "trace", PassToTest: true},
	{Name: "v", BoolVar: &testV, PassToTest: true},
}

// add build flags to testFlagDefn
func init() {
	cmdflag.AddKnownFlags("test", testFlagDefn)
	var cmd base.Command
	work.AddBuildFlags(&cmd)
	cmd.Flag.VisitAll(func(f *flag.Flag) {
		if f.Name == "v" {
			// test overrides the build -v flag
			return
		}
		testFlagDefn = append(testFlagDefn, &cmdflag.Defn{
			Name:  f.Name,
			Value: f.Value,
		})
	})
}

// testFlags processes the command line, grabbing -x and -c, rewriting known flags
// to have "test" before them, and reading the command line for the 6.out.
// Unfortunately for us, we need to do our own flag processing because go test
// grabs some flags but otherwise its command line is just a holding place for
// pkg.test's arguments.
// We allow known flags both before and after the package name list,
// to allow both
//	go test fmt -custom-flag-for-fmt-test
//	go test -x math
func testFlags(usage func(), args []string) (packageNames, passToTest []string) {
	args = str.StringList(cmdflag.FindGOFLAGS(testFlagDefn), args)
	inPkg := false
	var explicitArgs []string
	for i := 0; i < len(args); i++ {
		if !strings.HasPrefix(args[i], "-") {
			if !inPkg && packageNames == nil {
				// First package name we've seen.
				inPkg = true
			}
			if inPkg {
				packageNames = append(packageNames, args[i])
				continue
			}
		}

		if inPkg {
			// Found an argument beginning with "-"; end of package list.
			inPkg = false
		}

		f, value, extraWord := cmdflag.Parse(cmd, usage, testFlagDefn, args, i)
		if f == nil {
			// This is a flag we do not know; we must assume
			// that any args we see after this might be flag
			// arguments, not package names.
			inPkg = false
			if packageNames == nil {
				// make non-nil: we have seen the empty package list
				packageNames = []string{}
			}
			if args[i] == "-args" || args[i] == "--args" {
				// -args or --args signals that everything that follows
				// should be passed to the test.
				explicitArgs = args[i+1:]
				break
			}
			passToTest = append(passToTest, args[i])
			continue
		}
		if f.Value != nil {
			if err := f.Value.Set(value); err != nil {
				base.Fatalf("invalid flag argument for -%s: %v", f.Name, err)
			}
		} else {
			// Test-only flags.
			// Arguably should be handled by f.Value, but aren't.
			switch f.Name {
			// bool flags.
			case "c", "i", "v", "cover", "json":
				cmdflag.SetBool(cmd, f.BoolVar, value)
				if f.Name == "json" && testJSON {
					passToTest = append(passToTest, "-test.v=true")
				}
			case "o":
				testO = value
				testNeedBinary = true
			case "exec":
				xcmd, err := str.SplitQuotedFields(value)
				if err != nil {
					base.Fatalf("invalid flag argument for -%s: %v", f.Name, err)
				}
				work.ExecCmd = xcmd
			case "bench":
				// record that we saw the flag; don't care about the value
				testBench = true
			case "list":
				testList = true
			case "timeout":
				testTimeout = value
			case "blockprofile", "cpuprofile", "memprofile", "mutexprofile":
				testProfile = "-" + f.Name
				testNeedBinary = true
			case "trace":
				testProfile = "-trace"
			case "coverpkg":
				testCover = true
				if value == "" {
					testCoverPaths = nil
				} else {
					testCoverPaths = strings.Split(value, ",")
				}
			case "coverprofile":
				testCover = true
				testCoverProfile = value
			case "covermode":
				switch value {
				case "set", "count", "atomic":
					testCoverMode = value
				default:
					base.Fatalf("invalid flag argument for -covermode: %q", value)
				}
				testCover = true
			case "outputdir":
				testOutputDir = value
			case "vet":
				testVetList = value
			}
		}
		if extraWord {
			i++
		}
		if f.PassToTest {
			passToTest = append(passToTest, "-test."+f.Name+"="+value)
		}
	}

	if testCoverMode == "" {
		testCoverMode = "set"
		if cfg.BuildRace {
			// Default coverage mode is atomic when -race is set.
			testCoverMode = "atomic"
		}
	}

	testVetExplicit = testVetList != ""
	if testVetList != "" && testVetList != "off" {
		if strings.Contains(testVetList, "=") {
			base.Fatalf("-vet argument cannot contain equal signs")
		}
		if strings.Contains(testVetList, " ") {
			base.Fatalf("-vet argument is comma-separated list, cannot contain spaces")
		}
		list := strings.Split(testVetList, ",")
		for i, arg := range list {
			list[i] = "-" + arg
		}
		testVetFlags = list
	}

	if cfg.BuildRace && testCoverMode != "atomic" {
		base.Fatalf(`-covermode must be "atomic", not %q, when -race is enabled`, testCoverMode)
	}

	// Tell the test what directory we're running in, so it can write the profiles there.
	if testProfile != "" && testOutputDir == "" {
		dir, err := os.Getwd()
		if err != nil {
			base.Fatalf("error from os.Getwd: %s", err)
		}
		passToTest = append(passToTest, "-test.outputdir", dir)
	}

	passToTest = append(passToTest, explicitArgs...)
	return
}