When a process fails, check if the output is enabled

With the recent addition of the ability to disable the output, it was
not taken into account within the `ProcessFailedException`.

So, if the output was indeed disabled, and the process returns an
error (i.e via a `mustRun`) we could have another LogicException which
is not expected.
This commit is contained in:
Baptiste Clavié 2014-03-25 16:03:53 +01:00 committed by Fabien Potencier
parent d6fccdd1df
commit 849703a784
2 changed files with 66 additions and 9 deletions

View File

@ -28,16 +28,20 @@ class ProcessFailedException extends RuntimeException
throw new InvalidArgumentException('Expected a failed process, but the given process was successful.'); throw new InvalidArgumentException('Expected a failed process, but the given process was successful.');
} }
parent::__construct( $error = sprintf('The command "%s" failed.'."\nExit Code: %s(%s)",
sprintf( $process->getCommandLine(),
'The command "%s" failed.'."\nExit Code: %s(%s)\n\nOutput:\n================\n%s\n\nError Output:\n================\n%s", $process->getExitCode(),
$process->getCommandLine(), $process->getExitCodeText()
$process->getExitCode(), );
$process->getExitCodeText(),
if (!$process->isOutputDisabled()) {
$error .= sprintf("\n\nOutput:\n================\n%s\n\nError Output:\n================\n%s",
$process->getOutput(), $process->getOutput(),
$process->getErrorOutput() $process->getErrorOutput()
) );
); }
parent::__construct($error);
$this->process = $process; $this->process = $process;
} }

View File

@ -54,25 +54,33 @@ class ProcessFailedExceptionTest extends \PHPUnit_Framework_TestCase
$process = $this->getMock( $process = $this->getMock(
'Symfony\Component\Process\Process', 'Symfony\Component\Process\Process',
array('isSuccessful', 'getOutput', 'getErrorOutput', 'getExitCode', 'getExitCodeText'), array('isSuccessful', 'getOutput', 'getErrorOutput', 'getExitCode', 'getExitCodeText', 'isOutputDisabled'),
array($cmd) array($cmd)
); );
$process->expects($this->once()) $process->expects($this->once())
->method('isSuccessful') ->method('isSuccessful')
->will($this->returnValue(false)); ->will($this->returnValue(false));
$process->expects($this->once()) $process->expects($this->once())
->method('getOutput') ->method('getOutput')
->will($this->returnValue($output)); ->will($this->returnValue($output));
$process->expects($this->once()) $process->expects($this->once())
->method('getErrorOutput') ->method('getErrorOutput')
->will($this->returnValue($errorOutput)); ->will($this->returnValue($errorOutput));
$process->expects($this->once()) $process->expects($this->once())
->method('getExitCode') ->method('getExitCode')
->will($this->returnValue($exitCode)); ->will($this->returnValue($exitCode));
$process->expects($this->once()) $process->expects($this->once())
->method('getExitCodeText') ->method('getExitCodeText')
->will($this->returnValue($exitText)); ->will($this->returnValue($exitText));
$process->expects($this->once())
->method('isOutputDisabled')
->will($this->returnValue(false));
$exception = new ProcessFailedException($process); $exception = new ProcessFailedException($process);
$this->assertEquals( $this->assertEquals(
@ -80,4 +88,49 @@ class ProcessFailedExceptionTest extends \PHPUnit_Framework_TestCase
$exception->getMessage() $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()
);
}
} }