Skip to content

Commit

Permalink
Fix regression/bug in "lookupFiles()" (#3905)
Browse files Browse the repository at this point in the history
  • Loading branch information
juergba committed May 17, 2019
1 parent ffe1967 commit 750c5f8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 16 deletions.
37 changes: 21 additions & 16 deletions lib/utils.js
Expand Up @@ -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;
}
Expand Down
28 changes: 28 additions & 0 deletions test/integration/file-utils.spec.js
Expand Up @@ -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);
Expand Down

0 comments on commit 750c5f8

Please sign in to comment.