diff --git a/lib/cli/collect-files.js b/lib/cli/collect-files.js index 9ea7e83fd2..c7b84b4945 100644 --- a/lib/cli/collect-files.js +++ b/lib/cli/collect-files.js @@ -6,6 +6,7 @@ const debug = require('debug')('mocha:cli:run:helpers'); const minimatch = require('minimatch'); const {NO_FILES_MATCH_PATTERN} = require('../errors').constants; const lookupFiles = require('./lookup-files'); +const {castArray} = require('../utils'); /** * Exports a function that collects test files from CLI parameters. @@ -21,45 +22,44 @@ const lookupFiles = require('./lookup-files'); * @returns {string[]} List of files to test * @private */ -module.exports = ({ignore, extension, file, recursive, sort, spec} = {}) => { - let files = []; +module.exports = ({ + ignore, + extension, + file: fileArgs, + recursive, + sort, + spec +} = {}) => { const unmatched = []; - spec.forEach(arg => { - let newFiles; + const specFiles = spec.reduce((specFiles, arg) => { try { - newFiles = lookupFiles(arg, extension, recursive); + const moreSpecFiles = castArray(lookupFiles(arg, extension, recursive)) + .filter(filename => + ignore.every(pattern => !minimatch(filename, pattern)) + ) + .map(filename => path.resolve(filename)); + return [...specFiles, ...moreSpecFiles]; } catch (err) { if (err.code === NO_FILES_MATCH_PATTERN) { unmatched.push({message: err.message, pattern: err.pattern}); - return; + return specFiles; } throw err; } - - if (typeof newFiles !== 'undefined') { - if (typeof newFiles === 'string') { - newFiles = [newFiles]; - } - newFiles = newFiles.filter(fileName => - ignore.every(pattern => !minimatch(fileName, pattern)) - ); - } - - files = files.concat(newFiles); - }); - - const fileArgs = file.map(filepath => path.resolve(filepath)); - files = files.map(filepath => path.resolve(filepath)); + }, []); // ensure we don't sort the stuff from fileArgs; order is important! if (sort) { - files.sort(); + specFiles.sort(); } // add files given through --file to be ran first - files = fileArgs.concat(files); - debug('files (in order): ', files); + const files = [ + ...fileArgs.map(filepath => path.resolve(filepath)), + ...specFiles + ]; + debug('test files (in order): ', files); if (!files.length) { // give full message details when only 1 file is missing @@ -70,7 +70,7 @@ module.exports = ({ignore, extension, file, recursive, sort, spec} = {}) => { console.error(ansi.red(noneFoundMsg)); process.exit(1); } else { - // print messages as an warning + // print messages as anwarning unmatched.forEach(warning => { console.warn(ansi.yellow(`Warning: ${warning.message}`)); });