Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Console] Ensure proper exit code on Exception #8217

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 2 commits 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
6 changes: 1 addition & 5 deletions src/Symfony/Component/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,11 @@ public function run(InputInterface $input = null, OutputInterface $output = null
} else {
$this->renderException($e, $output);
}
$statusCode = $e->getCode();

$statusCode = is_numeric($statusCode) && $statusCode ? (int) $statusCode : 1;
$statusCode = max(1, min(254, intval($e->getCode())));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I already said, this changes behavior and breaks BC considerably.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Provide a failing test case to support your statement. Cuz I have 5 passing ones that only prove mine - behaviour is not changed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So your claim in the description, it does not break bc is wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Tobion prove it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, you really don't see it apparently:
Just set $this->autoExit = false and let $e->getCode() return something greater than 255. Result is different before and after.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The funny thing is, that you even have a test for it. You just need to run it without your changes applied. And you will see it fail...^^

5 passing ones that only prove mine - behaviour is not changed.

so you probably mean the opposite

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Thanks for pointing this one out.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is another bc break and code error:
Let $e->getCode() return an object. You will receive a php notice.

}

if ($this->autoExit) {
if ($statusCode > 255) {
$statusCode = 255;
}
// @codeCoverageIgnoreStart
exit($statusCode);
// @codeCoverageIgnoreEnd
Expand Down
45 changes: 45 additions & 0 deletions src/Symfony/Component/Console/Tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,51 @@ public function testRunReturnsExitCodeOneForExceptionCodeZero()
$this->assertSame(1, $exitCode, '->run() returns exit code 1 when exception code is 0');
}

public function testRunReturnsExitCodeOneForExceptionCodeNegative()
{
$exception = new \Exception('', -20);

$application = $this->getMock('Symfony\Component\Console\Application', array('doRun'));
$application->setAutoExit(false);
$application->expects($this->once())
->method('doRun')
->will($this->throwException($exception));

$exitCode = $application->run(new ArrayInput(array()), new NullOutput());

$this->assertSame(1, $exitCode, '->run() returns exit code 1 when exception code is negative');
}

public function testRunReturnsExitCodeOneForExceptionCodeNonInteger()
{
$exception = new \Exception('', false);

$application = $this->getMock('Symfony\Component\Console\Application', array('doRun'));
$application->setAutoExit(false);
$application->expects($this->once())
->method('doRun')
->will($this->throwException($exception));

$exitCode = $application->run(new ArrayInput(array()), new NullOutput());

$this->assertSame(1, $exitCode, '->run() returns exit code 1 when exception code is not integer');
}

public function testRunReturnsExitCode254ForExceptionCodeHigher254()
{
$exception = new \Exception('', 404);

$application = $this->getMock('Symfony\Component\Console\Application', array('doRun'));
$application->setAutoExit(false);
$application->expects($this->once())
->method('doRun')
->will($this->throwException($exception));

$exitCode = $application->run(new ArrayInput(array()), new NullOutput());

$this->assertSame(254, $exitCode, '->run() returns exit code 254 when exception code is 254+');
}

/**
* @expectedException \LogicException
* @dataProvider getAddingAlreadySetDefinitionElementData
Expand Down