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

Function TestCopyUnreadableSource

internal/sheet/copy_error_test.go:106–145  ·  view source on GitHub ↗

TestCopyUnreadableSource verifies that Copy returns an error when the source file cannot be opened (e.g., permission denied).

(t *testing.T)

Source from the content-addressed store, hash-verified

104// TestCopyUnreadableSource verifies that Copy returns an error when the source
105// file cannot be opened (e.g., permission denied).
106func TestCopyUnreadableSource(t *testing.T) {
107 if runtime.GOOS == "windows" {
108 t.Skip("chmod does not restrict reads on Windows")
109 }
110
111 src, err := os.CreateTemp("", "copy-test-unreadable-*")
112 if err != nil {
113 t.Fatalf("failed to create temp file: %v", err)
114 }
115 defer os.Remove(src.Name())
116
117 if _, err := src.WriteString("test content"); err != nil {
118 t.Fatalf("failed to write content: %v", err)
119 }
120 src.Close()
121
122 sheet := &Sheet{
123 Title: "test",
124 Path: src.Name(),
125 CheatPath: "test",
126 }
127
128 dest := filepath.Join(os.TempDir(), "copy-unreadable-test.txt")
129 defer os.Remove(dest)
130
131 if err := os.Chmod(src.Name(), 0000); err != nil {
132 t.Skip("Cannot change file permissions on this platform")
133 }
134 defer os.Chmod(src.Name(), 0644)
135
136 err = sheet.Copy(dest)
137 if err == nil {
138 t.Error("expected Copy to fail with permission error")
139 }
140
141 // Destination should not exist since the error occurs before it is created
142 if _, err := os.Stat(dest); !os.IsNotExist(err) {
143 t.Error("destination file should not exist after open failure")
144 }
145}

Callers

nothing calls this directly

Calls 1

CopyMethod · 0.95

Tested by

no test coverage detected