From 6c0da3081cb8725abfda558a8538e0d80cdce7ee Mon Sep 17 00:00:00 2001 From: dimmo Date: Sat, 30 Dec 2023 14:03:30 +0500 Subject: [PATCH 1/2] Make zaptest.NewTestingWriter public --- zaptest/logger.go | 18 +++++++++--------- zaptest/logger_test.go | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/zaptest/logger.go b/zaptest/logger.go index 6a4a35497..d303a7574 100644 --- a/zaptest/logger.go +++ b/zaptest/logger.go @@ -82,7 +82,7 @@ func NewLogger(t TestingT, opts ...LoggerOption) *zap.Logger { o.applyLoggerOption(&cfg) } - writer := newTestingWriter(t) + writer := NewTestingWriter(t) zapOptions := []zap.Option{ // Send zap errors to the same writer and mark the test as failed if // that happens. @@ -100,27 +100,27 @@ func NewLogger(t TestingT, opts ...LoggerOption) *zap.Logger { ) } -// testingWriter is a WriteSyncer that writes to the given testing.TB. -type testingWriter struct { +// TestingWriter is a WriteSyncer that writes to the given testing.TB. +type TestingWriter struct { t TestingT - // If true, the test will be marked as failed if this testingWriter is + // If true, the test will be marked as failed if this TestingWriter is // ever used. markFailed bool } -func newTestingWriter(t TestingT) testingWriter { - return testingWriter{t: t} +func NewTestingWriter(t TestingT) TestingWriter { + return TestingWriter{t: t} } // WithMarkFailed returns a copy of this testingWriter with markFailed set to // the provided value. -func (w testingWriter) WithMarkFailed(v bool) testingWriter { +func (w TestingWriter) WithMarkFailed(v bool) TestingWriter { w.markFailed = v return w } -func (w testingWriter) Write(p []byte) (n int, err error) { +func (w TestingWriter) Write(p []byte) (n int, err error) { n = len(p) // Strip trailing newline because t.Log always adds one. @@ -135,6 +135,6 @@ func (w testingWriter) Write(p []byte) (n int, err error) { return n, nil } -func (w testingWriter) Sync() error { +func (w TestingWriter) Sync() error { return nil } diff --git a/zaptest/logger_test.go b/zaptest/logger_test.go index 576f6828c..40e368b50 100644 --- a/zaptest/logger_test.go +++ b/zaptest/logger_test.go @@ -106,7 +106,7 @@ func TestTestLoggerSupportsWrappedZapOptions(t *testing.T) { func TestTestingWriter(t *testing.T) { ts := newTestLogSpy(t) - w := newTestingWriter(ts) + w := NewTestingWriter(ts) n, err := io.WriteString(w, "hello\n\n") assert.NoError(t, err, "WriteString must not fail") From 8dcabdbfc6234af7e5493b63643222c95398a545 Mon Sep 17 00:00:00 2001 From: dimmo Date: Wed, 17 Jan 2024 10:16:50 +0500 Subject: [PATCH 2/2] Add comments for exported functions --- zaptest/logger.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/zaptest/logger.go b/zaptest/logger.go index d303a7574..4734c33f6 100644 --- a/zaptest/logger.go +++ b/zaptest/logger.go @@ -109,17 +109,33 @@ type TestingWriter struct { markFailed bool } +// NewTestingWriter builds a new TestingWriter that writes to the given +// testing.TB. +// +// Use this if you need more flexibility when creating *zap.Logger +// than zaptest.NewLogger() provides. +// +// E.g., if you want to use custom core with zaptest.TestingWriter: +// +// encoder := newCustomEncoder() +// writer := zaptest.NewTestingWriter(t) +// level := zap.NewAtomicLevelAt(zapcore.DebugLevel) +// +// core := newCustomCore(encoder, writer, level) +// +// logger := zap.New(core, zap.AddCaller()) func NewTestingWriter(t TestingT) TestingWriter { return TestingWriter{t: t} } -// WithMarkFailed returns a copy of this testingWriter with markFailed set to +// WithMarkFailed returns a copy of this TestingWriter with markFailed set to // the provided value. func (w TestingWriter) WithMarkFailed(v bool) TestingWriter { w.markFailed = v return w } +// Write writes bytes from p to the underlying testing.TB. func (w TestingWriter) Write(p []byte) (n int, err error) { n = len(p) @@ -135,6 +151,7 @@ func (w TestingWriter) Write(p []byte) (n int, err error) { return n, nil } +// Sync commits the current contents (a no-op for TestingWriter). func (w TestingWriter) Sync() error { return nil }