Skip to content

Commit

Permalink
Handle StopException in ShellDispatcher.
Browse files Browse the repository at this point in the history
The ShellDispatcher should drop exception messages, and use the
exception code as the exit code. Also add a default error code of
1 which will be non-zero making shell environments behave correctly.
  • Loading branch information
markstory committed Dec 29, 2015
1 parent c6f4088 commit a723f6b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Console/Shell.php
Expand Up @@ -723,7 +723,7 @@ public function hr($newlines = 0, $width = 63)
* @return void
* @link http://book.cakephp.org/3.0/en/console-and-shells.html#styling-output
*/
public function abort($message, $exitCode)
public function abort($message, $exitCode = self::CODE_ERROR)
{
$this->_io->err('<error>' . $message . '</error>');
throw new StopException($message, $exitCode);
Expand Down
7 changes: 6 additions & 1 deletion src/Console/ShellDispatcher.php
Expand Up @@ -15,6 +15,7 @@
namespace Cake\Console;

use Cake\Console\Exception\MissingShellException;
use Cake\Console\Exception\StopException;
use Cake\Core\App;
use Cake\Core\Configure;
use Cake\Core\Exception\Exception;
Expand Down Expand Up @@ -176,7 +177,11 @@ protected function _bootstrap()
*/
public function dispatch($extra = [])
{
$result = $this->_dispatch($extra);
try {
$result = $this->_dispatch($extra);
} catch (StopException $e) {
return $e->getCode();
}
if ($result === null || $result === true) {
return 0;
}
Expand Down
26 changes: 26 additions & 0 deletions tests/TestCase/Console/ShellDispatcherTest.php
Expand Up @@ -148,6 +148,32 @@ public function testFindShellAliasedAppShadow()
$this->assertEquals('Sample', $result->name);
}

/**
* Verify dispatch handling stop errors
*
* @return void
*/
public function testDispatchShellWithAbort()
{
$io = $this->getMock('Cake\Console\ConsoleIo');
$shell = $this->getMock('Cake\Console\Shell', ['main'], [$io]);
$shell->expects($this->once())
->method('main')
->will($this->returnCallback(function () use ($shell) {
$shell->abort('Bad things', 99);
}));

$dispatcher = $this->getMock('Cake\Console\ShellDispatcher', ['findShell']);
$dispatcher->expects($this->any())
->method('findShell')
->with('aborter')
->will($this->returnValue($shell));

$dispatcher->args = ['aborter'];
$result = $dispatcher->dispatch();
$this->assertSame(99, $result, 'Should return the exception error code.');
}

/**
* Verify correct dispatch of Shell subclasses with a main method
*
Expand Down

0 comments on commit a723f6b

Please sign in to comment.