-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmjgologger_test.go
More file actions
406 lines (344 loc) · 9.74 KB
/
mjgologger_test.go
File metadata and controls
406 lines (344 loc) · 9.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
package mjgologger
import (
"bufio"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
func TestSetupAndStop(t *testing.T) {
testFile := "test_setup.log"
defer os.Remove(testFile)
err := Setup(testFile)
if err != nil {
t.Fatalf("Setup() returned error: %v", err)
}
if myLogger == nil {
t.Error("Setup() failed to initialize logger")
}
if file == nil {
t.Error("Setup() failed to open file")
}
err = Stop()
if err != nil {
t.Errorf("Stop() returned error: %v", err)
}
// Verify file was created
if _, err := os.Stat(testFile); os.IsNotExist(err) {
t.Error("Setup() did not create log file")
}
}
func TestInfo(t *testing.T) {
testFile := "test_info.log"
defer os.Remove(testFile)
err := Setup(testFile)
if err != nil {
t.Fatalf("Setup() returned error: %v", err)
}
defer func() {
if err := Stop(); err != nil {
t.Errorf("Stop() returned error: %v", err)
}
}()
Info("Test info message")
Info("Test with args: %s %d", "hello", 42)
// Read and verify log content
if err := file.Sync(); err != nil {
t.Fatalf("file.Sync() returned error: %v", err)
}
content := readLogFile(t, testFile)
if !strings.Contains(content, "[INFO]") {
t.Error("Info() did not write [INFO] prefix")
}
if !strings.Contains(content, "Test info message") {
t.Error("Info() did not write message")
}
if !strings.Contains(content, "hello 42") {
t.Error("Info() did not format args correctly")
}
}
func TestDebug(t *testing.T) {
testFile := "test_debug.log"
defer os.Remove(testFile)
err := Setup(testFile)
if err != nil {
t.Fatalf("Setup() returned error: %v", err)
}
defer func() {
if err := Stop(); err != nil {
t.Errorf("Stop() returned error: %v", err)
}
}()
Debug("Test debug message")
Debug("Debug with number: %d", 123)
if err := file.Sync(); err != nil {
t.Fatalf("file.Sync() returned error: %v", err)
}
content := readLogFile(t, testFile)
if !strings.Contains(content, "[DEBUG]") {
t.Error("Debug() did not write [DEBUG] prefix")
}
if !strings.Contains(content, "Test debug message") {
t.Error("Debug() did not write message")
}
}
func TestWarn(t *testing.T) {
testFile := "test_warn.log"
defer os.Remove(testFile)
err := Setup(testFile)
if err != nil {
t.Fatalf("Setup() returned error: %v", err)
}
defer func() {
if err := Stop(); err != nil {
t.Errorf("Stop() returned error: %v", err)
}
}()
Warn("Test warning message")
Warn("Warning with value: %v", true)
if err := file.Sync(); err != nil {
t.Fatalf("file.Sync() returned error: %v", err)
}
content := readLogFile(t, testFile)
if !strings.Contains(content, "[WARN]") {
t.Error("Warn() did not write [WARN] prefix")
}
if !strings.Contains(content, "Test warning message") {
t.Error("Warn() did not write message")
}
}
func TestError(t *testing.T) {
testFile := "test_error.log"
defer os.Remove(testFile)
err := Setup(testFile)
if err != nil {
t.Fatalf("Setup() returned error: %v", err)
}
defer func() {
if err := Stop(); err != nil {
t.Errorf("Stop() returned error: %v", err)
}
}()
Error("Test error message")
Error("Error with code: %d", 500)
if err := file.Sync(); err != nil {
t.Fatalf("file.Sync() returned error: %v", err)
}
content := readLogFile(t, testFile)
if !strings.Contains(content, "[ERROR]") {
t.Error("Error() did not write [ERROR] prefix")
}
if !strings.Contains(content, "Test error message") {
t.Error("Error() did not write message")
}
}
func TestCallerInfo(t *testing.T) {
testFile := "test_caller.log"
defer os.Remove(testFile)
err := Setup(testFile)
if err != nil {
t.Fatalf("Setup() returned error: %v", err)
}
defer func() {
if err := Stop(); err != nil {
t.Errorf("Stop() returned error: %v", err)
}
}()
Info("Test caller information")
if err := file.Sync(); err != nil {
t.Fatalf("file.Sync() returned error: %v", err)
}
content := readLogFile(t, testFile)
// Verify that caller information is included (file path and line number)
if !strings.Contains(content, "mjgologger_test.go") {
t.Error("Log did not include caller file information")
}
if !strings.Contains(content, "[") || !strings.Contains(content, "]") {
t.Error("Log did not include proper formatting for caller info")
}
}
func TestMultipleLogLevels(t *testing.T) {
testFile := "test_multiple.log"
defer os.Remove(testFile)
err := Setup(testFile)
if err != nil {
t.Fatalf("Setup() returned error: %v", err)
}
defer func() {
if err := Stop(); err != nil {
t.Errorf("Stop() returned error: %v", err)
}
}()
Info("Info message")
Debug("Debug message")
Warn("Warn message")
Error("Error message")
if err := file.Sync(); err != nil {
t.Fatalf("file.Sync() returned error: %v", err)
}
content := readLogFile(t, testFile)
// Verify all log levels are present
if !strings.Contains(content, "[INFO]") {
t.Error("Missing [INFO] level")
}
if !strings.Contains(content, "[DEBUG]") {
t.Error("Missing [DEBUG] level")
}
if !strings.Contains(content, "[WARN]") {
t.Error("Missing [WARN] level")
}
if !strings.Contains(content, "[ERROR]") {
t.Error("Missing [ERROR] level")
}
}
func TestSetupWithInvalidPath(t *testing.T) {
// Try to create a file in an invalid directory
invalidPath := "Z:\\nonexistent\\directory\\test.log"
err := Setup(invalidPath)
// The function should return an error for invalid paths
if err == nil {
t.Error("Setup with invalid path should return an error")
}
}
func TestSetupRenamesExistingFile(t *testing.T) {
testFile := "test_rename.log"
defer os.Remove(testFile)
// Clean up any renamed files after test
defer func() {
matches, _ := filepath.Glob(testFile + ".*")
for _, match := range matches {
os.Remove(match)
}
}()
// First Setup: Create initial log file
err := Setup(testFile)
if err != nil {
t.Fatalf("First Setup() returned error: %v", err)
}
Info("First log entry")
err = Stop()
if err != nil {
t.Fatalf("First Stop() returned error: %v", err)
}
// Verify first file was created
if _, err := os.Stat(testFile); os.IsNotExist(err) {
t.Fatal("First Setup() did not create log file")
}
// Read content from first file
firstContent := readLogFile(t, testFile)
if !strings.Contains(firstContent, "First log entry") {
t.Error("First log file does not contain expected content")
}
// Wait a second to ensure timestamp will be different
time.Sleep(1 * time.Second)
// Second Setup: Should rename existing file and create new one
err = Setup(testFile)
if err != nil {
t.Fatalf("Second Setup() returned error: %v", err)
}
Info("Second log entry")
err = Stop()
if err != nil {
t.Fatalf("Second Stop() returned error: %v", err)
}
// Verify original filename still exists (as new file)
if _, err := os.Stat(testFile); os.IsNotExist(err) {
t.Fatal("Second Setup() did not create new log file")
}
// Verify the renamed file exists (with timestamp)
matches, err := filepath.Glob(testFile + ".*")
if err != nil {
t.Fatalf("Failed to search for renamed files: %v", err)
}
if len(matches) != 1 {
t.Fatalf("Expected 1 renamed file, found %d", len(matches))
}
// Verify the renamed file has the timestamp format
renamedFile := matches[0]
if !strings.HasPrefix(renamedFile, testFile+".") {
t.Errorf("Renamed file does not have expected prefix: %s", renamedFile)
}
// Verify renamed file contains old content
renamedContent := readLogFile(t, renamedFile)
if !strings.Contains(renamedContent, "First log entry") {
t.Error("Renamed file does not contain original content")
}
if strings.Contains(renamedContent, "Second log entry") {
t.Error("Renamed file should not contain new content")
}
// Verify new file contains only new content
newContent := readLogFile(t, testFile)
if strings.Contains(newContent, "First log entry") {
t.Error("New file should not contain old content")
}
if !strings.Contains(newContent, "Second log entry") {
t.Error("New file does not contain new content")
}
}
func TestSetupMultipleRenames(t *testing.T) {
testFile := "test_multiple_rename.log"
defer os.Remove(testFile)
// Clean up any renamed files after test
defer func() {
matches, _ := filepath.Glob(testFile + ".*")
for _, match := range matches {
os.Remove(match)
}
}()
// Create and rename files multiple times
for i := 1; i <= 3; i++ {
err := Setup(testFile)
if err != nil {
t.Fatalf("Setup() iteration %d returned error: %v", i, err)
}
Info("Log entry %d", i)
err = Stop()
if err != nil {
t.Fatalf("Stop() iteration %d returned error: %v", i, err)
}
time.Sleep(1 * time.Second) // Ensure different timestamps
}
// Verify original file exists
if _, err := os.Stat(testFile); os.IsNotExist(err) {
t.Fatal("Final log file does not exist")
}
// Verify multiple renamed files exist
matches, err := filepath.Glob(testFile + ".*")
if err != nil {
t.Fatalf("Failed to search for renamed files: %v", err)
}
if len(matches) != 2 {
t.Errorf("Expected 2 renamed files, found %d", len(matches))
}
// Verify final file contains only the latest entry
finalContent := readLogFile(t, testFile)
if !strings.Contains(finalContent, "Log entry 3") {
t.Error("Final file does not contain latest entry")
}
if strings.Contains(finalContent, "Log entry 1") || strings.Contains(finalContent, "Log entry 2") {
t.Error("Final file should not contain old entries")
}
}
// Helper function to read log file content
func readLogFile(t *testing.T, filename string) string {
f, err := os.Open(filename)
if err != nil {
t.Fatalf("Failed to open log file: %v", err)
}
defer func() {
if err := f.Close(); err != nil {
t.Errorf("Failed to close log file: %v", err)
}
}()
var content strings.Builder
scanner := bufio.NewScanner(f)
for scanner.Scan() {
content.WriteString(scanner.Text())
content.WriteString("\n")
}
if err := scanner.Err(); err != nil {
t.Fatalf("Failed to read log file: %v", err)
}
return content.String()
}