2017-05-24 00:19:04 +03:00
|
|
|
"use strict";
|
|
|
|
|
2017-09-03 16:31:33 +03:00
|
|
|
const fs = require("fs");
|
2017-09-07 08:16:45 +03:00
|
|
|
const path = require("path");
|
2017-10-02 20:56:34 +03:00
|
|
|
const stripAnsi = require("strip-ansi");
|
2017-05-24 00:19:04 +03:00
|
|
|
|
2017-10-02 18:58:55 +03:00
|
|
|
const isProduction = process.env.NODE_ENV === "production";
|
2018-01-12 19:19:20 +03:00
|
|
|
const prettierRootDir = isProduction ? process.env.PRETTIER_DIR : "../";
|
|
|
|
const prettierPkg = require(path.join(prettierRootDir, "package.json"));
|
|
|
|
const prettierCli = path.join(prettierRootDir, prettierPkg.bin.prettier);
|
|
|
|
|
2017-12-26 04:23:50 +03:00
|
|
|
const thirdParty = isProduction
|
2018-01-12 19:19:20 +03:00
|
|
|
? path.join(prettierRootDir, "./third-party")
|
|
|
|
: path.join(prettierRootDir, "./src/common/third-party");
|
2017-10-02 18:58:55 +03:00
|
|
|
|
2017-06-07 02:10:24 +03:00
|
|
|
function runPrettier(dir, args, options) {
|
2017-09-09 06:49:22 +03:00
|
|
|
args = args || [];
|
|
|
|
options = options || {};
|
|
|
|
|
2017-09-03 12:21:06 +03:00
|
|
|
let status;
|
|
|
|
let stdout = "";
|
|
|
|
let stderr = "";
|
2017-05-24 00:19:04 +03:00
|
|
|
|
2017-10-02 18:58:55 +03:00
|
|
|
jest.spyOn(process, "exit").mockImplementation(exitCode => {
|
2017-09-03 12:21:06 +03:00
|
|
|
if (status === undefined) {
|
|
|
|
status = exitCode || 0;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-10-02 18:58:55 +03:00
|
|
|
jest
|
|
|
|
.spyOn(process.stdout, "write")
|
|
|
|
.mockImplementation(text => appendStdout(text));
|
2017-09-03 12:21:06 +03:00
|
|
|
|
2017-10-02 18:58:55 +03:00
|
|
|
jest
|
|
|
|
.spyOn(process.stderr, "write")
|
|
|
|
.mockImplementation(text => appendStderr(text));
|
2017-09-03 12:21:06 +03:00
|
|
|
|
2017-10-02 18:58:55 +03:00
|
|
|
jest
|
|
|
|
.spyOn(console, "log")
|
|
|
|
.mockImplementation(text => appendStdout(text + "\n"));
|
2017-09-03 12:21:06 +03:00
|
|
|
|
2017-10-02 18:58:55 +03:00
|
|
|
jest
|
|
|
|
.spyOn(console, "warn")
|
|
|
|
.mockImplementation(text => appendStderr(text + "\n"));
|
2017-09-03 12:21:06 +03:00
|
|
|
|
2017-10-02 18:58:55 +03:00
|
|
|
jest
|
|
|
|
.spyOn(console, "error")
|
|
|
|
.mockImplementation(text => appendStderr(text + "\n"));
|
2017-05-24 00:19:04 +03:00
|
|
|
|
2017-10-06 09:20:47 +03:00
|
|
|
jest.spyOn(Date, "now").mockImplementation(() => 0);
|
|
|
|
|
2017-09-03 16:31:33 +03:00
|
|
|
const write = [];
|
|
|
|
|
2017-10-02 18:58:55 +03:00
|
|
|
jest.spyOn(fs, "writeFileSync").mockImplementation((filename, content) => {
|
2017-09-03 16:31:33 +03:00
|
|
|
write.push({ filename, content });
|
|
|
|
});
|
|
|
|
|
2017-09-03 12:21:06 +03:00
|
|
|
const originalCwd = process.cwd();
|
|
|
|
const originalArgv = process.argv;
|
|
|
|
const originalExitCode = process.exitCode;
|
2017-09-07 08:16:45 +03:00
|
|
|
const originalStdinIsTTY = process.stdin.isTTY;
|
2017-10-05 14:02:31 +03:00
|
|
|
const originalStdoutIsTTY = process.stdout.isTTY;
|
2017-05-24 00:19:04 +03:00
|
|
|
|
2017-09-03 12:21:06 +03:00
|
|
|
process.chdir(normalizeDir(dir));
|
2017-09-09 06:49:22 +03:00
|
|
|
process.stdin.isTTY = !!options.isTTY;
|
2017-10-05 14:02:31 +03:00
|
|
|
process.stdout.isTTY = !!options.stdoutIsTTY;
|
2017-09-09 06:49:22 +03:00
|
|
|
process.argv = ["path/to/node", "path/to/prettier/bin"].concat(args);
|
2017-05-24 00:19:04 +03:00
|
|
|
|
2017-09-03 12:21:06 +03:00
|
|
|
jest.resetModules();
|
2017-10-02 18:58:55 +03:00
|
|
|
|
|
|
|
// 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.
|
2017-11-22 14:27:40 +03:00
|
|
|
jest.spyOn(require(thirdParty), "getStream").mockImplementation(() => ({
|
|
|
|
then: handler => handler(options.input || "")
|
|
|
|
}));
|
2017-10-02 18:58:55 +03:00
|
|
|
jest
|
2017-11-22 14:27:40 +03:00
|
|
|
.spyOn(require(thirdParty), "cosmiconfig")
|
|
|
|
.mockImplementation((moduleName, options) =>
|
|
|
|
require("cosmiconfig")(
|
|
|
|
moduleName,
|
|
|
|
Object.assign({}, options, { stopDir: __dirname })
|
|
|
|
)
|
|
|
|
);
|
2018-05-09 14:17:12 +03:00
|
|
|
jest
|
|
|
|
.spyOn(require(thirdParty), "findParentDir")
|
|
|
|
.mockImplementation(() => process.cwd());
|
2017-09-03 12:21:06 +03:00
|
|
|
|
|
|
|
try {
|
2017-10-02 18:58:55 +03:00
|
|
|
require(prettierCli);
|
2017-09-08 05:59:48 +03:00
|
|
|
status = (status === undefined ? process.exitCode : status) || 0;
|
2017-09-03 12:21:06 +03:00
|
|
|
} catch (error) {
|
|
|
|
status = 1;
|
2017-09-07 08:16:45 +03:00
|
|
|
stderr += error.message;
|
2017-09-03 12:21:06 +03:00
|
|
|
} finally {
|
|
|
|
process.chdir(originalCwd);
|
2017-09-07 08:16:45 +03:00
|
|
|
process.argv = originalArgv;
|
|
|
|
process.exitCode = originalExitCode;
|
|
|
|
process.stdin.isTTY = originalStdinIsTTY;
|
2017-10-05 14:02:31 +03:00
|
|
|
process.stdout.isTTY = originalStdoutIsTTY;
|
2017-09-14 08:23:10 +03:00
|
|
|
jest.restoreAllMocks();
|
2017-09-03 12:21:06 +03:00
|
|
|
}
|
|
|
|
|
2017-10-02 19:08:34 +03:00
|
|
|
const result = { status, stdout, stderr, write };
|
|
|
|
|
|
|
|
const testResult = testOptions => {
|
|
|
|
testOptions = testOptions || {};
|
|
|
|
|
|
|
|
Object.keys(result).forEach(name => {
|
|
|
|
test(`(${name})`, () => {
|
2017-10-02 20:56:34 +03:00
|
|
|
const value =
|
|
|
|
typeof result[name] === "string"
|
|
|
|
? stripAnsi(result[name])
|
|
|
|
: result[name];
|
2017-10-02 19:08:34 +03:00
|
|
|
if (name in testOptions) {
|
|
|
|
if (name === "status" && testOptions[name] === "non-zero") {
|
2017-10-02 20:56:34 +03:00
|
|
|
expect(value).not.toEqual(0);
|
2017-10-02 19:08:34 +03:00
|
|
|
} else {
|
2017-10-02 20:56:34 +03:00
|
|
|
expect(value).toEqual(testOptions[name]);
|
2017-10-02 19:08:34 +03:00
|
|
|
}
|
|
|
|
} else {
|
2017-10-02 20:56:34 +03:00
|
|
|
expect(value).toMatchSnapshot();
|
2017-10-02 19:08:34 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2017-10-06 06:12:32 +03:00
|
|
|
|
|
|
|
return result;
|
2017-10-02 19:08:34 +03:00
|
|
|
};
|
|
|
|
|
2017-10-14 04:17:01 +03:00
|
|
|
return Object.assign({ test: testResult }, result);
|
2017-09-07 08:16:45 +03:00
|
|
|
|
|
|
|
function appendStdout(text) {
|
|
|
|
if (status === undefined) {
|
|
|
|
stdout += text;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function appendStderr(text) {
|
|
|
|
if (status === undefined) {
|
|
|
|
stderr += text;
|
|
|
|
}
|
|
|
|
}
|
2017-09-03 12:21:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function normalizeDir(dir) {
|
|
|
|
const isRelative = dir[0] !== "/";
|
|
|
|
return isRelative ? path.resolve(__dirname, dir) : dir;
|
2017-05-24 00:19:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = runPrettier;
|