From 750c5f81625f4f67d0d3a4602e344dac8663ee07 Mon Sep 17 00:00:00 2001 From: Juerg B <44573692+juergba@users.noreply.github.com> Date: Fri, 17 May 2019 15:39:33 +0200 Subject: [PATCH] Fix regression/bug in "lookupFiles()" (#3905) --- lib/utils.js | 37 ++++++++++++++++------------- test/integration/file-utils.spec.js | 28 ++++++++++++++++++++++ 2 files changed, 49 insertions(+), 16 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 93005cedf2..013d3ffe0a 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -563,33 +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); - } - }); + var pattern; + if (glob.hasMagic(filepath)) { + // Handle glob as is without extensions + pattern = filepath; + } else { + // glob pattern e.g. 'filepath+(.js|.ts)' + var strExtensions = extensions + .map(function(v) { + return '.' + v; + }) + .join('|'); + pattern = filepath + '+(' + strExtensions + ')'; + } + files = glob.sync(pattern, {nodir: true}); if (!files.length) { - // Handle glob - files = glob.sync(filepath); - if (!files.length) { - throw createNoFilesMatchPatternError( - 'Cannot find any files matching pattern ' + exports.dQuote(filepath), - filepath - ); - } + throw createNoFilesMatchPatternError( + 'Cannot find any files matching pattern ' + exports.dQuote(filepath), + filepath + ); } return files; } diff --git a/test/integration/file-utils.spec.js b/test/integration/file-utils.spec.js index f09018871b..ae97b996e2 100644 --- a/test/integration/file-utils.spec.js +++ b/test/integration/file-utils.spec.js @@ -66,6 +66,34 @@ describe('file utils', function() { expect(res, 'to contain', nonJsFile).and('to have length', 1); }); + it('should return only the ".js" file', function() { + var TsFile = tmpFile('mocha-utils.ts'); + fs.writeFileSync(TsFile, 'yippy skippy ying yang yow'); + + var res = utils + .lookupFiles(tmpFile('mocha-utils'), ['js'], false) + .map(path.normalize.bind(path)); + expect(res, 'to contain', tmpFile('mocha-utils.js')).and( + 'to have length', + 1 + ); + }); + + it('should return ".js" and ".ts" files', function() { + var TsFile = tmpFile('mocha-utils.ts'); + fs.writeFileSync(TsFile, 'yippy skippy ying yang yow'); + + var res = utils + .lookupFiles(tmpFile('mocha-utils'), ['js', 'ts'], false) + .map(path.normalize.bind(path)); + expect( + res, + 'to contain', + tmpFile('mocha-utils.js'), + tmpFile('mocha-utils.ts') + ).and('to have length', 2); + }); + it('should require the extensions parameter when looking up a file', function() { var dirLookup = function() { return utils.lookupFiles(tmpFile('mocha-utils'), undefined, false);