-
-
Notifications
You must be signed in to change notification settings - Fork 626
/
Copy pathtest.utils.js
226 lines (189 loc) · 6 KB
/
test.utils.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
"use strict";
const testUtilsPkg = require("../utils/test-utils.js");
const { processKill } = testUtilsPkg;
const path = require("path");
const execa = require("execa");
const { node: execaNode } = execa;
const stripAnsi = require("strip-ansi");
const concat = require("concat-stream");
const { Writable } = require("readable-stream");
const ENABLE_LOG_COMPILATION = process.env.ENABLE_PIPE || false;
const nodeVersion = parseInt(process.versions.node.split(".")[0]);
function createPathDependentUtils(cli) {
const CLI_PATH = path.resolve(__dirname, `../../packages/${cli}/bin/cli.js`);
/**
* Webpack CLI test runner.
*
* @param {string} cwd The path to folder that contains test
* @param {Array<string>} args Array of arguments
* @param {Object<string, any>} options Options for tests
* @returns {Promise}
*/
const createProcess = (cwd, args, options) => {
const { nodeOptions = [] } = options;
const processExecutor = nodeOptions.length ? execaNode : execa;
return processExecutor(CLI_PATH, args, {
cwd: path.resolve(cwd),
reject: false,
stdio: ENABLE_LOG_COMPILATION ? "inherit" : "pipe",
maxBuffer: Infinity,
env: { WEBPACK_CLI_HELP_WIDTH: 1024 },
...options,
});
};
/**
* Run the webpack CLI for a test case.
*
* @param {string} cwd The path to folder that contains test
* @param {Array<string>} args Array of arguments
* @param {Object<string, any>} options Options for tests
* @returns {Promise}
*/
const run = async (cwd, args = [], options = {}) => {
return createProcess(cwd, args, options);
};
/**
* Run the webpack CLI for a test case and get process.
*
* @param {string} cwd The path to folder that contains test
* @param {Array<string>} args Array of arguments
* @param {Object<string, any>} options Options for tests
* @returns {Promise}
*/
const runAndGetProcess = (cwd, args = [], options = {}) => {
return createProcess(cwd, args, options);
};
/**
* Run the webpack CLI in watch mode for a test case.
*
* @param {string} cwd The path to folder that contains test
* @param {Array<string>} args Array of arguments
* @param {Object<string, any>} options Options for tests
* @returns {Object} The webpack output or Promise when nodeOptions are present
*/
const runWatch = (cwd, args = [], options = {}) => {
return new Promise((resolve, reject) => {
const process = createProcess(cwd, args, options);
const outputKillStr = options.killString || /webpack \d+\.\d+\.\d/;
const stdoutKillStr = options.stdoutKillStr;
const stderrKillStr = options.stderrKillStr;
let isStdoutDone = false;
let isStderrDone = false;
process.stdout.pipe(
new Writable({
write(chunk, encoding, callback) {
const output = stripAnsi(chunk.toString("utf8"));
if (stdoutKillStr && stdoutKillStr.test(output)) {
isStdoutDone = true;
} else if (!stdoutKillStr && outputKillStr.test(output)) {
processKill(process);
}
if (isStdoutDone && isStderrDone) {
processKill(process);
}
callback();
},
}),
);
process.stderr.pipe(
new Writable({
write(chunk, encoding, callback) {
const output = stripAnsi(chunk.toString("utf8"));
if (stderrKillStr && stderrKillStr.test(output)) {
isStderrDone = true;
} else if (!stderrKillStr && outputKillStr.test(output)) {
processKill(process);
}
if (isStdoutDone && isStderrDone) {
processKill(process);
}
callback();
},
}),
);
process
.then((result) => {
resolve(result);
})
.catch((error) => {
reject(error);
});
});
};
/*
* runPromptWithAnswers
* @param {string} location of current working directory
* @param {string[]} args CLI args to pass in
* @param {string[]} answers to be passed to stdout for inquirer question
*/
const runPromptWithAnswers = (location, args, answers) => {
const process = runAndGetProcess(location, args);
process.stdin.setDefaultEncoding("utf-8");
const delay = 1000;
let outputTimeout;
let currentAnswer = 0;
const writeAnswer = (output) => {
if (!answers) {
process.stdin.write(output);
processKill(process);
return;
}
if (currentAnswer < answers.length) {
process.stdin.write(answers[currentAnswer]);
currentAnswer++;
}
};
process.stdout.pipe(
new Writable({
write(chunk, encoding, callback) {
const output = chunk.toString("utf8");
if (output.length > 0) {
if (outputTimeout) {
clearTimeout(outputTimeout);
}
// we must receive new stdout, then have 1 second
// without any stdout before writing the next answer
outputTimeout = setTimeout(() => {
writeAnswer(output);
}, delay);
}
callback();
},
}),
);
return new Promise((resolve) => {
const obj = {};
let stdoutDone = false;
let stderrDone = false;
const complete = () => {
if (outputTimeout) {
clearTimeout(outputTimeout);
}
if (stdoutDone && stderrDone) {
processKill(process);
resolve(obj);
}
};
process.stdout.pipe(
concat((result) => {
stdoutDone = true;
obj.stdout = result.toString();
complete();
}),
);
process.stderr.pipe(
concat((result) => {
stderrDone = true;
obj.stderr = result.toString();
complete();
}),
);
});
};
return { runAndGetProcess, run, runWatch, runPromptWithAnswers };
}
module.exports = {
createPathDependentUtils,
nodeVersion,
...testUtilsPkg,
};