Skip to content

Commit

Permalink
lookupFiles(): fix regression and glob
Browse files Browse the repository at this point in the history
  • Loading branch information
juergba committed May 9, 2019
1 parent aed20bd commit c58b911
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -563,35 +563,38 @@ function isHiddenOnUnix(pathname) {
* @public
* @memberof Mocha.utils
* @param {string} filepath - Base path to start searching from.
* @param {string[]} extensions - File extensions to look for.
* @param {boolean} recursive - Whether to recurse into subdirectories.
* @param {string[]} [extensions=[]] - File extensions to look for.
* @param {boolean} [recursive=false] - Whether to recurse into subdirectories.
* @return {string[]} An array of paths.
* @throws {Error} if no files match pattern.
* @throws {TypeError} if `filepath` is directory and `extensions` not provided.
*/
exports.lookupFiles = function lookupFiles(filepath, extensions, recursive) {
extensions = extensions || [];
recursive = recursive || false;
var files = [];
var stat;

if (!fs.existsSync(filepath)) {
// check all extensions
extensions.forEach(function(ext) {
if (fs.existsSync(filepath + '.' + ext)) {
files.push(filepath + '.' + ext);
}
});
if (!files.length) {
// Check all extensions, using first match found (if any)
if (
!extensions.some(function(ext) {
if (fs.existsSync(filepath + '.' + ext)) {
filepath += '.' + ext;
return true;
}
})
) {
// Handle glob
files = glob.sync(filepath);
files = glob.sync(filepath, {nodir: true});
if (!files.length) {
throw createNoFilesMatchPatternError(
'Cannot find any files matching pattern ' + exports.dQuote(filepath),
filepath
);
}
return files;
}
return files;
}

// Handle file
Expand Down

0 comments on commit c58b911

Please sign in to comment.