diff --git a/src/Symfony/Component/Process/Exception/ProcessFailedException.php b/src/Symfony/Component/Process/Exception/ProcessFailedException.php index 890935933b64..7523a5e9cd4e 100644 --- a/src/Symfony/Component/Process/Exception/ProcessFailedException.php +++ b/src/Symfony/Component/Process/Exception/ProcessFailedException.php @@ -28,16 +28,20 @@ public function __construct(Process $process) throw new InvalidArgumentException('Expected a failed process, but the given process was successful.'); } - parent::__construct( - sprintf( - 'The command "%s" failed.'."\nExit Code: %s(%s)\n\nOutput:\n================\n%s\n\nError Output:\n================\n%s", - $process->getCommandLine(), - $process->getExitCode(), - $process->getExitCodeText(), + $error = sprintf('The command "%s" failed.'."\nExit Code: %s(%s)", + $process->getCommandLine(), + $process->getExitCode(), + $process->getExitCodeText() + ); + + if (!$process->isOutputDisabled()) { + $error .= sprintf("\n\nOutput:\n================\n%s\n\nError Output:\n================\n%s", $process->getOutput(), $process->getErrorOutput() - ) - ); + ); + } + + parent::__construct($error); $this->process = $process; } diff --git a/src/Symfony/Component/Process/Tests/ProcessFailedExceptionTest.php b/src/Symfony/Component/Process/Tests/ProcessFailedExceptionTest.php index 5da55e75ecfd..c48648f51735 100644 --- a/src/Symfony/Component/Process/Tests/ProcessFailedExceptionTest.php +++ b/src/Symfony/Component/Process/Tests/ProcessFailedExceptionTest.php @@ -54,25 +54,33 @@ public function testProcessFailedExceptionPopulatesInformationFromProcessOutput( $process = $this->getMock( 'Symfony\Component\Process\Process', - array('isSuccessful', 'getOutput', 'getErrorOutput', 'getExitCode', 'getExitCodeText'), + array('isSuccessful', 'getOutput', 'getErrorOutput', 'getExitCode', 'getExitCodeText', 'isOutputDisabled'), array($cmd) ); $process->expects($this->once()) ->method('isSuccessful') ->will($this->returnValue(false)); + $process->expects($this->once()) ->method('getOutput') ->will($this->returnValue($output)); + $process->expects($this->once()) ->method('getErrorOutput') ->will($this->returnValue($errorOutput)); + $process->expects($this->once()) ->method('getExitCode') ->will($this->returnValue($exitCode)); + $process->expects($this->once()) ->method('getExitCodeText') ->will($this->returnValue($exitText)); + $process->expects($this->once()) + ->method('isOutputDisabled') + ->will($this->returnValue(false)); + $exception = new ProcessFailedException($process); $this->assertEquals( @@ -80,4 +88,49 @@ public function testProcessFailedExceptionPopulatesInformationFromProcessOutput( $exception->getMessage() ); } + + /** + * Tests that ProcessFailedException does not extract information from + * process output if it was previously disabled + */ + public function testDisabledOutputInFailedExceptionDoesNotPopulateOutput() + { + $cmd = 'php'; + $exitCode = 1; + $exitText = 'General error'; + + $process = $this->getMock( + 'Symfony\Component\Process\Process', + array('isSuccessful', 'isOutputDisabled', 'getExitCode', 'getExitCodeText', 'getOutput', 'getErrorOutput'), + array($cmd) + ); + $process->expects($this->once()) + ->method('isSuccessful') + ->will($this->returnValue(false)); + + $process->expects($this->never()) + ->method('getOutput'); + + $process->expects($this->never()) + ->method('getErrorOutput'); + + $process->expects($this->once()) + ->method('getExitCode') + ->will($this->returnValue($exitCode)); + + $process->expects($this->once()) + ->method('getExitCodeText') + ->will($this->returnValue($exitText)); + + $process->expects($this->once()) + ->method('isOutputDisabled') + ->will($this->returnValue(true)); + + $exception = new ProcessFailedException($process); + + $this->assertEquals( + "The command \"$cmd\" failed.\nExit Code: $exitCode($exitText)", + $exception->getMessage() + ); + } }