Skip to content

Commit b98c1b4

Browse files
author
suntala
committed
Extract appending to output from Write
1 parent 0d19c84 commit b98c1b4

File tree

1 file changed

+30
-30
lines changed

1 file changed

+30
-30
lines changed

src/testing/testing.go

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,34 +1054,6 @@ func (o *outputWriter) Write(p []byte) (int, error) {
10541054
o.c.mu.Lock()
10551055
defer o.c.mu.Unlock()
10561056

1057-
doWrite := func(s string) {
1058-
if o.c.done {
1059-
// This test has already finished. Try and log this message
1060-
// with our parent. If we don't have a parent, panic.
1061-
for parent := o.c.parent; parent != nil; parent = parent.parent {
1062-
parent.mu.Lock()
1063-
defer parent.mu.Unlock()
1064-
if !parent.done {
1065-
parent.output = append(parent.output, s...)
1066-
return
1067-
}
1068-
}
1069-
panic("Log in goroutine after " + o.c.name + " has completed: " + string(p))
1070-
} else {
1071-
if o.c.chatty != nil {
1072-
if o.c.bench {
1073-
// Benchmarks don't print === CONT, so we should skip the test
1074-
// printer and just print straight to stdout.
1075-
fmt.Print(s)
1076-
} else {
1077-
o.c.chatty.Printf(o.c.name, "%s", s)
1078-
}
1079-
} else {
1080-
o.c.output = append(o.c.output, s...)
1081-
}
1082-
}
1083-
}
1084-
10851057
str := string(o.b)
10861058
lines := strings.Split(str, "\n")
10871059
for i, line := range lines {
@@ -1091,7 +1063,7 @@ func (o *outputWriter) Write(p []byte) (int, error) {
10911063
if line != "" {
10921064
o.b = []byte(line)
10931065
if i > 0 {
1094-
doWrite("\n")
1066+
o.doWrite("\n", p)
10951067
}
10961068
break
10971069
}
@@ -1112,11 +1084,39 @@ func (o *outputWriter) Write(p []byte) (int, error) {
11121084

11131085
buf.WriteString(line)
11141086

1115-
doWrite(buf.String())
1087+
o.doWrite(buf.String(), p)
11161088
}
11171089
return len(p), nil
11181090
}
11191091

1092+
func (o *outputWriter) doWrite(s string, p []byte) {
1093+
if o.c.done {
1094+
// This test has already finished. Try and log this message
1095+
// with our parent. If we don't have a parent, panic.
1096+
for parent := o.c.parent; parent != nil; parent = parent.parent {
1097+
parent.mu.Lock()
1098+
defer parent.mu.Unlock()
1099+
if !parent.done {
1100+
parent.output = append(parent.output, s...)
1101+
return
1102+
}
1103+
}
1104+
panic("Log in goroutine after " + o.c.name + " has completed: " + string(p))
1105+
} else {
1106+
if o.c.chatty != nil {
1107+
if o.c.bench {
1108+
// Benchmarks don't print === CONT, so we should skip the test
1109+
// printer and just print straight to stdout.
1110+
fmt.Print(s)
1111+
} else {
1112+
o.c.chatty.Printf(o.c.name, "%s", s)
1113+
}
1114+
} else {
1115+
o.c.output = append(o.c.output, s...)
1116+
}
1117+
}
1118+
}
1119+
11201120
// Log formats its arguments using default formatting, analogous to Println,
11211121
// and records the text in the error log. For tests, the text will be printed only if
11221122
// the test fails or the -test.v flag is set. For benchmarks, the text is always

0 commit comments

Comments
 (0)