Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ensure (Logger).exit is non-nil before calling (#132)
On an empty logger (which we use), it's possible for (Logger).Fatal to panic when attempting to call exit. This ensures that `l.exit` isn't nil before calling it.
- Loading branch information
Showing
2 changed files
with
42 additions
and
0 deletions.
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,35 @@ | ||
package slog | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"cdr.dev/slog/internal/assert" | ||
) | ||
|
||
func TestExit(t *testing.T) { | ||
// This can't be parallel since it modifies a global variable. | ||
t.Run("defaultExitFn", func(t *testing.T) { | ||
var ( | ||
ctx = context.Background() | ||
log Logger | ||
defaultExitFnCalled bool | ||
) | ||
|
||
prevExitFn := defaultExitFn | ||
t.Cleanup(func() { defaultExitFn = prevExitFn }) | ||
|
||
defaultExitFn = func(_ int) { | ||
defaultExitFnCalled = true | ||
} | ||
|
||
log.Debug(ctx, "hi") | ||
log.Info(ctx, "hi") | ||
log.Warn(ctx, "hi") | ||
log.Error(ctx, "hi") | ||
log.Critical(ctx, "hi") | ||
log.Fatal(ctx, "hi") | ||
|
||
assert.True(t, "default exit fn used", defaultExitFnCalled) | ||
}) | ||
} |