Skip to content

Commit

Permalink
Drop support for Go 1.8
Browse files Browse the repository at this point in the history
Drop support for Go 1.8. Since all supported versions of Go now have
type aliases, we can drop some duplicated code.
  • Loading branch information
Akshay Shah authored and akshayjshah committed Apr 13, 2018
1 parent 06a2398 commit 6bacc35
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 158 deletions.
88 changes: 17 additions & 71 deletions zaptest/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,81 +18,27 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

// +build !go1.9

package zaptest

import (
"bytes"
"errors"
"io/ioutil"
"strings"
)

// A Syncer is a spy for the Sync portion of zapcore.WriteSyncer.
type Syncer struct {
err error
called bool
}

// SetError sets the error that the Sync method will return.
func (s *Syncer) SetError(err error) {
s.err = err
}

// Sync records that it was called, then returns the user-supplied error (if
// any).
func (s *Syncer) Sync() error {
s.called = true
return s.err
}

// Called reports whether the Sync method was called.
func (s *Syncer) Called() bool {
return s.called
}
import "go.uber.org/zap/internal/ztest"

// A Discarder sends all writes to ioutil.Discard.
type Discarder struct{ Syncer }
type (
// A Syncer is a spy for the Sync portion of zapcore.WriteSyncer.
Syncer = ztest.Syncer

// Write implements io.Writer.
func (d *Discarder) Write(b []byte) (int, error) {
return ioutil.Discard.Write(b)
}
// A Discarder sends all writes to ioutil.Discard.
Discarder = ztest.Discarder

// FailWriter is a WriteSyncer that always returns an error on writes.
type FailWriter struct{ Syncer }
// FailWriter is a WriteSyncer that always returns an error on writes.
FailWriter = ztest.FailWriter

// Write implements io.Writer.
func (w FailWriter) Write(b []byte) (int, error) {
return len(b), errors.New("failed")
}
// ShortWriter is a WriteSyncer whose write method never returns an error,
// but always reports that it wrote one byte less than the input slice's
// length (thus, a "short write").
ShortWriter = ztest.ShortWriter

// ShortWriter is a WriteSyncer whose write method never fails, but
// nevertheless fails to the last byte of the input.
type ShortWriter struct{ Syncer }

// Write implements io.Writer.
func (w ShortWriter) Write(b []byte) (int, error) {
return len(b) - 1, nil
}

// Buffer is an implementation of zapcore.WriteSyncer that sends all writes to
// a bytes.Buffer. It has convenience methods to split the accumulated buffer
// on newlines.
type Buffer struct {
bytes.Buffer
Syncer
}

// Lines returns the current buffer contents, split on newlines.
func (b *Buffer) Lines() []string {
output := strings.Split(b.String(), "\n")
return output[:len(output)-1]
}

// Stripped returns the current buffer contents with the last trailing newline
// stripped.
func (b *Buffer) Stripped() string {
return strings.TrimRight(b.String(), "\n")
}
// Buffer is an implementation of zapcore.WriteSyncer that sends all writes to
// a bytes.Buffer. It has convenience methods to split the accumulated buffer
// on newlines.
Buffer = ztest.Buffer
)
47 changes: 0 additions & 47 deletions zaptest/writer_go_19.go

This file was deleted.

40 changes: 0 additions & 40 deletions zaptest/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,52 +21,12 @@
package zaptest

import (
"bufio"
"errors"
"os"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func readCode(t testing.TB, fname string) []string {
f, err := os.Open(fname)
require.NoError(t, err, "Failed to read %s.", fname)
defer func() {
require.NoError(t, f.Close(), "Error closing file %s.", fname)
}()

var lines []string
s := bufio.NewScanner(f)
for s.Scan() {
l := s.Text()
if len(l) == 0 {
continue
}
if strings.HasPrefix(l, "//") {
continue
}
if strings.HasPrefix(l, "package ") {
continue
}
lines = append(lines, l)
}
return lines
}

func TestCopiedCodeInSync(t *testing.T) {
// Until we drop Go 1.8 support, we need to keep a near-exact copy of the
// ztest package's WriteSyncer test spies in zaptest. This test ensures that
// the two files stay in sync.
assert.Equal(t,
readCode(t, "../internal/ztest/writer.go"),
readCode(t, "writer.go"),
"Writer spy implementations in zaptest and internal/ztest should be identical.",
)
}

func TestSyncer(t *testing.T) {
err := errors.New("sentinel")
s := &Syncer{}
Expand Down

0 comments on commit 6bacc35

Please sign in to comment.