github.com/mangodowner/go-gm@v0.0.0-20180818020936-8baa2bd4408c/src/cmd/go/internal/test/test.go (about)

     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  
     5  package test
     6  
     7  import (
     8  	"bytes"
     9  	"errors"
    10  	"fmt"
    11  	"go/ast"
    12  	"go/build"
    13  	"go/doc"
    14  	"go/parser"
    15  	"go/token"
    16  	"os"
    17  	"os/exec"
    18  	"path"
    19  	"path/filepath"
    20  	"regexp"
    21  	"runtime"
    22  	"sort"
    23  	"strings"
    24  	"text/template"
    25  	"time"
    26  	"unicode"
    27  	"unicode/utf8"
    28  
    29  	"cmd/go/internal/base"
    30  	"cmd/go/internal/cfg"
    31  	"cmd/go/internal/load"
    32  	"cmd/go/internal/str"
    33  	"cmd/go/internal/work"
    34  )
    35  
    36  // Break init loop.
    37  func init() {
    38  	CmdTest.Run = runTest
    39  }
    40  
    41  const testUsage = "test [build/test flags] [packages] [build/test flags & test binary flags]"
    42  
    43  var CmdTest = &base.Command{
    44  	CustomFlags: true,
    45  	UsageLine:   testUsage,
    46  	Short:       "test packages",
    47  	Long: `
    48  'Go test' automates testing the packages named by the import paths.
    49  It prints a summary of the test results in the format:
    50  
    51  	ok   archive/tar   0.011s
    52  	FAIL archive/zip   0.022s
    53  	ok   compress/gzip 0.033s
    54  	...
    55  
    56  followed by detailed output for each failed package.
    57  
    58  'Go test' recompiles each package along with any files with names matching
    59  the file pattern "*_test.go".
    60  Files whose names begin with "_" (including "_test.go") or "." are ignored.
    61  These additional files can contain test functions, benchmark functions, and
    62  example functions. See 'go help testfunc' for more.
    63  Each listed package causes the execution of a separate test binary.
    64  
    65  Test files that declare a package with the suffix "_test" will be compiled as a
    66  separate package, and then linked and run with the main test binary.
    67  
    68  The go tool will ignore a directory named "testdata", making it available
    69  to hold ancillary data needed by the tests.
    70  
    71  By default, go test needs no arguments. It compiles and tests the package
    72  with source in the current directory, including tests, and runs the tests.
    73  
    74  The package is built in a temporary directory so it does not interfere with the
    75  non-test installation.
    76  
    77  ` + strings.TrimSpace(testFlag1) + ` See 'go help testflag' for details.
    78  
    79  For more about build flags, see 'go help build'.
    80  For more about specifying packages, see 'go help packages'.
    81  
    82  See also: go build, go vet.
    83  `,
    84  }
    85  
    86  const testFlag1 = `
    87  In addition to the build flags, the flags handled by 'go test' itself are:
    88  
    89  	-args
    90  	    Pass the remainder of the command line (everything after -args)
    91  	    to the test binary, uninterpreted and unchanged.
    92  	    Because this flag consumes the remainder of the command line,
    93  	    the package list (if present) must appear before this flag.
    94  
    95  	-c
    96  	    Compile the test binary to pkg.test but do not run it
    97  	    (where pkg is the last element of the package's import path).
    98  	    The file name can be changed with the -o flag.
    99  
   100  	-exec xprog
   101  	    Run the test binary using xprog. The behavior is the same as
   102  	    in 'go run'. See 'go help run' for details.
   103  
   104  	-i
   105  	    Install packages that are dependencies of the test.
   106  	    Do not run the test.
   107  
   108  	-o file
   109  	    Compile the test binary to the named file.
   110  	    The test still runs (unless -c or -i is specified).
   111  
   112  The test binary also accepts flags that control execution of the test; these
   113  flags are also accessible by 'go test'.
   114  `
   115  
   116  // Usage prints the usage message for 'go test -h' and exits.
   117  func Usage() {
   118  	os.Stderr.WriteString(testUsage + "\n\n" +
   119  		strings.TrimSpace(testFlag1) + "\n\n\t" +
   120  		strings.TrimSpace(testFlag2) + "\n")
   121  	os.Exit(2)
   122  }
   123  
   124  var HelpTestflag = &base.Command{
   125  	UsageLine: "testflag",
   126  	Short:     "description of testing flags",
   127  	Long: `
   128  The 'go test' command takes both flags that apply to 'go test' itself
   129  and flags that apply to the resulting test binary.
   130  
   131  Several of the flags control profiling and write an execution profile
   132  suitable for "go tool pprof"; run "go tool pprof -h" for more
   133  information. The --alloc_space, --alloc_objects, and --show_bytes
   134  options of pprof control how the information is presented.
   135  
   136  The following flags are recognized by the 'go test' command and
   137  control the execution of any test:
   138  
   139  	` + strings.TrimSpace(testFlag2) + `
   140  `,
   141  }
   142  
   143  const testFlag2 = `
   144  	-bench regexp
   145  	    Run only those benchmarks matching a regular expression.
   146  	    By default, no benchmarks are run. 
   147  	    To run all benchmarks, use '-bench .' or '-bench=.'.
   148  	    The regular expression is split by unbracketed slash (/)
   149  	    characters into a sequence of regular expressions, and each
   150  	    part of a benchmark's identifier must match the corresponding
   151  	    element in the sequence, if any. Possible parents of matches
   152  	    are run with b.N=1 to identify sub-benchmarks. For example,
   153  	    given -bench=X/Y, top-level benchmarks matching X are run
   154  	    with b.N=1 to find any sub-benchmarks matching Y, which are
   155  	    then run in full.
   156  
   157  	-benchtime t
   158  	    Run enough iterations of each benchmark to take t, specified
   159  	    as a time.Duration (for example, -benchtime 1h30s).
   160  	    The default is 1 second (1s).
   161  
   162  	-count n
   163  	    Run each test and benchmark n times (default 1).
   164  	    If -cpu is set, run n times for each GOMAXPROCS value.
   165  	    Examples are always run once.
   166  
   167  	-cover
   168  	    Enable coverage analysis.
   169  	    Note that because coverage works by annotating the source
   170  	    code before compilation, compilation and test failures with
   171  	    coverage enabled may report line numbers that don't correspond
   172  	    to the original sources.
   173  
   174  	-covermode set,count,atomic
   175  	    Set the mode for coverage analysis for the package[s]
   176  	    being tested. The default is "set" unless -race is enabled,
   177  	    in which case it is "atomic".
   178  	    The values:
   179  		set: bool: does this statement run?
   180  		count: int: how many times does this statement run?
   181  		atomic: int: count, but correct in multithreaded tests;
   182  			significantly more expensive.
   183  	    Sets -cover.
   184  
   185  	-coverpkg pkg1,pkg2,pkg3
   186  	    Apply coverage analysis in each test to the given list of packages.
   187  	    The default is for each test to analyze only the package being tested.
   188  	    Packages are specified as import paths.
   189  	    Sets -cover.
   190  
   191  	-cpu 1,2,4
   192  	    Specify a list of GOMAXPROCS values for which the tests or
   193  	    benchmarks should be executed. The default is the current value
   194  	    of GOMAXPROCS.
   195  
   196  	-list regexp
   197  	    List tests, benchmarks, or examples matching the regular expression.
   198  	    No tests, benchmarks or examples will be run. This will only
   199  	    list top-level tests. No subtest or subbenchmarks will be shown.
   200  
   201  	-parallel n
   202  	    Allow parallel execution of test functions that call t.Parallel.
   203  	    The value of this flag is the maximum number of tests to run
   204  	    simultaneously; by default, it is set to the value of GOMAXPROCS.
   205  	    Note that -parallel only applies within a single test binary.
   206  	    The 'go test' command may run tests for different packages
   207  	    in parallel as well, according to the setting of the -p flag
   208  	    (see 'go help build').
   209  
   210  	-run regexp
   211  	    Run only those tests and examples matching the regular expression.
   212  	    For tests, the regular expression is split by unbracketed slash (/)
   213  	    characters into a sequence of regular expressions, and each part
   214  	    of a test's identifier must match the corresponding element in
   215  	    the sequence, if any. Note that possible parents of matches are
   216  	    run too, so that -run=X/Y matches and runs and reports the result
   217  	    of all tests matching X, even those without sub-tests matching Y,
   218  	    because it must run them to look for those sub-tests.
   219  
   220  	-short
   221  	    Tell long-running tests to shorten their run time.
   222  	    It is off by default but set during all.bash so that installing
   223  	    the Go tree can run a sanity check but not spend time running
   224  	    exhaustive tests.
   225  
   226  	-timeout d
   227  	    If a test binary runs longer than duration d, panic.
   228  	    The default is 10 minutes (10m).
   229  
   230  	-v
   231  	    Verbose output: log all tests as they are run. Also print all
   232  	    text from Log and Logf calls even if the test succeeds.
   233  
   234  The following flags are also recognized by 'go test' and can be used to
   235  profile the tests during execution:
   236  
   237  	-benchmem
   238  	    Print memory allocation statistics for benchmarks.
   239  
   240  	-blockprofile block.out
   241  	    Write a goroutine blocking profile to the specified file
   242  	    when all tests are complete.
   243  	    Writes test binary as -c would.
   244  
   245  	-blockprofilerate n
   246  	    Control the detail provided in goroutine blocking profiles by
   247  	    calling runtime.SetBlockProfileRate with n.
   248  	    See 'go doc runtime.SetBlockProfileRate'.
   249  	    The profiler aims to sample, on average, one blocking event every
   250  	    n nanoseconds the program spends blocked. By default,
   251  	    if -test.blockprofile is set without this flag, all blocking events
   252  	    are recorded, equivalent to -test.blockprofilerate=1.
   253  
   254  	-coverprofile cover.out
   255  	    Write a coverage profile to the file after all tests have passed.
   256  	    Sets -cover.
   257  
   258  	-cpuprofile cpu.out
   259  	    Write a CPU profile to the specified file before exiting.
   260  	    Writes test binary as -c would.
   261  
   262  	-memprofile mem.out
   263  	    Write a memory profile to the file after all tests have passed.
   264  	    Writes test binary as -c would.
   265  
   266  	-memprofilerate n
   267  	    Enable more precise (and expensive) memory profiles by setting
   268  	    runtime.MemProfileRate. See 'go doc runtime.MemProfileRate'.
   269  	    To profile all memory allocations, use -test.memprofilerate=1
   270  	    and pass --alloc_space flag to the pprof tool.
   271  
   272  	-mutexprofile mutex.out
   273  	    Write a mutex contention profile to the specified file
   274  	    when all tests are complete.
   275  	    Writes test binary as -c would.
   276  
   277  	-mutexprofilefraction n
   278  	    Sample 1 in n stack traces of goroutines holding a
   279  	    contended mutex.
   280  
   281  	-outputdir directory
   282  	    Place output files from profiling in the specified directory,
   283  	    by default the directory in which "go test" is running.
   284  
   285  	-trace trace.out
   286  	    Write an execution trace to the specified file before exiting.
   287  
   288  Each of these flags is also recognized with an optional 'test.' prefix,
   289  as in -test.v. When invoking the generated test binary (the result of
   290  'go test -c') directly, however, the prefix is mandatory.
   291  
   292  The 'go test' command rewrites or removes recognized flags,
   293  as appropriate, both before and after the optional package list,
   294  before invoking the test binary.
   295  
   296  For instance, the command
   297  
   298  	go test -v -myflag testdata -cpuprofile=prof.out -x
   299  
   300  will compile the test binary and then run it as
   301  
   302  	pkg.test -test.v -myflag testdata -test.cpuprofile=prof.out
   303  
   304  (The -x flag is removed because it applies only to the go command's
   305  execution, not to the test itself.)
   306  
   307  The test flags that generate profiles (other than for coverage) also
   308  leave the test binary in pkg.test for use when analyzing the profiles.
   309  
   310  When 'go test' runs a test binary, it does so from within the
   311  corresponding package's source code directory. Depending on the test,
   312  it may be necessary to do the same when invoking a generated test
   313  binary directly.
   314  
   315  The command-line package list, if present, must appear before any
   316  flag not known to the go test command. Continuing the example above,
   317  the package list would have to appear before -myflag, but could appear
   318  on either side of -v.
   319  
   320  To keep an argument for a test binary from being interpreted as a
   321  known flag or a package name, use -args (see 'go help test') which
   322  passes the remainder of the command line through to the test binary
   323  uninterpreted and unaltered.
   324  
   325  For instance, the command
   326  
   327  	go test -v -args -x -v
   328  
   329  will compile the test binary and then run it as
   330  
   331  	pkg.test -test.v -x -v
   332  
   333  Similarly,
   334  
   335  	go test -args math
   336  
   337  will compile the test binary and then run it as
   338  
   339  	pkg.test math
   340  
   341  In the first example, the -x and the second -v are passed through to the
   342  test binary unchanged and with no effect on the go command itself.
   343  In the second example, the argument math is passed through to the test
   344  binary, instead of being interpreted as the package list.
   345  `
   346  
   347  var HelpTestfunc = &base.Command{
   348  	UsageLine: "testfunc",
   349  	Short:     "description of testing functions",
   350  	Long: `
   351  The 'go test' command expects to find test, benchmark, and example functions
   352  in the "*_test.go" files corresponding to the package under test.
   353  
   354  A test function is one named TestXXX (where XXX is any alphanumeric string
   355  not starting with a lower case letter) and should have the signature,
   356  
   357  	func TestXXX(t *testing.T) { ... }
   358  
   359  A benchmark function is one named BenchmarkXXX and should have the signature,
   360  
   361  	func BenchmarkXXX(b *testing.B) { ... }
   362  
   363  An example function is similar to a test function but, instead of using
   364  *testing.T to report success or failure, prints output to os.Stdout.
   365  If the last comment in the function starts with "Output:" then the output
   366  is compared exactly against the comment (see examples below). If the last
   367  comment begins with "Unordered output:" then the output is compared to the
   368  comment, however the order of the lines is ignored. An example with no such
   369  comment is compiled but not executed. An example with no text after
   370  "Output:" is compiled, executed, and expected to produce no output.
   371  
   372  Godoc displays the body of ExampleXXX to demonstrate the use
   373  of the function, constant, or variable XXX. An example of a method M with
   374  receiver type T or *T is named ExampleT_M. There may be multiple examples
   375  for a given function, constant, or variable, distinguished by a trailing _xxx,
   376  where xxx is a suffix not beginning with an upper case letter.
   377  
   378  Here is an example of an example:
   379  
   380  	func ExamplePrintln() {
   381  		Println("The output of\nthis example.")
   382  		// Output: The output of
   383  		// this example.
   384  	}
   385  
   386  Here is another example where the ordering of the output is ignored:
   387  
   388  	func ExamplePerm() {
   389  		for _, value := range Perm(4) {
   390  			fmt.Println(value)
   391  		}
   392  
   393  		// Unordered output: 4
   394  		// 2
   395  		// 1
   396  		// 3
   397  		// 0
   398  	}
   399  
   400  The entire test file is presented as the example when it contains a single
   401  example function, at least one other function, type, variable, or constant
   402  declaration, and no test or benchmark functions.
   403  
   404  See the documentation of the testing package for more information.
   405  `,
   406  }
   407  
   408  var (
   409  	testC            bool            // -c flag
   410  	testCover        bool            // -cover flag
   411  	testCoverMode    string          // -covermode flag
   412  	testCoverPaths   []string        // -coverpkg flag
   413  	testCoverPkgs    []*load.Package // -coverpkg flag
   414  	testO            string          // -o flag
   415  	testProfile      bool            // some profiling flag
   416  	testNeedBinary   bool            // profile needs to keep binary around
   417  	testV            bool            // -v flag
   418  	testTimeout      string          // -timeout flag
   419  	testArgs         []string
   420  	testBench        bool
   421  	testList         bool
   422  	testStreamOutput bool // show output as it is generated
   423  	testShowPass     bool // show passing output
   424  
   425  	testKillTimeout = 10 * time.Minute
   426  )
   427  
   428  var testMainDeps = map[string]bool{
   429  	// Dependencies for testmain.
   430  	"testing":                   true,
   431  	"testing/internal/testdeps": true,
   432  	"os":                        true,
   433  }
   434  
   435  func runTest(cmd *base.Command, args []string) {
   436  	var pkgArgs []string
   437  	pkgArgs, testArgs = testFlags(args)
   438  
   439  	work.FindExecCmd() // initialize cached result
   440  
   441  	work.InstrumentInit()
   442  	work.BuildModeInit()
   443  	pkgs := load.PackagesForBuild(pkgArgs)
   444  	if len(pkgs) == 0 {
   445  		base.Fatalf("no packages to test")
   446  	}
   447  
   448  	if testC && len(pkgs) != 1 {
   449  		base.Fatalf("cannot use -c flag with multiple packages")
   450  	}
   451  	if testO != "" && len(pkgs) != 1 {
   452  		base.Fatalf("cannot use -o flag with multiple packages")
   453  	}
   454  	if testProfile && len(pkgs) != 1 {
   455  		base.Fatalf("cannot use test profile flag with multiple packages")
   456  	}
   457  
   458  	// If a test timeout was given and is parseable, set our kill timeout
   459  	// to that timeout plus one minute. This is a backup alarm in case
   460  	// the test wedges with a goroutine spinning and its background
   461  	// timer does not get a chance to fire.
   462  	if dt, err := time.ParseDuration(testTimeout); err == nil && dt > 0 {
   463  		testKillTimeout = dt + 1*time.Minute
   464  	}
   465  
   466  	// show passing test output (after buffering) with -v flag.
   467  	// must buffer because tests are running in parallel, and
   468  	// otherwise the output will get mixed.
   469  	testShowPass = testV || testList
   470  
   471  	// stream test output (no buffering) when no package has
   472  	// been given on the command line (implicit current directory)
   473  	// or when benchmarking.
   474  	// Also stream if we're showing output anyway with a
   475  	// single package under test or if parallelism is set to 1.
   476  	// In these cases, streaming the output produces the same result
   477  	// as not streaming, just more immediately.
   478  	testStreamOutput = len(pkgArgs) == 0 || testBench ||
   479  		(testShowPass && (len(pkgs) == 1 || cfg.BuildP == 1))
   480  
   481  	// For 'go test -i -o x.test', we want to build x.test. Imply -c to make the logic easier.
   482  	if cfg.BuildI && testO != "" {
   483  		testC = true
   484  	}
   485  
   486  	var b work.Builder
   487  	b.Init()
   488  
   489  	if cfg.BuildI {
   490  		cfg.BuildV = testV
   491  
   492  		deps := make(map[string]bool)
   493  		for dep := range testMainDeps {
   494  			deps[dep] = true
   495  		}
   496  
   497  		for _, p := range pkgs {
   498  			// Dependencies for each test.
   499  			for _, path := range p.Imports {
   500  				deps[path] = true
   501  			}
   502  			for _, path := range p.Resolve(p.TestImports) {
   503  				deps[path] = true
   504  			}
   505  			for _, path := range p.Resolve(p.XTestImports) {
   506  				deps[path] = true
   507  			}
   508  		}
   509  
   510  		// translate C to runtime/cgo
   511  		if deps["C"] {
   512  			delete(deps, "C")
   513  			deps["runtime/cgo"] = true
   514  			if cfg.Goos == runtime.GOOS && cfg.Goarch == runtime.GOARCH && !cfg.BuildRace && !cfg.BuildMSan {
   515  				deps["cmd/cgo"] = true
   516  			}
   517  		}
   518  		// Ignore pseudo-packages.
   519  		delete(deps, "unsafe")
   520  
   521  		all := []string{}
   522  		for path := range deps {
   523  			if !build.IsLocalImport(path) {
   524  				all = append(all, path)
   525  			}
   526  		}
   527  		sort.Strings(all)
   528  
   529  		a := &work.Action{}
   530  		for _, p := range load.PackagesForBuild(all) {
   531  			a.Deps = append(a.Deps, b.Action(work.ModeInstall, work.ModeInstall, p))
   532  		}
   533  		b.Do(a)
   534  		if !testC || a.Failed {
   535  			return
   536  		}
   537  		b.Init()
   538  	}
   539  
   540  	var builds, runs, prints []*work.Action
   541  
   542  	if testCoverPaths != nil {
   543  		// Load packages that were asked about for coverage.
   544  		// packagesForBuild exits if the packages cannot be loaded.
   545  		testCoverPkgs = load.PackagesForBuild(testCoverPaths)
   546  
   547  		// Warn about -coverpkg arguments that are not actually used.
   548  		used := make(map[string]bool)
   549  		for _, p := range pkgs {
   550  			used[p.ImportPath] = true
   551  			for _, dep := range p.Deps {
   552  				used[dep] = true
   553  			}
   554  		}
   555  		for _, p := range testCoverPkgs {
   556  			if !used[p.ImportPath] {
   557  				fmt.Fprintf(os.Stderr, "warning: no packages being tested depend on %s\n", p.ImportPath)
   558  			}
   559  		}
   560  
   561  		// Mark all the coverage packages for rebuilding with coverage.
   562  		for _, p := range testCoverPkgs {
   563  			// There is nothing to cover in package unsafe; it comes from the compiler.
   564  			if p.ImportPath == "unsafe" {
   565  				continue
   566  			}
   567  			p.Stale = true // rebuild
   568  			p.StaleReason = "rebuild for coverage"
   569  			p.Internal.Fake = true // do not warn about rebuild
   570  			p.Internal.CoverMode = testCoverMode
   571  			var coverFiles []string
   572  			coverFiles = append(coverFiles, p.GoFiles...)
   573  			coverFiles = append(coverFiles, p.CgoFiles...)
   574  			coverFiles = append(coverFiles, p.TestGoFiles...)
   575  			p.Internal.CoverVars = declareCoverVars(p.ImportPath, coverFiles...)
   576  		}
   577  	}
   578  
   579  	// Prepare build + run + print actions for all packages being tested.
   580  	for _, p := range pkgs {
   581  		// sync/atomic import is inserted by the cover tool. See #18486
   582  		if testCover && testCoverMode == "atomic" {
   583  			ensureImport(p, "sync/atomic")
   584  		}
   585  
   586  		buildTest, runTest, printTest, err := builderTest(&b, p)
   587  		if err != nil {
   588  			str := err.Error()
   589  			if strings.HasPrefix(str, "\n") {
   590  				str = str[1:]
   591  			}
   592  			failed := fmt.Sprintf("FAIL\t%s [setup failed]\n", p.ImportPath)
   593  
   594  			if p.ImportPath != "" {
   595  				base.Errorf("# %s\n%s\n%s", p.ImportPath, str, failed)
   596  			} else {
   597  				base.Errorf("%s\n%s", str, failed)
   598  			}
   599  			continue
   600  		}
   601  		builds = append(builds, buildTest)
   602  		runs = append(runs, runTest)
   603  		prints = append(prints, printTest)
   604  	}
   605  
   606  	// Ultimately the goal is to print the output.
   607  	root := &work.Action{Deps: prints}
   608  
   609  	// Force the printing of results to happen in order,
   610  	// one at a time.
   611  	for i, a := range prints {
   612  		if i > 0 {
   613  			a.Deps = append(a.Deps, prints[i-1])
   614  		}
   615  	}
   616  
   617  	// Force benchmarks to run in serial.
   618  	if !testC && testBench {
   619  		// The first run must wait for all builds.
   620  		// Later runs must wait for the previous run's print.
   621  		for i, run := range runs {
   622  			if i == 0 {
   623  				run.Deps = append(run.Deps, builds...)
   624  			} else {
   625  				run.Deps = append(run.Deps, prints[i-1])
   626  			}
   627  		}
   628  	}
   629  
   630  	// If we are building any out-of-date packages other
   631  	// than those under test, warn.
   632  	okBuild := map[*load.Package]bool{}
   633  	for _, p := range pkgs {
   634  		okBuild[p] = true
   635  	}
   636  	warned := false
   637  	for _, a := range work.ActionList(root) {
   638  		if a.Package == nil || okBuild[a.Package] {
   639  			continue
   640  		}
   641  		okBuild[a.Package] = true // warn at most once
   642  
   643  		// Don't warn about packages being rebuilt because of
   644  		// things like coverage analysis.
   645  		for _, p1 := range a.Package.Internal.Imports {
   646  			if p1.Internal.Fake {
   647  				a.Package.Internal.Fake = true
   648  			}
   649  		}
   650  
   651  		if a.Func != nil && !okBuild[a.Package] && !a.Package.Internal.Fake && !a.Package.Internal.Local {
   652  			if !warned {
   653  				fmt.Fprintf(os.Stderr, "warning: building out-of-date packages:\n")
   654  				warned = true
   655  			}
   656  			fmt.Fprintf(os.Stderr, "\t%s\n", a.Package.ImportPath)
   657  		}
   658  	}
   659  	if warned {
   660  		args := strings.Join(pkgArgs, " ")
   661  		if args != "" {
   662  			args = " " + args
   663  		}
   664  		extraOpts := ""
   665  		if cfg.BuildRace {
   666  			extraOpts = "-race "
   667  		}
   668  		if cfg.BuildMSan {
   669  			extraOpts = "-msan "
   670  		}
   671  		fmt.Fprintf(os.Stderr, "installing these packages with 'go test %s-i%s' will speed future tests.\n\n", extraOpts, args)
   672  	}
   673  
   674  	b.Do(root)
   675  }
   676  
   677  // ensures that package p imports the named package
   678  func ensureImport(p *load.Package, pkg string) {
   679  	for _, d := range p.Internal.Deps {
   680  		if d.Name == pkg {
   681  			return
   682  		}
   683  	}
   684  
   685  	a := load.LoadPackage(pkg, &load.ImportStack{})
   686  	if a.Error != nil {
   687  		base.Fatalf("load %s: %v", pkg, a.Error)
   688  	}
   689  	load.ComputeStale(a)
   690  
   691  	p.Internal.Imports = append(p.Internal.Imports, a)
   692  }
   693  
   694  var windowsBadWords = []string{
   695  	"install",
   696  	"patch",
   697  	"setup",
   698  	"update",
   699  }
   700  
   701  func builderTest(b *work.Builder, p *load.Package) (buildAction, runAction, printAction *work.Action, err error) {
   702  	if len(p.TestGoFiles)+len(p.XTestGoFiles) == 0 {
   703  		build := b.Action(work.ModeBuild, work.ModeBuild, p)
   704  		run := &work.Action{Package: p, Deps: []*work.Action{build}}
   705  		print := &work.Action{Func: builderNoTest, Package: p, Deps: []*work.Action{run}}
   706  		return build, run, print, nil
   707  	}
   708  
   709  	// Build Package structs describing:
   710  	//	ptest - package + test files
   711  	//	pxtest - package of external test files
   712  	//	pmain - pkg.test binary
   713  	var ptest, pxtest, pmain *load.Package
   714  
   715  	var imports, ximports []*load.Package
   716  	var stk load.ImportStack
   717  	stk.Push(p.ImportPath + " (test)")
   718  	rawTestImports := str.StringList(p.TestImports)
   719  	for i, path := range p.TestImports {
   720  		p1 := load.LoadImport(path, p.Dir, p, &stk, p.Internal.Build.TestImportPos[path], load.ResolveImport)
   721  		if p1.Error != nil {
   722  			return nil, nil, nil, p1.Error
   723  		}
   724  		if len(p1.DepsErrors) > 0 {
   725  			err := p1.DepsErrors[0]
   726  			err.Pos = "" // show full import stack
   727  			return nil, nil, nil, err
   728  		}
   729  		if str.Contains(p1.Deps, p.ImportPath) || p1.ImportPath == p.ImportPath {
   730  			// Same error that loadPackage returns (via reusePackage) in pkg.go.
   731  			// Can't change that code, because that code is only for loading the
   732  			// non-test copy of a package.
   733  			err := &load.PackageError{
   734  				ImportStack:   testImportStack(stk[0], p1, p.ImportPath),
   735  				Err:           "import cycle not allowed in test",
   736  				IsImportCycle: true,
   737  			}
   738  			return nil, nil, nil, err
   739  		}
   740  		p.TestImports[i] = p1.ImportPath
   741  		imports = append(imports, p1)
   742  	}
   743  	stk.Pop()
   744  	stk.Push(p.ImportPath + "_test")
   745  	pxtestNeedsPtest := false
   746  	rawXTestImports := str.StringList(p.XTestImports)
   747  	for i, path := range p.XTestImports {
   748  		p1 := load.LoadImport(path, p.Dir, p, &stk, p.Internal.Build.XTestImportPos[path], load.ResolveImport)
   749  		if p1.Error != nil {
   750  			return nil, nil, nil, p1.Error
   751  		}
   752  		if len(p1.DepsErrors) > 0 {
   753  			err := p1.DepsErrors[0]
   754  			err.Pos = "" // show full import stack
   755  			return nil, nil, nil, err
   756  		}
   757  		if p1.ImportPath == p.ImportPath {
   758  			pxtestNeedsPtest = true
   759  		} else {
   760  			ximports = append(ximports, p1)
   761  		}
   762  		p.XTestImports[i] = p1.ImportPath
   763  	}
   764  	stk.Pop()
   765  
   766  	// Use last element of import path, not package name.
   767  	// They differ when package name is "main".
   768  	// But if the import path is "command-line-arguments",
   769  	// like it is during 'go run', use the package name.
   770  	var elem string
   771  	if p.ImportPath == "command-line-arguments" {
   772  		elem = p.Name
   773  	} else {
   774  		_, elem = path.Split(p.ImportPath)
   775  	}
   776  	testBinary := elem + ".test"
   777  
   778  	// The ptest package needs to be importable under the
   779  	// same import path that p has, but we cannot put it in
   780  	// the usual place in the temporary tree, because then
   781  	// other tests will see it as the real package.
   782  	// Instead we make a _test directory under the import path
   783  	// and then repeat the import path there. We tell the
   784  	// compiler and linker to look in that _test directory first.
   785  	//
   786  	// That is, if the package under test is unicode/utf8,
   787  	// then the normal place to write the package archive is
   788  	// $WORK/unicode/utf8.a, but we write the test package archive to
   789  	// $WORK/unicode/utf8/_test/unicode/utf8.a.
   790  	// We write the external test package archive to
   791  	// $WORK/unicode/utf8/_test/unicode/utf8_test.a.
   792  	testDir := filepath.Join(b.WorkDir, filepath.FromSlash(p.ImportPath+"/_test"))
   793  	ptestObj := work.BuildToolchain.Pkgpath(testDir, p)
   794  
   795  	// Create the directory for the .a files.
   796  	ptestDir, _ := filepath.Split(ptestObj)
   797  	if err := b.Mkdir(ptestDir); err != nil {
   798  		return nil, nil, nil, err
   799  	}
   800  
   801  	// Should we apply coverage analysis locally,
   802  	// only for this package and only for this test?
   803  	// Yes, if -cover is on but -coverpkg has not specified
   804  	// a list of packages for global coverage.
   805  	localCover := testCover && testCoverPaths == nil
   806  
   807  	// Test package.
   808  	if len(p.TestGoFiles) > 0 || localCover || p.Name == "main" {
   809  		ptest = new(load.Package)
   810  		*ptest = *p
   811  		ptest.GoFiles = nil
   812  		ptest.GoFiles = append(ptest.GoFiles, p.GoFiles...)
   813  		ptest.GoFiles = append(ptest.GoFiles, p.TestGoFiles...)
   814  		ptest.Internal.Target = ""
   815  		// Note: The preparation of the compiler import map requires that common
   816  		// indexes in ptest.Imports, ptest.Internal.Imports, and ptest.Internal.RawImports
   817  		// all line up (but RawImports can be shorter than the others).
   818  		// That is, for 0 ≤ i < len(RawImports),
   819  		// RawImports[i] is the import string in the program text,
   820  		// Imports[i] is the expanded import string (vendoring applied or relative path expanded away),
   821  		// and Internal.Imports[i] is the corresponding *Package.
   822  		// Any implicitly added imports appear in Imports and Internal.Imports
   823  		// but not RawImports (because they were not in the source code).
   824  		// We insert TestImports, imports, and rawTestImports at the start of
   825  		// these lists to preserve the alignment.
   826  		ptest.Imports = str.StringList(p.TestImports, p.Imports)
   827  		ptest.Internal.Imports = append(imports, p.Internal.Imports...)
   828  		ptest.Internal.RawImports = str.StringList(rawTestImports, p.Internal.RawImports)
   829  		ptest.Internal.Pkgdir = testDir
   830  		ptest.Internal.Fake = true
   831  		ptest.Internal.ForceLibrary = true
   832  		ptest.Stale = true
   833  		ptest.StaleReason = "rebuild for test"
   834  		ptest.Internal.Build = new(build.Package)
   835  		*ptest.Internal.Build = *p.Internal.Build
   836  		m := map[string][]token.Position{}
   837  		for k, v := range p.Internal.Build.ImportPos {
   838  			m[k] = append(m[k], v...)
   839  		}
   840  		for k, v := range p.Internal.Build.TestImportPos {
   841  			m[k] = append(m[k], v...)
   842  		}
   843  		ptest.Internal.Build.ImportPos = m
   844  
   845  		if localCover {
   846  			ptest.Internal.CoverMode = testCoverMode
   847  			var coverFiles []string
   848  			coverFiles = append(coverFiles, ptest.GoFiles...)
   849  			coverFiles = append(coverFiles, ptest.CgoFiles...)
   850  			ptest.Internal.CoverVars = declareCoverVars(ptest.ImportPath, coverFiles...)
   851  		}
   852  	} else {
   853  		ptest = p
   854  	}
   855  
   856  	// External test package.
   857  	if len(p.XTestGoFiles) > 0 {
   858  		pxtest = &load.Package{
   859  			PackagePublic: load.PackagePublic{
   860  				Name:       p.Name + "_test",
   861  				ImportPath: p.ImportPath + "_test",
   862  				Root:       p.Root,
   863  				Dir:        p.Dir,
   864  				GoFiles:    p.XTestGoFiles,
   865  				Imports:    p.XTestImports,
   866  				Stale:      true,
   867  			},
   868  			Internal: load.PackageInternal{
   869  				LocalPrefix: p.Internal.LocalPrefix,
   870  				Build: &build.Package{
   871  					ImportPos: p.Internal.Build.XTestImportPos,
   872  				},
   873  				Imports:    ximports,
   874  				RawImports: rawXTestImports,
   875  				Pkgdir:     testDir,
   876  				Fake:       true,
   877  				External:   true,
   878  			},
   879  		}
   880  		if pxtestNeedsPtest {
   881  			pxtest.Internal.Imports = append(pxtest.Internal.Imports, ptest)
   882  		}
   883  	}
   884  
   885  	// Action for building pkg.test.
   886  	pmain = &load.Package{
   887  		PackagePublic: load.PackagePublic{
   888  			Name:       "main",
   889  			Dir:        testDir,
   890  			GoFiles:    []string{"_testmain.go"},
   891  			ImportPath: "testmain",
   892  			Root:       p.Root,
   893  			Stale:      true,
   894  		},
   895  		Internal: load.PackageInternal{
   896  			Build:     &build.Package{Name: "main"},
   897  			Pkgdir:    testDir,
   898  			Fake:      true,
   899  			OmitDebug: !testC && !testNeedBinary,
   900  		},
   901  	}
   902  
   903  	// The generated main also imports testing, regexp, and os.
   904  	stk.Push("testmain")
   905  	for dep := range testMainDeps {
   906  		if dep == ptest.ImportPath {
   907  			pmain.Internal.Imports = append(pmain.Internal.Imports, ptest)
   908  		} else {
   909  			p1 := load.LoadImport(dep, "", nil, &stk, nil, 0)
   910  			if p1.Error != nil {
   911  				return nil, nil, nil, p1.Error
   912  			}
   913  			pmain.Internal.Imports = append(pmain.Internal.Imports, p1)
   914  		}
   915  	}
   916  
   917  	if testCoverPkgs != nil {
   918  		// Add imports, but avoid duplicates.
   919  		seen := map[*load.Package]bool{p: true, ptest: true}
   920  		for _, p1 := range pmain.Internal.Imports {
   921  			seen[p1] = true
   922  		}
   923  		for _, p1 := range testCoverPkgs {
   924  			if !seen[p1] {
   925  				seen[p1] = true
   926  				pmain.Internal.Imports = append(pmain.Internal.Imports, p1)
   927  			}
   928  		}
   929  	}
   930  
   931  	// Do initial scan for metadata needed for writing _testmain.go
   932  	// Use that metadata to update the list of imports for package main.
   933  	// The list of imports is used by recompileForTest and by the loop
   934  	// afterward that gathers t.Cover information.
   935  	t, err := loadTestFuncs(ptest)
   936  	if err != nil {
   937  		return nil, nil, nil, err
   938  	}
   939  	if len(ptest.GoFiles)+len(ptest.CgoFiles) > 0 {
   940  		pmain.Internal.Imports = append(pmain.Internal.Imports, ptest)
   941  		t.ImportTest = true
   942  	}
   943  	if pxtest != nil {
   944  		pmain.Internal.Imports = append(pmain.Internal.Imports, pxtest)
   945  		t.ImportXtest = true
   946  	}
   947  
   948  	if ptest != p && localCover {
   949  		// We have made modifications to the package p being tested
   950  		// and are rebuilding p (as ptest), writing it to the testDir tree.
   951  		// Arrange to rebuild, writing to that same tree, all packages q
   952  		// such that the test depends on q, and q depends on p.
   953  		// This makes sure that q sees the modifications to p.
   954  		// Strictly speaking, the rebuild is only necessary if the
   955  		// modifications to p change its export metadata, but
   956  		// determining that is a bit tricky, so we rebuild always.
   957  		//
   958  		// This will cause extra compilation, so for now we only do it
   959  		// when testCover is set. The conditions are more general, though,
   960  		// and we may find that we need to do it always in the future.
   961  		recompileForTest(pmain, p, ptest, testDir)
   962  	}
   963  
   964  	if cfg.BuildContext.GOOS == "darwin" {
   965  		if cfg.BuildContext.GOARCH == "arm" || cfg.BuildContext.GOARCH == "arm64" {
   966  			t.NeedCgo = true
   967  		}
   968  	}
   969  
   970  	for _, cp := range pmain.Internal.Imports {
   971  		if len(cp.Internal.CoverVars) > 0 {
   972  			t.Cover = append(t.Cover, coverInfo{cp, cp.Internal.CoverVars})
   973  		}
   974  	}
   975  
   976  	if !cfg.BuildN {
   977  		// writeTestmain writes _testmain.go. This must happen after recompileForTest,
   978  		// because recompileForTest modifies XXX.
   979  		if err := writeTestmain(filepath.Join(testDir, "_testmain.go"), t); err != nil {
   980  			return nil, nil, nil, err
   981  		}
   982  	}
   983  
   984  	load.ComputeStale(pmain)
   985  
   986  	if ptest != p {
   987  		a := b.Action(work.ModeBuild, work.ModeBuild, ptest)
   988  		a.Objdir = testDir + string(filepath.Separator) + "_obj_test" + string(filepath.Separator)
   989  		a.Objpkg = ptestObj
   990  		a.Target = ptestObj
   991  		a.Link = false
   992  	}
   993  
   994  	if pxtest != nil {
   995  		a := b.Action(work.ModeBuild, work.ModeBuild, pxtest)
   996  		a.Objdir = testDir + string(filepath.Separator) + "_obj_xtest" + string(filepath.Separator)
   997  		a.Objpkg = work.BuildToolchain.Pkgpath(testDir, pxtest)
   998  		a.Target = a.Objpkg
   999  	}
  1000  
  1001  	a := b.Action(work.ModeBuild, work.ModeBuild, pmain)
  1002  	a.Objdir = testDir + string(filepath.Separator)
  1003  	a.Objpkg = filepath.Join(testDir, "main.a")
  1004  	a.Target = filepath.Join(testDir, testBinary) + cfg.ExeSuffix
  1005  	if cfg.Goos == "windows" {
  1006  		// There are many reserved words on Windows that,
  1007  		// if used in the name of an executable, cause Windows
  1008  		// to try to ask for extra permissions.
  1009  		// The word list includes setup, install, update, and patch,
  1010  		// but it does not appear to be defined anywhere.
  1011  		// We have run into this trying to run the
  1012  		// go.codereview/patch tests.
  1013  		// For package names containing those words, use test.test.exe
  1014  		// instead of pkgname.test.exe.
  1015  		// Note that this file name is only used in the Go command's
  1016  		// temporary directory. If the -c or other flags are
  1017  		// given, the code below will still use pkgname.test.exe.
  1018  		// There are two user-visible effects of this change.
  1019  		// First, you can actually run 'go test' in directories that
  1020  		// have names that Windows thinks are installer-like,
  1021  		// without getting a dialog box asking for more permissions.
  1022  		// Second, in the Windows process listing during go test,
  1023  		// the test shows up as test.test.exe, not pkgname.test.exe.
  1024  		// That second one is a drawback, but it seems a small
  1025  		// price to pay for the test running at all.
  1026  		// If maintaining the list of bad words is too onerous,
  1027  		// we could just do this always on Windows.
  1028  		for _, bad := range windowsBadWords {
  1029  			if strings.Contains(testBinary, bad) {
  1030  				a.Target = filepath.Join(testDir, "test.test") + cfg.ExeSuffix
  1031  				break
  1032  			}
  1033  		}
  1034  	}
  1035  	buildAction = a
  1036  
  1037  	if testC || testNeedBinary {
  1038  		// -c or profiling flag: create action to copy binary to ./test.out.
  1039  		target := filepath.Join(base.Cwd, testBinary+cfg.ExeSuffix)
  1040  		if testO != "" {
  1041  			target = testO
  1042  			if !filepath.IsAbs(target) {
  1043  				target = filepath.Join(base.Cwd, target)
  1044  			}
  1045  		}
  1046  		buildAction = &work.Action{
  1047  			Func:    work.BuildInstallFunc,
  1048  			Deps:    []*work.Action{buildAction},
  1049  			Package: pmain,
  1050  			Target:  target,
  1051  		}
  1052  		runAction = buildAction // make sure runAction != nil even if not running test
  1053  	}
  1054  	if testC {
  1055  		printAction = &work.Action{Package: p, Deps: []*work.Action{runAction}} // nop
  1056  	} else {
  1057  		// run test
  1058  		runAction = &work.Action{
  1059  			Func:       builderRunTest,
  1060  			Deps:       []*work.Action{buildAction},
  1061  			Package:    p,
  1062  			IgnoreFail: true,
  1063  		}
  1064  		cleanAction := &work.Action{
  1065  			Func:    builderCleanTest,
  1066  			Deps:    []*work.Action{runAction},
  1067  			Package: p,
  1068  		}
  1069  		printAction = &work.Action{
  1070  			Func:    builderPrintTest,
  1071  			Deps:    []*work.Action{cleanAction},
  1072  			Package: p,
  1073  		}
  1074  	}
  1075  
  1076  	return buildAction, runAction, printAction, nil
  1077  }
  1078  
  1079  func testImportStack(top string, p *load.Package, target string) []string {
  1080  	stk := []string{top, p.ImportPath}
  1081  Search:
  1082  	for p.ImportPath != target {
  1083  		for _, p1 := range p.Internal.Imports {
  1084  			if p1.ImportPath == target || str.Contains(p1.Deps, target) {
  1085  				stk = append(stk, p1.ImportPath)
  1086  				p = p1
  1087  				continue Search
  1088  			}
  1089  		}
  1090  		// Can't happen, but in case it does...
  1091  		stk = append(stk, "<lost path to cycle>")
  1092  		break
  1093  	}
  1094  	return stk
  1095  }
  1096  
  1097  func recompileForTest(pmain, preal, ptest *load.Package, testDir string) {
  1098  	// The "test copy" of preal is ptest.
  1099  	// For each package that depends on preal, make a "test copy"
  1100  	// that depends on ptest. And so on, up the dependency tree.
  1101  	testCopy := map[*load.Package]*load.Package{preal: ptest}
  1102  	for _, p := range load.PackageList([]*load.Package{pmain}) {
  1103  		// Copy on write.
  1104  		didSplit := false
  1105  		split := func() {
  1106  			if didSplit {
  1107  				return
  1108  			}
  1109  			didSplit = true
  1110  			if p.Internal.Pkgdir != testDir {
  1111  				p1 := new(load.Package)
  1112  				testCopy[p] = p1
  1113  				*p1 = *p
  1114  				p1.Internal.Imports = make([]*load.Package, len(p.Internal.Imports))
  1115  				copy(p1.Internal.Imports, p.Internal.Imports)
  1116  				p = p1
  1117  				p.Internal.Pkgdir = testDir
  1118  				p.Internal.Target = ""
  1119  				p.Internal.Fake = true
  1120  				p.Stale = true
  1121  				p.StaleReason = "depends on package being tested"
  1122  			}
  1123  		}
  1124  
  1125  		// Update p.Deps and p.Internal.Imports to use at test copies.
  1126  		for i, dep := range p.Internal.Deps {
  1127  			if p1 := testCopy[dep]; p1 != nil && p1 != dep {
  1128  				split()
  1129  				p.Internal.Deps[i] = p1
  1130  			}
  1131  		}
  1132  		for i, imp := range p.Internal.Imports {
  1133  			if p1 := testCopy[imp]; p1 != nil && p1 != imp {
  1134  				split()
  1135  				p.Internal.Imports[i] = p1
  1136  			}
  1137  		}
  1138  	}
  1139  }
  1140  
  1141  var coverIndex = 0
  1142  
  1143  // isTestFile reports whether the source file is a set of tests and should therefore
  1144  // be excluded from coverage analysis.
  1145  func isTestFile(file string) bool {
  1146  	// We don't cover tests, only the code they test.
  1147  	return strings.HasSuffix(file, "_test.go")
  1148  }
  1149  
  1150  // declareCoverVars attaches the required cover variables names
  1151  // to the files, to be used when annotating the files.
  1152  func declareCoverVars(importPath string, files ...string) map[string]*load.CoverVar {
  1153  	coverVars := make(map[string]*load.CoverVar)
  1154  	for _, file := range files {
  1155  		if isTestFile(file) {
  1156  			continue
  1157  		}
  1158  		coverVars[file] = &load.CoverVar{
  1159  			File: filepath.Join(importPath, file),
  1160  			Var:  fmt.Sprintf("GoCover_%d", coverIndex),
  1161  		}
  1162  		coverIndex++
  1163  	}
  1164  	return coverVars
  1165  }
  1166  
  1167  var noTestsToRun = []byte("\ntesting: warning: no tests to run\n")
  1168  
  1169  // builderRunTest is the action for running a test binary.
  1170  func builderRunTest(b *work.Builder, a *work.Action) error {
  1171  	args := str.StringList(work.FindExecCmd(), a.Deps[0].Target, testArgs)
  1172  	a.TestOutput = new(bytes.Buffer)
  1173  
  1174  	if cfg.BuildN || cfg.BuildX {
  1175  		b.Showcmd("", "%s", strings.Join(args, " "))
  1176  		if cfg.BuildN {
  1177  			return nil
  1178  		}
  1179  	}
  1180  
  1181  	if a.Failed {
  1182  		// We were unable to build the binary.
  1183  		a.Failed = false
  1184  		fmt.Fprintf(a.TestOutput, "FAIL\t%s [build failed]\n", a.Package.ImportPath)
  1185  		base.SetExitStatus(1)
  1186  		return nil
  1187  	}
  1188  
  1189  	cmd := exec.Command(args[0], args[1:]...)
  1190  	cmd.Dir = a.Package.Dir
  1191  	cmd.Env = base.EnvForDir(cmd.Dir, cfg.OrigEnv)
  1192  	var buf bytes.Buffer
  1193  	if testStreamOutput {
  1194  		cmd.Stdout = os.Stdout
  1195  		cmd.Stderr = os.Stderr
  1196  	} else {
  1197  		cmd.Stdout = &buf
  1198  		cmd.Stderr = &buf
  1199  	}
  1200  
  1201  	// If there are any local SWIG dependencies, we want to load
  1202  	// the shared library from the build directory.
  1203  	if a.Package.UsesSwig() {
  1204  		env := cmd.Env
  1205  		found := false
  1206  		prefix := "LD_LIBRARY_PATH="
  1207  		for i, v := range env {
  1208  			if strings.HasPrefix(v, prefix) {
  1209  				env[i] = v + ":."
  1210  				found = true
  1211  				break
  1212  			}
  1213  		}
  1214  		if !found {
  1215  			env = append(env, "LD_LIBRARY_PATH=.")
  1216  		}
  1217  		cmd.Env = env
  1218  	}
  1219  
  1220  	t0 := time.Now()
  1221  	err := cmd.Start()
  1222  
  1223  	// This is a last-ditch deadline to detect and
  1224  	// stop wedged test binaries, to keep the builders
  1225  	// running.
  1226  	if err == nil {
  1227  		tick := time.NewTimer(testKillTimeout)
  1228  		base.StartSigHandlers()
  1229  		done := make(chan error)
  1230  		go func() {
  1231  			done <- cmd.Wait()
  1232  		}()
  1233  	Outer:
  1234  		select {
  1235  		case err = <-done:
  1236  			// ok
  1237  		case <-tick.C:
  1238  			if base.SignalTrace != nil {
  1239  				// Send a quit signal in the hope that the program will print
  1240  				// a stack trace and exit. Give it five seconds before resorting
  1241  				// to Kill.
  1242  				cmd.Process.Signal(base.SignalTrace)
  1243  				select {
  1244  				case err = <-done:
  1245  					fmt.Fprintf(&buf, "*** Test killed with %v: ran too long (%v).\n", base.SignalTrace, testKillTimeout)
  1246  					break Outer
  1247  				case <-time.After(5 * time.Second):
  1248  				}
  1249  			}
  1250  			cmd.Process.Kill()
  1251  			err = <-done
  1252  			fmt.Fprintf(&buf, "*** Test killed: ran too long (%v).\n", testKillTimeout)
  1253  		}
  1254  		tick.Stop()
  1255  	}
  1256  	out := buf.Bytes()
  1257  	t := fmt.Sprintf("%.3fs", time.Since(t0).Seconds())
  1258  	if err == nil {
  1259  		norun := ""
  1260  		if testShowPass {
  1261  			a.TestOutput.Write(out)
  1262  		}
  1263  		if bytes.HasPrefix(out, noTestsToRun[1:]) || bytes.Contains(out, noTestsToRun) {
  1264  			norun = " [no tests to run]"
  1265  		}
  1266  		fmt.Fprintf(a.TestOutput, "ok  \t%s\t%s%s%s\n", a.Package.ImportPath, t, coveragePercentage(out), norun)
  1267  		return nil
  1268  	}
  1269  
  1270  	base.SetExitStatus(1)
  1271  	if len(out) > 0 {
  1272  		a.TestOutput.Write(out)
  1273  		// assume printing the test binary's exit status is superfluous
  1274  	} else {
  1275  		fmt.Fprintf(a.TestOutput, "%s\n", err)
  1276  	}
  1277  	fmt.Fprintf(a.TestOutput, "FAIL\t%s\t%s\n", a.Package.ImportPath, t)
  1278  
  1279  	return nil
  1280  }
  1281  
  1282  // coveragePercentage returns the coverage results (if enabled) for the
  1283  // test. It uncovers the data by scanning the output from the test run.
  1284  func coveragePercentage(out []byte) string {
  1285  	if !testCover {
  1286  		return ""
  1287  	}
  1288  	// The string looks like
  1289  	//	test coverage for encoding/binary: 79.9% of statements
  1290  	// Extract the piece from the percentage to the end of the line.
  1291  	re := regexp.MustCompile(`coverage: (.*)\n`)
  1292  	matches := re.FindSubmatch(out)
  1293  	if matches == nil {
  1294  		// Probably running "go test -cover" not "go test -cover fmt".
  1295  		// The coverage output will appear in the output directly.
  1296  		return ""
  1297  	}
  1298  	return fmt.Sprintf("\tcoverage: %s", matches[1])
  1299  }
  1300  
  1301  // builderCleanTest is the action for cleaning up after a test.
  1302  func builderCleanTest(b *work.Builder, a *work.Action) error {
  1303  	if cfg.BuildWork {
  1304  		return nil
  1305  	}
  1306  	run := a.Deps[0]
  1307  	testDir := filepath.Join(b.WorkDir, filepath.FromSlash(run.Package.ImportPath+"/_test"))
  1308  	os.RemoveAll(testDir)
  1309  	return nil
  1310  }
  1311  
  1312  // builderPrintTest is the action for printing a test result.
  1313  func builderPrintTest(b *work.Builder, a *work.Action) error {
  1314  	clean := a.Deps[0]
  1315  	run := clean.Deps[0]
  1316  	os.Stdout.Write(run.TestOutput.Bytes())
  1317  	run.TestOutput = nil
  1318  	return nil
  1319  }
  1320  
  1321  // builderNoTest is the action for testing a package with no test files.
  1322  func builderNoTest(b *work.Builder, a *work.Action) error {
  1323  	fmt.Printf("?   \t%s\t[no test files]\n", a.Package.ImportPath)
  1324  	return nil
  1325  }
  1326  
  1327  // isTestFunc tells whether fn has the type of a testing function. arg
  1328  // specifies the parameter type we look for: B, M or T.
  1329  func isTestFunc(fn *ast.FuncDecl, arg string) bool {
  1330  	if fn.Type.Results != nil && len(fn.Type.Results.List) > 0 ||
  1331  		fn.Type.Params.List == nil ||
  1332  		len(fn.Type.Params.List) != 1 ||
  1333  		len(fn.Type.Params.List[0].Names) > 1 {
  1334  		return false
  1335  	}
  1336  	ptr, ok := fn.Type.Params.List[0].Type.(*ast.StarExpr)
  1337  	if !ok {
  1338  		return false
  1339  	}
  1340  	// We can't easily check that the type is *testing.M
  1341  	// because we don't know how testing has been imported,
  1342  	// but at least check that it's *M or *something.M.
  1343  	// Same applies for B and T.
  1344  	if name, ok := ptr.X.(*ast.Ident); ok && name.Name == arg {
  1345  		return true
  1346  	}
  1347  	if sel, ok := ptr.X.(*ast.SelectorExpr); ok && sel.Sel.Name == arg {
  1348  		return true
  1349  	}
  1350  	return false
  1351  }
  1352  
  1353  // isTest tells whether name looks like a test (or benchmark, according to prefix).
  1354  // It is a Test (say) if there is a character after Test that is not a lower-case letter.
  1355  // We don't want TesticularCancer.
  1356  func isTest(name, prefix string) bool {
  1357  	if !strings.HasPrefix(name, prefix) {
  1358  		return false
  1359  	}
  1360  	if len(name) == len(prefix) { // "Test" is ok
  1361  		return true
  1362  	}
  1363  	rune, _ := utf8.DecodeRuneInString(name[len(prefix):])
  1364  	return !unicode.IsLower(rune)
  1365  }
  1366  
  1367  type coverInfo struct {
  1368  	Package *load.Package
  1369  	Vars    map[string]*load.CoverVar
  1370  }
  1371  
  1372  // loadTestFuncs returns the testFuncs describing the tests that will be run.
  1373  func loadTestFuncs(ptest *load.Package) (*testFuncs, error) {
  1374  	t := &testFuncs{
  1375  		Package: ptest,
  1376  	}
  1377  	for _, file := range ptest.TestGoFiles {
  1378  		if err := t.load(filepath.Join(ptest.Dir, file), "_test", &t.ImportTest, &t.NeedTest); err != nil {
  1379  			return nil, err
  1380  		}
  1381  	}
  1382  	for _, file := range ptest.XTestGoFiles {
  1383  		if err := t.load(filepath.Join(ptest.Dir, file), "_xtest", &t.ImportXtest, &t.NeedXtest); err != nil {
  1384  			return nil, err
  1385  		}
  1386  	}
  1387  	return t, nil
  1388  }
  1389  
  1390  // writeTestmain writes the _testmain.go file for t to the file named out.
  1391  func writeTestmain(out string, t *testFuncs) error {
  1392  	f, err := os.Create(out)
  1393  	if err != nil {
  1394  		return err
  1395  	}
  1396  	defer f.Close()
  1397  
  1398  	if err := testmainTmpl.Execute(f, t); err != nil {
  1399  		return err
  1400  	}
  1401  
  1402  	return nil
  1403  }
  1404  
  1405  type testFuncs struct {
  1406  	Tests       []testFunc
  1407  	Benchmarks  []testFunc
  1408  	Examples    []testFunc
  1409  	TestMain    *testFunc
  1410  	Package     *load.Package
  1411  	ImportTest  bool
  1412  	NeedTest    bool
  1413  	ImportXtest bool
  1414  	NeedXtest   bool
  1415  	NeedCgo     bool
  1416  	Cover       []coverInfo
  1417  }
  1418  
  1419  func (t *testFuncs) CoverMode() string {
  1420  	return testCoverMode
  1421  }
  1422  
  1423  func (t *testFuncs) CoverEnabled() bool {
  1424  	return testCover
  1425  }
  1426  
  1427  // ImportPath returns the import path of the package being tested, if it is within GOPATH.
  1428  // This is printed by the testing package when running benchmarks.
  1429  func (t *testFuncs) ImportPath() string {
  1430  	pkg := t.Package.ImportPath
  1431  	if strings.HasPrefix(pkg, "_/") {
  1432  		return ""
  1433  	}
  1434  	if pkg == "command-line-arguments" {
  1435  		return ""
  1436  	}
  1437  	return pkg
  1438  }
  1439  
  1440  // Covered returns a string describing which packages are being tested for coverage.
  1441  // If the covered package is the same as the tested package, it returns the empty string.
  1442  // Otherwise it is a comma-separated human-readable list of packages beginning with
  1443  // " in", ready for use in the coverage message.
  1444  func (t *testFuncs) Covered() string {
  1445  	if testCoverPaths == nil {
  1446  		return ""
  1447  	}
  1448  	return " in " + strings.Join(testCoverPaths, ", ")
  1449  }
  1450  
  1451  // Tested returns the name of the package being tested.
  1452  func (t *testFuncs) Tested() string {
  1453  	return t.Package.Name
  1454  }
  1455  
  1456  type testFunc struct {
  1457  	Package   string // imported package name (_test or _xtest)
  1458  	Name      string // function name
  1459  	Output    string // output, for examples
  1460  	Unordered bool   // output is allowed to be unordered.
  1461  }
  1462  
  1463  var testFileSet = token.NewFileSet()
  1464  
  1465  func (t *testFuncs) load(filename, pkg string, doImport, seen *bool) error {
  1466  	f, err := parser.ParseFile(testFileSet, filename, nil, parser.ParseComments)
  1467  	if err != nil {
  1468  		return base.ExpandScanner(err)
  1469  	}
  1470  	for _, d := range f.Decls {
  1471  		n, ok := d.(*ast.FuncDecl)
  1472  		if !ok {
  1473  			continue
  1474  		}
  1475  		if n.Recv != nil {
  1476  			continue
  1477  		}
  1478  		name := n.Name.String()
  1479  		switch {
  1480  		case name == "TestMain" && isTestFunc(n, "M"):
  1481  			if t.TestMain != nil {
  1482  				return errors.New("multiple definitions of TestMain")
  1483  			}
  1484  			t.TestMain = &testFunc{pkg, name, "", false}
  1485  			*doImport, *seen = true, true
  1486  		case isTest(name, "Test"):
  1487  			err := checkTestFunc(n, "T")
  1488  			if err != nil {
  1489  				return err
  1490  			}
  1491  			t.Tests = append(t.Tests, testFunc{pkg, name, "", false})
  1492  			*doImport, *seen = true, true
  1493  		case isTest(name, "Benchmark"):
  1494  			err := checkTestFunc(n, "B")
  1495  			if err != nil {
  1496  				return err
  1497  			}
  1498  			t.Benchmarks = append(t.Benchmarks, testFunc{pkg, name, "", false})
  1499  			*doImport, *seen = true, true
  1500  		}
  1501  	}
  1502  	ex := doc.Examples(f)
  1503  	sort.Slice(ex, func(i, j int) bool { return ex[i].Order < ex[j].Order })
  1504  	for _, e := range ex {
  1505  		*doImport = true // import test file whether executed or not
  1506  		if e.Output == "" && !e.EmptyOutput {
  1507  			// Don't run examples with no output.
  1508  			continue
  1509  		}
  1510  		t.Examples = append(t.Examples, testFunc{pkg, "Example" + e.Name, e.Output, e.Unordered})
  1511  		*seen = true
  1512  	}
  1513  	return nil
  1514  }
  1515  
  1516  func checkTestFunc(fn *ast.FuncDecl, arg string) error {
  1517  	if !isTestFunc(fn, arg) {
  1518  		name := fn.Name.String()
  1519  		pos := testFileSet.Position(fn.Pos())
  1520  		return fmt.Errorf("%s: wrong signature for %s, must be: func %s(%s *testing.%s)", pos, name, name, strings.ToLower(arg), arg)
  1521  	}
  1522  	return nil
  1523  }
  1524  
  1525  var testmainTmpl = template.Must(template.New("main").Parse(`
  1526  package main
  1527  
  1528  import (
  1529  {{if not .TestMain}}
  1530  	"os"
  1531  {{end}}
  1532  	"testing"
  1533  	"testing/internal/testdeps"
  1534  
  1535  {{if .ImportTest}}
  1536  	{{if .NeedTest}}_test{{else}}_{{end}} {{.Package.ImportPath | printf "%q"}}
  1537  {{end}}
  1538  {{if .ImportXtest}}
  1539  	{{if .NeedXtest}}_xtest{{else}}_{{end}} {{.Package.ImportPath | printf "%s_test" | printf "%q"}}
  1540  {{end}}
  1541  {{range $i, $p := .Cover}}
  1542  	_cover{{$i}} {{$p.Package.ImportPath | printf "%q"}}
  1543  {{end}}
  1544  
  1545  {{if .NeedCgo}}
  1546  	_ "runtime/cgo"
  1547  {{end}}
  1548  )
  1549  
  1550  var tests = []testing.InternalTest{
  1551  {{range .Tests}}
  1552  	{"{{.Name}}", {{.Package}}.{{.Name}}},
  1553  {{end}}
  1554  }
  1555  
  1556  var benchmarks = []testing.InternalBenchmark{
  1557  {{range .Benchmarks}}
  1558  	{"{{.Name}}", {{.Package}}.{{.Name}}},
  1559  {{end}}
  1560  }
  1561  
  1562  var examples = []testing.InternalExample{
  1563  {{range .Examples}}
  1564  	{"{{.Name}}", {{.Package}}.{{.Name}}, {{.Output | printf "%q"}}, {{.Unordered}}},
  1565  {{end}}
  1566  }
  1567  
  1568  func init() {
  1569  	testdeps.ImportPath = {{.ImportPath | printf "%q"}}
  1570  }
  1571  
  1572  {{if .CoverEnabled}}
  1573  
  1574  // Only updated by init functions, so no need for atomicity.
  1575  var (
  1576  	coverCounters = make(map[string][]uint32)
  1577  	coverBlocks = make(map[string][]testing.CoverBlock)
  1578  )
  1579  
  1580  func init() {
  1581  	{{range $i, $p := .Cover}}
  1582  	{{range $file, $cover := $p.Vars}}
  1583  	coverRegisterFile({{printf "%q" $cover.File}}, _cover{{$i}}.{{$cover.Var}}.Count[:], _cover{{$i}}.{{$cover.Var}}.Pos[:], _cover{{$i}}.{{$cover.Var}}.NumStmt[:])
  1584  	{{end}}
  1585  	{{end}}
  1586  }
  1587  
  1588  func coverRegisterFile(fileName string, counter []uint32, pos []uint32, numStmts []uint16) {
  1589  	if 3*len(counter) != len(pos) || len(counter) != len(numStmts) {
  1590  		panic("coverage: mismatched sizes")
  1591  	}
  1592  	if coverCounters[fileName] != nil {
  1593  		// Already registered.
  1594  		return
  1595  	}
  1596  	coverCounters[fileName] = counter
  1597  	block := make([]testing.CoverBlock, len(counter))
  1598  	for i := range counter {
  1599  		block[i] = testing.CoverBlock{
  1600  			Line0: pos[3*i+0],
  1601  			Col0: uint16(pos[3*i+2]),
  1602  			Line1: pos[3*i+1],
  1603  			Col1: uint16(pos[3*i+2]>>16),
  1604  			Stmts: numStmts[i],
  1605  		}
  1606  	}
  1607  	coverBlocks[fileName] = block
  1608  }
  1609  {{end}}
  1610  
  1611  func main() {
  1612  {{if .CoverEnabled}}
  1613  	testing.RegisterCover(testing.Cover{
  1614  		Mode: {{printf "%q" .CoverMode}},
  1615  		Counters: coverCounters,
  1616  		Blocks: coverBlocks,
  1617  		CoveredPackages: {{printf "%q" .Covered}},
  1618  	})
  1619  {{end}}
  1620  	m := testing.MainStart(testdeps.TestDeps{}, tests, benchmarks, examples)
  1621  {{with .TestMain}}
  1622  	{{.Package}}.{{.Name}}(m)
  1623  {{else}}
  1624  	os.Exit(m.Run())
  1625  {{end}}
  1626  }
  1627  
  1628  `))