]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: version command should error when given bad args
authorDaniel Martí <mvdan@mvdan.cc>
Thu, 27 Feb 2020 16:19:06 +0000 (16:19 +0000)
committerDaniel Martí <mvdan@mvdan.cc>
Fri, 28 Feb 2020 09:29:22 +0000 (09:29 +0000)
For example, 'go version -m' happily gives you Go's own version, even
though the -m flag only makes sense when grabbing the version of a
binary on disk.

Similarly, if any of the directly named files can't be found, the tool
would succeed. That's acceptable if an error is encountered while
walking a large directory, but not when locating a path directly given
by the user.

These added test cases run even in short mode, as 'go build' is not
needed for them.

Change-Id: I7bb40b72853799e31d9f86cc5e999c8d57813eef
Reviewed-on: https://go-review.googlesource.com/c/go/+/221397
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
src/cmd/go/internal/version/version.go
src/cmd/go/testdata/script/version.txt

index 857548c7ed8d41ddde7399242487527064723613..ac2ae501552926258ef63261be6fc0d92f8f170d 100644 (file)
@@ -53,6 +53,11 @@ var (
 
 func runVersion(cmd *base.Command, args []string) {
        if len(args) == 0 {
+               if *versionM || *versionV {
+                       fmt.Fprintf(os.Stderr, "go version: flags can only be used with arguments\n")
+                       base.SetExitStatus(2)
+                       return
+               }
                fmt.Printf("go version %s %s/%s\n", runtime.Version(), runtime.GOOS, runtime.GOARCH)
                return
        }
@@ -61,6 +66,7 @@ func runVersion(cmd *base.Command, args []string) {
                info, err := os.Stat(arg)
                if err != nil {
                        fmt.Fprintf(os.Stderr, "%v\n", err)
+                       base.SetExitStatus(1)
                        continue
                }
                if info.IsDir() {
index 4eafe1f1845327d8ab698ad0946272956739c314..0ed1194840773416cf5ce91cc518c2d2378b1059 100644 (file)
@@ -1,4 +1,16 @@
+# Without arguments, we just print Go's own version.
+go version
+stdout '^go version'
+
+# Flags without files, or paths to misisng files, should error.
+! go version missing.exe
+! go version -m
+stderr 'with arguments'
+! go version -v
+stderr 'with arguments'
+
 env GO111MODULE=on
+# Skip the builds below if we are running in short mode.
 [short] skip
 
 # Check that 'go version' and 'go version -m' work on a binary built in module mode.