Repository: uber-go/zap
File: zbark/debark_test.go (Go)

Code before:
//
// 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 zbark

import (
	"bytes"
	"testing"

	"github.com/Sirupsen/logrus"
	"github.com/stretchr/testify/assert"
	"github.com/uber-go/zap"

	"github.com/uber-common/bark"
)

func newDebark() (zap.Logger, *bytes.Buffer) {
	buf := &bytes.Buffer{}
	logger := logrus.New()
	logger.Out = buf
	logger.Level = logrus.DebugLevel
	return Debarkify(bark.NewLoggerFromLogrus(logger), zap.DebugLevel), buf
}

func TestDebark_CastNoop(t *testing.T) {
	orig := zap.NewJSON(zap.DebugLevel)
	assert.True(t, orig == Debarkify(Barkify(orig), zap.DebugLevel))
}

var levels = []zap.Level{
	zap.DebugLevel,
	zap.InfoLevel,
	zap.WarnLevel,
	zap.ErrorLevel,
}

func TestDebark_Levels(t *testing.T) {
	logger, _ := newDebark()
	for _, l := range append(levels, zap.PanicLevel, zap.FatalLevel) {
		logger.SetLevel(l)
		assert.Equal(t, l, logger.Level())
	}

Review comment (suggestion):
apart from ensuring there's output, we're not really testing the output specifically. I don't think our tests should be tied to the logrus output format, but I also think we should make sure that the output is what we expect. One way to do this is:
- Debarkify a logger, and log something with a few fields
- Log the same message using logrus with the same fields
- ensure both the output buffers are the same

(we should set logrus' `DisableTimestamp` so that the tests don't rely on time)


Code after:
//
// 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 zbark

import (
	"bytes"
	"testing"

	"github.com/uber-go/zap"

	"github.com/Sirupsen/logrus"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"github.com/uber-common/bark"
)

func newLogrus() (bark.Logger, *bytes.Buffer) {
	buf := &bytes.Buffer{}
	logger := logrus.New()
	logger.Out = buf
	logger.Level = logrus.DebugLevel
	logger.Formatter.(*logrus.TextFormatter).DisableTimestamp = true
	return bark.NewLoggerFromLogrus(logger), buf
}

func newDebark() (zap.Logger, *bytes.Buffer) {
	logrus, buf := newLogrus()
	return Debarkify(logrus, zap.DebugLevel), buf
}

func TestLogrusOutputIsTheSame(t *testing.T) {
	logrus, lbuf := newLogrus()
	debark, dbuf := newDebark()

	zfields := []zap.Field{
		zap.Bool("a", true),
		zap.Float64("b", float64(0.1)),
		zap.Int("c", 42),
		zap.String("d", "bar"),
		zap.Int64("e", int64(42)),
		zap.Stringer("g", stringable("g")),