aboutsummaryrefslogtreecommitdiff
path: root/integration/bunjs-only-snippets/response.file.test.js
diff options
context:
space:
mode:
authorGravatar Jarred SUmner <jarred@jarredsumner.com> 2022-03-23 01:46:47 -0700
committerGravatar Jarred SUmner <jarred@jarredsumner.com> 2022-03-23 01:46:47 -0700
commit5e5f0bd2930a0e1a2f897ba6e857be05e8ca04f2 (patch)
treec7b707280cba036f57ba2888bde6034845cb0448 /integration/bunjs-only-snippets/response.file.test.js
parent9974142eef9909866128951f64f4beb8a9617db7 (diff)
downloadbun-5e5f0bd2930a0e1a2f897ba6e857be05e8ca04f2.tar.gz
bun-5e5f0bd2930a0e1a2f897ba6e857be05e8ca04f2.tar.zst
bun-5e5f0bd2930a0e1a2f897ba6e857be05e8ca04f2.zip
[bun.js] Implement Bun.write()
Diffstat (limited to 'integration/bunjs-only-snippets/response.file.test.js')
-rw-r--r--integration/bunjs-only-snippets/response.file.test.js52
1 files changed, 52 insertions, 0 deletions
diff --git a/integration/bunjs-only-snippets/response.file.test.js b/integration/bunjs-only-snippets/response.file.test.js
index ab7c51b5f..f9cb886a2 100644
--- a/integration/bunjs-only-snippets/response.file.test.js
+++ b/integration/bunjs-only-snippets/response.file.test.js
@@ -1,6 +1,58 @@
import fs from "fs";
import { it, expect } from "bun:test";
import path from "path";
+
+it("Bun.write('out.txt', 'string')", async () => {
+ for (let erase of [true, false]) {
+ if (erase) {
+ try {
+ fs.unlinkSync(path.join("/tmp", "out.txt"));
+ } catch (e) {}
+ }
+
+ const out = await Bun.write("/tmp/out.txt", "string");
+ expect(await out.text()).toBe("string");
+ expect(await out.text()).toBe(fs.readFileSync("/tmp/out.txt", "utf8"));
+ }
+});
+
+it("Bun.file -> Bun.file", async () => {
+ try {
+ fs.unlinkSync(path.join("/tmp", "fetch.js.in"));
+ } catch (e) {}
+
+ try {
+ fs.unlinkSync(path.join("/tmp", "fetch.js.out"));
+ } catch (e) {}
+
+ const file = path.join(import.meta.dir, "fetch.js.txt");
+ const text = fs.readFileSync(file, "utf8");
+ fs.writeFileSync("/tmp/fetch.js.in", text, { mode: 0644 });
+ {
+ const result = await Bun.write(
+ Bun.file("/tmp/fetch.js.out"),
+ Bun.file("/tmp/fetch.js.in")
+ );
+ expect(await result.text()).toBe(text);
+ }
+
+ {
+ const result = 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));
+ }
+
+ {
+ const result = await Bun.write(
+ "/tmp/fetch.js.in",
+ Bun.file("/tmp/fetch.js.out")
+ );
+ expect(await result.text()).toBe(text);
+ }
+});
+
it("Bun.file", async () => {
const file = path.join(import.meta.dir, "fetch.js.txt");
expect(await Bun.file(file).text()).toBe(fs.readFileSync(file, "utf8"));