Write to testing.TB.
(p []byte)
| 158 | |
| 159 | // Write to testing.TB. |
| 160 | func (t TestWriter) Write(p []byte) (n int, err error) { |
| 161 | t.T.Helper() |
| 162 | |
| 163 | n = len(p) |
| 164 | |
| 165 | // Strip trailing newline because t.Log always adds one. |
| 166 | p = bytes.TrimRight(p, "\n") |
| 167 | |
| 168 | // Try to correct the log file and line number to the caller. |
| 169 | if t.Frame > 0 { |
| 170 | _, origFile, origLine, _ := runtime.Caller(1) |
| 171 | _, frameFile, frameLine, ok := runtime.Caller(1 + t.Frame) |
| 172 | if ok { |
| 173 | erase := strings.Repeat("\b", len(path.Base(origFile))+len(strconv.Itoa(origLine))+3) |
| 174 | t.T.Logf("%s%s:%d: %s", erase, path.Base(frameFile), frameLine, p) |
| 175 | return n, err |
| 176 | } |
| 177 | } |
| 178 | t.T.Log(string(p)) |
| 179 | |
| 180 | return n, err |
| 181 | } |
| 182 | |
| 183 | // ConsoleTestWriter creates an option that correctly sets the file frame depth for testing.TB log. |
| 184 | func ConsoleTestWriter(t TestingLog) func(w *ConsoleWriter) { |