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