Skip to content

Commit

Permalink
test: Add a test for os.Exit to the logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Dobefu committed Jan 17, 2025
1 parent afadb58 commit 3e88004
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
4 changes: 3 additions & 1 deletion cmd/logger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/Dobefu/csb/cmd/color"
)

var osExit = os.Exit

var (
LOG_VERBOSE byte = 0
LOG_INFO byte = 1
Expand Down Expand Up @@ -70,7 +72,7 @@ func Fatal(format string, a ...any) string {
output := logMessage(LOG_FATAL, format, a...)

if EXIT_ON_FATAL {
os.Exit(1)
osExit(1)
}

return output
Expand Down
15 changes: 15 additions & 0 deletions cmd/logger/main_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package logger

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -85,3 +86,17 @@ func TestLogFatal(t *testing.T) {
SetLogLevel(LOG_FATAL)
assert.NotEmpty(t, Fatal("test"))
}

func TestLogFatalWithExit(t *testing.T) {
SetExitOnFatal(true)
isExitCalled := false

osExit = func(code int) {
isExitCalled = true
}

defer func() { osExit = os.Exit }()

assert.NotEmpty(t, Fatal("test"))
assert.True(t, isExitCalled, "os.Exit should have been called")
}

0 comments on commit 3e88004

Please sign in to comment.