mirror of
https://github.com/oven-sh/bun
synced 2026-02-10 02:48:50 +00:00
### What does this PR do? Add missing check for .write() on a data-backed blob ### How did you verify your code works? There is a test --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Alistair Smith <hi@alistair.sh>
76 lines
2.8 KiB
TypeScript
76 lines
2.8 KiB
TypeScript
import { expect, test } from "bun:test";
|
|
import { tempDirWithFiles } from "harness";
|
|
import path from "path";
|
|
|
|
test("blob.write() throws for data-backed blob", () => {
|
|
const blob = new Blob(["Hello, world!"]);
|
|
expect(() => blob.write("test.txt")).toThrowErrorMatchingInlineSnapshot(
|
|
`"Cannot write to a Blob backed by bytes, which are always read-only"`,
|
|
);
|
|
});
|
|
|
|
test("Bun.file(path).write() does not throw", async () => {
|
|
const file = Bun.file(path.join(tempDirWithFiles("bun-write", { a: "Hello, world!" }), "a"));
|
|
expect(() => file.write(new Blob(["Hello, world!!"]))).not.toThrow();
|
|
expect(await file.text()).toBe("Hello, world!!");
|
|
});
|
|
|
|
test("blob.unlink() throws for data-backed blob", () => {
|
|
const blob = new Blob(["Hello, world!"]);
|
|
expect(() => blob.unlink()).toThrowErrorMatchingInlineSnapshot(
|
|
`"Cannot write to a Blob backed by bytes, which are always read-only"`,
|
|
);
|
|
});
|
|
|
|
test("blob.delete() throws for data-backed blob", () => {
|
|
const blob = new Blob(["Hello, world!"]);
|
|
expect(() => blob.delete()).toThrowErrorMatchingInlineSnapshot(
|
|
`"Cannot write to a Blob backed by bytes, which are always read-only"`,
|
|
);
|
|
});
|
|
|
|
test("Bun.file(path).unlink() does not throw", async () => {
|
|
const dir = tempDirWithFiles("bun-unlink", { a: "Hello, world!" });
|
|
const file = Bun.file(path.join(dir, "a"));
|
|
expect(file.unlink()).resolves.toBeUndefined();
|
|
expect(await Bun.file(path.join(dir, "a")).exists()).toBe(false);
|
|
});
|
|
|
|
test("Bun.file(path).delete() does not throw", async () => {
|
|
const dir = tempDirWithFiles("bun-unlink", { a: "Hello, world!" });
|
|
const file = Bun.file(path.join(dir, "a"));
|
|
expect(file.delete()).resolves.toBeUndefined();
|
|
expect(await Bun.file(path.join(dir, "a")).exists()).toBe(false);
|
|
});
|
|
|
|
test("blob.writer() throws for data-backed blob", () => {
|
|
const blob = new Blob(["Hello, world!"]);
|
|
expect(() => blob.writer()).toThrowErrorMatchingInlineSnapshot(
|
|
`"Cannot write to a Blob backed by bytes, which are always read-only"`,
|
|
);
|
|
});
|
|
|
|
test("Bun.file(path).writer() does not throw", async () => {
|
|
const dir = tempDirWithFiles("bun-writer", {});
|
|
const file = Bun.file(path.join(dir, "test.txt"));
|
|
const writer = file.writer();
|
|
expect(writer).toBeDefined();
|
|
writer.write("New content");
|
|
await writer.end();
|
|
expect(await file.text()).toBe("New content");
|
|
});
|
|
|
|
test("blob.stat() returns undefined for data-backed blob", async () => {
|
|
const blob = new Blob(["Hello, world!"]);
|
|
const stat = await blob.stat();
|
|
expect(stat).toBeUndefined();
|
|
});
|
|
|
|
test("Bun.file(path).stat() returns stats", async () => {
|
|
const dir = tempDirWithFiles("bun-stat", { a: "Hello, world!" });
|
|
const file = Bun.file(path.join(dir, "a"));
|
|
const stat = await file.stat();
|
|
expect(stat).toBeDefined();
|
|
expect(stat.size).toBe(13); // "Hello, world!" is 13 bytes
|
|
});
|