-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
runPrettier.js
160 lines (133 loc) · 4.55 KB
/
runPrettier.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"use strict";
const fs = require("fs");
const path = require("path");
const stripAnsi = require("strip-ansi");
const isProduction = process.env.NODE_ENV === "production";
const prettierRootDir = isProduction ? process.env.PRETTIER_DIR : "../";
const prettierPkg = require(path.join(prettierRootDir, "package.json"));
const prettierCli = path.join(prettierRootDir, prettierPkg.bin.prettier);
const thirdParty = isProduction
? path.join(prettierRootDir, "./third-party")
: path.join(prettierRootDir, "./src/common/third-party");
function runPrettier(dir, args, options) {
args = args || [];
options = options || {};
let status;
let stdout = "";
let stderr = "";
jest.spyOn(process, "exit").mockImplementation(exitCode => {
if (status === undefined) {
status = exitCode || 0;
}
});
jest
.spyOn(process.stdout, "write")
.mockImplementation(text => appendStdout(text));
jest
.spyOn(process.stderr, "write")
.mockImplementation(text => appendStderr(text));
jest
.spyOn(console, "log")
.mockImplementation(text => appendStdout(text + "\n"));
jest
.spyOn(console, "warn")
.mockImplementation(text => appendStderr(text + "\n"));
jest
.spyOn(console, "error")
.mockImplementation(text => appendStderr(text + "\n"));
jest.spyOn(Date, "now").mockImplementation(() => 0);
const write = [];
jest.spyOn(fs, "writeFileSync").mockImplementation((filename, content) => {
write.push({ filename, content });
});
const originalCwd = process.cwd();
const originalArgv = process.argv;
const originalExitCode = process.exitCode;
const originalStdinIsTTY = process.stdin.isTTY;
const originalStdoutIsTTY = process.stdout.isTTY;
const originalEnv = process.env;
process.chdir(normalizeDir(dir));
process.stdin.isTTY = !!options.isTTY;
process.stdout.isTTY = !!options.stdoutIsTTY;
process.argv = ["path/to/node", "path/to/prettier/bin"].concat(args);
process.env = Object.assign({}, process.env, options.env);
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.spyOn(require(thirdParty), "getStream").mockImplementation(() => ({
then: handler => handler(options.input || "")
}));
jest
.spyOn(require(thirdParty), "isCI")
.mockImplementation(() => process.env.CI);
jest
.spyOn(require(thirdParty), "cosmiconfig")
.mockImplementation((moduleName, options) =>
require("cosmiconfig")(
moduleName,
Object.assign({}, options, { stopDir: __dirname })
)
);
jest
.spyOn(require(thirdParty), "findParentDir")
.mockImplementation(() => process.cwd());
try {
require(prettierCli);
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;
process.env = originalEnv;
jest.restoreAllMocks();
}
const result = { status, stdout, stderr, write };
const testResult = testOptions => {
testOptions = testOptions || {};
Object.keys(result).forEach(name => {
test(`(${name})`, () => {
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
typeof result[name] === "string"
? options.ignoreLineEndings
? stripAnsi(result[name]).replace(/\r/g, "")
: stripAnsi(result[name]).replace(/\r/g, "/*CR*/")
: result[name];
if (name in testOptions) {
if (name === "status" && testOptions[name] === "non-zero") {
expect(value).not.toEqual(0);
} else {
expect(value).toEqual(testOptions[name]);
}
} else {
expect(value).toMatchSnapshot();
}
});
});
return result;
};
return Object.assign({ 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;