Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add error handling for nonexistent file case with --file option #5086

Merged
merged 29 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
bd47d79
feat: handle nonexistent files passed to --file
khoaHyh Jan 24, 2024
4aad184
refactor: remove path.resolve()
khoaHyh Jan 24, 2024
8983b84
add comment to new code in collect-files.js
khoaHyh Jan 24, 2024
b6d6dc2
fix: add back absolute path resolving
khoaHyh Jan 24, 2024
9b3d5d2
refactor: log warning and remove call stack
khoaHyh Feb 7, 2024
645e0b0
revert changes to bin/mocha.js
khoaHyh Feb 7, 2024
0e1e84a
improve test case
khoaHyh Feb 7, 2024
9685b82
Merge branch 'master' into issue/4047
khoaHyh Feb 11, 2024
f8aedff
Merge branch 'master' into issue/4047
khoaHyh Feb 21, 2024
a6bdef4
Merge branch 'master' into issue/4047
khoaHyh Mar 24, 2024
909834d
throw error and have handler work with it
khoaHyh Mar 24, 2024
cf69df1
change collectFiles to return object
khoaHyh Mar 25, 2024
b905d21
clean up
khoaHyh Mar 25, 2024
ea092be
exit mocha immediately on missing file
khoaHyh Mar 25, 2024
6d4170e
new log message
khoaHyh Mar 26, 2024
bad240d
add tests
khoaHyh Mar 26, 2024
21e566d
Merge branch 'master' into issue/4047
khoaHyh Mar 26, 2024
dce26f4
Merge branch 'master' into issue/4047
khoaHyh Mar 26, 2024
4c9f318
Merge branch 'master' into issue/4047
khoaHyh Mar 27, 2024
1a9f155
Merge remote-tracking branch 'upstream/master' into issue/4047
khoaHyh May 29, 2024
ff58b19
code quality improvements
khoaHyh Jun 5, 2024
d6f49b9
Merge branch 'master' into issue/4047
khoaHyh Jun 5, 2024
8423875
Merge branch 'master' into issue/4047
khoaHyh Jun 18, 2024
c82c874
add comments
khoaHyh Jun 18, 2024
118fa4c
docs: update to new link name
voxpelli Jun 18, 2024
5c076cd
pass mocha instance to helper function
khoaHyh Jun 18, 2024
3ec3dcd
Merge branch 'master' into issue/4047
khoaHyh Jun 20, 2024
0d6f9c4
Merge branch 'main' into issue/4047
khoaHyh Jun 23, 2024
ffdf2d6
Merge branch 'main' into issue/4047
khoaHyh Jun 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions lib/cli/collect-files.js
Original file line number Diff line number Diff line change
@@ -1,12 +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 {createNoFilesMatchPatternError} = require('../errors');

/**
* Exports a function that collects test files from CLI parameters.
Expand All @@ -30,7 +32,7 @@ module.exports = ({
sort,
spec
} = {}) => {
const unmatched = [];
const unmatchedSpecFiles = [];
khoaHyh marked this conversation as resolved.
Show resolved Hide resolved
const specFiles = spec.reduce((specFiles, arg) => {
try {
const moreSpecFiles = castArray(lookupFiles(arg, extension, recursive))
Expand All @@ -44,14 +46,25 @@ 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;
}

throw err;
}
}, []);

// check that each file passed in to --file exists
fileArgs.forEach(file => {
const fileAbsolutePath = path.resolve(file);
if (!fs.existsSync(fileAbsolutePath)) {
throw createNoFilesMatchPatternError(
`Cannot find any files matching pattern "${fileAbsolutePath}"`,
fileAbsolutePath
);
}
});

// ensure we don't sort the stuff from fileArgs; order is important!
if (sort) {
specFiles.sort();
Expand All @@ -67,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
29 changes: 26 additions & 3 deletions test/integration/options/file.spec.js
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
'use strict';

var path = require('path').posix;
var helpers = require('../helpers');
var runMochaJSON = helpers.runMochaJSON;
var resolvePath = helpers.resolveFixturePath;
const {
runMochaJSON,
resolveFixturePath: resolvePath,
runMocha
} = require('../helpers');

describe('--file', function () {
var args = [];
Expand Down Expand Up @@ -64,4 +66,25 @@ describe('--file', function () {
done();
});
});

it('should log a warning if a nonexistent file is specified', function (done) {
voxpelli marked this conversation as resolved.
Show resolved Hide resolved
const nonexistentTestFileArg = 'nonexistent.test.ts';
runMocha(
nonexistentTestFileArg,
['--file'],
function (err, res) {
if (err) {
return done(err);
}

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