From afc7da33c952c1f900317eb022187f9be40643c6 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Mon, 4 Apr 2022 01:07:38 -0700 Subject: Add a couple more tests --- .../bunjs-only-snippets/response.file.test.js | 27 ++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'integration/bunjs-only-snippets/response.file.test.js') diff --git a/integration/bunjs-only-snippets/response.file.test.js b/integration/bunjs-only-snippets/response.file.test.js index 97e5902c5..a7c829d57 100644 --- a/integration/bunjs-only-snippets/response.file.test.js +++ b/integration/bunjs-only-snippets/response.file.test.js @@ -170,6 +170,7 @@ it("Bun.file -> Response", async () => { await gcTick(); const resp = await fetch("https://example.com"); await gcTick(); + expect(await Bun.write("/tmp/fetch.js.out", resp)).toBe(text.length); await gcTick(); expect(await Bun.file("/tmp/fetch.js.out").text()).toBe(text); @@ -189,3 +190,29 @@ it("Response -> Bun.file -> Response -> text", async () => { expect(await response2.text()).toBe(text); await gcTick(); }); + +// If you write nothing to a file, it shouldn't modify it +// If you want to truncate a file, it should be more explicit +it("Bun.write('output.html', '')", async () => { + await Bun.write("/tmp/output.html", "lalalala"); + expect(await Bun.write("/tmp/output.html", "")).toBe(0); + expect(await Bun.file("/tmp/output.html").text()).toBe("lalalala"); +}); + +// Since Bun.file is resolved lazily, this needs to specifically be checked +it("Bun.write('output.html', HTMLRewriter.transform(Bun.file)))", 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); + const outpath = `/tmp/html-rewriter.${Date.now()}.html`; + await Bun.write(outpath, output); + expect(await Bun.file(outpath).text()).toBe( + "
it worked!
" + ); +}); -- cgit v1.2.3