|
| 1 | +import { closeSync, openSync, readFileSync, writeFileSync } from "node:fs"; |
| 2 | +import { writeFile } from "node:fs/promises"; |
| 3 | + |
| 4 | +const tmp = Deno.makeTempDirSync(); |
| 5 | + |
| 6 | +// Test 1: writeFileSync with a numeric fd cleans up properly |
| 7 | +{ |
| 8 | + const path = `${tmp}/sync.txt`; |
| 9 | + Deno.writeTextFileSync(path, ""); |
| 10 | + const fd = openSync(path, "w"); |
| 11 | + writeFileSync(fd, "sync write via fd"); |
| 12 | + // fd should still be usable after writeFileSync (not closed) |
| 13 | + writeFileSync(fd, "sync overwrite"); |
| 14 | + closeSync(fd); |
| 15 | + console.log("sync:", readFileSync(path, "utf8")); |
| 16 | +} |
| 17 | + |
| 18 | +// Test 2: writeFile (async) with a numeric fd cleans up properly |
| 19 | +{ |
| 20 | + const path = `${tmp}/async.txt`; |
| 21 | + Deno.writeTextFileSync(path, ""); |
| 22 | + const fd = openSync(path, "w"); |
| 23 | + await writeFile(fd, "async write via fd"); |
| 24 | + // fd should still be usable after writeFile |
| 25 | + await writeFile(fd, "async overwrite"); |
| 26 | + closeSync(fd); |
| 27 | + console.log("async:", readFileSync(path, "utf8")); |
| 28 | +} |
| 29 | + |
| 30 | +// Test 3: writeFileSync with a path (string) still works |
| 31 | +{ |
| 32 | + const path = `${tmp}/path.txt`; |
| 33 | + writeFileSync(path, "via path"); |
| 34 | + console.log("path:", readFileSync(path, "utf8")); |
| 35 | +} |
| 36 | + |
| 37 | +// Test 4: multiple writeFileSync calls with same fd don't leak |
| 38 | +{ |
| 39 | + const path = `${tmp}/multi.txt`; |
| 40 | + Deno.writeTextFileSync(path, ""); |
| 41 | + const fd = openSync(path, "w"); |
| 42 | + for (let i = 0; i < 10; i++) { |
| 43 | + writeFileSync(fd, `write ${i}`); |
| 44 | + } |
| 45 | + closeSync(fd); |
| 46 | + console.log("multi:", readFileSync(path, "utf8")); |
| 47 | +} |
| 48 | + |
| 49 | +Deno.removeSync(tmp, { recursive: true }); |
| 50 | +console.log("ok"); |
0 commit comments