1// Copyright 2011 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package test
6
7import (
8	"bytes"
9	"crypto/sha256"
10	"errors"
11	"fmt"
12	"go/build"
13	"io"
14	"io/ioutil"
15	"os"
16	"os/exec"
17	"path"
18	"path/filepath"
19	"regexp"
20	"sort"
21	"strconv"
22	"strings"
23	"sync"
24	"time"
25
26	"cmd/go/internal/base"
27	"cmd/go/internal/cache"
28	"cmd/go/internal/cfg"
29	"cmd/go/internal/load"
30	"cmd/go/internal/lockedfile"
31	"cmd/go/internal/modload"
32	"cmd/go/internal/str"
33	"cmd/go/internal/work"
34	"cmd/internal/test2json"
35)
36
37// Break init loop.
38func init() {
39	CmdTest.Run = runTest
40}
41
42const testUsage = "go test [build/test flags] [packages] [build/test flags & test binary flags]"
43
44var CmdTest = &base.Command{
45	CustomFlags: true,
46	UsageLine:   testUsage,
47	Short:       "test packages",
48	Long: `
49'Go test' automates testing the packages named by the import paths.
50It prints a summary of the test results in the format:
51
52	ok   archive/tar   0.011s
53	FAIL archive/zip   0.022s
54	ok   compress/gzip 0.033s
55	...
56
57followed by detailed output for each failed package.
58
59'Go test' recompiles each package along with any files with names matching
60the file pattern "*_test.go".
61These additional files can contain test functions, benchmark functions, and
62example functions. See 'go help testfunc' for more.
63Each listed package causes the execution of a separate test binary.
64Files whose names begin with "_" (including "_test.go") or "." are ignored.
65
66Test files that declare a package with the suffix "_test" will be compiled as a
67separate package, and then linked and run with the main test binary.
68
69The go tool will ignore a directory named "testdata", making it available
70to hold ancillary data needed by the tests.
71
72As part of building a test binary, go test runs go vet on the package
73and its test source files to identify significant problems. If go vet
74finds any problems, go test reports those and does not run the test
75binary. Only a high-confidence subset of the default go vet checks are
76used. That subset is: 'atomic', 'bool', 'buildtags', 'nilfunc', and
77'printf'. You can see the documentation for these and other vet tests
78via "go doc cmd/vet". To disable the running of go vet, use the
79-vet=off flag.
80
81All test output and summary lines are printed to the go command's
82standard output, even if the test printed them to its own standard
83error. (The go command's standard error is reserved for printing
84errors building the tests.)
85
86Go test runs in two different modes:
87
88The first, called local directory mode, occurs when go test is
89invoked with no package arguments (for example, 'go test' or 'go
90test -v'). In this mode, go test compiles the package sources and
91tests found in the current directory and then runs the resulting
92test binary. In this mode, caching (discussed below) is disabled.
93After the package test finishes, go test prints a summary line
94showing the test status ('ok' or 'FAIL'), package name, and elapsed
95time.
96
97The second, called package list mode, occurs when go test is invoked
98with explicit package arguments (for example 'go test math', 'go
99test ./...', and even 'go test .'). In this mode, go test compiles
100and tests each of the packages listed on the command line. If a
101package test passes, go test prints only the final 'ok' summary
102line. If a package test fails, go test prints the full test output.
103If invoked with the -bench or -v flag, go test prints the full
104output even for passing package tests, in order to display the
105requested benchmark results or verbose logging. After the package
106tests for all of the listed packages finish, and their output is
107printed, go test prints a final 'FAIL' status if any package test
108has failed.
109
110In package list mode only, go test caches successful package test
111results to avoid unnecessary repeated running of tests. When the
112result of a test can be recovered from the cache, go test will
113redisplay the previous output instead of running the test binary
114again. When this happens, go test prints '(cached)' in place of the
115elapsed time in the summary line.
116
117The rule for a match in the cache is that the run involves the same
118test binary and the flags on the command line come entirely from a
119restricted set of 'cacheable' test flags, defined as -cpu, -list,
120-parallel, -run, -short, and -v. If a run of go test has any test
121or non-test flags outside this set, the result is not cached. To
122disable test caching, use any test flag or argument other than the
123cacheable flags. The idiomatic way to disable test caching explicitly
124is to use -count=1. Tests that open files within the package's source
125root (usually $GOPATH) or that consult environment variables only
126match future runs in which the files and environment variables are unchanged.
127A cached test result is treated as executing in no time at all,
128so a successful package test result will be cached and reused
129regardless of -timeout setting.
130
131In addition to the build flags, the flags handled by 'go test' itself are:
132
133	-args
134	    Pass the remainder of the command line (everything after -args)
135	    to the test binary, uninterpreted and unchanged.
136	    Because this flag consumes the remainder of the command line,
137	    the package list (if present) must appear before this flag.
138
139	-c
140	    Compile the test binary to pkg.test but do not run it
141	    (where pkg is the last element of the package's import path).
142	    The file name can be changed with the -o flag.
143
144	-exec xprog
145	    Run the test binary using xprog. The behavior is the same as
146	    in 'go run'. See 'go help run' for details.
147
148	-i
149	    Install packages that are dependencies of the test.
150	    Do not run the test.
151
152	-json
153	    Convert test output to JSON suitable for automated processing.
154	    See 'go doc test2json' for the encoding details.
155
156	-o file
157	    Compile the test binary to the named file.
158	    The test still runs (unless -c or -i is specified).
159
160The test binary also accepts flags that control execution of the test; these
161flags are also accessible by 'go test'. See 'go help testflag' for details.
162
163For more about build flags, see 'go help build'.
164For more about specifying packages, see 'go help packages'.
165
166See also: go build, go vet.
167`,
168}
169
170var HelpTestflag = &base.Command{
171	UsageLine: "testflag",
172	Short:     "testing flags",
173	Long: `
174The 'go test' command takes both flags that apply to 'go test' itself
175and flags that apply to the resulting test binary.
176
177Several of the flags control profiling and write an execution profile
178suitable for "go tool pprof"; run "go tool pprof -h" for more
179information. The --alloc_space, --alloc_objects, and --show_bytes
180options of pprof control how the information is presented.
181
182The following flags are recognized by the 'go test' command and
183control the execution of any test:
184
185	-bench regexp
186	    Run only those benchmarks matching a regular expression.
187	    By default, no benchmarks are run.
188	    To run all benchmarks, use '-bench .' or '-bench=.'.
189	    The regular expression is split by unbracketed slash (/)
190	    characters into a sequence of regular expressions, and each
191	    part of a benchmark's identifier must match the corresponding
192	    element in the sequence, if any. Possible parents of matches
193	    are run with b.N=1 to identify sub-benchmarks. For example,
194	    given -bench=X/Y, top-level benchmarks matching X are run
195	    with b.N=1 to find any sub-benchmarks matching Y, which are
196	    then run in full.
197
198	-benchtime t
199	    Run enough iterations of each benchmark to take t, specified
200	    as a time.Duration (for example, -benchtime 1h30s).
201	    The default is 1 second (1s).
202	    The special syntax Nx means to run the benchmark N times
203	    (for example, -benchtime 100x).
204
205	-count n
206	    Run each test and benchmark n times (default 1).
207	    If -cpu is set, run n times for each GOMAXPROCS value.
208	    Examples are always run once.
209
210	-cover
211	    Enable coverage analysis.
212	    Note that because coverage works by annotating the source
213	    code before compilation, compilation and test failures with
214	    coverage enabled may report line numbers that don't correspond
215	    to the original sources.
216
217	-covermode set,count,atomic
218	    Set the mode for coverage analysis for the package[s]
219	    being tested. The default is "set" unless -race is enabled,
220	    in which case it is "atomic".
221	    The values:
222		set: bool: does this statement run?
223		count: int: how many times does this statement run?
224		atomic: int: count, but correct in multithreaded tests;
225			significantly more expensive.
226	    Sets -cover.
227
228	-coverpkg pattern1,pattern2,pattern3
229	    Apply coverage analysis in each test to packages matching the patterns.
230	    The default is for each test to analyze only the package being tested.
231	    See 'go help packages' for a description of package patterns.
232	    Sets -cover.
233
234	-cpu 1,2,4
235	    Specify a list of GOMAXPROCS values for which the tests or
236	    benchmarks should be executed. The default is the current value
237	    of GOMAXPROCS.
238
239	-failfast
240	    Do not start new tests after the first test failure.
241
242	-list regexp
243	    List tests, benchmarks, or examples matching the regular expression.
244	    No tests, benchmarks or examples will be run. This will only
245	    list top-level tests. No subtest or subbenchmarks will be shown.
246
247	-parallel n
248	    Allow parallel execution of test functions that call t.Parallel.
249	    The value of this flag is the maximum number of tests to run
250	    simultaneously; by default, it is set to the value of GOMAXPROCS.
251	    Note that -parallel only applies within a single test binary.
252	    The 'go test' command may run tests for different packages
253	    in parallel as well, according to the setting of the -p flag
254	    (see 'go help build').
255
256	-run regexp
257	    Run only those tests and examples matching the regular expression.
258	    For tests, the regular expression is split by unbracketed slash (/)
259	    characters into a sequence of regular expressions, and each part
260	    of a test's identifier must match the corresponding element in
261	    the sequence, if any. Note that possible parents of matches are
262	    run too, so that -run=X/Y matches and runs and reports the result
263	    of all tests matching X, even those without sub-tests matching Y,
264	    because it must run them to look for those sub-tests.
265
266	-short
267	    Tell long-running tests to shorten their run time.
268	    It is off by default but set during all.bash so that installing
269	    the Go tree can run a sanity check but not spend time running
270	    exhaustive tests.
271
272	-timeout d
273	    If a test binary runs longer than duration d, panic.
274	    If d is 0, the timeout is disabled.
275	    The default is 10 minutes (10m).
276
277	-v
278	    Verbose output: log all tests as they are run. Also print all
279	    text from Log and Logf calls even if the test succeeds.
280
281	-vet list
282	    Configure the invocation of "go vet" during "go test"
283	    to use the comma-separated list of vet checks.
284	    If list is empty, "go test" runs "go vet" with a curated list of
285	    checks believed to be always worth addressing.
286	    If list is "off", "go test" does not run "go vet" at all.
287
288The following flags are also recognized by 'go test' and can be used to
289profile the tests during execution:
290
291	-benchmem
292	    Print memory allocation statistics for benchmarks.
293
294	-blockprofile block.out
295	    Write a goroutine blocking profile to the specified file
296	    when all tests are complete.
297	    Writes test binary as -c would.
298
299	-blockprofilerate n
300	    Control the detail provided in goroutine blocking profiles by
301	    calling runtime.SetBlockProfileRate with n.
302	    See 'go doc runtime.SetBlockProfileRate'.
303	    The profiler aims to sample, on average, one blocking event every
304	    n nanoseconds the program spends blocked. By default,
305	    if -test.blockprofile is set without this flag, all blocking events
306	    are recorded, equivalent to -test.blockprofilerate=1.
307
308	-coverprofile cover.out
309	    Write a coverage profile to the file after all tests have passed.
310	    Sets -cover.
311
312	-cpuprofile cpu.out
313	    Write a CPU profile to the specified file before exiting.
314	    Writes test binary as -c would.
315
316	-memprofile mem.out
317	    Write an allocation profile to the file after all tests have passed.
318	    Writes test binary as -c would.
319
320	-memprofilerate n
321	    Enable more precise (and expensive) memory allocation profiles by
322	    setting runtime.MemProfileRate. See 'go doc runtime.MemProfileRate'.
323	    To profile all memory allocations, use -test.memprofilerate=1.
324
325	-mutexprofile mutex.out
326	    Write a mutex contention profile to the specified file
327	    when all tests are complete.
328	    Writes test binary as -c would.
329
330	-mutexprofilefraction n
331	    Sample 1 in n stack traces of goroutines holding a
332	    contended mutex.
333
334	-outputdir directory
335	    Place output files from profiling in the specified directory,
336	    by default the directory in which "go test" is running.
337
338	-trace trace.out
339	    Write an execution trace to the specified file before exiting.
340
341Each of these flags is also recognized with an optional 'test.' prefix,
342as in -test.v. When invoking the generated test binary (the result of
343'go test -c') directly, however, the prefix is mandatory.
344
345The 'go test' command rewrites or removes recognized flags,
346as appropriate, both before and after the optional package list,
347before invoking the test binary.
348
349For instance, the command
350
351	go test -v -myflag testdata -cpuprofile=prof.out -x
352
353will compile the test binary and then run it as
354
355	pkg.test -test.v -myflag testdata -test.cpuprofile=prof.out
356
357(The -x flag is removed because it applies only to the go command's
358execution, not to the test itself.)
359
360The test flags that generate profiles (other than for coverage) also
361leave the test binary in pkg.test for use when analyzing the profiles.
362
363When 'go test' runs a test binary, it does so from within the
364corresponding package's source code directory. Depending on the test,
365it may be necessary to do the same when invoking a generated test
366binary directly.
367
368The command-line package list, if present, must appear before any
369flag not known to the go test command. Continuing the example above,
370the package list would have to appear before -myflag, but could appear
371on either side of -v.
372
373When 'go test' runs in package list mode, 'go test' caches successful
374package test results to avoid unnecessary repeated running of tests. To
375disable test caching, use any test flag or argument other than the
376cacheable flags. The idiomatic way to disable test caching explicitly
377is to use -count=1.
378
379To keep an argument for a test binary from being interpreted as a
380known flag or a package name, use -args (see 'go help test') which
381passes the remainder of the command line through to the test binary
382uninterpreted and unaltered.
383
384For instance, the command
385
386	go test -v -args -x -v
387
388will compile the test binary and then run it as
389
390	pkg.test -test.v -x -v
391
392Similarly,
393
394	go test -args math
395
396will compile the test binary and then run it as
397
398	pkg.test math
399
400In the first example, the -x and the second -v are passed through to the
401test binary unchanged and with no effect on the go command itself.
402In the second example, the argument math is passed through to the test
403binary, instead of being interpreted as the package list.
404`,
405}
406
407var HelpTestfunc = &base.Command{
408	UsageLine: "testfunc",
409	Short:     "testing functions",
410	Long: `
411The 'go test' command expects to find test, benchmark, and example functions
412in the "*_test.go" files corresponding to the package under test.
413
414A test function is one named TestXxx (where Xxx does not start with a
415lower case letter) and should have the signature,
416
417	func TestXxx(t *testing.T) { ... }
418
419A benchmark function is one named BenchmarkXxx and should have the signature,
420
421	func BenchmarkXxx(b *testing.B) { ... }
422
423An example function is similar to a test function but, instead of using
424*testing.T to report success or failure, prints output to os.Stdout.
425If the last comment in the function starts with "Output:" then the output
426is compared exactly against the comment (see examples below). If the last
427comment begins with "Unordered output:" then the output is compared to the
428comment, however the order of the lines is ignored. An example with no such
429comment is compiled but not executed. An example with no text after
430"Output:" is compiled, executed, and expected to produce no output.
431
432Godoc displays the body of ExampleXxx to demonstrate the use
433of the function, constant, or variable Xxx. An example of a method M with
434receiver type T or *T is named ExampleT_M. There may be multiple examples
435for a given function, constant, or variable, distinguished by a trailing _xxx,
436where xxx is a suffix not beginning with an upper case letter.
437
438Here is an example of an example:
439
440	func ExamplePrintln() {
441		Println("The output of\nthis example.")
442		// Output: The output of
443		// this example.
444	}
445
446Here is another example where the ordering of the output is ignored:
447
448	func ExamplePerm() {
449		for _, value := range Perm(4) {
450			fmt.Println(value)
451		}
452
453		// Unordered output: 4
454		// 2
455		// 1
456		// 3
457		// 0
458	}
459
460The entire test file is presented as the example when it contains a single
461example function, at least one other function, type, variable, or constant
462declaration, and no test or benchmark functions.
463
464See the documentation of the testing package for more information.
465`,
466}
467
468var (
469	testC            bool            // -c flag
470	testCover        bool            // -cover flag
471	testCoverMode    string          // -covermode flag
472	testCoverPaths   []string        // -coverpkg flag
473	testCoverPkgs    []*load.Package // -coverpkg flag
474	testCoverProfile string          // -coverprofile flag
475	testOutputDir    string          // -outputdir flag
476	testO            string          // -o flag
477	testProfile      string          // profiling flag that limits test to one package
478	testNeedBinary   bool            // profile needs to keep binary around
479	testJSON         bool            // -json flag
480	testV            bool            // -v flag
481	testTimeout      string          // -timeout flag
482	testArgs         []string
483	testBench        bool
484	testList         bool
485	testShowPass     bool   // show passing output
486	testVetList      string // -vet flag
487	pkgArgs          []string
488	pkgs             []*load.Package
489
490	testActualTimeout = 10 * time.Minute                  // actual timeout which is passed to tests
491	testKillTimeout   = testActualTimeout + 1*time.Minute // backup alarm
492	testCacheExpire   time.Time                           // ignore cached test results before this time
493)
494
495// testVetExplicit records whether testVetFlags were set by an explicit -vet.
496var testVetExplicit = false
497
498// testVetFlags is the list of flags to pass to vet when invoked automatically during go test.
499var testVetFlags = []string{
500	// TODO(rsc): Decide which tests are enabled by default.
501	// See golang.org/issue/18085.
502	// "-asmdecl",
503	// "-assign",
504	"-atomic",
505	"-bool",
506	"-buildtags",
507	// "-cgocall",
508	// "-composites",
509	// "-copylocks",
510	"-errorsas",
511	// "-httpresponse",
512	// "-lostcancel",
513	// "-methods",
514	"-nilfunc",
515	"-printf",
516	// "-rangeloops",
517	// "-shift",
518	// "-structtags",
519	// "-tests",
520	// "-unreachable",
521	// "-unsafeptr",
522	// "-unusedresult",
523}
524
525func testCmdUsage() {
526	fmt.Fprintf(os.Stderr, "usage: %s\n", CmdTest.UsageLine)
527	fmt.Fprintf(os.Stderr, "Run 'go help %s' and 'go help %s' for details.\n", CmdTest.LongName(), HelpTestflag.LongName())
528	os.Exit(2)
529}
530
531func runTest(cmd *base.Command, args []string) {
532	modload.LoadTests = true
533
534	pkgArgs, testArgs = testFlags(testCmdUsage, args)
535
536	work.FindExecCmd() // initialize cached result
537
538	work.BuildInit()
539	work.VetFlags = testVetFlags
540	work.VetExplicit = testVetExplicit
541
542	pkgs = load.PackagesForBuild(pkgArgs)
543	if len(pkgs) == 0 {
544		base.Fatalf("no packages to test")
545	}
546
547	if testC && len(pkgs) != 1 {
548		base.Fatalf("cannot use -c flag with multiple packages")
549	}
550	if testO != "" && len(pkgs) != 1 {
551		base.Fatalf("cannot use -o flag with multiple packages")
552	}
553	if testProfile != "" && len(pkgs) != 1 {
554		base.Fatalf("cannot use %s flag with multiple packages", testProfile)
555	}
556	initCoverProfile()
557	defer closeCoverProfile()
558
559	// If a test timeout was given and is parseable, set our kill timeout
560	// to that timeout plus one minute. This is a backup alarm in case
561	// the test wedges with a goroutine spinning and its background
562	// timer does not get a chance to fire.
563	if dt, err := time.ParseDuration(testTimeout); err == nil && dt > 0 {
564		testActualTimeout = dt
565		testKillTimeout = testActualTimeout + 1*time.Minute
566	} else if err == nil && dt == 0 {
567		// An explicit zero disables the test timeout.
568		// No timeout is passed to tests.
569		// Let it have one century (almost) before we kill it.
570		testActualTimeout = -1
571		testKillTimeout = 100 * 365 * 24 * time.Hour
572	}
573
574	// Pass timeout to tests if it exists.
575	// Prepend rather than appending so that it appears before positional arguments.
576	if testActualTimeout > 0 {
577		testArgs = append([]string{"-test.timeout=" + testActualTimeout.String()}, testArgs...)
578	}
579
580	// show passing test output (after buffering) with -v flag.
581	// must buffer because tests are running in parallel, and
582	// otherwise the output will get mixed.
583	testShowPass = testV || testList
584
585	// For 'go test -i -o x.test', we want to build x.test. Imply -c to make the logic easier.
586	if cfg.BuildI && testO != "" {
587		testC = true
588	}
589
590	// Read testcache expiration time, if present.
591	// (We implement go clean -testcache by writing an expiration date
592	// instead of searching out and deleting test result cache entries.)
593	if dir := cache.DefaultDir(); dir != "off" {
594		if data, _ := lockedfile.Read(filepath.Join(dir, "testexpire.txt")); len(data) > 0 && data[len(data)-1] == '\n' {
595			if t, err := strconv.ParseInt(string(data[:len(data)-1]), 10, 64); err == nil {
596				testCacheExpire = time.Unix(0, t)
597			}
598		}
599	}
600
601	var b work.Builder
602	b.Init()
603
604	if cfg.BuildI {
605		cfg.BuildV = testV
606
607		deps := make(map[string]bool)
608		for _, dep := range load.TestMainDeps {
609			deps[dep] = true
610		}
611
612		for _, p := range pkgs {
613			// Dependencies for each test.
614			for _, path := range p.Imports {
615				deps[path] = true
616			}
617			for _, path := range p.Resolve(p.TestImports) {
618				deps[path] = true
619			}
620			for _, path := range p.Resolve(p.XTestImports) {
621				deps[path] = true
622			}
623		}
624
625		// translate C to runtime/cgo
626		if deps["C"] {
627			delete(deps, "C")
628			deps["runtime/cgo"] = true
629		}
630		// Ignore pseudo-packages.
631		delete(deps, "unsafe")
632
633		all := []string{}
634		for path := range deps {
635			if !build.IsLocalImport(path) {
636				all = append(all, path)
637			}
638		}
639		sort.Strings(all)
640
641		a := &work.Action{Mode: "go test -i"}
642		for _, p := range load.PackagesForBuild(all) {
643			if cfg.BuildToolchainName == "gccgo" && p.Standard {
644				// gccgo's standard library packages
645				// can not be reinstalled.
646				continue
647			}
648			a.Deps = append(a.Deps, b.CompileAction(work.ModeInstall, work.ModeInstall, p))
649		}
650		b.Do(a)
651		if !testC || a.Failed {
652			return
653		}
654		b.Init()
655	}
656
657	var builds, runs, prints []*work.Action
658
659	if testCoverPaths != nil {
660		match := make([]func(*load.Package) bool, len(testCoverPaths))
661		matched := make([]bool, len(testCoverPaths))
662		for i := range testCoverPaths {
663			match[i] = load.MatchPackage(testCoverPaths[i], base.Cwd)
664		}
665
666		// Select for coverage all dependencies matching the testCoverPaths patterns.
667		for _, p := range load.TestPackageList(pkgs) {
668			haveMatch := false
669			for i := range testCoverPaths {
670				if match[i](p) {
671					matched[i] = true
672					haveMatch = true
673				}
674			}
675
676			// Silently ignore attempts to run coverage on
677			// sync/atomic when using atomic coverage mode.
678			// Atomic coverage mode uses sync/atomic, so
679			// we can't also do coverage on it.
680			if testCoverMode == "atomic" && p.Standard && p.ImportPath == "sync/atomic" {
681				continue
682			}
683
684			// If using the race detector, silently ignore
685			// attempts to run coverage on the runtime
686			// packages. It will cause the race detector
687			// to be invoked before it has been initialized.
688			if cfg.BuildRace && p.Standard && (p.ImportPath == "runtime" || strings.HasPrefix(p.ImportPath, "runtime/internal")) {
689				continue
690			}
691
692			if haveMatch {
693				testCoverPkgs = append(testCoverPkgs, p)
694			}
695		}
696
697		// Warn about -coverpkg arguments that are not actually used.
698		for i := range testCoverPaths {
699			if !matched[i] {
700				fmt.Fprintf(os.Stderr, "warning: no packages being tested depend on matches for pattern %s\n", testCoverPaths[i])
701			}
702		}
703
704		// Mark all the coverage packages for rebuilding with coverage.
705		for _, p := range testCoverPkgs {
706			// There is nothing to cover in package unsafe; it comes from the compiler.
707			if p.ImportPath == "unsafe" {
708				continue
709			}
710			p.Internal.CoverMode = testCoverMode
711			var coverFiles []string
712			coverFiles = append(coverFiles, p.GoFiles...)
713			coverFiles = append(coverFiles, p.CgoFiles...)
714			coverFiles = append(coverFiles, p.TestGoFiles...)
715			p.Internal.CoverVars = declareCoverVars(p, coverFiles...)
716			if testCover && testCoverMode == "atomic" {
717				ensureImport(p, "sync/atomic")
718			}
719		}
720	}
721
722	// Prepare build + run + print actions for all packages being tested.
723	for _, p := range pkgs {
724		// sync/atomic import is inserted by the cover tool. See #18486
725		if testCover && testCoverMode == "atomic" {
726			ensureImport(p, "sync/atomic")
727		}
728
729		buildTest, runTest, printTest, err := builderTest(&b, p)
730		if err != nil {
731			str := err.Error()
732			str = strings.TrimPrefix(str, "\n")
733			if p.ImportPath != "" {
734				base.Errorf("# %s\n%s", p.ImportPath, str)
735			} else {
736				base.Errorf("%s", str)
737			}
738			fmt.Printf("FAIL\t%s [setup failed]\n", p.ImportPath)
739			continue
740		}
741		builds = append(builds, buildTest)
742		runs = append(runs, runTest)
743		prints = append(prints, printTest)
744	}
745
746	// Ultimately the goal is to print the output.
747	root := &work.Action{Mode: "go test", Func: printExitStatus, Deps: prints}
748
749	// Force the printing of results to happen in order,
750	// one at a time.
751	for i, a := range prints {
752		if i > 0 {
753			a.Deps = append(a.Deps, prints[i-1])
754		}
755	}
756
757	// Force benchmarks to run in serial.
758	if !testC && testBench {
759		// The first run must wait for all builds.
760		// Later runs must wait for the previous run's print.
761		for i, run := range runs {
762			if i == 0 {
763				run.Deps = append(run.Deps, builds...)
764			} else {
765				run.Deps = append(run.Deps, prints[i-1])
766			}
767		}
768	}
769
770	b.Do(root)
771}
772
773// ensures that package p imports the named package
774func ensureImport(p *load.Package, pkg string) {
775	for _, d := range p.Internal.Imports {
776		if d.Name == pkg {
777			return
778		}
779	}
780
781	p1 := load.LoadImportWithFlags(pkg, p.Dir, p, &load.ImportStack{}, nil, 0)
782	if p1.Error != nil {
783		base.Fatalf("load %s: %v", pkg, p1.Error)
784	}
785
786	p.Internal.Imports = append(p.Internal.Imports, p1)
787}
788
789var windowsBadWords = []string{
790	"install",
791	"patch",
792	"setup",
793	"update",
794}
795
796func builderTest(b *work.Builder, p *load.Package) (buildAction, runAction, printAction *work.Action, err error) {
797	if len(p.TestGoFiles)+len(p.XTestGoFiles) == 0 {
798		build := b.CompileAction(work.ModeBuild, work.ModeBuild, p)
799		run := &work.Action{Mode: "test run", Package: p, Deps: []*work.Action{build}}
800		addTestVet(b, p, run, nil)
801		print := &work.Action{Mode: "test print", Func: builderNoTest, Package: p, Deps: []*work.Action{run}}
802		return build, run, print, nil
803	}
804
805	// Build Package structs describing:
806	//	pmain - pkg.test binary
807	//	ptest - package + test files
808	//	pxtest - package of external test files
809	var cover *load.TestCover
810	if testCover {
811		cover = &load.TestCover{
812			Mode:     testCoverMode,
813			Local:    testCover && testCoverPaths == nil,
814			Pkgs:     testCoverPkgs,
815			Paths:    testCoverPaths,
816			DeclVars: declareCoverVars,
817		}
818	}
819	pmain, ptest, pxtest, err := load.TestPackagesFor(p, cover)
820	if err != nil {
821		return nil, nil, nil, err
822	}
823
824	// Use last element of import path, not package name.
825	// They differ when package name is "main".
826	// But if the import path is "command-line-arguments",
827	// like it is during 'go run', use the package name.
828	var elem string
829	if p.ImportPath == "command-line-arguments" {
830		elem = p.Name
831	} else {
832		elem = p.DefaultExecName()
833	}
834	testBinary := elem + ".test"
835
836	testDir := b.NewObjdir()
837	if err := b.Mkdir(testDir); err != nil {
838		return nil, nil, nil, err
839	}
840
841	pmain.Dir = testDir
842	pmain.Internal.OmitDebug = !testC && !testNeedBinary
843
844	if !cfg.BuildN {
845		// writeTestmain writes _testmain.go,
846		// using the test description gathered in t.
847		if err := ioutil.WriteFile(testDir+"_testmain.go", *pmain.Internal.TestmainGo, 0666); err != nil {
848			return nil, nil, nil, err
849		}
850	}
851
852	// Set compile objdir to testDir we've already created,
853	// so that the default file path stripping applies to _testmain.go.
854	b.CompileAction(work.ModeBuild, work.ModeBuild, pmain).Objdir = testDir
855
856	a := b.LinkAction(work.ModeBuild, work.ModeBuild, pmain)
857	a.Target = testDir + testBinary + cfg.ExeSuffix
858	if cfg.Goos == "windows" {
859		// There are many reserved words on Windows that,
860		// if used in the name of an executable, cause Windows
861		// to try to ask for extra permissions.
862		// The word list includes setup, install, update, and patch,
863		// but it does not appear to be defined anywhere.
864		// We have run into this trying to run the
865		// go.codereview/patch tests.
866		// For package names containing those words, use test.test.exe
867		// instead of pkgname.test.exe.
868		// Note that this file name is only used in the Go command's
869		// temporary directory. If the -c or other flags are
870		// given, the code below will still use pkgname.test.exe.
871		// There are two user-visible effects of this change.
872		// First, you can actually run 'go test' in directories that
873		// have names that Windows thinks are installer-like,
874		// without getting a dialog box asking for more permissions.
875		// Second, in the Windows process listing during go test,
876		// the test shows up as test.test.exe, not pkgname.test.exe.
877		// That second one is a drawback, but it seems a small
878		// price to pay for the test running at all.
879		// If maintaining the list of bad words is too onerous,
880		// we could just do this always on Windows.
881		for _, bad := range windowsBadWords {
882			if strings.Contains(testBinary, bad) {
883				a.Target = testDir + "test.test" + cfg.ExeSuffix
884				break
885			}
886		}
887	}
888	buildAction = a
889	var installAction, cleanAction *work.Action
890	if testC || testNeedBinary {
891		// -c or profiling flag: create action to copy binary to ./test.out.
892		target := filepath.Join(base.Cwd, testBinary+cfg.ExeSuffix)
893		if testO != "" {
894			target = testO
895			if !filepath.IsAbs(target) {
896				target = filepath.Join(base.Cwd, target)
897			}
898		}
899		if target == os.DevNull {
900			runAction = buildAction
901		} else {
902			pmain.Target = target
903			installAction = &work.Action{
904				Mode:    "test build",
905				Func:    work.BuildInstallFunc,
906				Deps:    []*work.Action{buildAction},
907				Package: pmain,
908				Target:  target,
909			}
910			runAction = installAction // make sure runAction != nil even if not running test
911		}
912	}
913	var vetRunAction *work.Action
914	if testC {
915		printAction = &work.Action{Mode: "test print (nop)", Package: p, Deps: []*work.Action{runAction}} // nop
916		vetRunAction = printAction
917	} else {
918		// run test
919		c := new(runCache)
920		runAction = &work.Action{
921			Mode:       "test run",
922			Func:       c.builderRunTest,
923			Deps:       []*work.Action{buildAction},
924			Package:    p,
925			IgnoreFail: true, // run (prepare output) even if build failed
926			TryCache:   c.tryCache,
927			Objdir:     testDir,
928		}
929		vetRunAction = runAction
930		cleanAction = &work.Action{
931			Mode:       "test clean",
932			Func:       builderCleanTest,
933			Deps:       []*work.Action{runAction},
934			Package:    p,
935			IgnoreFail: true, // clean even if test failed
936			Objdir:     testDir,
937		}
938		printAction = &work.Action{
939			Mode:       "test print",
940			Func:       builderPrintTest,
941			Deps:       []*work.Action{cleanAction},
942			Package:    p,
943			IgnoreFail: true, // print even if test failed
944		}
945	}
946
947	if len(ptest.GoFiles)+len(ptest.CgoFiles) > 0 {
948		addTestVet(b, ptest, vetRunAction, installAction)
949	}
950	if pxtest != nil {
951		addTestVet(b, pxtest, vetRunAction, installAction)
952	}
953
954	if installAction != nil {
955		if runAction != installAction {
956			installAction.Deps = append(installAction.Deps, runAction)
957		}
958		if cleanAction != nil {
959			cleanAction.Deps = append(cleanAction.Deps, installAction)
960		}
961	}
962
963	return buildAction, runAction, printAction, nil
964}
965
966func addTestVet(b *work.Builder, p *load.Package, runAction, installAction *work.Action) {
967	if testVetList == "off" {
968		return
969	}
970
971	vet := b.VetAction(work.ModeBuild, work.ModeBuild, p)
972	runAction.Deps = append(runAction.Deps, vet)
973	// Install will clean the build directory.
974	// Make sure vet runs first.
975	// The install ordering in b.VetAction does not apply here
976	// because we are using a custom installAction (created above).
977	if installAction != nil {
978		installAction.Deps = append(installAction.Deps, vet)
979	}
980}
981
982// isTestFile reports whether the source file is a set of tests and should therefore
983// be excluded from coverage analysis.
984func isTestFile(file string) bool {
985	// We don't cover tests, only the code they test.
986	return strings.HasSuffix(file, "_test.go")
987}
988
989// declareCoverVars attaches the required cover variables names
990// to the files, to be used when annotating the files.
991func declareCoverVars(p *load.Package, files ...string) map[string]*load.CoverVar {
992	coverVars := make(map[string]*load.CoverVar)
993	coverIndex := 0
994	// We create the cover counters as new top-level variables in the package.
995	// We need to avoid collisions with user variables (GoCover_0 is unlikely but still)
996	// and more importantly with dot imports of other covered packages,
997	// so we append 12 hex digits from the SHA-256 of the import path.
998	// The point is only to avoid accidents, not to defeat users determined to
999	// break things.
1000	sum := sha256.Sum256([]byte(p.ImportPath))
1001	h := fmt.Sprintf("%x", sum[:6])
1002	for _, file := range files {
1003		if isTestFile(file) {
1004			continue
1005		}
1006		// For a package that is "local" (imported via ./ import or command line, outside GOPATH),
1007		// we record the full path to the file name.
1008		// Otherwise we record the import path, then a forward slash, then the file name.
1009		// This makes profiles within GOPATH file system-independent.
1010		// These names appear in the cmd/cover HTML interface.
1011		var longFile string
1012		if p.Internal.Local {
1013			longFile = filepath.Join(p.Dir, file)
1014		} else {
1015			longFile = path.Join(p.ImportPath, file)
1016		}
1017		coverVars[file] = &load.CoverVar{
1018			File: longFile,
1019			Var:  fmt.Sprintf("GoCover_%d_%x", coverIndex, h),
1020		}
1021		coverIndex++
1022	}
1023	return coverVars
1024}
1025
1026var noTestsToRun = []byte("\ntesting: warning: no tests to run\n")
1027
1028type runCache struct {
1029	disableCache bool // cache should be disabled for this run
1030
1031	buf *bytes.Buffer
1032	id1 cache.ActionID
1033	id2 cache.ActionID
1034}
1035
1036// stdoutMu and lockedStdout provide a locked standard output
1037// that guarantees never to interlace writes from multiple
1038// goroutines, so that we can have multiple JSON streams writing
1039// to a lockedStdout simultaneously and know that events will
1040// still be intelligible.
1041var stdoutMu sync.Mutex
1042
1043type lockedStdout struct{}
1044
1045func (lockedStdout) Write(b []byte) (int, error) {
1046	stdoutMu.Lock()
1047	defer stdoutMu.Unlock()
1048	return os.Stdout.Write(b)
1049}
1050
1051// builderRunTest is the action for running a test binary.
1052func (c *runCache) builderRunTest(b *work.Builder, a *work.Action) error {
1053	if a.Failed {
1054		// We were unable to build the binary.
1055		a.Failed = false
1056		a.TestOutput = new(bytes.Buffer)
1057		fmt.Fprintf(a.TestOutput, "FAIL\t%s [build failed]\n", a.Package.ImportPath)
1058		base.SetExitStatus(1)
1059		return nil
1060	}
1061
1062	var stdout io.Writer = os.Stdout
1063	if testJSON {
1064		json := test2json.NewConverter(lockedStdout{}, a.Package.ImportPath, test2json.Timestamp)
1065		defer json.Close()
1066		stdout = json
1067	}
1068
1069	var buf bytes.Buffer
1070	if len(pkgArgs) == 0 || testBench {
1071		// Stream test output (no buffering) when no package has
1072		// been given on the command line (implicit current directory)
1073		// or when benchmarking.
1074		// No change to stdout.
1075	} else {
1076		// If we're only running a single package under test or if parallelism is
1077		// set to 1, and if we're displaying all output (testShowPass), we can
1078		// hurry the output along, echoing it as soon as it comes in.
1079		// We still have to copy to &buf for caching the result. This special
1080		// case was introduced in Go 1.5 and is intentionally undocumented:
1081		// the exact details of output buffering are up to the go command and
1082		// subject to change. It would be nice to remove this special case
1083		// entirely, but it is surely very helpful to see progress being made
1084		// when tests are run on slow single-CPU ARM systems.
1085		//
1086		// If we're showing JSON output, then display output as soon as
1087		// possible even when multiple tests are being run: the JSON output
1088		// events are attributed to specific package tests, so interlacing them
1089		// is OK.
1090		if testShowPass && (len(pkgs) == 1 || cfg.BuildP == 1) || testJSON {
1091			// Write both to stdout and buf, for possible saving
1092			// to cache, and for looking for the "no tests to run" message.
1093			stdout = io.MultiWriter(stdout, &buf)
1094		} else {
1095			stdout = &buf
1096		}
1097	}
1098
1099	if c.buf == nil {
1100		// We did not find a cached result using the link step action ID,
1101		// so we ran the link step. Try again now with the link output
1102		// content ID. The attempt using the action ID makes sure that
1103		// if the link inputs don't change, we reuse the cached test
1104		// result without even rerunning the linker. The attempt using
1105		// the link output (test binary) content ID makes sure that if
1106		// we have different link inputs but the same final binary,
1107		// we still reuse the cached test result.
1108		// c.saveOutput will store the result under both IDs.
1109		c.tryCacheWithID(b, a, a.Deps[0].BuildContentID())
1110	}
1111	if c.buf != nil {
1112		if stdout != &buf {
1113			stdout.Write(c.buf.Bytes())
1114			c.buf.Reset()
1115		}
1116		a.TestOutput = c.buf
1117		return nil
1118	}
1119
1120	execCmd := work.FindExecCmd()
1121	testlogArg := []string{}
1122	if !c.disableCache && len(execCmd) == 0 {
1123		testlogArg = []string{"-test.testlogfile=" + a.Objdir + "testlog.txt"}
1124	}
1125	args := str.StringList(execCmd, a.Deps[0].BuiltTarget(), testlogArg, testArgs)
1126
1127	if testCoverProfile != "" {
1128		// Write coverage to temporary profile, for merging later.
1129		for i, arg := range args {
1130			if strings.HasPrefix(arg, "-test.coverprofile=") {
1131				args[i] = "-test.coverprofile=" + a.Objdir + "_cover_.out"
1132			}
1133		}
1134	}
1135
1136	if cfg.BuildN || cfg.BuildX {
1137		b.Showcmd("", "%s", strings.Join(args, " "))
1138		if cfg.BuildN {
1139			return nil
1140		}
1141	}
1142
1143	cmd := exec.Command(args[0], args[1:]...)
1144	cmd.Dir = a.Package.Dir
1145	cmd.Env = base.EnvForDir(cmd.Dir, cfg.OrigEnv[:len(cfg.OrigEnv):len(cfg.OrigEnv)])
1146	cmd.Stdout = stdout
1147	cmd.Stderr = stdout
1148
1149	// If there are any local SWIG dependencies, we want to load
1150	// the shared library from the build directory.
1151	if a.Package.UsesSwig() {
1152		env := cmd.Env
1153		found := false
1154		prefix := "LD_LIBRARY_PATH="
1155		for i, v := range env {
1156			if strings.HasPrefix(v, prefix) {
1157				env[i] = v + ":."
1158				found = true
1159				break
1160			}
1161		}
1162		if !found {
1163			env = append(env, "LD_LIBRARY_PATH=.")
1164		}
1165		cmd.Env = env
1166	}
1167
1168	t0 := time.Now()
1169	err := cmd.Start()
1170
1171	// This is a last-ditch deadline to detect and
1172	// stop wedged test binaries, to keep the builders
1173	// running.
1174	if err == nil {
1175		tick := time.NewTimer(testKillTimeout)
1176		base.StartSigHandlers()
1177		done := make(chan error)
1178		go func() {
1179			done <- cmd.Wait()
1180		}()
1181	Outer:
1182		select {
1183		case err = <-done:
1184			// ok
1185		case <-tick.C:
1186			if base.SignalTrace != nil {
1187				// Send a quit signal in the hope that the program will print
1188				// a stack trace and exit. Give it five seconds before resorting
1189				// to Kill.
1190				cmd.Process.Signal(base.SignalTrace)
1191				select {
1192				case err = <-done:
1193					fmt.Fprintf(cmd.Stdout, "*** Test killed with %v: ran too long (%v).\n", base.SignalTrace, testKillTimeout)
1194					break Outer
1195				case <-time.After(5 * time.Second):
1196				}
1197			}
1198			cmd.Process.Kill()
1199			err = <-done
1200			fmt.Fprintf(cmd.Stdout, "*** Test killed: ran too long (%v).\n", testKillTimeout)
1201		}
1202		tick.Stop()
1203	}
1204	out := buf.Bytes()
1205	a.TestOutput = &buf
1206	t := fmt.Sprintf("%.3fs", time.Since(t0).Seconds())
1207
1208	mergeCoverProfile(cmd.Stdout, a.Objdir+"_cover_.out")
1209
1210	if err == nil {
1211		norun := ""
1212		if !testShowPass && !testJSON {
1213			buf.Reset()
1214		}
1215		if bytes.HasPrefix(out, noTestsToRun[1:]) || bytes.Contains(out, noTestsToRun) {
1216			norun = " [no tests to run]"
1217		}
1218		fmt.Fprintf(cmd.Stdout, "ok  \t%s\t%s%s%s\n", a.Package.ImportPath, t, coveragePercentage(out), norun)
1219		c.saveOutput(a)
1220	} else {
1221		base.SetExitStatus(1)
1222		// If there was test output, assume we don't need to print the exit status.
1223		// Buf there's no test output, do print the exit status.
1224		if len(out) == 0 {
1225			fmt.Fprintf(cmd.Stdout, "%s\n", err)
1226		}
1227		// NOTE(golang.org/issue/37555): test2json reports that a test passes
1228		// unless "FAIL" is printed at the beginning of a line. The test may not
1229		// actually print that if it panics, exits, or terminates abnormally,
1230		// so we print it here. We can't always check whether it was printed
1231		// because some tests need stdout to be a terminal (golang.org/issue/34791),
1232		// not a pipe.
1233		// TODO(golang.org/issue/29062): tests that exit with status 0 without
1234		// printing a final result should fail.
1235		fmt.Fprintf(cmd.Stdout, "FAIL\t%s\t%s\n", a.Package.ImportPath, t)
1236	}
1237
1238	if cmd.Stdout != &buf {
1239		buf.Reset() // cmd.Stdout was going to os.Stdout already
1240	}
1241	return nil
1242}
1243
1244// tryCache is called just before the link attempt,
1245// to see if the test result is cached and therefore the link is unneeded.
1246// It reports whether the result can be satisfied from cache.
1247func (c *runCache) tryCache(b *work.Builder, a *work.Action) bool {
1248	return c.tryCacheWithID(b, a, a.Deps[0].BuildActionID())
1249}
1250
1251func (c *runCache) tryCacheWithID(b *work.Builder, a *work.Action, id string) bool {
1252	if len(pkgArgs) == 0 {
1253		// Caching does not apply to "go test",
1254		// only to "go test foo" (including "go test .").
1255		if cache.DebugTest {
1256			fmt.Fprintf(os.Stderr, "testcache: caching disabled in local directory mode\n")
1257		}
1258		c.disableCache = true
1259		return false
1260	}
1261
1262	if a.Package.Root == "" {
1263		// Caching does not apply to tests outside of any module, GOPATH, or GOROOT.
1264		if cache.DebugTest {
1265			fmt.Fprintf(os.Stderr, "testcache: caching disabled for package outside of module root, GOPATH, or GOROOT: %s\n", a.Package.ImportPath)
1266		}
1267		c.disableCache = true
1268		return false
1269	}
1270
1271	var cacheArgs []string
1272	for _, arg := range testArgs {
1273		i := strings.Index(arg, "=")
1274		if i < 0 || !strings.HasPrefix(arg, "-test.") {
1275			if cache.DebugTest {
1276				fmt.Fprintf(os.Stderr, "testcache: caching disabled for test argument: %s\n", arg)
1277			}
1278			c.disableCache = true
1279			return false
1280		}
1281		switch arg[:i] {
1282		case "-test.cpu",
1283			"-test.list",
1284			"-test.parallel",
1285			"-test.run",
1286			"-test.short",
1287			"-test.v":
1288			// These are cacheable.
1289			// Note that this list is documented above,
1290			// so if you add to this list, update the docs too.
1291			cacheArgs = append(cacheArgs, arg)
1292
1293		case "-test.timeout":
1294			// Special case: this is cacheable but ignored during the hash.
1295			// Do not add to cacheArgs.
1296
1297		default:
1298			// nothing else is cacheable
1299			if cache.DebugTest {
1300				fmt.Fprintf(os.Stderr, "testcache: caching disabled for test argument: %s\n", arg)
1301			}
1302			c.disableCache = true
1303			return false
1304		}
1305	}
1306
1307	if cache.Default() == nil {
1308		if cache.DebugTest {
1309			fmt.Fprintf(os.Stderr, "testcache: GOCACHE=off\n")
1310		}
1311		c.disableCache = true
1312		return false
1313	}
1314
1315	// The test cache result fetch is a two-level lookup.
1316	//
1317	// First, we use the content hash of the test binary
1318	// and its command-line arguments to find the
1319	// list of environment variables and files consulted
1320	// the last time the test was run with those arguments.
1321	// (To avoid unnecessary links, we store this entry
1322	// under two hashes: id1 uses the linker inputs as a
1323	// proxy for the test binary, and id2 uses the actual
1324	// test binary. If the linker inputs are unchanged,
1325	// this way we avoid the link step, even though we
1326	// do not cache link outputs.)
1327	//
1328	// Second, we compute a hash of the values of the
1329	// environment variables and the content of the files
1330	// listed in the log from the previous run.
1331	// Then we look up test output using a combination of
1332	// the hash from the first part (testID) and the hash of the
1333	// test inputs (testInputsID).
1334	//
1335	// In order to store a new test result, we must redo the
1336	// testInputsID computation using the log from the run
1337	// we want to cache, and then we store that new log and
1338	// the new outputs.
1339
1340	h := cache.NewHash("testResult")
1341	fmt.Fprintf(h, "test binary %s args %q execcmd %q", id, cacheArgs, work.ExecCmd)
1342	testID := h.Sum()
1343	if c.id1 == (cache.ActionID{}) {
1344		c.id1 = testID
1345	} else {
1346		c.id2 = testID
1347	}
1348	if cache.DebugTest {
1349		fmt.Fprintf(os.Stderr, "testcache: %s: test ID %x => %x\n", a.Package.ImportPath, id, testID)
1350	}
1351
1352	// Load list of referenced environment variables and files
1353	// from last run of testID, and compute hash of that content.
1354	data, entry, err := cache.Default().GetBytes(testID)
1355	if !bytes.HasPrefix(data, testlogMagic) || data[len(data)-1] != '\n' {
1356		if cache.DebugTest {
1357			if err != nil {
1358				fmt.Fprintf(os.Stderr, "testcache: %s: input list not found: %v\n", a.Package.ImportPath, err)
1359			} else {
1360				fmt.Fprintf(os.Stderr, "testcache: %s: input list malformed\n", a.Package.ImportPath)
1361			}
1362		}
1363		return false
1364	}
1365	testInputsID, err := computeTestInputsID(a, data)
1366	if err != nil {
1367		return false
1368	}
1369	if cache.DebugTest {
1370		fmt.Fprintf(os.Stderr, "testcache: %s: test ID %x => input ID %x => %x\n", a.Package.ImportPath, testID, testInputsID, testAndInputKey(testID, testInputsID))
1371	}
1372
1373	// Parse cached result in preparation for changing run time to "(cached)".
1374	// If we can't parse the cached result, don't use it.
1375	data, entry, err = cache.Default().GetBytes(testAndInputKey(testID, testInputsID))
1376	if len(data) == 0 || data[len(data)-1] != '\n' {
1377		if cache.DebugTest {
1378			if err != nil {
1379				fmt.Fprintf(os.Stderr, "testcache: %s: test output not found: %v\n", a.Package.ImportPath, err)
1380			} else {
1381				fmt.Fprintf(os.Stderr, "testcache: %s: test output malformed\n", a.Package.ImportPath)
1382			}
1383		}
1384		return false
1385	}
1386	if entry.Time.Before(testCacheExpire) {
1387		if cache.DebugTest {
1388			fmt.Fprintf(os.Stderr, "testcache: %s: test output expired due to go clean -testcache\n", a.Package.ImportPath)
1389		}
1390		return false
1391	}
1392	i := bytes.LastIndexByte(data[:len(data)-1], '\n') + 1
1393	if !bytes.HasPrefix(data[i:], []byte("ok  \t")) {
1394		if cache.DebugTest {
1395			fmt.Fprintf(os.Stderr, "testcache: %s: test output malformed\n", a.Package.ImportPath)
1396		}
1397		return false
1398	}
1399	j := bytes.IndexByte(data[i+len("ok  \t"):], '\t')
1400	if j < 0 {
1401		if cache.DebugTest {
1402			fmt.Fprintf(os.Stderr, "testcache: %s: test output malformed\n", a.Package.ImportPath)
1403		}
1404		return false
1405	}
1406	j += i + len("ok  \t") + 1
1407
1408	// Committed to printing.
1409	c.buf = new(bytes.Buffer)
1410	c.buf.Write(data[:j])
1411	c.buf.WriteString("(cached)")
1412	for j < len(data) && ('0' <= data[j] && data[j] <= '9' || data[j] == '.' || data[j] == 's') {
1413		j++
1414	}
1415	c.buf.Write(data[j:])
1416	return true
1417}
1418
1419var errBadTestInputs = errors.New("error parsing test inputs")
1420var testlogMagic = []byte("# test log\n") // known to testing/internal/testdeps/deps.go
1421
1422// computeTestInputsID computes the "test inputs ID"
1423// (see comment in tryCacheWithID above) for the
1424// test log.
1425func computeTestInputsID(a *work.Action, testlog []byte) (cache.ActionID, error) {
1426	testlog = bytes.TrimPrefix(testlog, testlogMagic)
1427	h := cache.NewHash("testInputs")
1428	pwd := a.Package.Dir
1429	for _, line := range bytes.Split(testlog, []byte("\n")) {
1430		if len(line) == 0 {
1431			continue
1432		}
1433		s := string(line)
1434		i := strings.Index(s, " ")
1435		if i < 0 {
1436			if cache.DebugTest {
1437				fmt.Fprintf(os.Stderr, "testcache: %s: input list malformed (%q)\n", a.Package.ImportPath, line)
1438			}
1439			return cache.ActionID{}, errBadTestInputs
1440		}
1441		op := s[:i]
1442		name := s[i+1:]
1443		switch op {
1444		default:
1445			if cache.DebugTest {
1446				fmt.Fprintf(os.Stderr, "testcache: %s: input list malformed (%q)\n", a.Package.ImportPath, line)
1447			}
1448			return cache.ActionID{}, errBadTestInputs
1449		case "getenv":
1450			fmt.Fprintf(h, "env %s %x\n", name, hashGetenv(name))
1451		case "chdir":
1452			pwd = name // always absolute
1453			fmt.Fprintf(h, "chdir %s %x\n", name, hashStat(name))
1454		case "stat":
1455			if !filepath.IsAbs(name) {
1456				name = filepath.Join(pwd, name)
1457			}
1458			if a.Package.Root == "" || !inDir(name, a.Package.Root) {
1459				// Do not recheck files outside the module, GOPATH, or GOROOT root.
1460				break
1461			}
1462			fmt.Fprintf(h, "stat %s %x\n", name, hashStat(name))
1463		case "open":
1464			if !filepath.IsAbs(name) {
1465				name = filepath.Join(pwd, name)
1466			}
1467			if a.Package.Root == "" || !inDir(name, a.Package.Root) {
1468				// Do not recheck files outside the module, GOPATH, or GOROOT root.
1469				break
1470			}
1471			fh, err := hashOpen(name)
1472			if err != nil {
1473				if cache.DebugTest {
1474					fmt.Fprintf(os.Stderr, "testcache: %s: input file %s: %s\n", a.Package.ImportPath, name, err)
1475				}
1476				return cache.ActionID{}, err
1477			}
1478			fmt.Fprintf(h, "open %s %x\n", name, fh)
1479		}
1480	}
1481	sum := h.Sum()
1482	return sum, nil
1483}
1484
1485func inDir(path, dir string) bool {
1486	if str.HasFilePathPrefix(path, dir) {
1487		return true
1488	}
1489	xpath, err1 := filepath.EvalSymlinks(path)
1490	xdir, err2 := filepath.EvalSymlinks(dir)
1491	if err1 == nil && err2 == nil && str.HasFilePathPrefix(xpath, xdir) {
1492		return true
1493	}
1494	return false
1495}
1496
1497func hashGetenv(name string) cache.ActionID {
1498	h := cache.NewHash("getenv")
1499	v, ok := os.LookupEnv(name)
1500	if !ok {
1501		h.Write([]byte{0})
1502	} else {
1503		h.Write([]byte{1})
1504		h.Write([]byte(v))
1505	}
1506	return h.Sum()
1507}
1508
1509const modTimeCutoff = 2 * time.Second
1510
1511var errFileTooNew = errors.New("file used as input is too new")
1512
1513func hashOpen(name string) (cache.ActionID, error) {
1514	h := cache.NewHash("open")
1515	info, err := os.Stat(name)
1516	if err != nil {
1517		fmt.Fprintf(h, "err %v\n", err)
1518		return h.Sum(), nil
1519	}
1520	hashWriteStat(h, info)
1521	if info.IsDir() {
1522		names, err := ioutil.ReadDir(name)
1523		if err != nil {
1524			fmt.Fprintf(h, "err %v\n", err)
1525		}
1526		for _, f := range names {
1527			fmt.Fprintf(h, "file %s ", f.Name())
1528			hashWriteStat(h, f)
1529		}
1530	} else if info.Mode().IsRegular() {
1531		// Because files might be very large, do not attempt
1532		// to hash the entirety of their content. Instead assume
1533		// the mtime and size recorded in hashWriteStat above
1534		// are good enough.
1535		//
1536		// To avoid problems for very recent files where a new
1537		// write might not change the mtime due to file system
1538		// mtime precision, reject caching if a file was read that
1539		// is less than modTimeCutoff old.
1540		if time.Since(info.ModTime()) < modTimeCutoff {
1541			return cache.ActionID{}, errFileTooNew
1542		}
1543	}
1544	return h.Sum(), nil
1545}
1546
1547func hashStat(name string) cache.ActionID {
1548	h := cache.NewHash("stat")
1549	if info, err := os.Stat(name); err != nil {
1550		fmt.Fprintf(h, "err %v\n", err)
1551	} else {
1552		hashWriteStat(h, info)
1553	}
1554	if info, err := os.Lstat(name); err != nil {
1555		fmt.Fprintf(h, "err %v\n", err)
1556	} else {
1557		hashWriteStat(h, info)
1558	}
1559	return h.Sum()
1560}
1561
1562func hashWriteStat(h io.Writer, info os.FileInfo) {
1563	fmt.Fprintf(h, "stat %d %x %v %v\n", info.Size(), uint64(info.Mode()), info.ModTime(), info.IsDir())
1564}
1565
1566// testAndInputKey returns the actual cache key for the pair (testID, testInputsID).
1567func testAndInputKey(testID, testInputsID cache.ActionID) cache.ActionID {
1568	return cache.Subkey(testID, fmt.Sprintf("inputs:%x", testInputsID))
1569}
1570
1571func (c *runCache) saveOutput(a *work.Action) {
1572	if c.id1 == (cache.ActionID{}) && c.id2 == (cache.ActionID{}) {
1573		return
1574	}
1575
1576	// See comment about two-level lookup in tryCacheWithID above.
1577	testlog, err := ioutil.ReadFile(a.Objdir + "testlog.txt")
1578	if err != nil || !bytes.HasPrefix(testlog, testlogMagic) || testlog[len(testlog)-1] != '\n' {
1579		if cache.DebugTest {
1580			if err != nil {
1581				fmt.Fprintf(os.Stderr, "testcache: %s: reading testlog: %v\n", a.Package.ImportPath, err)
1582			} else {
1583				fmt.Fprintf(os.Stderr, "testcache: %s: reading testlog: malformed\n", a.Package.ImportPath)
1584			}
1585		}
1586		return
1587	}
1588	testInputsID, err := computeTestInputsID(a, testlog)
1589	if err != nil {
1590		return
1591	}
1592	if c.id1 != (cache.ActionID{}) {
1593		if cache.DebugTest {
1594			fmt.Fprintf(os.Stderr, "testcache: %s: save test ID %x => input ID %x => %x\n", a.Package.ImportPath, c.id1, testInputsID, testAndInputKey(c.id1, testInputsID))
1595		}
1596		cache.Default().PutNoVerify(c.id1, bytes.NewReader(testlog))
1597		cache.Default().PutNoVerify(testAndInputKey(c.id1, testInputsID), bytes.NewReader(a.TestOutput.Bytes()))
1598	}
1599	if c.id2 != (cache.ActionID{}) {
1600		if cache.DebugTest {
1601			fmt.Fprintf(os.Stderr, "testcache: %s: save test ID %x => input ID %x => %x\n", a.Package.ImportPath, c.id2, testInputsID, testAndInputKey(c.id2, testInputsID))
1602		}
1603		cache.Default().PutNoVerify(c.id2, bytes.NewReader(testlog))
1604		cache.Default().PutNoVerify(testAndInputKey(c.id2, testInputsID), bytes.NewReader(a.TestOutput.Bytes()))
1605	}
1606}
1607
1608// coveragePercentage returns the coverage results (if enabled) for the
1609// test. It uncovers the data by scanning the output from the test run.
1610func coveragePercentage(out []byte) string {
1611	if !testCover {
1612		return ""
1613	}
1614	// The string looks like
1615	//	test coverage for encoding/binary: 79.9% of statements
1616	// Extract the piece from the percentage to the end of the line.
1617	re := regexp.MustCompile(`coverage: (.*)\n`)
1618	matches := re.FindSubmatch(out)
1619	if matches == nil {
1620		// Probably running "go test -cover" not "go test -cover fmt".
1621		// The coverage output will appear in the output directly.
1622		return ""
1623	}
1624	return fmt.Sprintf("\tcoverage: %s", matches[1])
1625}
1626
1627// builderCleanTest is the action for cleaning up after a test.
1628func builderCleanTest(b *work.Builder, a *work.Action) error {
1629	if cfg.BuildWork {
1630		return nil
1631	}
1632	if cfg.BuildX {
1633		b.Showcmd("", "rm -r %s", a.Objdir)
1634	}
1635	os.RemoveAll(a.Objdir)
1636	return nil
1637}
1638
1639// builderPrintTest is the action for printing a test result.
1640func builderPrintTest(b *work.Builder, a *work.Action) error {
1641	clean := a.Deps[0]
1642	run := clean.Deps[0]
1643	if run.TestOutput != nil {
1644		os.Stdout.Write(run.TestOutput.Bytes())
1645		run.TestOutput = nil
1646	}
1647	return nil
1648}
1649
1650// builderNoTest is the action for testing a package with no test files.
1651func builderNoTest(b *work.Builder, a *work.Action) error {
1652	var stdout io.Writer = os.Stdout
1653	if testJSON {
1654		json := test2json.NewConverter(lockedStdout{}, a.Package.ImportPath, test2json.Timestamp)
1655		defer json.Close()
1656		stdout = json
1657	}
1658	fmt.Fprintf(stdout, "?   \t%s\t[no test files]\n", a.Package.ImportPath)
1659	return nil
1660}
1661
1662// printExitStatus is the action for printing the exit status
1663func printExitStatus(b *work.Builder, a *work.Action) error {
1664	if !testJSON && len(pkgArgs) != 0 {
1665		if base.GetExitStatus() != 0 {
1666			fmt.Println("FAIL")
1667			return nil
1668		}
1669	}
1670	return nil
1671}
1672