Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/go: test hides -v from underlying binary #7221

Closed
robpike opened this issue Jan 27, 2014 · 5 comments
Closed

cmd/go: test hides -v from underlying binary #7221

robpike opened this issue Jan 27, 2014 · 5 comments
Milestone

Comments

@robpike
Copy link
Contributor

robpike commented Jan 27, 2014

Go help test says:

The go tool treats as a flag the first argument that begins with
a minus sign that it does not recognize itself; that argument and all subsequent
arguments are passed as arguments to the test binary.

But this isn't quite true. Given

package foo

import (
    "flag"
    "testing"
)

var v = flag.Int("v", 23, "test")

func TestFlag(t *testing.T) {
    println(*v)
}

One would expect

$ go test -- -v 27

to print 27, but it prints 23.


$ go test -- -v 27
23
PASS
ok      _/Users/r/foo   0.026s
$ 

Should probably fix this for 1.3, since the issue of how to pass flags to tests comes up
a lot. In particular, github.com/golang/glog has a -v flag that this bug prevents
setting, at least directly. Here's a workaround:

var myV = flag.Int("myV", 23, "test")

func TestFlag(t *testing.T) {
  flag.Lookup("v").Value.Set(fmt.Sprint(*myV))
  println(*v, *myV)
}
@robpike
Copy link
Contributor Author

robpike commented Jan 29, 2014

Comment 1:

Issue #7235 has been merged into this issue.

@rsc
Copy link
Contributor

rsc commented Feb 10, 2014

Comment 2:

I do not believe the -- fix is correct. I expect 
go test foo -- -v 27
to invoke:
  foo.test -- -v 27
The shadowing of -v is fundamental to the design of the merged command line that 'go
test' accepts. Renaming the test's flag is the easiest fix. I believe the issue as
stated (cmd/go: test hides -v from underlying binary) is working as intended.
If for some reason a test needs to use the same flag as a standard 'go test' option, a
workaround is to take advantage of the fact that an unrecognized flag marks the end of
the 'go test' flags, and put this in the test:
var myflags = flag.Bool("myflags", false, "unused")
and then invoke
go test -myflags -v

@rsc
Copy link
Contributor

rsc commented Apr 3, 2014

Comment 3:

Status changed to WorkingAsIntended.

@robpike
Copy link
Contributor Author

robpike commented Apr 7, 2014

Comment 4:

I have no idea why this is 'working as intended' since it's not working and that is not
intended.

@gopherbot
Copy link

CL https://golang.org/cl/17775 mentions this issue.

mk0x9 pushed a commit to mk0x9/go that referenced this issue Dec 17, 2015
The new flag -args stops flag processing, leaving the rest of the command line
to be passed to the underlying test binary verbatim. Thus, both of these pass
a literal -v -n on the test binary command line, without putting the go command
into verbose mode or disabling execution of commands:

	go test . -args -v -n
	go test -args -v -n

Also try to make the documentation a bit clearer.

Fixes golang#7221.
Fixes golang#12177.

Change-Id: Ief9e830a6fbb9475d96011716a86e2524a35eceb
Reviewed-on: https://go-review.googlesource.com/17775
Reviewed-by: Rob Pike <r@golang.org>
@golang golang locked and limited conversation to collaborators Dec 14, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants