@@ -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 .
149149func 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