TestInitExistingFile tests that Init overwrites existing files
(t *testing.T)
| 93 | |
| 94 | // TestInitExistingFile tests that Init overwrites existing files |
| 95 | func TestInitExistingFile(t *testing.T) { |
| 96 | // Create a temp file |
| 97 | tempFile, err := os.CreateTemp("", "cheat-init-existing-*") |
| 98 | if err != nil { |
| 99 | t.Fatalf("failed to create temp file: %v", err) |
| 100 | } |
| 101 | defer os.Remove(tempFile.Name()) |
| 102 | |
| 103 | // Write initial content |
| 104 | initialContent := "initial content" |
| 105 | if err := os.WriteFile(tempFile.Name(), []byte(initialContent), 0644); err != nil { |
| 106 | t.Fatalf("failed to write initial content: %v", err) |
| 107 | } |
| 108 | |
| 109 | // Initialize with new content |
| 110 | newContent := "new config content" |
| 111 | if err = Init(tempFile.Name(), newContent); err != nil { |
| 112 | t.Errorf("failed to init over existing file: %v", err) |
| 113 | } |
| 114 | |
| 115 | // Verify the file was overwritten |
| 116 | bytes, err := os.ReadFile(tempFile.Name()) |
| 117 | if err != nil { |
| 118 | t.Errorf("failed to read config file: %v", err) |
| 119 | } |
| 120 | if string(bytes) != newContent { |
| 121 | t.Errorf("config not overwritten: got %q, want %q", string(bytes), newContent) |
| 122 | } |
| 123 | } |