aboutsummaryrefslogtreecommitdiff
path: root/test/js/node/child_process/child-process-stdio.test.js
diff options
context:
space:
mode:
authorGravatar Ashcon Partovi <ashcon@partovi.net> 2023-03-07 12:22:34 -0800
committerGravatar GitHub <noreply@github.com> 2023-03-07 12:22:34 -0800
commitf7e4eb83694aa007a492ef66c28ffbe6a2dae791 (patch)
tree7af25aa5c42a2e1b2b47ba1df35f8caa9054cbeb /test/js/node/child_process/child-process-stdio.test.js
parent36275a44ce7a33587bd26aad120042ab95470ff3 (diff)
downloadbun-f7e4eb83694aa007a492ef66c28ffbe6a2dae791.tar.gz
bun-f7e4eb83694aa007a492ef66c28ffbe6a2dae791.tar.zst
bun-f7e4eb83694aa007a492ef66c28ffbe6a2dae791.zip
Reorganize tests (#2332)
Diffstat (limited to 'test/js/node/child_process/child-process-stdio.test.js')
-rw-r--r--test/js/node/child_process/child-process-stdio.test.js112
1 files changed, 112 insertions, 0 deletions
diff --git a/test/js/node/child_process/child-process-stdio.test.js b/test/js/node/child_process/child-process-stdio.test.js
new file mode 100644
index 000000000..d0c6d9bc7
--- /dev/null
+++ b/test/js/node/child_process/child-process-stdio.test.js
@@ -0,0 +1,112 @@
+import { describe, it, expect, beforeAll } from "bun:test";
+import { spawn, execSync } from "node:child_process";
+import { bunExe, bunEnv } from "harness";
+
+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", done => {
+ const child = spawn(bunExe(), [CHILD_PROCESS_FILE, "STDOUT"], {
+ env: bunEnv,
+ });
+ child.stdout.setEncoding("utf8");
+ child.stdout.on("data", data => {
+ try {
+ expect(data).toBe("stdout_test");
+ done();
+ } catch (err) {
+ done(err);
+ }
+ });
+ });
+});
+
+describe("process.stdin", () => {
+ it("should allow us to read from stdin in readable mode", done => {
+ const input = "hello\n";
+ // Child should read from stdin and write it back
+ const child = spawn(bunExe(), [CHILD_PROCESS_FILE, "STDIN", "READABLE"], {
+ env: bunEnv,
+ });
+ let data = "";
+ child.stdout.setEncoding("utf8");
+ child.stdout
+ .on("data", chunk => {
+ data += chunk;
+ })
+ .on("end", function () {
+ try {
+ expect(data).toBe(`data: ${input}`);
+ done();
+ } catch (err) {
+ done(err);
+ }
+ });
+ child.stdin.write(input);
+ child.stdin.end();
+ });
+
+ it("should allow us to read from stdin via flowing mode", done => {
+ const input = "hello\n";
+ // Child should read from stdin and write it back
+ const child = spawn(bunExe(), [CHILD_PROCESS_FILE, "STDIN", "FLOWING"], {
+ env: bunEnv,
+ });
+ let data = "";
+ child.stdout.setEncoding("utf8");
+ child.stdout
+ .on("readable", () => {
+ let chunk;
+ while ((chunk = child.stdout.read()) !== null) {
+ data += chunk;
+ }
+ })
+ .on("end", function () {
+ try {
+ expect(data).toBe(`data: ${input}`);
+ done();
+ } catch (err) {
+ done(err);
+ }
+ });
+ child.stdin.write(input);
+ child.stdin.end();
+ });
+
+ it("should allow us to read > 65kb from stdin", done => {
+ const numReps = Math.ceil((66 * 1024) / 5);
+ const input = "hello".repeat(numReps);
+ // Child should read from stdin and write it back
+ const child = spawn(bunExe(), [CHILD_PROCESS_FILE, "STDIN", "FLOWING"], {
+ env: bunEnv,
+ });
+ let data = "";
+ child.stdout.setEncoding("utf8");
+ child.stdout
+ .on("readable", () => {
+ let chunk;
+ while ((chunk = child.stdout.read()) !== null) {
+ data += chunk;
+ }
+ })
+ .on("end", function () {
+ try {
+ expect(data).toBe(`data: ${input}`);
+ done();
+ } catch (err) {
+ done(err);
+ }
+ });
+ child.stdin.write(input);
+ child.stdin.end();
+ });
+
+ it("should allow us to read from a file", () => {
+ const result = execSync(`${bunExe()} ${CHILD_PROCESS_FILE} STDIN FLOWING < ${import.meta.dir}/readFileSync.txt`, {
+ encoding: "utf8",
+ env: bunEnv,
+ });
+ expect(result).toEqual("data: File read successfully");
+ });
+});