The Wayback Machine - https://web.archive.org/web/20201123153126/https://github.com/shelljs/shelljs/pull/848
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

@freitagbr freitagbr commented May 10, 2018

Fixes #789

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

@freitagbr freitagbr added the chore label May 10, 2018
@freitagbr freitagbr requested a review from nfischer May 10, 2018
Copy link
Member

@nfischer nfischer left a comment

LGTM, but please address comments before landing

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

if (require.main !== module) {

This comment has been minimized.

@nfischer

nfischer May 10, 2018
Member

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) {

This comment has been minimized.

@nfischer

nfischer May 10, 2018
Member

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

@freitagbr
Copy link
Contributor Author

@freitagbr freitagbr commented May 11, 2018

@nfischer PTAL

@codecov-io
Copy link

@codecov-io 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

This comment has been minimized.

@nfischer

nfischer May 11, 2018
Member

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.

This comment has been minimized.

@freitagbr

freitagbr May 19, 2018
Author Contributor

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

@freitagbr still lgtm

@freitagbr freitagbr merged commit 93bbf68 into master Jun 27, 2018
4 checks passed
4 checks passed
continuous-integration/appveyor/branch AppVeyor build succeeded
Details
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
continuous-integration/travis-ci/push The Travis CI build passed
Details
@freitagbr freitagbr deleted the no-require-exec branch Jun 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants
You can’t perform that action at this time.