Skip to content

[Process] ExecutableFinder::addSuffix() has no effect #52679

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

Merged
Show file tree
Hide file tree
Changes from all commits
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
21 changes: 16 additions & 5 deletions src/Symfony/Component/Process/ExecutableFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@
*/
class ExecutableFinder
{
private array $suffixes = ['.exe', '.bat', '.cmd', '.com'];
private array $suffixes = [];

public function __construct()
{
// Set common extensions on Windows.
if ('\\' === \DIRECTORY_SEPARATOR) {
$this->suffixes = ['.exe', '.bat', '.cmd', '.com'];
}
}

/**
* Replaces default suffixes of executable.
Expand All @@ -30,7 +38,10 @@ public function setSuffixes(array $suffixes): void
}

/**
* Adds new possible suffix to check for executable.
* Adds new possible suffix to check for executable, including the dot (.).
*
* $finder = new ExecutableFinder();
* $finder->addSuffix('.foo');
*/
public function addSuffix(string $suffix): void
{
Expand All @@ -52,10 +63,10 @@ public function find(string $name, ?string $default = null, array $extraDirs = [
);

$suffixes = [''];
if ('\\' === \DIRECTORY_SEPARATOR) {
$pathExt = getenv('PATHEXT');
$suffixes = array_merge($pathExt ? explode(\PATH_SEPARATOR, $pathExt) : $this->suffixes, $suffixes);
if ('\\' === \DIRECTORY_SEPARATOR && $pathExt = getenv('PATHEXT')) {
$suffixes = array_merge(explode(\PATH_SEPARATOR, $pathExt), $suffixes);
}
$suffixes = array_merge($suffixes, $this->suffixes);
foreach ($suffixes as $suffix) {
foreach ($dirs as $dir) {
if (@is_file($file = $dir.\DIRECTORY_SEPARATOR.$name.$suffix) && ('\\' === \DIRECTORY_SEPARATOR || @is_executable($file))) {
Expand Down
25 changes: 25 additions & 0 deletions src/Symfony/Component/Process/Tests/ExecutableFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,31 @@ public function testFindWithExtraDirs()
$this->assertSamePath(\PHP_BINARY, $result);
}

public function testFindWithoutSuffix()
{
$fixturesDir = __DIR__.\DIRECTORY_SEPARATOR.'Fixtures';
$name = 'executable_without_suffix';

$finder = new ExecutableFinder();
$result = $finder->find($name, null, [$fixturesDir]);

$this->assertSamePath($fixturesDir.\DIRECTORY_SEPARATOR.$name, $result);
}

public function testFindWithAddedSuffixes()
{
$fixturesDir = __DIR__.\DIRECTORY_SEPARATOR.'Fixtures';
$name = 'executable_with_added_suffix';
$suffix = '.foo';

$finder = new ExecutableFinder();
$finder->addSuffix($suffix);

$result = $finder->find($name, null, [$fixturesDir]);

$this->assertSamePath($fixturesDir.\DIRECTORY_SEPARATOR.$name.$suffix, $result);
}

/**
* @runInSeparateProcess
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
See \Symfony\Component\Process\Tests\ExecutableFinderTest::testFindWithAddedSuffixes()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
See \Symfony\Component\Process\Tests\ExecutableFinderTest::testFindWithoutSuffix()