From b8586b33dad8ab66ebd5d1aa4d5a0df305266e7f Mon Sep 17 00:00:00 2001 From: Derrick Farris Date: Fri, 2 Dec 2022 10:25:13 -0600 Subject: feat(process): add process.{stdin, stdout, stderr} support (#1495) * fix(stream): get Duplex working * feat(process): add stdin,stdout,stderr in a semi-broken state (pipes??) * test(NodeTestHelpers): fix test names * test(NodeTestHelpers): add test for createDoneDotAll done called w error * test(NodeTestHelpers): remove stray console.log * fix(stream): fix bug in Duplex, Readable * test(process.stdio): rename test * fix(process.stdio): change onData listener to onReadable * refactor(streams): add file-wide debug fn, destructure opts * fix(child_process): check isCallable on promise * fix: get stdio streams mostly working (mostly) * fix(child_process): wait until stream is drained before calling end? * fix(child_process): change to result?.then * debug(child_process,streams): add EE id tracking, add shim for stdio after handle is dead * test(child_process): fix double pipe test, temp fix for ChildProcess.kill() return val * fix(child_process): remove immediate emit of exit on kill * debug(streams): add more debug log * debug(streams): add more debug logs part 2 * feat(streams,fs): add NativeWritable, adapt fs.WriteStream and fs.ReadStream to native --- test/bun.js/process-stdio.test.js | 80 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 test/bun.js/process-stdio.test.js (limited to 'test/bun.js/process-stdio.test.js') diff --git a/test/bun.js/process-stdio.test.js b/test/bun.js/process-stdio.test.js new file mode 100644 index 000000000..75ab0e49f --- /dev/null +++ b/test/bun.js/process-stdio.test.js @@ -0,0 +1,80 @@ +import { describe, it, expect, beforeAll } from "bun:test"; +import { spawn, execSync } from "node:child_process"; + +const CHILD_PROCESS_FILE = import.meta.dir + "/spawned-child.js"; +const OUT_FILE = import.meta.dir + "/stdio-test-out.txt"; + +// describe("process.stdout", () => { +// // it("should allow us to write to it", () => { +// // process.stdout.write("Bun is cool\n"); +// // }); +// // it("should allow us to use a file as stdout", () => { +// // const output = "Bun is cool\n"; +// // execSync(`rm -f ${OUT_FILE}`); +// // const result = execSync(`bun ${CHILD_PROCESS_FILE} STDOUT > ${OUT_FILE}`, { +// // encoding: "utf8", +// // stdin, +// // }); +// // expect(result).toBe(output); +// // expect(readSync(OUT_FILE)).toBe(output); +// // }); +// }); + +describe("process.stdin", () => { + it("should allow us to read from stdin in readable mode", (done) => { + // Child should read from stdin and write it back + const child = spawn("bun", [CHILD_PROCESS_FILE, "STDIN", "READABLE"]); + child.stdout.setEncoding("utf8"); + child.stdout.on("data", (data) => { + expect(data.trim()).toBe("data: hello"); + done(); + }); + child.stdin.write("hello\n"); + child.stdin.end(); + }); + + it("should allow us to read from stdin via flowing mode", (done) => { + // Child should read from stdin and write it back + const child = spawn("bun", [CHILD_PROCESS_FILE, "STDIN", "FLOWING"]); + child.stdout.setEncoding("utf8"); + child.stdout.on("data", (data) => { + expect(data.trim()).toBe("data: hello"); + done(); + }); + child.stdin.write("hello\n"); + child.stdin.end(); + }); + + it("should allow us to read > 65kb from stdin", (done) => { + // Child should read from stdin and write it back + const child = spawn("bun", [CHILD_PROCESS_FILE, "STDIN", "FLOWING"]); + child.stdout.setEncoding("utf8"); + + const numReps = Math.ceil((66 * 1024) / 5); + const input = "hello".repeat(numReps); + + let data = ""; + child.stdout.on("end", () => { + expect(data).toBe(`data: ${input}`); + done(); + }); + child.stdout.on("readable", () => { + let chunk; + while ((chunk = child.stdout.read()) !== null) { + data += chunk.trim(); + } + }); + child.stdin.write(input); + child.stdin.end(); + }); + + it("should allow us to read from a file", () => { + const result = execSync( + `bun ${CHILD_PROCESS_FILE} STDIN FLOWING < ${ + import.meta.dir + }/readFileSync.txt`, + { encoding: "utf8" }, + ); + expect(result.trim()).toEqual("File read successfully"); + }); +}); -- cgit v1.2.3