Skip to content

Commit

Permalink
refactor collect-files to be a little more simple
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher Hiller <boneskull@boneskull.com>
  • Loading branch information
boneskull committed Nov 4, 2020
1 parent 185cada commit d1781b3
Showing 1 changed file with 25 additions and 25 deletions.
50 changes: 25 additions & 25 deletions lib/cli/collect-files.js
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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}`));
});
Expand Down

0 comments on commit d1781b3

Please sign in to comment.