diff options
author | 2022-10-09 00:56:10 -0700 | |
---|---|---|
committer | 2022-10-09 02:02:47 -0700 | |
commit | dffaeaca1e83489fbf522dda5737bc392ca6ce00 (patch) | |
tree | 50a2204d69af82fa78ee90c6bf243bd269a90f83 /test/bun.js | |
parent | 88bdae8218d1b2783dddbc5b5250384f8355b236 (diff) | |
download | bun-dffaeaca1e83489fbf522dda5737bc392ca6ce00.tar.gz bun-dffaeaca1e83489fbf522dda5737bc392ca6ce00.tar.zst bun-dffaeaca1e83489fbf522dda5737bc392ca6ce00.zip |
Start to add tests for spawn
Diffstat (limited to 'test/bun.js')
-rw-r--r-- | test/bun.js/spawn.test.ts | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/test/bun.js/spawn.test.ts b/test/bun.js/spawn.test.ts new file mode 100644 index 000000000..0d62d0a5b --- /dev/null +++ b/test/bun.js/spawn.test.ts @@ -0,0 +1,89 @@ +import { test, expect, it, describe } from "bun:test"; +import { readableStreamToText, spawn } from "bun"; + +describe("spawn", () => { + const hugeString = "hello".repeat(100000).slice(); + + it("stdin can write", async () => { + const { stdin, stdout } = spawn({ + cmd: ["cat"], + stdin: "pipe", + stdout: "pipe", + }); + await stdin.write(hugeString); + stdin.end(); + return readableStreamToText(stdout).then((text) => { + expect(text).toBe(hugeString); + }); + }); + + describe("pipe", () => { + function huge() { + return spawn({ + cmd: ["echo", hugeString], + stdout: "pipe", + stdin: "pipe", + stderr: "inherit", + }); + } + + function helloWorld() { + return spawn({ + cmd: ["echo", "hello"], + stdout: "pipe", + stdin: "pipe", + }); + } + + const fixtures = [ + [helloWorld, "hello"], + [huge, hugeString], + ]; + + for (const [callback, fixture] of fixtures) { + describe(fixture.slice(0, 12), () => { + describe("should should allow reading stdout", () => { + it("before exit", async () => { + const process = callback(); + const output = await readableStreamToText(process.stdout); + const expected = fixture + "\n"; + expect(output.length).toBe(expected.length); + expect(output).toBe(expected); + + await process.exited; + }); + + it("before exit (chunked)", async () => { + const process = callback(); + var output = ""; + var reader = process.stdout.getReader(); + var done = false; + while (!done) { + var { value, done } = await reader.read(); + if (value) output += new TextDecoder().decode(value); + } + + const expected = fixture + "\n"; + expect(output.length).toBe(expected.length); + expect(output).toBe(expected); + + await process.exited; + }); + + it("after exit", async () => { + const process = callback(); + await process.stdin.end(); + + const output = await readableStreamToText(process.stdout); + const expected = fixture + "\n"; + + expect(output.length).toBe(expected.length); + expect(output).toBe(expected); + + await process.exited; + }); + }); + }); + } + }); +}); |