Move and refactor child_process tests
This commit is contained in:
@ -60,9 +60,9 @@ class ChildProcess extends CallbackEmitter implements cp.ChildProcess {
|
||||
}
|
||||
|
||||
ae.on("disconnect", () => childProcess.disconnect());
|
||||
ae.on("kill", (signal) => childProcess.kill(signal));
|
||||
ae.on("kill", (signal: string) => childProcess.kill(signal));
|
||||
ae.on("ref", () => childProcess.ref());
|
||||
ae.on("send", (message, callbackId) => childProcess.send(message, maybeCallback(ae, callbackId)));
|
||||
ae.on("send", (message: string, callbackId: number) => childProcess.send(message, maybeCallback(ae, callbackId)));
|
||||
ae.on("unref", () => childProcess.unref());
|
||||
|
||||
ae.emit("pid", childProcess.pid);
|
||||
@ -72,9 +72,15 @@ class ChildProcess extends CallbackEmitter implements cp.ChildProcess {
|
||||
childProcess.on("exit", (code, signal) => ae.emit("exit", code, signal));
|
||||
childProcess.on("message", (message) => ae.emit("message", message));
|
||||
|
||||
bindWritable(createUniqueEval(ae, "stdin"), childProcess.stdin);
|
||||
bindReadable(createUniqueEval(ae, "stdout"), childProcess.stdout);
|
||||
bindReadable(createUniqueEval(ae, "stderr"), childProcess.stderr);
|
||||
if (childProcess.stdin) {
|
||||
bindWritable(createUniqueEval(ae, "stdin"), childProcess.stdin);
|
||||
}
|
||||
if (childProcess.stdout) {
|
||||
bindReadable(createUniqueEval(ae, "stdout"), childProcess.stdout);
|
||||
}
|
||||
if (childProcess.stderr) {
|
||||
bindReadable(createUniqueEval(ae, "stderr"), childProcess.stderr);
|
||||
}
|
||||
|
||||
return {
|
||||
onDidDispose: (cb): cp.ChildProcess => childProcess.on("close", cb),
|
||||
|
74
packages/ide/test/child_process.test.ts
Normal file
74
packages/ide/test/child_process.test.ts
Normal file
@ -0,0 +1,74 @@
|
||||
import { ChildProcess } from "child_process";
|
||||
import * as path from "path";
|
||||
import { Readable } from "stream";
|
||||
import * as util from "util";
|
||||
import { createClient } from "@coder/protocol/test";
|
||||
|
||||
const client = createClient();
|
||||
jest.mock("../src/fill/client", () => ({ client }));
|
||||
const cp = require("../src/fill/child_process") as typeof import("child_process");
|
||||
|
||||
describe("child_process", () => {
|
||||
const getStdout = async (proc: ChildProcess): Promise<string> => {
|
||||
return new Promise((r): Readable => proc.stdout.on("data", r))
|
||||
.then((s) => s.toString());
|
||||
};
|
||||
|
||||
describe("exec", () => {
|
||||
it("should get exec stdout", async () => {
|
||||
await expect(util.promisify(cp.exec)("echo test", { encoding: "utf8" }))
|
||||
.resolves.toEqual({
|
||||
stdout: "test\n",
|
||||
stderr: "",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("spawn", () => {
|
||||
it("should get spawn stdout", async () => {
|
||||
const proc = cp.spawn("echo", ["test"]);
|
||||
await expect(Promise.all([
|
||||
getStdout(proc),
|
||||
new Promise((r): ChildProcess => proc.on("exit", r)),
|
||||
]).then((values) => values[0])).resolves.toEqual("test\n");
|
||||
});
|
||||
|
||||
it("should cat", async () => {
|
||||
const proc = cp.spawn("cat", []);
|
||||
expect(proc.pid).toBe(-1);
|
||||
proc.stdin.write("banana");
|
||||
await expect(getStdout(proc)).resolves.toBe("banana");
|
||||
|
||||
proc.stdin.end();
|
||||
proc.kill();
|
||||
|
||||
expect(proc.pid).toBeGreaterThan(-1);
|
||||
await new Promise((r): ChildProcess => proc.on("exit", r));
|
||||
});
|
||||
|
||||
it("should print env", async () => {
|
||||
const proc = cp.spawn("env", [], {
|
||||
env: { hi: "donkey" },
|
||||
});
|
||||
|
||||
await expect(getStdout(proc)).resolves.toContain("hi=donkey\n");
|
||||
});
|
||||
});
|
||||
|
||||
describe("fork", () => {
|
||||
it("should echo messages", async () => {
|
||||
const proc = cp.fork(path.join(__dirname, "forker.js"));
|
||||
|
||||
proc.send({ bananas: true });
|
||||
|
||||
await expect(new Promise((r): ChildProcess => proc.on("message", r)))
|
||||
.resolves.toMatchObject({
|
||||
bananas: true,
|
||||
});
|
||||
|
||||
proc.kill();
|
||||
|
||||
await new Promise((r): ChildProcess => proc.on("exit", r));
|
||||
});
|
||||
});
|
||||
});
|
3
packages/ide/test/forker.js
Normal file
3
packages/ide/test/forker.js
Normal file
@ -0,0 +1,3 @@
|
||||
process.on("message", (data) => {
|
||||
process.send(data);
|
||||
});
|
Reference in New Issue
Block a user