Skip to content

Commit

Permalink
Build line as bytes instead of string
Browse files Browse the repository at this point in the history
  • Loading branch information
suntala committed Mar 6, 2025
1 parent 9352f78 commit 0017acb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/testing/sub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1039,7 +1039,7 @@ func TestOutputWriter(t *T) {
}

func TestOutputWriterBuffering(t *T) {
w := outputWriter{&t.common, nil}
w := outputWriter{c: &t.common}

w.Write([]byte("Hel"))
w.Write([]byte("lo\nWorld\nInput to log\n\n\nMore logging\nShouldn't be logged"))
Expand Down
23 changes: 12 additions & 11 deletions src/testing/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -1073,53 +1073,54 @@ func (o *outputWriter) Write(p []byte) (int, error) {
if i == (l-1) && line != "" {
o.b = []byte(line)
if i > 0 {
o.writeLine("\n", p)
o.writeLine([]byte("\n"), p)
}
break
}

buf := new(strings.Builder)
var b []byte
// Add back newlines.
if i != 0 {
buf.WriteString("\n")
b = append(b, []byte("\n")...)
}
// All lines are indented 4 spaces except the final one, which must be
// empty otherwise the loop would have terminated earlier.
if i != l-1 {
buf.WriteString(" ")
b = append(b, []byte(" ")...)
}
buf.WriteString(line)
b = append(b, []byte(line)...)

o.writeLine(buf.String(), p)
o.writeLine(b, p)
}
return len(p), nil
}

// writeLine generates the output for a given line.
func (o *outputWriter) writeLine(s string, p []byte) {
func (o *outputWriter) writeLine(l []byte, p []byte) {
if o.c.done {
// This test has already finished. Try and log this message
// with our parent. If we don't have a parent, panic.
for parent := o.c.parent; parent != nil; parent = parent.parent {
parent.mu.Lock()
defer parent.mu.Unlock()
if !parent.done {
parent.output = append(parent.output, s...)
parent.output = append(parent.output, l...)
return
}
}
panic("Log in goroutine after " + o.c.name + " has completed: " + string(p))
} else {
if o.c.chatty != nil {
line := string(l)
if o.c.bench {
// Benchmarks don't print === CONT, so we should skip the test
// printer and just print straight to stdout.
fmt.Print(s)
fmt.Print(line)
} else {
o.c.chatty.Printf(o.c.name, "%s", s)
o.c.chatty.Printf(o.c.name, "%s", line)
}
} else {
o.c.output = append(o.c.output, s...)
o.c.output = append(o.c.output, l...)
}
}
}
Expand Down

0 comments on commit 0017acb

Please sign in to comment.