diff options
author | 2023-01-12 13:09:10 -0800 | |
---|---|---|
committer | 2023-01-12 13:11:05 -0800 | |
commit | bbd4504954b6eacb6cb5ee480a70b9cb65e6e700 (patch) | |
tree | 3e1f0016935a504df01accfc436410eb772d5696 | |
parent | a0b44e9873848ff0e3d131487afc98b56c93c926 (diff) | |
download | bun-bbd4504954b6eacb6cb5ee480a70b9cb65e6e700.tar.gz bun-bbd4504954b6eacb6cb5ee480a70b9cb65e6e700.tar.zst bun-bbd4504954b6eacb6cb5ee480a70b9cb65e6e700.zip |
Add a couple more tests for errors with Bun.file()
-rw-r--r-- | test/bun.js/fetch.test.js | 44 |
1 files changed, 42 insertions, 2 deletions
diff --git a/test/bun.js/fetch.test.js b/test/bun.js/fetch.test.js index ca8e387bf..5d0ca4854 100644 --- a/test/bun.js/fetch.test.js +++ b/test/bun.js/fetch.test.js @@ -1,5 +1,5 @@ -import { it, describe, expect } from "bun:test"; -import fs, { unlinkSync } from "fs"; +import { afterAll, beforeAll, describe, expect, it, test } from "bun:test"; +import fs, { chmodSync, unlinkSync } from "fs"; import { mkfifo } from "mkfifo"; import { gc, withoutAggressiveGC } from "./gc"; @@ -393,6 +393,46 @@ describe("Bun.file", () => { const { size } = Bun.file("/tmp/test-fifo"); expect(size).toBe(Infinity); }); + + function forEachMethod(fn) { + const method = ["arrayBuffer", "text", "json"]; + for (const m of method) { + test(m, fn(m)); + } + } + + describe("bad permissions throws", () => { + beforeAll(async () => { + try { + unlinkSync("/tmp/my-new-file"); + } catch {} + await Bun.write("/tmp/my-new-file", "hey"); + chmodSync("/tmp/my-new-file", 0o000); + }); + afterAll(() => { + try { + unlinkSync("/tmp/my-new-file"); + } catch {} + }); + + forEachMethod((m) => () => { + const file = Bun.file("/tmp/my-new-file"); + expect(async () => await file[m]()).toThrow("Permission denied"); + }); + }); + + describe("non-existent file throws", () => { + beforeAll(() => { + try { + unlinkSync("/tmp/does-not-exist"); + } catch {} + }); + + forEachMethod((m) => async () => { + const file = Bun.file("/tmp/does-not-exist"); + expect(async () => await file[m]()).toThrow("No such file or directory"); + }); + }); }); describe("Blob", () => { |