MCPcopy Index your code
hub / github.com/cheat/cheat / TestInitCreateDirectory

Function TestInitCreateDirectory

internal/config/init_test.go:42–72  ·  view source on GitHub ↗

TestInitCreateDirectory tests that Init creates the directory if it doesn't exist

(t *testing.T)

Source from the content-addressed store, hash-verified

40
41// TestInitCreateDirectory tests that Init creates the directory if it doesn't exist
42func 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
75func TestInitWriteError(t *testing.T) {

Callers

nothing calls this directly

Calls 1

InitFunction · 0.85

Tested by

no test coverage detected