]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: error when -c or -i are used with unknown flags
authorDaniel Martí <mvdan@mvdan.cc>
Fri, 12 Jun 2020 14:14:42 +0000 (15:14 +0100)
committerDaniel Martí <mvdan@mvdan.cc>
Thu, 24 Sep 2020 20:27:51 +0000 (20:27 +0000)
Other test flags passed to the test binary, such as -run or -count, are
equally pointless when -c or -i are used, since the test binary is never
run. However, custom flags in that scenario are far more likely to be
due to human error, such as:

# note the "ldflags" typo, which silently did nothing
go test -c -lflags=-w

Instead, make this scenario error. It seems unlikely that anyone is
using -c along with intended custom-defined test flags, and if they are,
removing those extra flags that do nothing is probably a good idea
anyway.

We don't add this restriction for the flags defined in 'go help
testflag', since they are far less likely to be typos or unintended
mistakes. Another reason not to do that change is that other commands
similarly silently ignore no-op flags, such as:

# -d disables the build, so -ldflags is never used
go get -d -ldflags=-w

Fixes #39484.

Change-Id: I6ba2f6866562fe8f8fceaf4cd862d874bf5cd978
Reviewed-on: https://go-review.googlesource.com/c/go/+/237697
Trust: Daniel Martí <mvdan@mvdan.cc>
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
src/cmd/go/internal/test/testflag.go
src/cmd/go/testdata/script/test_flag.txt

index 4f0a8924f12c0bde08cf9b37a82717b6b9b6dda5..d2671ff5a7925c63c25df4be9b5cae5be9822c21 100644 (file)
@@ -212,6 +212,10 @@ func testFlags(args []string) (packageNames, passToTest []string) {
                }
        })
 
+       // firstUnknownFlag helps us report an error when flags not known to 'go
+       // test' are used along with -i or -c.
+       firstUnknownFlag := ""
+
        explicitArgs := make([]string, 0, len(args))
        inPkgList := false
        afterFlagWithoutValue := false
@@ -288,6 +292,10 @@ func testFlags(args []string) (packageNames, passToTest []string) {
                                break
                        }
 
+                       if firstUnknownFlag == "" {
+                               firstUnknownFlag = nd.RawArg
+                       }
+
                        explicitArgs = append(explicitArgs, nd.RawArg)
                        args = remainingArgs
                        if !nd.HasValue {
@@ -312,6 +320,14 @@ func testFlags(args []string) (packageNames, passToTest []string) {
 
                args = remainingArgs
        }
+       if firstUnknownFlag != "" && (testC || cfg.BuildI) {
+               buildFlag := "-c"
+               if !testC {
+                       buildFlag = "-i"
+               }
+               fmt.Fprintf(os.Stderr, "flag %s is not a 'go test' flag (unknown flags cannot be used with %s)\n", firstUnknownFlag, buildFlag)
+               exitWithUsage()
+       }
 
        var injectedFlags []string
        if testJSON {
index bbcad1c59c6f290fbc48853bed41630613fa634f..ec88d38cbe805f96194d13670e00b4fcd145e837 100644 (file)
@@ -3,6 +3,22 @@
 go test flag_test.go -v -args -v=7 # Two distinct -v flags
 go test -v flag_test.go -args -v=7 # Two distinct -v flags
 
+# Using a custom flag mixed with regular 'go test' flags should be OK.
+go test -count=1 -custom -args -v=7
+
+# However, it should be an error to use custom flags when -i or -c are used,
+# since we know for sure that no test binary will run at all.
+! go test -i -custom
+stderr '^flag -custom is not a ''go test'' flag \(unknown flags cannot be used with -i\)$'
+! go test -c -custom
+stderr '^flag -custom is not a ''go test'' flag \(unknown flags cannot be used with -c\)$'
+
+# The same should apply even if -c or -i come after a custom flag.
+! go test -custom -c
+stderr '^flag -custom is not a ''go test'' flag \(unknown flags cannot be used with -c\)$'
+
+-- go.mod --
+module m
 -- flag_test.go --
 package flag_test
 
@@ -14,6 +30,8 @@ import (
 
 var v = flag.Int("v", 0, "v flag")
 
+var custom = flag.Bool("custom", false, "")
+
 // Run this as go test pkg -v=7
 func TestVFlagIsSet(t *testing.T) {
        if *v != 7 {