]> Cypherpunks repositories - gostls13.git/commitdiff
testing: don't warn if -bench was passed
authorDaniel Martí <mvdan@mvdan.cc>
Thu, 27 Oct 2016 18:47:47 +0000 (19:47 +0100)
committerMarcel van Lohuizen <mpvl@golang.org>
Tue, 1 Nov 2016 13:13:18 +0000 (13:13 +0000)
In a previous change, cmd/go was taught to show a "no tests ran" warning
if test did nothing. But it missed a case - if no tests nor examples ran
but any benchmarks were meant to be run, it would still produce the
warning. This meant that running only benchmarks, which is common, would
be confusing:

 $ go test -run='^$' -bench=.
testing: warning: no tests to run
BenchmarkFoo-4            300000              5056 ns/op
[...]

I believe this was because of a copy-paste error in the tests. This was
being tested, but on the wrong file which does contain a test that was
being run. Fix the path and fix the now failing test by never showing
the warning if -bench was given a non-empty string.

The rationale is that if -bench was given but there was no output, it's
obvious that nothing happened as benchmarks always produce output even
without -v. So showing a warning in those cases is redundant.

To make future typos less likely, make sure that no tests are being run
in the cases where we only want to run benchmarks.

Fixes #17603.

Change-Id: I4c626caf39f72260c6a9761c06446663f465f947
Reviewed-on: https://go-review.googlesource.com/32157
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
Run-TryBot: Marcel van Lohuizen <mpvl@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/cmd/go/go_test.go
src/testing/testing.go

index 93639ee5ea4cb048da846568d1b49b288b6bf388..89916f371a85536ac3a1f055d16fe01f2c6eec73 100644 (file)
@@ -3161,7 +3161,7 @@ func TestIssue17119(t *testing.T) {
 func TestFatalInBenchmarkCauseNonZeroExitStatus(t *testing.T) {
        tg := testgo(t)
        defer tg.cleanup()
-       tg.runFail("test", "-bench", ".", "./testdata/src/benchfatal")
+       tg.runFail("test", "-run", "^$", "-bench", ".", "./testdata/src/benchfatal")
        tg.grepBothNot("^ok", "test passed unexpectedly")
        tg.grepBoth("FAIL.*benchfatal", "test did not run everything")
 }
@@ -3357,11 +3357,12 @@ func TestMatchesNoTestsDoesNotOverrideBuildFailure(t *testing.T) {
        tg.grepBoth("FAIL", "go test did not say FAIL")
 }
 
-func TestMatchesNoBenchmarks(t *testing.T) {
+func TestMatchesNoBenchmarksIsOK(t *testing.T) {
        tg := testgo(t)
        defer tg.cleanup()
-       tg.run("test", "-bench", "ThisWillNotMatch", "testdata/standalone_benchmark_test.go")
-       tg.grepBoth(noMatchesPattern, "go test did not say [no tests to run]")
+       tg.run("test", "-run", "^$", "-bench", "ThisWillNotMatch", "testdata/standalone_benchmark_test.go")
+       tg.grepBothNot(noMatchesPattern, "go test did say [no tests to run]")
+       tg.grepBoth(okPattern, "go test did not say ok")
 }
 
 func TestMatchesOnlyExampleIsOK(t *testing.T) {
@@ -3375,7 +3376,7 @@ func TestMatchesOnlyExampleIsOK(t *testing.T) {
 func TestMatchesOnlyBenchmarkIsOK(t *testing.T) {
        tg := testgo(t)
        defer tg.cleanup()
-       tg.run("test", "-bench", ".", "testdata/standalone_test.go")
+       tg.run("test", "-run", "^$", "-bench", ".", "testdata/standalone_benchmark_test.go")
        tg.grepBothNot(noMatchesPattern, "go test did say [no tests to run]")
        tg.grepBoth(okPattern, "go test did not say ok")
 }
index 3822f8aacbe8f8acbc4e847f56eae8a413a0af16..ce5b85236418c93a25114e95f78c920291a1a4b1 100644 (file)
@@ -772,7 +772,7 @@ func (m *M) Run() int {
        haveExamples = len(m.examples) > 0
        testRan, testOk := runTests(m.matchString, m.tests)
        exampleRan, exampleOk := runExamples(m.matchString, m.examples)
-       if !testRan && !exampleRan {
+       if !testRan && !exampleRan && *matchBenchmarks == "" {
                fmt.Fprintln(os.Stderr, "testing: warning: no tests to run")
        }
        if !testOk || !exampleOk || !runBenchmarks(m.matchString, m.benchmarks) {