]> Cypherpunks.ru repositories - gostls13.git/commitdiff
testing: do not print 'root' benchName during test of package testing
authorRuss Cox <rsc@golang.org>
Fri, 5 Aug 2022 18:30:20 +0000 (14:30 -0400)
committerRuss Cox <rsc@golang.org>
Tue, 16 Aug 2022 14:36:05 +0000 (14:36 +0000)
$ cd $GOROOT/src/testing
$ go test
root
root
root
root
PASS
$

The root prints have been happening since Go 1.14.
There is a test in sub_test.go that calls b.Run directly
with a benchmark named "root", which triggers the print.
Silence them.

Change-Id: I2f0c186f04c6139bc24fab0e91975fcf0a8e80fc
Reviewed-on: https://go-review.googlesource.com/c/go/+/421437
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/testing/benchmark.go
src/testing/sub_test.go

index d8ec217f66c628e0950a6223dd0a67e12625f8a0..4fee421d391dfdaa40334ea05be0f5e560473cdb 100644 (file)
@@ -615,6 +615,11 @@ func (ctx *benchContext) processBench(b *B) {
        }
 }
 
+// If hideStdoutForTesting is true, Run does not print the benchName.
+// This avoids a spurious print during 'go test' on package testing itself,
+// which invokes b.Run in its own tests (see sub_test.go).
+var hideStdoutForTesting = false
+
 // Run benchmarks f as a subbenchmark with the given name. It reports
 // whether there were any failures.
 //
@@ -670,7 +675,9 @@ func (b *B) Run(name string, f func(b *B)) bool {
                        }
                })
 
-               fmt.Println(benchName)
+               if !hideStdoutForTesting {
+                       fmt.Println(benchName)
+               }
        }
 
        if sub.run1() {
index 6324d463cf08d9301575b515efaeeef0d7127694..bb1715b23f4197dcf9223ed0e056d184ddd5d118 100644 (file)
@@ -657,6 +657,10 @@ func TestBRun(t *T) {
                        }
                },
        }}
+       hideStdoutForTesting = true
+       defer func() {
+               hideStdoutForTesting = false
+       }()
        for _, tc := range testCases {
                t.Run(tc.desc, func(t *T) {
                        var ok bool