Skip to content

Commit 6a167c7

Browse files
cmd/go: display test binary output if invoked with -help
Fixes #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]>
1 parent 20afbe8 commit 6a167c7

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

Diff for: src/cmd/go/internal/test/test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,8 @@ var (
487487
pkgArgs []string
488488
pkgs []*load.Package
489489

490+
testHelp bool // -help option passed to test via -args
491+
490492
testKillTimeout = 100 * 365 * 24 * time.Hour // backup alarm; defaults to about a century if no timeout is set
491493
testCacheExpire time.Time // ignore cached test results before this time
492494

@@ -532,7 +534,7 @@ func testNeedBinary() bool {
532534

533535
// testShowPass reports whether the output for a passing test should be shown.
534536
func testShowPass() bool {
535-
return testV || (testList != "")
537+
return testV || (testList != "") || testHelp
536538
}
537539

538540
var defaultVetFlags = []string{

Diff for: src/cmd/go/internal/test/testflag.go

+17
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,23 @@ func testFlags(args []string) (packageNames, passToTest []string) {
333333
injectedFlags = append(injectedFlags, "-test.outputdir="+testOutputDir)
334334
}
335335

336+
// If the user is explicitly passing -help or -h, show output
337+
// of the test binary so that the help output is displayed
338+
// even though the test will exit with success.
339+
// This loop is imperfect: it will do the wrong thing for a case
340+
// like -args -test.outputdir -help. Such cases are probably rare,
341+
// and getting this wrong doesn't do too much harm.
342+
helpLoop:
343+
for _, arg := range explicitArgs {
344+
switch arg {
345+
case "--":
346+
break helpLoop
347+
case "-h", "-help", "--help":
348+
testHelp = true
349+
break helpLoop
350+
}
351+
}
352+
336353
// Ensure that -race and -covermode are compatible.
337354
if testCoverMode == "" {
338355
testCoverMode = "set"

Diff for: src/cmd/go/testdata/script/test_flags.txt

+6
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ stderr -count=1 'invalid value "walrus" for flag -covermode: valid modes are .*$
5757
stderr '^usage: go test .*$'
5858
stderr '^Run ''go help test'' and ''go help testflag'' for details.$'
5959

60+
# Passing -help to the test binary should show flag help.
61+
go test ./x -args -help
62+
stdout 'usage_message'
63+
6064
# -covermode, -coverpkg, and -coverprofile should imply -cover
6165
go test -covermode=set ./x
6266
stdout '\s+coverage:\s+'
@@ -98,6 +102,8 @@ import (
98102
"testing"
99103
)
100104

105+
var _ = flag.String("usage_message", "", "dummy flag to check usage message")
106+
101107
func TestLogTimeout(t *testing.T) {
102108
t.Log(flag.Lookup("test.timeout").Value)
103109
}

0 commit comments

Comments
 (0)