Files
test-repo/tests_integration/runPrettier.js
T

167 lines
4.6 KiB
JavaScript
Raw Normal View History

"use strict";
2017-09-03 21:31:33 +08:00
const fs = require("fs");
const path = require("path");
2017-10-02 12:56:34 -05:00
const stripAnsi = require("strip-ansi");
2020-01-14 23:30:18 +08:00
const { SynchronousPromise } = require("synchronous-promise");
2020-01-15 22:55:08 +08:00
const { prettierCli, thirdParty } = require("./env");
function runPrettier(dir, args, options) {
2017-09-09 11:49:22 +08:00
args = args || [];
options = options || {};
let status;
let stdout = "";
let stderr = "";
2020-03-21 22:07:52 +02:00
jest.spyOn(process, "exit").mockImplementation((exitCode) => {
if (status === undefined) {
status = exitCode || 0;
}
});
jest
.spyOn(process.stdout, "write")
2020-03-21 22:07:52 +02:00
.mockImplementation((text) => appendStdout(text));
jest
.spyOn(process.stderr, "write")
2020-03-21 22:07:52 +02:00
.mockImplementation((text) => appendStderr(text));
jest
.spyOn(console, "log")
2020-03-21 22:07:52 +02:00
.mockImplementation((text) => appendStdout(text + "\n"));
jest
.spyOn(console, "warn")
2020-03-21 22:07:52 +02:00
.mockImplementation((text) => appendStderr(text + "\n"));
jest
.spyOn(console, "error")
2020-03-21 22:07:52 +02:00
.mockImplementation((text) => appendStderr(text + "\n"));
jest.spyOn(Date, "now").mockImplementation(() => 0);
2017-09-03 21:31:33 +08:00
const write = [];
jest.spyOn(fs, "writeFileSync").mockImplementation((filename, content) => {
2017-09-03 21:31:33 +08:00
write.push({ filename, content });
});
2019-02-05 12:47:55 +00:00
const origStatSync = fs.statSync;
2020-03-21 22:07:52 +02:00
jest.spyOn(fs, "statSync").mockImplementation((filename) => {
2020-01-14 18:58:20 +08:00
if (path.basename(filename) === "virtualDirectory") {
2019-02-05 12:47:55 +00:00
return origStatSync(path.join(__dirname, __filename));
}
return origStatSync(filename);
});
const originalCwd = process.cwd();
const originalArgv = process.argv;
const originalExitCode = process.exitCode;
const originalStdinIsTTY = process.stdin.isTTY;
const originalStdoutIsTTY = process.stdout.isTTY;
process.chdir(normalizeDir(dir));
2017-09-09 11:49:22 +08:00
process.stdin.isTTY = !!options.isTTY;
process.stdout.isTTY = !!options.stdoutIsTTY;
2017-09-09 11:49:22 +08:00
process.argv = ["path/to/node", "path/to/prettier/bin"].concat(args);
jest.resetModules();
// 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.
jest
2020-09-17 07:32:19 +08:00
.spyOn(require(thirdParty), "getStdin")
.mockImplementation(() => SynchronousPromise.resolve(options.input || ""));
jest
.spyOn(require(thirdParty), "isCI")
.mockImplementation(() => !!options.ci);
jest
2017-11-22 19:27:40 +08:00
.spyOn(require(thirdParty), "cosmiconfig")
.mockImplementation((moduleName, options) =>
2020-01-19 07:23:17 +08:00
require("cosmiconfig").cosmiconfig(moduleName, {
...options,
2020-03-21 22:07:52 +02:00
stopDir: __dirname,
2020-01-19 07:23:17 +08:00
})
2020-01-10 09:21:44 +08:00
);
jest
.spyOn(require(thirdParty), "cosmiconfigSync")
.mockImplementation((moduleName, options) =>
2020-01-19 07:23:17 +08:00
require("cosmiconfig").cosmiconfigSync(moduleName, {
...options,
2020-03-21 22:07:52 +02:00
stopDir: __dirname,
2020-01-19 07:23:17 +08:00
})
2017-11-22 19:27:40 +08:00
);
jest
.spyOn(require(thirdParty), "findParentDir")
.mockImplementation(() => process.cwd());
try {
require(prettierCli);
2017-09-08 10:59:48 +08:00
status = (status === undefined ? process.exitCode : status) || 0;
} catch (error) {
status = 1;
stderr += error.message;
} finally {
process.chdir(originalCwd);
process.argv = originalArgv;
process.exitCode = originalExitCode;
process.stdin.isTTY = originalStdinIsTTY;
process.stdout.isTTY = originalStdoutIsTTY;
jest.restoreAllMocks();
}
const result = { status, stdout, stderr, write };
2020-03-21 22:07:52 +02:00
const testResult = (testOptions) => {
testOptions = testOptions || {};
2020-03-21 22:07:52 +02:00
Object.keys(result).forEach((name) => {
test(`(${name})`, () => {
2017-10-02 12:56:34 -05:00
const value =
// \r is trimmed from jest snapshots by default;
// manually replacing this character with /*CR*/ to test its true presence
// If ignoreLineEndings is specified, \r is simply deleted instead
2017-10-02 12:56:34 -05:00
typeof result[name] === "string"
? options.ignoreLineEndings
? stripAnsi(result[name]).replace(/\r/g, "")
: stripAnsi(result[name]).replace(/\r/g, "/*CR*/")
2017-10-02 12:56:34 -05:00
: result[name];
if (name in testOptions) {
if (name === "status" && testOptions[name] === "non-zero") {
2017-10-02 12:56:34 -05:00
expect(value).not.toEqual(0);
} else {
2017-10-02 12:56:34 -05:00
expect(value).toEqual(testOptions[name]);
}
} else {
2017-10-02 12:56:34 -05:00
expect(value).toMatchSnapshot();
}
});
});
2017-10-06 14:12:32 +11:00
return result;
};
2020-01-19 07:23:17 +08:00
return { test: testResult, ...result };
function appendStdout(text) {
if (status === undefined) {
stdout += text;
}
}
function appendStderr(text) {
if (status === undefined) {
stderr += text;
}
}
}
function normalizeDir(dir) {
const isRelative = dir[0] !== "/";
return isRelative ? path.resolve(__dirname, dir) : dir;
}
module.exports = runPrettier;