ripgrep.test.ts 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { describe, expect } from "bun:test"
  2. import fs from "fs/promises"
  3. import path from "path"
  4. import { Effect } from "effect"
  5. import { Ripgrep } from "@opencode-ai/core/ripgrep"
  6. import { RelativePath } from "@opencode-ai/core/schema"
  7. import { tmpdir } from "./fixture/tmpdir"
  8. import { testEffect } from "./lib/effect"
  9. const it = testEffect(Ripgrep.defaultLayer)
  10. describe("Ripgrep", () => {
  11. it.live("keeps ignored files out of catch-all find results", () =>
  12. Effect.acquireUseRelease(
  13. Effect.promise(() => tmpdir()),
  14. (tmp) =>
  15. Effect.gen(function* () {
  16. yield* Effect.promise(() => fs.mkdir(path.join(tmp.path, "node_modules", "pkg"), { recursive: true }))
  17. yield* Effect.promise(() => fs.mkdir(path.join(tmp.path, "src")))
  18. yield* Effect.promise(() => Bun.$`git init -q ${tmp.path}`)
  19. yield* Effect.promise(() => fs.writeFile(path.join(tmp.path, ".gitignore"), "node_modules/\n"))
  20. yield* Effect.promise(() => fs.writeFile(path.join(tmp.path, "node_modules", "pkg", "index.js"), "ignored\n"))
  21. yield* Effect.promise(() => fs.writeFile(path.join(tmp.path, "src", "index.js"), "included\n"))
  22. const files = yield* (yield* Ripgrep.Service).find({ cwd: tmp.path, pattern: "*", limit: 10 })
  23. expect(files.map((item) => item.path)).toContain(RelativePath.make("src/index.js"))
  24. expect(files.map((item) => item.path)).not.toContain(RelativePath.make("node_modules/pkg/index.js"))
  25. }),
  26. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
  27. ),
  28. )
  29. it.live("never includes git metadata", () =>
  30. Effect.acquireUseRelease(
  31. Effect.promise(() => tmpdir()),
  32. (tmp) =>
  33. Effect.gen(function* () {
  34. yield* Effect.promise(() => fs.mkdir(path.join(tmp.path, ".opencode")))
  35. yield* Effect.promise(() => fs.writeFile(path.join(tmp.path, ".opencode", "config"), "needle\n"))
  36. yield* Effect.promise(() => fs.mkdir(path.join(tmp.path, ".git")))
  37. yield* Effect.promise(() => fs.writeFile(path.join(tmp.path, ".git", "config"), "needle\n"))
  38. const ripgrep = yield* Ripgrep.Service
  39. const files = yield* ripgrep.find({ cwd: tmp.path, pattern: "**/*", limit: 10 })
  40. expect(files.map((item) => item.path)).toContain(RelativePath.make(".opencode/config"))
  41. expect(files.map((item) => item.path)).not.toContain(RelativePath.make(".git/config"))
  42. const observed: string[] = []
  43. const limited = yield* ripgrep.find({
  44. cwd: tmp.path,
  45. pattern: "**/*",
  46. limit: 1,
  47. onEntry: (entry) => Effect.sync(() => observed.push(entry.path)),
  48. })
  49. expect(observed).toEqual(limited.map((item) => item.path))
  50. const matches = yield* ripgrep.grep({ cwd: tmp.path, pattern: "needle", include: "config", limit: 10 })
  51. expect(matches.map((item) => item.entry.path)).toContain(RelativePath.make(".opencode/config"))
  52. expect(matches.map((item) => item.entry.path)).not.toContain(RelativePath.make(".git/config"))
  53. }),
  54. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
  55. ),
  56. )
  57. })