diff options
author | 2022-10-11 00:04:48 -0700 | |
---|---|---|
committer | 2022-10-11 00:04:48 -0700 | |
commit | 3867431ed18966c88eeadfb238a0535893f0f6d3 (patch) | |
tree | a61b6141fa4b7443d014885a9f0a960cfc323862 /test/bun.js | |
parent | 1f7f5646debd2a2eb90127b9a4c501de6edded43 (diff) | |
download | bun-3867431ed18966c88eeadfb238a0535893f0f6d3.tar.gz bun-3867431ed18966c88eeadfb238a0535893f0f6d3.tar.zst bun-3867431ed18966c88eeadfb238a0535893f0f6d3.zip |
Add test that reads & writes stdin/stderr
Diffstat (limited to 'test/bun.js')
-rw-r--r-- | test/bun.js/bash-echo.sh | 7 | ||||
-rw-r--r-- | test/bun.js/spawn.test.ts | 18 | ||||
-rw-r--r-- | test/bun.js/streams.test.js | 15 |
3 files changed, 32 insertions, 8 deletions
diff --git a/test/bun.js/bash-echo.sh b/test/bun.js/bash-echo.sh new file mode 100644 index 000000000..ad5eabdd6 --- /dev/null +++ b/test/bun.js/bash-echo.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +echoerr() { echo "$@" 1>&2; } + +myvar=$(cat /dev/stdin) +# echoerr ${#myvar} chars +echo -e "$myvar" diff --git a/test/bun.js/spawn.test.ts b/test/bun.js/spawn.test.ts index a393922c0..f7426660f 100644 --- a/test/bun.js/spawn.test.ts +++ b/test/bun.js/spawn.test.ts @@ -2,7 +2,7 @@ import { readableStreamToText, spawn } from "bun"; import { describe, expect, it } from "bun:test"; describe("spawn", () => { - const hugeString = "hello".repeat(100000).slice(); + const hugeString = "hello".repeat(10000).slice(); it("stdout can be read", async () => { await Bun.write("/tmp/out.txt", hugeString); @@ -15,6 +15,22 @@ describe("spawn", () => { expect(text).toBe(hugeString); }); + it("stdin can be read and stdout can be written", async () => { + const { stdout, stdin, exited } = spawn({ + cmd: ["bash", import.meta.dir + "/bash-echo.sh"], + stdout: "pipe", + stdin: "pipe", + stderr: "inherit", + }); + + await stdin.write(hugeString); + await stdin.end(); + + const text = await readableStreamToText(stdout); + expect(text.trim()).toBe(hugeString); + await exited; + }); + describe("pipe", () => { function huge() { return spawn({ diff --git a/test/bun.js/streams.test.js b/test/bun.js/streams.test.js index 20aa5c270..88620e9e6 100644 --- a/test/bun.js/streams.test.js +++ b/test/bun.js/streams.test.js @@ -37,25 +37,26 @@ it("Bun.file() read text from pipe", async () => { }); const exited = proc.exited; proc.ref(); + var prom = new Promise((resolve, reject) => { setTimeout(() => { (async function () { var reader = out.getReader(); + var total = 0; + while (true) { const chunk = await reader.read(); - if (chunk.done) { - if (chunks.length == 0) { - out = Bun.file("/tmp/fifo").stream(); - reader = out.getReader(); - continue; - } + total += chunk.value?.byteLength || 0; + if (chunk.value) chunks.push(chunk.value); + + if (chunk.done || total >= large.length) { const output = new TextDecoder() .decode(new Uint8Array(Buffer.concat(chunks))) .trim(); + reader.releaseLock(); resolve(output); break; } - chunks.push(chunk.value); } })(); }); |