Skip to content

Commit

Permalink
cmd/go: display test binary output if invoked with -help
Browse files Browse the repository at this point in the history
Fixes golang#39997

Change-Id: I87ea616bac809b96fcd40f3bbdbbf1c603b9d00e
Reviewed-on: https://go-review.googlesource.com/c/go/+/240878
Run-TryBot: Ian Lance Taylor <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
Reviewed-by: Jay Conrod <[email protected]>
  • Loading branch information
ianlancetaylor committed Jul 6, 2020
1 parent 20afbe8 commit 6a167c7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/cmd/go/internal/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,8 @@ var (
pkgArgs []string
pkgs []*load.Package

testHelp bool // -help option passed to test via -args

testKillTimeout = 100 * 365 * 24 * time.Hour // backup alarm; defaults to about a century if no timeout is set
testCacheExpire time.Time // ignore cached test results before this time

Expand Down Expand Up @@ -532,7 +534,7 @@ func testNeedBinary() bool {

// testShowPass reports whether the output for a passing test should be shown.
func testShowPass() bool {
return testV || (testList != "")
return testV || (testList != "") || testHelp
}

var defaultVetFlags = []string{
Expand Down
17 changes: 17 additions & 0 deletions src/cmd/go/internal/test/testflag.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,23 @@ func testFlags(args []string) (packageNames, passToTest []string) {
injectedFlags = append(injectedFlags, "-test.outputdir="+testOutputDir)
}

// If the user is explicitly passing -help or -h, show output
// of the test binary so that the help output is displayed
// even though the test will exit with success.
// This loop is imperfect: it will do the wrong thing for a case
// like -args -test.outputdir -help. Such cases are probably rare,
// and getting this wrong doesn't do too much harm.
helpLoop:
for _, arg := range explicitArgs {
switch arg {
case "--":
break helpLoop
case "-h", "-help", "--help":
testHelp = true
break helpLoop
}
}

// Ensure that -race and -covermode are compatible.
if testCoverMode == "" {
testCoverMode = "set"
Expand Down
6 changes: 6 additions & 0 deletions src/cmd/go/testdata/script/test_flags.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ stderr -count=1 'invalid value "walrus" for flag -covermode: valid modes are .*$
stderr '^usage: go test .*$'
stderr '^Run ''go help test'' and ''go help testflag'' for details.$'

# Passing -help to the test binary should show flag help.
go test ./x -args -help
stdout 'usage_message'

# -covermode, -coverpkg, and -coverprofile should imply -cover
go test -covermode=set ./x
stdout '\s+coverage:\s+'
Expand Down Expand Up @@ -98,6 +102,8 @@ import (
"testing"
)

var _ = flag.String("usage_message", "", "dummy flag to check usage message")

func TestLogTimeout(t *testing.T) {
t.Log(flag.Lookup("test.timeout").Value)
}

0 comments on commit 6a167c7

Please sign in to comment.