-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
Utils.ts
371 lines (326 loc) · 9.75 KB
/
Utils.ts
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import * as path from 'path';
import * as util from 'util';
import dedent = require('dedent');
import {
ExecaSyncError,
SyncOptions as ExecaSyncOptions,
ExecaSyncReturnValue,
sync as spawnSync,
} from 'execa';
import * as fs from 'graceful-fs';
import which = require('which');
import type {Config} from '@jest/types';
export const run = (
cmd: string,
cwd?: string,
env?: Record<string, string>,
): ExecaSyncReturnValue => {
const args = cmd.split(/\s/).slice(1);
const spawnOptions: ExecaSyncOptions = {
cwd,
env,
preferLocal: false,
reject: false,
};
const result = spawnSync(cmd.split(/\s/)[0], args, spawnOptions);
if (result.exitCode !== 0) {
const errorResult = result as ExecaSyncError;
// have to extract message for the `util.inspect` to be useful for some reason
const {message, ...rest} = errorResult;
errorResult.message += `\n\n${util.inspect(rest)}`;
throw errorResult;
}
return result;
};
const yarnInstallImmutable = 'yarn install --immutable';
const yarnInstallNoImmutable = 'yarn install --no-immutable';
export const runYarnInstall = (cwd: string, env?: Record<string, string>) => {
const lockfilePath = path.resolve(cwd, 'yarn.lock');
let exists = true;
// If the lockfile doesn't exist, yarn's project detection is confused. Just creating an empty file works
if (!fs.existsSync(lockfilePath)) {
exists = false;
fs.writeFileSync(lockfilePath, '');
}
try {
return run(
exists ? yarnInstallImmutable : yarnInstallNoImmutable,
cwd,
env,
);
} catch (error) {
try {
// retry once in case of e.g. permission errors
return run(
fs.readFileSync(lockfilePath, 'utf8').trim().length > 0
? yarnInstallImmutable
: yarnInstallNoImmutable,
cwd,
env,
);
} catch {
throw error;
}
}
};
export const linkJestPackage = (packageName: string, cwd: string) => {
const packagesDir = path.resolve(__dirname, '../packages');
const packagePath = path.resolve(packagesDir, packageName);
const destination = path.resolve(cwd, 'node_modules/', packageName);
fs.mkdirSync(destination, {recursive: true});
fs.rmSync(destination, {force: true, recursive: true});
fs.symlinkSync(packagePath, destination, 'junction');
};
export const makeTemplate =
(str: string): ((values?: Array<unknown>) => string) =>
(values = []) =>
str.replace(/\$(\d+)/g, (_match, number) => {
if (!Array.isArray(values)) {
throw new Error('Array of values must be passed to the template.');
}
return values[number - 1];
});
export const cleanup = (directory: string) => {
try {
fs.rmSync(directory, {force: true, recursive: true});
} catch (error) {
try {
// retry once in case of e.g. permission errors
fs.rmSync(directory, {force: true, recursive: true});
} catch {
throw error;
}
}
};
/**
* Creates a nested directory with files and their contents
* writeFiles(
* '/home/tmp',
* {
* 'package.json': '{}',
* '__tests__/test.test.js': 'test("lol")',
* }
* );
*/
export const writeFiles = (
directory: string,
files: {[filename: string]: string},
) => {
fs.mkdirSync(directory, {recursive: true});
Object.keys(files).forEach(fileOrPath => {
const dirname = path.dirname(fileOrPath);
if (dirname !== '/') {
fs.mkdirSync(path.join(directory, dirname), {recursive: true});
}
fs.writeFileSync(
path.resolve(directory, ...fileOrPath.split('/')),
dedent(files[fileOrPath]),
);
});
};
export const writeSymlinks = (
directory: string,
symlinks: {[existingFile: string]: string},
) => {
fs.mkdirSync(directory, {recursive: true});
Object.keys(symlinks).forEach(fileOrPath => {
const symLinkPath = symlinks[fileOrPath];
const dirname = path.dirname(symLinkPath);
if (dirname !== '/') {
fs.mkdirSync(path.join(directory, dirname), {recursive: true});
}
fs.symlinkSync(
path.resolve(directory, ...fileOrPath.split('/')),
path.resolve(directory, ...symLinkPath.split('/')),
'junction',
);
});
};
const NUMBER_OF_TESTS_TO_FORCE_USING_WORKERS = 25;
/**
* Forces Jest to use workers by generating many test files to run.
* Slow and modifies the test output. Use sparingly.
*/
export const generateTestFilesToForceUsingWorkers = () => {
const testFiles: Record<string, string> = {};
for (let i = 0; i <= NUMBER_OF_TESTS_TO_FORCE_USING_WORKERS; i++) {
testFiles[`__tests__/test${i}.test.js`] = `
test.todo('test ${i}');
`;
}
return testFiles;
};
export const copyDir = (src: string, dest: string) => {
const srcStat = fs.lstatSync(src);
if (srcStat.isDirectory()) {
if (!fs.existsSync(dest)) {
fs.mkdirSync(dest);
}
fs.readdirSync(src).map(filePath =>
copyDir(path.join(src, filePath), path.join(dest, filePath)),
);
} else {
fs.writeFileSync(dest, fs.readFileSync(src));
}
};
export const replaceSeed = (str: string) =>
str.replace(/Seed: {8}(-?\d+)/g, 'Seed: <<REPLACED>>');
export const replaceTime = (str: string) =>
str
.replace(/\d*\.?\d+ m?s\b/g, '<<REPLACED>>')
.replace(/, estimated <<REPLACED>>/g, '');
// Since Jest does not guarantee the order of tests we'll sort the output.
export const sortLines = (output: string) =>
output
.split('\n')
.sort()
.map(str => str.trim())
.join('\n');
export interface PackageJson {
dependencies?: Record<string, string>;
devDependencies?: Record<string, string>;
jest?: Config.InitialOptions;
}
const DEFAULT_PACKAGE_JSON: PackageJson = {
jest: {
testEnvironment: 'node',
},
};
export const createEmptyPackage = (
directory: string,
packageJson: PackageJson = DEFAULT_PACKAGE_JSON,
) => {
const packageJsonWithDefaults = {
...packageJson,
description: 'THIS IS AN AUTOGENERATED FILE AND SHOULD NOT BE ADDED TO GIT',
};
fs.mkdirSync(directory, {recursive: true});
fs.writeFileSync(
path.resolve(directory, 'package.json'),
JSON.stringify(packageJsonWithDefaults, null, 2),
);
};
export const extractSummary = (stdout: string) => {
const match = stdout
.replace(/(?:\\[rn])+/g, '\n')
.match(
/(Seed:.*\n)?Test Suites:.*\nTests.*\nSnapshots.*\nTime.*(\nRan all test suites)*.*\n*$/gm,
);
if (!match) {
throw new Error(dedent`
Could not find test summary in the output.
OUTPUT:
${stdout}
`);
}
const summary = replaceTime(match[0]);
const rest = stdout
.replace(match[0], '')
// remove all timestamps
.replace(/\s*\(\d*\.?\d+ m?s\b\)$/gm, '');
return {
rest: rest.trim(),
summary: summary.trim(),
};
};
const sortTests = (stdout: string) =>
stdout
.split('\n')
.reduce<Array<Array<string>>>((tests, line) => {
if (['RUNS', 'PASS', 'FAIL'].includes(line.slice(0, 4))) {
tests.push([line]);
} else {
tests[tests.length - 1].push(line);
}
return tests;
}, [])
.sort(([a], [b]) => (a > b ? 1 : -1))
.map(strings =>
strings.length > 1 ? `${strings.join('\n').trimRight()}\n` : strings[0],
)
.join('\n')
.trim();
export const extractSortedSummary = (stdout: string) => {
const {rest, summary} = extractSummary(stdout);
return {
rest: sortTests(replaceTime(rest)),
summary,
};
};
export const extractSummaries = (
stdout: string,
): Array<{rest: string; summary: string}> => {
const regex =
/(Seed:.*\n)?Test Suites:.*\nTests.*\nSnapshots.*\nTime.*(\nRan all test suites)*.*\n*$/gm;
let match = regex.exec(stdout);
const matches: Array<RegExpExecArray> = [];
while (match) {
matches.push(match);
match = regex.exec(stdout);
}
return matches
.map((currentMatch, i) => {
const prevMatch = matches[i - 1];
const start = prevMatch ? prevMatch.index + prevMatch[0].length : 0;
const end = currentMatch.index + currentMatch[0].length;
return {end, start};
})
.map(({start, end}) => extractSortedSummary(stdout.slice(start, end)));
};
export const normalizeIcons = (str: string) => {
if (!str) {
return str;
}
// Make sure to keep in sync with `jest-util/src/specialChars`
return str
.replace(new RegExp('\u00D7', 'gu'), '\u2715')
.replace(new RegExp('\u221A', 'gu'), '\u2713');
};
// Certain environments (like CITGM and GH Actions) do not come with mercurial installed
let hgIsInstalled: boolean | null = null;
export const testIfHg = (...args: Parameters<typeof test>) => {
if (hgIsInstalled === null) {
hgIsInstalled = which.sync('hg', {nothrow: true}) !== null;
}
if (hgIsInstalled) {
test(...args);
} else {
console.warn('Mercurial (hg) is not installed - skipping some tests');
test.skip(...args);
}
};
// Certain environments (like CITGM and GH Actions) do not come with sapling installed
let slIsInstalled: boolean | null = null;
export const testIfSl = (...args: Parameters<typeof test>) => {
if (slIsInstalled === null) {
slIsInstalled = which.sync('sl', {nothrow: true}) !== null;
}
if (slIsInstalled) {
test(...args);
} else {
console.warn('Sapling (sl) is not installed - skipping some tests');
test.skip(...args);
}
};
export const testIfSlAndHg = (...args: Parameters<typeof test>) => {
if (slIsInstalled === null) {
slIsInstalled = which.sync('sl', {nothrow: true}) !== null;
}
if (hgIsInstalled === null) {
hgIsInstalled = which.sync('hg', {nothrow: true}) !== null;
}
if (slIsInstalled && hgIsInstalled) {
test(...args);
} else {
console.warn(
'Sapling (sl) or Mercurial (hg) is not installed - skipping some tests',
);
test.skip(...args);
}
};