diff options
author | 2022-04-13 18:48:09 -0700 | |
---|---|---|
committer | 2022-04-13 18:48:09 -0700 | |
commit | f2c21b77336b9dcfa90676670877790ee48b31cc (patch) | |
tree | f04d3ae0bbae6edaf4c0965efe162b83f3fbd1bb /integration/bunjs-only-snippets/fs.test.js | |
parent | d273948f168fdb939f21622f2837c832d9ef4934 (diff) | |
download | bun-f2c21b77336b9dcfa90676670877790ee48b31cc.tar.gz bun-f2c21b77336b9dcfa90676670877790ee48b31cc.tar.zst bun-f2c21b77336b9dcfa90676670877790ee48b31cc.zip |
[bun.js] Fix bug with `readdirSync` on folders with less than 32 files
Closes https://github.com/Jarred-Sumner/bun/issues/143
Diffstat (limited to 'integration/bunjs-only-snippets/fs.test.js')
-rw-r--r-- | integration/bunjs-only-snippets/fs.test.js | 89 |
1 files changed, 82 insertions, 7 deletions
diff --git a/integration/bunjs-only-snippets/fs.test.js b/integration/bunjs-only-snippets/fs.test.js index 660289152..79ac60eaa 100644 --- a/integration/bunjs-only-snippets/fs.test.js +++ b/integration/bunjs-only-snippets/fs.test.js @@ -1,14 +1,15 @@ -import { describe, it, expect } from "bun:test"; +import { gc } from "bun"; +import { describe, expect, it } from "bun:test"; import { - mkdirSync, + closeSync, existsSync, - readFileSync, - writeFileSync, - readFile, - read, + mkdirSync, openSync, + readdirSync, + readFile, + readFileSync, readSync, - closeSync, + writeFileSync, writeSync, } from "node:fs"; @@ -29,6 +30,80 @@ describe("mkdirSync", () => { }); }); +it("readdirSync on import.meta.dir", () => { + const dirs = readdirSync(import.meta.dir); + expect(dirs.length > 0).toBe(true); + var match = false; + gc(true); + for (let i = 0; i < dirs.length; i++) { + if (dirs[i] === import.meta.file) { + match = true; + } + } + gc(true); + expect(match).toBe(true); +}); + +it("readdirSync on import.meta.dir with trailing slash", () => { + const dirs = readdirSync(import.meta.dir + "/"); + expect(dirs.length > 0).toBe(true); + // this file should exist in it + var match = false; + for (let i = 0; i < dirs.length; i++) { + if (dirs[i] === import.meta.file) { + match = true; + } + } + expect(match).toBe(true); +}); + +it("readdirSync works on empty directories", () => { + const path = `/tmp/fs-test-empty-dir-${( + Math.random() * 100000 + + 100 + ).toString(32)}`; + mkdirSync(path, { recursive: true }); + expect(readdirSync(path).length).toBe(0); +}); + +it("readdirSync works on directories with under 32 files", () => { + const path = `/tmp/fs-test-one-dir-${(Math.random() * 100000 + 100).toString( + 32 + )}`; + mkdirSync(path, { recursive: true }); + writeFileSync(`${path}/a`, "a"); + const results = readdirSync(path); + expect(results.length).toBe(1); + expect(results[0]).toBe("a"); +}); + +it("readdirSync throws when given a file path", () => { + try { + readdirSync(import.meta.path); + throw new Error("should not get here"); + } catch (exception) { + expect(exception.name).toBe("ENOTDIR"); + } +}); + +it("readdirSync throws when given a path that doesn't exist", () => { + try { + readdirSync(import.meta.path + "/does-not-exist/really"); + throw new Error("should not get here"); + } catch (exception) { + expect(exception.name).toBe("ENOTDIR"); + } +}); + +it("readdirSync throws when given a file path with trailing slash", () => { + try { + readdirSync(import.meta.path + "/"); + throw new Error("should not get here"); + } catch (exception) { + expect(exception.name).toBe("ENOTDIR"); + } +}); + describe("readSync", () => { const firstFourBytes = new Uint32Array( new TextEncoder().encode("File").buffer |