Skip to content

Commit

Permalink
merged branch Tobion/console-exit-code (PR #8080)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.1 branch.

Discussion
----------

[Console] fix and refactor exit code handling

Split of #8038

Commits
-------

5c317b7 [Console] fix and refactor exit code handling
  • Loading branch information
fabpot committed May 19, 2013
2 parents b31769b + 5c317b7 commit 92399ff
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/Symfony/Component/Console/Application.php
Expand Up @@ -115,7 +115,7 @@ public function run(InputInterface $input = null, OutputInterface $output = null
}
$statusCode = $e->getCode();

$statusCode = is_numeric($statusCode) && $statusCode ? $statusCode : 1;
$statusCode = $statusCode ? (is_numeric($statusCode) ? (int) $statusCode : 1) : 0;
}

if ($this->autoExit) {
Expand Down Expand Up @@ -192,7 +192,7 @@ public function doRun(InputInterface $input, OutputInterface $output)
$statusCode = $command->run($input, $output);
$this->runningCommand = null;

return is_numeric($statusCode) ? $statusCode : 0;
return $statusCode;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Console/Command/Command.php
Expand Up @@ -238,7 +238,7 @@ public function run(InputInterface $input, OutputInterface $output)
$statusCode = $this->execute($input, $output);
}

return is_numeric($statusCode) ? $statusCode : 0;
return is_numeric($statusCode) ? (int) $statusCode : 0;
}

/**
Expand Down
15 changes: 15 additions & 0 deletions src/Symfony/Component/Console/Tests/ApplicationTest.php
Expand Up @@ -501,6 +501,21 @@ public function testRun()
$this->assertSame('called'.PHP_EOL, $tester->getDisplay(), '->run() does not call interact() if -n is passed');
}

public function testRunReturnsIntegerExitCode()
{
$exception = new \Exception('', 4);

$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(4, $exitCode, '->run() returns integer exit code extracted from raised exception');
}

/**
* @expectedException \LogicException
* @dataProvider getAddingAlreadySetDefinitionElementData
Expand Down
14 changes: 14 additions & 0 deletions src/Symfony/Component/Console/Tests/Command/CommandTest.php
Expand Up @@ -221,6 +221,20 @@ public function testRun()
}
}

public function testRunReturnsIntegerExitCode()
{
$command = new \TestCommand();
$exitCode = $command->run(new StringInput(''), new NullOutput());
$this->assertSame(0, $exitCode, '->run() returns integer exit code (treats null as 0)');

$command = $this->getMock('TestCommand', array('execute'));
$command->expects($this->once())
->method('execute')
->will($this->returnValue('2.3'));
$exitCode = $command->run(new StringInput(''), new NullOutput());
$this->assertSame(2, $exitCode, '->run() returns integer exit code (casts numeric to int)');
}

public function testRunReturnsAlwaysInteger()
{
$command = new \TestCommand();
Expand Down

0 comments on commit 92399ff

Please sign in to comment.