2019-02-01 15:21:54 +00:00
|
|
|
package testutil
|
|
|
|
|
|
|
|
import (
|
2019-02-01 18:25:04 +00:00
|
|
|
"fmt"
|
2019-02-01 15:21:54 +00:00
|
|
|
"io"
|
|
|
|
"log"
|
2019-02-01 18:25:04 +00:00
|
|
|
"os"
|
2019-02-01 15:21:54 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2019-02-01 18:25:04 +00:00
|
|
|
var sendTestLogsToStdout bool
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
sendTestLogsToStdout = os.Getenv("NOLOGBUFFER") == "1"
|
|
|
|
}
|
|
|
|
|
2019-02-01 15:21:54 +00:00
|
|
|
func TestLogger(t testing.TB) *log.Logger {
|
|
|
|
return log.New(&testWriter{t}, "test: ", log.LstdFlags)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoggerWithName(t testing.TB, name string) *log.Logger {
|
|
|
|
return log.New(&testWriter{t}, "test["+name+"]: ", log.LstdFlags)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWriter(t testing.TB) io.Writer {
|
|
|
|
return &testWriter{t}
|
|
|
|
}
|
|
|
|
|
|
|
|
type testWriter struct {
|
|
|
|
t testing.TB
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tw *testWriter) Write(p []byte) (n int, err error) {
|
|
|
|
tw.t.Helper()
|
2019-02-01 18:25:04 +00:00
|
|
|
if sendTestLogsToStdout {
|
|
|
|
fmt.Fprint(os.Stdout, strings.TrimSpace(string(p))+"\n")
|
|
|
|
} else {
|
|
|
|
tw.t.Log(strings.TrimSpace(string(p)))
|
|
|
|
}
|
2019-02-01 15:21:54 +00:00
|
|
|
return len(p), nil
|
|
|
|
}
|