Skip to content

Commit 9b04cce

Browse files
authored
Merge pull request #65 from deploymenttheory/dev
Refactor logger tests and add sub-test for Fatal method
2 parents f020c6a + d6821b8 commit 9b04cce

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

logger/zaplogger_logger_test.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -143,26 +143,31 @@ func TestDefaultLogger_Panic(t *testing.T) {
143143
mockLogger.AssertExpectations(t)
144144
}
145145

146-
// TestDefaultLogger_Fatal tests the Fatal method of the defaultLogger struct.
147-
// It confirms that Fatal logs messages at the Fatal level and then terminates the program.
148-
// Given the os.Exit call in Fatal, this test might need to intercept the os.Exit call to prevent test suite termination.
146+
// TestDefaultLogger_Fatal verifies the Fatal method of the defaultLogger struct.
147+
// It ensures that Fatal logs messages at the Fatal level and exits the application with a non-zero status code.
148+
// The test utilizes a mockLogger to capture and assert the call to the zap.Logger's Fatal method.
149149
func TestDefaultLogger_Fatal(t *testing.T) {
150150
mockLogger := NewMockLogger()
151151
dLogger := &defaultLogger{logger: mockLogger.Logger, logLevel: LogLevelFatal}
152152

153-
mockLogger.On("Fatal", "fatal message", mock.Anything).Once()
153+
expectedFatalMsg := "fatal message"
154+
mockLogger.On("Fatal", expectedFatalMsg, mock.Anything).Once()
154155

155-
// Intercept os.Exit calls
156-
originalExit := osExit
157-
defer func() { osExit = originalExit }()
158-
var exitCode int
159-
osExit = func(code int) {
160-
exitCode = code
161-
}
156+
// Since Fatal exits the application, we use a sub-test to capture the exit status
157+
t.Run("TestFatal", func(t *testing.T) {
158+
// Replace os.Exit temporarily to capture exit status
159+
oldExit := osExit
160+
defer func() { osExit = oldExit }()
161+
var exitStatus int
162+
osExit = func(code int) {
163+
exitStatus = code
164+
}
165+
166+
dLogger.Fatal(expectedFatalMsg)
162167

163-
dLogger.Fatal("fatal message")
168+
assert.Equal(t, 1, exitStatus, "Expected non-zero exit status")
169+
})
164170

165-
assert.Equal(t, 1, exitCode, "Fatal should terminate the program with exit code 1")
166171
mockLogger.AssertExpectations(t)
167172
}
168173

0 commit comments

Comments
 (0)