Skip to content

[Process] Do not show output in FailedException if it was disabled #10530

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions src/Symfony/Component/Process/Exception/ProcessFailedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,30 +54,83 @@ 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(
"The command \"$cmd\" failed.\nExit Code: $exitCode($exitText)\n\nOutput:\n================\n{$output}\n\nError Output:\n================\n{$errorOutput}",
$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()
);
}
}