|
1 | 1 | package utils |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
4 | 5 | "os" |
5 | 6 | "path/filepath" |
6 | 7 | "strings" |
@@ -87,3 +88,76 @@ func TestLogFilePath(t *testing.T) { |
87 | 88 | t.Errorf("Logs directory should be absolute path, got: %s", logsDir) |
88 | 89 | } |
89 | 90 | } |
| 91 | + |
| 92 | +func TestCleanupLogs(t *testing.T) { |
| 93 | + // Use a temporary directory for this test |
| 94 | + tempDir, err := os.MkdirTemp("", "surge-logs-test") |
| 95 | + if err != nil { |
| 96 | + t.Fatalf("Failed to create temp dir: %v", err) |
| 97 | + } |
| 98 | + defer os.RemoveAll(tempDir) |
| 99 | + |
| 100 | + // Configure debug to use this temp dir |
| 101 | + ConfigureDebug(tempDir) |
| 102 | + |
| 103 | + // Reset configuration after test (though this changes global state, so might affect other tests potentially) |
| 104 | + // But in these unit tests, parallelism isn't enabled by default. |
| 105 | + defer ConfigureDebug(config.GetLogsDir()) |
| 106 | + |
| 107 | + // Create 10 dummy log files |
| 108 | + baseTime := time.Now() |
| 109 | + for i := 0; i < 10; i++ { |
| 110 | + // Use file name format matching debug.go: debug-YYYYMMDD-HHMMSS.log |
| 111 | + // We add 'i' to time to ensure uniqueness and order |
| 112 | + ts := baseTime.Add(time.Duration(i) * time.Hour) |
| 113 | + filename := fmt.Sprintf("debug-%s.log", ts.Format("20060102-150405")) |
| 114 | + path := filepath.Join(tempDir, filename) |
| 115 | + |
| 116 | + err := os.WriteFile(path, []byte("dummy log"), 0644) |
| 117 | + if err != nil { |
| 118 | + t.Fatalf("Failed to write dummy log: %v", err) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + // Verify we created 10 |
| 123 | + entries, err := os.ReadDir(tempDir) |
| 124 | + if err != nil { |
| 125 | + t.Fatalf("Failed to read dir: %v", err) |
| 126 | + } |
| 127 | + if len(entries) != 10 { |
| 128 | + t.Fatalf("Expected 10 files, got %d", len(entries)) |
| 129 | + } |
| 130 | + |
| 131 | + // Test cleanup: Keep 5 |
| 132 | + CleanupLogs(5) |
| 133 | + |
| 134 | + // Verify we have 5 left |
| 135 | + entries, err = os.ReadDir(tempDir) |
| 136 | + if err != nil { |
| 137 | + t.Fatalf("Failed to read dir after cleanup: %v", err) |
| 138 | + } |
| 139 | + |
| 140 | + if len(entries) != 5 { |
| 141 | + // For debugging failure |
| 142 | + var names []string |
| 143 | + for _, e := range entries { |
| 144 | + names = append(names, e.Name()) |
| 145 | + } |
| 146 | + t.Errorf("Expected 5 files, got %d. Files: %v", len(entries), names) |
| 147 | + } |
| 148 | + |
| 149 | + // Verify we kept the NEWEST ones (indices 5, 6, 7, 8, 9 from loop) |
| 150 | + // The file created with i=9 should be present |
| 151 | + newestTS := baseTime.Add(9 * time.Hour).Format("20060102-150405") |
| 152 | + expectedName := fmt.Sprintf("debug-%s.log", newestTS) |
| 153 | + found := false |
| 154 | + for _, e := range entries { |
| 155 | + if e.Name() == expectedName { |
| 156 | + found = true |
| 157 | + break |
| 158 | + } |
| 159 | + } |
| 160 | + if !found { |
| 161 | + t.Errorf("Expected newest file %s to be present, but it was not", expectedName) |
| 162 | + } |
| 163 | +} |
0 commit comments