From 07d77c1e00a3c74774ab4ea8c79d5bdeb0d9be4a Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Wed, 23 Mar 2022 04:26:49 -0700 Subject: [bun.js] Bun.write for macOS --- .../bunjs-only-snippets/html-rewriter.test.js | 13 ++++++ .../bunjs-only-snippets/response.file.test.js | 50 ++++++++++++++++++---- 2 files changed, 54 insertions(+), 9 deletions(-) (limited to 'integration/bunjs-only-snippets') diff --git a/integration/bunjs-only-snippets/html-rewriter.test.js b/integration/bunjs-only-snippets/html-rewriter.test.js index bb63d8d25..a4ca965aa 100644 --- a/integration/bunjs-only-snippets/html-rewriter.test.js +++ b/integration/bunjs-only-snippets/html-rewriter.test.js @@ -42,6 +42,19 @@ describe("HTMLRewriter", () => { expect(await output.text()).toBe("
it worked!
"); }); + it("(from file) supports element handlers", async () => { + var rewriter = new HTMLRewriter(); + rewriter.on("div", { + element(element) { + element.setInnerContent("it worked!", { html: true }); + }, + }); + await Bun.write("/tmp/html-rewriter.txt.js", "
hello
"); + var input = new Response(Bun.file("/tmp/html-rewriter.txt.js")); + var output = rewriter.transform(input); + expect(await output.text()).toBe("
it worked!
"); + }); + it("supports attribute iterator", async () => { var rewriter = new HTMLRewriter(); var expected = [ diff --git a/integration/bunjs-only-snippets/response.file.test.js b/integration/bunjs-only-snippets/response.file.test.js index f9cb886a2..6e1191433 100644 --- a/integration/bunjs-only-snippets/response.file.test.js +++ b/integration/bunjs-only-snippets/response.file.test.js @@ -10,12 +10,32 @@ it("Bun.write('out.txt', 'string')", async () => { } catch (e) {} } - const out = await Bun.write("/tmp/out.txt", "string"); + await Bun.write("/tmp/out.txt", "string"); + const out = Bun.file("/tmp/out.txt"); expect(await out.text()).toBe("string"); expect(await out.text()).toBe(fs.readFileSync("/tmp/out.txt", "utf8")); } }); +it("Bun.write blob", async () => { + await Bun.write( + Bun.file("/tmp/response-file.test.txt"), + Bun.file(path.join(import.meta.dir, "fetch.js.txt")) + ); + await Bun.write(Bun.file("/tmp/response-file.test.txt"), "blah blah blha"); + await Bun.write( + Bun.file("/tmp/response-file.test.txt"), + new Uint32Array(1024) + ); + await Bun.write("/tmp/response-file.test.txt", new Uint32Array(1024)); + expect( + await Bun.write( + new TextEncoder().encode("/tmp/response-file.test.txt"), + new Uint32Array(1024) + ) + ).toBe(new Uint32Array(1024).byteLength); +}); + it("Bun.file -> Bun.file", async () => { try { fs.unlinkSync(path.join("/tmp", "fetch.js.in")); @@ -33,23 +53,22 @@ it("Bun.file -> Bun.file", async () => { Bun.file("/tmp/fetch.js.out"), Bun.file("/tmp/fetch.js.in") ); - expect(await result.text()).toBe(text); + expect(await Bun.file("/tmp/fetch.js.out").text()).toBe(text); } { - const result = await Bun.write( + await Bun.write( Bun.file("/tmp/fetch.js.in").slice(0, (text.length / 2) | 0), Bun.file("/tmp/fetch.js.out") ); - expect(await result.text()).toBe(text.substring(0, (text.length / 2) | 0)); + expect(await Bun.file("/tmp/fetch.js.in").text()).toBe( + text.substring(0, (text.length / 2) | 0) + ); } { - const result = await Bun.write( - "/tmp/fetch.js.in", - Bun.file("/tmp/fetch.js.out") - ); - expect(await result.text()).toBe(text); + await Bun.write("/tmp/fetch.js.in", Bun.file("/tmp/fetch.js.out")); + expect(await Bun.file("/tmp/fetch.js.in").text()).toBe(text); } }); @@ -99,6 +118,19 @@ it("Response -> Bun.file", async () => { expect(await response.text()).toBe(text); }); +it("Bun.file -> Response", async () => { + // ensure the file doesn't already exist + try { + fs.unlinkSync("/tmp/fetch.js.out"); + } catch {} + + const file = path.join(import.meta.dir, "fetch.js.txt"); + const text = fs.readFileSync(file, "utf8"); + const resp = await fetch("https://example.com"); + expect(await Bun.write("/tmp/fetch.js.out", resp)).toBe(text.length); + expect(await Bun.file("/tmp/fetch.js.out").text()).toBe(text); +}); + it("Response -> Bun.file -> Response -> text", async () => { const file = path.join(import.meta.dir, "fetch.js.txt"); const text = fs.readFileSync(file, "utf8"); -- cgit v1.2.3