-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest262.js
More file actions
320 lines (289 loc) · 11.2 KB
/
Copy pathtest262.js
File metadata and controls
320 lines (289 loc) · 11.2 KB
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
'use strict';
// Runs the tc39/test262 Iterator suite (the `test262` git submodule) against
// this package's shim. The shim is browserified into self-contained source and
// prepended to each compiled test via a `--preprocessor`. Two reasons it must be
// browserified source rather than a `require`: test262-harness runs each test in
// its own `vm` realm (so the shim must execute inside the realm, not the
// runner's); and `--prelude` content is concatenated *after* the harness
// includes, but some includes (e.g. wellKnownIntrinsicObjects.js) derive
// intrinsics from the global `Iterator` at load time -- before a prelude would
// install it. The preprocessor prepends the shim to the very top instead, so it
// is installed before anything reads from it.
//
// Run across a Node version matrix: on a version without native helpers the shim
// provides everything (exercising this package's own code); on current LTS the
// ES2025 helpers are native passthrough and only zip/zipKeyed exercise our impl.
//
// `node test262.js [suite]` runs a suite (default `main`). `main` covers the
// tests merged into the pinned submodule; the PR suites cover features whose
// test262 tests are still in open PRs, by fetching the PR's test ref and running
// just its files. A PR suite that *adds* files auto-skips once they exist in the
// pinned submodule (merged + bumped); `main`'s `features` then covers them.
var path = require('path');
var fs = require('fs');
var os = require('os');
var spawnSync = require('child_process').spawnSync;
var browserify = require('browserify');
var root = __dirname;
var autoPath = path.join(root, 'auto.js');
var test262Dir = path.join(root, 'test262');
var harnessBin = require.resolve('test262-harness/bin/run.js');
// Known failures, keyed by path. A new failure fails the run; a listed test
// that starts passing is reported so it can be removed. Each reason is tagged
// `engine:` (an artifact of the native intrinsic differing from this package's
// on a native-capable engine; not a logic bug) or `polyfill:` (a real spec
// divergence tracked here rather than fixed alongside wiring up test262).
var EXPECTED_FAILURES = {
'test262/test/built-ins/Iterator/zip/result-is-iterator.js': 'engine: zip result inherits this package\'s %IteratorHelperPrototype%, which is not SameValue as the native intrinsic on a native-capable engine',
'test262/test/built-ins/Iterator/zipKeyed/result-is-iterator.js': 'engine: zipKeyed result inherits this package\'s %IteratorHelperPrototype%, which is not SameValue as the native intrinsic on a native-capable engine',
'test262/test/built-ins/Iterator/proto-from-ctor-realm.js': 'engine: cross-realm construction (Reflect.construct with a NewTarget from another realm whose .prototype is a non-object) requires the internal GetFunctionRealm to obtain that realm\'s %Iterator.prototype%, which has no userland equivalent'
};
function prototypeTests(methods, names) {
var paths = [];
methods.forEach(function (method) {
names.forEach(function (name) {
paths.push('test/built-ins/Iterator/prototype/' + method + '/' + name + '.js');
});
});
return paths;
}
// `main` runs the merged Iterator tests. The PR suites run a not-yet-merged
// feature's tests by fetching the PR ref; `main`'s `features` includes
// `iterator-includes` so that once PR #5031 merges and the submodule is bumped,
// `main` covers it and the `includes` suite auto-skips. Suites for features this
// package does not implement (chunks/windows, join) are informational
// (non-blocking).
var SUITES = {
main: {
label: 'main',
features: ['iterator-helpers', 'iterator-sequencing', 'joint-iteration', 'iterator-includes'],
paths: ['test/built-ins/Iterator/**/*.js'],
expectedFailures: EXPECTED_FAILURES,
blocking: true
},
includes: {
label: 'Iterator.prototype.includes (test262 PR #5031)',
pr: 5031,
paths: ['test/built-ins/Iterator/prototype/includes'],
blocking: true
},
'helper-edge': {
label: 'helper iterator-close edge cases (test262 PR #4496)',
pr: 4496,
paths: prototypeTests(
['drop', 'filter', 'map', 'take'],
[
'next-method-called-with-zero-arguments',
'return-method-called-with-zero-arguments',
'suspended-start-iterator-close-calls-next',
'suspended-start-iterator-close-calls-return',
'suspended-yield-iterator-close-calls-next',
'suspended-yield-iterator-close-calls-return'
]
).concat(prototypeTests(
['flatMap'],
[
'next-method-called-with-zero-arguments',
'return-method-called-with-zero-arguments',
'return-method-can-be-absent-for-inner',
'return-method-can-be-absent-for-underlying',
'suspended-start-iterator-close-calls-next',
'suspended-start-iterator-close-calls-return',
'suspended-yield-iterator-close-calls-next',
'suspended-yield-iterator-close-calls-next-from-inner',
'suspended-yield-iterator-close-calls-return',
'suspended-yield-iterator-close-calls-return-from-inner'
]
)),
blocking: true
},
chunking: {
label: 'iterator-chunking (test262 PR #5011)',
pr: 5011,
paths: ['test/built-ins/Iterator/prototype/chunks', 'test/built-ins/Iterator/prototype/windows'],
blocking: false
},
join: {
label: 'Iterator.prototype.join (test262 PR #4768)',
pr: 4768,
paths: ['test/built-ins/Iterator/prototype/join'],
blocking: false
},
'take-drop-rangeerror': {
// #5065 *modifies* existing files for the (still-unmerged) ecma262 #3776
// RangeError, which this package now implements; because it modifies rather
// than adds files, it can't auto-skip even once the submodule is bumped past it.
label: 'take/drop RangeError (test262 PR #5065)',
pr: 5065,
modifies: true,
paths: prototypeTests(
['take', 'drop'],
['argument-effect-order', 'argument-validation-failure-closes-underlying', 'limit-rangeerror']
),
blocking: true
}
};
function bundle(callback) {
browserify([autoPath]).bundle(function (err, buf) {
if (err) {
callback(err);
return;
}
var bundlePath = path.join(os.tmpdir(), 'es-iterator-helpers-test262-bundle.js');
fs.writeFileSync(bundlePath, buf);
var preprocessorPath = path.join(os.tmpdir(), 'es-iterator-helpers-test262-preprocessor.js');
fs.writeFileSync(preprocessorPath, [
'var fs = require(\'fs\');',
'var shim = fs.readFileSync(' + JSON.stringify(bundlePath) + ', \'utf8\');',
'module.exports = function prependShim(test) {',
'\ttest.contents = shim + \'\\n\' + test.contents;',
'\treturn test;',
'};',
''
].join('\n'));
callback(null, preprocessorPath);
});
}
function git(args) {
return spawnSync('git', ['-C', test262Dir].concat(args), { encoding: 'utf8' });
}
function fetchPR(suite) {
var fetched = git(['fetch', '--depth', '1', 'origin', 'refs/pull/' + suite.pr + '/head']);
if (fetched.status !== 0) {
throw new Error('failed to fetch test262 PR #' + suite.pr + ': ' + (fetched.stderr || ''));
}
var checkedOut = git(['checkout', 'FETCH_HEAD', '--'].concat(suite.paths));
if (checkedOut.status !== 0) {
throw new Error('failed to check out PR #' + suite.pr + ' tests: ' + (checkedOut.stderr || ''));
}
}
function cleanupPR(suite) {
git(['reset', '--quiet', 'HEAD', '--'].concat(suite.paths));
if (suite.modifies) {
git(['checkout', '--quiet', 'HEAD', '--'].concat(suite.paths));
} else {
suite.paths.forEach(function (p) {
fs.rmSync(path.join(test262Dir, p), { recursive: true, force: true });
});
}
}
function globFor(p) {
var abs = path.join(test262Dir, p).replace(/\\/g, '/');
return (/\.js$/).test(p) ? abs : abs + '/**/*.js';
}
function runHarness(preprocessorPath, suite) {
var threads = Math.max(1, os.cpus().length - 1);
var expectedFailures = suite.expectedFailures || {};
var args = [
harnessBin,
'--host-type',
'node',
'--host-path',
process.execPath,
'--preprocessor',
preprocessorPath,
'--test262-dir',
test262Dir,
'--reporter',
'json',
'--reporter-keys',
'file,scenario,result',
'-t',
String(threads)
];
if (suite.features) {
args.push('--features-include', suite.features.join(','));
}
args = args.concat(suite.paths.map(globFor));
var result = spawnSync(process.execPath, args, { encoding: 'utf8', maxBuffer: 256 * 1024 * 1024 });
if (!result.stdout) {
process.stderr.write(result.stderr || '');
throw new Error('test262-harness produced no output');
}
var records;
try {
records = JSON.parse(result.stdout);
} catch (e) {
process.stderr.write(result.stdout);
process.stderr.write(result.stderr || '');
throw new Error('could not parse test262-harness output', { cause: e });
}
var passed = 0;
var failingFiles = {};
var unexpectedFailures = [];
for (var i = 0; i < records.length; i += 1) {
var record = records[i];
if (record.result && record.result.pass) {
passed += 1;
} else {
failingFiles[record.file] = true;
if (!(record.file in expectedFailures)) {
unexpectedFailures.push(record.file + ' (' + record.scenario + '): ' + (record.result && record.result.message));
}
}
}
// `engine:` failures are version-dependent: they pass where the shim provides
// every helper and fail where native helpers exist, so passing is not a signal
// to remove them. Only `polyfill:` (real-bug) entries are flagged when they pass.
var unexpectedPasses = Object.keys(expectedFailures).filter(function (file) {
return !(file in failingFiles) && expectedFailures[file].indexOf('engine:') !== 0;
});
var prefix = 'test262 [' + suite.label + ']: ';
console.log(prefix + 'ran ' + records.length + ' (' + passed + ' passed, ' + (records.length - passed) + ' failed)');
if (Object.keys(expectedFailures).length > 0) {
console.log(prefix + Object.keys(expectedFailures).length + ' known failures allow-listed');
}
if (unexpectedPasses.length > 0) {
console.warn('\n' + prefix + unexpectedPasses.length + ' allow-listed test(s) now PASS; remove them from EXPECTED_FAILURES in test262.js:');
unexpectedPasses.forEach(function (file) {
console.warn(' - ' + file);
});
}
if (unexpectedFailures.length > 0) {
var log = suite.blocking ? console.error : console.warn;
log('\n' + prefix + unexpectedFailures.length + (suite.blocking ? ' UNEXPECTED failure(s):' : ' failure(s) (non-blocking; feature not implemented here):'));
unexpectedFailures.forEach(function (failure) {
log(' - ' + failure);
});
if (suite.blocking) {
process.exitCode = 1;
}
return;
}
console.log('\n' + prefix + 'no unexpected failures');
}
function run(preprocessorPath) {
if (!fs.existsSync(path.join(test262Dir, 'test'))) {
throw new Error('the `test262` submodule is not checked out; run `git submodule update --init --depth 1`');
}
var suiteName = process.argv[2] || 'main';
var suite = SUITES[suiteName];
if (!suite) {
throw new Error('unknown suite `' + suiteName + '`; known suites: ' + Object.keys(SUITES).join(', '));
}
if (!suite.pr) {
runHarness(preprocessorPath, suite);
return;
}
if (!suite.modifies) {
var alreadyMerged = suite.paths.every(function (p) {
return fs.existsSync(path.join(test262Dir, p));
});
if (alreadyMerged) {
console.log('test262 [' + suite.label + ']: tests are present in the pinned submodule; the `main` run covers them -- skipping');
return;
}
}
fetchPR(suite);
try {
runHarness(preprocessorPath, suite);
} finally {
cleanupPR(suite);
}
}
bundle(function (err, preprocessorPath) {
if (err) {
throw err;
}
run(preprocessorPath);
});