/
test-utils.js
272 lines (239 loc) · 8.45 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
'use strict';
const path = require('path');
const fs = require('fs');
const execa = require('execa');
const { exec } = require('child_process');
const { sync: spawnSync, node: execaNode } = execa;
const { Writable } = require('readable-stream');
const concat = require('concat-stream');
const { version } = require('webpack');
const stripAnsi = require('strip-ansi');
const isWebpack5 = version.startsWith('5');
let devServerVersion;
try {
// eslint-disable-next-line
devServerVersion = require('webpack-dev-server/package.json').version;
} catch (error) {
// Nothing
}
const isDevServer4 = devServerVersion && devServerVersion.startsWith('4');
const WEBPACK_PATH = path.resolve(__dirname, '../../packages/webpack-cli/bin/cli.js');
const ENABLE_LOG_COMPILATION = process.env.ENABLE_PIPE || false;
const isWindows = process.platform === 'win32';
const hyphenToUpperCase = (name) => {
if (!name) {
return name;
}
return name.replace(/-([a-z])/g, function (g) {
return g[1].toUpperCase();
});
};
/**
* Run the webpack CLI for a test case.
*
* @param {String} testCase The path to folder that contains the webpack.config.js
* @param {Array} args Array of arguments to pass to webpack
* @param {Boolean} setOutput Boolean that decides if a default output path will be set or not
* @param {Array<string>} nodeOptions Boolean that decides if a default output path will be set or not
* @param {Record<string, any>} env Boolean that decides if a default output path will be set or not
* @returns {Object} The webpack output or Promise when nodeOptions are present
*/
const run = (testCase, args = [], setOutput = true, nodeOptions = [], env) => {
const cwd = path.resolve(testCase);
const outputPath = path.resolve(testCase, 'bin');
const processExecutor = nodeOptions.length ? execaNode : spawnSync;
const argsWithOutput = setOutput ? args.concat('--output-path', outputPath) : args;
const result = processExecutor(WEBPACK_PATH, argsWithOutput, {
cwd,
reject: false,
nodeOptions: nodeOptions,
env,
stdio: ENABLE_LOG_COMPILATION ? 'inherit' : 'pipe',
maxBuffer: Infinity,
});
return result;
};
const runWatch = (testCase, args = [], setOutput = true, outputKillStr = /webpack \d+\.\d+\.\d/) => {
const cwd = path.resolve(testCase);
const outputPath = path.resolve(testCase, 'bin');
const argsWithOutput = setOutput ? args.concat('--output-path', outputPath) : args;
return new Promise((resolve, reject) => {
const proc = execa(WEBPACK_PATH, argsWithOutput, {
cwd,
reject: false,
stdio: 'pipe',
});
proc.stdout.pipe(
new Writable({
write(chunk, encoding, callback) {
const output = stripAnsi(chunk.toString('utf8'));
if (outputKillStr.test(output)) {
if (isWindows) {
exec('taskkill /pid ' + proc.pid + ' /T /F');
} else {
proc.kill();
}
}
callback();
},
}),
);
proc.then((result) => {
resolve(result);
}).catch((error) => {
reject(error);
});
});
};
const runAndGetWatchProc = (testCase, args = [], setOutput = true, input = '', forcePipe = false) => {
const cwd = path.resolve(testCase);
const outputPath = path.resolve(testCase, 'bin');
const argsWithOutput = setOutput ? args.concat('--output-path', outputPath) : args;
const options = {
cwd,
reject: false,
stdio: ENABLE_LOG_COMPILATION && !forcePipe ? 'inherit' : 'pipe',
};
// some tests don't work if the input option is an empty string
if (input) {
options.input = input;
}
const webpackProc = execa(WEBPACK_PATH, argsWithOutput, options);
return webpackProc;
};
/**
* runPromptWithAnswers
* @param {string} location location of current working directory
* @param {string[]} args CLI args to pass in
* @param {string[]} answers answers to be passed to stdout for inquirer question
* @param {boolean} waitForOutput whether to wait for stdout before writing the next answer
*/
const runPromptWithAnswers = (location, args, answers, waitForOutput = true) => {
const runner = runAndGetWatchProc(location, args, false, '', true);
runner.stdin.setDefaultEncoding('utf-8');
const delay = 2000;
let outputTimeout;
if (waitForOutput) {
let currentAnswer = 0;
const writeAnswer = () => {
if (currentAnswer < answers.length) {
runner.stdin.write(answers[currentAnswer]);
currentAnswer++;
}
};
runner.stdout.pipe(
new Writable({
write(chunk, encoding, callback) {
const output = chunk.toString('utf8');
if (output) {
if (outputTimeout) {
clearTimeout(outputTimeout);
}
// we must receive new stdout, then have 2 seconds
// without any stdout before writing the next answer
outputTimeout = setTimeout(writeAnswer, delay);
}
callback();
},
}),
);
} else {
// Simulate answers by sending the answers every 2s
answers.reduce((prevAnswer, answer) => {
return prevAnswer.then(() => {
return new Promise((resolvePromise) => {
setTimeout(() => {
runner.stdin.write(answer);
resolvePromise();
}, delay);
});
});
}, Promise.resolve());
}
return new Promise((resolve) => {
const obj = {};
let stdoutDone = false;
let stderrDone = false;
const complete = () => {
if (outputTimeout) {
clearTimeout(outputTimeout);
}
if (stdoutDone && stderrDone) {
runner.kill('SIGKILL');
resolve(obj);
}
};
runner.stdout.pipe(
concat((result) => {
stdoutDone = true;
obj.stdout = result.toString();
complete();
}),
);
runner.stderr.pipe(
concat((result) => {
stderrDone = true;
obj.stderr = result.toString();
complete();
}),
);
});
};
/**
*
* @param {String} testCase - testCase directory
* @param {String} file - file relative to testCase
* @param {String} data - data to append
* @returns {undefined}
* @throws - throw an Error if file does not exist
*/
const appendDataIfFileExists = (testCase, file, data) => {
const filePath = path.resolve(testCase, file);
if (fs.existsSync(filePath)) {
fs.appendFileSync(filePath, data);
} else {
throw new Error(`Oops! ${filePath} does not exist!`);
}
};
/**
* fs.copyFileSync was added in Added in: v8.5.0
* We should refactor the below code once our minimal supported version is v8.5.0
* @param {String} testCase - testCase directory
* @param {String} file - file relative to testCase which is going to be copied
* @returns {String} - absolute file path of new file
* @throws - throw an Error if file copy fails
*/
const copyFileAsync = async (testCase, file) => {
const fileToChangePath = path.resolve(testCase, file);
const fileMetaData = path.parse(file);
const fileCopyName = fileMetaData.name.concat('_copy').concat(fileMetaData.ext);
const copyFilePath = path.resolve(testCase, fileCopyName);
fs.access(fileToChangePath, fs.F_OK, (accessErr) => {
if (accessErr) throw new Error(`Oops! ${fileToChangePath} does not exist!`);
});
const data = fs.readFileSync(fileToChangePath);
fs.writeFileSync(copyFilePath, data);
return copyFilePath;
};
const runInstall = async (cwd) => {
await execa('yarn', {
cwd,
});
};
const runServe = (args, testPath) => {
return runWatch(testPath, ['serve'].concat(args), false);
};
module.exports = {
run,
runWatch,
runServe,
runAndGetWatchProc,
runPromptWithAnswers,
appendDataIfFileExists,
copyFileAsync,
runInstall,
hyphenToUpperCase,
isWebpack5,
isDevServer4,
isWindows,
};