Skip to content

Commit

Permalink
Merge branch '6.4' into 7.1
Browse files Browse the repository at this point in the history
* 6.4:
  fix the constant being used
  fix the path separator being used
  fix the directory separator being used
  ignore case of built-in cmd.exe commands
  [Process] Improve test cleanup by unlinking in a `finally` block
  [Process] Return built-in cmd.exe commands directly in ExecutableFinder
  Re-add missing Profiler shortcuts on Profiler homepage
  • Loading branch information
xabbuh committed Nov 4, 2024
2 parents 1aa37ba + 836d34f commit 284aead
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 16 deletions.
12 changes: 12 additions & 0 deletions ExecutableFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@
class ExecutableFinder
{
private array $suffixes = ['.exe', '.bat', '.cmd', '.com'];
private const CMD_BUILTINS = [
'assoc', 'break', 'call', 'cd', 'chdir', 'cls', 'color', 'copy', 'date',
'del', 'dir', 'echo', 'endlocal', 'erase', 'exit', 'for', 'ftype', 'goto',
'help', 'if', 'label', 'md', 'mkdir', 'mklink', 'move', 'path', 'pause',
'popd', 'prompt', 'pushd', 'rd', 'rem', 'ren', 'rename', 'rmdir', 'set',
'setlocal', 'shift', 'start', 'time', 'title', 'type', 'ver', 'vol',
];

/**
* Replaces default suffixes of executable.
Expand All @@ -46,6 +53,11 @@ public function addSuffix(string $suffix): void
*/
public function find(string $name, ?string $default = null, array $extraDirs = []): ?string
{
// windows built-in commands that are present in cmd.exe should not be resolved using PATH as they do not exist as exes
if ('\\' === \DIRECTORY_SEPARATOR && \in_array(strtolower($name), self::CMD_BUILTINS, true)) {
return $name;
}

$dirs = array_merge(
explode(\PATH_SEPARATOR, getenv('PATH') ?: getenv('Path')),
$extraDirs
Expand Down
48 changes: 32 additions & 16 deletions Tests/ExecutableFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,18 +125,20 @@ public function testFindBatchExecutableOnWindows()

$target = tempnam(sys_get_temp_dir(), 'example-windows-executable');

touch($target);
touch($target.'.BAT');

$this->assertFalse(is_executable($target));
try {
touch($target);
touch($target.'.BAT');

putenv('PATH='.sys_get_temp_dir());
$this->assertFalse(is_executable($target));

$finder = new ExecutableFinder();
$result = $finder->find(basename($target), false);
putenv('PATH='.sys_get_temp_dir());

unlink($target);
unlink($target.'.BAT');
$finder = new ExecutableFinder();
$result = $finder->find(basename($target), false);
} finally {
unlink($target);
unlink($target.'.BAT');
}

$this->assertSamePath($target.'.BAT', $result);
}
Expand All @@ -146,17 +148,31 @@ public function testFindBatchExecutableOnWindows()
*/
public function testEmptyDirInPath()
{
putenv(sprintf('PATH=%s:', \dirname(\PHP_BINARY)));
putenv(sprintf('PATH=%s%s', \dirname(\PHP_BINARY), \PATH_SEPARATOR));

touch('executable');
chmod('executable', 0700);
try {
touch('executable');
chmod('executable', 0700);

$finder = new ExecutableFinder();
$result = $finder->find('executable');
$finder = new ExecutableFinder();
$result = $finder->find('executable');

$this->assertSame('./executable', $result);
$this->assertSame(sprintf('.%sexecutable', \DIRECTORY_SEPARATOR), $result);
} finally {
unlink('executable');
}
}

unlink('executable');
public function testFindBuiltInCommandOnWindows()
{
if ('\\' !== \DIRECTORY_SEPARATOR) {
$this->markTestSkipped('Can be only tested on windows');
}

$finder = new ExecutableFinder();
$this->assertSame('rmdir', strtolower($finder->find('RMDIR')));
$this->assertSame('cd', strtolower($finder->find('cd')));
$this->assertSame('move', strtolower($finder->find('MoVe')));
}

private function assertSamePath($expected, $tested)
Expand Down

0 comments on commit 284aead

Please sign in to comment.