filesystem.test.ts 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. import { describe, test, expect } from "bun:test"
  2. import path from "path"
  3. import fs from "fs/promises"
  4. import { Filesystem } from "../../src/util/filesystem"
  5. import { tmpdir } from "../fixture/fixture"
  6. describe("filesystem", () => {
  7. describe("exists()", () => {
  8. test("returns true for existing file", async () => {
  9. await using tmp = await tmpdir()
  10. const filepath = path.join(tmp.path, "test.txt")
  11. await fs.writeFile(filepath, "content", "utf-8")
  12. expect(await Filesystem.exists(filepath)).toBe(true)
  13. })
  14. test("returns false for non-existent file", async () => {
  15. await using tmp = await tmpdir()
  16. const filepath = path.join(tmp.path, "does-not-exist.txt")
  17. expect(await Filesystem.exists(filepath)).toBe(false)
  18. })
  19. test("returns true for existing directory", async () => {
  20. await using tmp = await tmpdir()
  21. const dirpath = path.join(tmp.path, "subdir")
  22. await fs.mkdir(dirpath)
  23. expect(await Filesystem.exists(dirpath)).toBe(true)
  24. })
  25. })
  26. describe("isDir()", () => {
  27. test("returns true for directory", async () => {
  28. await using tmp = await tmpdir()
  29. const dirpath = path.join(tmp.path, "testdir")
  30. await fs.mkdir(dirpath)
  31. expect(await Filesystem.isDir(dirpath)).toBe(true)
  32. })
  33. test("returns false for file", async () => {
  34. await using tmp = await tmpdir()
  35. const filepath = path.join(tmp.path, "test.txt")
  36. await fs.writeFile(filepath, "content", "utf-8")
  37. expect(await Filesystem.isDir(filepath)).toBe(false)
  38. })
  39. test("returns false for non-existent path", async () => {
  40. await using tmp = await tmpdir()
  41. const filepath = path.join(tmp.path, "does-not-exist")
  42. expect(await Filesystem.isDir(filepath)).toBe(false)
  43. })
  44. })
  45. describe("size()", () => {
  46. test("returns file size", async () => {
  47. await using tmp = await tmpdir()
  48. const filepath = path.join(tmp.path, "test.txt")
  49. const content = "Hello, World!"
  50. await fs.writeFile(filepath, content, "utf-8")
  51. expect(await Filesystem.size(filepath)).toBe(content.length)
  52. })
  53. test("returns 0 for non-existent file", async () => {
  54. await using tmp = await tmpdir()
  55. const filepath = path.join(tmp.path, "does-not-exist.txt")
  56. expect(await Filesystem.size(filepath)).toBe(0)
  57. })
  58. test("returns directory size", async () => {
  59. await using tmp = await tmpdir()
  60. const dirpath = path.join(tmp.path, "testdir")
  61. await fs.mkdir(dirpath)
  62. // Directories have size on some systems
  63. const size = await Filesystem.size(dirpath)
  64. expect(typeof size).toBe("number")
  65. })
  66. })
  67. describe("findUp()", () => {
  68. test("keeps previous nearest-first behavior for single target", async () => {
  69. await using tmp = await tmpdir()
  70. const parent = path.join(tmp.path, "parent")
  71. const child = path.join(parent, "child")
  72. await fs.mkdir(child, { recursive: true })
  73. await fs.writeFile(path.join(tmp.path, "marker"), "root", "utf-8")
  74. await fs.writeFile(path.join(parent, "marker"), "parent", "utf-8")
  75. const result = await Filesystem.findUp("marker", child, tmp.path)
  76. expect(result).toEqual([path.join(parent, "marker"), path.join(tmp.path, "marker")])
  77. })
  78. test("respects stop boundary", async () => {
  79. await using tmp = await tmpdir()
  80. const parent = path.join(tmp.path, "parent")
  81. const child = path.join(parent, "child")
  82. await fs.mkdir(child, { recursive: true })
  83. await fs.writeFile(path.join(tmp.path, "marker"), "root", "utf-8")
  84. await fs.writeFile(path.join(parent, "marker"), "parent", "utf-8")
  85. const result = await Filesystem.findUp("marker", child, parent)
  86. expect(result).toEqual([path.join(parent, "marker")])
  87. })
  88. test("supports multiple targets with nearest-first default ordering", async () => {
  89. await using tmp = await tmpdir()
  90. const parent = path.join(tmp.path, "parent")
  91. const child = path.join(parent, "child")
  92. await fs.mkdir(child, { recursive: true })
  93. await fs.writeFile(path.join(parent, "cfg.jsonc"), "{}", "utf-8")
  94. await fs.writeFile(path.join(tmp.path, "cfg.json"), "{}", "utf-8")
  95. await fs.writeFile(path.join(tmp.path, "cfg.jsonc"), "{}", "utf-8")
  96. const result = await Filesystem.findUp(["cfg.json", "cfg.jsonc"], child, tmp.path)
  97. expect(result).toEqual([
  98. path.join(parent, "cfg.jsonc"),
  99. path.join(tmp.path, "cfg.json"),
  100. path.join(tmp.path, "cfg.jsonc"),
  101. ])
  102. })
  103. test("supports rootFirst ordering for multiple targets", async () => {
  104. await using tmp = await tmpdir()
  105. const parent = path.join(tmp.path, "parent")
  106. const child = path.join(parent, "child")
  107. await fs.mkdir(child, { recursive: true })
  108. await fs.writeFile(path.join(parent, "cfg.jsonc"), "{}", "utf-8")
  109. await fs.writeFile(path.join(tmp.path, "cfg.json"), "{}", "utf-8")
  110. await fs.writeFile(path.join(tmp.path, "cfg.jsonc"), "{}", "utf-8")
  111. const result = await Filesystem.findUp(["cfg.json", "cfg.jsonc"], child, tmp.path, { rootFirst: true })
  112. expect(result).toEqual([
  113. path.join(tmp.path, "cfg.json"),
  114. path.join(tmp.path, "cfg.jsonc"),
  115. path.join(parent, "cfg.jsonc"),
  116. ])
  117. })
  118. test("rootFirst preserves json then jsonc order per directory", async () => {
  119. await using tmp = await tmpdir()
  120. const project = path.join(tmp.path, "project")
  121. const nested = path.join(project, "nested")
  122. await fs.mkdir(nested, { recursive: true })
  123. await fs.writeFile(path.join(tmp.path, "opencode.json"), "{}", "utf-8")
  124. await fs.writeFile(path.join(tmp.path, "opencode.jsonc"), "{}", "utf-8")
  125. await fs.writeFile(path.join(project, "opencode.json"), "{}", "utf-8")
  126. await fs.writeFile(path.join(project, "opencode.jsonc"), "{}", "utf-8")
  127. const result = await Filesystem.findUp(["opencode.json", "opencode.jsonc"], nested, tmp.path, {
  128. rootFirst: true,
  129. })
  130. expect(result).toEqual([
  131. path.join(tmp.path, "opencode.json"),
  132. path.join(tmp.path, "opencode.jsonc"),
  133. path.join(project, "opencode.json"),
  134. path.join(project, "opencode.jsonc"),
  135. ])
  136. })
  137. })
  138. describe("readText()", () => {
  139. test("reads file content", async () => {
  140. await using tmp = await tmpdir()
  141. const filepath = path.join(tmp.path, "test.txt")
  142. const content = "Hello, World!"
  143. await fs.writeFile(filepath, content, "utf-8")
  144. expect(await Filesystem.readText(filepath)).toBe(content)
  145. })
  146. test("throws for non-existent file", async () => {
  147. await using tmp = await tmpdir()
  148. const filepath = path.join(tmp.path, "does-not-exist.txt")
  149. await expect(Filesystem.readText(filepath)).rejects.toThrow()
  150. })
  151. test("reads UTF-8 content correctly", async () => {
  152. await using tmp = await tmpdir()
  153. const filepath = path.join(tmp.path, "unicode.txt")
  154. const content = "Hello 世界 🌍"
  155. await fs.writeFile(filepath, content, "utf-8")
  156. expect(await Filesystem.readText(filepath)).toBe(content)
  157. })
  158. })
  159. describe("readJson()", () => {
  160. test("reads and parses JSON", async () => {
  161. await using tmp = await tmpdir()
  162. const filepath = path.join(tmp.path, "test.json")
  163. const data = { key: "value", nested: { array: [1, 2, 3] } }
  164. await fs.writeFile(filepath, JSON.stringify(data), "utf-8")
  165. const result: typeof data = await Filesystem.readJson(filepath)
  166. expect(result).toEqual(data)
  167. })
  168. test("throws for invalid JSON", async () => {
  169. await using tmp = await tmpdir()
  170. const filepath = path.join(tmp.path, "invalid.json")
  171. await fs.writeFile(filepath, "{ invalid json", "utf-8")
  172. await expect(Filesystem.readJson(filepath)).rejects.toThrow()
  173. })
  174. test("throws for non-existent file", async () => {
  175. await using tmp = await tmpdir()
  176. const filepath = path.join(tmp.path, "does-not-exist.json")
  177. await expect(Filesystem.readJson(filepath)).rejects.toThrow()
  178. })
  179. test("returns typed data", async () => {
  180. await using tmp = await tmpdir()
  181. const filepath = path.join(tmp.path, "typed.json")
  182. interface Config {
  183. name: string
  184. version: number
  185. }
  186. const data: Config = { name: "test", version: 1 }
  187. await fs.writeFile(filepath, JSON.stringify(data), "utf-8")
  188. const result = await Filesystem.readJson<Config>(filepath)
  189. expect(result.name).toBe("test")
  190. expect(result.version).toBe(1)
  191. })
  192. })
  193. describe("readBytes()", () => {
  194. test("reads file as buffer", async () => {
  195. await using tmp = await tmpdir()
  196. const filepath = path.join(tmp.path, "test.txt")
  197. const content = "Hello, World!"
  198. await fs.writeFile(filepath, content, "utf-8")
  199. const buffer = await Filesystem.readBytes(filepath)
  200. expect(buffer).toBeInstanceOf(Buffer)
  201. expect(buffer.toString("utf-8")).toBe(content)
  202. })
  203. test("throws for non-existent file", async () => {
  204. await using tmp = await tmpdir()
  205. const filepath = path.join(tmp.path, "does-not-exist.bin")
  206. await expect(Filesystem.readBytes(filepath)).rejects.toThrow()
  207. })
  208. })
  209. describe("write()", () => {
  210. test("writes text content", async () => {
  211. await using tmp = await tmpdir()
  212. const filepath = path.join(tmp.path, "test.txt")
  213. const content = "Hello, World!"
  214. await Filesystem.write(filepath, content)
  215. expect(await fs.readFile(filepath, "utf-8")).toBe(content)
  216. })
  217. test("writes buffer content", async () => {
  218. await using tmp = await tmpdir()
  219. const filepath = path.join(tmp.path, "test.bin")
  220. const content = Buffer.from([0x00, 0x01, 0x02, 0x03])
  221. await Filesystem.write(filepath, content)
  222. const read = await fs.readFile(filepath)
  223. expect(read).toEqual(content)
  224. })
  225. test("writes with permissions", async () => {
  226. await using tmp = await tmpdir()
  227. const filepath = path.join(tmp.path, "protected.txt")
  228. const content = "secret"
  229. await Filesystem.write(filepath, content, 0o600)
  230. const stats = await fs.stat(filepath)
  231. // Check permissions on Unix
  232. if (process.platform !== "win32") {
  233. expect(stats.mode & 0o777).toBe(0o600)
  234. }
  235. })
  236. test("creates parent directories", async () => {
  237. await using tmp = await tmpdir()
  238. const filepath = path.join(tmp.path, "nested", "deep", "file.txt")
  239. const content = "nested content"
  240. await Filesystem.write(filepath, content)
  241. expect(await fs.readFile(filepath, "utf-8")).toBe(content)
  242. })
  243. })
  244. describe("writeJson()", () => {
  245. test("writes JSON data", async () => {
  246. await using tmp = await tmpdir()
  247. const filepath = path.join(tmp.path, "data.json")
  248. const data = { key: "value", number: 42 }
  249. await Filesystem.writeJson(filepath, data)
  250. const content = await fs.readFile(filepath, "utf-8")
  251. expect(JSON.parse(content)).toEqual(data)
  252. })
  253. test("writes formatted JSON", async () => {
  254. await using tmp = await tmpdir()
  255. const filepath = path.join(tmp.path, "pretty.json")
  256. const data = { key: "value" }
  257. await Filesystem.writeJson(filepath, data)
  258. const content = await fs.readFile(filepath, "utf-8")
  259. expect(content).toContain("\n")
  260. expect(content).toContain(" ")
  261. })
  262. test("writes with permissions", async () => {
  263. await using tmp = await tmpdir()
  264. const filepath = path.join(tmp.path, "config.json")
  265. const data = { secret: "data" }
  266. await Filesystem.writeJson(filepath, data, 0o600)
  267. const stats = await fs.stat(filepath)
  268. if (process.platform !== "win32") {
  269. expect(stats.mode & 0o777).toBe(0o600)
  270. }
  271. })
  272. })
  273. describe("mimeType()", () => {
  274. test("returns correct MIME type for JSON", () => {
  275. expect(Filesystem.mimeType("test.json")).toContain("application/json")
  276. })
  277. test("returns correct MIME type for JavaScript", () => {
  278. expect(Filesystem.mimeType("test.js")).toContain("javascript")
  279. })
  280. test("returns MIME type for TypeScript (or video/mp2t due to extension conflict)", () => {
  281. const mime = Filesystem.mimeType("test.ts")
  282. // .ts is ambiguous: TypeScript vs MPEG-2 TS video
  283. expect(mime === "video/mp2t" || mime === "application/typescript" || mime === "text/typescript").toBe(true)
  284. })
  285. test("returns correct MIME type for images", () => {
  286. expect(Filesystem.mimeType("test.png")).toContain("image/png")
  287. expect(Filesystem.mimeType("test.jpg")).toContain("image/jpeg")
  288. })
  289. test("returns default for unknown extension", () => {
  290. expect(Filesystem.mimeType("test.unknown")).toBe("application/octet-stream")
  291. })
  292. test("handles files without extension", () => {
  293. expect(Filesystem.mimeType("Makefile")).toBe("application/octet-stream")
  294. })
  295. })
  296. describe("windowsPath()", () => {
  297. test("converts Git Bash paths", () => {
  298. if (process.platform === "win32") {
  299. expect(Filesystem.windowsPath("/c/Users/test")).toBe("C:/Users/test")
  300. expect(Filesystem.windowsPath("/d/dev/project")).toBe("D:/dev/project")
  301. } else {
  302. expect(Filesystem.windowsPath("/c/Users/test")).toBe("/c/Users/test")
  303. }
  304. })
  305. test("converts Cygwin paths", () => {
  306. if (process.platform === "win32") {
  307. expect(Filesystem.windowsPath("/cygdrive/c/Users/test")).toBe("C:/Users/test")
  308. expect(Filesystem.windowsPath("/cygdrive/x/dev/project")).toBe("X:/dev/project")
  309. } else {
  310. expect(Filesystem.windowsPath("/cygdrive/c/Users/test")).toBe("/cygdrive/c/Users/test")
  311. }
  312. })
  313. test("converts WSL paths", () => {
  314. if (process.platform === "win32") {
  315. expect(Filesystem.windowsPath("/mnt/c/Users/test")).toBe("C:/Users/test")
  316. expect(Filesystem.windowsPath("/mnt/z/dev/project")).toBe("Z:/dev/project")
  317. } else {
  318. expect(Filesystem.windowsPath("/mnt/c/Users/test")).toBe("/mnt/c/Users/test")
  319. }
  320. })
  321. test("ignores normal Windows paths", () => {
  322. expect(Filesystem.windowsPath("C:/Users/test")).toBe("C:/Users/test")
  323. expect(Filesystem.windowsPath("D:\\dev\\project")).toBe("D:\\dev\\project")
  324. })
  325. })
  326. describe("writeStream()", () => {
  327. test("writes from Web ReadableStream", async () => {
  328. await using tmp = await tmpdir()
  329. const filepath = path.join(tmp.path, "streamed.txt")
  330. const content = "Hello from stream!"
  331. const encoder = new TextEncoder()
  332. const stream = new ReadableStream({
  333. start(controller) {
  334. controller.enqueue(encoder.encode(content))
  335. controller.close()
  336. },
  337. })
  338. await Filesystem.writeStream(filepath, stream)
  339. expect(await fs.readFile(filepath, "utf-8")).toBe(content)
  340. })
  341. test("writes from Node.js Readable stream", async () => {
  342. await using tmp = await tmpdir()
  343. const filepath = path.join(tmp.path, "node-streamed.txt")
  344. const content = "Hello from Node stream!"
  345. const { Readable } = await import("stream")
  346. const stream = Readable.from([content])
  347. await Filesystem.writeStream(filepath, stream)
  348. expect(await fs.readFile(filepath, "utf-8")).toBe(content)
  349. })
  350. test("writes binary data from Web ReadableStream", async () => {
  351. await using tmp = await tmpdir()
  352. const filepath = path.join(tmp.path, "binary.dat")
  353. const binaryData = new Uint8Array([0x00, 0x01, 0x02, 0x03, 0xff])
  354. const stream = new ReadableStream({
  355. start(controller) {
  356. controller.enqueue(binaryData)
  357. controller.close()
  358. },
  359. })
  360. await Filesystem.writeStream(filepath, stream)
  361. const read = await fs.readFile(filepath)
  362. expect(Buffer.from(read)).toEqual(Buffer.from(binaryData))
  363. })
  364. test("writes large content in chunks", async () => {
  365. await using tmp = await tmpdir()
  366. const filepath = path.join(tmp.path, "large.txt")
  367. const chunks = ["chunk1", "chunk2", "chunk3", "chunk4", "chunk5"]
  368. const stream = new ReadableStream({
  369. start(controller) {
  370. for (const chunk of chunks) {
  371. controller.enqueue(new TextEncoder().encode(chunk))
  372. }
  373. controller.close()
  374. },
  375. })
  376. await Filesystem.writeStream(filepath, stream)
  377. expect(await fs.readFile(filepath, "utf-8")).toBe(chunks.join(""))
  378. })
  379. test("creates parent directories", async () => {
  380. await using tmp = await tmpdir()
  381. const filepath = path.join(tmp.path, "nested", "deep", "streamed.txt")
  382. const content = "nested stream content"
  383. const stream = new ReadableStream({
  384. start(controller) {
  385. controller.enqueue(new TextEncoder().encode(content))
  386. controller.close()
  387. },
  388. })
  389. await Filesystem.writeStream(filepath, stream)
  390. expect(await fs.readFile(filepath, "utf-8")).toBe(content)
  391. })
  392. test("writes with permissions", async () => {
  393. await using tmp = await tmpdir()
  394. const filepath = path.join(tmp.path, "protected-stream.txt")
  395. const content = "secret stream content"
  396. const stream = new ReadableStream({
  397. start(controller) {
  398. controller.enqueue(new TextEncoder().encode(content))
  399. controller.close()
  400. },
  401. })
  402. await Filesystem.writeStream(filepath, stream, 0o600)
  403. const stats = await fs.stat(filepath)
  404. if (process.platform !== "win32") {
  405. expect(stats.mode & 0o777).toBe(0o600)
  406. }
  407. })
  408. test("writes executable with permissions", async () => {
  409. await using tmp = await tmpdir()
  410. const filepath = path.join(tmp.path, "script.sh")
  411. const content = "#!/bin/bash\necho hello"
  412. const stream = new ReadableStream({
  413. start(controller) {
  414. controller.enqueue(new TextEncoder().encode(content))
  415. controller.close()
  416. },
  417. })
  418. await Filesystem.writeStream(filepath, stream, 0o755)
  419. const stats = await fs.stat(filepath)
  420. if (process.platform !== "win32") {
  421. expect(stats.mode & 0o777).toBe(0o755)
  422. }
  423. expect(await fs.readFile(filepath, "utf-8")).toBe(content)
  424. })
  425. })
  426. describe("resolve()", () => {
  427. test("resolves slash-prefixed drive paths on Windows", async () => {
  428. if (process.platform !== "win32") return
  429. await using tmp = await tmpdir()
  430. const forward = tmp.path.replaceAll("\\", "/")
  431. expect(Filesystem.resolve(`/${forward}`)).toBe(Filesystem.normalizePath(tmp.path))
  432. })
  433. test("resolves slash-prefixed drive roots on Windows", async () => {
  434. if (process.platform !== "win32") return
  435. await using tmp = await tmpdir()
  436. const drive = tmp.path[0].toUpperCase()
  437. expect(Filesystem.resolve(`/${drive}:`)).toBe(Filesystem.resolve(`${drive}:/`))
  438. })
  439. test("resolves Git Bash and MSYS2 paths on Windows", async () => {
  440. // Git Bash and MSYS2 both use /<drive>/... paths on Windows.
  441. if (process.platform !== "win32") return
  442. await using tmp = await tmpdir()
  443. const drive = tmp.path[0].toLowerCase()
  444. const rest = tmp.path.slice(2).replaceAll("\\", "/")
  445. expect(Filesystem.resolve(`/${drive}${rest}`)).toBe(Filesystem.normalizePath(tmp.path))
  446. })
  447. test("resolves Git Bash and MSYS2 drive roots on Windows", async () => {
  448. // Git Bash and MSYS2 both use /<drive> paths on Windows.
  449. if (process.platform !== "win32") return
  450. await using tmp = await tmpdir()
  451. const drive = tmp.path[0].toLowerCase()
  452. expect(Filesystem.resolve(`/${drive}`)).toBe(Filesystem.resolve(`${drive.toUpperCase()}:/`))
  453. })
  454. test("resolves Cygwin paths on Windows", async () => {
  455. if (process.platform !== "win32") return
  456. await using tmp = await tmpdir()
  457. const drive = tmp.path[0].toLowerCase()
  458. const rest = tmp.path.slice(2).replaceAll("\\", "/")
  459. expect(Filesystem.resolve(`/cygdrive/${drive}${rest}`)).toBe(Filesystem.normalizePath(tmp.path))
  460. })
  461. test("resolves Cygwin drive roots on Windows", async () => {
  462. if (process.platform !== "win32") return
  463. await using tmp = await tmpdir()
  464. const drive = tmp.path[0].toLowerCase()
  465. expect(Filesystem.resolve(`/cygdrive/${drive}`)).toBe(Filesystem.resolve(`${drive.toUpperCase()}:/`))
  466. })
  467. test("resolves WSL mount paths on Windows", async () => {
  468. if (process.platform !== "win32") return
  469. await using tmp = await tmpdir()
  470. const drive = tmp.path[0].toLowerCase()
  471. const rest = tmp.path.slice(2).replaceAll("\\", "/")
  472. expect(Filesystem.resolve(`/mnt/${drive}${rest}`)).toBe(Filesystem.normalizePath(tmp.path))
  473. })
  474. test("resolves WSL mount roots on Windows", async () => {
  475. if (process.platform !== "win32") return
  476. await using tmp = await tmpdir()
  477. const drive = tmp.path[0].toLowerCase()
  478. expect(Filesystem.resolve(`/mnt/${drive}`)).toBe(Filesystem.resolve(`${drive.toUpperCase()}:/`))
  479. })
  480. test("resolves symlinked directory to canonical path", async () => {
  481. await using tmp = await tmpdir()
  482. const target = path.join(tmp.path, "real")
  483. await fs.mkdir(target)
  484. const link = path.join(tmp.path, "link")
  485. await fs.symlink(target, link)
  486. expect(Filesystem.resolve(link)).toBe(Filesystem.resolve(target))
  487. })
  488. test("returns unresolved path when target does not exist", async () => {
  489. await using tmp = await tmpdir()
  490. const missing = path.join(tmp.path, "does-not-exist-" + Date.now())
  491. const result = Filesystem.resolve(missing)
  492. expect(result).toBe(Filesystem.normalizePath(path.resolve(missing)))
  493. })
  494. test("throws ELOOP on symlink cycle", async () => {
  495. await using tmp = await tmpdir()
  496. const a = path.join(tmp.path, "a")
  497. const b = path.join(tmp.path, "b")
  498. await fs.symlink(b, a)
  499. await fs.symlink(a, b)
  500. expect(() => Filesystem.resolve(a)).toThrow()
  501. })
  502. // Windows: chmod(0o000) is a no-op, so EACCES cannot be triggered
  503. test("throws EACCES on permission-denied symlink target", async () => {
  504. if (process.platform === "win32") return
  505. if (process.getuid?.() === 0) return // skip when running as root
  506. await using tmp = await tmpdir()
  507. const dir = path.join(tmp.path, "restricted")
  508. await fs.mkdir(dir)
  509. const link = path.join(tmp.path, "link")
  510. await fs.symlink(dir, link)
  511. await fs.chmod(dir, 0o000)
  512. try {
  513. expect(() => Filesystem.resolve(path.join(link, "child"))).toThrow()
  514. } finally {
  515. await fs.chmod(dir, 0o755)
  516. }
  517. })
  518. // Windows: traversing through a file throws ENOENT (not ENOTDIR),
  519. // which resolve() catches as a fallback instead of rethrowing
  520. test("rethrows non-ENOENT errors", async () => {
  521. if (process.platform === "win32") return
  522. await using tmp = await tmpdir()
  523. const file = path.join(tmp.path, "not-a-directory")
  524. await fs.writeFile(file, "x")
  525. expect(() => Filesystem.resolve(path.join(file, "child"))).toThrow()
  526. })
  527. })
  528. describe("normalizePathPattern()", () => {
  529. test("preserves drive root globs on Windows", async () => {
  530. if (process.platform !== "win32") return
  531. await using tmp = await tmpdir()
  532. const root = path.parse(tmp.path).root
  533. expect(Filesystem.normalizePathPattern(path.join(root, "*"))).toBe(path.join(root, "*"))
  534. })
  535. })
  536. })