FuzzFindLocalCheatpath exercises findLocalCheatpath with randomised directory depths and .cheat placements. For each fuzz input it builds a temporary directory hierarchy, places a single .cheat directory at a computed level, and asserts that the function always returns it.
(f *testing.F)
| 13 | // temporary directory hierarchy, places a single .cheat directory at a |
| 14 | // computed level, and asserts that the function always returns it. |
| 15 | func FuzzFindLocalCheatpath(f *testing.F) { |
| 16 | // Seed corpus: (totalDepth, cheatPlacement) |
| 17 | f.Add(uint8(1), uint8(0)) // depth 1, .cheat at root |
| 18 | f.Add(uint8(3), uint8(0)) // depth 3, .cheat at root |
| 19 | f.Add(uint8(5), uint8(3)) // depth 5, .cheat at level 3 |
| 20 | f.Add(uint8(1), uint8(1)) // depth 1, .cheat at same level as search dir |
| 21 | f.Add(uint8(10), uint8(5)) // deep hierarchy |
| 22 | |
| 23 | f.Fuzz(func(t *testing.T, totalDepth uint8, cheatPlacement uint8) { |
| 24 | // Clamp to reasonable values to keep I/O bounded |
| 25 | depth := int(totalDepth%15) + 1 // 1..15 |
| 26 | cheatAt := int(cheatPlacement) % (depth + 1) // 0..depth (0 = tempDir itself) |
| 27 | |
| 28 | tempDir := t.TempDir() |
| 29 | |
| 30 | // Build chain: tempDir/d0/d1/…/d{depth-1} |
| 31 | dirs := make([]string, 0, depth+1) |
| 32 | dirs = append(dirs, tempDir) |
| 33 | current := tempDir |
| 34 | for i := 0; i < depth; i++ { |
| 35 | current = filepath.Join(current, fmt.Sprintf("d%d", i)) |
| 36 | if err := os.Mkdir(current, 0755); err != nil { |
| 37 | t.Fatalf("mkdir: %v", err) |
| 38 | } |
| 39 | dirs = append(dirs, current) |
| 40 | } |
| 41 | |
| 42 | // Place .cheat at dirs[cheatAt] |
| 43 | cheatDir := filepath.Join(dirs[cheatAt], ".cheat") |
| 44 | if err := os.Mkdir(cheatDir, 0755); err != nil { |
| 45 | t.Fatalf("mkdir .cheat: %v", err) |
| 46 | } |
| 47 | |
| 48 | // Search from the deepest directory |
| 49 | result := findLocalCheatpath(current) |
| 50 | |
| 51 | // Invariant 1: must find the .cheat we placed |
| 52 | if result != cheatDir { |
| 53 | t.Errorf("depth=%d cheatAt=%d: expected %s, got %s", |
| 54 | depth, cheatAt, cheatDir, result) |
| 55 | } |
| 56 | |
| 57 | // Invariant 2: result must end with /.cheat |
| 58 | if !strings.HasSuffix(result, string(filepath.Separator)+".cheat") { |
| 59 | t.Errorf("result %q does not end with /.cheat", result) |
| 60 | } |
| 61 | |
| 62 | // Invariant 3: result must be under tempDir |
| 63 | if !strings.HasPrefix(result, tempDir) { |
| 64 | t.Errorf("result %q is not under tempDir %s", result, tempDir) |
| 65 | } |
| 66 | }) |
| 67 | } |
nothing calls this directly
no test coverage detected