From 6eaff580987831b0725124de60be053f24f31582 Mon Sep 17 00:00:00 2001 From: Dave Cheney Date: Tue, 14 Jun 2016 16:29:23 +1000 Subject: [PATCH] cmd/gb: expand boolean flags when passed to test subprocess (#608) Fixes #605 Goconvey, and probably others, do not parse flags, they just string match against `os.Args`. Because of this the semantically identical, `-test.v` and `-test.v=true` are not identical. Fix this by expanding boolean arguments that are passed to the test subprocess. --- cmd/gb/gb_test.go | 31 +++++++++++++++++++++++++++++++ cmd/gb/testflag.go | 7 +++++++ cmd/gb/testflag_test.go | 8 ++++---- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/cmd/gb/gb_test.go b/cmd/gb/gb_test.go index b617df9..b74d28b 100644 --- a/cmd/gb/gb_test.go +++ b/cmd/gb/gb_test.go @@ -1661,3 +1661,34 @@ func main() { gb.run("build") gb.mustBeEmpty(tmpdir) } + +// goconvey (and probably others) do not parse flags passed to the +// test binary, they just expect them to be present in their raw form +// in os.Args. As such -test.v and -test.v=true are not the same. +// Assert that gb is passing the latter form. +func TestIssue605(t *testing.T) { + gb := T{T: t} + defer gb.cleanup() + + gb.tempDir("src/issue605") + gb.tempFile("src/issue605/issue_test.go", `package issue605 + +import ( + "os" + "testing" +) + +func TestFlags(t *testing.T) { + for _, f := range os.Args { + if f == "-test.v=true" { + return + } + } + t.Fatalf("could not find test flag: %q", os.Args) +}`) + gb.cd(gb.tempdir) + tmpdir := gb.tempDir("tmp") + gb.run("test", "-v") // should translate into -test.v=true + gb.mustBeEmpty(tmpdir) + +} diff --git a/cmd/gb/testflag.go b/cmd/gb/testflag.go index 6190612..3bb4bb6 100644 --- a/cmd/gb/testflag.go +++ b/cmd/gb/testflag.go @@ -75,6 +75,13 @@ func TestFlags(testArgs []string) []string { } if val.passToTest || val.passToAll { fArg = "-test." + nArg + if val.boolVar { + // boolean variables can be either -bool, or -bool=true + // some code, see issue 605, expects the latter form, so + // when present, expand boolean args to their canonical + // form. + nVal = "true" + } if nVal != "" { fArg = fArg + "=" + nVal } diff --git a/cmd/gb/testflag_test.go b/cmd/gb/testflag_test.go index 425bcdf..14830bf 100644 --- a/cmd/gb/testflag_test.go +++ b/cmd/gb/testflag_test.go @@ -146,10 +146,10 @@ func TestTestFlags(t *testing.T) { }{ { eargs: []string{"-q", "-debug"}, - targs: []string{"-test.v", "-debug"}, + targs: []string{"-test.v=true", "-debug"}, }, { eargs: []string{"-v", "-debug"}, - targs: []string{"-test.v", "-debug"}, + targs: []string{"-test.v=true", "-debug"}, }, { eargs: []string{"-bench"}, targs: []string{"-test.bench"}, @@ -161,7 +161,7 @@ func TestTestFlags(t *testing.T) { targs: []string{"-test.bench='Test*'"}, }, { eargs: []string{"-benchmem"}, - targs: []string{"-test.benchmem"}, + targs: []string{"-test.benchmem=true"}, }, { eargs: []string{"-benchtime"}, targs: []string{"-test.benchtime"}, @@ -188,7 +188,7 @@ func TestTestFlags(t *testing.T) { targs: []string{"-test.memprofile"}, }, { eargs: []string{"-short"}, - targs: []string{"-test.short"}, + targs: []string{"-test.short=true"}, }, { eargs: []string{"-memprofilerate", "1"}, targs: []string{"-test.memprofilerate", "1"},