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