diff options
author | 2022-03-23 04:26:49 -0700 | |
---|---|---|
committer | 2022-03-23 04:26:49 -0700 | |
commit | 07d77c1e00a3c74774ab4ea8c79d5bdeb0d9be4a (patch) | |
tree | 52b4a5c69ac1f0a5433166a92004a3134ba935f2 /integration/bunjs-only-snippets | |
parent | 5e5f0bd2930a0e1a2f897ba6e857be05e8ca04f2 (diff) | |
download | bun-07d77c1e00a3c74774ab4ea8c79d5bdeb0d9be4a.tar.gz bun-07d77c1e00a3c74774ab4ea8c79d5bdeb0d9be4a.tar.zst bun-07d77c1e00a3c74774ab4ea8c79d5bdeb0d9be4a.zip |
[bun.js] Bun.write for macOS
Diffstat (limited to 'integration/bunjs-only-snippets')
-rw-r--r-- | integration/bunjs-only-snippets/html-rewriter.test.js | 13 | ||||
-rw-r--r-- | integration/bunjs-only-snippets/response.file.test.js | 50 |
2 files changed, 54 insertions, 9 deletions
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("<div><blink>it worked!</blink></div>"); }); + it("(from file) supports element handlers", async () => { + var rewriter = new HTMLRewriter(); + rewriter.on("div", { + element(element) { + element.setInnerContent("<blink>it worked!</blink>", { html: true }); + }, + }); + await Bun.write("/tmp/html-rewriter.txt.js", "<div>hello</div>"); + var input = new Response(Bun.file("/tmp/html-rewriter.txt.js")); + var output = rewriter.transform(input); + expect(await output.text()).toBe("<div><blink>it worked!</blink></div>"); + }); + 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"); |