fsmonitor.test.ts 2.5 KB

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