Skip to content

Commit 8b1744e

Browse files
Merge branch '7.1' into 7.2
* 7.1: [Validator] Fix 58691 (missing plural-options in serbian language translation) profiler form data collector extart value property if it is setted [Process] Fix escaping /X arguments on Windows 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 [Notifier] Fix test with hard coded date in `SmsboxTransportTest` [Process] Return built-in cmd.exe commands directly in ExecutableFinder Re-add missing Profiler shortcuts on Profiler homepage [Config] Handle Phar absolute path in `FileLocator` [Runtime] Remove unused `SKIPIF` from `dotenv_overload.phpt`
2 parents 269b336 + f4fb6b8 commit 8b1744e

File tree

4 files changed

+52
-18
lines changed

4 files changed

+52
-18
lines changed

ExecutableFinder.php

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

2432
public function __construct()
@@ -57,6 +65,11 @@ public function addSuffix(string $suffix): void
5765
*/
5866
public function find(string $name, ?string $default = null, array $extraDirs = []): ?string
5967
{
68+
// windows built-in commands that are present in cmd.exe should not be resolved using PATH as they do not exist as exes
69+
if ('\\' === \DIRECTORY_SEPARATOR && \in_array(strtolower($name), self::CMD_BUILTINS, true)) {
70+
return $name;
71+
}
72+
6073
$dirs = array_merge(
6174
explode(\PATH_SEPARATOR, getenv('PATH') ?: getenv('Path')),
6275
$extraDirs

Process.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1631,7 +1631,7 @@ private function escapeArgument(?string $argument): string
16311631
if (str_contains($argument, "\0")) {
16321632
$argument = str_replace("\0", '?', $argument);
16331633
}
1634-
if (!preg_match('/[\/()%!^"<>&|\s]/', $argument)) {
1634+
if (!preg_match('/[()%!^"<>&|\s]/', $argument)) {
16351635
return $argument;
16361636
}
16371637
$argument = preg_replace('/(\\\\+)$/', '$1$1', $argument);

Tests/ExecutableFinderTest.php

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -150,18 +150,20 @@ public function testFindBatchExecutableOnWindows()
150150

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

153-
touch($target);
154-
touch($target.'.BAT');
155-
156-
$this->assertFalse(is_executable($target));
153+
try {
154+
touch($target);
155+
touch($target.'.BAT');
157156

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

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

163-
unlink($target);
164-
unlink($target.'.BAT');
161+
$finder = new ExecutableFinder();
162+
$result = $finder->find(basename($target), false);
163+
} finally {
164+
unlink($target);
165+
unlink($target.'.BAT');
166+
}
165167

166168
$this->assertSamePath($target.'.BAT', $result);
167169
}
@@ -171,17 +173,31 @@ public function testFindBatchExecutableOnWindows()
171173
*/
172174
public function testEmptyDirInPath()
173175
{
174-
putenv(sprintf('PATH=%s:', \dirname(\PHP_BINARY)));
176+
putenv(sprintf('PATH=%s%s', \dirname(\PHP_BINARY), \PATH_SEPARATOR));
175177

176-
touch('executable');
177-
chmod('executable', 0700);
178+
try {
179+
touch('executable');
180+
chmod('executable', 0700);
178181

179-
$finder = new ExecutableFinder();
180-
$result = $finder->find('executable');
182+
$finder = new ExecutableFinder();
183+
$result = $finder->find('executable');
181184

182-
$this->assertSame('./executable', $result);
185+
$this->assertSame(sprintf('.%sexecutable', \DIRECTORY_SEPARATOR), $result);
186+
} finally {
187+
unlink('executable');
188+
}
189+
}
183190

184-
unlink('executable');
191+
public function testFindBuiltInCommandOnWindows()
192+
{
193+
if ('\\' !== \DIRECTORY_SEPARATOR) {
194+
$this->markTestSkipped('Can be only tested on windows');
195+
}
196+
197+
$finder = new ExecutableFinder();
198+
$this->assertSame('rmdir', strtolower($finder->find('RMDIR')));
199+
$this->assertSame('cd', strtolower($finder->find('cd')));
200+
$this->assertSame('move', strtolower($finder->find('MoVe')));
185201
}
186202

187203
private function assertSamePath($expected, $tested)

Tests/ProcessTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1469,7 +1469,12 @@ public function testGetCommandLine()
14691469
{
14701470
$p = new Process(['/usr/bin/php']);
14711471

1472-
$expected = '\\' === \DIRECTORY_SEPARATOR ? '"/usr/bin/php"' : "'/usr/bin/php'";
1472+
$expected = '\\' === \DIRECTORY_SEPARATOR ? '/usr/bin/php' : "'/usr/bin/php'";
1473+
$this->assertSame($expected, $p->getCommandLine());
1474+
1475+
$p = new Process(['cd', '/d']);
1476+
1477+
$expected = '\\' === \DIRECTORY_SEPARATOR ? 'cd /d' : "'cd' '/d'";
14731478
$this->assertSame($expected, $p->getCommandLine());
14741479
}
14751480

0 commit comments

Comments
 (0)