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