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