MCPcopy Create free account
hub / github.com/github/gh-aw / TestFileTracker_RollbackAllFiles

Function TestFileTracker_RollbackAllFiles

pkg/cli/file_tracker_test.go:186–251  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

184}
185
186func TestFileTracker_RollbackAllFiles(t *testing.T) {
187 // Create a temporary directory for testing
188 tempDir, err := os.MkdirTemp("", "file-tracker-rollback-test")
189 if err != nil {
190 t.Fatalf("Failed to create temp dir: %v", err)
191 }
192 defer os.RemoveAll(tempDir)
193
194 // Initialize git repository in temp directory
195 gitCmd := []string{"git", "init"}
196 if err := runCommandInDir(gitCmd, tempDir); err != nil {
197 t.Skipf("Skipping test - git not available or failed to init: %v", err)
198 }
199
200 // Change to temp directory
201 oldWd, _ := os.Getwd()
202 defer func() {
203 _ = os.Chdir(oldWd)
204 }()
205 if err := os.Chdir(tempDir); err != nil {
206 t.Fatalf("Failed to change to temp directory: %v", err)
207 }
208
209 // Create file tracker
210 tracker := NewFileTracker()
211
212 // Create an existing file
213 existingFile := filepath.Join(tempDir, "existing.md")
214 originalContent := "# Original Content"
215 if err := os.WriteFile(existingFile, []byte(originalContent), 0644); err != nil {
216 t.Fatalf("Failed to write existing file: %v", err)
217 }
218
219 // Track modification before modifying
220 tracker.TrackModified(existingFile)
221 modifiedContent := "# Modified Content"
222 if err := os.WriteFile(existingFile, []byte(modifiedContent), 0644); err != nil {
223 t.Fatalf("Failed to modify existing file: %v", err)
224 }
225
226 // Create a new file
227 newFile := filepath.Join(tempDir, "new.md")
228 tracker.TrackCreated(newFile)
229 if err := os.WriteFile(newFile, []byte("# New Content"), 0644); err != nil {
230 t.Fatalf("Failed to write new file: %v", err)
231 }
232
233 // Rollback all files
234 if err := tracker.RollbackAllFiles(false); err != nil {
235 t.Errorf("Failed to rollback all files: %v", err)
236 }
237
238 // Verify new file was deleted
239 if _, err := os.Stat(newFile); !os.IsNotExist(err) {
240 t.Errorf("New file %s should have been deleted", newFile)
241 }
242
243 // Verify existing file was restored to original content

Callers

nothing calls this directly

Calls 6

TrackModifiedMethod · 0.95
TrackCreatedMethod · 0.95
RollbackAllFilesMethod · 0.95
runCommandInDirFunction · 0.85
NewFileTrackerFunction · 0.85
ErrorfMethod · 0.45

Tested by

no test coverage detected