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