github.com/peggyl/go@v0.0.0-20151008231540-ae315999c2d5/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  	if !buildN {
   821  		// writeTestmain writes _testmain.go. This must happen after recompileForTest,
   822  		// because recompileForTest modifies XXX.
   823  		if err := writeTestmain(filepath.Join(testDir, "_testmain.go"), t); err != nil {
   824  			return nil, nil, nil, err
   825  		}
   826  	}
   827  
   828  	computeStale(pmain)
   829  
   830  	if ptest != p {
   831  		a := b.action(modeBuild, modeBuild, ptest)
   832  		a.objdir = testDir + string(filepath.Separator) + "_obj_test" + string(filepath.Separator)
   833  		a.objpkg = ptestObj
   834  		a.target = ptestObj
   835  		a.link = false
   836  	}
   837  
   838  	if pxtest != nil {
   839  		a := b.action(modeBuild, modeBuild, pxtest)
   840  		a.objdir = testDir + string(filepath.Separator) + "_obj_xtest" + string(filepath.Separator)
   841  		a.objpkg = buildToolchain.pkgpath(testDir, pxtest)
   842  		a.target = a.objpkg
   843  	}
   844  
   845  	a := b.action(modeBuild, modeBuild, pmain)
   846  	a.objdir = testDir + string(filepath.Separator)
   847  	a.objpkg = filepath.Join(testDir, "main.a")
   848  	a.target = filepath.Join(testDir, testBinary) + exeSuffix
   849  	if goos == "windows" {
   850  		// There are many reserved words on Windows that,
   851  		// if used in the name of an executable, cause Windows
   852  		// to try to ask for extra permissions.
   853  		// The word list includes setup, install, update, and patch,
   854  		// but it does not appear to be defined anywhere.
   855  		// We have run into this trying to run the
   856  		// go.codereview/patch tests.
   857  		// For package names containing those words, use test.test.exe
   858  		// instead of pkgname.test.exe.
   859  		// Note that this file name is only used in the Go command's
   860  		// temporary directory. If the -c or other flags are
   861  		// given, the code below will still use pkgname.test.exe.
   862  		// There are two user-visible effects of this change.
   863  		// First, you can actually run 'go test' in directories that
   864  		// have names that Windows thinks are installer-like,
   865  		// without getting a dialog box asking for more permissions.
   866  		// Second, in the Windows process listing during go test,
   867  		// the test shows up as test.test.exe, not pkgname.test.exe.
   868  		// That second one is a drawback, but it seems a small
   869  		// price to pay for the test running at all.
   870  		// If maintaining the list of bad words is too onerous,
   871  		// we could just do this always on Windows.
   872  		for _, bad := range windowsBadWords {
   873  			if strings.Contains(testBinary, bad) {
   874  				a.target = filepath.Join(testDir, "test.test") + exeSuffix
   875  				break
   876  			}
   877  		}
   878  	}
   879  	buildAction = a
   880  
   881  	if testC || testNeedBinary {
   882  		// -c or profiling flag: create action to copy binary to ./test.out.
   883  		target := filepath.Join(cwd, testBinary+exeSuffix)
   884  		if testO != "" {
   885  			target = testO
   886  			if !filepath.IsAbs(target) {
   887  				target = filepath.Join(cwd, target)
   888  			}
   889  		}
   890  		buildAction = &action{
   891  			f:      (*builder).install,
   892  			deps:   []*action{buildAction},
   893  			p:      pmain,
   894  			target: target,
   895  		}
   896  		runAction = buildAction // make sure runAction != nil even if not running test
   897  	}
   898  	if testC {
   899  		printAction = &action{p: p, deps: []*action{runAction}} // nop
   900  	} else {
   901  		// run test
   902  		runAction = &action{
   903  			f:          (*builder).runTest,
   904  			deps:       []*action{buildAction},
   905  			p:          p,
   906  			ignoreFail: true,
   907  		}
   908  		cleanAction := &action{
   909  			f:    (*builder).cleanTest,
   910  			deps: []*action{runAction},
   911  			p:    p,
   912  		}
   913  		printAction = &action{
   914  			f:    (*builder).printTest,
   915  			deps: []*action{cleanAction},
   916  			p:    p,
   917  		}
   918  	}
   919  
   920  	return buildAction, runAction, printAction, nil
   921  }
   922  
   923  func testImportStack(top string, p *Package, target string) []string {
   924  	stk := []string{top, p.ImportPath}
   925  Search:
   926  	for p.ImportPath != target {
   927  		for _, p1 := range p.imports {
   928  			if p1.ImportPath == target || contains(p1.Deps, target) {
   929  				stk = append(stk, p1.ImportPath)
   930  				p = p1
   931  				continue Search
   932  			}
   933  		}
   934  		// Can't happen, but in case it does...
   935  		stk = append(stk, "<lost path to cycle>")
   936  		break
   937  	}
   938  	return stk
   939  }
   940  
   941  func recompileForTest(pmain, preal, ptest *Package, testDir string) {
   942  	// The "test copy" of preal is ptest.
   943  	// For each package that depends on preal, make a "test copy"
   944  	// that depends on ptest. And so on, up the dependency tree.
   945  	testCopy := map[*Package]*Package{preal: ptest}
   946  	for _, p := range packageList([]*Package{pmain}) {
   947  		// Copy on write.
   948  		didSplit := false
   949  		split := func() {
   950  			if didSplit {
   951  				return
   952  			}
   953  			didSplit = true
   954  			if p.pkgdir != testDir {
   955  				p1 := new(Package)
   956  				testCopy[p] = p1
   957  				*p1 = *p
   958  				p1.imports = make([]*Package, len(p.imports))
   959  				copy(p1.imports, p.imports)
   960  				p = p1
   961  				p.pkgdir = testDir
   962  				p.target = ""
   963  				p.fake = true
   964  				p.Stale = true
   965  			}
   966  		}
   967  
   968  		// Update p.deps and p.imports to use at test copies.
   969  		for i, dep := range p.deps {
   970  			if p1 := testCopy[dep]; p1 != nil && p1 != dep {
   971  				split()
   972  				p.deps[i] = p1
   973  			}
   974  		}
   975  		for i, imp := range p.imports {
   976  			if p1 := testCopy[imp]; p1 != nil && p1 != imp {
   977  				split()
   978  				p.imports[i] = p1
   979  			}
   980  		}
   981  	}
   982  }
   983  
   984  var coverIndex = 0
   985  
   986  // isTestFile reports whether the source file is a set of tests and should therefore
   987  // be excluded from coverage analysis.
   988  func isTestFile(file string) bool {
   989  	// We don't cover tests, only the code they test.
   990  	return strings.HasSuffix(file, "_test.go")
   991  }
   992  
   993  // declareCoverVars attaches the required cover variables names
   994  // to the files, to be used when annotating the files.
   995  func declareCoverVars(importPath string, files ...string) map[string]*CoverVar {
   996  	coverVars := make(map[string]*CoverVar)
   997  	for _, file := range files {
   998  		if isTestFile(file) {
   999  			continue
  1000  		}
  1001  		coverVars[file] = &CoverVar{
  1002  			File: filepath.Join(importPath, file),
  1003  			Var:  fmt.Sprintf("GoCover_%d", coverIndex),
  1004  		}
  1005  		coverIndex++
  1006  	}
  1007  	return coverVars
  1008  }
  1009  
  1010  // runTest is the action for running a test binary.
  1011  func (b *builder) runTest(a *action) error {
  1012  	args := stringList(findExecCmd(), a.deps[0].target, testArgs)
  1013  	a.testOutput = new(bytes.Buffer)
  1014  
  1015  	if buildN || buildX {
  1016  		b.showcmd("", "%s", strings.Join(args, " "))
  1017  		if buildN {
  1018  			return nil
  1019  		}
  1020  	}
  1021  
  1022  	if a.failed {
  1023  		// We were unable to build the binary.
  1024  		a.failed = false
  1025  		fmt.Fprintf(a.testOutput, "FAIL\t%s [build failed]\n", a.p.ImportPath)
  1026  		setExitStatus(1)
  1027  		return nil
  1028  	}
  1029  
  1030  	cmd := exec.Command(args[0], args[1:]...)
  1031  	cmd.Dir = a.p.Dir
  1032  	cmd.Env = envForDir(cmd.Dir, origEnv)
  1033  	var buf bytes.Buffer
  1034  	if testStreamOutput {
  1035  		cmd.Stdout = os.Stdout
  1036  		cmd.Stderr = os.Stderr
  1037  	} else {
  1038  		cmd.Stdout = &buf
  1039  		cmd.Stderr = &buf
  1040  	}
  1041  
  1042  	// If there are any local SWIG dependencies, we want to load
  1043  	// the shared library from the build directory.
  1044  	if a.p.usesSwig() {
  1045  		env := cmd.Env
  1046  		found := false
  1047  		prefix := "LD_LIBRARY_PATH="
  1048  		for i, v := range env {
  1049  			if strings.HasPrefix(v, prefix) {
  1050  				env[i] = v + ":."
  1051  				found = true
  1052  				break
  1053  			}
  1054  		}
  1055  		if !found {
  1056  			env = append(env, "LD_LIBRARY_PATH=.")
  1057  		}
  1058  		cmd.Env = env
  1059  	}
  1060  
  1061  	t0 := time.Now()
  1062  	err := cmd.Start()
  1063  
  1064  	// This is a last-ditch deadline to detect and
  1065  	// stop wedged test binaries, to keep the builders
  1066  	// running.
  1067  	if err == nil {
  1068  		tick := time.NewTimer(testKillTimeout)
  1069  		startSigHandlers()
  1070  		done := make(chan error)
  1071  		go func() {
  1072  			done <- cmd.Wait()
  1073  		}()
  1074  	Outer:
  1075  		select {
  1076  		case err = <-done:
  1077  			// ok
  1078  		case <-tick.C:
  1079  			if signalTrace != nil {
  1080  				// Send a quit signal in the hope that the program will print
  1081  				// a stack trace and exit. Give it five seconds before resorting
  1082  				// to Kill.
  1083  				cmd.Process.Signal(signalTrace)
  1084  				select {
  1085  				case err = <-done:
  1086  					fmt.Fprintf(&buf, "*** Test killed with %v: ran too long (%v).\n", signalTrace, testKillTimeout)
  1087  					break Outer
  1088  				case <-time.After(5 * time.Second):
  1089  				}
  1090  			}
  1091  			cmd.Process.Kill()
  1092  			err = <-done
  1093  			fmt.Fprintf(&buf, "*** Test killed: ran too long (%v).\n", testKillTimeout)
  1094  		}
  1095  		tick.Stop()
  1096  	}
  1097  	out := buf.Bytes()
  1098  	t := fmt.Sprintf("%.3fs", time.Since(t0).Seconds())
  1099  	if err == nil {
  1100  		if testShowPass {
  1101  			a.testOutput.Write(out)
  1102  		}
  1103  		fmt.Fprintf(a.testOutput, "ok  \t%s\t%s%s\n", a.p.ImportPath, t, coveragePercentage(out))
  1104  		return nil
  1105  	}
  1106  
  1107  	setExitStatus(1)
  1108  	if len(out) > 0 {
  1109  		a.testOutput.Write(out)
  1110  		// assume printing the test binary's exit status is superfluous
  1111  	} else {
  1112  		fmt.Fprintf(a.testOutput, "%s\n", err)
  1113  	}
  1114  	fmt.Fprintf(a.testOutput, "FAIL\t%s\t%s\n", a.p.ImportPath, t)
  1115  
  1116  	return nil
  1117  }
  1118  
  1119  // coveragePercentage returns the coverage results (if enabled) for the
  1120  // test. It uncovers the data by scanning the output from the test run.
  1121  func coveragePercentage(out []byte) string {
  1122  	if !testCover {
  1123  		return ""
  1124  	}
  1125  	// The string looks like
  1126  	//	test coverage for encoding/binary: 79.9% of statements
  1127  	// Extract the piece from the percentage to the end of the line.
  1128  	re := regexp.MustCompile(`coverage: (.*)\n`)
  1129  	matches := re.FindSubmatch(out)
  1130  	if matches == nil {
  1131  		// Probably running "go test -cover" not "go test -cover fmt".
  1132  		// The coverage output will appear in the output directly.
  1133  		return ""
  1134  	}
  1135  	return fmt.Sprintf("\tcoverage: %s", matches[1])
  1136  }
  1137  
  1138  // cleanTest is the action for cleaning up after a test.
  1139  func (b *builder) cleanTest(a *action) error {
  1140  	if buildWork {
  1141  		return nil
  1142  	}
  1143  	run := a.deps[0]
  1144  	testDir := filepath.Join(b.work, filepath.FromSlash(run.p.ImportPath+"/_test"))
  1145  	os.RemoveAll(testDir)
  1146  	return nil
  1147  }
  1148  
  1149  // printTest is the action for printing a test result.
  1150  func (b *builder) printTest(a *action) error {
  1151  	clean := a.deps[0]
  1152  	run := clean.deps[0]
  1153  	os.Stdout.Write(run.testOutput.Bytes())
  1154  	run.testOutput = nil
  1155  	return nil
  1156  }
  1157  
  1158  // notest is the action for testing a package with no test files.
  1159  func (b *builder) notest(a *action) error {
  1160  	fmt.Printf("?   \t%s\t[no test files]\n", a.p.ImportPath)
  1161  	return nil
  1162  }
  1163  
  1164  // isTestMain tells whether fn is a TestMain(m *testing.M) function.
  1165  func isTestMain(fn *ast.FuncDecl) bool {
  1166  	if fn.Name.String() != "TestMain" ||
  1167  		fn.Type.Results != nil && len(fn.Type.Results.List) > 0 ||
  1168  		fn.Type.Params == nil ||
  1169  		len(fn.Type.Params.List) != 1 ||
  1170  		len(fn.Type.Params.List[0].Names) > 1 {
  1171  		return false
  1172  	}
  1173  	ptr, ok := fn.Type.Params.List[0].Type.(*ast.StarExpr)
  1174  	if !ok {
  1175  		return false
  1176  	}
  1177  	// We can't easily check that the type is *testing.M
  1178  	// because we don't know how testing has been imported,
  1179  	// but at least check that it's *M or *something.M.
  1180  	if name, ok := ptr.X.(*ast.Ident); ok && name.Name == "M" {
  1181  		return true
  1182  	}
  1183  	if sel, ok := ptr.X.(*ast.SelectorExpr); ok && sel.Sel.Name == "M" {
  1184  		return true
  1185  	}
  1186  	return false
  1187  }
  1188  
  1189  // isTest tells whether name looks like a test (or benchmark, according to prefix).
  1190  // It is a Test (say) if there is a character after Test that is not a lower-case letter.
  1191  // We don't want TesticularCancer.
  1192  func isTest(name, prefix string) bool {
  1193  	if !strings.HasPrefix(name, prefix) {
  1194  		return false
  1195  	}
  1196  	if len(name) == len(prefix) { // "Test" is ok
  1197  		return true
  1198  	}
  1199  	rune, _ := utf8.DecodeRuneInString(name[len(prefix):])
  1200  	return !unicode.IsLower(rune)
  1201  }
  1202  
  1203  type coverInfo struct {
  1204  	Package *Package
  1205  	Vars    map[string]*CoverVar
  1206  }
  1207  
  1208  // loadTestFuncs returns the testFuncs describing the tests that will be run.
  1209  func loadTestFuncs(ptest *Package) (*testFuncs, error) {
  1210  	t := &testFuncs{
  1211  		Package: ptest,
  1212  	}
  1213  	for _, file := range ptest.TestGoFiles {
  1214  		if err := t.load(filepath.Join(ptest.Dir, file), "_test", &t.ImportTest, &t.NeedTest); err != nil {
  1215  			return nil, err
  1216  		}
  1217  	}
  1218  	for _, file := range ptest.XTestGoFiles {
  1219  		if err := t.load(filepath.Join(ptest.Dir, file), "_xtest", &t.ImportXtest, &t.NeedXtest); err != nil {
  1220  			return nil, err
  1221  		}
  1222  	}
  1223  	return t, nil
  1224  }
  1225  
  1226  // writeTestmain writes the _testmain.go file for t to the file named out.
  1227  func writeTestmain(out string, t *testFuncs) error {
  1228  	f, err := os.Create(out)
  1229  	if err != nil {
  1230  		return err
  1231  	}
  1232  	defer f.Close()
  1233  
  1234  	if err := testmainTmpl.Execute(f, t); err != nil {
  1235  		return err
  1236  	}
  1237  
  1238  	return nil
  1239  }
  1240  
  1241  type testFuncs struct {
  1242  	Tests       []testFunc
  1243  	Benchmarks  []testFunc
  1244  	Examples    []testFunc
  1245  	TestMain    *testFunc
  1246  	Package     *Package
  1247  	ImportTest  bool
  1248  	NeedTest    bool
  1249  	ImportXtest bool
  1250  	NeedXtest   bool
  1251  	NeedCgo     bool
  1252  	Cover       []coverInfo
  1253  }
  1254  
  1255  func (t *testFuncs) CoverMode() string {
  1256  	return testCoverMode
  1257  }
  1258  
  1259  func (t *testFuncs) CoverEnabled() bool {
  1260  	return testCover
  1261  }
  1262  
  1263  // Covered returns a string describing which packages are being tested for coverage.
  1264  // If the covered package is the same as the tested package, it returns the empty string.
  1265  // Otherwise it is a comma-separated human-readable list of packages beginning with
  1266  // " in", ready for use in the coverage message.
  1267  func (t *testFuncs) Covered() string {
  1268  	if testCoverPaths == nil {
  1269  		return ""
  1270  	}
  1271  	return " in " + strings.Join(testCoverPaths, ", ")
  1272  }
  1273  
  1274  // Tested returns the name of the package being tested.
  1275  func (t *testFuncs) Tested() string {
  1276  	return t.Package.Name
  1277  }
  1278  
  1279  type testFunc struct {
  1280  	Package string // imported package name (_test or _xtest)
  1281  	Name    string // function name
  1282  	Output  string // output, for examples
  1283  }
  1284  
  1285  var testFileSet = token.NewFileSet()
  1286  
  1287  func (t *testFuncs) load(filename, pkg string, doImport, seen *bool) error {
  1288  	f, err := parser.ParseFile(testFileSet, filename, nil, parser.ParseComments)
  1289  	if err != nil {
  1290  		return expandScanner(err)
  1291  	}
  1292  	for _, d := range f.Decls {
  1293  		n, ok := d.(*ast.FuncDecl)
  1294  		if !ok {
  1295  			continue
  1296  		}
  1297  		if n.Recv != nil {
  1298  			continue
  1299  		}
  1300  		name := n.Name.String()
  1301  		switch {
  1302  		case isTestMain(n):
  1303  			if t.TestMain != nil {
  1304  				return errors.New("multiple definitions of TestMain")
  1305  			}
  1306  			t.TestMain = &testFunc{pkg, name, ""}
  1307  			*doImport, *seen = true, true
  1308  		case isTest(name, "Test"):
  1309  			t.Tests = append(t.Tests, testFunc{pkg, name, ""})
  1310  			*doImport, *seen = true, true
  1311  		case isTest(name, "Benchmark"):
  1312  			t.Benchmarks = append(t.Benchmarks, testFunc{pkg, name, ""})
  1313  			*doImport, *seen = true, true
  1314  		}
  1315  	}
  1316  	ex := doc.Examples(f)
  1317  	sort.Sort(byOrder(ex))
  1318  	for _, e := range ex {
  1319  		*doImport = true // import test file whether executed or not
  1320  		if e.Output == "" && !e.EmptyOutput {
  1321  			// Don't run examples with no output.
  1322  			continue
  1323  		}
  1324  		t.Examples = append(t.Examples, testFunc{pkg, "Example" + e.Name, e.Output})
  1325  		*seen = true
  1326  	}
  1327  	return nil
  1328  }
  1329  
  1330  type byOrder []*doc.Example
  1331  
  1332  func (x byOrder) Len() int           { return len(x) }
  1333  func (x byOrder) Swap(i, j int)      { x[i], x[j] = x[j], x[i] }
  1334  func (x byOrder) Less(i, j int) bool { return x[i].Order < x[j].Order }
  1335  
  1336  var testmainTmpl = template.Must(template.New("main").Parse(`
  1337  package main
  1338  
  1339  import (
  1340  {{if not .TestMain}}
  1341  	"os"
  1342  {{end}}
  1343  	"regexp"
  1344  	"testing"
  1345  
  1346  {{if .ImportTest}}
  1347  	{{if .NeedTest}}_test{{else}}_{{end}} {{.Package.ImportPath | printf "%q"}}
  1348  {{end}}
  1349  {{if .ImportXtest}}
  1350  	{{if .NeedXtest}}_xtest{{else}}_{{end}} {{.Package.ImportPath | printf "%s_test" | printf "%q"}}
  1351  {{end}}
  1352  {{range $i, $p := .Cover}}
  1353  	_cover{{$i}} {{$p.Package.ImportPath | printf "%q"}}
  1354  {{end}}
  1355  
  1356  {{if .NeedCgo}}
  1357  	_ "runtime/cgo"
  1358  {{end}}
  1359  )
  1360  
  1361  var tests = []testing.InternalTest{
  1362  {{range .Tests}}
  1363  	{"{{.Name}}", {{.Package}}.{{.Name}}},
  1364  {{end}}
  1365  }
  1366  
  1367  var benchmarks = []testing.InternalBenchmark{
  1368  {{range .Benchmarks}}
  1369  	{"{{.Name}}", {{.Package}}.{{.Name}}},
  1370  {{end}}
  1371  }
  1372  
  1373  var examples = []testing.InternalExample{
  1374  {{range .Examples}}
  1375  	{"{{.Name}}", {{.Package}}.{{.Name}}, {{.Output | printf "%q"}}},
  1376  {{end}}
  1377  }
  1378  
  1379  var matchPat string
  1380  var matchRe *regexp.Regexp
  1381  
  1382  func matchString(pat, str string) (result bool, err error) {
  1383  	if matchRe == nil || matchPat != pat {
  1384  		matchPat = pat
  1385  		matchRe, err = regexp.Compile(matchPat)
  1386  		if err != nil {
  1387  			return
  1388  		}
  1389  	}
  1390  	return matchRe.MatchString(str), nil
  1391  }
  1392  
  1393  {{if .CoverEnabled}}
  1394  
  1395  // Only updated by init functions, so no need for atomicity.
  1396  var (
  1397  	coverCounters = make(map[string][]uint32)
  1398  	coverBlocks = make(map[string][]testing.CoverBlock)
  1399  )
  1400  
  1401  func init() {
  1402  	{{range $i, $p := .Cover}}
  1403  	{{range $file, $cover := $p.Vars}}
  1404  	coverRegisterFile({{printf "%q" $cover.File}}, _cover{{$i}}.{{$cover.Var}}.Count[:], _cover{{$i}}.{{$cover.Var}}.Pos[:], _cover{{$i}}.{{$cover.Var}}.NumStmt[:])
  1405  	{{end}}
  1406  	{{end}}
  1407  }
  1408  
  1409  func coverRegisterFile(fileName string, counter []uint32, pos []uint32, numStmts []uint16) {
  1410  	if 3*len(counter) != len(pos) || len(counter) != len(numStmts) {
  1411  		panic("coverage: mismatched sizes")
  1412  	}
  1413  	if coverCounters[fileName] != nil {
  1414  		// Already registered.
  1415  		return
  1416  	}
  1417  	coverCounters[fileName] = counter
  1418  	block := make([]testing.CoverBlock, len(counter))
  1419  	for i := range counter {
  1420  		block[i] = testing.CoverBlock{
  1421  			Line0: pos[3*i+0],
  1422  			Col0: uint16(pos[3*i+2]),
  1423  			Line1: pos[3*i+1],
  1424  			Col1: uint16(pos[3*i+2]>>16),
  1425  			Stmts: numStmts[i],
  1426  		}
  1427  	}
  1428  	coverBlocks[fileName] = block
  1429  }
  1430  {{end}}
  1431  
  1432  func main() {
  1433  {{if .CoverEnabled}}
  1434  	testing.RegisterCover(testing.Cover{
  1435  		Mode: {{printf "%q" .CoverMode}},
  1436  		Counters: coverCounters,
  1437  		Blocks: coverBlocks,
  1438  		CoveredPackages: {{printf "%q" .Covered}},
  1439  	})
  1440  {{end}}
  1441  	m := testing.MainStart(matchString, tests, benchmarks, examples)
  1442  {{with .TestMain}}
  1443  	{{.Package}}.{{.Name}}(m)
  1444  {{else}}
  1445  	os.Exit(m.Run())
  1446  {{end}}
  1447  }
  1448  
  1449  `))