TestInitCreateDirectory tests that Init creates the directory if it doesn't exist
(t *testing.T)
| 40 | |
| 41 | // TestInitCreateDirectory tests that Init creates the directory if it doesn't exist |
| 42 | func TestInitCreateDirectory(t *testing.T) { |
| 43 | // Create a temp directory |
| 44 | tempDir, err := os.MkdirTemp("", "cheat-init-test-*") |
| 45 | if err != nil { |
| 46 | t.Fatalf("failed to create temp dir: %v", err) |
| 47 | } |
| 48 | defer os.RemoveAll(tempDir) |
| 49 | |
| 50 | // Path to a config file in a non-existent subdirectory |
| 51 | confPath := filepath.Join(tempDir, "subdir", "conf.yml") |
| 52 | |
| 53 | // Initialize the config file |
| 54 | conf := "test config" |
| 55 | if err = Init(confPath, conf); err != nil { |
| 56 | t.Errorf("failed to init config file: %v", err) |
| 57 | } |
| 58 | |
| 59 | // Verify the directory was created |
| 60 | if _, err := os.Stat(filepath.Dir(confPath)); os.IsNotExist(err) { |
| 61 | t.Error("Init did not create the directory") |
| 62 | } |
| 63 | |
| 64 | // Verify the file was created with correct content |
| 65 | bytes, err := os.ReadFile(confPath) |
| 66 | if err != nil { |
| 67 | t.Errorf("failed to read config file: %v", err) |
| 68 | } |
| 69 | if string(bytes) != conf { |
| 70 | t.Errorf("config content mismatch: got %q, want %q", string(bytes), conf) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // TestInitWriteError tests error handling when file write fails |
| 75 | func TestInitWriteError(t *testing.T) { |