generateFileTree creates n files under root in a realistic nested directory structure.
(t testing.TB, root string, n int, seed int64)
| 35 | |
| 36 | // generateFileTree creates n files under root in a realistic nested directory structure. |
| 37 | func generateFileTree(t testing.TB, root string, n int, seed int64) { |
| 38 | t.Helper() |
| 39 | rng := rand.New(rand.NewSource(seed)) //nolint:gosec // deterministic benchmarks |
| 40 | |
| 41 | numDirs := n / 5 |
| 42 | if numDirs < 10 { |
| 43 | numDirs = 10 |
| 44 | } |
| 45 | dirs := make([]string, 0, numDirs) |
| 46 | for i := 0; i < numDirs; i++ { |
| 47 | depth := rng.Intn(6) + 1 |
| 48 | parts := make([]string, depth) |
| 49 | for d := 0; d < depth; d++ { |
| 50 | parts[d] = dirNames[rng.Intn(len(dirNames))] |
| 51 | } |
| 52 | dirs = append(dirs, filepath.Join(parts...)) |
| 53 | } |
| 54 | |
| 55 | created := make(map[string]struct{}) |
| 56 | for _, d := range dirs { |
| 57 | full := filepath.Join(root, d) |
| 58 | if _, ok := created[full]; ok { |
| 59 | continue |
| 60 | } |
| 61 | require.NoError(t, os.MkdirAll(full, 0o755)) |
| 62 | created[full] = struct{}{} |
| 63 | } |
| 64 | |
| 65 | for i := 0; i < n; i++ { |
| 66 | dir := dirs[rng.Intn(len(dirs))] |
| 67 | stem := fileStems[rng.Intn(len(fileStems))] |
| 68 | ext := fileExts[rng.Intn(len(fileExts))] |
| 69 | name := fmt.Sprintf("%s_%d%s", stem, i, ext) |
| 70 | full := filepath.Join(root, dir, name) |
| 71 | f, err := os.Create(full) |
| 72 | require.NoError(t, err) |
| 73 | _ = f.Close() |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // buildIndex walks root and returns a populated Index, the same |
| 78 | // way Engine.AddRoot does but without starting a watcher. |
no test coverage detected