Skip to content

Commit

Permalink
Merge branch '7.1' into 7.2
Browse files Browse the repository at this point in the history
* 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`
  • Loading branch information
nicolas-grekas committed Nov 4, 2024
2 parents 269b336 + f4fb6b8 commit 8b1744e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 18 deletions.
13 changes: 13 additions & 0 deletions ExecutableFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@
*/
class ExecutableFinder
{
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',
];

private array $suffixes = [];

public function __construct()
Expand Down Expand Up @@ -57,6 +65,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
2 changes: 1 addition & 1 deletion Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -1631,7 +1631,7 @@ private function escapeArgument(?string $argument): string
if (str_contains($argument, "\0")) {
$argument = str_replace("\0", '?', $argument);
}
if (!preg_match('/[\/()%!^"<>&|\s]/', $argument)) {
if (!preg_match('/[()%!^"<>&|\s]/', $argument)) {
return $argument;
}
$argument = preg_replace('/(\\\\+)$/', '$1$1', $argument);
Expand Down
48 changes: 32 additions & 16 deletions Tests/ExecutableFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,18 +150,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 @@ -171,17 +173,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
7 changes: 6 additions & 1 deletion Tests/ProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1469,7 +1469,12 @@ public function testGetCommandLine()
{
$p = new Process(['/usr/bin/php']);

$expected = '\\' === \DIRECTORY_SEPARATOR ? '"/usr/bin/php"' : "'/usr/bin/php'";
$expected = '\\' === \DIRECTORY_SEPARATOR ? '/usr/bin/php' : "'/usr/bin/php'";
$this->assertSame($expected, $p->getCommandLine());

$p = new Process(['cd', '/d']);

$expected = '\\' === \DIRECTORY_SEPARATOR ? 'cd /d' : "'cd' '/d'";
$this->assertSame($expected, $p->getCommandLine());
}

Expand Down

0 comments on commit 8b1744e

Please sign in to comment.