Skip to content

Commit abff258

Browse files
author
suntala
committed
Add outputWriter as a common field
1 parent 664f85f commit abff258

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

src/testing/benchmark.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,7 @@ func (s *benchState) processBench(b *B) {
675675
benchFunc: b.benchFunc,
676676
benchTime: b.benchTime,
677677
}
678+
b.o = &outputWriter{&b.common, nil}
678679
b.run1()
679680
}
680681
r := b.doBench()
@@ -751,6 +752,7 @@ func (b *B) Run(name string, f func(b *B)) bool {
751752
benchTime: b.benchTime,
752753
bstate: b.bstate,
753754
}
755+
sub.o = &outputWriter{&sub.common, nil}
754756
if partial {
755757
// Partial name match, like -bench=X/Y matching BenchmarkX.
756758
// Only process sub-benchmarks, if any.
@@ -927,6 +929,7 @@ func Benchmark(f func(b *B)) BenchmarkResult {
927929
benchFunc: f,
928930
benchTime: benchTime,
929931
}
932+
b.o = &outputWriter{&b.common, nil}
930933
if b.run1() {
931934
b.run()
932935
}

src/testing/fuzz.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ func (f *F) Fuzz(ff any) {
320320
t.parent.w = captureOut
321321
}
322322
t.w = indenter{&t.common}
323+
t.o = &outputWriter{&t.common, nil}
323324
if t.chatty != nil {
324325
t.chatty.Updatef(t.name, "=== RUN %s\n", t.name)
325326
}
@@ -529,6 +530,7 @@ func runFuzzTests(deps testDeps, fuzzTests []InternalFuzzTarget, deadline time.T
529530
fstate: fstate,
530531
}
531532
f.w = indenter{&f.common}
533+
f.o = &outputWriter{&f.common, nil}
532534
if f.chatty != nil {
533535
f.chatty.Updatef(f.name, "=== RUN %s\n", f.name)
534536
}

src/testing/testing.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,7 @@ type common struct {
626626
mu sync.RWMutex // guards this group of fields
627627
output []byte // Output generated by test or benchmark.
628628
w io.Writer // For flushToParent.
629+
o io.Writer // Writes output.
629630
ran bool // Test or benchmark (or one of its subtests) was executed.
630631
failed bool // Test or benchmark has failed.
631632
skipped bool // Test or benchmark has been skipped.
@@ -1007,17 +1008,18 @@ func (c *common) FailNow() {
10071008
// indentation and the final newline if necessary. It prefixes the string
10081009
// with the file and line of the call site.
10091010
func (c *common) log(s string) {
1010-
if l := len(s); l > 0 && (string(s[l-1]) != "\n") {
1011-
s += "\n"
1011+
if l := len(s); l > 0 && (string(s[l-1]) == "\n") {
1012+
s = s[:l-1]
10121013
}
10131014
// Second and subsequent lines are indented 4 spaces. This is in addition to
10141015
// the indentation provided by outputWriter.
10151016
s = strings.Replace(s, "\n", "\n ", -1)
1017+
s += "\n"
10161018

10171019
// Prefix with the call site. It is located by skipping 3 functions:
10181020
// callSite + log + public function
10191021
s = c.callSite(3) + s
1020-
c.newOutputWriter().Write([]byte(s))
1022+
c.o.Write([]byte(s))
10211023
}
10221024

10231025
// callSite retrieves and formats the file and line of the call site.
@@ -1881,6 +1883,7 @@ func (t *T) Run(name string, f func(t *T)) bool {
18811883
tstate: t.tstate,
18821884
}
18831885
t.w = indenter{&t.common}
1886+
t.o = &outputWriter{&t.common, nil}
18841887

18851888
if t.chatty != nil {
18861889
t.chatty.Updatef(t.name, "=== RUN %s\n", t.name)

0 commit comments

Comments
 (0)