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