MCPcopy
hub / github.com/cheat/cheat / TestFindLocalCheatpathPermissionDenied

Function TestFindLocalCheatpathPermissionDenied

internal/config/config_test.go:214–261  ·  view source on GitHub ↗

TestFindLocalCheatpathPermissionDenied tests that unreadable ancestor directories are skipped and the walk continues upward.

(t *testing.T)

Source from the content-addressed store, hash-verified

212// TestFindLocalCheatpathPermissionDenied tests that unreadable ancestor
213// directories are skipped and the walk continues upward.
214func TestFindLocalCheatpathPermissionDenied(t *testing.T) {
215 if runtime.GOOS == "windows" {
216 t.Skip("Unix permissions do not apply on Windows")
217 }
218 if os.Getuid() == 0 {
219 t.Skip("test requires non-root user")
220 }
221
222 tempDir := t.TempDir()
223
224 // Resolve symlinks (macOS /var -> /private/var)
225 tempDir, err := filepath.EvalSymlinks(tempDir)
226 if err != nil {
227 t.Fatalf("failed to resolve symlinks: %v", err)
228 }
229
230 // Create tempDir/.cheat (the target we want found)
231 cheatDir := filepath.Join(tempDir, ".cheat")
232 if err := os.Mkdir(cheatDir, 0755); err != nil {
233 t.Fatalf("failed to create .cheat dir: %v", err)
234 }
235
236 // Create tempDir/restricted/ with its own .cheat and sub/
237 restricted := filepath.Join(tempDir, "restricted")
238 if err := os.Mkdir(restricted, 0755); err != nil {
239 t.Fatalf("failed to create restricted dir: %v", err)
240 }
241 if err := os.Mkdir(filepath.Join(restricted, ".cheat"), 0755); err != nil {
242 t.Fatalf("failed to create restricted .cheat dir: %v", err)
243 }
244 subDir := filepath.Join(restricted, "sub")
245 if err := os.Mkdir(subDir, 0755); err != nil {
246 t.Fatalf("failed to create sub dir: %v", err)
247 }
248
249 // Make restricted/ unreadable — blocks stat of children
250 if err := os.Chmod(restricted, 0000); err != nil {
251 t.Fatalf("failed to chmod: %v", err)
252 }
253 t.Cleanup(func() { os.Chmod(restricted, 0755) })
254
255 // Walk from restricted/sub: stat("restricted/sub/.cheat") fails (EACCES),
256 // stat("restricted/.cheat") fails (EACCES), walk continues to tempDir/.cheat
257 result := findLocalCheatpath(subDir)
258 if result != cheatDir {
259 t.Errorf("expected %s (walked past restricted dir), got %s", cheatDir, result)
260 }
261}
262
263// TestConfig asserts that the configs are loaded correctly
264func TestConfigSuccessful(t *testing.T) {

Callers

nothing calls this directly

Calls 1

findLocalCheatpathFunction · 0.85

Tested by

no test coverage detected