Skip to content

Commit a813d3c

Browse files
jeanbzaianlancetaylor
authored andcommitted
testing: stream log output in verbose mode
Fixes #24929 Change-Id: Icc426068cd73b75b78001f55e1e5d81ccebbe854 Reviewed-on: https://go-review.googlesource.com/c/go/+/127120 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
1 parent 7416315 commit a813d3c

File tree

4 files changed

+76
-35
lines changed

4 files changed

+76
-35
lines changed

src/cmd/go/go_test.go

+20-24
Original file line numberDiff line numberDiff line change
@@ -4938,35 +4938,31 @@ func TestTestRegexps(t *testing.T) {
49384938
// BenchmarkXX is run but only with N=1, once
49394939
// BenchmarkX/Y is run in full, twice
49404940
want := `=== RUN TestX
4941+
TestX: x_test.go:6: LOG: X running
49414942
=== RUN TestX/Y
4942-
x_test.go:6: LOG: X running
4943-
x_test.go:8: LOG: Y running
4943+
TestX/Y: x_test.go:8: LOG: Y running
49444944
=== RUN TestXX
4945-
z_test.go:10: LOG: XX running
4945+
TestXX: z_test.go:10: LOG: XX running
49464946
=== RUN TestX
4947+
TestX: x_test.go:6: LOG: X running
49474948
=== RUN TestX/Y
4948-
x_test.go:6: LOG: X running
4949-
x_test.go:8: LOG: Y running
4949+
TestX/Y: x_test.go:8: LOG: Y running
49504950
=== RUN TestXX
4951-
z_test.go:10: LOG: XX running
4952-
--- BENCH: BenchmarkX/Y
4953-
x_test.go:15: LOG: Y running N=1
4954-
x_test.go:15: LOG: Y running N=100
4955-
x_test.go:15: LOG: Y running N=10000
4956-
x_test.go:15: LOG: Y running N=1000000
4957-
x_test.go:15: LOG: Y running N=100000000
4958-
x_test.go:15: LOG: Y running N=1000000000
4959-
--- BENCH: BenchmarkX/Y
4960-
x_test.go:15: LOG: Y running N=1
4961-
x_test.go:15: LOG: Y running N=100
4962-
x_test.go:15: LOG: Y running N=10000
4963-
x_test.go:15: LOG: Y running N=1000000
4964-
x_test.go:15: LOG: Y running N=100000000
4965-
x_test.go:15: LOG: Y running N=1000000000
4966-
--- BENCH: BenchmarkX
4967-
x_test.go:13: LOG: X running N=1
4968-
--- BENCH: BenchmarkXX
4969-
z_test.go:18: LOG: XX running N=1
4951+
TestXX: z_test.go:10: LOG: XX running
4952+
BenchmarkX: x_test.go:13: LOG: X running N=1
4953+
BenchmarkX/Y: x_test.go:15: LOG: Y running N=1
4954+
BenchmarkX/Y: x_test.go:15: LOG: Y running N=100
4955+
BenchmarkX/Y: x_test.go:15: LOG: Y running N=10000
4956+
BenchmarkX/Y: x_test.go:15: LOG: Y running N=1000000
4957+
BenchmarkX/Y: x_test.go:15: LOG: Y running N=100000000
4958+
BenchmarkX/Y: x_test.go:15: LOG: Y running N=1000000000
4959+
BenchmarkX/Y: x_test.go:15: LOG: Y running N=1
4960+
BenchmarkX/Y: x_test.go:15: LOG: Y running N=100
4961+
BenchmarkX/Y: x_test.go:15: LOG: Y running N=10000
4962+
BenchmarkX/Y: x_test.go:15: LOG: Y running N=1000000
4963+
BenchmarkX/Y: x_test.go:15: LOG: Y running N=100000000
4964+
BenchmarkX/Y: x_test.go:15: LOG: Y running N=1000000000
4965+
BenchmarkXX: z_test.go:18: LOG: XX running N=1
49704966
`
49714967

49724968
have := strings.Join(lines, "")

src/testing/benchmark.go

+21-1
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,11 @@ func (ctx *benchContext) processBench(b *B) {
545545
for j := uint(0); j < *count; j++ {
546546
runtime.GOMAXPROCS(procs)
547547
benchName := benchmarkName(b.name, procs)
548-
fmt.Fprintf(b.w, "%-*s\t", ctx.maxLen, benchName)
548+
549+
// If it's chatty, we've already printed this information.
550+
if !b.chatty {
551+
fmt.Fprintf(b.w, "%-*s\t", ctx.maxLen, benchName)
552+
}
549553
// Recompute the running time for all but the first iteration.
550554
if i > 0 || j > 0 {
551555
b = &B{
@@ -569,6 +573,9 @@ func (ctx *benchContext) processBench(b *B) {
569573
continue
570574
}
571575
results := r.String()
576+
if b.chatty {
577+
fmt.Fprintf(b.w, "%-*s\t", ctx.maxLen, benchName)
578+
}
572579
if *benchmarkMemory || b.showAllocResult {
573580
results += "\t" + r.MemString()
574581
}
@@ -627,6 +634,19 @@ func (b *B) Run(name string, f func(b *B)) bool {
627634
// Only process sub-benchmarks, if any.
628635
atomic.StoreInt32(&sub.hasSub, 1)
629636
}
637+
638+
if b.chatty {
639+
labelsOnce.Do(func() {
640+
fmt.Printf("goos: %s\n", runtime.GOOS)
641+
fmt.Printf("goarch: %s\n", runtime.GOARCH)
642+
if b.importPath != "" {
643+
fmt.Printf("pkg: %s\n", b.importPath)
644+
}
645+
})
646+
647+
fmt.Println(benchName)
648+
}
649+
630650
if sub.run1() {
631651
sub.run()
632652
}

src/testing/sub_test.go

+25-7
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,31 @@ func TestTRun(t *T) {
434434
<-ch
435435
t.Errorf("error")
436436
},
437+
}, {
438+
// A chatty test should always log with fmt.Print, even if the
439+
// parent test has completed.
440+
// TODO(deklerk) Capture the log of fmt.Print and assert that the
441+
// subtest message is not lost.
442+
desc: "log in finished sub test with chatty",
443+
ok: false,
444+
chatty: true,
445+
output: `
446+
--- FAIL: log in finished sub test with chatty (N.NNs)`,
447+
maxPar: 1,
448+
f: func(t *T) {
449+
ch := make(chan bool)
450+
t.Run("sub", func(t2 *T) {
451+
go func() {
452+
<-ch
453+
t2.Log("message1")
454+
ch <- true
455+
}()
456+
})
457+
t.Log("message2")
458+
ch <- true
459+
<-ch
460+
t.Errorf("error")
461+
},
437462
}}
438463
for _, tc := range testCases {
439464
ctx := newTestContext(tc.maxPar, newMatcher(regexp.MatchString, "", ""))
@@ -521,13 +546,6 @@ func TestBRun(t *T) {
521546
chatty: true,
522547
output: "--- SKIP: root",
523548
f: func(b *B) { b.SkipNow() },
524-
}, {
525-
desc: "skipping with message, chatty",
526-
chatty: true,
527-
output: `
528-
--- SKIP: root
529-
sub_test.go:NNN: skipping`,
530-
f: func(b *B) { b.Skip("skipping") },
531549
}, {
532550
desc: "chatty with recursion",
533551
chatty: true,

src/testing/testing.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,9 @@ func (c *common) decorate(s string, skip int) string {
479479
buf := new(strings.Builder)
480480
// Every line is indented at least 4 spaces.
481481
buf.WriteString(" ")
482+
if c.chatty {
483+
fmt.Fprintf(buf, "%s: ", c.name)
484+
}
482485
fmt.Fprintf(buf, "%s:%d: ", file, line)
483486
lines := strings.Split(s, "\n")
484487
if l := len(lines); l > 1 && lines[l-1] == "" {
@@ -662,9 +665,7 @@ func (c *common) log(s string) {
662665
func (c *common) logDepth(s string, depth int) {
663666
c.mu.Lock()
664667
defer c.mu.Unlock()
665-
if !c.done {
666-
c.output = append(c.output, c.decorate(s, depth+1)...)
667-
} else {
668+
if c.done {
668669
// This test has already finished. Try and log this message
669670
// with our parent. If we don't have a parent, panic.
670671
for parent := c.parent; parent != nil; parent = parent.parent {
@@ -676,6 +677,12 @@ func (c *common) logDepth(s string, depth int) {
676677
}
677678
}
678679
panic("Log in goroutine after " + c.name + " has completed")
680+
} else {
681+
if c.chatty {
682+
fmt.Print(c.decorate(s, depth+1))
683+
return
684+
}
685+
c.output = append(c.output, c.decorate(s, depth+1)...)
679686
}
680687
}
681688

0 commit comments

Comments
 (0)