From a2a3ace51aa0600765d44a71f47fd830fa6217c5 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Fri, 16 Jun 2017 14:46:24 -0400 Subject: testing: harmonize handling of prefix-matched benchmarks If you have BenchmarkX1 with sub-benchmark Y and you have BenchmarkX2 with no sub-benchmarks, then go test -bench=X/Y runs BenchmarkX1 once with b.N=1 (to find out about Y) and then not again, because it has sub-benchmarks, but arguably also because we're interested in Y. In contrast, it runs BenchmarkX2 in full, even though clearly that is not relevant to the match X/Y. We do have to run X2 once with b.N=1 to probe for having X2/Y, but we should not run it with larger b.N. Fixes #20589. Change-Id: Ib86907e844f34dcaac6cd05757f57db1019201d0 Reviewed-on: https://go-review.googlesource.com/46031 Run-TryBot: Russ Cox Reviewed-by: Marcel van Lohuizen --- src/cmd/go/go_test.go | 58 ++++++++++++++++++++++++++++ src/cmd/go/testdata/src/testregexp/x_test.go | 17 ++++++++ src/cmd/go/testdata/src/testregexp/z_test.go | 19 +++++++++ 3 files changed, 94 insertions(+) create mode 100644 src/cmd/go/testdata/src/testregexp/x_test.go create mode 100644 src/cmd/go/testdata/src/testregexp/z_test.go (limited to 'src/cmd') diff --git a/src/cmd/go/go_test.go b/src/cmd/go/go_test.go index a59da8bc90..e7fc5fc103 100644 --- a/src/cmd/go/go_test.go +++ b/src/cmd/go/go_test.go @@ -4194,3 +4194,61 @@ func main() {}`) tg.setenv("GOARM", "7") })) } + +func TestTestRegexps(t *testing.T) { + tg := testgo(t) + defer tg.cleanup() + tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata")) + tg.run("test", "-cpu=1", "-run=X/Y", "-bench=X/Y", "-count=2", "-v", "testregexp") + var lines []string + for _, line := range strings.SplitAfter(tg.getStdout(), "\n") { + if strings.Contains(line, "=== RUN") || strings.Contains(line, "--- BENCH") || strings.Contains(line, "LOG") { + lines = append(lines, line) + } + } + + // Important parts: + // TestX is run, twice + // TestX/Y is run, twice + // TestXX is run, twice + // TestZ is not run + // BenchmarkX is run but only with N=1, once + // BenchmarkXX is run but only with N=1, once + // BenchmarkX/Y is run in full, twice + want := `=== RUN TestX +=== RUN TestX/Y + x_test.go:6: LOG: X running + x_test.go:8: LOG: Y running +=== RUN TestXX + z_test.go:10: LOG: XX running +=== RUN TestX +=== RUN TestX/Y + x_test.go:6: LOG: X running + x_test.go:8: LOG: Y running +=== RUN TestXX + z_test.go:10: LOG: XX running +--- BENCH: BenchmarkX/Y + x_test.go:15: LOG: Y running N=1 + x_test.go:15: LOG: Y running N=100 + x_test.go:15: LOG: Y running N=10000 + x_test.go:15: LOG: Y running N=1000000 + x_test.go:15: LOG: Y running N=100000000 + x_test.go:15: LOG: Y running N=2000000000 +--- BENCH: BenchmarkX/Y + x_test.go:15: LOG: Y running N=1 + x_test.go:15: LOG: Y running N=100 + x_test.go:15: LOG: Y running N=10000 + x_test.go:15: LOG: Y running N=1000000 + x_test.go:15: LOG: Y running N=100000000 + x_test.go:15: LOG: Y running N=2000000000 +--- BENCH: BenchmarkX + x_test.go:13: LOG: X running N=1 +--- BENCH: BenchmarkXX + z_test.go:18: LOG: XX running N=1 +` + + have := strings.Join(lines, "") + if have != want { + t.Errorf("reduced output:<<<\n%s>>> want:<<<\n%s>>>", have, want) + } +} diff --git a/src/cmd/go/testdata/src/testregexp/x_test.go b/src/cmd/go/testdata/src/testregexp/x_test.go new file mode 100644 index 0000000000..7573e79e16 --- /dev/null +++ b/src/cmd/go/testdata/src/testregexp/x_test.go @@ -0,0 +1,17 @@ +package x + +import "testing" + +func TestX(t *testing.T) { + t.Logf("LOG: X running") + t.Run("Y", func(t *testing.T) { + t.Logf("LOG: Y running") + }) +} + +func BenchmarkX(b *testing.B) { + b.Logf("LOG: X running N=%d", b.N) + b.Run("Y", func(b *testing.B) { + b.Logf("LOG: Y running N=%d", b.N) + }) +} diff --git a/src/cmd/go/testdata/src/testregexp/z_test.go b/src/cmd/go/testdata/src/testregexp/z_test.go new file mode 100644 index 0000000000..4fd1979154 --- /dev/null +++ b/src/cmd/go/testdata/src/testregexp/z_test.go @@ -0,0 +1,19 @@ +package x + +import "testing" + +func TestZ(t *testing.T) { + t.Logf("LOG: Z running") +} + +func TestXX(t *testing.T) { + t.Logf("LOG: XX running") +} + +func BenchmarkZ(b *testing.B) { + b.Logf("LOG: Z running N=%d", b.N) +} + +func BenchmarkXX(b *testing.B) { + b.Logf("LOG: XX running N=%d", b.N) +} -- cgit v1.3-5-g9baa