fsmonitor.test.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { $ } from "bun"
  2. import { describe, expect, test } from "bun:test"
  3. import { Effect } from "effect"
  4. import fs from "fs/promises"
  5. import path from "path"
  6. import { File } from "../../src/file"
  7. import { Instance } from "../../src/project/instance"
  8. import { WithInstance } from "../../src/project/with-instance"
  9. import { provideInstance, tmpdir } from "../fixture/fixture"
  10. const run = <A, E>(eff: Effect.Effect<A, E, File.Service>) =>
  11. Effect.runPromise(provideInstance(Instance.directory)(eff.pipe(Effect.provide(File.defaultLayer))))
  12. const status = () => run(File.Service.use((svc) => svc.status()))
  13. const read = (file: string) => run(File.Service.use((svc) => svc.read(file)))
  14. const wintest = process.platform === "win32" ? test : test.skip
  15. describe("file fsmonitor", () => {
  16. wintest("status does not start fsmonitor for readonly git checks", async () => {
  17. await using tmp = await tmpdir({ git: true })
  18. const target = path.join(tmp.path, "tracked.txt")
  19. await fs.writeFile(target, "base\n")
  20. await $`git add tracked.txt`.cwd(tmp.path).quiet()
  21. await $`git commit -m init`.cwd(tmp.path).quiet()
  22. await $`git config core.fsmonitor true`.cwd(tmp.path).quiet()
  23. await $`git fsmonitor--daemon stop`.cwd(tmp.path).quiet().nothrow()
  24. await fs.writeFile(target, "next\n")
  25. await fs.writeFile(path.join(tmp.path, "new.txt"), "new\n")
  26. const before = await $`git fsmonitor--daemon status`.cwd(tmp.path).quiet().nothrow()
  27. expect(before.exitCode).not.toBe(0)
  28. await WithInstance.provide({
  29. directory: tmp.path,
  30. fn: async () => {
  31. await status()
  32. },
  33. })
  34. const after = await $`git fsmonitor--daemon status`.cwd(tmp.path).quiet().nothrow()
  35. expect(after.exitCode).not.toBe(0)
  36. })
  37. wintest("read does not start fsmonitor for git diffs", async () => {
  38. await using tmp = await tmpdir({ git: true })
  39. const target = path.join(tmp.path, "tracked.txt")
  40. await fs.writeFile(target, "base\n")
  41. await $`git add tracked.txt`.cwd(tmp.path).quiet()
  42. await $`git commit -m init`.cwd(tmp.path).quiet()
  43. await $`git config core.fsmonitor true`.cwd(tmp.path).quiet()
  44. await $`git fsmonitor--daemon stop`.cwd(tmp.path).quiet().nothrow()
  45. await fs.writeFile(target, "next\n")
  46. const before = await $`git fsmonitor--daemon status`.cwd(tmp.path).quiet().nothrow()
  47. expect(before.exitCode).not.toBe(0)
  48. await WithInstance.provide({
  49. directory: tmp.path,
  50. fn: async () => {
  51. await read("tracked.txt")
  52. },
  53. })
  54. const after = await $`git fsmonitor--daemon status`.cwd(tmp.path).quiet().nothrow()
  55. expect(after.exitCode).not.toBe(0)
  56. })
  57. })