From bc5068e6d63c389850fee1cdaac209741fcb7a2f Mon Sep 17 00:00:00 2001 From: juergba Date: Thu, 9 May 2019 16:17:51 +0200 Subject: [PATCH 1/3] lookupFiles(): fix regression and glob --- lib/utils.js | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 93005cedf2..5b782698e3 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -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 From 10cfd40f30c5ee83a3b33487442182e3cc302e05 Mon Sep 17 00:00:00 2001 From: juergba Date: Thu, 9 May 2019 19:10:02 +0200 Subject: [PATCH 2/3] more glob --- lib/utils.js | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 5b782698e3..013d3ffe0a 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -576,25 +576,27 @@ exports.lookupFiles = function lookupFiles(filepath, extensions, recursive) { var stat; if (!fs.existsSync(filepath)) { - // 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, {nodir: true}); - if (!files.length) { - throw createNoFilesMatchPatternError( - 'Cannot find any files matching pattern ' + exports.dQuote(filepath), - filepath - ); - } - return files; + 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) { + throw createNoFilesMatchPatternError( + 'Cannot find any files matching pattern ' + exports.dQuote(filepath), + filepath + ); } + return files; } // Handle file From 0a7b8308735193653d0741c366c8fc24c5a7dcbd Mon Sep 17 00:00:00 2001 From: juergba Date: Wed, 15 May 2019 11:22:31 +0200 Subject: [PATCH 3/3] tests --- test/integration/file-utils.spec.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) 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);