-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test: add tests to improve code coverage for logging #244
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package logger | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/rs/zerolog" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var mu sync.Mutex | ||
|
||
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, _ *bytes.Buffer) { | ||
t.Helper() | ||
if os.Getenv("BE_CRASHER") == "1" { | ||
log.Fatal("fatal message") | ||
|
||
return | ||
} | ||
cmd := exec.Command(os.Args[0], "-test.run=TestLogger") // #nosec | ||
cmd.Env = append(os.Environ(), "BE_CRASHER=1") | ||
err := cmd.Run() | ||
var exitError *exec.ExitError | ||
if errors.As(err, &exitError) && !exitError.Success() { | ||
return | ||
} | ||
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) { | ||
t.Parallel() | ||
|
||
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 { | ||
tc := tc | ||
|
||
t.Run(fmt.Sprintf("LogLevel_%s", tc.level), func(t *testing.T) { | ||
t.Parallel() | ||
|
||
mu.Lock() | ||
defer mu.Unlock() | ||
|
||
log := New(tc.level) | ||
require.NotNil(t, log) | ||
assert.Equal(t, tc.expectedLevel, log.localLevel) | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hey @emeeker2002 typically you'd want to have an expected result in your test struct. and then in each test case, you'd set the expected result. That way all assets can be handled in the for loop at the end.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would explain why the linter was wanting t.Helper in each function which was a bit odd to me.