Skip to content

Commit

Permalink
add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
belimawr committed Jan 26, 2024
1 parent 36858ba commit 24878e6
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion logp/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

func TestLogger(t *testing.T) {
Expand Down Expand Up @@ -217,7 +218,7 @@ func TestLoggingECSFields(t *testing.T) {
}
}

func TestWithFileOutput(t *testing.T) {
func TestCreatingNewLoggerWithDifferentOutput(t *testing.T) {
var tempDir1, tempDir2 string
// Because of the way logp and zap work, when the test finishes, the log
// file is still open, this creates a problem on Windows because the
Expand Down Expand Up @@ -332,3 +333,49 @@ func TestWithFileOutput(t *testing.T) {
t.Fatalf("expecting 'message' to be '%s, got '%s' instead", expectedLogMessage, logEntry["message"])
}
}

func TestWithFileOrStderrOutput(t *testing.T) {
testCases := []struct {
name string
toStderr bool
toFile bool
}{
{
name: "stderr output",
toStderr: true,
toFile: false,
},
{
name: "file output",
toStderr: false,
toFile: true,
},
}

notEnabledLevels := []zapcore.Level{zapcore.InfoLevel, zapcore.DebugLevel}
enabledLevels := []zapcore.Level{zapcore.ErrorLevel, zapcore.PanicLevel}

for _, tc := range testCases {
cfg := DefaultConfig(DefaultEnvironment)
cfg.ToStderr = tc.toStderr
cfg.ToFiles = tc.toFile
cfg.Level = ErrorLevel

f := WithFileOrStderrOutput(cfg)
core := f(zapcore.NewNopCore())

t.Run(tc.name, func(t *testing.T) {
for _, l := range notEnabledLevels {
if core.Enabled(l) {
t.Errorf("level %s must not be enabled", l.String())
}
}

for _, l := range enabledLevels {
if !core.Enabled(l) {
t.Errorf("level %s must Vbe enabled", l.String())
}
}
})
}
}

0 comments on commit 24878e6

Please sign in to comment.