/
index.ts
1018 lines (885 loc) 路 27.6 KB
/
index.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* eslint-env jest */
import * as babel from "@babel/core";
import {
buildExternalHelpers,
type InputOptions,
type FileResult,
} from "@babel/core";
import {
default as getFixtures,
resolveOptionPluginOrPreset,
readFile,
type Test,
type TestFile,
type TaskOptions,
} from "@babel/helper-fixtures";
import { codeFrameColumns } from "@babel/code-frame";
import * as helpers from "./helpers.ts";
import visualizeSourceMap from "./source-map-visualizer.ts";
import assert from "assert";
import fs, { readFileSync, realpathSync } from "fs";
import path from "path";
import vm from "vm";
import LruCache from "lru-cache";
import { fileURLToPath } from "url";
import { diff } from "jest-diff";
import type { ChildProcess } from "child_process";
import { spawn } from "child_process";
import os from "os";
import { sync as makeDir } from "make-dir";
import readdir from "fs-readdir-recursive";
import { createRequire } from "module";
const require = createRequire(import.meta.url);
const dirname = path.dirname(fileURLToPath(import.meta.url));
import checkDuplicateNodes from "@babel/helper-check-duplicate-nodes";
import { createHash } from "crypto";
type Module = {
id: string;
exports: Record<string, unknown>;
};
if (!process.env.BABEL_8_BREAKING) {
// Introduced in Node.js 10
if (!assert.rejects) {
assert.rejects = async function (block, validateError) {
try {
await (typeof block === "function" ? block() : block);
return Promise.reject(new Error("Promise not rejected"));
} catch (error) {
// @ts-expect-error Fixme: validateError can be a string | object
// see https://nodejs.org/api/assert.html#assertrejectsasyncfn-error-message
if (typeof validateError === "function" && !validateError(error)) {
return Promise.reject(
new Error("Promise rejected with invalid error"),
);
}
}
};
}
}
const EXTERNAL_HELPERS_VERSION = "7.100.0";
const cachedScripts = new LruCache<
string,
{ code: string; cachedData?: Buffer }
>({ max: 10 });
const contextModuleCache = new WeakMap();
const sharedTestContext = createTestContext();
// We never want our tests to accidentally load the root
// babel.config.js file, so we disable config loading by
// default. Tests can still set `configFile: true | string`
// to re-enable config loading.
function transformWithoutConfigFile(code: string, opts: InputOptions) {
return babel.transformSync(code, {
configFile: false,
babelrc: false,
...opts,
});
}
function transformAsyncWithoutConfigFile(code: string, opts: InputOptions) {
return babel.transformAsync(code, {
configFile: false,
babelrc: false,
...opts,
});
}
export function createTestContext() {
const context = vm.createContext({
...helpers,
process: process,
transform: transformWithoutConfigFile,
transformAsync: transformAsyncWithoutConfigFile,
setTimeout: setTimeout,
setImmediate: setImmediate,
expect,
});
context.global = context;
const moduleCache = Object.create(null);
contextModuleCache.set(context, moduleCache);
// Populate the "babelHelpers" global with Babel's helper utilities.
runCacheableScriptInTestContext(
path.join(
path.dirname(fileURLToPath(import.meta.url)),
"babel-helpers-in-memory.js",
),
buildExternalHelpers,
context,
moduleCache,
);
return context;
}
function runCacheableScriptInTestContext(
filename: string,
srcFn: () => string,
context: vm.Context,
moduleCache: any,
): Module {
let cached = cachedScripts.get(filename);
if (!cached) {
const code = `(function (exports, require, module, __filename, __dirname) {\n${srcFn()}\n});`;
cached = {
code,
cachedData: undefined,
};
cachedScripts.set(filename, cached);
}
let script: vm.Script;
if (process.env.BABEL_8_BREAKING) {
script = new vm.Script(cached.code, {
filename,
lineOffset: -1,
cachedData: cached.cachedData,
});
cached.cachedData = script.createCachedData();
} else {
script = new vm.Script(cached.code, {
filename,
lineOffset: -1,
cachedData: cached.cachedData,
produceCachedData: true,
});
if (script.cachedDataProduced) {
cached.cachedData = script.cachedData;
}
}
const module = {
id: filename,
exports: {},
};
moduleCache[filename] = module;
const req = (id: string) =>
runModuleInTestContext(id, filename, context, moduleCache);
const dirname = path.dirname(filename);
script
.runInContext(context)
.call(module.exports, module.exports, req, module, filename, dirname);
return module;
}
/**
* A basic implementation of CommonJS so we can execute `@babel/polyfill` inside our test context.
* This allows us to run our unittests
*/
function runModuleInTestContext(
id: string,
relativeFilename: string,
context: vm.Context,
moduleCache: any,
) {
const filename = require.resolve(id, {
paths: [path.dirname(relativeFilename)],
});
// Expose Node-internal modules if the tests want them. Note, this will not execute inside
// the context's global scope.
if (filename === id) return require(id);
// Modules can only evaluate once per context, so the moduleCache is a
// stronger cache guarantee than the LRU's Script cache.
if (moduleCache[filename]) return moduleCache[filename].exports;
return runCacheableScriptInTestContext(
filename,
() => fs.readFileSync(filename, "utf8"),
context,
moduleCache,
).exports;
}
/**
* Run the given snippet of code inside a CommonJS module.
*
* Exposed for unit tests, not for use as an API.
*/
export function runCodeInTestContext(
code: string,
opts: {
filename: string;
},
context = sharedTestContext,
) {
const filename = opts.filename;
const dirname = path.dirname(filename);
const moduleCache = contextModuleCache.get(context);
const req = (id: string) =>
runModuleInTestContext(id, filename, context, moduleCache);
const module: Module = {
id: filename,
exports: {},
};
const oldCwd = process.cwd();
try {
if (opts.filename) process.chdir(path.dirname(opts.filename));
// Expose the test options as "opts", but otherwise run the test in a CommonJS-like environment.
// Note: This isn't doing .call(module.exports, ...) because some of our tests currently
// rely on 'this === global'.
const src = `(function(exports, require, module, __filename, __dirname, opts) {\n${code}\n});`;
return vm.runInContext(src, context, {
filename,
displayErrors: true,
lineOffset: -1,
})(module.exports, req, module, filename, dirname, opts);
} finally {
process.chdir(oldCwd);
}
}
async function maybeMockConsole<R>(
validateLogs: boolean,
run: () => Promise<R>,
) {
const actualLogs = { stdout: "", stderr: "" };
if (!validateLogs) return { result: await run(), actualLogs };
const spy1 = jest.spyOn(console, "log").mockImplementation(msg => {
actualLogs.stdout += `${msg}\n`;
});
const spy2 = jest.spyOn(console, "warn").mockImplementation(msg => {
actualLogs.stderr += `${msg}\n`;
});
try {
return { result: await run(), actualLogs };
} finally {
spy1.mockRestore();
spy2.mockRestore();
}
}
async function run(task: Test) {
const {
actual,
expect: expected,
exec,
options: opts,
doNotSetSourceType,
optionsDir,
validateLogs,
ignoreOutput,
stdout,
stderr,
} = task;
// todo(flow->ts) add proper return type (added any, because empty object is inferred)
function getOpts(self: TestFile): any {
const newOpts = {
ast: true,
cwd: path.dirname(self.loc),
filename: self.loc,
filenameRelative: self.filename,
sourceFileName: self.filename,
...(doNotSetSourceType ? {} : { sourceType: "script" }),
babelrc: false,
configFile: false,
inputSourceMap: task.inputSourceMap || undefined,
...opts,
};
return resolveOptionPluginOrPreset(newOpts, optionsDir);
}
let execCode = exec.code;
let result: FileResult;
let resultExec;
let execErr: Error;
if (execCode) {
const context = createTestContext();
const execOpts = getOpts(exec);
// Ignore Babel logs of exec.js files.
// They will be validated in input/output files.
({ result } = await maybeMockConsole(validateLogs, () =>
babel.transformAsync(execCode, execOpts),
));
checkDuplicateNodes(result.ast);
execCode = result.code;
try {
resultExec = runCodeInTestContext(execCode, execOpts, context);
} catch (err) {
// Pass empty location to include the whole file in the output.
if (typeof err === "object" && err.message) {
err.message =
`${exec.loc}: ${err.message}\n` +
codeFrameColumns(execCode, {} as any);
}
execErr = err;
}
}
const inputCode = actual.code;
const expectedCode = expected.code;
if (!execCode || inputCode) {
let actualLogs;
({ result, actualLogs } = await maybeMockConsole(validateLogs, () =>
babel.transformAsync(inputCode, getOpts(actual)),
));
const outputCode = normalizeOutput(result.code);
checkDuplicateNodes(result.ast);
if (!ignoreOutput) {
if (
!expected.code &&
outputCode &&
!opts.throws &&
fs.statSync(path.dirname(expected.loc)).isDirectory() &&
!process.env.CI
) {
const expectedFile = expected.loc.replace(
/\.m?js$/,
result.sourceType === "module" ? ".mjs" : ".js",
);
console.log(`New test file created: ${expectedFile}`);
fs.writeFileSync(expectedFile, `${outputCode}\n`);
if (expected.loc !== expectedFile) {
try {
fs.unlinkSync(expected.loc);
} catch (e) {}
}
} else {
validateFile(outputCode, expected.loc, expectedCode);
if (inputCode) {
expect(expected.loc).toMatch(
result.sourceType === "module" ? /\.mjs$/ : /\.js$/,
);
}
}
}
if (validateLogs) {
const normalizationOpts = {
normalizePathSeparator: true,
normalizePresetEnvDebug: task.taskDir.includes("babel-preset-env"),
};
validateFile(
normalizeOutput(actualLogs.stdout, normalizationOpts),
stdout.loc,
process.env.BABEL_8_BREAKING
? // In Babel 8, preset-env does not enable all the unnecessary syntax
// plugins. For simplicity, just strip them fro the expected output
// so that we do not need to separate tests for every fixture.
stdout.code.replace(
/\n\s*syntax-(?!import-attributes|import-assertions).*/g,
"",
)
: stdout.code,
);
validateFile(
normalizeOutput(actualLogs.stderr, normalizationOpts),
stderr.loc,
stderr.code,
);
}
}
if (execErr) {
throw execErr;
}
if (task.validateSourceMapVisual === true) {
const visual = visualizeSourceMap(result.code, result.map);
try {
expect(visual).toEqual(task.sourceMapVisual.code);
} catch (e) {
if (!process.env.OVERWRITE && task.sourceMapVisual.code) throw e;
console.log(`Updated test file: ${task.sourceMapVisual.loc}`);
fs.writeFileSync(
task.sourceMapVisual.loc ?? task.taskDir + "/source-map-visual.txt",
visual + "\n",
);
}
}
if (opts.sourceMaps === true) {
try {
expect(result.map).toEqual(task.sourceMap);
} catch (e) {
if (!process.env.OVERWRITE && task.sourceMap) throw e;
console.log(`Updated test file: ${task.sourceMapFile.loc}`);
fs.writeFileSync(
task.sourceMapFile.loc,
JSON.stringify(result.map, null, 2),
);
}
}
if (execCode && resultExec) {
return resultExec;
}
}
function validateFile(
actualCode: string,
expectedLoc: string,
expectedCode: string,
) {
if (actualCode !== expectedCode) {
if (process.env.OVERWRITE) {
console.log(`Updated test file: ${expectedLoc}`);
fs.writeFileSync(expectedLoc, `${actualCode}\n`);
return;
}
throw new Error(
`Expected ${expectedLoc} to match transform output.\n` +
`To autogenerate a passing version of this file, delete ` +
` the file and re-run the tests.\n\n` +
`Diff:\n\n${diff(expectedCode, actualCode, { expand: false })}`,
);
}
}
function escapeRegExp(string: string) {
return string.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&");
}
function normalizeOutput(
code: string,
{ normalizePathSeparator = false, normalizePresetEnvDebug = false } = {},
) {
const projectRoot = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
"../../../",
);
const cwdSymbol = "<CWD>";
let result = code
.trim()
// (non-win32) /foo/babel/packages -> <CWD>/packages
// (win32) C:\foo\babel\packages -> <CWD>\packages
.replace(new RegExp(escapeRegExp(projectRoot), "g"), cwdSymbol);
if (process.platform === "win32") {
result = result
// C:/foo/babel/packages -> <CWD>/packages
.replace(
new RegExp(escapeRegExp(projectRoot.replace(/\\/g, "/")), "g"),
cwdSymbol,
)
// C:\\foo\\babel\\packages -> <CWD>\\packages (in js string literal)
.replace(
new RegExp(escapeRegExp(projectRoot.replace(/\\/g, "\\\\")), "g"),
cwdSymbol,
);
if (normalizePathSeparator) {
result = result.replace(/<CWD>[\w\\/.-]+/g, path =>
path.replace(/\\\\?/g, "/"),
);
}
}
if (!process.env.BABEL_8_BREAKING) {
// In Babel 8, preset-env logs transform- instead of proposal-. Manually rewrite
// the output logs so that we don't have to duplicate all the debug fixtures for
// the two different Babel versions.
if (normalizePresetEnvDebug) {
result = result.replace(/(\s+)proposal-/gm, "$1transform-");
}
// For some reasons, in older Node.js versions some symlinks are not properly
// resolved. The behavior is still ok, but we need to unify the output with
// newer Node.js versions.
if (parseInt(process.versions.node, 10) <= 8) {
result = result.replace(
/<CWD>\/node_modules\/@babel\/runtime-corejs3/g,
"<CWD>/packages/babel-runtime-corejs3",
);
}
}
return result;
}
export type SuiteOptions = {
ignoreSuites?: string[];
ignoreTasks?: string[];
};
export default function (
fixturesLoc: string,
name: string,
suiteOpts: SuiteOptions = {},
taskOpts: TaskOptions = {},
dynamicOpts?: (options: TaskOptions, task: Test) => void,
) {
const suites = getFixtures(fixturesLoc);
for (const testSuite of suites) {
if (suiteOpts.ignoreSuites?.includes(testSuite.title)) continue;
describe(name + "/" + testSuite.title, function () {
for (const task of testSuite.tests) {
if (
suiteOpts.ignoreTasks?.includes(task.title) ||
suiteOpts.ignoreTasks?.includes(testSuite.title + "/" + task.title)
) {
continue;
}
const testFn = task.disabled ? it.skip : it;
testFn(
task.title,
async function () {
const runTask = () => run(task);
if ("sourceMap" in task.options) {
throw new Error(
"`sourceMap` option is deprecated. Use `sourceMaps` instead.",
);
}
if ("sourceMaps" in task.options === false) {
task.options.sourceMaps = !!task.sourceMap;
}
Object.assign(task.options, taskOpts);
if (dynamicOpts) dynamicOpts(task.options, task);
if (task.externalHelpers) {
(task.options.plugins ??= [])
// @ts-expect-error manipulating input options
.push([
"external-helpers",
{ helperVersion: EXTERNAL_HELPERS_VERSION },
]);
}
const throwMsg = task.options.throws;
if (throwMsg) {
// internal api doesn't have this option but it's best not to pollute
// the options object with useless options
delete task.options.throws;
await assert.rejects(runTask, function (err: Error) {
assert.ok(
throwMsg === true || err.message.includes(throwMsg),
`
Expected Error: ${throwMsg}
Actual Error: ${err.message}`,
);
return true;
});
} else {
return runTask();
}
},
);
}
});
}
}
export type ProcessTestOpts = {
args: string[];
executor?: string;
ipc?: boolean;
ipcMessage?: string;
stdout?: string;
stderr?: string;
stdin?: string;
stdoutPath?: string;
stderrPath?: string;
stdoutContains?: boolean;
stderrContains?: boolean;
testLoc?: string;
outFiles?: Record<string, string>;
inFiles?: Record<string, string>;
noBabelrc?: boolean;
minNodeVersion?: number;
flaky?: boolean;
env?: Record<string, string>;
BABEL_8_BREAKING?: boolean;
};
export type ProcessTest = {
suiteName: string;
testName: string;
skip: boolean;
fn: Function;
opts: ProcessTestOpts;
binLoc?: string;
};
export type ProcessTestBeforeHook = (test: ProcessTest, tmpDir: string) => void;
export type ProcessTestAfterHook = (
test: ProcessTest,
tmpDir: string,
stdout: string,
stderr: string,
) => {
stdout: string;
stderr: string;
};
const nodeGte8 = parseInt(process.versions.node, 10) >= 8;
// https://github.com/nodejs/node/issues/11422#issue-208189446
const tmpDir = realpathSync(os.tmpdir());
const readDir = function (loc: string, filter: Parameters<typeof readdir>[1]) {
const files: Record<string, string> = {};
if (fs.existsSync(loc)) {
readdir(loc, filter).forEach(function (filename) {
files[filename] = readFile(path.join(loc, filename));
});
}
return files;
};
const outputFileSync = function (filePath: string, data: string) {
makeDir(path.dirname(filePath));
fs.writeFileSync(filePath, data);
};
function deleteDir(path: string): void {
if (fs.existsSync(path)) {
fs.readdirSync(path).forEach(function (file) {
const curPath = path + "/" + file;
if (fs.lstatSync(curPath).isDirectory()) {
// recurse
deleteDir(curPath);
} else {
// delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
}
const fileFilter = function (x: string) {
return x !== ".DS_Store";
};
const assertTest = function (
stdout: string,
stderr: string,
ipcMessage: unknown,
opts: ProcessTestOpts,
tmpDir: string,
) {
const expectStderr = opts.stderr.trim();
stderr = stderr.trim();
try {
if (opts.stderr) {
if (opts.stderrContains) {
expect(stderr).toContain(expectStderr);
} else {
expect(stderr).toBe(expectStderr);
}
} else if (stderr) {
throw new Error("stderr:\n" + stderr);
}
} catch (e) {
if (!process.env.OVERWRITE) throw e;
console.log(`Updated test file: ${opts.stderrPath}`);
outputFileSync(opts.stderrPath, stderr + "\n");
}
const expectStdout = opts.stdout.trim();
stdout = stdout.trim();
stdout = stdout.replace(/\\/g, "/");
try {
if (opts.stdout) {
if (opts.stdoutContains) {
expect(stdout).toContain(expectStdout);
} else {
expect(stdout).toBe(expectStdout);
}
} else if (stdout) {
throw new Error("stdout:\n" + stdout);
}
} catch (e) {
console.log(JSON.stringify(opts.stdout), JSON.stringify(stdout));
if (!process.env.OVERWRITE) throw e;
console.log(`Updated test file: ${opts.stdoutPath}`);
outputFileSync(opts.stdoutPath, stdout + "\n");
}
if (opts.ipc) {
expect(ipcMessage).toEqual(opts.ipcMessage);
}
if (opts.outFiles) {
const actualFiles = readDir(tmpDir, fileFilter);
Object.keys(actualFiles).forEach(function (filename) {
try {
if (
// saveInFiles always creates an empty .babelrc, so lets exclude for now
filename !== ".babelrc" &&
filename !== ".babelignore" &&
!Object.hasOwn(opts.inFiles, filename)
) {
const expected = opts.outFiles[filename];
const actual = actualFiles[filename];
expect(actual).toBe(expected || "");
}
} catch (e) {
if (!process.env.OVERWRITE) {
e.message += "\n at " + filename;
throw e;
}
const expectedLoc = path.join(opts.testLoc, "out-files", filename);
console.log(`Updated test file: ${expectedLoc}`);
outputFileSync(expectedLoc, actualFiles[filename]);
}
});
Object.keys(opts.outFiles).forEach(function (filename) {
expect(actualFiles).toHaveProperty([filename]);
});
}
};
export function buildParallelProcessTests(name: string, tests: ProcessTest[]) {
return function (curr: number, total: number) {
const sliceLength = Math.ceil(tests.length / total);
const sliceStart = curr * sliceLength;
const sliceEnd = sliceStart + sliceLength;
const testsSlice = tests.slice(sliceStart, sliceEnd);
describe(`${name} [${curr}/${total}]`, function () {
it("dummy", () => {});
for (const test of testsSlice) {
(test.skip ? it.skip : it)(
test.suiteName + " " + test.testName,
test.fn as any,
);
}
});
};
}
export function buildProcessTests(
dir: string,
beforeHook: ProcessTestBeforeHook,
afterHook?: ProcessTestAfterHook,
) {
const tests: ProcessTest[] = [];
fs.readdirSync(dir).forEach(function (suiteName) {
if (suiteName.startsWith(".") || suiteName === "package.json") return;
const suiteLoc = path.join(dir, suiteName);
fs.readdirSync(suiteLoc).forEach(function (testName) {
if (testName.startsWith(".")) return;
const testLoc = path.join(suiteLoc, testName);
let opts: ProcessTestOpts = {
args: [],
};
const optionsLoc = path.join(testLoc, "options.json");
if (fs.existsSync(optionsLoc)) {
const taskOpts = JSON.parse(readFileSync(optionsLoc, "utf8"));
if (taskOpts.os) {
let os = taskOpts.os;
if (!Array.isArray(os) && typeof os !== "string") {
throw new Error(
`'os' should be either string or string array: ${taskOpts.os}`,
);
}
if (typeof os === "string") {
os = [os];
}
if (!os.includes(process.platform)) {
return;
}
delete taskOpts.os;
}
opts = { args: [], ...taskOpts };
}
const executorLoc = path.join(testLoc, "executor.js");
if (fs.existsSync(executorLoc)) {
opts.executor = executorLoc;
}
opts.stderrPath = path.join(testLoc, "stderr.txt");
opts.stdoutPath = path.join(testLoc, "stdout.txt");
for (const key of ["stdout", "stdin", "stderr"] as const) {
const loc = path.join(testLoc, key + ".txt");
if (fs.existsSync(loc)) {
opts[key] = readFile(loc);
} else {
opts[key] = opts[key] || "";
}
}
opts.testLoc = testLoc;
opts.outFiles = readDir(path.join(testLoc, "out-files"), fileFilter);
opts.inFiles = readDir(path.join(testLoc, "in-files"), fileFilter);
const babelrcLoc = path.join(testLoc, ".babelrc");
const babelIgnoreLoc = path.join(testLoc, ".babelignore");
if (fs.existsSync(babelrcLoc)) {
// copy .babelrc file to tmp directory
opts.inFiles[".babelrc"] = readFile(babelrcLoc);
} else if (!opts.noBabelrc) {
opts.inFiles[".babelrc"] = "{}";
}
if (fs.existsSync(babelIgnoreLoc)) {
// copy .babelignore file to tmp directory
opts.inFiles[".babelignore"] = readFile(babelIgnoreLoc);
}
const skip =
(opts.minNodeVersion &&
parseInt(process.versions.node, 10) < opts.minNodeVersion) ||
(opts.flaky && !process.env.BABEL_CLI_FLAKY_TESTS) ||
opts.BABEL_8_BREAKING === false;
if (opts.flaky) {
testName += " (flaky)";
}
const test: ProcessTest = {
suiteName,
testName,
skip,
opts,
fn: function (callback: Function) {
const tmpLoc = path.join(
tmpDir,
"babel-process-test",
createHash("sha1").update(testLoc).digest("hex"),
);
deleteDir(tmpLoc);
makeDir(tmpLoc);
const { inFiles } = opts;
for (const filename of Object.keys(inFiles)) {
outputFileSync(path.join(tmpLoc, filename), inFiles[filename]);
}
try {
beforeHook(test, tmpLoc);
if (test.binLoc === undefined) {
throw new Error("test.binLoc is undefined");
}
let args =
opts.executor && nodeGte8
? [
"--require",
path.join(dirname, "./exit-loader.cjs"),
test.binLoc,
]
: [test.binLoc];
args = args.concat(opts.args);
const env = { ...process.env, FORCE_COLOR: "false", ...opts.env };
const child = spawn(process.execPath, args, {
env,
cwd: tmpLoc,
stdio:
(opts.executor && nodeGte8) || opts.ipc
? ["pipe", "pipe", "pipe", "ipc"]
: "pipe",
});
let stderr = "";
let stdout = "";
let ipcMessage: unknown;
child.on("close", function () {
let err;
try {
const result = afterHook
? afterHook(test, tmpLoc, stdout, stderr)
: { stdout, stderr };
assertTest(
result.stdout,
result.stderr,
ipcMessage,
opts,
tmpLoc,
);
} catch (e) {
err = e;
} finally {
try {
deleteDir(tmpLoc);
} catch (error) {
console.error(error);
}
}
if (err) {
err.message =
args.map(arg => `"${arg}"`).join(" ") + ": " + err.message;
}
callback(err);
});
if (opts.ipc) {
child.on("message", function (message) {
ipcMessage = message;
});
}
if (opts.stdin) {
child.stdin.write(opts.stdin);
child.stdin.end();
}
const captureOutput = (proc: ChildProcess) => {
proc.stderr.on("data", function (chunk) {
stderr += chunk;
});
proc.stdout.on("data", function (chunk) {
stdout += chunk;
});
};
if (opts.executor) {
const executor = spawn(process.execPath, [opts.executor], {
cwd: tmpLoc,
});
child.stdout.pipe(executor.stdin);
child.stderr.pipe(executor.stdin);
executor.on("close", function () {
if (nodeGte8) {
child.send("exit");
} else {
child.kill("SIGKILL");
}
});
captureOutput(executor);
} else {
captureOutput(child);
}