Skip to content

Commit

Permalink
Allow absolute paths to be given.
Browse files Browse the repository at this point in the history
Both bash and zsh support absolute paths being given to where, so it
only makes sense for this to as well.  When given the absolute path, the
configured paths are ignored and only the single absolute path is
considered.
  • Loading branch information
nubs committed Jun 18, 2014
1 parent 32a7ab9 commit f33f3e7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
21 changes: 19 additions & 2 deletions src/Locator.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ public function locateAll($command)
*/
protected function _getPotentialCommandLocations($command)
{
if ($this->_isAbsoluteCommandPath($command)) {
return array($command);
}

$getCommandPath = function($path) use($command) {
return "{$path}/{$command}";
};
Expand All @@ -108,13 +112,26 @@ protected function _getPaths()
}

/**
* Checks to see if the command (basename) is an allowed name for a command.
* Checks to see if the command is an allowed name for a command.
*
* @param string $command The command name to check.
* @return bool The validity of the command name.
*/
protected function _isValidCommandName($command)
{
return strpos($command, '/') === false && $command !== '.' && $command !== '..';
$isSubdirectory = strpos($command, '/') !== false && !$this->_isAbsoluteCommandPath($command);
$isDotDirectory = $command === '.' || $command === '..';
return !$isSubdirectory && !$isDotDirectory;
}

/**
* Checks to see if the command is an absolute path.
*
* @param string $command The command name to check.
* @return bool True if the command is an absolute path, false otherwise.
*/
protected function _isAbsoluteCommandPath($command)
{
return $command[0] === '/';
}
}
28 changes: 26 additions & 2 deletions tests/LocatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class LocatorTest extends PHPUnit_Framework_TestCase
* @covers ::_getPotentialCommandLocations
* @covers ::_getPaths
* @covers ::_isValidCommandName
* @covers ::_isAbsoluteCommandPath
*/
public function locateSimpleCommand()
{
Expand All @@ -40,6 +41,7 @@ public function locateSimpleCommand()
* @covers ::_getPotentialCommandLocations
* @covers ::_getPaths
* @covers ::_isValidCommandName
* @covers ::_isAbsoluteCommandPath
*/
public function locateNonExecutableCommand()
{
Expand All @@ -52,15 +54,35 @@ public function locateNonExecutableCommand()
}

/**
* Verify that a bad command name returns no result.
* Verify that an absolute path to a command works.
*
* @test
* @covers ::__construct
* @covers ::locate
* @covers ::locateAll
* @covers ::_getPotentialCommandLocations
* @covers ::_getPaths
* @covers ::_isValidCommandName
* @covers ::_isAbsoluteCommandPath
*/
public function locateAbsoluteCommand()
{
$locator = new Locator(array());

$this->assertSame('/usr/bin/env', $locator->locate('/usr/bin/env'));
}

/**
* Verify that a command name that is in a subdirectory returns no result.
*
* @test
* @covers ::__construct
* @covers ::locate
* @covers ::locateAll
* @covers ::_isValidCommandName
* @covers ::_isAbsoluteCommandPath
*/
public function locateBadCommandName()
public function locateSubdirectoryCommand()
{
$locator = new Locator(array());

Expand All @@ -77,6 +99,7 @@ public function locateBadCommandName()
* @covers ::_getPotentialCommandLocations
* @covers ::_getPaths
* @covers ::_isValidCommandName
* @covers ::_isAbsoluteCommandPath
*/
public function locateMultipleLocations()
{
Expand Down Expand Up @@ -104,6 +127,7 @@ public function locateMultipleLocations()
* @covers ::_getPotentialCommandLocations
* @covers ::_getPaths
* @covers ::_isValidCommandName
* @covers ::_isAbsoluteCommandPath
*/
public function locateAll()
{
Expand Down

0 comments on commit f33f3e7

Please sign in to comment.