function drainAsync() {
var p = Promise.resolve();
for (var i = 0; i < 50; i++) {
p = p.then(function() { return new Promise(function(r) { setTimeout(r, 0); }); });
}
for (var j = 0; j < 25; j++) {
p = p.then(function() { return new Promise(function(r) { setTimeout(r, 100); }); });
}
for (var k = 0; k < 20; k++) {
p = p.then(function() { return new Promise(function(r) { setTimeout(r, 0); }); });
}
return p;
}
var _firstUnhandledRejection = null;
function installRejectionTracking() {
_firstUnhandledRejection = null;
function onUnhandledRejection(reason) {
if (!_firstUnhandledRejection) {
_firstUnhandledRejection = reason;
}
}
if (globalThis.process && typeof globalThis.process.on === 'function') {
globalThis.process.on('unhandledRejection', onUnhandledRejection);
}
return function restore() {
if (globalThis.process && typeof globalThis.process.removeListener === 'function') {
globalThis.process.removeListener('unhandledRejection', onUnhandledRejection);
}
var rejection = _firstUnhandledRejection;
_firstUnhandledRejection = null;
return rejection;
};
}
function parseTestFlags(testPath) {
var source;
try {
source = require('node:fs').readFileSync(testPath, 'utf8');
} catch (_) {
return [];
}
var flags = [];
var re = /^\/\/\s*Flags:\s*(.+)$/gm;
var match;
while ((match = re.exec(source)) !== null) {
var line = match[1] || '';
var parts = line.trim().split(/\s+/).filter(Boolean);
for (var i = 0; i < parts.length; i++) {
flags.push(parts[i]);
}
}
return flags;
}
function applyTestFlagsToProcess(testPath) {
if (!globalThis.process) return;
var flags = parseTestFlags(testPath);
if (!Array.isArray(globalThis.process.execArgv)) {
globalThis.process.execArgv = [];
}
globalThis.process.execArgv.length = 0;
for (var i = 0; i < flags.length; i++) {
globalThis.process.execArgv.push(flags[i]);
}
}
export const runTest = async (testPath) => {
var restorePromise = null;
var restoreArgv = null;
if (globalThis.process) {
var originalArgv = Array.isArray(globalThis.process.argv) ? globalThis.process.argv.slice() : null;
var originalArgv0 = globalThis.process.argv0;
var execPath = (typeof globalThis.process.execPath === 'string' && globalThis.process.execPath.length > 0)
? globalThis.process.execPath
: 'node';
globalThis.process.argv = [execPath, testPath];
globalThis.process.argv0 = execPath;
restoreArgv = function restoreArgv() {
if (originalArgv) {
globalThis.process.argv = originalArgv;
}
globalThis.process.argv0 = originalArgv0;
};
}
try {
applyTestFlagsToProcess(testPath);
var commonMod;
try {
commonMod = require('/home/node/test/common/index.js');
} catch(e) {}
if (commonMod && typeof commonMod._resetMustCalls === 'function') {
commonMod._resetMustCalls();
}
restorePromise = installRejectionTracking();
if (testPath.endsWith('.mjs')) {
await import(testPath);
} else {
var testRequire = require('node:module').createRequire('/');
testRequire(testPath);
}
var testModule = require('node:test');
if (testModule && typeof testModule._awaitPendingTests === 'function') {
await testModule._awaitPendingTests();
}
await drainAsync();
if (globalThis.process && typeof globalThis.process._runExitHandlers === 'function') {
globalThis.process._runExitHandlers(0);
}
var rejection = restorePromise();
restorePromise = null;
var common;
try {
common = require('/home/node/test/common/index.js');
} catch(e) {}
var mustCallErrors = [];
if (common && typeof common._checkMustCalls === 'function') {
mustCallErrors = common._checkMustCalls();
}
if (rejection) {
var errMsg = (rejection && rejection.stack) ? rejection.stack : String(rejection);
if (mustCallErrors.length > 0) {
return "FAIL: Unhandled promise rejection (likely cause of mustCall failure): " + errMsg;
}
return "FAIL: Unhandled promise rejection: " + errMsg;
}
if (mustCallErrors.length > 0) {
return "FAIL: mustCall verification failed:\n" + mustCallErrors.join("\n");
}
return "PASS";
} catch (e) {
if (restorePromise) restorePromise();
if (e && e.__isProcessExit) {
return "PASS";
}
var msg = (e && e.stack) ? e.stack : String(e);
var errorMsg = (e && e.message) ? e.message : String(e);
if (errorMsg.startsWith("SKIP:")) {
return "SKIP: " + errorMsg.slice("SKIP:".length).trim();
}
var fullMsg = (e && e.message) ? (e.message + "\n" + msg) : msg;
return "FAIL: " + fullMsg;
} finally {
if (restoreArgv) {
restoreArgv();
}
}
};