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(which): match only executable files (#657) #874
Merged
nfischer
merged 4 commits into
shelljs:master
from
termosa:b/657/which-finds-executables
Jul 18, 2018
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
79b8feb
fix(which): match only executable files (#657)
termosa 644e632
fix(which): replace constant to support earlier versions of NodeJS (#…
termosa 9157b53
fix(which): address @nfischer comments (#657)
termosa ce70542
fix(which): use indexOf (#657)
termosa File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
text file, not an executable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
import test from 'ava'; | ||
|
||
|
@@ -69,5 +70,27 @@ test('Searching with -a flag returns an array with first item equals to the regu | |
t.falsy(shell.error()); | ||
t.truthy(resultForWhich); | ||
t.truthy(resultForWhichA); | ||
t.is(resultForWhich.toString(), resultForWhichA[0].toString()); | ||
t.is(resultForWhich.toString(), resultForWhichA[0]); | ||
}); | ||
|
||
test('None executable files does not appear in the result list', t => { | ||
const commandName = 'node'; // Should be an existing command | ||
const extraPath = path.resolve(__dirname, 'resources', 'which'); | ||
const matchingFile = path.resolve(extraPath, commandName); | ||
const pathEnv = process.env.PATH; | ||
|
||
// make sure that file is exists (will throw error otherwise) | ||
t.truthy(fs.existsSync(matchingFile)); | ||
|
||
process.env.PATH = extraPath + path.delimiter + process.env.PATH; | ||
const resultForWhich = shell.which(commandName); | ||
const resultForWhichA = shell.which('-a', commandName); | ||
t.falsy(shell.error()); | ||
t.truthy(resultForWhich); | ||
t.truthy(resultForWhichA); | ||
t.truthy(resultForWhichA.length); | ||
t.not(resultForWhich.toString(), matchingFile); | ||
t.is(resultForWhichA.indexOf(matchingFile), -1); | ||
|
||
process.env.PATH = pathEnv; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a TODO to use the node-provided constant on v6+? We'll eventually drop v4/5.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, I'm trusting that (1) this value is respected on v4, even though the constant does not exist, and (2) the value is treated the same as
F_OK
on Windows, just asX_OK
is documented:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True, on Windows
X_OK
andF_OK
are equivalent. But on Windows platform file extensions are used to determine if file is executable or not.I will not do this check for Windows platform.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good