ripgrep.test.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { describe, expect, test } from "bun:test"
  2. import fs from "fs/promises"
  3. import path from "path"
  4. import { tmpdir } from "../fixture/fixture"
  5. import { Ripgrep } from "../../src/file/ripgrep"
  6. describe("file.ripgrep", () => {
  7. test("defaults to include hidden", async () => {
  8. await using tmp = await tmpdir({
  9. init: async (dir) => {
  10. await Bun.write(path.join(dir, "visible.txt"), "hello")
  11. await fs.mkdir(path.join(dir, ".opencode"), { recursive: true })
  12. await Bun.write(path.join(dir, ".opencode", "thing.json"), "{}")
  13. },
  14. })
  15. const files = await Array.fromAsync(Ripgrep.files({ cwd: tmp.path }))
  16. const hasVisible = files.includes("visible.txt")
  17. const hasHidden = files.includes(path.join(".opencode", "thing.json"))
  18. expect(hasVisible).toBe(true)
  19. expect(hasHidden).toBe(true)
  20. })
  21. test("hidden false excludes hidden", async () => {
  22. await using tmp = await tmpdir({
  23. init: async (dir) => {
  24. await Bun.write(path.join(dir, "visible.txt"), "hello")
  25. await fs.mkdir(path.join(dir, ".opencode"), { recursive: true })
  26. await Bun.write(path.join(dir, ".opencode", "thing.json"), "{}")
  27. },
  28. })
  29. const files = await Array.fromAsync(Ripgrep.files({ cwd: tmp.path, hidden: false }))
  30. const hasVisible = files.includes("visible.txt")
  31. const hasHidden = files.includes(path.join(".opencode", "thing.json"))
  32. expect(hasVisible).toBe(true)
  33. expect(hasHidden).toBe(false)
  34. })
  35. test("search returns match metadata", async () => {
  36. await using tmp = await tmpdir({
  37. init: async (dir) => {
  38. await Bun.write(path.join(dir, "match.ts"), "const value = 'needle'\n")
  39. await Bun.write(path.join(dir, "other.ts"), "const value = 'other'\n")
  40. },
  41. })
  42. const hits = await Ripgrep.search({
  43. cwd: tmp.path,
  44. pattern: "needle",
  45. })
  46. expect(hits.length).toBe(1)
  47. expect(path.basename(hits[0]?.path.text ?? "")).toBe("match.ts")
  48. expect(hits[0]?.line_number).toBe(1)
  49. expect(hits[0]?.lines.text).toContain("needle")
  50. })
  51. test("search returns empty when nothing matches", async () => {
  52. await using tmp = await tmpdir({
  53. init: async (dir) => {
  54. await Bun.write(path.join(dir, "match.ts"), "const value = 'other'\n")
  55. },
  56. })
  57. const hits = await Ripgrep.search({
  58. cwd: tmp.path,
  59. pattern: "needle",
  60. })
  61. expect(hits).toEqual([])
  62. })
  63. })