Skip to content

Commit

Permalink
test: add tests to improve code coverage for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
emeeker2002 committed Jul 1, 2024
1 parent 49801f4 commit 9d0d626
Showing 1 changed file with 120 additions and 0 deletions.
120 changes: 120 additions & 0 deletions pkg/logger/logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package logger

import (
"bytes"
"fmt"
"os"
"os/exec"
"testing"

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

type loggerTest struct {
name string
logLevel string
testFunction func(t *testing.T, log *Logger, buf *bytes.Buffer)
}

func TestLogger(t *testing.T) {
t.Parallel()

tests := []loggerTest{
{
name: "Debug level logging",
logLevel: "debug",
testFunction: func(t *testing.T, log *Logger, buf *bytes.Buffer) {
t.Helper()
log.Debug("debug message")
assert.Contains(t, buf.String(), "debug message")
},
},
{
name: "Info level logging",
logLevel: "info",
testFunction: func(t *testing.T, log *Logger, buf *bytes.Buffer) {
t.Helper()
log.Info("info message")
assert.Contains(t, buf.String(), "info message")
},
},
{
name: "Warn level logging",
logLevel: "warn",
testFunction: func(t *testing.T, log *Logger, buf *bytes.Buffer) {
t.Helper()
log.Warn("warn message")
assert.Contains(t, buf.String(), "warn message")
},
},
{
name: "Error level logging",
logLevel: "error",
testFunction: func(t *testing.T, log *Logger, buf *bytes.Buffer) {
t.Helper()
log.Error("error message")
assert.Contains(t, buf.String(), "error message")
},
},
{
name: "Fatal level logging",
logLevel: "fatal",
testFunction: func(t *testing.T, log *Logger, buf *bytes.Buffer) {

Check failure on line 64 in pkg/logger/logger_test.go

View workflow job for this annotation

GitHub Actions / runner / golangci-lint

[golangci] reported by reviewdog 🐶 unused-parameter: parameter 'buf' seems to be unused, consider removing or renaming it as _ (revive) Raw Output: pkg/logger/logger_test.go:64:50: unused-parameter: parameter 'buf' seems to be unused, consider removing or renaming it as _ (revive) testFunction: func(t *testing.T, log *Logger, buf *bytes.Buffer) { ^
t.Helper()
if os.Getenv("BE_CRASHER") == "1" {
log.Fatal("fatal message")

return
}
cmd := exec.Command(os.Args[0], "-test.run=TestLogger")

Check failure on line 71 in pkg/logger/logger_test.go

View workflow job for this annotation

GitHub Actions / runner / golangci-lint

[golangci] reported by reviewdog 🐶 G204: Subprocess launched with a potential tainted input or cmd arguments (gosec) Raw Output: pkg/logger/logger_test.go:71:12: G204: Subprocess launched with a potential tainted input or cmd arguments (gosec) cmd := exec.Command(os.Args[0], "-test.run=TestLogger") ^
cmd.Env = append(os.Environ(), "BE_CRASHER=1")
err := cmd.Run()
if e, ok := err.(*exec.ExitError); ok && !e.Success() {

Check failure on line 74 in pkg/logger/logger_test.go

View workflow job for this annotation

GitHub Actions / runner / golangci-lint

[golangci] reported by reviewdog 🐶 unnecessary leading newline (whitespace) Raw Output: pkg/logger/logger_test.go:74:60: unnecessary leading newline (whitespace) ^

Check failure on line 75 in pkg/logger/logger_test.go

View workflow job for this annotation

GitHub Actions / runner / golangci-lint

[golangci] reported by reviewdog 🐶 File is not `gofumpt`-ed with `-extra` (gofumpt) Raw Output: pkg/logger/logger_test.go:75: File is not `gofumpt`-ed with `-extra` (gofumpt)
return

Check failure on line 77 in pkg/logger/logger_test.go

View workflow job for this annotation

GitHub Actions / runner / golangci-lint

[golangci] reported by reviewdog 🐶 File is not `gofumpt`-ed with `-extra` (gofumpt) Raw Output: pkg/logger/logger_test.go:77: File is not `gofumpt`-ed with `-extra` (gofumpt)
}

Check failure on line 78 in pkg/logger/logger_test.go

View workflow job for this annotation

GitHub Actions / runner / golangci-lint

[golangci] reported by reviewdog 🐶 unnecessary trailing newline (whitespace) Raw Output: pkg/logger/logger_test.go:78:5: unnecessary trailing newline (whitespace) ^
t.Fatalf("process ran with err %v, want exit status 1", err)
},
},
}

for _, tc := range tests {
tc := tc // capture range variable

t.Run(tc.name, func(t *testing.T) {
t.Parallel()

var buf bytes.Buffer
logger := zerolog.New(&buf).With().Timestamp().Logger()
log := &Logger{logger: &logger}

tc.testFunction(t, log, &buf)
})
}
}

func TestNewLogger(t *testing.T) {

Check failure on line 99 in pkg/logger/logger_test.go

View workflow job for this annotation

GitHub Actions / runner / golangci-lint

[golangci] reported by reviewdog 🐶 Function TestNewLogger missing the call to method parallel (paralleltest) Raw Output: pkg/logger/logger_test.go:99:1: Function TestNewLogger missing the call to method parallel (paralleltest) func TestNewLogger(t *testing.T) { ^
tests := []struct {
level string
expectedLevel zerolog.Level
}{
{"debug", zerolog.DebugLevel},
{"info", zerolog.InfoLevel},
{"warn", zerolog.WarnLevel},
{"error", zerolog.ErrorLevel},
{"invalid", zerolog.InfoLevel},
}

for _, tc := range tests {

Check failure on line 111 in pkg/logger/logger_test.go

View workflow job for this annotation

GitHub Actions / runner / golangci-lint

[golangci] reported by reviewdog 🐶 Range statement for test TestNewLogger missing the call to method parallel in test Run (paralleltest) Raw Output: pkg/logger/logger_test.go:111:2: Range statement for test TestNewLogger missing the call to method parallel in test Run (paralleltest) for _, tc := range tests { ^
tc := tc

t.Run(fmt.Sprintf("LogLevel_%s", tc.level), func(t *testing.T) {
log := New(tc.level)
require.NotNil(t, log)
assert.Equal(t, tc.expectedLevel, zerolog.GlobalLevel())
})
}
}

0 comments on commit 9d0d626

Please sign in to comment.