github.com/sanprasirt/go@v0.0.0-20170607001320-a027466e4b6d/src/cmd/go/internal/test/test.go (about)

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