Fix: Only create @babel/node IPC channel when needed (#13295)

Co-authored-by: Huáng Jùnliàng <jlhwung@gmail.com>
This commit is contained in:
Clark Jacobsohn
2021-05-11 17:59:07 -04:00
committed by GitHub
parent 875fc8e693
commit cca97d1e78
8 changed files with 38 additions and 35 deletions

View File

@@ -44,7 +44,7 @@ const saveInFiles = function (files) {
});
};
const assertTest = function (stdout, stderr, opts) {
const assertTest = function (stdout, stderr, ipcMessage, opts) {
const expectStderr = opts.stderr.trim();
stderr = stderr.trim();
@@ -72,6 +72,10 @@ const assertTest = function (stdout, stderr, opts) {
throw new Error("stdout:\n" + stdout);
}
if (opts.ipc) {
expect(ipcMessage).toEqual(opts.ipcMessage);
}
if (opts.outFiles) {
const actualFiles = readDir(path.join(tmpLoc));
@@ -101,10 +105,16 @@ const buildTest = function (testName, opts) {
args.push("--config-file", "../config.json");
args = args.concat(opts.args);
const spawn = child.spawn(process.execPath, args);
const spawnOpts = {};
if (opts.ipc) {
spawnOpts.stdio = ["pipe", "pipe", "pipe", "ipc"];
}
const spawn = child.spawn(process.execPath, args, spawnOpts);
let stderr = "";
let stdout = "";
let ipcMessage;
spawn.stderr.on("data", function (chunk) {
stderr += chunk;
@@ -114,11 +124,17 @@ const buildTest = function (testName, opts) {
stdout += chunk;
});
if (opts.ipc) {
spawn.on("message", function (message) {
ipcMessage = message;
});
}
spawn.on("close", function () {
let err;
try {
assertTest(stdout, stderr, opts);
assertTest(stdout, stderr, ipcMessage, opts);
} catch (e) {
err = e;
}

View File

@@ -1,2 +1,2 @@
process.send({ hello: "world" });
console.log("sent");
console.log("ipc enabled");

View File

@@ -0,0 +1,6 @@
{
"args": ["payload.js"],
"ipcMessage": { "hello": "world" },
"ipc": true,
"stdout": "ipc enabled"
}

View File

@@ -0,0 +1,3 @@
if (process.send == null) {
console.log("ipc disabled");
}

View File

@@ -1,4 +1,4 @@
{
"args": ["payload.js"],
"stdout": "sent"
"stdout": "ipc disabled"
}

View File

@@ -1 +0,0 @@
process.send("hello");

View File

@@ -1,27 +0,0 @@
import { spawn } from "child_process";
import path from "path";
import { fileURLToPath } from "url";
const dirname = path.dirname(fileURLToPath(import.meta.url));
const binLoc = path.join(dirname, "../bin/babel-node.js");
const childLoc = path.join(dirname, "fixtures/misc/child.js");
describe("babel-node", () => {
it("ipc works with spawned babel-node process", done => {
expect.assertions(1);
const child = spawn(process.execPath, [binLoc, childLoc], {
stdio: ["inherit", "inherit", "inherit", "ipc"],
});
child.on("message", msg => {
expect(msg).toBe("hello");
done();
});
child.on("error", error => {
console.error(error);
done();
});
child.on("exit", () => done());
}, /* timeout */ 20_000);
});