From 209799b18eb47821c7122f98af869e83bac9b82f Mon Sep 17 00:00:00 2001 From: Romain Neutron Date: Tue, 23 Apr 2013 22:48:37 +0200 Subject: [PATCH 1/2] [Process] Use new ProcessUtils::escapeArgument to escape ProcessBuilder prefix --- src/Symfony/Component/Process/ProcessBuilder.php | 4 ++-- .../Component/Process/Tests/ProcessBuilderTest.php | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Process/ProcessBuilder.php b/src/Symfony/Component/Process/ProcessBuilder.php index 23d98b504f..1a7e579e38 100644 --- a/src/Symfony/Component/Process/ProcessBuilder.php +++ b/src/Symfony/Component/Process/ProcessBuilder.php @@ -160,8 +160,8 @@ class ProcessBuilder $options = $this->options; - $script = ($this->prefix ? escapeshellarg($this->prefix) . ' ' : '') - .implode(' ', array_map(array(__NAMESPACE__.'\\ProcessUtils', 'escapeArgument'), $this->arguments)); + $arguments = $this->prefix ? array_merge(array($this->prefix), $this->arguments) : $this->arguments; + $script = implode(' ', array_map(array(__NAMESPACE__.'\\ProcessUtils', 'escapeArgument'), $arguments)); if ($this->inheritEnv) { $env = $this->env ? $this->env + $_ENV : null; diff --git a/src/Symfony/Component/Process/Tests/ProcessBuilderTest.php b/src/Symfony/Component/Process/Tests/ProcessBuilderTest.php index e585eb411c..6db0799d38 100644 --- a/src/Symfony/Component/Process/Tests/ProcessBuilderTest.php +++ b/src/Symfony/Component/Process/Tests/ProcessBuilderTest.php @@ -139,4 +139,17 @@ class ProcessBuilderTest extends \PHPUnit_Framework_TestCase $this->assertSame("'%path%' 'foo \" bar'", $proc->getCommandLine()); } } + + public function testShouldEscapeArgumentsAndPrefix() + { + $pb = new ProcessBuilder(array('arg')); + $pb->setPrefix('%prefix%'); + $proc = $pb->getProcess(); + + if (defined('PHP_WINDOWS_VERSION_BUILD')) { + $this->assertSame('^%"prefix"^% "arg"', $proc->getCommandLine()); + } else { + $this->assertSame("'%prefix%' 'arg'", $proc->getCommandLine()); + } + } } From 1de29b9a7b5d087cbc434dccc7dc8e2f82adfcbd Mon Sep 17 00:00:00 2001 From: Romain Neutron Date: Tue, 23 Apr 2013 22:53:10 +0200 Subject: [PATCH 2/2] [Process] Do not throw LogicException in ProcessBuilder::getProcess if no arguments are set but a prefix is --- .../Component/Process/ProcessBuilder.php | 2 +- .../Process/Tests/ProcessBuilderTest.php | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Process/ProcessBuilder.php b/src/Symfony/Component/Process/ProcessBuilder.php index 1a7e579e38..ddd064a2b8 100644 --- a/src/Symfony/Component/Process/ProcessBuilder.php +++ b/src/Symfony/Component/Process/ProcessBuilder.php @@ -154,7 +154,7 @@ class ProcessBuilder public function getProcess() { - if (!count($this->arguments)) { + if (!$this->prefix && !count($this->arguments)) { throw new LogicException('You must add() command arguments before calling getProcess().'); } diff --git a/src/Symfony/Component/Process/Tests/ProcessBuilderTest.php b/src/Symfony/Component/Process/Tests/ProcessBuilderTest.php index 6db0799d38..eea028a406 100644 --- a/src/Symfony/Component/Process/Tests/ProcessBuilderTest.php +++ b/src/Symfony/Component/Process/Tests/ProcessBuilderTest.php @@ -152,4 +152,29 @@ class ProcessBuilderTest extends \PHPUnit_Framework_TestCase $this->assertSame("'%prefix%' 'arg'", $proc->getCommandLine()); } } + + /** + * @expectedException \Symfony\Component\Process\Exception\LogicException + */ + public function testShouldThrowALogicExceptionIfNoPrefixAndNoArgument() + { + ProcessBuilder::create()->getProcess(); + } + + public function testShouldNotThrowALogicExceptionIfNoArgument() + { + $process = ProcessBuilder::create() + ->setPrefix('/usr/bin/php') + ->getProcess(); + + $this->assertEquals("'/usr/bin/php'", $process->getCommandLine()); + } + + public function testShouldNotThrowALogicExceptionIfNoPrefix() + { + $process = ProcessBuilder::create(array('/usr/bin/php')) + ->getProcess(); + + $this->assertEquals("'/usr/bin/php'", $process->getCommandLine()); + } }