filesystem.test.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { describe, expect, test } from "bun:test"
  2. import os from "node:os"
  3. import path from "node:path"
  4. import { mkdtemp, mkdir, rm } from "node:fs/promises"
  5. import { Filesystem } from "../../src/util/filesystem"
  6. describe("util.filesystem", () => {
  7. test("exists() is true for files and directories", async () => {
  8. const tmp = await mkdtemp(path.join(os.tmpdir(), "opencode-filesystem-"))
  9. const dir = path.join(tmp, "dir")
  10. const file = path.join(tmp, "file.txt")
  11. const missing = path.join(tmp, "missing")
  12. await mkdir(dir, { recursive: true })
  13. await Bun.write(file, "hello")
  14. const cases = await Promise.all([Filesystem.exists(dir), Filesystem.exists(file), Filesystem.exists(missing)])
  15. expect(cases).toEqual([true, true, false])
  16. await rm(tmp, { recursive: true, force: true })
  17. })
  18. test("isDir() is true only for directories", async () => {
  19. const tmp = await mkdtemp(path.join(os.tmpdir(), "opencode-filesystem-"))
  20. const dir = path.join(tmp, "dir")
  21. const file = path.join(tmp, "file.txt")
  22. const missing = path.join(tmp, "missing")
  23. await mkdir(dir, { recursive: true })
  24. await Bun.write(file, "hello")
  25. const cases = await Promise.all([Filesystem.isDir(dir), Filesystem.isDir(file), Filesystem.isDir(missing)])
  26. expect(cases).toEqual([true, false, false])
  27. await rm(tmp, { recursive: true, force: true })
  28. })
  29. })