Skip to content

Commit

Permalink
Remove call site from outputWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
suntala committed Feb 19, 2025
1 parent 6fb1d3f commit 664f85f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
12 changes: 8 additions & 4 deletions src/testing/sub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,7 @@ func TestLogAfterComplete(t *T) {
tRunner(t1, func(t *T) {
t.Run("TestLateLog", func(t *T) {
go func() {
const l = "log after test"
defer close(c2)
defer func() {
p := recover()
Expand All @@ -843,14 +844,17 @@ func TestLogAfterComplete(t *T) {
c2 <- fmt.Sprintf("subtest panic with unexpected value %v", p)
return
}
const want = "Log in goroutine after TestLateLog has completed: log after test"
if !strings.Contains(s, want) {
c2 <- fmt.Sprintf("subtest panic %q does not contain %q", s, want)
const message = "Log in goroutine after TestLateLog has completed"
if !strings.Contains(s, message) {
c2 <- fmt.Sprintf("subtest panic %q does not contain %q", s, message)
}
if !strings.Contains(s, l) {
c2 <- fmt.Sprintf("subtest panic %q does not contain %q", s, l)
}
}()

<-c1
t.Log("log after test")
t.Log(l)
}()
})
})
Expand Down
42 changes: 20 additions & 22 deletions src/testing/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -1003,8 +1003,9 @@ func (c *common) FailNow() {
runtime.Goexit()
}

// log generates the output. It is always at the same stack depth. It inserts
// the final newline if necessary.
// log generates the output. It is always at the same stack depth. log inserts
// indentation and the final newline if necessary. It prefixes the string
// with the file and line of the call site.
func (c *common) log(s string) {
if l := len(s); l > 0 && (string(s[l-1]) != "\n") {
s += "\n"
Expand All @@ -1013,8 +1014,10 @@ func (c *common) log(s string) {
// the indentation provided by outputWriter.
s = strings.Replace(s, "\n", "\n ", -1)

cs := c.callSite(3) // callSite + log + public function
c.newOutputWriter(cs).Write([]byte(s))
// Prefix with the call site. It is located by skipping 3 functions:
// callSite + log + public function
s = c.callSite(3) + s
c.newOutputWriter().Write([]byte(s))
}

// callSite retrieves and formats the file and line of the call site.
Expand All @@ -1041,20 +1044,18 @@ func (c *common) callSite(skip int) string {
return fmt.Sprintf("%s:%d: ", file, line)
}

// newOutputWriter initialises a new outputWriter with the provided call site.
func (c *common) newOutputWriter(cs string) io.Writer {
return &outputWriter{c, nil, cs}
// newOutputWriter initialises a new outputWriter.
func (c *common) newOutputWriter() io.Writer {
return &outputWriter{c, nil}
}

// outputWriter buffers, formats and writes input.
type outputWriter struct {
c *common
b []byte // Stores incomplete input between writes.
cs string // Call site.
c *common
b []byte // Stores incomplete input between writes.
}

// Write generates the output. It prefixes the string with the file and line of
// the call site if provided. It inserts indentation spaces for formatting. It
// Write generates the output. It inserts indentation spaces for formatting and
// stores input for later if it is not terminated by a newline.
func (o *outputWriter) Write(p []byte) (int, error) {
s := string(append(o.b, p...))
Expand All @@ -1076,18 +1077,15 @@ func (o *outputWriter) Write(p []byte) (int, error) {
}

buf := new(strings.Builder)
// All lines are indented 4 spaces unless a line is the final one and
// is empty.
if i == 0 {
buf.WriteString(" ")
buf.WriteString(o.cs)
} else if i < l-1 {
buf.WriteString("\n ")
} else {
// The final line must be empty otherwise the loop would have
// terminated earlier.
// Add back newlines.
if i != 0 {
buf.WriteString("\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(" ")
}
buf.WriteString(line)

o.writeLine(buf.String(), p)
Expand Down

0 comments on commit 664f85f

Please sign in to comment.