Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
378 lines (327 loc) · 11.7 KB
/
Copy pathindex.js
File metadata and controls
378 lines (327 loc) · 11.7 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
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
'use strict';
var callBound = require('call-bound');
var defined = require('defined');
var through = require('@ljharb/through');
var createDefaultStream = require('./lib/default_stream');
var Test = require('./lib/test');
var Results = require('./lib/results');
var $indexOf = callBound('Array.prototype.indexOf');
var canEmitExit = typeof process !== 'undefined'
&& process
&& typeof process.on === 'function'
&& /** @type {{ browser?: boolean }} */ (process).browser !== true;
var canExit = typeof process !== 'undefined'
&& process
&& typeof process.exit === 'function';
/** @typedef {{ getActiveResourcesInfo?: () => string[] }} ResourcesProcess */
var $getResources = canEmitExit
? /** @type {ResourcesProcess} */ (process).getActiveResourcesInfo
: undefined;
// timers, immediates, sockets, fs requests, etc, that keep the event loop alive, ignoring writes, which tape's own output makes.
// node < 16.14 can not tell us, so there, work scheduled by `beforeExit` listeners is only noticed once it asserts or ends the test
function countPendingWork() {
if (typeof $getResources !== 'function') {
return 0;
}
var resources = $getResources();
var count = 0;
for (var i = 0; i < resources.length; i++) {
if (resources[i] !== 'WriteWrap' && resources[i] !== 'ShutdownWrap') {
count += 1;
}
}
return count;
}
/**
* @import Tape, {
* createHarness as CreateHarness,
* CreateStream,
* Harness,
* HarnessConfig,
* HarnessEventHandler,
* TestOptions,
* } from '.'
*/
/** @import { Result } from './lib/results' */
var tape = (function () {
var wait = false;
/** @type {undefined | Harness} */
var harness;
/** @type {(opts?: HarnessConfig) => Harness} */
function getHarness(opts) {
// this override is here since tests fail via nyc if createHarness is moved upwards
if (!harness) {
// eslint-disable-next-line no-use-before-define
harness = createExitHarness(opts || {}, wait);
}
return harness;
}
/** @type {(this: Harness, ...args: Parameters<typeof Tape>) => ReturnType<typeof Tape>} */
function lazyLoad() {
// eslint-disable-next-line no-invalid-this
return getHarness().apply(this, arguments);
}
lazyLoad.wait = function () {
wait = true;
};
lazyLoad.run = function () {
var run = getHarness().run;
if (run) { run(); }
};
lazyLoad.only = function () {
return getHarness().only.apply(this, arguments);
};
/** @type {typeof Test.skip} */
lazyLoad.skip = function () {
return getHarness().skip.apply(this, arguments);
};
/** @type {CreateStream} */
lazyLoad.createStream = function (opts) {
var options = opts || {};
if (!harness) {
var output = through();
getHarness({ stream: output, objectMode: options.objectMode });
return output;
}
return harness.createStream(options);
};
/** @type {typeof Tape.onFinish} */
lazyLoad.onFinish = function () {
return getHarness().onFinish.apply(this, arguments);
};
/** @type {typeof Tape.onFailure} */
lazyLoad.onFailure = function () {
return getHarness().onFailure.apply(this, arguments);
};
lazyLoad.getHarness = getHarness;
return lazyLoad;
}());
/** @type {typeof CreateHarness} */
function createHarness(conf_) {
var results = new Results({ todoIsOK: !!(process.env.TODO_IS_OK === '1') });
if (!conf_ || conf_.autoclose !== false) {
results.once('done', function () { results.close(); });
}
/** @type {(name: string, conf: TestOptions, cb: Test.TestCase) => Test} */
function test(name, conf, cb) {
// eslint-disable-next-line no-use-before-define
return register(new Test(name, conf, cb));
}
/** @type {(t: Test) => Test} */
function register(t) {
test._tests.push(t);
(function inspectCode(st) {
st.on('test', /** @param {Test} st_ */ function sub(st_) {
inspectCode(st_);
});
st.on('result', /** @param {Result} r */ function (r) {
if (!r.todo && !r.ok && typeof r !== 'string') { test._exitCode = 1; }
});
}(t));
results.push(t);
return t;
}
test._results = results;
/** @type {typeof Test.skip} */
test.skip = function () {
return register(Test.skip.apply(null, arguments));
};
/** @type {Test[]} */ test._tests = [];
/** @type {CreateStream} */
test.createStream = function (opts) {
return results.createStream(opts);
};
/** @type {HarnessEventHandler} */
test.onFinish = function (cb) {
results.on('done', cb);
};
/** @type {HarnessEventHandler} */
test.onFailure = function (cb) {
results.on('fail', cb);
};
var only = false;
/** @returns {Test} */
test.only = function () {
if (only) { throw new Error('there can only be one only test'); }
if (conf_ && conf_.noOnly) { throw new Error('`only` tests are prohibited'); }
only = true;
var t = /** @type {Test} */ (test.apply(null, arguments));
results.only(t);
return t;
};
test._exitCode = 0;
test.close = function () { results.close(); };
// @ts-expect-error TODO FIXME: why is `test` not assignable to `Harness`???
return test;
}
/** @type {(conf: Omit<HarnessConfig, 'autoclose'>, wait?: boolean) => Harness} */
function createExitHarness(config, wait) {
var noOnly = config.noOnly;
var objectMode = config.objectMode;
var cStream = config.stream;
var exit = config.exit;
var harness = createHarness({
autoclose: !canEmitExit,
noOnly: defined(noOnly, defined(process.env.NODE_TAPE_NO_ONLY_TEST, false))
});
var running = false;
var ended = false;
function run() {
if (running) { return; }
running = true;
var stream = harness.createStream({ objectMode: objectMode });
var es = stream.pipe(/** @type {NodeJS.WritableStream} */ (cStream || createDefaultStream()));
if (canEmitExit && es) { // in node v0.4, `es` is `undefined`
// eslint-disable-next-line no-unused-vars
es.on('error', function (_) { harness._exitCode = 1; });
}
stream.on('end', function () { ended = true; });
}
if (wait) {
harness.run = run;
} else {
run();
}
if (exit === false) { return harness; }
if (!canEmitExit || !canExit) { return harness; }
// tests (and subtests) that have started but not ended yet, each with its ancestors; the last one is the innermost
/** @type {{ test: Test, ancestors: Test[] }[]} */
var started = [];
/** @param {Test} t */
function isStarted(t) {
for (var i = 0; i < started.length; i++) {
if (started[i].test === t) { return true; }
}
return false;
}
/** @type {(t: Test, ancestors: Test[]) => void} */
function track(t, ancestors) {
t.once('prerun', function () {
// a subtest whose parent has already ended (eg, the subtest called `.end()` on its parent) is orphaned: nothing waits on it
if (ancestors.length === 0 || isStarted(ancestors[ancestors.length - 1])) {
started[started.length] = { test: t, ancestors: ancestors };
}
});
t.once('end', function () {
var stillRunning = [];
for (var i = 0; i < started.length; i++) {
if (started[i].test !== t && $indexOf(started[i].ancestors, t) < 0) {
stillRunning[stillRunning.length] = started[i];
}
}
started = stillRunning;
});
t.on('test', /** @param {Test} st */ function (st) {
track(st, ancestors.concat(t));
});
}
harness._results.on('_push', /** @param {Test} t */ function (t) {
track(t, []);
});
// node emits `beforeExit` when the event loop is empty. other `beforeExit` listeners (user code, a library, or the test
// itself) may still do or schedule work that finishes the running test. so, once every listener has run, if they scheduled
// no work, we note the innermost running test and its assertion count, and keep the loop alive for one more turn. if the
// loop empties again with the same test, no new assertions before the listeners ran, and the listeners neither ended it nor
// scheduled work, it has stalled: fail it and end it, so that the tests after it still run. assertions the listeners make
// do not count, so a listener that asserts every time can not keep the process alive forever; in node < 6, which can not
// run our listener first, the round limit does that.
/** @type {Test | undefined} */
var stallCandidate;
var candidateCount = 0;
var roundsKeptAlive = 0;
var maxRoundsKeptAlive = 10;
function innermost() {
var entry = started[started.length - 1];
return entry && entry.test;
}
function keepAlive() {}
/** @type {Test | undefined} */
var testBeforeListeners;
var countBeforeListeners = 0;
var workBeforeListeners = 0;
function checkForStall() {
var t = ended ? undefined : innermost();
if (!t || countPendingWork() > workBeforeListeners) {
stallCandidate = undefined;
return;
}
if (
t === testBeforeListeners
&& t === stallCandidate
&& (countBeforeListeners === candidateCount || roundsKeptAlive >= maxRoundsKeptAlive)
) {
if (!t._stalled) {
t._stalled = true;
t._exit();
}
// with a plan, `_exit()` can start a queued subtest; then `t` ends when its subtests do
if (!t.ended && innermost() === t) {
// a test that already called `.end()` is stuck on a teardown that never settles; skip past it
if (t.calledEnd) {
t._end();
} else {
t.end();
}
}
// ending `t` can start the next test synchronously, and it may stall too
t = ended ? undefined : innermost();
if (!t) {
stallCandidate = undefined;
return;
}
}
roundsKeptAlive = t === stallCandidate ? roundsKeptAlive + 1 : 1;
stallCandidate = t;
candidateCount = t.assertCount;
setImmediate(keepAlive);
}
// run before every other `beforeExit` listener, so that the assertions they make count as theirs
/** @type {'prependListener' | 'on'} */
var addListener = typeof process.prependListener === 'function' ? 'prependListener' : 'on';
process[addListener]('beforeExit', function () {
testBeforeListeners = ended ? undefined : innermost();
countBeforeListeners = testBeforeListeners ? testBeforeListeners.assertCount : 0;
workBeforeListeners = countPendingWork();
// `nextTick` callbacks run after every `beforeExit` listener has
process.nextTick(checkForStall);
});
process.on('exit', /** @param {number | undefined} code */ function (code) {
// let the process exit cleanly.
if (typeof code === 'number' && code !== 0) {
return;
}
if (!ended) {
var only = harness._results._only;
for (var i = 0; i < harness._tests.length; i++) {
var t = harness._tests[i];
if (!only || t === only) {
t._exit();
}
}
}
harness.close();
process.removeAllListeners('exit'); // necessary for node v0.6
process.exit(code || harness._exitCode); // eslint-disable-line no-process-exit
});
return harness;
}
// `cjs-module-lexer` - used by node's ESM interop and by `attw` - only detects named exports
// from literal `module.exports.x =` statements, so attaching these to `tape` before the export
// assignment, as TS 7 wants, would break `import { test } from 'tape'`. see microsoft/typescript-go#1685.
// @ts-ignore TODO FIXME TS2309
module.exports = tape;
// @ts-ignore TODO FIXME TS2551: attw errors without this line
module.exports.createHarness = createHarness;
// @ts-ignore TODO FIXME TS2339: attw errors without this line
module.exports.Test = Test;
// @ts-ignore TODO FIXME TS2339: attw errors without this line
module.exports.test = tape; // tap compat
// the casts are load-bearing: without them TS 6 reports TS2303, a circular import alias
module.exports.createStream = /** @type {typeof Tape.createStream} */ (tape.createStream);
module.exports.only = /** @type {() => any} */ (tape.only);
module.exports.skip = /** @type {typeof Test.skip} */ (tape.skip);
module.exports.getHarness = /** @type {typeof Tape.getHarness} */ (tape.getHarness);
module.exports.run = /** @type {typeof Tape.run} */ (tape.run);
module.exports.wait = /** @type {typeof Tape.wait} */ (tape.wait);
module.exports.onFinish = /** @type {typeof Tape.onFinish} */ (tape.onFinish);
module.exports.onFailure = /** @type {typeof Tape.onFailure} */ (tape.onFailure);