mirror of
https://github.com/nodejs/node.git
synced 2025-04-29 22:40:57 +00:00

PR-URL: https://github.com/nodejs/node/pull/43420 Refs: https://github.com/nodejs/node/issues/43415 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
57 lines
1.3 KiB
JavaScript
57 lines
1.3 KiB
JavaScript
'use strict';
|
|
const { RegExpPrototypeExec } = primordials;
|
|
const { basename } = require('path');
|
|
const { createDeferredPromise } = require('internal/util');
|
|
const {
|
|
codes: {
|
|
ERR_TEST_FAILURE,
|
|
},
|
|
} = require('internal/errors');
|
|
|
|
const kMultipleCallbackInvocations = 'multipleCallbackInvocations';
|
|
const kSupportedFileExtensions = /\.[cm]?js$/;
|
|
const kTestFilePattern = /((^test(-.+)?)|(.+[.\-_]test))\.[cm]?js$/;
|
|
|
|
function doesPathMatchFilter(p) {
|
|
return RegExpPrototypeExec(kTestFilePattern, basename(p)) !== null;
|
|
}
|
|
|
|
function isSupportedFileType(p) {
|
|
return RegExpPrototypeExec(kSupportedFileExtensions, p) !== null;
|
|
}
|
|
|
|
function createDeferredCallback() {
|
|
let calledCount = 0;
|
|
const { promise, resolve, reject } = createDeferredPromise();
|
|
const cb = (err) => {
|
|
calledCount++;
|
|
|
|
// If the callback is called a second time, let the user know, but
|
|
// don't let them know more than once.
|
|
if (calledCount > 1) {
|
|
if (calledCount === 2) {
|
|
throw new ERR_TEST_FAILURE(
|
|
'callback invoked multiple times',
|
|
kMultipleCallbackInvocations
|
|
);
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
if (err) {
|
|
return reject(err);
|
|
}
|
|
|
|
resolve();
|
|
};
|
|
|
|
return { promise, cb };
|
|
}
|
|
|
|
module.exports = {
|
|
createDeferredCallback,
|
|
doesPathMatchFilter,
|
|
isSupportedFileType,
|
|
};
|