diff --git a/pkg/test/test.go b/pkg/test/test.go index a87b4acde..222f34e70 100644 --- a/pkg/test/test.go +++ b/pkg/test/test.go @@ -20,6 +20,7 @@ import ( "log" "os" "strconv" + "strings" "testing" ) @@ -34,7 +35,7 @@ func BrokenTest(t *testing.T) { // TLog changes the log package's output to log to t and returns a function // to reset it back to stderr. func TLog(t testing.TB) func() { - log.SetOutput(&twriter{t: t}) + log.SetOutput(twriter{t: t}) return func() { log.SetOutput(os.Stderr) } @@ -44,11 +45,14 @@ type twriter struct { t testing.TB } -func (w *twriter) Write(p []byte) (n int, err error) { - t.Logf("%s", p) +func (w twriter) Write(p []byte) (n int, err error) { + if w.t != nil { + w.t.Log(strings.TrimSuffix(string(p), "\n")) + } + return len(p), nil } // NewLogger returns a logger that logs to t with the given prefix. func NewLogger(t *testing.T, prefix string) *log.Logger { - return log.New(&twriter{t: t}, prefix, log.LstdFlags) + return log.New(twriter{t: t}, prefix, log.LstdFlags) } diff --git a/pkg/test/test_test.go b/pkg/test/test_test.go index a83c01ca2..169f6330f 100644 --- a/pkg/test/test_test.go +++ b/pkg/test/test_test.go @@ -48,8 +48,7 @@ func TestTLog(t *testing.T) { want := []string{ "hello", "hello", - "some text", - "and more text", + "some text\nand more text", } if !reflect.DeepEqual(tb.log, want) { t.Errorf("Got %q; want %q", tb.log, want)