Skip to content

Commit d854b76

Browse files
TravisCardenfabpot
authored andcommitted
[Process] ExecutableFinder::addSuffix() has no effect
1 parent b3508ee commit d854b76

File tree

4 files changed

+43
-5
lines changed

4 files changed

+43
-5
lines changed

src/Symfony/Component/Process/ExecutableFinder.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,15 @@
1919
*/
2020
class ExecutableFinder
2121
{
22-
private array $suffixes = ['.exe', '.bat', '.cmd', '.com'];
22+
private array $suffixes = [];
23+
24+
public function __construct()
25+
{
26+
// Set common extensions on Windows.
27+
if ('\\' === \DIRECTORY_SEPARATOR) {
28+
$this->suffixes = ['.exe', '.bat', '.cmd', '.com'];
29+
}
30+
}
2331

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

3240
/**
33-
* Adds new possible suffix to check for executable.
41+
* Adds new possible suffix to check for executable, including the dot (.).
42+
*
43+
* $finder = new ExecutableFinder();
44+
* $finder->addSuffix('.foo');
3445
*/
3546
public function addSuffix(string $suffix): void
3647
{
@@ -52,10 +63,10 @@ public function find(string $name, ?string $default = null, array $extraDirs = [
5263
);
5364

5465
$suffixes = [''];
55-
if ('\\' === \DIRECTORY_SEPARATOR) {
56-
$pathExt = getenv('PATHEXT');
57-
$suffixes = array_merge($pathExt ? explode(\PATH_SEPARATOR, $pathExt) : $this->suffixes, $suffixes);
66+
if ('\\' === \DIRECTORY_SEPARATOR && $pathExt = getenv('PATHEXT')) {
67+
$suffixes = array_merge(explode(\PATH_SEPARATOR, $pathExt), $suffixes);
5868
}
69+
$suffixes = array_merge($suffixes, $this->suffixes);
5970
foreach ($suffixes as $suffix) {
6071
foreach ($dirs as $dir) {
6172
if (@is_file($file = $dir.\DIRECTORY_SEPARATOR.$name.$suffix) && ('\\' === \DIRECTORY_SEPARATOR || @is_executable($file))) {

src/Symfony/Component/Process/Tests/ExecutableFinderTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,31 @@ public function testFindWithExtraDirs()
8585
$this->assertSamePath(\PHP_BINARY, $result);
8686
}
8787

88+
public function testFindWithoutSuffix()
89+
{
90+
$fixturesDir = __DIR__.\DIRECTORY_SEPARATOR.'Fixtures';
91+
$name = 'executable_without_suffix';
92+
93+
$finder = new ExecutableFinder();
94+
$result = $finder->find($name, null, [$fixturesDir]);
95+
96+
$this->assertSamePath($fixturesDir.\DIRECTORY_SEPARATOR.$name, $result);
97+
}
98+
99+
public function testFindWithAddedSuffixes()
100+
{
101+
$fixturesDir = __DIR__.\DIRECTORY_SEPARATOR.'Fixtures';
102+
$name = 'executable_with_added_suffix';
103+
$suffix = '.foo';
104+
105+
$finder = new ExecutableFinder();
106+
$finder->addSuffix($suffix);
107+
108+
$result = $finder->find($name, null, [$fixturesDir]);
109+
110+
$this->assertSamePath($fixturesDir.\DIRECTORY_SEPARATOR.$name.$suffix, $result);
111+
}
112+
88113
/**
89114
* @runInSeparateProcess
90115
*/
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
See \Symfony\Component\Process\Tests\ExecutableFinderTest::testFindWithAddedSuffixes()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
See \Symfony\Component\Process\Tests\ExecutableFinderTest::testFindWithoutSuffix()

0 commit comments

Comments
 (0)