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

Replace process.exit() with throwing an error #233

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

evantahler
Copy link

@evantahler evantahler commented Mar 16, 2020

Using process.exit() does not allow us to use this tool programmatically, as we cannot then handle the error in a custom way (ie: formatting the response for CI, logging it somewhere, etc).

This PR replaces console.error + using process.exit with throwing an error. This will have the same effect in CLI usage by immediately terminating the process with a message, but can also be try/catch handled in a parent program.

A downside might be that the error output is less terse... but it will be more noticeable!

Using process.exit() does not allow us to use this tool programmatically, as we cannot then handle the error in a custom way (ie: formatting the response for CI, logging it, somewhere, etc). 

This PR replaces logging to `console.error` + using `process.exit` with throwing an error.  This will have the same effect in CLI usage by immediately terminating the process with a message, but can also be `try/catch` handled in a parent program.
@evantahler evantahler changed the title replace process.exit() with throwing an error Replace process.exit() with throwing an error Mar 16, 2020
Copy link

@mojoaxel mojoaxel left a comment

Choose a reason for hiding this comment

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

The tests need to be adjusted for this!

@evantahler
Copy link
Author

Can you give me a hint where to look? The test seem to be OK on this PR...

@evantahler
Copy link
Author

I imagine those tests are still passing because a throw that isn't caught will exit the process with 1. So... perhaps this is OK?

@mojoaxel
Copy link

mojoaxel commented Apr 7, 2021

I imagine those tests are still passing because a throw that isn't caught will exit the process with 1. So... perhaps this is OK?

I guess you are right, at least for the "bin/license-checker" tests.
The parseAndFailOn function should be adopted to the new error handling IMHO.

@evantahler
Copy link
Author

I'm sorry, but I still don't think I follow what you are asking for.

I'm looking at this example:

    describe('should exit on given list of onlyAllow licenses', function() {
        var result={};
        before(parseAndFailOn('onlyAllow', '../', "MIT; ISC", result));

        it('should exit on non MIT and ISC licensed modules from results', function() {
            assert.equal(result.exitCode, 1);
        });
    });

from (https://github.com/davglass/license-checker/blob/master/tests/test.js#L233-L240)

The test runs parseAndFailOn() and then asserts that the process exited with code 1 when being passed those license names - and it still does.

@evantahler
Copy link
Author

evantahler commented Apr 7, 2021

OH! the test suite is not passing with these changes, even though travis reports success. I see the problem now :D

@mojoaxel
Copy link

mojoaxel commented Apr 7, 2021

The function parseAndFailOn explicitly checks if process.exit is called. IMHO this function should be changed to check if an error is thrown instead:

https://github.com/davglass/license-checker/blob/master/tests/test.js#L215-L231

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.

2 participants