From d6f5902c5e625fc6018f804f397dd7396bd119e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Wed, 30 Oct 2019 15:46:11 +0000 Subject: [PATCH] cmd/gogio: send all e2e logs to t.Logf MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit chromedp was defaulting to log.Printf, which is not good for tests. The xgb and xgbutil logs were suppressed if -v wasn't given, but they were sent straight to os.Stderr otherwise: === RUN TestX11 === PAUSE TestX11 === CONT TestX11 XGB: conn.go:47: Could not get authority info: EOF XGB: conn.go:48: Trying connection without authority info... --- PASS: TestX11 (0.87s) Instead, direct their loggers to an io.Writer implementation that sends its output to t.Logf: === RUN TestX11 === PAUSE TestX11 === CONT TestX11 TestX11: x11_test.go:187: XGB: conn.go:47: Could not get authority info: EOF TestX11: x11_test.go:187: XGB: conn.go:48: Trying connection without authority info... --- PASS: TestX11 (0.86s) We do end up with duplicate log prefixes, but at least we don't write straight to stderr, which will be a problem as we add more concurrent tests. Signed-off-by: Daniel Martí --- cmd/gogio/js_test.go | 5 ++++- cmd/gogio/x11_test.go | 21 +++++++++++++++++---- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/cmd/gogio/js_test.go b/cmd/gogio/js_test.go index 2c91fc18..50f18dec 100644 --- a/cmd/gogio/js_test.go +++ b/cmd/gogio/js_test.go @@ -66,7 +66,10 @@ func TestJSOnChrome(t *testing.T) { actx, cancel := chromedp.NewExecAllocator(context.Background(), opts...) defer cancel() - ctx, cancel := chromedp.NewContext(actx) + ctx, cancel := chromedp.NewContext(actx, + // Send all logf/errf calls to t.Logf + chromedp.WithLogf(t.Logf), + ) defer cancel() if err := chromedp.Run(ctx); err != nil { diff --git a/cmd/gogio/x11_test.go b/cmd/gogio/x11_test.go index 9d26987d..561d1e4c 100644 --- a/cmd/gogio/x11_test.go +++ b/cmd/gogio/x11_test.go @@ -136,10 +136,8 @@ func TestX11(t *testing.T) { // Finally, run our tests. A connection to the X server is used to // interact with it. { - if !testing.Verbose() { - xgb.Logger.SetOutput(ioutil.Discard) - xgbutil.Logger.SetOutput(ioutil.Discard) - } + xgb.Logger.SetOutput(testLogWriter{t}) + xgbutil.Logger.SetOutput(testLogWriter{t}) xu, err := xgbutil.NewConnDisplay(display) if err != nil { t.Fatal(err) @@ -174,3 +172,18 @@ func TestX11(t *testing.T) { wantColor(t, img, 595, 595, 0xdede, 0xadad, 0xbebe) } } + +// testLogWriter is a bit of a hack to redirect libraries that use a *log.Logger +// variable to instead send their logs to t.Logf. +// +// Since *log.Logger isn't an interface and can only take an io.Writer, all we +// can do is implement an io.Writer that sends its output to t.Logf. We end up +// with duplicate log prefixes, but that doesn't seem so bad. +type testLogWriter struct { + t *testing.T +} + +func (w testLogWriter) Write(p []byte) (n int, err error) { + w.t.Logf("%s", p) + return len(p), nil +}