github.com/tidwall/go@v0.0.0-20170415222209-6694a6888b7d/src/cmd/go/internal/test/testflag.go (about)

     1  // Copyright 2011 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package test
     6  
     7  import (
     8  	"flag"
     9  	"os"
    10  	"strings"
    11  
    12  	"cmd/go/internal/base"
    13  	"cmd/go/internal/cfg"
    14  	"cmd/go/internal/cmdflag"
    15  	"cmd/go/internal/str"
    16  	"cmd/go/internal/work"
    17  )
    18  
    19  const cmd = "test"
    20  
    21  // The flag handling part of go test is large and distracting.
    22  // We can't use the flag package because some of the flags from
    23  // our command line are for us, and some are for 6.out, and
    24  // some are for both.
    25  
    26  // testFlagDefn is the set of flags we process.
    27  var testFlagDefn = []*cmdflag.Defn{
    28  	// local.
    29  	{Name: "c", BoolVar: &testC},
    30  	{Name: "i", BoolVar: &cfg.BuildI},
    31  	{Name: "o"},
    32  	{Name: "cover", BoolVar: &testCover},
    33  	{Name: "covermode"},
    34  	{Name: "coverpkg"},
    35  	{Name: "exec"},
    36  
    37  	// Passed to 6.out, adding a "test." prefix to the name if necessary: -v becomes -test.v.
    38  	{Name: "bench", PassToTest: true},
    39  	{Name: "benchmem", BoolVar: new(bool), PassToTest: true},
    40  	{Name: "benchtime", PassToTest: true},
    41  	{Name: "count", PassToTest: true},
    42  	{Name: "coverprofile", PassToTest: true},
    43  	{Name: "cpu", PassToTest: true},
    44  	{Name: "cpuprofile", PassToTest: true},
    45  	{Name: "memprofile", PassToTest: true},
    46  	{Name: "memprofilerate", PassToTest: true},
    47  	{Name: "blockprofile", PassToTest: true},
    48  	{Name: "blockprofilerate", PassToTest: true},
    49  	{Name: "mutexprofile", PassToTest: true},
    50  	{Name: "mutexprofilefraction", PassToTest: true},
    51  	{Name: "outputdir", PassToTest: true},
    52  	{Name: "parallel", PassToTest: true},
    53  	{Name: "run", PassToTest: true},
    54  	{Name: "short", BoolVar: new(bool), PassToTest: true},
    55  	{Name: "timeout", PassToTest: true},
    56  	{Name: "trace", PassToTest: true},
    57  	{Name: "v", BoolVar: &testV, PassToTest: true},
    58  }
    59  
    60  // add build flags to testFlagDefn
    61  func init() {
    62  	var cmd base.Command
    63  	work.AddBuildFlags(&cmd)
    64  	cmd.Flag.VisitAll(func(f *flag.Flag) {
    65  		if f.Name == "v" {
    66  			// test overrides the build -v flag
    67  			return
    68  		}
    69  		testFlagDefn = append(testFlagDefn, &cmdflag.Defn{
    70  			Name:  f.Name,
    71  			Value: f.Value,
    72  		})
    73  	})
    74  }
    75  
    76  // testFlags processes the command line, grabbing -x and -c, rewriting known flags
    77  // to have "test" before them, and reading the command line for the 6.out.
    78  // Unfortunately for us, we need to do our own flag processing because go test
    79  // grabs some flags but otherwise its command line is just a holding place for
    80  // pkg.test's arguments.
    81  // We allow known flags both before and after the package name list,
    82  // to allow both
    83  //	go test fmt -custom-flag-for-fmt-test
    84  //	go test -x math
    85  func testFlags(args []string) (packageNames, passToTest []string) {
    86  	inPkg := false
    87  	outputDir := ""
    88  	var explicitArgs []string
    89  	for i := 0; i < len(args); i++ {
    90  		if !strings.HasPrefix(args[i], "-") {
    91  			if !inPkg && packageNames == nil {
    92  				// First package name we've seen.
    93  				inPkg = true
    94  			}
    95  			if inPkg {
    96  				packageNames = append(packageNames, args[i])
    97  				continue
    98  			}
    99  		}
   100  
   101  		if inPkg {
   102  			// Found an argument beginning with "-"; end of package list.
   103  			inPkg = false
   104  		}
   105  
   106  		f, value, extraWord := cmdflag.Parse(cmd, testFlagDefn, args, i)
   107  		if f == nil {
   108  			// This is a flag we do not know; we must assume
   109  			// that any args we see after this might be flag
   110  			// arguments, not package names.
   111  			inPkg = false
   112  			if packageNames == nil {
   113  				// make non-nil: we have seen the empty package list
   114  				packageNames = []string{}
   115  			}
   116  			if args[i] == "-args" || args[i] == "--args" {
   117  				// -args or --args signals that everything that follows
   118  				// should be passed to the test.
   119  				explicitArgs = args[i+1:]
   120  				break
   121  			}
   122  			passToTest = append(passToTest, args[i])
   123  			continue
   124  		}
   125  		if f.Value != nil {
   126  			if err := f.Value.Set(value); err != nil {
   127  				base.Fatalf("invalid flag argument for -%s: %v", f.Name, err)
   128  			}
   129  		} else {
   130  			// Test-only flags.
   131  			// Arguably should be handled by f.Value, but aren't.
   132  			switch f.Name {
   133  			// bool flags.
   134  			case "c", "i", "v", "cover":
   135  				cmdflag.SetBool(cmd, f.BoolVar, value)
   136  			case "o":
   137  				testO = value
   138  				testNeedBinary = true
   139  			case "exec":
   140  				xcmd, err := str.SplitQuotedFields(value)
   141  				if err != nil {
   142  					base.Fatalf("invalid flag argument for -%s: %v", f.Name, err)
   143  				}
   144  				work.ExecCmd = xcmd
   145  			case "bench":
   146  				// record that we saw the flag; don't care about the value
   147  				testBench = true
   148  			case "timeout":
   149  				testTimeout = value
   150  			case "blockprofile", "cpuprofile", "memprofile", "mutexprofile":
   151  				testProfile = true
   152  				testNeedBinary = true
   153  			case "trace":
   154  				testProfile = true
   155  			case "coverpkg":
   156  				testCover = true
   157  				if value == "" {
   158  					testCoverPaths = nil
   159  				} else {
   160  					testCoverPaths = strings.Split(value, ",")
   161  				}
   162  			case "coverprofile":
   163  				testCover = true
   164  				testProfile = true
   165  			case "covermode":
   166  				switch value {
   167  				case "set", "count", "atomic":
   168  					testCoverMode = value
   169  				default:
   170  					base.Fatalf("invalid flag argument for -covermode: %q", value)
   171  				}
   172  				testCover = true
   173  			case "outputdir":
   174  				outputDir = value
   175  			}
   176  		}
   177  		if extraWord {
   178  			i++
   179  		}
   180  		if f.PassToTest {
   181  			passToTest = append(passToTest, "-test."+f.Name+"="+value)
   182  		}
   183  	}
   184  
   185  	if testCoverMode == "" {
   186  		testCoverMode = "set"
   187  		if cfg.BuildRace {
   188  			// Default coverage mode is atomic when -race is set.
   189  			testCoverMode = "atomic"
   190  		}
   191  	}
   192  
   193  	// Tell the test what directory we're running in, so it can write the profiles there.
   194  	if testProfile && outputDir == "" {
   195  		dir, err := os.Getwd()
   196  		if err != nil {
   197  			base.Fatalf("error from os.Getwd: %s", err)
   198  		}
   199  		passToTest = append(passToTest, "-test.outputdir", dir)
   200  	}
   201  
   202  	passToTest = append(passToTest, explicitArgs...)
   203  	return
   204  }