@@ -11,10 +11,6 @@ import (
1111 "go.uber.org/zap/zapcore"
1212)
1313
14- // osExit is a variable that holds a reference to os.Exit function.
15- // It allows overriding os.Exit in tests to prevent exiting the test runner.
16- var osExit = os .Exit
17-
1814// TestDefaultLogger_SetLevel tests the SetLevel method of defaultLogger
1915func TestDefaultLogger_SetLevel (t * testing.T ) {
2016 logger := zap .NewNop ()
@@ -64,151 +60,6 @@ func TestDefaultLogger_GetLogLevel(t *testing.T) {
6460 }
6561}
6662
67- /*
68- // TestDefaultLogger_Debug verifies that the Debug method of the defaultLogger struct correctly
69- // invokes the underlying zap.Logger's Debug method when the log level is set to allow Debug messages.
70- // The test uses a mockLogger to simulate the zap.Logger behavior, allowing verification of method calls
71- // without actual logging output. This ensures that the Debug method adheres to the expected behavior
72- // based on the current log level setting, providing confidence in the logging logic's correctness.
73- func TestDefaultLogger_Debug(t *testing.T) {
74- mockLogger := NewMockLogger()
75- dLogger := &defaultLogger{logger: mockLogger.Logger, logLevel: LogLevelDebug}
76-
77- expectedMessage := "test message"
78- mockLogger.On("Debug", expectedMessage, mock.Anything).Once()
79-
80- dLogger.Debug(expectedMessage)
81-
82- mockLogger.AssertExpectations(t)
83- }
84-
85- // TestDefaultLogger_Info verifies the Info method of the defaultLogger struct.
86- // It ensures that Info logs messages at the Info level when the logger's level allows for it.
87- // The test uses a mockLogger to intercept and verify the call to the underlying zap.Logger's Info method.
88- func TestDefaultLogger_Info(t *testing.T) {
89- mockLogger := NewMockLogger()
90- dLogger := &defaultLogger{logger: mockLogger.Logger, logLevel: LogLevelInfo}
91-
92- expectedMessage := "info message"
93- mockLogger.On("Info", expectedMessage, mock.Anything).Once()
94-
95- dLogger.Info(expectedMessage)
96-
97- mockLogger.AssertExpectations(t)
98- }
99-
100- // TestDefaultLogger_Warn verifies the Warn method of the defaultLogger struct.
101- // This test checks that Warn correctly logs messages at the Warn level based on the logger's current level.
102- // The behavior is validated using a mockLogger to capture and assert the call to the zap.Logger's Warn method.
103- func TestDefaultLogger_Warn(t *testing.T) {
104- mockLogger := NewMockLogger()
105- dLogger := &defaultLogger{logger: mockLogger.Logger, logLevel: LogLevelWarn}
106-
107- expectedMessage := "warn message"
108- mockLogger.On("Warn", expectedMessage, mock.Anything).Once()
109-
110- dLogger.Warn(expectedMessage)
111-
112- mockLogger.AssertExpectations(t)
113- }
114-
115- // TestDefaultLogger_Error checks the functionality of the Error method in the defaultLogger struct.
116- // It ensures that Error logs messages at the Error level and returns an error as expected.
117- // The test utilizes a mockLogger to track and affirm the invocation of zap.Logger's Error method.
118- func TestDefaultLogger_Error(t *testing.T) {
119- mockLogger := NewMockLogger()
120- dLogger := &defaultLogger{logger: mockLogger.Logger, logLevel: LogLevelError}
121-
122- expectedErrorMsg := "error message"
123- // Mock the Error method to return nil, indicating no error
124- mockLogger.On("Error", expectedErrorMsg, mock.Anything).Once().Return(fmt.Errorf("error message"))
125-
126- err := dLogger.Error(expectedErrorMsg)
127-
128- assert.NoError(t, err) // Check that the error returned is nil
129- mockLogger.AssertExpectations(t)
130- }
131-
132- // TestDefaultLogger_Panic ensures the Panic method of the defaultLogger behaves correctly.
133- // This test verifies that Panic logs messages at the Panic level and triggers a panic as expected.
134- // Due to the nature of panic, this test needs to recover from the panic to verify the behavior.
135- func TestDefaultLogger_Panic(t *testing.T) {
136- mockLogger := NewMockLogger()
137- dLogger := &defaultLogger{logger: mockLogger.Logger, logLevel: LogLevelPanic}
138-
139- expectedMessage := "panic message"
140- mockLogger.On("Panic", expectedMessage, mock.Anything).Once()
141-
142- // Assert that calling Panic method triggers a panic
143- assert.Panics(t, func() { dLogger.Panic(expectedMessage) }, "The Panic method should trigger a panic")
144-
145- mockLogger.AssertExpectations(t)
146- }
147-
148-
149- // TestDefaultLogger_Fatal verifies the Fatal method of the defaultLogger struct.
150- // It ensures that Fatal logs messages at the Fatal level and exits the application with a non-zero status code.
151- // The test utilizes a mockLogger to capture and assert the call to the zap.Logger's Fatal method.
152- func TestDefaultLogger_Fatal(t *testing.T) {
153- mockLogger := NewMockLogger()
154- dLogger := &defaultLogger{logger: mockLogger.Logger, logLevel: LogLevelFatal}
155-
156- expectedFatalMsg := "fatal message"
157- // Use mock.Anything for fields argument to avoid strict matching issues
158- mockLogger.On("Fatal", expectedFatalMsg, mock.Anything).Once()
159-
160- // Replace os.Exit temporarily to capture exit status
161- oldExit := osExit
162- var exitStatus int
163- osExit = func(code int) {
164- exitStatus = code
165- }
166- // Ensure the original os.Exit is restored after this test
167- defer func() { osExit = oldExit }()
168-
169- // Execute the Fatal method, which is expected to call os.Exit
170- dLogger.Fatal(expectedFatalMsg)
171-
172- // Verify the exit status is set as expected
173- assert.Equal(t, 1, exitStatus, "Expected non-zero exit status")
174-
175- // Confirm that the mock logger's expectations are met
176- mockLogger.AssertExpectations(t)
177- }
178- */
179- // Debug mocks the Debug method of the Logger interface
180- func (m * MockLogger ) Debug (msg string , fields ... zapcore.Field ) {
181- m .Called (msg , fields )
182- }
183-
184- // Info mocks the Info method of the Logger interface
185- func (m * MockLogger ) Info (msg string , fields ... zapcore.Field ) {
186- m .Called (msg , fields )
187- }
188-
189- // Warn mocks the Warn method of the Logger interface
190- func (m * MockLogger ) Warn (msg string , fields ... zapcore.Field ) {
191- m .Called (msg , fields )
192- }
193-
194- // Error mocks the Error method of the Logger interface
195- func (m * MockLogger ) Error (msg string , fields ... zapcore.Field ) error {
196- args := m .Called (msg , fields )
197- return args .Error (0 )
198- }
199-
200- // With mocks the With method of the Logger interface
201- func (m * MockLogger ) With (fields ... zapcore.Field ) Logger {
202- args := m .Called (fields )
203- return args .Get (0 ).(Logger )
204- }
205-
206- // GetLogLevel mocks the GetLogLevel method of the Logger interface
207- func (m * MockLogger ) GetLogLevel () LogLevel {
208- args := m .Called ()
209- return args .Get (0 ).(LogLevel )
210- }
211-
21263// TestGetLoggerBasedOnEnv tests the GetLoggerBasedOnEnv function for different environment settings
21364func TestGetLoggerBasedOnEnv (t * testing.T ) {
21465 tests := []struct {
0 commit comments