Black Lives Matter. Support the Equal Justice Initiative.

Source file src/cmd/go/internal/test/test.go

Documentation: cmd/go/internal/test

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

View as plain text