github.com/yukk001/go1.10.8@v0.0.0-20190813125351-6df2d3982e20/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  	{Name: "json", BoolVar: &testJSON},
    37  	{Name: "vet"},
    38  
    39  	// Passed to 6.out, adding a "test." prefix to the name if necessary: -v becomes -test.v.
    40  	{Name: "bench", PassToTest: true},
    41  	{Name: "benchmem", BoolVar: new(bool), PassToTest: true},
    42  	{Name: "benchtime", PassToTest: true},
    43  	{Name: "blockprofile", PassToTest: true},
    44  	{Name: "blockprofilerate", PassToTest: true},
    45  	{Name: "count", PassToTest: true},
    46  	{Name: "coverprofile", PassToTest: true},
    47  	{Name: "cpu", PassToTest: true},
    48  	{Name: "cpuprofile", PassToTest: true},
    49  	{Name: "failfast", BoolVar: new(bool), PassToTest: true},
    50  	{Name: "list", PassToTest: true},
    51  	{Name: "memprofile", PassToTest: true},
    52  	{Name: "memprofilerate", PassToTest: true},
    53  	{Name: "mutexprofile", PassToTest: true},
    54  	{Name: "mutexprofilefraction", PassToTest: true},
    55  	{Name: "outputdir", PassToTest: true},
    56  	{Name: "parallel", PassToTest: true},
    57  	{Name: "run", PassToTest: true},
    58  	{Name: "short", BoolVar: new(bool), PassToTest: true},
    59  	{Name: "timeout", PassToTest: true},
    60  	{Name: "trace", PassToTest: true},
    61  	{Name: "v", BoolVar: &testV, PassToTest: true},
    62  }
    63  
    64  // add build flags to testFlagDefn
    65  func init() {
    66  	var cmd base.Command
    67  	work.AddBuildFlags(&cmd)
    68  	cmd.Flag.VisitAll(func(f *flag.Flag) {
    69  		if f.Name == "v" {
    70  			// test overrides the build -v flag
    71  			return
    72  		}
    73  		testFlagDefn = append(testFlagDefn, &cmdflag.Defn{
    74  			Name:  f.Name,
    75  			Value: f.Value,
    76  		})
    77  	})
    78  }
    79  
    80  // testFlags processes the command line, grabbing -x and -c, rewriting known flags
    81  // to have "test" before them, and reading the command line for the 6.out.
    82  // Unfortunately for us, we need to do our own flag processing because go test
    83  // grabs some flags but otherwise its command line is just a holding place for
    84  // pkg.test's arguments.
    85  // We allow known flags both before and after the package name list,
    86  // to allow both
    87  //	go test fmt -custom-flag-for-fmt-test
    88  //	go test -x math
    89  func testFlags(args []string) (packageNames, passToTest []string) {
    90  	inPkg := false
    91  	var explicitArgs []string
    92  	for i := 0; i < len(args); i++ {
    93  		if !strings.HasPrefix(args[i], "-") {
    94  			if !inPkg && packageNames == nil {
    95  				// First package name we've seen.
    96  				inPkg = true
    97  			}
    98  			if inPkg {
    99  				packageNames = append(packageNames, args[i])
   100  				continue
   101  			}
   102  		}
   103  
   104  		if inPkg {
   105  			// Found an argument beginning with "-"; end of package list.
   106  			inPkg = false
   107  		}
   108  
   109  		f, value, extraWord := cmdflag.Parse(cmd, testFlagDefn, args, i)
   110  		if f == nil {
   111  			// This is a flag we do not know; we must assume
   112  			// that any args we see after this might be flag
   113  			// arguments, not package names.
   114  			inPkg = false
   115  			if packageNames == nil {
   116  				// make non-nil: we have seen the empty package list
   117  				packageNames = []string{}
   118  			}
   119  			if args[i] == "-args" || args[i] == "--args" {
   120  				// -args or --args signals that everything that follows
   121  				// should be passed to the test.
   122  				explicitArgs = args[i+1:]
   123  				break
   124  			}
   125  			passToTest = append(passToTest, args[i])
   126  			continue
   127  		}
   128  		if f.Value != nil {
   129  			if err := f.Value.Set(value); err != nil {
   130  				base.Fatalf("invalid flag argument for -%s: %v", f.Name, err)
   131  			}
   132  		} else {
   133  			// Test-only flags.
   134  			// Arguably should be handled by f.Value, but aren't.
   135  			switch f.Name {
   136  			// bool flags.
   137  			case "c", "i", "v", "cover", "json":
   138  				cmdflag.SetBool(cmd, f.BoolVar, value)
   139  				if f.Name == "json" && testJSON {
   140  					passToTest = append(passToTest, "-test.v=true")
   141  				}
   142  			case "o":
   143  				testO = value
   144  				testNeedBinary = true
   145  			case "exec":
   146  				xcmd, err := str.SplitQuotedFields(value)
   147  				if err != nil {
   148  					base.Fatalf("invalid flag argument for -%s: %v", f.Name, err)
   149  				}
   150  				work.ExecCmd = xcmd
   151  			case "bench":
   152  				// record that we saw the flag; don't care about the value
   153  				testBench = true
   154  			case "list":
   155  				testList = true
   156  			case "timeout":
   157  				testTimeout = value
   158  			case "blockprofile", "cpuprofile", "memprofile", "mutexprofile":
   159  				testProfile = "-" + f.Name
   160  				testNeedBinary = true
   161  			case "trace":
   162  				testProfile = "-trace"
   163  			case "coverpkg":
   164  				testCover = true
   165  				if value == "" {
   166  					testCoverPaths = nil
   167  				} else {
   168  					testCoverPaths = strings.Split(value, ",")
   169  				}
   170  			case "coverprofile":
   171  				testCover = true
   172  				testCoverProfile = value
   173  			case "covermode":
   174  				switch value {
   175  				case "set", "count", "atomic":
   176  					testCoverMode = value
   177  				default:
   178  					base.Fatalf("invalid flag argument for -covermode: %q", value)
   179  				}
   180  				testCover = true
   181  			case "outputdir":
   182  				testOutputDir = value
   183  			case "vet":
   184  				testVetList = value
   185  			}
   186  		}
   187  		if extraWord {
   188  			i++
   189  		}
   190  		if f.PassToTest {
   191  			passToTest = append(passToTest, "-test."+f.Name+"="+value)
   192  		}
   193  	}
   194  
   195  	if testCoverMode == "" {
   196  		testCoverMode = "set"
   197  		if cfg.BuildRace {
   198  			// Default coverage mode is atomic when -race is set.
   199  			testCoverMode = "atomic"
   200  		}
   201  	}
   202  
   203  	if testVetList != "" && testVetList != "off" {
   204  		if strings.Contains(testVetList, "=") {
   205  			base.Fatalf("-vet argument cannot contain equal signs")
   206  		}
   207  		if strings.Contains(testVetList, " ") {
   208  			base.Fatalf("-vet argument is comma-separated list, cannot contain spaces")
   209  		}
   210  		list := strings.Split(testVetList, ",")
   211  		for i, arg := range list {
   212  			list[i] = "-" + arg
   213  		}
   214  		testVetFlags = list
   215  	}
   216  
   217  	if cfg.BuildRace && testCoverMode != "atomic" {
   218  		base.Fatalf(`-covermode must be "atomic", not %q, when -race is enabled`, testCoverMode)
   219  	}
   220  
   221  	// Tell the test what directory we're running in, so it can write the profiles there.
   222  	if testProfile != "" && testOutputDir == "" {
   223  		dir, err := os.Getwd()
   224  		if err != nil {
   225  			base.Fatalf("error from os.Getwd: %s", err)
   226  		}
   227  		passToTest = append(passToTest, "-test.outputdir", dir)
   228  	}
   229  
   230  	passToTest = append(passToTest, explicitArgs...)
   231  	return
   232  }