From 0783b043d2f392b4b21e77cf0abed8bedcd04cd6 Mon Sep 17 00:00:00 2001 From: John Stevenson Date: Fri, 8 Oct 2021 21:46:07 +0100 Subject: [PATCH] Fix Windows escaping in tests --- .../Test/Downloader/FossilDownloaderTest.php | 6 ------ .../Test/Downloader/GitDownloaderTest.php | 2 +- .../Test/Downloader/HgDownloaderTest.php | 6 ------ .../EventDispatcher/EventDispatcherTest.php | 2 +- tests/Composer/Test/TestCase.php | 21 +++++++++++++++++++ tests/Composer/Test/Util/SvnTest.php | 6 ------ 6 files changed, 23 insertions(+), 20 deletions(-) diff --git a/tests/Composer/Test/Downloader/FossilDownloaderTest.php b/tests/Composer/Test/Downloader/FossilDownloaderTest.php index ab8e0ab71..dedbc89fb 100644 --- a/tests/Composer/Test/Downloader/FossilDownloaderTest.php +++ b/tests/Composer/Test/Downloader/FossilDownloaderTest.php @@ -15,7 +15,6 @@ namespace Composer\Test\Downloader; use Composer\Downloader\FossilDownloader; use Composer\Test\TestCase; use Composer\Util\Filesystem; -use Composer\Util\Platform; class FossilDownloaderTest extends TestCase { @@ -168,9 +167,4 @@ class FossilDownloaderTest extends TestCase $this->assertEquals('source', $downloader->getInstallationSource()); } - - private function getCmd($cmd) - { - return Platform::isWindows() ? strtr($cmd, "'", '"') : $cmd; - } } diff --git a/tests/Composer/Test/Downloader/GitDownloaderTest.php b/tests/Composer/Test/Downloader/GitDownloaderTest.php index cd446a73b..71342f478 100644 --- a/tests/Composer/Test/Downloader/GitDownloaderTest.php +++ b/tests/Composer/Test/Downloader/GitDownloaderTest.php @@ -701,7 +701,7 @@ composer https://github.com/old/url (push) $cmd = str_replace('cd ', 'cd /D ', $cmd); $cmd = str_replace('composerPath', getcwd().'/composerPath', $cmd); - return strtr($cmd, "'", '"'); + return $this->getCmd($cmd); } return $cmd; diff --git a/tests/Composer/Test/Downloader/HgDownloaderTest.php b/tests/Composer/Test/Downloader/HgDownloaderTest.php index 34363d29a..f14502055 100644 --- a/tests/Composer/Test/Downloader/HgDownloaderTest.php +++ b/tests/Composer/Test/Downloader/HgDownloaderTest.php @@ -15,7 +15,6 @@ namespace Composer\Test\Downloader; use Composer\Downloader\HgDownloader; use Composer\Test\TestCase; use Composer\Util\Filesystem; -use Composer\Util\Platform; class HgDownloaderTest extends TestCase { @@ -157,9 +156,4 @@ class HgDownloaderTest extends TestCase $this->assertEquals('source', $downloader->getInstallationSource()); } - - private function getCmd($cmd) - { - return Platform::isWindows() ? strtr($cmd, "'", '"') : $cmd; - } } diff --git a/tests/Composer/Test/EventDispatcher/EventDispatcherTest.php b/tests/Composer/Test/EventDispatcher/EventDispatcherTest.php index f1a7bb1db..f16874948 100644 --- a/tests/Composer/Test/EventDispatcher/EventDispatcherTest.php +++ b/tests/Composer/Test/EventDispatcher/EventDispatcherTest.php @@ -401,7 +401,7 @@ class EventDispatcherTest extends TestCase $dispatcher->dispatch('helloWorld', new CommandEvent('helloWorld', $composer, $io)); $expected = "> helloWorld: @hello World".PHP_EOL. - "> hello: echo Hello " .escapeshellarg('World').PHP_EOL; + "> hello: echo Hello " .$this->getCmd("'World'").PHP_EOL; $this->assertEquals($expected, $io->getOutput()); } diff --git a/tests/Composer/Test/TestCase.php b/tests/Composer/Test/TestCase.php index d43d3d911..6e9477b14 100644 --- a/tests/Composer/Test/TestCase.php +++ b/tests/Composer/Test/TestCase.php @@ -16,6 +16,7 @@ use Composer\Semver\VersionParser; use Composer\Package\AliasPackage; use Composer\Semver\Constraint\Constraint; use Composer\Util\Filesystem; +use Composer\Util\Platform; use Composer\Util\Silencer; use PHPUnit\Framework\TestCase as BaseTestCase; use Symfony\Component\Process\ExecutableFinder; @@ -125,4 +126,24 @@ abstract class TestCase extends BaseTestCase parent::setExpectedException($exception, $message, $code); } } + + /** + * Transforms an escaped non-Windows command to match Windows escaping. + * + * @param string $cmd + * + * @return string The transformed command + */ + protected function getCmd($cmd) + { + if (Platform::isWindows()) { + $cmd = preg_replace_callback("/('[^']*')/", function ($m) { + // Double-quotes are used only when needed + $char = (strpbrk($m[1], " \t^&|<>()") !== false || $m[1] === "''") ? '"' : ''; + return str_replace("'", $char, $m[1]); + }, $cmd); + } + + return $cmd; + } } diff --git a/tests/Composer/Test/Util/SvnTest.php b/tests/Composer/Test/Util/SvnTest.php index ada3818ae..8d38c567b 100644 --- a/tests/Composer/Test/Util/SvnTest.php +++ b/tests/Composer/Test/Util/SvnTest.php @@ -14,7 +14,6 @@ namespace Composer\Test\Util; use Composer\Config; use Composer\IO\NullIO; -use Composer\Util\Platform; use Composer\Util\Svn; use Composer\Test\TestCase; @@ -130,9 +129,4 @@ class SvnTest extends TestCase $this->assertEquals($this->getCmd(" --no-auth-cache --username 'foo' --password 'bar' "), $reflMethod->invoke($svn)); } - - private function getCmd($cmd) - { - return Platform::isWindows() ? strtr($cmd, "'", '"') : $cmd; - } }