Skip to content

Commit a723f6b

Browse files
committed
Handle StopException in ShellDispatcher.
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.
1 parent c6f4088 commit a723f6b

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

src/Console/Shell.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,7 @@ public function hr($newlines = 0, $width = 63)
723723
* @return void
724724
* @link http://book.cakephp.org/3.0/en/console-and-shells.html#styling-output
725725
*/
726-
public function abort($message, $exitCode)
726+
public function abort($message, $exitCode = self::CODE_ERROR)
727727
{
728728
$this->_io->err('<error>' . $message . '</error>');
729729
throw new StopException($message, $exitCode);

src/Console/ShellDispatcher.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
namespace Cake\Console;
1616

1717
use Cake\Console\Exception\MissingShellException;
18+
use Cake\Console\Exception\StopException;
1819
use Cake\Core\App;
1920
use Cake\Core\Configure;
2021
use Cake\Core\Exception\Exception;
@@ -176,7 +177,11 @@ protected function _bootstrap()
176177
*/
177178
public function dispatch($extra = [])
178179
{
179-
$result = $this->_dispatch($extra);
180+
try {
181+
$result = $this->_dispatch($extra);
182+
} catch (StopException $e) {
183+
return $e->getCode();
184+
}
180185
if ($result === null || $result === true) {
181186
return 0;
182187
}

tests/TestCase/Console/ShellDispatcherTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,32 @@ public function testFindShellAliasedAppShadow()
148148
$this->assertEquals('Sample', $result->name);
149149
}
150150

151+
/**
152+
* Verify dispatch handling stop errors
153+
*
154+
* @return void
155+
*/
156+
public function testDispatchShellWithAbort()
157+
{
158+
$io = $this->getMock('Cake\Console\ConsoleIo');
159+
$shell = $this->getMock('Cake\Console\Shell', ['main'], [$io]);
160+
$shell->expects($this->once())
161+
->method('main')
162+
->will($this->returnCallback(function () use ($shell) {
163+
$shell->abort('Bad things', 99);
164+
}));
165+
166+
$dispatcher = $this->getMock('Cake\Console\ShellDispatcher', ['findShell']);
167+
$dispatcher->expects($this->any())
168+
->method('findShell')
169+
->with('aborter')
170+
->will($this->returnValue($shell));
171+
172+
$dispatcher->args = ['aborter'];
173+
$result = $dispatcher->dispatch();
174+
$this->assertSame(99, $result, 'Should return the exception error code.');
175+
}
176+
151177
/**
152178
* Verify correct dispatch of Shell subclasses with a main method
153179
*

0 commit comments

Comments
 (0)