mirror of
https://github.com/oven-sh/bun
synced 2026-02-15 05:12:29 +00:00
* node:child_process: support defining extra pipes * unneeded * lazily load node:fs * use $isJSArray instead of ArrayIsArray * remove std.log call * don't close child fd we don't own * close child fd's in parent * add Subprocess.stdio getter that aligns with ChildProcess.stdio fd's * [autofix.ci] apply automated fixes * use ArrayList instead of BoundedArray for stdio_pipes * fix stream primordials * dont use unreachable for syscalls * this file was testing Bun.spawn not child_process.spawn * skip ipc for now * ensure the socketpair is created non-blocking on non-mac posix * allow creating a node:net.Socket from an fd via node:net.connect * node:stream tidy * node:child_process: use net.Socket for stdio instead of fs streams * try again * fix Socket eager loading --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
152 lines
4.0 KiB
JavaScript
152 lines
4.0 KiB
JavaScript
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");
|
|
});
|
|
});
|
|
|
|
describe("process.stdio pipes", () => {
|
|
it("is writable", () => {
|
|
const child = spawn(bunExe(), [import.meta.dir + "/fixtures/child-process-pipe-read.js"], {
|
|
env: bunEnv,
|
|
stdio: ["pipe", "pipe", "pipe", "pipe"],
|
|
});
|
|
const pipe = child.stdio[3];
|
|
expect(pipe).not.toBe(null);
|
|
pipe.write("stdout_test");
|
|
|
|
child.stdout.on("data", data => {
|
|
try {
|
|
expect(data).toBe("stdout_test");
|
|
done();
|
|
} catch (err) {
|
|
done(err);
|
|
}
|
|
});
|
|
});
|
|
|
|
it("is readable", () => {
|
|
const child = spawn(bunExe(), [import.meta.dir + "/fixtures/child-process-pipe-read.js"], {
|
|
env: bunEnv,
|
|
stdio: ["pipe", "pipe", "pipe", "pipe"],
|
|
});
|
|
const pipe = child.stdio[3];
|
|
expect(pipe).not.toBe(null);
|
|
|
|
child.stdout.on("data", data => {
|
|
try {
|
|
expect(data).toBe("stdout_test");
|
|
done();
|
|
} catch (err) {
|
|
done(err);
|
|
}
|
|
});
|
|
});
|
|
});
|