github.com/hlts2/go@v0.0.0-20170904000733-812b34efaed8/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 = []string{
   429  	// Dependencies for testmain.
   430  	"os",
   431  	"testing",
   432  	"testing/internal/testdeps",
   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.Vendored(p.TestImports) {
   503  				deps[path] = true
   504  			}
   505  			for _, path := range p.Vendored(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.Imports {
   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  	for i, path := range p.TestImports {
   719  		p1 := load.LoadImport(path, p.Dir, p, &stk, p.Internal.Build.TestImportPos[path], load.UseVendor)
   720  		if p1.Error != nil {
   721  			return nil, nil, nil, p1.Error
   722  		}
   723  		if len(p1.DepsErrors) > 0 {
   724  			err := p1.DepsErrors[0]
   725  			err.Pos = "" // show full import stack
   726  			return nil, nil, nil, err
   727  		}
   728  		if str.Contains(p1.Deps, p.ImportPath) || p1.ImportPath == p.ImportPath {
   729  			// Same error that loadPackage returns (via reusePackage) in pkg.go.
   730  			// Can't change that code, because that code is only for loading the
   731  			// non-test copy of a package.
   732  			err := &load.PackageError{
   733  				ImportStack:   testImportStack(stk[0], p1, p.ImportPath),
   734  				Err:           "import cycle not allowed in test",
   735  				IsImportCycle: true,
   736  			}
   737  			return nil, nil, nil, err
   738  		}
   739  		p.TestImports[i] = p1.ImportPath
   740  		imports = append(imports, p1)
   741  	}
   742  	stk.Pop()
   743  	stk.Push(p.ImportPath + "_test")
   744  	pxtestNeedsPtest := false
   745  	for i, path := range p.XTestImports {
   746  		p1 := load.LoadImport(path, p.Dir, p, &stk, p.Internal.Build.XTestImportPos[path], load.UseVendor)
   747  		if p1.Error != nil {
   748  			return nil, nil, nil, p1.Error
   749  		}
   750  		if len(p1.DepsErrors) > 0 {
   751  			err := p1.DepsErrors[0]
   752  			err.Pos = "" // show full import stack
   753  			return nil, nil, nil, err
   754  		}
   755  		if p1.ImportPath == p.ImportPath {
   756  			pxtestNeedsPtest = true
   757  		} else {
   758  			ximports = append(ximports, p1)
   759  		}
   760  		p.XTestImports[i] = p1.ImportPath
   761  	}
   762  	stk.Pop()
   763  
   764  	// Use last element of import path, not package name.
   765  	// They differ when package name is "main".
   766  	// But if the import path is "command-line-arguments",
   767  	// like it is during 'go run', use the package name.
   768  	var elem string
   769  	if p.ImportPath == "command-line-arguments" {
   770  		elem = p.Name
   771  	} else {
   772  		_, elem = path.Split(p.ImportPath)
   773  	}
   774  	testBinary := elem + ".test"
   775  
   776  	// The ptest package needs to be importable under the
   777  	// same import path that p has, but we cannot put it in
   778  	// the usual place in the temporary tree, because then
   779  	// other tests will see it as the real package.
   780  	// Instead we make a _test directory under the import path
   781  	// and then repeat the import path there. We tell the
   782  	// compiler and linker to look in that _test directory first.
   783  	//
   784  	// That is, if the package under test is unicode/utf8,
   785  	// then the normal place to write the package archive is
   786  	// $WORK/unicode/utf8.a, but we write the test package archive to
   787  	// $WORK/unicode/utf8/_test/unicode/utf8.a.
   788  	// We write the external test package archive to
   789  	// $WORK/unicode/utf8/_test/unicode/utf8_test.a.
   790  	testDir := filepath.Join(b.WorkDir, filepath.FromSlash(p.ImportPath+"/_test"))
   791  	ptestObj := work.BuildToolchain.Pkgpath(testDir, p)
   792  
   793  	// Create the directory for the .a files.
   794  	ptestDir, _ := filepath.Split(ptestObj)
   795  	if err := b.Mkdir(ptestDir); err != nil {
   796  		return nil, nil, nil, err
   797  	}
   798  
   799  	// Should we apply coverage analysis locally,
   800  	// only for this package and only for this test?
   801  	// Yes, if -cover is on but -coverpkg has not specified
   802  	// a list of packages for global coverage.
   803  	localCover := testCover && testCoverPaths == nil
   804  
   805  	// Test package.
   806  	if len(p.TestGoFiles) > 0 || localCover || p.Name == "main" {
   807  		ptest = new(load.Package)
   808  		*ptest = *p
   809  		ptest.GoFiles = nil
   810  		ptest.GoFiles = append(ptest.GoFiles, p.GoFiles...)
   811  		ptest.GoFiles = append(ptest.GoFiles, p.TestGoFiles...)
   812  		ptest.Internal.Target = ""
   813  		ptest.Imports = str.StringList(p.Imports, p.TestImports)
   814  		ptest.Internal.Imports = append(append([]*load.Package{}, p.Internal.Imports...), imports...)
   815  		ptest.Internal.Pkgdir = testDir
   816  		ptest.Internal.Fake = true
   817  		ptest.Internal.ForceLibrary = true
   818  		ptest.Stale = true
   819  		ptest.StaleReason = "rebuild for test"
   820  		ptest.Internal.Build = new(build.Package)
   821  		*ptest.Internal.Build = *p.Internal.Build
   822  		m := map[string][]token.Position{}
   823  		for k, v := range p.Internal.Build.ImportPos {
   824  			m[k] = append(m[k], v...)
   825  		}
   826  		for k, v := range p.Internal.Build.TestImportPos {
   827  			m[k] = append(m[k], v...)
   828  		}
   829  		ptest.Internal.Build.ImportPos = m
   830  
   831  		if localCover {
   832  			ptest.Internal.CoverMode = testCoverMode
   833  			var coverFiles []string
   834  			coverFiles = append(coverFiles, ptest.GoFiles...)
   835  			coverFiles = append(coverFiles, ptest.CgoFiles...)
   836  			ptest.Internal.CoverVars = declareCoverVars(ptest.ImportPath, coverFiles...)
   837  		}
   838  	} else {
   839  		ptest = p
   840  	}
   841  
   842  	// External test package.
   843  	if len(p.XTestGoFiles) > 0 {
   844  		pxtest = &load.Package{
   845  			PackagePublic: load.PackagePublic{
   846  				Name:       p.Name + "_test",
   847  				ImportPath: p.ImportPath + "_test",
   848  				Root:       p.Root,
   849  				Dir:        p.Dir,
   850  				GoFiles:    p.XTestGoFiles,
   851  				Imports:    p.XTestImports,
   852  				Stale:      true,
   853  			},
   854  			Internal: load.PackageInternal{
   855  				LocalPrefix: p.Internal.LocalPrefix,
   856  				Build: &build.Package{
   857  					ImportPos: p.Internal.Build.XTestImportPos,
   858  				},
   859  				Imports:  ximports,
   860  				Pkgdir:   testDir,
   861  				Fake:     true,
   862  				External: true,
   863  			},
   864  		}
   865  		if pxtestNeedsPtest {
   866  			pxtest.Internal.Imports = append(pxtest.Internal.Imports, ptest)
   867  		}
   868  	}
   869  
   870  	// Action for building pkg.test.
   871  	pmain = &load.Package{
   872  		PackagePublic: load.PackagePublic{
   873  			Name:       "main",
   874  			Dir:        testDir,
   875  			GoFiles:    []string{"_testmain.go"},
   876  			ImportPath: "testmain",
   877  			Root:       p.Root,
   878  			Stale:      true,
   879  		},
   880  		Internal: load.PackageInternal{
   881  			Build:     &build.Package{Name: "main"},
   882  			Pkgdir:    testDir,
   883  			Fake:      true,
   884  			OmitDebug: !testC && !testNeedBinary,
   885  		},
   886  	}
   887  
   888  	// The generated main also imports testing, regexp, and os.
   889  	stk.Push("testmain")
   890  	for _, dep := range testMainDeps {
   891  		if dep == ptest.ImportPath {
   892  			pmain.Internal.Imports = append(pmain.Internal.Imports, ptest)
   893  		} else {
   894  			p1 := load.LoadImport(dep, "", nil, &stk, nil, 0)
   895  			if p1.Error != nil {
   896  				return nil, nil, nil, p1.Error
   897  			}
   898  			pmain.Internal.Imports = append(pmain.Internal.Imports, p1)
   899  		}
   900  	}
   901  
   902  	if testCoverPkgs != nil {
   903  		// Add imports, but avoid duplicates.
   904  		seen := map[*load.Package]bool{p: true, ptest: true}
   905  		for _, p1 := range pmain.Internal.Imports {
   906  			seen[p1] = true
   907  		}
   908  		for _, p1 := range testCoverPkgs {
   909  			if !seen[p1] {
   910  				seen[p1] = true
   911  				pmain.Internal.Imports = append(pmain.Internal.Imports, p1)
   912  			}
   913  		}
   914  	}
   915  
   916  	// Do initial scan for metadata needed for writing _testmain.go
   917  	// Use that metadata to update the list of imports for package main.
   918  	// The list of imports is used by recompileForTest and by the loop
   919  	// afterward that gathers t.Cover information.
   920  	t, err := loadTestFuncs(ptest)
   921  	if err != nil {
   922  		return nil, nil, nil, err
   923  	}
   924  	if len(ptest.GoFiles)+len(ptest.CgoFiles) > 0 {
   925  		pmain.Internal.Imports = append(pmain.Internal.Imports, ptest)
   926  		t.ImportTest = true
   927  	}
   928  	if pxtest != nil {
   929  		pmain.Internal.Imports = append(pmain.Internal.Imports, pxtest)
   930  		t.ImportXtest = true
   931  	}
   932  
   933  	if ptest != p && localCover {
   934  		// We have made modifications to the package p being tested
   935  		// and are rebuilding p (as ptest), writing it to the testDir tree.
   936  		// Arrange to rebuild, writing to that same tree, all packages q
   937  		// such that the test depends on q, and q depends on p.
   938  		// This makes sure that q sees the modifications to p.
   939  		// Strictly speaking, the rebuild is only necessary if the
   940  		// modifications to p change its export metadata, but
   941  		// determining that is a bit tricky, so we rebuild always.
   942  		//
   943  		// This will cause extra compilation, so for now we only do it
   944  		// when testCover is set. The conditions are more general, though,
   945  		// and we may find that we need to do it always in the future.
   946  		recompileForTest(pmain, p, ptest, testDir)
   947  	}
   948  
   949  	if cfg.BuildContext.GOOS == "darwin" {
   950  		if cfg.BuildContext.GOARCH == "arm" || cfg.BuildContext.GOARCH == "arm64" {
   951  			t.NeedCgo = true
   952  		}
   953  	}
   954  
   955  	for _, cp := range pmain.Internal.Imports {
   956  		if len(cp.Internal.CoverVars) > 0 {
   957  			t.Cover = append(t.Cover, coverInfo{cp, cp.Internal.CoverVars})
   958  		}
   959  	}
   960  
   961  	if !cfg.BuildN {
   962  		// writeTestmain writes _testmain.go. This must happen after recompileForTest,
   963  		// because recompileForTest modifies XXX.
   964  		if err := writeTestmain(filepath.Join(testDir, "_testmain.go"), t); err != nil {
   965  			return nil, nil, nil, err
   966  		}
   967  	}
   968  
   969  	load.ComputeStale(pmain)
   970  
   971  	if ptest != p {
   972  		a := b.Action(work.ModeBuild, work.ModeBuild, ptest)
   973  		a.Objdir = testDir + string(filepath.Separator) + "_obj_test" + string(filepath.Separator)
   974  		a.Objpkg = ptestObj
   975  		a.Target = ptestObj
   976  		a.Link = false
   977  	}
   978  
   979  	if pxtest != nil {
   980  		a := b.Action(work.ModeBuild, work.ModeBuild, pxtest)
   981  		a.Objdir = testDir + string(filepath.Separator) + "_obj_xtest" + string(filepath.Separator)
   982  		a.Objpkg = work.BuildToolchain.Pkgpath(testDir, pxtest)
   983  		a.Target = a.Objpkg
   984  	}
   985  
   986  	a := b.Action(work.ModeBuild, work.ModeBuild, pmain)
   987  	a.Objdir = testDir + string(filepath.Separator)
   988  	a.Objpkg = filepath.Join(testDir, "main.a")
   989  	a.Target = filepath.Join(testDir, testBinary) + cfg.ExeSuffix
   990  	if cfg.Goos == "windows" {
   991  		// There are many reserved words on Windows that,
   992  		// if used in the name of an executable, cause Windows
   993  		// to try to ask for extra permissions.
   994  		// The word list includes setup, install, update, and patch,
   995  		// but it does not appear to be defined anywhere.
   996  		// We have run into this trying to run the
   997  		// go.codereview/patch tests.
   998  		// For package names containing those words, use test.test.exe
   999  		// instead of pkgname.test.exe.
  1000  		// Note that this file name is only used in the Go command's
  1001  		// temporary directory. If the -c or other flags are
  1002  		// given, the code below will still use pkgname.test.exe.
  1003  		// There are two user-visible effects of this change.
  1004  		// First, you can actually run 'go test' in directories that
  1005  		// have names that Windows thinks are installer-like,
  1006  		// without getting a dialog box asking for more permissions.
  1007  		// Second, in the Windows process listing during go test,
  1008  		// the test shows up as test.test.exe, not pkgname.test.exe.
  1009  		// That second one is a drawback, but it seems a small
  1010  		// price to pay for the test running at all.
  1011  		// If maintaining the list of bad words is too onerous,
  1012  		// we could just do this always on Windows.
  1013  		for _, bad := range windowsBadWords {
  1014  			if strings.Contains(testBinary, bad) {
  1015  				a.Target = filepath.Join(testDir, "test.test") + cfg.ExeSuffix
  1016  				break
  1017  			}
  1018  		}
  1019  	}
  1020  	buildAction = a
  1021  
  1022  	if testC || testNeedBinary {
  1023  		// -c or profiling flag: create action to copy binary to ./test.out.
  1024  		target := filepath.Join(base.Cwd, testBinary+cfg.ExeSuffix)
  1025  		if testO != "" {
  1026  			target = testO
  1027  			if !filepath.IsAbs(target) {
  1028  				target = filepath.Join(base.Cwd, target)
  1029  			}
  1030  		}
  1031  		buildAction = &work.Action{
  1032  			Func:    work.BuildInstallFunc,
  1033  			Deps:    []*work.Action{buildAction},
  1034  			Package: pmain,
  1035  			Target:  target,
  1036  		}
  1037  		runAction = buildAction // make sure runAction != nil even if not running test
  1038  	}
  1039  	if testC {
  1040  		printAction = &work.Action{Package: p, Deps: []*work.Action{runAction}} // nop
  1041  	} else {
  1042  		// run test
  1043  		runAction = &work.Action{
  1044  			Func:       builderRunTest,
  1045  			Deps:       []*work.Action{buildAction},
  1046  			Package:    p,
  1047  			IgnoreFail: true,
  1048  		}
  1049  		cleanAction := &work.Action{
  1050  			Func:    builderCleanTest,
  1051  			Deps:    []*work.Action{runAction},
  1052  			Package: p,
  1053  		}
  1054  		printAction = &work.Action{
  1055  			Func:    builderPrintTest,
  1056  			Deps:    []*work.Action{cleanAction},
  1057  			Package: p,
  1058  		}
  1059  	}
  1060  
  1061  	return buildAction, runAction, printAction, nil
  1062  }
  1063  
  1064  func testImportStack(top string, p *load.Package, target string) []string {
  1065  	stk := []string{top, p.ImportPath}
  1066  Search:
  1067  	for p.ImportPath != target {
  1068  		for _, p1 := range p.Internal.Imports {
  1069  			if p1.ImportPath == target || str.Contains(p1.Deps, target) {
  1070  				stk = append(stk, p1.ImportPath)
  1071  				p = p1
  1072  				continue Search
  1073  			}
  1074  		}
  1075  		// Can't happen, but in case it does...
  1076  		stk = append(stk, "<lost path to cycle>")
  1077  		break
  1078  	}
  1079  	return stk
  1080  }
  1081  
  1082  func recompileForTest(pmain, preal, ptest *load.Package, testDir string) {
  1083  	// The "test copy" of preal is ptest.
  1084  	// For each package that depends on preal, make a "test copy"
  1085  	// that depends on ptest. And so on, up the dependency tree.
  1086  	testCopy := map[*load.Package]*load.Package{preal: ptest}
  1087  	for _, p := range load.PackageList([]*load.Package{pmain}) {
  1088  		// Copy on write.
  1089  		didSplit := false
  1090  		split := func() {
  1091  			if didSplit {
  1092  				return
  1093  			}
  1094  			didSplit = true
  1095  			if p.Internal.Pkgdir != testDir {
  1096  				p1 := new(load.Package)
  1097  				testCopy[p] = p1
  1098  				*p1 = *p
  1099  				p1.Internal.Imports = make([]*load.Package, len(p.Internal.Imports))
  1100  				copy(p1.Internal.Imports, p.Internal.Imports)
  1101  				p = p1
  1102  				p.Internal.Pkgdir = testDir
  1103  				p.Internal.Target = ""
  1104  				p.Internal.Fake = true
  1105  				p.Stale = true
  1106  				p.StaleReason = "depends on package being tested"
  1107  			}
  1108  		}
  1109  
  1110  		// Update p.Internal.Imports to use test copies.
  1111  		for i, imp := range p.Internal.Imports {
  1112  			if p1 := testCopy[imp]; p1 != nil && p1 != imp {
  1113  				split()
  1114  				p.Internal.Imports[i] = p1
  1115  			}
  1116  		}
  1117  	}
  1118  }
  1119  
  1120  var coverIndex = 0
  1121  
  1122  // isTestFile reports whether the source file is a set of tests and should therefore
  1123  // be excluded from coverage analysis.
  1124  func isTestFile(file string) bool {
  1125  	// We don't cover tests, only the code they test.
  1126  	return strings.HasSuffix(file, "_test.go")
  1127  }
  1128  
  1129  // declareCoverVars attaches the required cover variables names
  1130  // to the files, to be used when annotating the files.
  1131  func declareCoverVars(importPath string, files ...string) map[string]*load.CoverVar {
  1132  	coverVars := make(map[string]*load.CoverVar)
  1133  	for _, file := range files {
  1134  		if isTestFile(file) {
  1135  			continue
  1136  		}
  1137  		coverVars[file] = &load.CoverVar{
  1138  			File: filepath.Join(importPath, file),
  1139  			Var:  fmt.Sprintf("GoCover_%d", coverIndex),
  1140  		}
  1141  		coverIndex++
  1142  	}
  1143  	return coverVars
  1144  }
  1145  
  1146  var noTestsToRun = []byte("\ntesting: warning: no tests to run\n")
  1147  
  1148  // builderRunTest is the action for running a test binary.
  1149  func builderRunTest(b *work.Builder, a *work.Action) error {
  1150  	args := str.StringList(work.FindExecCmd(), a.Deps[0].Target, testArgs)
  1151  	a.TestOutput = new(bytes.Buffer)
  1152  
  1153  	if cfg.BuildN || cfg.BuildX {
  1154  		b.Showcmd("", "%s", strings.Join(args, " "))
  1155  		if cfg.BuildN {
  1156  			return nil
  1157  		}
  1158  	}
  1159  
  1160  	if a.Failed {
  1161  		// We were unable to build the binary.
  1162  		a.Failed = false
  1163  		fmt.Fprintf(a.TestOutput, "FAIL\t%s [build failed]\n", a.Package.ImportPath)
  1164  		base.SetExitStatus(1)
  1165  		return nil
  1166  	}
  1167  
  1168  	cmd := exec.Command(args[0], args[1:]...)
  1169  	cmd.Dir = a.Package.Dir
  1170  	cmd.Env = base.EnvForDir(cmd.Dir, cfg.OrigEnv)
  1171  	var buf bytes.Buffer
  1172  	if testStreamOutput {
  1173  		cmd.Stdout = os.Stdout
  1174  		cmd.Stderr = os.Stderr
  1175  	} else {
  1176  		cmd.Stdout = &buf
  1177  		cmd.Stderr = &buf
  1178  	}
  1179  
  1180  	// If there are any local SWIG dependencies, we want to load
  1181  	// the shared library from the build directory.
  1182  	if a.Package.UsesSwig() {
  1183  		env := cmd.Env
  1184  		found := false
  1185  		prefix := "LD_LIBRARY_PATH="
  1186  		for i, v := range env {
  1187  			if strings.HasPrefix(v, prefix) {
  1188  				env[i] = v + ":."
  1189  				found = true
  1190  				break
  1191  			}
  1192  		}
  1193  		if !found {
  1194  			env = append(env, "LD_LIBRARY_PATH=.")
  1195  		}
  1196  		cmd.Env = env
  1197  	}
  1198  
  1199  	t0 := time.Now()
  1200  	err := cmd.Start()
  1201  
  1202  	// This is a last-ditch deadline to detect and
  1203  	// stop wedged test binaries, to keep the builders
  1204  	// running.
  1205  	if err == nil {
  1206  		tick := time.NewTimer(testKillTimeout)
  1207  		base.StartSigHandlers()
  1208  		done := make(chan error)
  1209  		go func() {
  1210  			done <- cmd.Wait()
  1211  		}()
  1212  	Outer:
  1213  		select {
  1214  		case err = <-done:
  1215  			// ok
  1216  		case <-tick.C:
  1217  			if base.SignalTrace != nil {
  1218  				// Send a quit signal in the hope that the program will print
  1219  				// a stack trace and exit. Give it five seconds before resorting
  1220  				// to Kill.
  1221  				cmd.Process.Signal(base.SignalTrace)
  1222  				select {
  1223  				case err = <-done:
  1224  					fmt.Fprintf(&buf, "*** Test killed with %v: ran too long (%v).\n", base.SignalTrace, testKillTimeout)
  1225  					break Outer
  1226  				case <-time.After(5 * time.Second):
  1227  				}
  1228  			}
  1229  			cmd.Process.Kill()
  1230  			err = <-done
  1231  			fmt.Fprintf(&buf, "*** Test killed: ran too long (%v).\n", testKillTimeout)
  1232  		}
  1233  		tick.Stop()
  1234  	}
  1235  	out := buf.Bytes()
  1236  	t := fmt.Sprintf("%.3fs", time.Since(t0).Seconds())
  1237  	if err == nil {
  1238  		norun := ""
  1239  		if testShowPass {
  1240  			a.TestOutput.Write(out)
  1241  		}
  1242  		if bytes.HasPrefix(out, noTestsToRun[1:]) || bytes.Contains(out, noTestsToRun) {
  1243  			norun = " [no tests to run]"
  1244  		}
  1245  		fmt.Fprintf(a.TestOutput, "ok  \t%s\t%s%s%s\n", a.Package.ImportPath, t, coveragePercentage(out), norun)
  1246  		return nil
  1247  	}
  1248  
  1249  	base.SetExitStatus(1)
  1250  	if len(out) > 0 {
  1251  		a.TestOutput.Write(out)
  1252  		// assume printing the test binary's exit status is superfluous
  1253  	} else {
  1254  		fmt.Fprintf(a.TestOutput, "%s\n", err)
  1255  	}
  1256  	fmt.Fprintf(a.TestOutput, "FAIL\t%s\t%s\n", a.Package.ImportPath, t)
  1257  
  1258  	return nil
  1259  }
  1260  
  1261  // coveragePercentage returns the coverage results (if enabled) for the
  1262  // test. It uncovers the data by scanning the output from the test run.
  1263  func coveragePercentage(out []byte) string {
  1264  	if !testCover {
  1265  		return ""
  1266  	}
  1267  	// The string looks like
  1268  	//	test coverage for encoding/binary: 79.9% of statements
  1269  	// Extract the piece from the percentage to the end of the line.
  1270  	re := regexp.MustCompile(`coverage: (.*)\n`)
  1271  	matches := re.FindSubmatch(out)
  1272  	if matches == nil {
  1273  		// Probably running "go test -cover" not "go test -cover fmt".
  1274  		// The coverage output will appear in the output directly.
  1275  		return ""
  1276  	}
  1277  	return fmt.Sprintf("\tcoverage: %s", matches[1])
  1278  }
  1279  
  1280  // builderCleanTest is the action for cleaning up after a test.
  1281  func builderCleanTest(b *work.Builder, a *work.Action) error {
  1282  	if cfg.BuildWork {
  1283  		return nil
  1284  	}
  1285  	run := a.Deps[0]
  1286  	testDir := filepath.Join(b.WorkDir, filepath.FromSlash(run.Package.ImportPath+"/_test"))
  1287  	os.RemoveAll(testDir)
  1288  	return nil
  1289  }
  1290  
  1291  // builderPrintTest is the action for printing a test result.
  1292  func builderPrintTest(b *work.Builder, a *work.Action) error {
  1293  	clean := a.Deps[0]
  1294  	run := clean.Deps[0]
  1295  	os.Stdout.Write(run.TestOutput.Bytes())
  1296  	run.TestOutput = nil
  1297  	return nil
  1298  }
  1299  
  1300  // builderNoTest is the action for testing a package with no test files.
  1301  func builderNoTest(b *work.Builder, a *work.Action) error {
  1302  	fmt.Printf("?   \t%s\t[no test files]\n", a.Package.ImportPath)
  1303  	return nil
  1304  }
  1305  
  1306  // isTestFunc tells whether fn has the type of a testing function. arg
  1307  // specifies the parameter type we look for: B, M or T.
  1308  func isTestFunc(fn *ast.FuncDecl, arg string) bool {
  1309  	if fn.Type.Results != nil && len(fn.Type.Results.List) > 0 ||
  1310  		fn.Type.Params.List == nil ||
  1311  		len(fn.Type.Params.List) != 1 ||
  1312  		len(fn.Type.Params.List[0].Names) > 1 {
  1313  		return false
  1314  	}
  1315  	ptr, ok := fn.Type.Params.List[0].Type.(*ast.StarExpr)
  1316  	if !ok {
  1317  		return false
  1318  	}
  1319  	// We can't easily check that the type is *testing.M
  1320  	// because we don't know how testing has been imported,
  1321  	// but at least check that it's *M or *something.M.
  1322  	// Same applies for B and T.
  1323  	if name, ok := ptr.X.(*ast.Ident); ok && name.Name == arg {
  1324  		return true
  1325  	}
  1326  	if sel, ok := ptr.X.(*ast.SelectorExpr); ok && sel.Sel.Name == arg {
  1327  		return true
  1328  	}
  1329  	return false
  1330  }
  1331  
  1332  // isTest tells whether name looks like a test (or benchmark, according to prefix).
  1333  // It is a Test (say) if there is a character after Test that is not a lower-case letter.
  1334  // We don't want TesticularCancer.
  1335  func isTest(name, prefix string) bool {
  1336  	if !strings.HasPrefix(name, prefix) {
  1337  		return false
  1338  	}
  1339  	if len(name) == len(prefix) { // "Test" is ok
  1340  		return true
  1341  	}
  1342  	rune, _ := utf8.DecodeRuneInString(name[len(prefix):])
  1343  	return !unicode.IsLower(rune)
  1344  }
  1345  
  1346  type coverInfo struct {
  1347  	Package *load.Package
  1348  	Vars    map[string]*load.CoverVar
  1349  }
  1350  
  1351  // loadTestFuncs returns the testFuncs describing the tests that will be run.
  1352  func loadTestFuncs(ptest *load.Package) (*testFuncs, error) {
  1353  	t := &testFuncs{
  1354  		Package: ptest,
  1355  	}
  1356  	for _, file := range ptest.TestGoFiles {
  1357  		if err := t.load(filepath.Join(ptest.Dir, file), "_test", &t.ImportTest, &t.NeedTest); err != nil {
  1358  			return nil, err
  1359  		}
  1360  	}
  1361  	for _, file := range ptest.XTestGoFiles {
  1362  		if err := t.load(filepath.Join(ptest.Dir, file), "_xtest", &t.ImportXtest, &t.NeedXtest); err != nil {
  1363  			return nil, err
  1364  		}
  1365  	}
  1366  	return t, nil
  1367  }
  1368  
  1369  // writeTestmain writes the _testmain.go file for t to the file named out.
  1370  func writeTestmain(out string, t *testFuncs) error {
  1371  	f, err := os.Create(out)
  1372  	if err != nil {
  1373  		return err
  1374  	}
  1375  	defer f.Close()
  1376  
  1377  	if err := testmainTmpl.Execute(f, t); err != nil {
  1378  		return err
  1379  	}
  1380  
  1381  	return nil
  1382  }
  1383  
  1384  type testFuncs struct {
  1385  	Tests       []testFunc
  1386  	Benchmarks  []testFunc
  1387  	Examples    []testFunc
  1388  	TestMain    *testFunc
  1389  	Package     *load.Package
  1390  	ImportTest  bool
  1391  	NeedTest    bool
  1392  	ImportXtest bool
  1393  	NeedXtest   bool
  1394  	NeedCgo     bool
  1395  	Cover       []coverInfo
  1396  }
  1397  
  1398  func (t *testFuncs) CoverMode() string {
  1399  	return testCoverMode
  1400  }
  1401  
  1402  func (t *testFuncs) CoverEnabled() bool {
  1403  	return testCover
  1404  }
  1405  
  1406  // ImportPath returns the import path of the package being tested, if it is within GOPATH.
  1407  // This is printed by the testing package when running benchmarks.
  1408  func (t *testFuncs) ImportPath() string {
  1409  	pkg := t.Package.ImportPath
  1410  	if strings.HasPrefix(pkg, "_/") {
  1411  		return ""
  1412  	}
  1413  	if pkg == "command-line-arguments" {
  1414  		return ""
  1415  	}
  1416  	return pkg
  1417  }
  1418  
  1419  // Covered returns a string describing which packages are being tested for coverage.
  1420  // If the covered package is the same as the tested package, it returns the empty string.
  1421  // Otherwise it is a comma-separated human-readable list of packages beginning with
  1422  // " in", ready for use in the coverage message.
  1423  func (t *testFuncs) Covered() string {
  1424  	if testCoverPaths == nil {
  1425  		return ""
  1426  	}
  1427  	return " in " + strings.Join(testCoverPaths, ", ")
  1428  }
  1429  
  1430  // Tested returns the name of the package being tested.
  1431  func (t *testFuncs) Tested() string {
  1432  	return t.Package.Name
  1433  }
  1434  
  1435  type testFunc struct {
  1436  	Package   string // imported package name (_test or _xtest)
  1437  	Name      string // function name
  1438  	Output    string // output, for examples
  1439  	Unordered bool   // output is allowed to be unordered.
  1440  }
  1441  
  1442  var testFileSet = token.NewFileSet()
  1443  
  1444  func (t *testFuncs) load(filename, pkg string, doImport, seen *bool) error {
  1445  	f, err := parser.ParseFile(testFileSet, filename, nil, parser.ParseComments)
  1446  	if err != nil {
  1447  		return base.ExpandScanner(err)
  1448  	}
  1449  	for _, d := range f.Decls {
  1450  		n, ok := d.(*ast.FuncDecl)
  1451  		if !ok {
  1452  			continue
  1453  		}
  1454  		if n.Recv != nil {
  1455  			continue
  1456  		}
  1457  		name := n.Name.String()
  1458  		switch {
  1459  		case name == "TestMain" && isTestFunc(n, "M"):
  1460  			if t.TestMain != nil {
  1461  				return errors.New("multiple definitions of TestMain")
  1462  			}
  1463  			t.TestMain = &testFunc{pkg, name, "", false}
  1464  			*doImport, *seen = true, true
  1465  		case isTest(name, "Test"):
  1466  			err := checkTestFunc(n, "T")
  1467  			if err != nil {
  1468  				return err
  1469  			}
  1470  			t.Tests = append(t.Tests, testFunc{pkg, name, "", false})
  1471  			*doImport, *seen = true, true
  1472  		case isTest(name, "Benchmark"):
  1473  			err := checkTestFunc(n, "B")
  1474  			if err != nil {
  1475  				return err
  1476  			}
  1477  			t.Benchmarks = append(t.Benchmarks, testFunc{pkg, name, "", false})
  1478  			*doImport, *seen = true, true
  1479  		}
  1480  	}
  1481  	ex := doc.Examples(f)
  1482  	sort.Slice(ex, func(i, j int) bool { return ex[i].Order < ex[j].Order })
  1483  	for _, e := range ex {
  1484  		*doImport = true // import test file whether executed or not
  1485  		if e.Output == "" && !e.EmptyOutput {
  1486  			// Don't run examples with no output.
  1487  			continue
  1488  		}
  1489  		t.Examples = append(t.Examples, testFunc{pkg, "Example" + e.Name, e.Output, e.Unordered})
  1490  		*seen = true
  1491  	}
  1492  	return nil
  1493  }
  1494  
  1495  func checkTestFunc(fn *ast.FuncDecl, arg string) error {
  1496  	if !isTestFunc(fn, arg) {
  1497  		name := fn.Name.String()
  1498  		pos := testFileSet.Position(fn.Pos())
  1499  		return fmt.Errorf("%s: wrong signature for %s, must be: func %s(%s *testing.%s)", pos, name, name, strings.ToLower(arg), arg)
  1500  	}
  1501  	return nil
  1502  }
  1503  
  1504  var testmainTmpl = template.Must(template.New("main").Parse(`
  1505  package main
  1506  
  1507  import (
  1508  {{if not .TestMain}}
  1509  	"os"
  1510  {{end}}
  1511  	"testing"
  1512  	"testing/internal/testdeps"
  1513  
  1514  {{if .ImportTest}}
  1515  	{{if .NeedTest}}_test{{else}}_{{end}} {{.Package.ImportPath | printf "%q"}}
  1516  {{end}}
  1517  {{if .ImportXtest}}
  1518  	{{if .NeedXtest}}_xtest{{else}}_{{end}} {{.Package.ImportPath | printf "%s_test" | printf "%q"}}
  1519  {{end}}
  1520  {{range $i, $p := .Cover}}
  1521  	_cover{{$i}} {{$p.Package.ImportPath | printf "%q"}}
  1522  {{end}}
  1523  
  1524  {{if .NeedCgo}}
  1525  	_ "runtime/cgo"
  1526  {{end}}
  1527  )
  1528  
  1529  var tests = []testing.InternalTest{
  1530  {{range .Tests}}
  1531  	{"{{.Name}}", {{.Package}}.{{.Name}}},
  1532  {{end}}
  1533  }
  1534  
  1535  var benchmarks = []testing.InternalBenchmark{
  1536  {{range .Benchmarks}}
  1537  	{"{{.Name}}", {{.Package}}.{{.Name}}},
  1538  {{end}}
  1539  }
  1540  
  1541  var examples = []testing.InternalExample{
  1542  {{range .Examples}}
  1543  	{"{{.Name}}", {{.Package}}.{{.Name}}, {{.Output | printf "%q"}}, {{.Unordered}}},
  1544  {{end}}
  1545  }
  1546  
  1547  func init() {
  1548  	testdeps.ImportPath = {{.ImportPath | printf "%q"}}
  1549  }
  1550  
  1551  {{if .CoverEnabled}}
  1552  
  1553  // Only updated by init functions, so no need for atomicity.
  1554  var (
  1555  	coverCounters = make(map[string][]uint32)
  1556  	coverBlocks = make(map[string][]testing.CoverBlock)
  1557  )
  1558  
  1559  func init() {
  1560  	{{range $i, $p := .Cover}}
  1561  	{{range $file, $cover := $p.Vars}}
  1562  	coverRegisterFile({{printf "%q" $cover.File}}, _cover{{$i}}.{{$cover.Var}}.Count[:], _cover{{$i}}.{{$cover.Var}}.Pos[:], _cover{{$i}}.{{$cover.Var}}.NumStmt[:])
  1563  	{{end}}
  1564  	{{end}}
  1565  }
  1566  
  1567  func coverRegisterFile(fileName string, counter []uint32, pos []uint32, numStmts []uint16) {
  1568  	if 3*len(counter) != len(pos) || len(counter) != len(numStmts) {
  1569  		panic("coverage: mismatched sizes")
  1570  	}
  1571  	if coverCounters[fileName] != nil {
  1572  		// Already registered.
  1573  		return
  1574  	}
  1575  	coverCounters[fileName] = counter
  1576  	block := make([]testing.CoverBlock, len(counter))
  1577  	for i := range counter {
  1578  		block[i] = testing.CoverBlock{
  1579  			Line0: pos[3*i+0],
  1580  			Col0: uint16(pos[3*i+2]),
  1581  			Line1: pos[3*i+1],
  1582  			Col1: uint16(pos[3*i+2]>>16),
  1583  			Stmts: numStmts[i],
  1584  		}
  1585  	}
  1586  	coverBlocks[fileName] = block
  1587  }
  1588  {{end}}
  1589  
  1590  func main() {
  1591  {{if .CoverEnabled}}
  1592  	testing.RegisterCover(testing.Cover{
  1593  		Mode: {{printf "%q" .CoverMode}},
  1594  		Counters: coverCounters,
  1595  		Blocks: coverBlocks,
  1596  		CoveredPackages: {{printf "%q" .Covered}},
  1597  	})
  1598  {{end}}
  1599  	m := testing.MainStart(testdeps.TestDeps{}, tests, benchmarks, examples)
  1600  {{with .TestMain}}
  1601  	{{.Package}}.{{.Name}}(m)
  1602  {{else}}
  1603  	os.Exit(m.Run())
  1604  {{end}}
  1605  }
  1606  
  1607  `))