Skip to content

Commit

Permalink
throw error and have handler work with it
Browse files Browse the repository at this point in the history
  • Loading branch information
khoaHyh committed Mar 24, 2024
1 parent a6bdef4 commit 909834d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
28 changes: 13 additions & 15 deletions lib/cli/collect-files.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
'use strict';

const fs = require('fs');
const path = require('path');
const ansi = require('ansi-colors');
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');
const symbols = require('log-symbols');
const {createNoFilesMatchPatternError} = require('../errors');

/**
* Exports a function that collects test files from CLI parameters.
Expand All @@ -31,7 +32,7 @@ module.exports = ({
sort,
spec
} = {}) => {
const unmatched = [];
const unmatchedSpecFiles = [];
const specFiles = spec.reduce((specFiles, arg) => {
try {
const moreSpecFiles = castArray(lookupFiles(arg, extension, recursive))
Expand All @@ -45,7 +46,7 @@ module.exports = ({
return [...specFiles, ...moreSpecFiles];
} catch (err) {
if (err.code === NO_FILES_MATCH_PATTERN) {
unmatched.push({message: err.message, pattern: err.pattern});
unmatchedSpecFiles.push({message: err.message, pattern: err.pattern});
return specFiles;
}

Expand All @@ -56,16 +57,11 @@ module.exports = ({
// check that each file passed in to --file exists
fileArgs.forEach(file => {
const fileAbsolutePath = path.resolve(file);
try {
require.resolve(fileAbsolutePath);
} catch (err) {
// this error doesn't bubble up to the middleware like how
// --require handles it so we log a warning here and exit
const warningMessage = ansi.yellow(
`${symbols.warning} Warning: Cannot find test file "${file}" at the path "${fileAbsolutePath}"`
if (!fs.existsSync(fileAbsolutePath)) {
throw createNoFilesMatchPatternError(
`Cannot find any files matching pattern "${fileAbsolutePath}"`,
fileAbsolutePath
);
console.warn(`${warningMessage}\n`);
process.exit(1);
}
});

Expand All @@ -84,14 +80,16 @@ module.exports = ({
if (!files.length) {
// give full message details when only 1 file is missing
const noneFoundMsg =
unmatched.length === 1
? `Error: No test files found: ${JSON.stringify(unmatched[0].pattern)}` // stringify to print escaped characters raw
unmatchedSpecFiles.length === 1
? `Error: No test files found: ${JSON.stringify(
unmatchedSpecFiles[0].pattern
)}` // stringify to print escaped characters raw
: 'Error: No test files found';
console.error(ansi.red(noneFoundMsg));
process.exit(1);
} else {
// print messages as a warning
unmatched.forEach(warning => {
unmatchedSpecFiles.forEach(warning => {
console.warn(ansi.yellow(`Warning: ${warning.message}`));
});
}
Expand Down
9 changes: 9 additions & 0 deletions lib/cli/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const {
createInvalidArgumentValueError,
createMissingArgumentError
} = require('../errors');
const {NO_FILES_MATCH_PATTERN} = require('../errors').constants;

const {
list,
Expand Down Expand Up @@ -369,6 +370,14 @@ exports.handler = async function (argv) {
try {
await runMocha(mocha, argv);
} catch (err) {
if (err.code === NO_FILES_MATCH_PATTERN) {
console.error(ansi.yellow(`Warning: ${err.message}`));
console.log(
'No test file found with the given pattern, exiting with code 1'
);
process.exit(1);
}

console.error('\n' + (err.stack || `Error: ${err.message || err}`));
process.exit(1);
}
Expand Down
7 changes: 4 additions & 3 deletions test/integration/options/file.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ describe('--file', function () {
return done(err);
}

expect(res.output, 'to contain', 'Warning: Cannot find test file').and(
expect(
res.output,
'to contain',
nonexistentTestFileArg
);
'Warning: Cannot find any files matching pattern'
).and('to contain', nonexistentTestFileArg);
done();
},
{stdio: 'pipe'}
Expand Down

0 comments on commit 909834d

Please sign in to comment.