Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix(which): match only executable files (#657)
  • Loading branch information
termosa committed Jul 12, 2018
commit 79b8feb78d9e40b3a134eb3253501d95a0cb36af
11 changes: 10 additions & 1 deletion src/which.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,17 @@ function splitPath(p) {
return p ? p.split(path.delimiter) : [];
}

function isExecutable(pathName) {
try {
fs.accessSync(pathName, fs.constants.X_OK);
} catch (err) {
return false;
}
return true;
}

function checkPath(pathName) {
return fs.existsSync(pathName) && !common.statFollowLinks(pathName).isDirectory();
return fs.existsSync(pathName) && !common.statFollowLinks(pathName).isDirectory() && isExecutable(pathName);
}

//@
Expand Down
1 change: 1 addition & 0 deletions test/resources/which/node
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
text file, not an executable
23 changes: 22 additions & 1 deletion test/which.js
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';

Expand Down Expand Up @@ -69,5 +70,25 @@ 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 pathEnv = process.env.path || process.env.Path || process.env.PATH;
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't look right. This should be process.env.PATH.

Only all-caps $PATH works on unix. On Windows, process.env.path is totally the same as process.env.PATH (and all variations of capitalization of "path").

const extraPath = path.resolve(__dirname, 'resources', 'which');
const matchingFile = path.resolve(extraPath, commandName);

// make sure that file is exists (will throw error otherwise)
fs.statSync(matchingFile);
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 make this t.truthy(fs.existsSync())? We generally prefer assertion failures over exceptions.


process.env.path = process.env.Path = process.env.PATH = extraPath + path.delimiter + pathEnv;
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.not(resultForWhichA[0], matchingFile);
Copy link
Member

Choose a reason for hiding this comment

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

Could you rewrite this as:

t.falsy(resultForWhichA.includes(matchingFile));

Also, I think it makes sense to skip this test on Windows, or to assert the array does contain the value on Windows. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

includes is not handled by Ava and it is not supported in node@5. I would also save [0] index to make sure that order is proper.

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, I thought .includes() was added by babel. Could you instead write this as:

t.is(resultForWhichA.indexOf(matchingFile), -1);

That way we have the correct semantics for the assertion.

});