Skip to content

Commit

Permalink
Merge pull request #1 from imperfectgo/fix-tests
Browse files Browse the repository at this point in the history
Fix tests for golang 1.10
  • Loading branch information
timonwong authored Apr 12, 2018
2 parents 56ed737 + faabdb9 commit 0e6311b
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 22 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
[![Go Report Card](https://goreportcard.com/badge/github.com/imperfectgo/zap-syslog)](https://goreportcard.com/report/github.com/imperfectgo/zap-syslog)
[![Coverage](https://codecov.io/gh/imperfectgo/zap-syslog/branch/master/graph/badge.svg)](https://codecov.io/gh/imperfectgo/zap-syslog)

Syslog (RFC5424) encoder & syncer for [zap](https://github.com/uber-go/zap).
Syslog ([RFC5424]) encoder & syncer for [zap](https://github.com/uber-go/zap).

[RFC5424]: https://tools.ietf.org/html/rfc5424
20 changes: 10 additions & 10 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,19 @@ const (

var (
_ zapcore.Encoder = &syslogEncoder{}
_ jsonEncoder = zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()).(jsonEncoder)
_ = zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()).(jsonEncoder)
)

// Framing configures RFC6587 TCP transport framing.
type Framing int

// Framing.
const (
NonTransparentFraming Framing = iota
OctetCountingFraming
DefaultFraming = NonTransparentFraming
)

// Framing configures RFC6587 TCP transport framing.
type Framing int

type jsonEncoder interface {
zapcore.Encoder
zapcore.ArrayEncoder
Expand Down Expand Up @@ -92,7 +92,7 @@ func toRFC5424CompliantASCIIString(s string) string {
return strings.Map(rfc5424CompliantASCIIMapper, s)
}

// NewSyslogEncoder creates a syslogEncoder that you should not use because I'm not done with it yet.
// NewSyslogEncoder creates a syslogEncoder.
func NewSyslogEncoder(cfg SyslogEncoderConfig) zapcore.Encoder {
if cfg.Hostname == "" {
hostname, _ := os.Hostname()
Expand Down Expand Up @@ -243,12 +243,12 @@ func (enc *syslogEncoder) EncodeEntry(ent zapcore.Entry, fields []zapcore.Field)
msg.AppendInt(version)

// SP TIMESTAMP
ts := ent.Time.UTC().Format(timestampFormat)
if ts == "" {
ts = nilValue
}
msg.AppendByte(' ')
msg.AppendString(ts)
if ent.Time.IsZero() {
msg.AppendString(nilValue)
} else {
msg.AppendString(ent.Time.Format(timestampFormat))
}

// SP HOSTNAME
msg.AppendByte(' ')
Expand Down
15 changes: 5 additions & 10 deletions encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (

"github.com/imperfectgo/zap-syslog/syslog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/multierr"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
Expand Down Expand Up @@ -66,9 +67,7 @@ func TestToRFC5424CompliantASCIIString(t *testing.T) {

for _, f := range fixtures {
actual := toRFC5424CompliantASCIIString(f.s)
if !assert.Equal(t, f.expected, actual) {
return
}
require.Equal(t, f.expected, actual)
}
}

Expand Down Expand Up @@ -308,17 +307,13 @@ func assertOutput(t testing.TB, desc string, expected string, f func(zapcore.Enc
}).(*syslogEncoder)
f(enc)
buf, err := enc.EncodeEntry(testEntry, nil)
if !assert.NoError(t, err) {
return
}
require.NoError(t, err)
defer buf.Free()

const bom = "\xef\xbb\xbf"
output := buf.String()
i := strings.Index(output, "\xef\xbb\xbf")
if !assert.Condition(t, func() (success bool) { return i > 0 }, "not a valid syslog output") {
return
}
require.Condition(t, func() (success bool) { return i > 0 }, "not a valid syslog output")

jsonString := output[i+len(bom):]
assert.Contains(t, jsonString, expected)
Expand Down Expand Up @@ -417,7 +412,7 @@ func testSyslogEncoderFraming(t *testing.T, framing Framing) {
}

if !strings.HasPrefix(msg, msgPrefix) {
t.Errorf("Wrong syslog output for framing: %s", framing)
t.Errorf("Wrong syslog output for framing: %d", framing)
t.Log(msg)
t.Log(msgPrefix)
return
Expand Down
2 changes: 2 additions & 0 deletions syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var (
_ zapcore.WriteSyncer = &ConnSyncer{}
)

// ConnSyncer describes connection sink for syslog.
type ConnSyncer struct {
network string
raddr string
Expand Down Expand Up @@ -83,6 +84,7 @@ func (s *ConnSyncer) Write(p []byte) (n int, err error) {
return s.conn.Write(p)
}

// Sync implements zapcore.WriteSyncer interface.
func (s *ConnSyncer) Sync() error {
return nil
}
2 changes: 1 addition & 1 deletion syslog/syslog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func TestFacilityPriority(t *testing.T) {
}

if actual != priority {
t.Fatalf("Expected returned priority: %s, actual: %s", facility, actual)
t.Fatalf("Expected returned priority: %d, actual: %d", priority, actual)
}
}

Expand Down

0 comments on commit 0e6311b

Please sign in to comment.