Skip to content

Commit

Permalink
[Process] Return built-in cmd.exe commands directly in ExecutableFinder
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Nov 2, 2024
1 parent 32dfba3 commit 46c203f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 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 $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 Down Expand Up @@ -48,6 +55,11 @@ public function addSuffix(string $suffix)
*/
public function find(string $name, ?string $default = null, array $extraDirs = [])
{
// 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
12 changes: 12 additions & 0 deletions Tests/ExecutableFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,18 @@ public function testEmptyDirInPath()
unlink('executable');
}

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

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

private function assertSamePath($expected, $tested)
{
if ('\\' === \DIRECTORY_SEPARATOR) {
Expand Down

0 comments on commit 46c203f

Please sign in to comment.