From b6576feee64573294109da03cc07dbec29efc85a Mon Sep 17 00:00:00 2001 From: Michael McLoughlin Date: Sun, 6 Jan 2019 13:30:30 -0800 Subject: [PATCH] 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 --- internal/test/utils.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/internal/test/utils.go b/internal/test/utils.go index cf5c87c..9fc6651 100644 --- a/internal/test/utils.go +++ b/internal/test/utils.go @@ -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 +}