From 7c54e6d1020203278b64e39a17ad2775726439fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Mart=C3=ADnez?= Date: Fri, 20 Oct 2017 19:58:11 +0200 Subject: [PATCH] Fix duplicated crontab 2>&1 expression --- .../Framework/Shell/CommandRenderer.php | 2 +- .../Shell/Test/Unit/CommandRendererTest.php | 37 +++++++++++++------ 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/lib/internal/Magento/Framework/Shell/CommandRenderer.php b/lib/internal/Magento/Framework/Shell/CommandRenderer.php index 32e75a7420d1e..6cf91d09479e1 100644 --- a/lib/internal/Magento/Framework/Shell/CommandRenderer.php +++ b/lib/internal/Magento/Framework/Shell/CommandRenderer.php @@ -16,8 +16,8 @@ class CommandRenderer implements CommandRendererInterface */ public function render($command, array $arguments = []) { + $command = preg_replace('/(\s+2>&1)*(\s*\|)|$/', ' 2>&1$2', $command); $arguments = array_map('escapeshellarg', $arguments); - $command = preg_replace('/\s?\||$/', ' 2>&1$0', $command); if (empty($arguments)) { return $command; } diff --git a/lib/internal/Magento/Framework/Shell/Test/Unit/CommandRendererTest.php b/lib/internal/Magento/Framework/Shell/Test/Unit/CommandRendererTest.php index dcc091473eeec..213e2afa2c655 100644 --- a/lib/internal/Magento/Framework/Shell/Test/Unit/CommandRendererTest.php +++ b/lib/internal/Magento/Framework/Shell/Test/Unit/CommandRendererTest.php @@ -5,27 +5,40 @@ */ namespace Magento\Framework\Shell\Test\Unit; -use \Magento\Framework\Shell\CommandRenderer; +use Magento\Framework\Shell\CommandRenderer; class CommandRendererTest extends \PHPUnit\Framework\TestCase { - public function testRender() + /** + * @param $expectedCommand + * @param $actualCommand + * @param $testArguments + * @dataProvider commandsDataProvider + */ + public function testRender($expectedCommand, $actualCommand, $testArguments) { - $testArgument = 'argument'; - $testArgument2 = 'argument2'; $commandRenderer = new CommandRenderer(); $this->assertEquals( - "php -r " . escapeshellarg($testArgument) . " 2>&1 | grep " . escapeshellarg($testArgument2) . " 2>&1", - $commandRenderer->render('php -r %s | grep %s', [$testArgument, $testArgument2]) + $expectedCommand, + $commandRenderer->render($actualCommand, $testArguments) ); } - public function testRenderWithoutArguments() + public function commandsDataProvider() { - $commandRenderer = new CommandRenderer(); - $this->assertEquals( - "php -r %s 2>&1 | grep %s 2>&1", - $commandRenderer->render('php -r %s | grep %s', []) - ); + $testArgument = 'argument'; + $testArgument2 = 'argument2'; + + $expectedCommand = "php -r %s 2>&1 | grep %s 2>&1"; + $expectedCommandArgs = "php -r '" . $testArgument . "' 2>&1 | grep '" . $testArgument2 . "' 2>&1"; + + return [ + [$expectedCommand, 'php -r %s | grep %s', []], + [$expectedCommand, 'php -r %s 2>&1 | grep %s', []], + [$expectedCommand, 'php -r %s 2>&1 2>&1 | grep %s', []], + [$expectedCommandArgs, 'php -r %s | grep %s', [$testArgument, $testArgument2]], + [$expectedCommandArgs, 'php -r %s 2>&1 | grep %s', [$testArgument, $testArgument2]], + [$expectedCommandArgs, 'php -r %s 2>&1 2>&1 | grep %s', [$testArgument, $testArgument2]], + ]; } }