Skip to content

Commit

Permalink
Rely on subset of testing.TB interface
Browse files Browse the repository at this point in the history
This declares a TestingT interface which is a subset of the
functionality provided by testing.TB, and changes zaptest to rely on
that.

This also means that zaptest no longer imports the "testing" package.
  • Loading branch information
abhinav authored and akshayjshah committed Apr 12, 2018
1 parent 479a09b commit 5b0fd11
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 8 deletions.
10 changes: 4 additions & 6 deletions zaptest/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
package zaptest

import (
"testing"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
Expand All @@ -32,7 +30,7 @@ import (
//
// Use this with a *testing.T or *testing.B to get logs which get printed only
// if a test fails or if you ran go test -v.
func NewLogger(t testing.TB) *zap.Logger {
func NewLogger(t TestingT) *zap.Logger {
return NewLoggerAt(t, zapcore.DebugLevel)
}

Expand All @@ -41,7 +39,7 @@ func NewLogger(t testing.TB) *zap.Logger {
//
// Use this with a *testing.T or *testing.B to get logs which get printed only
// if a test fails or if you ran go test -v.
func NewLoggerAt(t testing.TB, enab zapcore.LevelEnabler) *zap.Logger {
func NewLoggerAt(t TestingT, enab zapcore.LevelEnabler) *zap.Logger {
return zap.New(zapcore.NewCore(
zapcore.NewConsoleEncoder(zapcore.EncoderConfig{
// EncoderConfig copied from zap.NewDevelopmentEncoderConfig.
Expand All @@ -64,7 +62,7 @@ func NewLoggerAt(t testing.TB, enab zapcore.LevelEnabler) *zap.Logger {
}

// testingWriter is a WriteSyncer that writes to the given testing.TB.
type testingWriter struct{ t testing.TB }
type testingWriter struct{ t TestingT }

func (w testingWriter) Write(p []byte) (n int, err error) {
s := string(p)
Expand All @@ -75,7 +73,7 @@ func (w testingWriter) Write(p []byte) (n int, err error) {
}

// Note: t.Log is safe for concurrent use.
w.t.Log(s)
w.t.Logf("%s", s)
return len(p), nil
}

Expand Down
4 changes: 2 additions & 2 deletions zaptest/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,14 @@ func newTestLogSpy(t testing.TB) *testLogSpy {
return &testLogSpy{TB: t}
}

func (t *testLogSpy) Log(args ...interface{}) {
func (t *testLogSpy) Logf(format string, args ...interface{}) {
// Log messages are in the format,
//
// 2017-10-27T13:03:01.000-0700 DEBUG your message here {data here}
//
// We strip the first part of these messages because we can't really test
// for the timestamp from these tests.
m := fmt.Sprint(args...)
m := fmt.Sprintf(format, args...)
m = m[strings.IndexByte(m, '\t')+1:]

// t.Log should be thread-safe.
Expand Down
38 changes: 38 additions & 0 deletions zaptest/testingt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package zaptest

// TestingT is a subset of the API provided by all *testing.T and *testing.B
// objects.
type TestingT interface {
// Logs the given message without failing the test.
Logf(string, ...interface{})

// Logs the given message and marks the test as failed.
Errorf(string, ...interface{})

// Marks the test as failed and stops execution of that test.
FailNow()
}

// Note: We currently only rely on Logf. We are including Errorf and FailNow
// in the interface in anticipation of future need since we can't extend the
// interface without a breaking change.
29 changes: 29 additions & 0 deletions zaptest/testingt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package zaptest

import "testing"

// Just a compile-time test to ensure that TestingT matches the testing.TB
// interface. We could do this in testingt.go but that would put a dependency
// on the "testing" package from zaptest.

var _ TestingT = (testing.TB)(nil)

0 comments on commit 5b0fd11

Please sign in to comment.