Skip to content

Commit 0eb55d7

Browse files
author
suntala
committed
Flush outputWriter's buffer when test finishes
1 parent 91934a0 commit 0eb55d7

File tree

2 files changed

+56
-8
lines changed

2 files changed

+56
-8
lines changed

src/testing/sub_test.go

+39-7
Original file line numberDiff line numberDiff line change
@@ -1018,14 +1018,46 @@ func TestOutputWriter(t *T) {
10181018
buf: "cd",
10191019
}}
10201020
for _, tc := range testCases {
1021+
o.Write([]byte(tc.in))
1022+
if string(o.c.output) != tc.out {
1023+
t.Errorf("output:\ngot:\n%s\nwant:\n%s", o.c.output, tc.out)
1024+
}
1025+
if string(o.b) != tc.buf {
1026+
t.Errorf("buffer:\ngot:\n%s\nwant:\n%s", o.b, tc.buf)
1027+
}
1028+
}
1029+
}
1030+
1031+
func TestOutputWriterFlushing(t *T) {
1032+
tstate := newTestState(1, allMatcher())
1033+
buf := &strings.Builder{}
1034+
root := &T{
1035+
common: common{
1036+
w: buf,
1037+
},
1038+
tstate: tstate,
1039+
}
1040+
1041+
f := func(t *T) {
10211042
t.Run("", func(t *T) {
1022-
o.Write([]byte(tc.in))
1023-
if string(o.c.output) != tc.out {
1024-
t.Errorf("output:\ngot:\n%s\nwant:\n%s", o.c.output, tc.out)
1025-
}
1026-
if string(o.b) != tc.buf {
1027-
t.Errorf("buffer:\ngot:\n%s\nwant:\n%s", o.b, tc.buf)
1028-
}
1043+
t.o.Write([]byte("a\n"))
1044+
t.o.Write([]byte("b"))
1045+
t.Fail()
10291046
})
10301047
}
1048+
root.Run("check flushing of outputWriter", f)
1049+
1050+
got := strings.TrimSpace(buf.String())
1051+
output := `
1052+
--- FAIL: check flushing of outputWriter (0.00s)
1053+
--- FAIL: check flushing of outputWriter/#00 (0.00s)
1054+
a
1055+
b
1056+
`
1057+
1058+
want := strings.TrimSpace(output)
1059+
re := makeRegexp(want)
1060+
if ok, err := regexp.MatchString(re, got); !ok || err != nil {
1061+
t.Errorf("output:\ngot:\n%s\nwant:\n%s", got, want)
1062+
}
10311063
}

src/testing/testing.go

+17-1
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ type common struct {
629629
mu sync.RWMutex // guards this group of fields
630630
output []byte // Output generated by test or benchmark.
631631
w io.Writer // For flushToParent.
632-
o io.Writer // Writes output.
632+
o *outputWriter // Writes output.
633633
ran bool // Test or benchmark (or one of its subtests) was executed.
634634
failed bool // Test or benchmark has failed.
635635
skipped bool // Test or benchmark has been skipped.
@@ -1118,6 +1118,14 @@ func (o *outputWriter) writeLine(b []byte) {
11181118
}
11191119
}
11201120

1121+
// flush outputs the contents of the buffer.
1122+
func (o *outputWriter) flush() {
1123+
if len(o.b) == 0 {
1124+
return
1125+
}
1126+
o.Write([]byte("\n"))
1127+
}
1128+
11211129
// Log formats its arguments using default formatting, analogous to Println,
11221130
// and records the text in the error log. For tests, the text will be printed only if
11231131
// the test fails or the -test.v flag is set. For benchmarks, the text is always
@@ -1772,6 +1780,10 @@ func tRunner(t *T, fn func(t *T)) {
17721780
root.duration += highPrecisionTimeSince(root.start)
17731781
d := root.duration
17741782
root.mu.Unlock()
1783+
// Output the outputWriter's buffer.
1784+
if t.o != nil {
1785+
t.o.flush()
1786+
}
17751787
root.flushToParent(root.name, "--- FAIL: %s (%s)\n", root.name, fmtDuration(d))
17761788
if r := root.parent.runCleanup(recoverAndReturnPanic); r != nil {
17771789
fmt.Fprintf(root.parent.w, "cleanup panicked with %v", r)
@@ -1819,6 +1831,10 @@ func tRunner(t *T, fn func(t *T)) {
18191831
// test. See comment in Run method.
18201832
t.tstate.release()
18211833
}
1834+
// Output the outputWriter's buffer.
1835+
if t.o != nil {
1836+
t.o.flush()
1837+
}
18221838
t.report() // Report after all subtests have finished.
18231839

18241840
// Do not lock t.done to allow race detector to detect race in case

0 commit comments

Comments
 (0)