internal/test: test Logger helper

Provides *log.Logger and io.Writer that will log to a test object.

See golang/go#22513
Helped with #34
This commit is contained in:
Michael McLoughlin
2019-01-06 13:30:30 -08:00
parent 602bb5197c
commit b6576feee6

View File

@@ -2,7 +2,9 @@
package test
import (
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
@@ -68,3 +70,22 @@ func goexec(t *testing.T, arg ...string) {
t.Fatal(err)
}
}
// Logger builds a logger that writes to the test object.
func Logger(tb testing.TB) *log.Logger {
return log.New(Writer(tb), "test", log.LstdFlags)
}
type writer struct {
tb testing.TB
}
// Writer builds a writer that logs all writes to the test object.
func Writer(tb testing.TB) io.Writer {
return writer{tb}
}
func (w writer) Write(p []byte) (n int, err error) {
w.tb.Log(string(p))
return len(p), nil
}