Skip to content

Commit 46c203f

Browse files
committed
[Process] Return built-in cmd.exe commands directly in ExecutableFinder
1 parent 32dfba3 commit 46c203f

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

ExecutableFinder.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@
2020
class ExecutableFinder
2121
{
2222
private $suffixes = ['.exe', '.bat', '.cmd', '.com'];
23+
private const CMD_BUILTINS = [
24+
'assoc', 'break', 'call', 'cd', 'chdir', 'cls', 'color', 'copy', 'date',
25+
'del', 'dir', 'echo', 'endlocal', 'erase', 'exit', 'for', 'ftype', 'goto',
26+
'help', 'if', 'label', 'md', 'mkdir', 'mklink', 'move', 'path', 'pause',
27+
'popd', 'prompt', 'pushd', 'rd', 'rem', 'ren', 'rename', 'rmdir', 'set',
28+
'setlocal', 'shift', 'start', 'time', 'title', 'type', 'ver', 'vol',
29+
];
2330

2431
/**
2532
* Replaces default suffixes of executable.
@@ -48,6 +55,11 @@ public function addSuffix(string $suffix)
4855
*/
4956
public function find(string $name, ?string $default = null, array $extraDirs = [])
5057
{
58+
// windows built-in commands that are present in cmd.exe should not be resolved using PATH as they do not exist as exes
59+
if ('\\' === \DIRECTORY_SEPARATOR && \in_array(strtolower($name), self::CMD_BUILTINS, true)) {
60+
return $name;
61+
}
62+
5163
$dirs = array_merge(
5264
explode(\PATH_SEPARATOR, getenv('PATH') ?: getenv('Path')),
5365
$extraDirs

Tests/ExecutableFinderTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,18 @@ public function testEmptyDirInPath()
159159
unlink('executable');
160160
}
161161

162+
public function testFindBuiltInCommandOnWindows()
163+
{
164+
if ('\\' !== \DIRECTORY_SEPARATOR) {
165+
$this->markTestSkipped('Can be only tested on windows');
166+
}
167+
168+
$finder = new ExecutableFinder();
169+
$this->assertSame('rmdir', $finder->find('RMDIR'));
170+
$this->assertSame('cd', $finder->find('cd'));
171+
$this->assertSame('move', $finder->find('MoVe'));
172+
}
173+
162174
private function assertSamePath($expected, $tested)
163175
{
164176
if ('\\' === \DIRECTORY_SEPARATOR) {

0 commit comments

Comments
 (0)