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

Prevent require-ing bin/shjs #848

Merged
merged 3 commits into from Jun 27, 2018
Merged

Prevent require-ing bin/shjs #848

merged 3 commits into from Jun 27, 2018

Conversation

freitagbr
Copy link
Contributor

Fixes #789

This makes it so that bin/shjs cannot be required as a module, and adds a test for this.

Copy link
Member

@nfischer nfischer left a comment

Choose a reason for hiding this comment

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

LGTM, but please address comments before landing

bin/shjs Outdated
console.log();
process.exit(1);
}

if (require.main !== module) {
Copy link
Member

Choose a reason for hiding this comment

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

nit: can you move this up? Also, I assume you moved require('../global') down so that it doesn't pollute the test--this is OK, but you need to add a comment explaining why.

bin/shjs Outdated

if (process.argv.length < 3) {
console.log('ShellJS: missing argument (script name)');
function exit(msg) {
Copy link
Member

Choose a reason for hiding this comment

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

nit: rename this function (it's too easy to confuse this with shell.exit()). Suggestion: exitWithErrorMessage()

@freitagbr
Copy link
Contributor Author

@nfischer PTAL

@codecov-io
Copy link

codecov-io commented May 11, 2018

Codecov Report

Merging #848 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@          Coverage Diff           @@
##           master    #848   +/-   ##
======================================
  Coverage    95.8%   95.8%           
======================================
  Files          34      34           
  Lines        1262    1262           
======================================
  Hits         1209    1209           
  Misses         53      53

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4733a32...2d21cd3. Read the comment docs.

bin/shjs Outdated
exitWithErrorMessage('ShellJS: missing argument (script name)');
}

// we only need ShellJS methods past this point, so require here
Copy link
Member

Choose a reason for hiding this comment

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

Our usual style is to import at the top, not to import immediately before use. If there's a real requirement that we import down here, please document that requirement.

I suspect the requirement is: "we must import this after the require.main check, otherwise we pollute the global namespace as a side effect of the disallow require-ing test case."

If that's the real requirement, then you should move this immediately below the require.main check (and indicate this requirement). Otherwise, please explain why this is the appropriate spot, because I can't figure that out.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct, the require at least needs to be after the require.main check, because the error it throws is catchable, and we don't want any side effects, as you said. I also moved it after the process.argv check, because we still don't need any globals at that point, but the fail condition is exiting. There is less of a benefit to require-ing after this check, so I will move it up.

Copy link
Member

@nfischer nfischer left a comment

Choose a reason for hiding this comment

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

@freitagbr still lgtm

@freitagbr freitagbr merged commit 93bbf68 into master Jun 27, 2018
@freitagbr freitagbr deleted the no-require-exec branch June 27, 2018 07:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants