Test: Use worker to run CLI test (#12705)
This commit is contained in:
1 parent
11720f1280
commit
567e4d9e79
12 files changed
+157
-125
No files matched your search
@@ -0,0 +1,73 @@
|
||||
import { workerData, parentPort } from "node:worker_threads";
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import createEsmUtils from "esm-utils";
|
||||
|
||||
const { __dirname, require } = createEsmUtils(import.meta);
|
||||
|
||||
async function run() {
|
||||
const { options, thirdParty: thirdPartyModuleFile, prettierCli } = workerData;
|
||||
|
||||
Date.now = () => 0;
|
||||
// eslint-disable-next-line require-await
|
||||
fs.promises.writeFile = async (filename, content) => {
|
||||
parentPort.postMessage({
|
||||
action: "write-file",
|
||||
data: { filename, content },
|
||||
});
|
||||
};
|
||||
|
||||
/*
|
||||
A fake non-existing directory to test plugin search won't crash.
|
||||
|
||||
See:
|
||||
- `isDirectory` function in `src/common/load-plugins.js`
|
||||
- Test file `./__tests__/plugin-virtual-directory.js`
|
||||
- Pull request #5819
|
||||
*/
|
||||
const originalStat = fs.promises.stat;
|
||||
fs.promises.statSync = (filename) =>
|
||||
originalStat(
|
||||
path.basename(filename) === "virtualDirectory"
|
||||
? import.meta.url
|
||||
: filename
|
||||
);
|
||||
|
||||
process.stdin.isTTY = Boolean(options.isTTY);
|
||||
process.stdout.isTTY = Boolean(options.stdoutIsTTY);
|
||||
|
||||
const thirdParty = require(thirdPartyModuleFile);
|
||||
|
||||
// We cannot use `jest.setMock("get-stream", impl)` here, because in the
|
||||
// production build everything is bundled into one file so there is no
|
||||
// "get-stream" module to mock.
|
||||
// eslint-disable-next-line require-await
|
||||
thirdParty.getStdin = async () => options.input || "";
|
||||
thirdParty.isCI = () => Boolean(options.ci);
|
||||
thirdParty.cosmiconfig = (moduleName, options) =>
|
||||
require("cosmiconfig").cosmiconfig(moduleName, {
|
||||
...options,
|
||||
stopDir: path.join(__dirname, "cli"),
|
||||
});
|
||||
thirdParty.findParentDir = () => process.cwd();
|
||||
await require(prettierCli).promise;
|
||||
}
|
||||
|
||||
parentPort.on("message", async () => {
|
||||
const originalExit = process.exit;
|
||||
|
||||
// https://github.com/nodejs/node/issues/30491
|
||||
process.stdout.cork();
|
||||
process.stderr.cork();
|
||||
process.exit = (code) => {
|
||||
process.stdout.end();
|
||||
process.stderr.end();
|
||||
originalExit(code);
|
||||
};
|
||||
|
||||
try {
|
||||
await run();
|
||||
} finally {
|
||||
process.exit();
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user