1
0
mirror of https://github.com/golang/go synced 2024-11-23 09:40:04 -07:00

cmd/go: version command should error when given bad args

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>
This commit is contained in:
Daniel Martí 2020-02-27 16:19:06 +00:00
parent 18053b7131
commit b8f54e57c2
2 changed files with 18 additions and 0 deletions

View 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() {

View 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.