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

Adding an existence test to files pass to mocha with --file flag (closes #4047) #4167

Closed
wants to merge 10 commits into from
Closed

Adding an existence test to files pass to mocha with --file flag (closes #4047) #4167

wants to merge 10 commits into from

Conversation

ntedgi
Copy link

@ntedgi ntedgi commented Jan 27, 2020

as explained in #4047

Description of the Change

Adding an existence test to files pass to mocha with --file flag
If specify non-existed file with --file option, mocha throw exception.

Screenshot from 2020-01-28 09-47-26

instead of showing the stack trace

./bin/mocha --file non-existed
/Users/mocha/node_modules/yargs/yargs.js:1163
      else throw err
           ^

Error: Cannot find module '/Users/mocha/non-existed'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:582:15)
    at Function.Module._load (internal/modules/cjs/loader.js:508:25)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at /Users/mocha/lib/mocha.js:309:36
    at Array.forEach (<anonymous>)
    at Mocha.loadFiles (/Users/mocha/lib/mocha.js:306:14)
    at Mocha.run (/Users/mocha/lib/mocha.js:784:10)
    at Object.exports.singleRun (/Users/mocha/lib/cli/run-helpers.js:108:16)
    at exports.runMocha (/Users/mocha/lib/cli/run-helpers.js:143:13)
    at Object.exports.handler.argv [as handler] (/Users/mocha/lib/cli/run.js:305:3)
    at Object.runCommand (/Users/mocha/node_modules/yargs/lib/command.js:242:26)
    at Object.parseArgs [as _parseArgs] (/Users/mocha/node_modules/yargs/yargs.js:1104:24)
    at Object.parse (/Users/mocha/node_modules/yargs/yargs.js:566:25)
    at Object.exports.main (/Users/mocha/lib/cli/cli.js:68:6)
    at Object.<anonymous> (/Users/mocha/bin/mocha:131:29)
    at Module._compile (internal/modules/cjs/loader.js:701:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
    at Module.load (internal/modules/cjs/loader.js:600:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
    at Function.Module._load (internal/modules/cjs/loader.js:531:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

Benefits

more friendly an informative way to terminate the process .

@jsf-clabot
Copy link

jsf-clabot commented Jan 27, 2020

CLA assistant check
All committers have signed the CLA.

@ntedgi ntedgi changed the title adding minor changes wip:adding minor changes Jan 27, 2020
@coveralls
Copy link

coveralls commented Jan 28, 2020

Coverage Status

Coverage increased (+0.07%) to 92.927% when pulling c059efc on ntedgi:naort/issue/4047 into d0f04e9 on mochajs:master.

@ntedgi ntedgi changed the title wip:adding minor changes wip: Adding an existence test to files pass to mocha with --file flag Jan 28, 2020
@ntedgi ntedgi changed the title wip: Adding an existence test to files pass to mocha with --file flag Adding an existence test to files pass to mocha with --file flag Jan 28, 2020
lib/cli/run.js Outdated Show resolved Hide resolved
@ntedgi ntedgi changed the title Adding an existence test to files pass to mocha with --file flag Adding an existence test to files pass to mocha with --file flag (closes #4047) Feb 2, 2020
Copy link
Member

@boneskull boneskull left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this!

I agree that it's currently not a great user experience. However, attempting to check the file before trying to require it is a lot of complexity. Can you change the code so, instead, there's a try/catch/rethrow around this line:

suite.emit(EVENT_FILE_REQUIRE, require(file), file, self);

@@ -270,6 +273,19 @@ exports.builder = yargs =>
}
});

if (argv.file) {
const nonExistentFiles = argv.file.filter(
file => !fs.existsSync(file) || !fs.statSync(file).isFile()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just because the file exists doesn't mean we have permission to stat it. likewise, this will fail on symlinks.

@ntedgi ntedgi closed this Jan 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants